Improve xep command
authorMikael Berthe <mikael@lilotux.net>
Wed, 14 Apr 2010 22:17:59 +0200
changeset 11 4993fc6b1108
parent 10 2dc0077b09d7
child 12 6428c88dc58a
Improve xep command Can search by string now
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: <http://xmpp.org/extensions/xep-"..xep.number..".html>"
+
+    -- #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", lookup_xep)