plugins/xeps.lua

Mon, 21 Jun 2010 14:39:19 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 21 Jun 2010 14:39:19 +0100
changeset 27
2c137706d42c
child 32
7e79b03f10e1
permissions
-rw-r--r--

plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number

27
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local parse_xeps, xeps_updated_at;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local xeps = {};
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 function riddim.plugins.xeps(bot)
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 bot.stream:add_plugin("http");
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local http = verse.http;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 bot:hook("commands/xep", function(command)
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 -- Cache XEP list for an hour
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 if os.difftime(os.time(), xeps_updated_at) > (60 * 60) then -- Not refreshed within 1 hour
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 http.request('http://xmpp.org/extensions/xeps.xml', nil, function (data, code)
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 if code ~= 200 then
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if code > 0 then
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 command:reply("Received HTTP "..code.." error trying to fetch the XEP list");
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 else
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 command:reply("Unable to fetch the XEP list from xmpp.org: "..data:gsub("%-", " "));
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 xeps_updated_at = os.time();
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 parse_xeps(data);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 command:reply(handle_xep_command(command));
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 end);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 else
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return handle_xep_command(command);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 end);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 function handle_xep_command(command)
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 local xepnum = command.param;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if not xepnum then return "Please supply an XEP number or a search string :)"; end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if not tonumber(xepnum) then -- Search for an XEP
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 if xepnum:match("^%d+ ex") then
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local num, example = xepnum:match("^(%d+) ex%S* (%d+)$");
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return "http://xmpp.org/extensions/xep-"..string.rep("0", 4-num:len())..num..".html#example-"..tostring(example);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 local results = {};
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 for x, xep in pairs(xeps) do
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 name = " "..xep.name:lower().." ";
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if name:match(xepnum:lower():gsub("%-", "%%-")) then
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 table.insert(results, x);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 --return commands.xep(msg, x);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if #results == 0 then
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 return "Sorry, I couldn't find a match";
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 elseif #results == 1 then
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 command.param = results[1];
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return handle_xep_command(command);
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 else
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 -- We have more than one match
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local ret = "Multiple matches:";
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 for _, x in ipairs(results) do
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local xepnum = tostring(tonumber(x));
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 xepnum = string.rep("0", 4-x:len())..x;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local xep = xeps[tostring(x)];
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 ret = string.format("%s XEP-%s: %s%s", ret, xep.number, xep.name, ((_ < #results) and ",") or "");
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 return ret;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 -- Check that xepnum is a valid number
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 xepnum = tostring(tonumber(xepnum));
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 if not xepnum then return "What XEP? or enter a search string."; end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 -- Expand to full 4 char number
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 xepnum = string.rep("0", 4-xepnum:len())..xepnum;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 xep = xeps[tostring(xepnum)];
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 if not xep then return "Sorry, I don't think there is a XEP-"..xepnum; end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 return "XEP-"..xep.number..": "..xep.name.." is "..xep.type.." ("..xep.status..", "..xep.updated..") See: http://xmpp.org/extensions/xep-"..xep.number..".html";
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 function parse_xeps(t)
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if not t then return nil; end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local currxep = {};
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 for b in string.gmatch(t,"<xep>(.-)</xep>") do
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 currxep[k] = v;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 xeps[currxep.number] = { };
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 for k, v in pairs(currxep) do xeps[currxep.number][k] = v end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 xeps["0028"] = { number = "0028", name = "XSF Plans for World Domination", type="Top Secret", status = "Hidden", updated = "Work ongoing" };
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return true;
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 end

mercurial