# HG changeset patch # User Mikael Berthe # Date 1271276279 -7200 # Node ID 4993fc6b11084db30a33cbd0c0c476a69e4bb297 # Parent 2dc0077b09d784f97f7510107cb7e44287b76748 Improve xep command Can search by string now diff -r 2dc0077b09d7 -r 4993fc6b1108 mcbot/cmds/xep.lua --- a/mcbot/cmds/xep.lua Wed Apr 14 00:34:39 2010 +0200 +++ b/mcbot/cmds/xep.lua Wed Apr 14 22:17:59 2010 +0200 @@ -31,22 +31,48 @@ parse_xeps(xepdata) end --- Logics comes from MattJ's riddim bot -local function lookup_xep (xepnum) - if (xepnum) then xepnum = xepnum:gsub("[%s%?%.%!]+$", "") end +-- Deeply inspired by MattJ's riddim bot +local function lookup_xep (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(xepnum) - if not xepnum or xepnum <= 0 or xepnum > 9999 then return nil, "What XEP?" end - xepnum = tostring(xepnum) + 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() - -- 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: " + + -- #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: " + 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", lookup_xep)