require "libs.shcmd"
-- The caching idea is borrowed from MattJ's riddim code,
-- and the function parse_xeps() as well.
local xeplisturl = "curl http://xmpp.org/extensions/xeps.xml"
local xeps = {}
-- parse_xep() from Matthew Wild (aka MattJ)
local function parse_xeps(t)
if not t or t == "" then return nil end
local currxep = {}
for b in string.gmatch(t,"<xep>(.-)</xep>") do
for k,v in string.gmatch(b,"<(%w+)>(.-)</%1>") do
currxep[k] = v
end
xeps[currxep.number] = { }
for k, v in pairs(currxep) do xeps[currxep.number][k] = v end
end
return true
end
local function fetch_xeps ()
local now = os.time()
if os.difftime(now, xeps_updated_at) < 43200 then return end
-- Let's get some fresh data
local xepdata = shcmd(xeplisturl)
xeps_updated_at = now
parse_xeps(xepdata)
end
-- Logics comes from MattJ's riddim bot
local function lookup_xep (xepnum)
if (xepnum) then xepnum = xepnum:gsub("[%s%?%.%!]+$", "") end
-- Check that xepnum is a valid number
xepnum = tonumber(xepnum)
if not xepnum or xepnum <= 0 or xepnum > 9999 then return nil, "What XEP?" end
xepnum = tostring(xepnum)
-- Download XEP list
fetch_xeps()
-- Expand to full 4 char number
xepnum = string.rep("0", 4-xepnum:len())..xepnum
xep = xeps[tostring(xepnum)]
if not xep then return nil,"Sorry, XEP-"..xepnum.." not found" end
return "XEP-"..xep.number.." is \""..xep.name.."\"\n"..
-- xep.type..", "..xep.status..", last updated "..xep.updated.."\n"..
"URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
end
mcbot_register_command("xep", lookup_xep)