30 end |
30 end |
31 end); |
31 end); |
32 end |
32 end |
33 |
33 |
34 function handle_xkcd_command(command) |
34 function handle_xkcd_command(command) |
35 local xkcdnum = command.param; |
35 local xkcdnum = command.param; |
36 if not xkcdnum then |
36 if not xkcdnum then |
37 xkcdnum = #xkcd_list; |
37 xkcdnum = #xkcd_list; |
|
38 elseif not tonumber(xkcdnum) then -- Search for an xkcd |
|
39 local xkcdname = xkcdnum:lower(); |
|
40 if xkcd_list[xkcdname] then |
|
41 xkcdnum = xkcd_list[xkcdname]; |
|
42 local xkcd = xkcd_list[xkcdnum]; |
|
43 return xkcd..", http://xkcd.org/"..xkcdnum.."/"; |
38 end |
44 end |
39 if not tonumber(xkcdnum) then -- Search for an xkcd |
45 |
40 xkcdnum = xkcdnum:lower() |
46 local xkcdpat = xkcdname:gsub("[-()%[]", "%%%0") |
41 local xkcdpat = xkcdnum:gsub("[()]", function(s) return "%" .. s end) |
47 :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end); |
42 :gsub("[%[]",function(s) return "%" .. s end) |
48 local results = {}; |
43 :gsub("%%(%b[])",function(s) return (#s > 2 and "" or "%") .. s end); |
49 |
44 local results = {}; |
50 for i, xkcd in pairs(xkcd_list) do |
45 for x, xkcd in pairs(xkcd_list) do |
51 if type(i) == "number" and xkcd:lower():match(xkcdpat) then |
46 name = xkcd:lower() |
52 results[#results+1] = i; |
47 if name == xkcdnum then -- exact match |
53 end |
48 return xkcd..", http://xkcd.org/"..x.."/"; |
54 end |
49 elseif name:match(xkcdpat) then |
55 |
50 table.insert(results, x); |
56 if #results == 0 then |
51 --return commands.xkcd(msg, x); |
57 return "Sorry, I couldn't find a match"; |
52 end |
58 elseif #results == 1 then |
53 end |
59 xkcdnum = results[1]; |
54 if #results == 0 then |
60 else |
55 return "Sorry, I couldn't find a match"; |
61 -- We have more than one match |
56 elseif #results == 1 then |
62 local ret = "Multiple matches:"; |
57 command.param = results[1]; |
63 for i, xkcdnum in ipairs(results) do |
58 return handle_xkcd_command(command); |
64 local xkcd = xkcd_list[xkcdnum]; |
59 else |
65 ret = string.format("%s %s (%d)%s", ret, xkcd, xkcdnum, ((i < #results) and ",") or ""); |
60 -- We have more than one match |
66 if i > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end |
61 local ret = "Multiple matches:"; |
67 end |
62 for _, x in ipairs(results) do |
68 return ret; |
63 local xkcdnum = tostring(tonumber(x)); |
69 end |
64 local xkcd = xkcd_list[tostring(x)]; |
70 end |
65 ret = string.format("%s %s%s", ret, xkcd, ((_ < #results) and ",") or ""); |
71 -- Check that xkcdnum is a valid number |
66 if _ > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end |
72 xkcdnum = tonumber(xkcdnum); |
67 end |
73 local xkcd = xkcd_list[xkcdnum]; |
68 return ret; |
74 if not xkcd then return "Sorry, I don't think there is a XKCD #"..xkcdnum; end |
69 end |
75 return xkcd..", http://xkcd.org/"..xkcdnum.."/"; |
70 end |
|
71 -- Check that xkcdnum is a valid number |
|
72 xkcdnum = tostring(tonumber(xkcdnum)); |
|
73 if not xkcdnum then return "What XKCD strip number? Or enter a search string."; end |
|
74 xkcd = xkcd_list[xkcdnum]; |
|
75 if not xkcd then return "Sorry, I don't think there is a XKCD #"..xkcdnum; end |
|
76 return xkcd..", http://xkcd.org/"..xkcdnum.."/"; |
|
77 end |
76 end |
78 |
77 |
79 function parse_xkcd_list(t) |
78 function parse_xkcd_list(t) |
80 if not t then return nil; end |
79 if not t then return nil; end |
81 for number, name in string.gmatch(t,"<a [^>]*href=\"/(%d+)/\"[^>]*>([^<]+)") do |
80 for number, name in string.gmatch(t,"<a [^>]*href=\"/(%d+)/\"[^>]*>([^<]+)") do |
82 xkcd_list[number] = name; |
81 number = tonumber(number); |
83 local number = tonumber(number); |
|
84 if number then |
82 if number then |
|
83 xkcd_list[name:lower()] = number; |
85 xkcd_list[number] = name; |
84 xkcd_list[number] = name; |
86 end |
85 end |
87 end |
86 end |
88 return true; |
87 return true; |
89 end |
88 end |