mirror of
https://github.com/minetest-mods/irc.git
synced 2024-12-28 09:40:17 +01:00
added msg_out, nick_change, and part callback support
This commit is contained in:
parent
f19033185b
commit
809ca1b526
44
doc/API.txt
44
doc/API.txt
@ -86,30 +86,56 @@ CALLBACKS
|
|||||||
The `mt_irc.register_callback' function can register functions to be called
|
The `mt_irc.register_callback' function can register functions to be called
|
||||||
when some events happen. These are the events supported:
|
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.
|
Called right before the bot sends a message to the channel.
|
||||||
<from> is the name of the user sending the message. <message> is the
|
<from> is the name of the user sending the message. <message> is the
|
||||||
unmodified message sent by the user.
|
unmodified message sent by the user.
|
||||||
Returning any value other than nil or false will prevent the message from
|
Return values:
|
||||||
being sent.
|
"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:
|
Example:
|
||||||
mt_irc.register_callback("channel_msg", function ( from, msg )
|
mt_irc.register_callback("msg_out", function ( from, msg )
|
||||||
if (from == "joe") then
|
if (from == "joe") then
|
||||||
mt_irc.say("joe", "You are not allowed to do that!")
|
mt_irc.say("joe", "You are not allowed to do that!")
|
||||||
return true
|
return false
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
private_msg ( from, to, message )
|
msg_in ( from, to, message )
|
||||||
Called right before the bot sends a private message to an user.
|
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
|
<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.
|
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
|
Return values:
|
||||||
being sent.
|
"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:
|
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
|
if (to == "admin") then
|
||||||
mt_irc.say(from, "You are not allowed to do that!")
|
mt_irc.say(from, "You are not allowed to do that!")
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
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)
|
||||||
|
@ -15,12 +15,12 @@ local irc = require("irc");
|
|||||||
|
|
||||||
mt_irc.callbacks = { };
|
mt_irc.callbacks = { };
|
||||||
|
|
||||||
mt_irc._callback = function ( name, ... )
|
mt_irc._callback = function ( name, breakonreturn, ... )
|
||||||
local list = mt_irc.callbacks[name];
|
local list = mt_irc.callbacks[name];
|
||||||
if (not list) then return; end
|
if (not list) then return; end
|
||||||
for n = 1, #list do
|
for n = 1, #list do
|
||||||
local r = list[n](...);
|
local r = list[n](...);
|
||||||
if (r) then return r; end
|
if (breakonreturn and (r ~= nil)) then return r; end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -34,10 +34,17 @@ mt_irc.register_callback = function ( name, func )
|
|||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function ( player )
|
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");
|
minetest.register_on_leaveplayer(function ( player )
|
||||||
mt_irc.connected_players[player:get_player_name()] = mt_irc.auto_join;
|
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);
|
end);
|
||||||
|
|
||||||
irc.register_callback("connect", function ( )
|
irc.register_callback("connect", function ( )
|
||||||
@ -125,6 +132,7 @@ end);
|
|||||||
|
|
||||||
irc.register_callback("nick_change", function ( from, old_nick )
|
irc.register_callback("nick_change", function ( from, old_nick )
|
||||||
if (not mt_irc.connect_ok) then return; end
|
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.."]";
|
local text = "["..old_nick.." changed his nick to "..from.."]";
|
||||||
for k, v in pairs(mt_irc.connected_players) do
|
for k, v in pairs(mt_irc.connected_players) do
|
||||||
if (v) then minetest.chat_send_player(k, text); end
|
if (v) then minetest.chat_send_player(k, text); end
|
||||||
@ -139,6 +147,7 @@ irc.register_callback("join", function ( servinfo, from )
|
|||||||
end);
|
end);
|
||||||
|
|
||||||
irc.register_callback("part", function ( servinfo, from, part_msg )
|
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..")";
|
local text = "*** "..from.." left "..mt_irc.channel.." ("..part_msg..")";
|
||||||
for k, v in pairs(mt_irc.connected_players) do
|
for k, v in pairs(mt_irc.connected_players) do
|
||||||
if (v) then minetest.chat_send_player(k, text); end
|
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
|
||||||
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 )
|
minetest.register_on_chat_message(function ( name, message )
|
||||||
if (not mt_irc.connect_ok) then return; end
|
if (not mt_irc.connect_ok) then return; end
|
||||||
if (message:sub(1, 1) == "/") 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.server_nick = mt_irc.server_nick:sub(1, -2)..n;
|
||||||
mt_irc.connect();
|
mt_irc.connect();
|
||||||
end
|
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)
|
||||||
|
]]
|
||||||
|
@ -133,6 +133,12 @@ mt_irc.say = function ( to, msg )
|
|||||||
end
|
end
|
||||||
to = to or mt_irc.channel;
|
to = to or mt_irc.channel;
|
||||||
msg = msg or "";
|
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);
|
irc.say(to, msg);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user