mcbot/cmds/xep.lua
changeset 11 4993fc6b1108
parent 7 c515e8218c87
child 16 064a50911e05
equal deleted inserted replaced
10:2dc0077b09d7 11:4993fc6b1108
    29     local xepdata = shcmd(xeplisturl)
    29     local xepdata = shcmd(xeplisturl)
    30     xeps_updated_at = now
    30     xeps_updated_at = now
    31     parse_xeps(xepdata)
    31     parse_xeps(xepdata)
    32 end
    32 end
    33 
    33 
    34 -- Logics comes from MattJ's riddim bot
    34 -- Deeply inspired by MattJ's riddim bot
    35 local function lookup_xep (xepnum)
    35 local function lookup_xep (xepstr)
    36     if (xepnum) then xepnum = xepnum:gsub("[%s%?%.%!]+$", "") end
    36     if (xepstr) then
       
    37         xepstr = xepstr:gsub("[%s%?%.%!]+$", "")
       
    38         local n = xepstr:upper():match("^XEP[%-%s]?(%d+)$")
       
    39         if n then xepstr = n end
       
    40     end
    37     -- Check that xepnum is a valid number
    41     -- Check that xepnum is a valid number
    38     xepnum = tonumber(xepnum)
    42     xepnum = tonumber(xepstr)
    39     if not xepnum or xepnum <= 0 or xepnum > 9999 then return nil, "What XEP?" end
    43     if xepnum and (xepnum <= 0 or xepnum > 9999) then
    40     xepnum = tostring(xepnum)
    44         return nil, "What XEP?"
       
    45     end
       
    46     if not xepnum and not xepstr:match("^[%w%s%-_]+$") then
       
    47         return nil, "What XEP?  Please use a simple keyword..."
       
    48     end
       
    49 
    41     -- Download XEP list
    50     -- Download XEP list
    42     fetch_xeps()
    51     fetch_xeps()
    43     -- Expand to full 4 char number
    52 
    44     xepnum = string.rep("0", 4-xepnum:len())..xepnum
    53     -- #1 The argument is a XEP number
    45     xep = xeps[tostring(xepnum)]
    54     if xepnum then
    46     if not xep then return nil,"Sorry, XEP-"..xepnum.." not found" end
    55         xepnum = tostring(xepnum)
    47     return "XEP-"..xep.number.." is \""..xep.name.."\"\n"..
    56         -- Expand to full 4 char number
    48            -- xep.type..", "..xep.status..", last updated "..xep.updated.."\n"..
    57         xepnum = string.rep("0", 4-xepnum:len())..xepnum
    49            "URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
    58         local xep = xeps[tostring(xepnum)]
       
    59         if not xep then return nil, "Sorry, XEP-"..xepnum.." not found" end
       
    60         return "XEP-"..xep.number.." is \""..xep.name.."\"\n"..
       
    61                -- xep.type..", "..xep.status..", last updated "..xep.updated.."\n"..
       
    62                "URL: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
       
    63     end
       
    64 
       
    65     -- #2 The argument is a keyword
       
    66     local r = ""
       
    67     xepstr = xepstr:lower()
       
    68     for n, xep in pairs(xeps) do
       
    69         if xep.name:lower():find(xepstr) then
       
    70             r = r.."XEP-"..xep.number..": "..xep.name.."\n"
       
    71         end
       
    72     end
       
    73     r = r:gsub("\n$", "")
       
    74     if r ~= "" then return r; end
       
    75     return nil, "Sorry, not matching XEP found"
    50 end
    76 end
    51 
    77 
    52 mcbot_register_command("xep", lookup_xep)
    78 mcbot_register_command("xep", lookup_xep)