# HG changeset patch # User Mikael Berthe # Date 1271185661 -7200 # Node ID 508108deee63e81cbc429b1f04f44d47dd186b0b # Parent c7ac7b6a76e04ce6899b00207fa63c6fc278a336 Add command xep diff -r c7ac7b6a76e0 -r 508108deee63 mcbot/cmds/xep.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mcbot/cmds/xep.lua Tue Apr 13 21:07:41 2010 +0200 @@ -0,0 +1,51 @@ + +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,"(.-)") do + for k,v in string.gmatch(b,"<(%w+)>(.-)") 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) + -- 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: " +end + +mcbot_register_command("xep", lookup_xep) diff -r c7ac7b6a76e0 -r 508108deee63 mcbot/libs/shcmd.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mcbot/libs/shcmd.lua Tue Apr 13 21:07:41 2010 +0200 @@ -0,0 +1,12 @@ + +function shcmd (cmd) + if not cmd or cmd == "" then return nil, "No command" end + + local fullcmd = cmd.." 2> /dev/null" + local fh = io.popen(fullcmd) + result = fh:read("*a") -- read cmd output + fh:close() + -- Trim trailing newlines + local r = result:gsub("\n+$", "") + return r +end diff -r c7ac7b6a76e0 -r 508108deee63 mcbot/mcbot_engine.lua --- a/mcbot/mcbot_engine.lua Tue Apr 13 20:34:59 2010 +0200 +++ b/mcbot/mcbot_engine.lua Tue Apr 13 21:07:41 2010 +0200 @@ -22,6 +22,7 @@ require "cmds.spell" require "cmds.dict" require "cmds.misc" +require "cmds.xep" function process (line, botdata, muc) local n