plugins/xeps.lua

Wed, 20 Jul 2011 01:11:11 +0200

author
Kim Alvefur <zash@zash.se>
date
Wed, 20 Jul 2011 01:11:11 +0200
changeset 84
ce687757ce3d
parent 83
4a25cd97711d
child 85
7ec6df0e0ed0
permissions
-rw-r--r--

plugins.xeps: Check for exact matches on shortnames.

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;
84
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
2 local xeps, xeps_short = {}, {};
27
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)
32
7e79b03f10e1 plugins.xeps: Use net.http
Florian Zeitz <florob@babelmonkeys.de>
parents: 27
diff changeset
5 require "net.httpclient_listener";
7e79b03f10e1 plugins.xeps: Use net.http
Florian Zeitz <florob@babelmonkeys.de>
parents: 27
diff changeset
6 local http = require("net.http");
27
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)
83
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
30 local xepnum = command.param;
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
31 if not xepnum then return "Please supply an XEP number or a search string :)"; end
84
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
32 if xeps_short[xepnum:lower()] then
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
33 xepnum = xeps_short[xepnum:lower()];
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
34 elseif not tonumber(xepnum) then -- Search for an XEP
83
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
35 if xepnum:match("^(%d+) ex%S* (%d+)$") then
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
36 local num, example = xepnum:match("^(%d+) ex%S* (%d+)$");
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
37 return "http://xmpp.org/extensions/xep-"..string.rep("0", 4-num:len())..num..".html#example-"..tostring(example);
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
38 end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
39 local results = {};
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
40 for x, xep in pairs(xeps) do
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
41 name = " "..xep.name:lower().." ";
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
42 if name:match(xepnum:lower():gsub("%-", "%%-")) then
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
43 table.insert(results, x);
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
44 --return commands.xep(msg, x);
27
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 end
83
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
46 end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
47 if #results == 0 then
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
48 return "Sorry, I couldn't find a match";
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
49 elseif #results == 1 then
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
50 command.param = results[1];
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
51 return handle_xep_command(command);
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
52 else
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
53 -- We have more than one match
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
54 local ret = "Multiple matches:";
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
55 for _, x in ipairs(results) do
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
56 local xepnum = tostring(tonumber(x));
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
57 xepnum = string.rep("0", 4-x:len())..x;
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
58 local xep = xeps[tostring(x)];
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
59 ret = string.format("%s XEP-%s: %s%s", ret, xep.number, xep.name, ((_ < #results) and ",") or "");
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
60 if _ > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
61 end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
62 return ret;
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
63 end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
64 end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
65 -- Check that xepnum is a valid number
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
66 xepnum = tostring(tonumber(xepnum));
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
67 if not xepnum then return "What XEP? or enter a search string."; end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
68 -- Expand to full 4 char number
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
69 xepnum = string.rep("0", 4-xepnum:len())..xepnum;
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
70 xep = xeps[tostring(xepnum)];
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
71 if not xep then return "Sorry, I don't think there is a XEP-"..xepnum; end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
72 return "XEP-"..xep.number..": "..xep.name.." is "..xep.type.." ("..xep.status..", "..xep.updated..") See: http://xmpp.org/extensions/xep-"..xep.number..".html";
27
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 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
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 function parse_xeps(t)
83
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
76 if not t then return nil; end
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
77 local currxep = {};
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
78 for b in string.gmatch(t,"<xep>(.-)</xep>") do
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
79 for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
80 currxep[k] = v;
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
81 end
27
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[currxep.number] = { };
84
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
83 for k, v in pairs(currxep) do
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
84 xeps[currxep.number][k] = v;
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
85 if xeps_short[currxep.shortname] == nil then
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
86 xeps_short[currxep.shortname] = currxep.number;
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
87 elseif xeps_short[currxep.shortname] then
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
88 xeps_short[currxep.shortname] = false; -- kill dupes
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
89 end
ce687757ce3d plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents: 83
diff changeset
90 end
83
4a25cd97711d plugins.xeps: Re-indent
Kim Alvefur <zash@zash.se>
parents: 75
diff changeset
91 end
27
2c137706d42c plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 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
93 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
94 end

mercurial