author | Mikael Berthe <mikael@lilotux.net> |
Tue, 27 Nov 2012 16:26:04 +0100 | |
changeset 66 | d9c00a9fe9d5 |
parent 39 | 237af42156a1 |
permissions | -rw-r--r-- |
66
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
1 |
|
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
2 |
-- This module is part of the McBot / mcabbot project |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
3 |
-- XEP module. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
4 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
5 |
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net> |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
6 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
7 |
-- This program is free software; you can redistribute it and/or modify |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
8 |
-- it under the terms of the GNU General Public License as published by |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
9 |
-- the Free Software Foundation; either version 2 of the License, or (at |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
10 |
-- your option) any later version. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
11 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
12 |
-- Please check the license in the COPYING file at the root of the tree. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
13 |
-- |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
14 |
-- Some parts were inspired by Matthew Wild (MattJ) code, see below. |
d9c00a9fe9d5
Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents:
39
diff
changeset
|
15 |
-- |
3 | 16 |
|
17 |
require "libs.shcmd" |
|
18 |
||
19 |
-- The caching idea is borrowed from MattJ's riddim code, |
|
20 |
-- and the function parse_xeps() as well. |
|
21 |
||
22 |
local xeplisturl = "curl http://xmpp.org/extensions/xeps.xml" |
|
23 |
||
22 | 24 |
local xep = { ["desc"] = "Search XEP database" } |
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
25 |
|
3 | 26 |
local xeps = {} |
27 |
||
28 |
-- parse_xep() from Matthew Wild (aka MattJ) |
|
29 |
local function parse_xeps(t) |
|
30 |
if not t or t == "" then return nil end |
|
31 |
local currxep = {} |
|
32 |
for b in string.gmatch(t,"<xep>(.-)</xep>") do |
|
33 |
for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do |
|
34 |
currxep[k] = v |
|
35 |
end |
|
36 |
xeps[currxep.number] = { } |
|
37 |
for k, v in pairs(currxep) do xeps[currxep.number][k] = v end |
|
38 |
end |
|
39 |
return true |
|
40 |
end |
|
41 |
||
42 |
local function fetch_xeps () |
|
43 |
local now = os.time() |
|
44 |
if os.difftime(now, xeps_updated_at) < 43200 then return end |
|
45 |
-- Let's get some fresh data |
|
46 |
local xepdata = shcmd(xeplisturl) |
|
39
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
47 |
if xepdata then |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
48 |
xeps_updated_at = now |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
49 |
parse_xeps(xepdata) |
237af42156a1
Add checks after shcmd() calls
Mikael Berthe <mikael@lilotux.net>
parents:
30
diff
changeset
|
50 |
end |
3 | 51 |
end |
52 |
||
11 | 53 |
-- Deeply inspired by MattJ's riddim bot |
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
54 |
function xep.cmd (xepstr) |
11 | 55 |
if (xepstr) then |
56 |
xepstr = xepstr:gsub("[%s%?%.%!]+$", "") |
|
57 |
local n = xepstr:upper():match("^XEP[%-%s]?(%d+)$") |
|
58 |
if n then xepstr = n end |
|
59 |
end |
|
3 | 60 |
-- Check that xepnum is a valid number |
11 | 61 |
xepnum = tonumber(xepstr) |
62 |
if xepnum and (xepnum <= 0 or xepnum > 9999) then |
|
63 |
return nil, "What XEP?" |
|
64 |
end |
|
65 |
if not xepnum and not xepstr:match("^[%w%s%-_]+$") then |
|
66 |
return nil, "What XEP? Please use a simple keyword..." |
|
67 |
end |
|
68 |
||
3 | 69 |
-- Download XEP list |
70 |
fetch_xeps() |
|
11 | 71 |
|
72 |
-- #1 The argument is a XEP number |
|
73 |
if xepnum then |
|
74 |
xepnum = tostring(xepnum) |
|
75 |
-- Expand to full 4 char number |
|
76 |
xepnum = string.rep("0", 4-xepnum:len())..xepnum |
|
77 |
local xep = xeps[tostring(xepnum)] |
|
78 |
if not xep then return nil, "Sorry, XEP-"..xepnum.." not found" end |
|
79 |
return "XEP-"..xep.number.." is \""..xep.name.."\"\n".. |
|
80 |
-- xep.type..", "..xep.status..", last updated "..xep.updated.."\n".. |
|
81 |
"URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>" |
|
82 |
end |
|
83 |
||
84 |
-- #2 The argument is a keyword |
|
85 |
local r = "" |
|
86 |
xepstr = xepstr:lower() |
|
87 |
for n, xep in pairs(xeps) do |
|
88 |
if xep.name:lower():find(xepstr) then |
|
89 |
r = r.."XEP-"..xep.number..": "..xep.name.."\n" |
|
90 |
end |
|
91 |
end |
|
92 |
r = r:gsub("\n$", "") |
|
93 |
if r ~= "" then return r; end |
|
30 | 94 |
return nil, "Sorry, no matching XEP found" |
3 | 95 |
end |
96 |
||
16
064a50911e05
Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
97 |
mcbot_register_command("xep", xep) |