1
0
mirror of https://github.com/minetest-mods/irc.git synced 2024-11-16 06:40:19 +01:00
irc/hooks.lua

264 lines
6.8 KiB
Lua
Raw Normal View History

2013-04-30 00:07:44 +02:00
-- This file is licensed under the terms of the BSD 2-clause license.
-- See LICENSE.txt for details.
2015-05-17 01:54:24 +02:00
local ie = ...
-- MIME is part of LuaSocket
2015-05-17 01:54:24 +02:00
local b64e = ie.require("mime").b64
2013-04-30 00:07:44 +02:00
irc.hooks = {}
irc.registered_hooks = {}
2013-04-30 00:07:44 +02:00
2017-01-12 02:37:54 +01:00
local stripped_chars = "[\2\31]"
local function normalize(text)
2014-05-06 21:26:13 +02:00
-- Strip colors
text = text:gsub("\3[0-9][0-9,]*", "")
return text:gsub(stripped_chars, "")
end
function irc.doHook(conn)
for name, hook in pairs(irc.registered_hooks) do
2013-04-30 00:07:44 +02:00
for _, func in pairs(hook) do
conn:hook(name, func)
end
end
end
function irc.register_hook(name, func)
irc.registered_hooks[name] = irc.registered_hooks[name] or {}
table.insert(irc.registered_hooks[name], func)
2013-04-30 00:07:44 +02:00
end
function irc.hooks.raw(line)
if irc.config.debug then
2013-04-30 00:07:44 +02:00
print("RECV: "..line)
end
end
function irc.hooks.send(line)
if irc.config.debug then
2013-04-30 00:07:44 +02:00
print("SEND: "..line)
end
end
function irc.hooks.chat(msg)
2014-05-06 21:26:13 +02:00
local channel, text = msg.args[1], msg.args[2]
if text:sub(1, 1) == string.char(1) then
irc.conn:invoke("OnCTCP", msg)
2013-10-08 18:29:22 +02:00
return
end
if channel == irc.conn.nick then
irc.last_from = msg.user.nick
irc.conn:invoke("PrivateMessage", msg)
2013-04-30 00:07:44 +02:00
else
irc.last_from = channel
irc.conn:invoke("OnChannelChat", msg)
2013-10-08 18:29:22 +02:00
end
end
local function get_core_version()
local status = minetest.get_server_status()
local start_pos = select(2, status:find("version=", 1, true))
local end_pos = status:find(",", start_pos, true)
return status:sub(start_pos + 1, end_pos - 1)
end
function irc.hooks.ctcp(msg)
2014-05-06 21:26:13 +02:00
local text = msg.args[2]:sub(2, -2) -- Remove ^C
local args = text:split(' ')
2013-10-23 14:32:03 +02:00
local command = args[1]:upper()
local function reply(s)
irc.queue(irc.msgs.notice(msg.user.nick,
2014-05-06 21:26:13 +02:00
("\1%s %s\1"):format(command, s)))
2013-10-23 14:32:03 +02:00
end
if command == "ACTION" and msg.args[1] == irc.config.channel then
2014-05-06 21:26:13 +02:00
local action = text:sub(8, -1)
irc.sendLocal(("* %s@IRC %s"):format(msg.user.nick, action))
2013-10-23 14:32:03 +02:00
elseif command == "VERSION" then
reply(("Minetest version %s, IRC mod version %s.")
:format(get_core_version(), irc.version))
2013-10-23 14:32:03 +02:00
elseif command == "PING" then
reply(args[2])
elseif command == "TIME" then
reply(os.date())
2013-04-30 00:07:44 +02:00
end
end
function irc.hooks.channelChat(msg)
2014-05-06 21:26:13 +02:00
local text = normalize(msg.args[2])
irc.check_botcmd(msg)
-- Don't let a user impersonate someone else by using the nick "IRC"
2017-03-25 02:33:43 +01:00
local fake = msg.user.nick:lower():match("^[il|]rc$")
if fake then
irc.sendLocal("<"..msg.user.nick.."@IRC> "..text)
return
end
-- Support multiple servers in a channel better by converting:
-- "<server@IRC> <player> message" into "<player@server> message"
2014-05-06 21:26:13 +02:00
-- "<server@IRC> *** player joined/left the game" into "*** player joined/left server"
-- and "<server@IRC> * player orders a pizza" into "* player@server orders a pizza"
2013-12-03 00:15:29 +01:00
local foundchat, _, chatnick, chatmessage =
2014-05-06 21:26:13 +02:00
text:find("^<([^>]+)> (.*)$")
2013-12-03 00:15:29 +01:00
local foundjoin, _, joinnick =
2014-05-06 21:26:13 +02:00
text:find("^%*%*%* ([^%s]+) joined the game$")
2013-12-03 00:15:29 +01:00
local foundleave, _, leavenick =
2014-05-06 21:26:13 +02:00
text:find("^%*%*%* ([^%s]+) left the game$")
local foundtimedout, _, timedoutnick =
text:find("^%*%*%* ([^%s]+) left the game %(Timed out%)$")
2013-12-03 00:15:29 +01:00
local foundaction, _, actionnick, actionmessage =
2014-05-06 21:26:13 +02:00
text:find("^%* ([^%s]+) (.*)$")
2013-12-03 00:15:29 +01:00
2014-05-06 21:26:13 +02:00
if text:sub(1, 5) == "[off]" then
return
2013-12-03 00:15:29 +01:00
elseif foundchat then
irc.sendLocal(("<%s@%s> %s")
2014-05-06 21:26:13 +02:00
:format(chatnick, msg.user.nick, chatmessage))
elseif foundjoin then
irc.sendLocal(("*** %s joined %s")
2014-05-06 21:26:13 +02:00
:format(joinnick, msg.user.nick))
elseif foundleave then
irc.sendLocal(("*** %s left %s")
2014-05-06 21:26:13 +02:00
:format(leavenick, msg.user.nick))
elseif foundtimedout then
irc.sendLocal(("*** %s left %s (Timed out)")
:format(timedoutnick, msg.user.nick))
elseif foundaction then
irc.sendLocal(("* %s@%s %s")
2014-05-06 21:26:13 +02:00
:format(actionnick, msg.user.nick, actionmessage))
else
irc.sendLocal(("<%s@IRC> %s"):format(msg.user.nick, text))
end
2013-04-30 00:07:44 +02:00
end
function irc.hooks.pm(msg)
2013-12-03 00:15:29 +01:00
-- Trim prefix if it is found
2014-05-06 21:26:13 +02:00
local text = msg.args[2]
local prefix = irc.config.command_prefix
2014-05-06 21:26:13 +02:00
if prefix and text:sub(1, #prefix) == prefix then
text = text:sub(#prefix + 1)
2013-04-30 00:07:44 +02:00
end
irc.bot_command(msg, text)
2013-04-30 00:07:44 +02:00
end
function irc.hooks.kick(channel, target, prefix, reason)
if target == irc.conn.nick then
2013-04-30 00:07:44 +02:00
minetest.chat_send_all("IRC: kicked from "..channel.." by "..prefix.nick..".")
irc.disconnect("Kicked")
2013-04-30 00:07:44 +02:00
else
irc.sendLocal(("-!- %s was kicked from %s by %s [%s]")
2013-04-30 00:07:44 +02:00
:format(target, channel, prefix.nick, reason))
end
end
function irc.hooks.notice(user, target, message)
if user.nick and target == irc.config.channel then
irc.sendLocal("-"..user.nick.."@IRC- "..message)
2014-05-06 21:26:13 +02:00
end
2013-04-30 00:07:44 +02:00
end
function irc.hooks.mode(user, target, modes, ...)
2013-04-30 00:07:44 +02:00
local by = ""
if user.nick then
by = " by "..user.nick
end
local options = ""
2014-05-06 21:26:13 +02:00
if select("#", ...) > 0 then
options = " "
2013-04-30 00:07:44 +02:00
end
2014-05-06 21:26:13 +02:00
options = options .. table.concat({...}, " ")
2013-04-30 00:07:44 +02:00
minetest.chat_send_all(("-!- mode/%s [%s%s]%s")
:format(target, modes, options, by))
end
function irc.hooks.nick(user, newNick)
irc.sendLocal(("-!- %s is now known as %s")
2013-04-30 00:07:44 +02:00
:format(user.nick, newNick))
end
function irc.hooks.join(user, channel)
irc.sendLocal(("-!- %s joined %s")
2013-04-30 00:07:44 +02:00
:format(user.nick, channel))
end
function irc.hooks.part(user, channel, reason)
2013-04-30 00:07:44 +02:00
reason = reason or ""
irc.sendLocal(("-!- %s has left %s [%s]")
2013-04-30 00:07:44 +02:00
:format(user.nick, channel, reason))
end
function irc.hooks.quit(user, reason)
irc.sendLocal(("-!- %s has quit [%s]")
2013-04-30 00:07:44 +02:00
:format(user.nick, reason))
end
function irc.hooks.disconnect(_, isError)
irc.connected = false
2013-04-30 00:07:44 +02:00
if isError then
minetest.log("error", "IRC: Error: Disconnected, reconnecting in one minute.")
minetest.chat_send_all("IRC: Error: Disconnected, reconnecting in one minute.")
minetest.after(60, irc.connect, irc)
2013-04-30 00:07:44 +02:00
else
minetest.log("action", "IRC: Disconnected.")
minetest.chat_send_all("IRC: Disconnected.")
end
end
function irc.hooks.preregister(conn)
if not (irc.config["sasl.user"] and irc.config["sasl.pass"]) then return end
local authString = b64e(
2013-04-30 00:07:44 +02:00
("%s\x00%s\x00%s"):format(
irc.config["sasl.user"],
irc.config["sasl.user"],
irc.config["sasl.pass"])
2013-04-30 00:07:44 +02:00
)
conn:send("CAP REQ sasl")
conn:send("AUTHENTICATE PLAIN")
conn:send("AUTHENTICATE "..authString)
2015-03-10 18:33:41 +01:00
conn:send("CAP END")
2013-04-30 00:07:44 +02:00
end
irc.register_hook("PreRegister", irc.hooks.preregister)
irc.register_hook("OnRaw", irc.hooks.raw)
irc.register_hook("OnSend", irc.hooks.send)
irc.register_hook("DoPrivmsg", irc.hooks.chat)
irc.register_hook("OnPart", irc.hooks.part)
irc.register_hook("OnKick", irc.hooks.kick)
irc.register_hook("OnJoin", irc.hooks.join)
irc.register_hook("OnQuit", irc.hooks.quit)
irc.register_hook("NickChange", irc.hooks.nick)
irc.register_hook("OnCTCP", irc.hooks.ctcp)
irc.register_hook("PrivateMessage", irc.hooks.pm)
irc.register_hook("OnNotice", irc.hooks.notice)
irc.register_hook("OnChannelChat", irc.hooks.channelChat)
irc.register_hook("OnModeChange", irc.hooks.mode)
irc.register_hook("OnDisconnect", irc.hooks.disconnect)
2013-04-30 00:07:44 +02:00