diff --git a/doc/API.txt b/doc/API.txt index bdb08e3..76c09e6 100644 --- a/doc/API.txt +++ b/doc/API.txt @@ -12,7 +12,6 @@ In order to allow your mod to interface with this mod, you must add `irc' REFERENCE --------- -These are the functions defined by the mod: mt_irc.say ( [name ,] message ) Sends to either the channel (if is nil or not specified), @@ -40,6 +39,77 @@ mt_irc.register_bot_command ( name, cmddef ) params = nil, -- No params description = "Greet user", func = function ( from, param ) - mt_irc.say(from, "Hello!"); + mt_irc.say(from, "Hello!") end, }); + +mt_irc.connected_players [ name ] + This table holds the players who are currently on the channel (may be less + than the players in the game). It is modified by the /part and /join chat + commands. + Example: + if (mt_irc.connected_players["joe"]) then + -- Joe is talking on IRC + end + +mt_irc.register_callback ( name, func ) + Registers a function to be called when an event happens. is the name + of the event, and is the function to be called. See CALLBACKS below + for more information + Example: + mt_irc.register_callback("channel_msg", function ( from, msg ) + if (from == "joe") then + mt_irc.say("joe", "You are not allowed to do that!") + return true + end + end) + +This mod also supplies some utility functions: + +string.expandvars ( string, vars ) + Expands all occurrences of the pattern "$(varname)" with the value of + `varname' in the table. Variable names not found on the table + are left verbatim in the string. + Example: + local tpl = "$(foo) $(bar) $(baz)" + local s = tpl:expandvars({ foo=1, bar="Hello" }) + -- `s' now contains "1 Hello $(baz)" + +In addition, all the configuration options decribed in `README.txt' are + available to other mods, though they should be considered "read only". Do + not modify these settings at runtime or you will most likely crash the + server! + + +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 ) + 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. + Example: + mt_irc.register_callback("channel_msg", function ( from, msg ) + if (from == "joe") then + mt_irc.say("joe", "You are not allowed to do that!") + return true + end + end) + +private_msg ( 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. + Example: + mt_irc.register_callback("private_msg", function ( from, to, msg ) + if (to == "admin") then + mt_irc.say(from, "You are not allowed to do that!") + return true + end + end) diff --git a/src/callback.lua b/src/callback.lua index f9a063f..c13ba5c 100644 --- a/src/callback.lua +++ b/src/callback.lua @@ -13,6 +13,26 @@ local irc = require("irc"); +mt_irc.callbacks = { }; + +mt_irc._callback = function ( name, ... ) + 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 + end +end + +mt_irc.register_callback = function ( name, func ) + local list = mt_irc.callbacks[name]; + if (not list) then + list = { }; + mt_irc.callbacks[name] = list; + end + list[#list + 1] = func; +end + minetest.register_on_joinplayer(function ( player ) mt_irc.say(mt_irc.channel, "*** "..player:get_player_name().." joined the game"); @@ -35,6 +55,7 @@ irc.register_callback("channel_msg", function ( channel, from, message ) channel=mt_irc.channel; }; local text = mt_irc.message_format_in:gsub("%$%(([^)]+)%)", t) + if (mt_irc._callback("channel_msg", from, message, text)) then return; end for k, v in pairs(mt_irc.connected_players) do if (v) then minetest.chat_send_player(k, text); end end @@ -89,7 +110,8 @@ irc.register_callback("private_msg", function ( from, message ) port=mt_irc.port; channel=mt_irc.channel; }; - local text = mt_irc.message_format_in:gsub("%$%(([^)]+)%)", t) + local text = mt_irc.message_format_in:expandvars(t); + if (mt_irc._callback("private_msg", from, player_to, message, text)) then return; end minetest.chat_send_player(player_to, "PRIVATE: "..text); end); diff --git a/src/chatcmds.lua b/src/chatcmds.lua index 0cc9ece..f8041b0 100644 --- a/src/chatcmds.lua +++ b/src/chatcmds.lua @@ -30,7 +30,7 @@ minetest.register_chatcommand("msg", { name=name; message=msg; }; - local text = mt_irc.message_format_out:gsub("%$%(([^)]+)%)", t) + local text = mt_irc.message_format_out:expandvars(t); irc.send("PRIVMSG", name, text); end; }); diff --git a/src/init.lua b/src/init.lua index 2673df8..825253d 100644 --- a/src/init.lua +++ b/src/init.lua @@ -116,7 +116,7 @@ mt_irc.connect = function ( ) name=(msg.name or ""); message=(msg.message or ""); }; - local text = mt_irc.message_format_out:gsub("%$%(([^)]+)%)", t) + local text = mt_irc.message_format_out:expandvars(t); irc.say(mt_irc.channel, text); end mt_irc.buffered_messages = nil; @@ -145,6 +145,13 @@ end mt_irc.irc = irc; +-- Misc helpers + +-- Requested by Exio +string.expandvars = function ( s, vars ) + return s:gsub("%$%(([^)]+)%)", vars); +end + dofile(MODPATH.."/callback.lua"); dofile(MODPATH.."/chatcmds.lua"); dofile(MODPATH.."/botcmds.lua");