Update command infrastructure
authorMikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 18:57:38 +0200
changeset 16 064a50911e05
parent 15 203b49c24dde
child 17 d941ef833b4a
Update command infrastructure
mcbot/cmds/calc.lua
mcbot/cmds/dict.lua
mcbot/cmds/mcabber_bts.lua
mcbot/cmds/misc.lua
mcbot/cmds/spell.lua
mcbot/cmds/wtf.lua
mcbot/cmds/xep.lua
mcbot/mcbot_engine.lua
--- a/mcbot/cmds/calc.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/calc.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -1,3 +1,6 @@
+
+local dc = { ["desc"] = "RPN calculator" }
+local calc = { ["desc"] = "Calculator" }
 
 local function sbox (untrusted_code)
     -- make environment
@@ -17,7 +20,7 @@
 
 -- ---- ----
 
-local function dc (args)
+function dc.cmd (args)
     if not args then return nil, "Give me an expression" end
     -- Downloaded from http://www.math.bas.bg/bantchev/place/rpn/rpn.lua.html
     tb = {}  z = 0
@@ -36,7 +39,7 @@
     elseif n>1 or z==nil then return nil, "dc: Error!" end
 end
 
-local function calc (args)
+function calc.cmd (args)
     if not args then return nil, "Give me an expression" end
     local f, msg = sbox("return "..args)
     if not f then
--- a/mcbot/cmds/dict.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/dict.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -1,5 +1,7 @@
 
-local function dict (args)
+local dict = { ["desc"] = "Dictionnary" }
+
+function dict.cmd (args)
     if not args then return nil, "Give me a word, please" end
     -- Be careful as we pass it as an argument to a shell command
     local word = string.match(args, "^(%w+)[%s%?%.%!]*$")
--- a/mcbot/cmds/mcabber_bts.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/mcabber_bts.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -1,6 +1,8 @@
 
 require "libs.shcmd"
 
+local mcabber_bts = { ["desc"] = "Query mcabber (crew) BTS" }
+
 local function get_issue_details (num)
     local item
 
@@ -28,7 +30,7 @@
     return issue
 end
 
-local function mcabber_bts (num)
+function mcabber_bts.cmd (num)
     -- Check that xepnum is a valid number
     if num then num = num:gsub("[%s%?%.%!]+$", "") end
     num = tonumber(num)
--- a/mcbot/cmds/misc.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/misc.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -1,34 +1,26 @@
 
-local function hello (args)
-    return "Hello!"
-end
+local hello = { ["hidden"] = true,
+                ["cmd"] = function (args) return "Hello!" end
+              }
 
 mcbot_register_command("hello", hello)
 mcbot_register_command("hi", hello)
 
 
-local function thanks (args)
-    return "You're welcome"
-end
+local thanks = { ["hidden"] = true,
+                 ["cmd"] = function (args) return "You're welcome" end
+               }
 
 mcbot_register_command("thanks", thanks)
 
 
-local function help (args)
-    return "I can't help you, buddy"
-end
-
-mcbot_register_command("help", help)
-
-
-local function ping (args)
-    return "pong"
-end
+local ping = { ["cmd"] = function (args) return "pong" end }
 
 mcbot_register_command("ping", ping)
 
 
-local function date (args)
+local date = { ["desc"] = "Display the current date" }
+function date.cmd (args)
     if args then
         return os.date(args)
     else
@@ -37,3 +29,11 @@
 end
 
 mcbot_register_command("date", date)
+
+
+local help = { ["desc"] = "Display the available commands" }
+function help.cmd (args)
+    return "I can't help you, buddy"
+end
+
+mcbot_register_command("help", help)
--- a/mcbot/cmds/spell.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/spell.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -1,3 +1,5 @@
+
+local spell = { desc = "Spell checker" }
 
 local function check (text, lang)
     local fname = os.tmpname()
@@ -19,7 +21,7 @@
     return result
 end
 
-local function spell (args)
+function spell.cmd (args)
     if not args then return nil, "What do you want me to spellcheck?" end
     local r
     local l, s = string.match(args, "^%s*-d%s?([%w_]+)%s+(.*)%s*$")
--- a/mcbot/cmds/wtf.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/wtf.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -1,7 +1,9 @@
 
 local wtfdbfile = "/home/mikael/.mcabber/lua/mcbot/wtfdb.txt"
 
-local function wtf (args)
+local wtf = { ["desc"] = "Acronym dictionary" }
+
+function wtf.cmd (args)
     local r = {}
     if not args then return nil, "WTH do you want?" end
     args = args:gsub("[%s%?%.%!]+$", ""):upper()
--- a/mcbot/cmds/xep.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/cmds/xep.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -6,6 +6,8 @@
 
 local xeplisturl = "curl http://xmpp.org/extensions/xeps.xml"
 
+local xep = { ["desc"] = "Lookup XEP database" }
+
 local xeps = {}
 
 -- parse_xep() from Matthew Wild (aka MattJ)
@@ -32,7 +34,7 @@
 end
 
 -- Deeply inspired by MattJ's riddim bot
-local function lookup_xep (xepstr)
+function xep.cmd (xepstr)
     if (xepstr) then
         xepstr = xepstr:gsub("[%s%?%.%!]+$", "")
         local n = xepstr:upper():match("^XEP[%-%s]?(%d+)$")
@@ -75,4 +77,4 @@
     return nil, "Sorry, not matching XEP found"
 end
 
-mcbot_register_command("xep", lookup_xep)
+mcbot_register_command("xep", xep)
--- a/mcbot/mcbot_engine.lua	Thu Apr 15 18:35:13 2010 +0200
+++ b/mcbot/mcbot_engine.lua	Thu Apr 15 18:57:38 2010 +0200
@@ -13,8 +13,8 @@
    return string.sub(String, 1, string.len(Start)) == Start
 end
 
-function mcbot_register_command (name, callback)
-    commands[name] = callback
+function mcbot_register_command (name, object)
+    commands[name] = object
 end
 
 require "cmds.wtf"
@@ -38,9 +38,9 @@
         cmd, arg = line:match("^(%w+)%s+(.*)")
     end
     if cmd then
-        for name, f in pairs(commands) do
+        for name, obj in pairs(commands) do
             if cmd and cmd:lower() == name then
-                return f(arg, botdata)
+                return obj.cmd(arg, botdata)
             end
         end
     else