mcabbot.lua
author Mikael Berthe <mikael@lilotux.net>
Sat, 17 Apr 2010 15:44:09 +0200
changeset 27 a166e9ab8065
parent 24 77a72b227d59
child 28 510849236c58
permissions -rw-r--r--
Add command prefix (useful for MUC rooms)


local home = os.getenv("HOME")
package.path = package.path..
                ";"..home.."/.mcabber/lua/?.lua"..
                ";"..home.."/.mcabber/lua/mcbot/?.lua"
home = nil

require "mcbot.mcbot_engine"

local mcabbot = {}
mcabbot.nickname = "McBot"
mcabbot.jid = "mcabbot@lilotux.net"
mcabbot.cmdprefix = "%%"

local function hk_subscription (h)
    if h.type == "subscribe" then
        -- Let's authorize them...
        main.run("authorization allow "..h.jid)
        -- ... and subscribe to their own presence
        main.run("authorization request "..h.jid)
        return 1
    elseif h.type == "unsubscribe" or h.type == "unsubscribed" then
        main.run("authorization cancel "..h.jid)
    end
    return 0
end

local function hk_message_in (h)
    local perso = false
    local muc = false
    local message = h.message
    if h.groupchat and h.groupchat == "true" then
        muc = true
    end
    if muc == true then
        if h.delayed == ""  and h.attention and h.attention == "true" then
            if h.resource and h.resource ~= mcabbot.nickname then
                perso = true
            end
        end
    else
        if h.jid ~= mcabbot.jid then
            perso = true
        end
    end
    if perso == true then
        local cmd, msg
        if muc == true then
            cmd = "say_to -q "..h.jid.." "..h.resource..": "
        else
            cmd = "say_to -q "..h.jid.."/"..h.resource.." "
        end
        mcabbot.message = h
        local res, errmsg = process(message, mcabbot, muc, mcabbot.cmdprefix)
        mcabbot.message = nil
        if res then
            msg = res
        elseif errmsg then
            msg = "! " .. errmsg
        end
        if (msg) then main.run(cmd..msg) end
    end
end

main.hook("hook-post-message-in", hk_message_in)
main.hook("hook-subscription", hk_subscription)