Add command prefix (useful for MUC rooms)
authorMikael Berthe <mikael@lilotux.net>
Sat, 17 Apr 2010 15:44:09 +0200
changeset 27 a166e9ab8065
parent 26 c12be727375b
child 28 510849236c58
Add command prefix (useful for MUC rooms)
mcabbot.lua
mcbot/mcbot_engine.lua
--- a/mcabbot.lua	Sat Apr 17 15:33:23 2010 +0200
+++ b/mcabbot.lua	Sat Apr 17 15:44:09 2010 +0200
@@ -10,6 +10,7 @@
 local mcabbot = {}
 mcabbot.nickname = "McBot"
 mcabbot.jid = "mcabbot@lilotux.net"
+mcabbot.cmdprefix = "%%"
 
 local function hk_subscription (h)
     if h.type == "subscribe" then
@@ -50,7 +51,7 @@
             cmd = "say_to -q "..h.jid.."/"..h.resource.." "
         end
         mcabbot.message = h
-        local res, errmsg = process(message, mcabbot, muc)
+        local res, errmsg = process(message, mcabbot, muc, mcabbot.cmdprefix)
         mcabbot.message = nil
         if res then
             msg = res
--- a/mcbot/mcbot_engine.lua	Sat Apr 17 15:33:23 2010 +0200
+++ b/mcbot/mcbot_engine.lua	Sat Apr 17 15:44:09 2010 +0200
@@ -9,7 +9,7 @@
   return s:gsub("%s+$", "")
 end
 
-function string.starts(String,Start)
+function string.starts(String, Start)
    return string.sub(String, 1, string.len(Start)) == Start
 end
 
@@ -32,11 +32,18 @@
 require "cmds.bofh"
 require "cmds.xmpp"
 
-function process (line, botdata, muc)
+function process (line, botdata, muc, cmdprefix)
     local n
     line = trim(line)
     line, n = line:gsub("^"..botdata.nickname.."[,:]?%s+", "")
-    if muc and n ~= 1 then return nil, nil end
+    if muc and n ~= 1 then
+        if cmdprefix and line:starts(cmdprefix) then
+            line = line:sub(string.len(cmdprefix)+1)
+            line = line:gsub("^%s*", "")
+        else
+            return nil, nil
+        end
+    end
 
     local cmd, arg
     cmd = line:match("^(%w+)%s*[!]*$")