mcbot/mcbot_engine.lua
author Mikael Berthe <mikael@lilotux.net>
Thu, 15 Apr 2010 20:22:36 +0200
changeset 19 96e08713cb6a
parent 18 615c9b336207
child 21 bc5d611e6d6f
permissions -rw-r--r--
Add support for aliases

#! /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, object)
    commands[name] = object
end

function mcbot_get_command_list ()
    return commands
end

require "cmds.wtf"
require "cmds.calc"
require "cmds.spell"
require "cmds.dict"
require "cmds.misc"
require "cmds.xep"
require "cmds.mcabber_bts"

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
        local lccmd = cmd:lower()
        for name, obj in pairs(commands) do
            if lccmd == name then
                return obj.cmd(arg, botdata, cmd)
            end
            if obj.aliases then
                for i, alias in ipairs(obj.aliases) do
                    if lccmd == alias then
                        return obj.cmd(arg, botdata, cmd)
                    end
                end
            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