mcbot/mcbot_engine.lua
author Mikael Berthe <mikael@lilotux.net>
Sun, 25 Nov 2012 19:01:31 +0100
changeset 64 62248d5fbb24
parent 54 be04d7241ef6
child 66 d9c00a9fe9d5
permissions -rw-r--r--
Add shortenurl module, based on isbear's code This code is derivated from the lua example script shortenurl.lua in isbear's lua module for mcabber.

#! /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.help"
require "cmds.wtf"
require "cmds.calc"
require "cmds.spell"
require "cmds.dict"
require "cmds.littre"
require "cmds.misc"
require "cmds.xep"
require "cmds.rfc"
require "cmds.mcabber_bts"
require "cmds.bofh"
require "cmds.xmpp"
require "cmds.tvcal"
require "cmds.shortenurl"

function process (line, botdata, muc, cmdprefix)
    local n
    line = trim(line)
    line, n = line:gsub("^"..botdata.nickname.."[,:]?%s+", "")
    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*[!]*$")
    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