mcbot/mcbot_engine.lua
changeset 0 89add07d6fe4
child 1 cca972635e5e
equal deleted inserted replaced
-1:000000000000 0:89add07d6fe4
       
     1 #! /usr/bin/env lua
       
     2 
       
     3 local commands = {}
       
     4 
       
     5 function trim(s)
       
     6   return s:match("^%s*(.-)%s*$")
       
     7 end
       
     8 function rtrim(s)
       
     9   return s:gsub("%s+$", "")
       
    10 end
       
    11 
       
    12 function string.starts(String,Start)
       
    13    return string.sub(String, 1, string.len(Start)) == Start
       
    14 end
       
    15 
       
    16 function mcbot_register_command (name, callback)
       
    17     commands[name] = callback
       
    18 end
       
    19 
       
    20 require "cmds.wtf"
       
    21 require "cmds.calc"
       
    22 require "cmds.spell"
       
    23 require "cmds.dict"
       
    24 require "cmds.misc"
       
    25 
       
    26 function process (line, botname, muc)
       
    27     local n
       
    28     line = trim(line)
       
    29     line, n = line:gsub("^"..botname.."[,: ]%s+", "")
       
    30     if muc and n ~= 1 then return nil, nil end
       
    31 
       
    32     local cmd, arg
       
    33     cmd = line:match("^(%w+)%s*[!]*$")
       
    34     if not cmd then
       
    35         -- Check if there are arguments
       
    36         cmd, arg = line:match("^(%w+)%s+(.*)")
       
    37     end
       
    38     if cmd then
       
    39         for name, f in pairs(commands) do
       
    40             if cmd and cmd:lower() == name then
       
    41                 return f(arg)
       
    42             end
       
    43         end
       
    44     else
       
    45         -- Ignore smileys
       
    46         if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end
       
    47         if line:match("^^^%s*$") then return nil,nil end
       
    48     end
       
    49     return "I don't understand, Sir"
       
    50 end