mcabbot.lua
author Mikael Berthe <mikael@lilotux.net>
Tue, 27 Nov 2012 16:36:17 +0100
changeset 67 5c756a9d7d8c
parent 66 d9c00a9fe9d5
permissions -rw-r--r--
Quick'n dirty hack to remove hardcoded paths
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     1
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     2
--  McBot / mcabbot project
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     3
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     4
-- Copyright (C) 2010-2012 Mikael Berthe <mikael@lilotux.net>
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     5
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     6
-- This program is free software; you can redistribute it and/or modify
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     7
-- 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: 41
diff changeset
     8
-- the Free Software Foundation; either version 2 of the License, or (at
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
     9
-- your option) any later version.
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
    10
--
d9c00a9fe9d5 Add notices before public release
Mikael Berthe <mikael@lilotux.net>
parents: 41
diff changeset
    11
-- 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: 41
diff changeset
    12
--
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
local home = os.getenv("HOME")
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
package.path = package.path..
67
5c756a9d7d8c Quick'n dirty hack to remove hardcoded paths
Mikael Berthe <mikael@lilotux.net>
parents: 66
diff changeset
    16
                ";"..home.."/.mcabber/mcabbot/?.lua"..
5c756a9d7d8c Quick'n dirty hack to remove hardcoded paths
Mikael Berthe <mikael@lilotux.net>
parents: 66
diff changeset
    17
                ";"..home.."/.mcabber/mcabbot/mcbot/?.lua"
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
home = nil
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
require "mcbot.mcbot_engine"
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
local mcabbot = {}
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
mcabbot.nickname = "McBot"
40
95ba0699f365 Update bot's JID
Mikael Berthe <mikael@lilotux.net>
parents: 28
diff changeset
    24
mcabbot.jid = "mcbot@lilotux.net"
27
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    25
mcabbot.cmdprefix = "%%"
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
24
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    27
local function hk_subscription (h)
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    28
    if h.type == "subscribe" then
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    29
        -- Let's authorize them...
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    30
        main.run("authorization allow "..h.jid)
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    31
        -- ... and subscribe to their own presence
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    32
        main.run("authorization request "..h.jid)
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    33
        return 1
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    34
    elseif h.type == "unsubscribe" or h.type == "unsubscribed" then
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    35
        main.run("authorization cancel "..h.jid)
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    36
    end
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    37
    return 0
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    38
end
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    39
1
cca972635e5e Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    40
local function hk_message_in (h)
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
    local perso = false
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
    local muc = false
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
    local message = h.message
41
5c0a2db896f5 Ignore incoming error messages
Mikael Berthe <mikael@lilotux.net>
parents: 40
diff changeset
    44
5c0a2db896f5 Ignore incoming error messages
Mikael Berthe <mikael@lilotux.net>
parents: 40
diff changeset
    45
    if h.error and h.error == "true" then return end
5c0a2db896f5 Ignore incoming error messages
Mikael Berthe <mikael@lilotux.net>
parents: 40
diff changeset
    46
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
    if h.groupchat and h.groupchat == "true" then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
        muc = true
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
    end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
    if muc == true then
28
510849236c58 Fix command prefix usage
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    51
        if h.delayed == "" then
510849236c58 Fix command prefix usage
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    52
            if mcabbot.cmdprefix and message:starts(mcabbot.cmdprefix) then
510849236c58 Fix command prefix usage
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    53
                perso = true
510849236c58 Fix command prefix usage
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    54
            elseif h.attention and h.attention == "true" and
510849236c58 Fix command prefix usage
Mikael Berthe <mikael@lilotux.net>
parents: 27
diff changeset
    55
                   h.resource and h.resource ~= mcabbot.nickname then
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
                perso = true
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
            end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
        end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
    else
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
        if h.jid ~= mcabbot.jid then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
            perso = true
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
        end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
    end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
    if perso == true then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
        local cmd, msg
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
        if muc == true then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
            cmd = "say_to -q "..h.jid.." "..h.resource..": "
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
        else
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
            cmd = "say_to -q "..h.jid.."/"..h.resource.." "
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
        end
1
cca972635e5e Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    71
        mcabbot.message = h
27
a166e9ab8065 Add command prefix (useful for MUC rooms)
Mikael Berthe <mikael@lilotux.net>
parents: 24
diff changeset
    72
        local res, errmsg = process(message, mcabbot, muc, mcabbot.cmdprefix)
1
cca972635e5e Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    73
        mcabbot.message = nil
0
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
        if res then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
            msg = res
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
        elseif errmsg then
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
            msg = "! " .. errmsg
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
        end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
        if (msg) then main.run(cmd..msg) end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
    end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
end
89add07d6fe4 Initial revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
1
cca972635e5e Provid more data to the bot functions
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    83
main.hook("hook-post-message-in", hk_message_in)
24
77a72b227d59 Handle subscription requests
Mikael Berthe <mikael@lilotux.net>
parents: 1
diff changeset
    84
main.hook("hook-subscription", hk_subscription)