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


--  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 home = os.getenv("HOME")
package.path = package.path..
                ";"..home.."/.mcabber/mcabbot/?.lua"..
                ";"..home.."/.mcabber/mcabbot/mcbot/?.lua"
home = nil

require "mcbot.mcbot_engine"

local mcabbot = {}
mcabbot.nickname = "McBot"
mcabbot.jid = "mcbot@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.error and h.error == "true" then return end

    if h.groupchat and h.groupchat == "true" then
        muc = true
    end
    if muc == true then
        if h.delayed == "" then
            if mcabbot.cmdprefix and message:starts(mcabbot.cmdprefix) then
                perso = true
            elseif h.attention and h.attention == "true" and
                   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)