mcbot/mcbot_engine.lua
author Mikael Berthe <mikael@lilotux.net>
Tue, 27 Nov 2012 16:26:04 +0100
changeset 66 d9c00a9fe9d5
parent 64 62248d5fbb24
permissions -rw-r--r--
Add notices before public release

#! /usr/bin/env lua

--  McBot / mcabbot project
--
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or (at
-- your option) any later version.
--
-- Please check the license in the COPYING file at the root of the tree.
--

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