mcbot/mcbot_engine.lua
author Mikael Berthe <mikael@lilotux.net>
Sun, 11 Apr 2010 22:03:12 +0200
changeset 1 cca972635e5e
parent 0 89add07d6fe4
child 3 508108deee63
permissions -rwxr-xr-x
Provid more data to the bot functions

#! /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, botdata, muc)
    local n
    line = trim(line)
    line, n = line:gsub("^"..botdata.nickname.."[,: ]%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, botdata)
            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