mcbot/cmds/xep.lua
author Mikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 18:57:38 +0200
changeset 16 064a50911e05
parent 11 4993fc6b1108
child 22 4b360fe207e6
permissions -rw-r--r--
Update command infrastructure


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 xep = { ["desc"] = "Lookup XEP database" }

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

-- Deeply inspired by MattJ's riddim bot
function xep.cmd (xepstr)
    if (xepstr) then
        xepstr = xepstr:gsub("[%s%?%.%!]+$", "")
        local n = xepstr:upper():match("^XEP[%-%s]?(%d+)$")
        if n then xepstr = n end
    end
    -- Check that xepnum is a valid number
    xepnum = tonumber(xepstr)
    if xepnum and (xepnum <= 0 or xepnum > 9999) then
        return nil, "What XEP?"
    end
    if not xepnum and not xepstr:match("^[%w%s%-_]+$") then
        return nil, "What XEP?  Please use a simple keyword..."
    end

    -- Download XEP list
    fetch_xeps()

    -- #1 The argument is a XEP number
    if xepnum then
        xepnum = tostring(xepnum)
        -- Expand to full 4 char number
        xepnum = string.rep("0", 4-xepnum:len())..xepnum
        local 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

    -- #2 The argument is a keyword
    local r = ""
    xepstr = xepstr:lower()
    for n, xep in pairs(xeps) do
        if xep.name:lower():find(xepstr) then
            r = r.."XEP-"..xep.number..": "..xep.name.."\n"
        end
    end
    r = r:gsub("\n$", "")
    if r ~= "" then return r; end
    return nil, "Sorry, not matching XEP found"
end

mcbot_register_command("xep", xep)