mcbot/mcbot_engine.lua
changeset 0 89add07d6fe4
child 1 cca972635e5e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mcbot/mcbot_engine.lua	Sun Apr 11 18:13:18 2010 +0200
@@ -0,0 +1,50 @@
+#! /usr/bin/env lua
+
+local commands = {}
+
+function trim(s)
+  return s:match("^%s*(.-)%s*$")
+end
+function rtrim(s)
+  return s:gsub("%s+$", "")
+end
+
+function string.starts(String,Start)
+   return string.sub(String, 1, string.len(Start)) == Start
+end
+
+function mcbot_register_command (name, callback)
+    commands[name] = callback
+end
+
+require "cmds.wtf"
+require "cmds.calc"
+require "cmds.spell"
+require "cmds.dict"
+require "cmds.misc"
+
+function process (line, botname, muc)
+    local n
+    line = trim(line)
+    line, n = line:gsub("^"..botname.."[,: ]%s+", "")
+    if muc and n ~= 1 then return nil, nil end
+
+    local cmd, arg
+    cmd = line:match("^(%w+)%s*[!]*$")
+    if not cmd then
+        -- Check if there are arguments
+        cmd, arg = line:match("^(%w+)%s+(.*)")
+    end
+    if cmd then
+        for name, f in pairs(commands) do
+            if cmd and cmd:lower() == name then
+                return f(arg)
+            end
+        end
+    else
+        -- Ignore smileys
+        if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end
+        if line:match("^^^%s*$") then return nil,nil end
+    end
+    return "I don't understand, Sir"
+end