From 809ca1b526804b78fcc9e44fc23f89dfe1cb70da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Mart=C3=ADnez?= Date: Thu, 28 Mar 2013 23:29:23 -0300 Subject: [PATCH] added msg_out, nick_change, and part callback support --- doc/API.txt | 44 +++++++++++++++++++++++++++++++++++--------- src/callback.lua | 37 +++++++++++++++++++++++++------------ src/init.lua | 6 ++++++ 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/doc/API.txt b/doc/API.txt index 76c09e6..70051c6 100644 --- a/doc/API.txt +++ b/doc/API.txt @@ -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. is the name of the user sending the 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. is the name of the user sending the message. is the recipient of the 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. + and 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. + is the user leaving; 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) diff --git a/src/callback.lua b/src/callback.lua index 7f9deba..9c431f9 100644 --- a/src/callback.lua +++ b/src/callback.lua @@ -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) +]] diff --git a/src/init.lua b/src/init.lua index 2636237..9a73e33 100644 --- a/src/init.lua +++ b/src/init.lua @@ -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