Fri, 07 Aug 2020 12:24:46 +0100
Dockerfile: include util.interpolation in build
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 | 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
|
6 | 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
|
7 | -- 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
|
8 | if os.difftime(os.time(), xeps_updated_at) > (60 * 60) then -- Not refreshed within 1 hour |
153
23b0ddf14e12
xeps: Update URLs (but still doesn't work)
Matthew Wild <mwild1@gmail.com>
parents:
115
diff
changeset
|
9 | http.request('https://xmpp.org/extensions/xeplist.xml', nil, function (data, code) |
27
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | 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
|
11 | 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
|
12 | 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
|
13 | else |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | 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
|
15 | end |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | return; |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | end |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | 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
|
19 | 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
|
20 | 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
|
21 | end); |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | else |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | 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
|
24 | end |
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 | |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | function handle_xep_command(command) |
83 | 29 | local xepnum = command.param; |
30 | 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
|
31 | if xeps_short[xepnum:lower()] then |
ce687757ce3d
plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents:
83
diff
changeset
|
32 | xepnum = xeps_short[xepnum:lower()]; |
ce687757ce3d
plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents:
83
diff
changeset
|
33 | elseif not tonumber(xepnum) then -- Search for an XEP |
83 | 34 | if xepnum:match("^(%d+) ex%S* (%d+)$") then |
35 | local num, example = xepnum:match("^(%d+) ex%S* (%d+)$"); | |
153
23b0ddf14e12
xeps: Update URLs (but still doesn't work)
Matthew Wild <mwild1@gmail.com>
parents:
115
diff
changeset
|
36 | return "https://xmpp.org/extensions/xep-"..string.rep("0", 4-num:len())..num..".html#example-"..tostring(example); |
83 | 37 | end |
38 | local results = {}; | |
39 | for x, xep in pairs(xeps) do | |
40 | name = " "..xep.name:lower().." "; | |
41 | if name:match(xepnum:lower():gsub("%-", "%%-")) then | |
42 | table.insert(results, x); | |
43 | --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
|
44 | end |
83 | 45 | end |
46 | if #results == 0 then | |
47 | return "Sorry, I couldn't find a match"; | |
48 | elseif #results == 1 then | |
49 | command.param = results[1]; | |
50 | return handle_xep_command(command); | |
51 | else | |
52 | -- We have more than one match | |
53 | local ret = "Multiple matches:"; | |
54 | for _, x in ipairs(results) do | |
55 | local xepnum = tostring(tonumber(x)); | |
56 | xepnum = string.rep("0", 4-x:len())..x; | |
57 | local xep = xeps[tostring(x)]; | |
58 | ret = string.format("%s XEP-%s: %s%s", ret, xep.number, xep.name, ((_ < #results) and ",") or ""); | |
59 | if _ > 5 then ret = ret .. " " .. (#results - 5) .. " more"; break; end | |
60 | end | |
61 | return ret; | |
62 | end | |
63 | end | |
64 | -- Check that xepnum is a valid number | |
65 | xepnum = tostring(tonumber(xepnum)); | |
66 | if not xepnum then return "What XEP? or enter a search string."; end | |
67 | -- Expand to full 4 char number | |
68 | xepnum = string.rep("0", 4-xepnum:len())..xepnum; | |
69 | xep = xeps[tostring(xepnum)]; | |
70 | if not xep then return "Sorry, I don't think there is a XEP-"..xepnum; end | |
153
23b0ddf14e12
xeps: Update URLs (but still doesn't work)
Matthew Wild <mwild1@gmail.com>
parents:
115
diff
changeset
|
71 | return "XEP-"..xep.number..": "..xep.name.." is "..xep.type.." ("..xep.status..", "..xep.updated..") See: https://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
|
72 | end |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | |
2c137706d42c
plugins.xeps: Add 'xep' command for looking up XMPP extensions by name or number
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | function parse_xeps(t) |
83 | 75 | if not t then return nil; end |
76 | local currxep = {}; | |
77 | for b in string.gmatch(t,"<xep>(.-)</xep>") do | |
78 | for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do | |
79 | currxep[k] = v; | |
80 | end | |
85
7ec6df0e0ed0
plugins.xeps: Fix previous commit (it was wrong side of the loopy loops)
Kim Alvefur <zash@zash.se>
parents:
84
diff
changeset
|
81 | if xeps_short[currxep.shortname] == nil then |
7ec6df0e0ed0
plugins.xeps: Fix previous commit (it was wrong side of the loopy loops)
Kim Alvefur <zash@zash.se>
parents:
84
diff
changeset
|
82 | xeps_short[currxep.shortname] = currxep.number; |
7ec6df0e0ed0
plugins.xeps: Fix previous commit (it was wrong side of the loopy loops)
Kim Alvefur <zash@zash.se>
parents:
84
diff
changeset
|
83 | elseif xeps_short[currxep.shortname] then |
7ec6df0e0ed0
plugins.xeps: Fix previous commit (it was wrong side of the loopy loops)
Kim Alvefur <zash@zash.se>
parents:
84
diff
changeset
|
84 | xeps_short[currxep.shortname] = false; -- kill dupes |
7ec6df0e0ed0
plugins.xeps: Fix previous commit (it was wrong side of the loopy loops)
Kim Alvefur <zash@zash.se>
parents:
84
diff
changeset
|
85 | 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
|
86 | xeps[currxep.number] = { }; |
84
ce687757ce3d
plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents:
83
diff
changeset
|
87 | 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
|
88 | xeps[currxep.number][k] = v; |
ce687757ce3d
plugins.xeps: Check for exact matches on shortnames.
Kim Alvefur <zash@zash.se>
parents:
83
diff
changeset
|
89 | end |
83 | 90 | 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
|
91 | 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
|
92 | 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
|
93 | end |