Add command xep
authorMikael Berthe <mikael@lilotux.net>
Tue, 13 Apr 2010 21:07:41 +0200
changeset 3 508108deee63
parent 2 c7ac7b6a76e0
child 4 3e3767ada403
Add command xep
mcbot/cmds/xep.lua
mcbot/libs/shcmd.lua
mcbot/mcbot_engine.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,"<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)
+    -- 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)
--- /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
--- 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