1
0
mirror of https://github.com/minetest-mods/irc.git synced 2024-11-05 01:30:19 +01:00

added msg_out, nick_change, and part callback support

This commit is contained in:
Diego Martínez 2013-03-28 23:29:23 -03:00
parent f19033185b
commit 809ca1b526
3 changed files with 66 additions and 21 deletions

View File

@ -86,30 +86,56 @@ CALLBACKS
The `mt_irc.register_callback' function can register functions to be called
when some events happen. These are the events supported:
channel_msg ( from, message )
msg_out ( from, message )
Called right before the bot sends a message to the channel.
<from> is the name of the user sending the message. <message> is the
unmodified message sent by the user.
Returning any value other than nil or false will prevent the message from
being sent.
Return values:
"string" New message to be sent.
false Filter out the message (do not send anything).
nil Use original message
other Use a string repr of the value as message.
Example:
mt_irc.register_callback("channel_msg", function ( from, msg )
mt_irc.register_callback("msg_out", function ( from, msg )
if (from == "joe") then
mt_irc.say("joe", "You are not allowed to do that!")
return true
return false
end
end)
private_msg ( from, to, message )
msg_in ( from, to, message )
Called right before the bot sends a private message to an user.
<from> is the name of the user sending the message. <to> is the recipient
of the message. <message> is the unmodified message sent by the user.
Returning any value other than nil or false will prevent the message from
being sent.
Return values:
"string" New message to be sent.
false Filter out the message (do not send anything).
nil Use original message
other Use a string repr of the value as message.
Example:
mt_irc.register_callback("private_msg", function ( from, to, msg )
mt_irc.register_callback("msg_in", function ( from, to, msg )
if (to == "admin") then
mt_irc.say(from, "You are not allowed to do that!")
return true
end
end)
nick_change ( old_nick, new_nick )
Called when an user in IRC changes nickname.
<old_nick> and <new_nick> are self-explanatory.
Return value:
none
Example:
mt_irc.register_callback("nick_change", function ( old_nick, new_nick )
mt_irc.say(from, "Hello "..new_nick.."! You were "..old_nick.."?")
end)
part ( nick, part_msg )
Called when an user leaves the IRC channel.
<nick> is the user leaving; <part_msg> is the "parting message".
Return value:
none
Example:
mt_irc.register_callback("part", function ( nick, part_msg )
mt_irc.say(from, nick.." has left the building!")
end)

View File

@ -15,12 +15,12 @@ local irc = require("irc");
mt_irc.callbacks = { };
mt_irc._callback = function ( name, ... )
mt_irc._callback = function ( name, breakonreturn, ... )
local list = mt_irc.callbacks[name];
if (not list) then return; end
for n = 1, #list do
local r = list[n](...);
if (r) then return r; end
if (breakonreturn and (r ~= nil)) then return r; end
end
end
@ -34,10 +34,17 @@ mt_irc.register_callback = function ( name, func )
end
minetest.register_on_joinplayer(function ( player )
local name = player:get_player_name();
mt_irc.connected_players[name] = mt_irc.auto_join;
if (not mt_irc.connect_ok) then return; end
mt_irc.say("*** "..name.." joined the game");
end);
mt_irc.say(mt_irc.channel, "*** "..player:get_player_name().." joined the game");
mt_irc.connected_players[player:get_player_name()] = mt_irc.auto_join;
minetest.register_on_leaveplayer(function ( player )
local name = player:get_player_name();
mt_irc.connected_players[name] = nil;
if (not mt_irc.connect_ok) then return; end
mt_irc.say("*** "..name.." left the game");
end);
irc.register_callback("connect", function ( )
@ -125,6 +132,7 @@ end);
irc.register_callback("nick_change", function ( from, old_nick )
if (not mt_irc.connect_ok) then return; end
mt_irc._callback("nick_change", false, old_nick, from);
local text = "["..old_nick.." changed his nick to "..from.."]";
for k, v in pairs(mt_irc.connected_players) do
if (v) then minetest.chat_send_player(k, text); end
@ -139,6 +147,7 @@ irc.register_callback("join", function ( servinfo, from )
end);
irc.register_callback("part", function ( servinfo, from, part_msg )
mt_irc._callback("part", false, from, part_msg);
local text = "*** "..from.." left "..mt_irc.channel.." ("..part_msg..")";
for k, v in pairs(mt_irc.connected_players) do
if (v) then minetest.chat_send_player(k, text); end
@ -153,13 +162,6 @@ irc.register_callback("channel_act", function ( servinfo, from, message)
end
end);
minetest.register_on_leaveplayer(function ( player )
local name = player:get_player_name();
mt_irc.connected_players[name] = false;
if (not mt_irc.connect_ok) then return; end
irc.say(mt_irc.channel, "*** "..name.." left the game");
end);
minetest.register_on_chat_message(function ( name, message )
if (not mt_irc.connect_ok) then return; end
if (message:sub(1, 1) == "/") then return; end
@ -211,3 +213,14 @@ irc.handlers.on_err_nicknameinuse = function ( from, respond_to )
mt_irc.server_nick = mt_irc.server_nick:sub(1, -2)..n;
mt_irc.connect();
end
-- TESTING
--[[
mt_irc.register_callback("part", function ( nick, part_msg )
mt_irc.say("TEST: "..nick.." has left the building!");
end)
mt_irc.register_callback("nick_change", function ( old_nick, new_nick )
mt_irc.say("TEST: "..old_nick.." -> "..new_nick);
end)
]]

View File

@ -133,6 +133,12 @@ mt_irc.say = function ( to, msg )
end
to = to or mt_irc.channel;
msg = msg or "";
local msg2 = mt_irc._callback("msg_out", true, to, msg);
if ((type(msg2) == "boolean") and (not msg2)) then
return;
elseif (msg2 ~= nil) then
msg = tostring(msg);
end
irc.say(to, msg);
end