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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
#! /usr/bin/env lua
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
66
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     3
--  McBot / mcabbot project
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     4
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     5
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net>
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     6
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     7
-- This program is free software; you can redistribute it and/or modify
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     8
-- it under the terms of the GNU General Public License as published by
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
     9
-- the Free Software Foundation; either version 2 of the License, or (at
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
    10
-- your option) any later version.
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
    11
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
    12
-- Please check the license in the COPYING file at the root of the tree.
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
    13
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 64
diff changeset
    14
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
local commands = {}
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
function trim(s)
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
  return s:match("^%s*(.-)%s*$")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
function rtrim(s)
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
  return s:gsub("%s+$", "")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
27
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    24
function string.starts(String, Start)
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
   return string.sub(String, 1, string.len(Start)) == Start
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
16
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
    28
function mcbot_register_command (name, object)
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
    29
    commands[name] = object
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
18
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    32
function mcbot_get_command_list ()
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    33
    return commands
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    34
end
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    35
21
bc5d611e6d6f Improve command help
Mikael Berthe <mikael@lilotux.net>
parents: 19
diff changeset
    36
require "cmds.help"
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
require "cmds.wtf"
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
require "cmds.calc"
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
require "cmds.spell"
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
require "cmds.dict"
54
be04d7241ef6 New command: littre
Mikael Berthe <mikael@lilotux.net>
parents: 35
diff changeset
    41
require "cmds.littre"
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
require "cmds.misc"
3
508108deee63 Add command xep
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    43
require "cmds.xep"
31
71bf91612a33 Add command rfc
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    44
require "cmds.rfc"
6
7cf014d0d206 Add command bts/bug (mcabber_bts)
Mikael Berthe <mikael@lilotux.net>
parents: 3
diff changeset
    45
require "cmds.mcabber_bts"
23
491f7d046353 Add command bofh :-)
Mikael Berthe <mikael@lilotux.net>
parents: 21
diff changeset
    46
require "cmds.bofh"
25
788c62c29e18 Add a simple xmpp command
Mikael Berthe <mikael@lilotux.net>
parents: 23
diff changeset
    47
require "cmds.xmpp"
35
1fba2631c743 Add command tvcal
Mikael Berthe <mikael@lilotux.net>
parents: 31
diff changeset
    48
require "cmds.tvcal"
64
62248d5fbb24 Add shortenurl module, based on isbear's code
Mikael Berthe <mikael@lilotux.net>
parents: 54
diff changeset
    49
require "cmds.shortenurl"
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
27
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    51
function process (line, botdata, muc, cmdprefix)
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
    local n
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
    line = trim(line)
26
c12be727375b Do not require ':' or ',' after the bot name
Mikael Berthe <mikael@lilotux.net>
parents: 25
diff changeset
    54
    line, n = line:gsub("^"..botdata.nickname.."[,:]?%s+", "")
27
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    55
    if muc and n ~= 1 then
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    56
        if cmdprefix and line:starts(cmdprefix) then
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    57
            line = line:sub(string.len(cmdprefix)+1)
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    58
            line = line:gsub("^%s*", "")
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    59
        else
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    60
            return nil, nil
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    61
        end
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 26
diff changeset
    62
    end
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
    local cmd, arg
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
    cmd = line:match("^(%w+)%s*[!]*$")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
    if not cmd then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
        -- Check if there are arguments
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
        cmd, arg = line:match("^(%w+)%s+(.*)")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
    end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
    if cmd then
18
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    71
        local lccmd = cmd:lower()
16
064a50911e05 Update command infrastructure
Mikael Berthe <mikael@lilotux.net>
parents: 6
diff changeset
    72
        for name, obj in pairs(commands) do
18
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    73
            if lccmd == name then
615c9b336207 Pass command to the callback functions
Mikael Berthe <mikael@lilotux.net>
parents: 16
diff changeset
    74
                return obj.cmd(arg, botdata, cmd)
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
            end
19
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    76
            if obj.aliases then
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    77
                for i, alias in ipairs(obj.aliases) do
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    78
                    if lccmd == alias then
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    79
                        return obj.cmd(arg, botdata, cmd)
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    80
                    end
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    81
                end
96e08713cb6a Add support for aliases
Mikael Berthe <mikael@lilotux.net>
parents: 18
diff changeset
    82
            end
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
        end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
    else
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
        -- Ignore smileys
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
        if line:match("^[:;8]'?%-?[D()|/\\%[%]pPoO$@]+%s*$") then return nil,nil end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
        if line:match("^^^%s*$") then return nil,nil end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
    end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
    return "I don't understand, Sir"
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
end