From f62cd34d4233b9c09ff48c24d4ce4cfbc6377cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Mart=C3=ADnez?= Date: Sun, 16 Dec 2012 13:56:02 -0200 Subject: [PATCH] Added support for joining/parting the channel --- README.txt | 19 ++++++--- doc/CHANGES.txt | 7 +++ src/config.lua | 11 ++++- src/init.lua | 111 ++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 127 insertions(+), 21 deletions(-) diff --git a/README.txt b/README.txt index 486e7fe..1e0605d 100644 --- a/README.txt +++ b/README.txt @@ -111,9 +111,12 @@ All settings are changed in the `config.lua' file. If any of these settings In the default configuration, this will yield: Hello! --- Enable debug output (boolean, default false) -mt_irc.debug = true; + mt_irc.debug (boolean, default false) + Whether to output debug information. + mt_irc.connect_on_join (boolean, default false) + If true, players are connected by default. If false, players + have to use the /irc_connect command to connect to the server. USAGE ----- @@ -122,11 +125,17 @@ Once the game is connected to the IRC channel, chatting using the 'T' or by anyone. Also, when someone sends a message to the channel, that text will be visible in-game. -This mod also adds one chat command: +This mod also adds a few chat commands: - /msg nick message + /msg Sends a private message to the IRC user whose nickname is `nick'. + /join + Join the IRC channel. + + /part + Part the IRC channel. + You can also send private messages from IRC to in-game players, though it's a bit tricky. @@ -135,7 +144,7 @@ To do it, you must send a private message to the "proxy" user (set with >playername message For example, if there's a player named `mtuser', you can send him/her a private message with: - >mtuser Hello! + /msg server_nick >mtuser Hello! To avoid possible misunderstandings (since all in-game players use the same IRC user to converse with you), the "proxy" user will reject any diff --git a/doc/CHANGES.txt b/doc/CHANGES.txt index 3841362..0036607 100644 --- a/doc/CHANGES.txt +++ b/doc/CHANGES.txt @@ -10,6 +10,13 @@ Version 0.1.1: - Now all sources (including Lua itself) are added to a single luasocket lib. This will hopefully fix some cases where the linker b0rk3d with undefined references under Linux. + - Added option to enable/disable connecting when starting the game, and + the /irc_connect chat command to connect manually to the server (as + suggested by OldCoder). The /irc_connect chat command requires the + `irc_admin' privilege. + - Added option to enable/disable joining the channel when a player joins, + and the /join and /part chat commands to join and part the channel + manually if desired (as suggested by OldCoder). Version 0.1.0: - At least it's working :) diff --git a/src/config.lua b/src/config.lua index c745ea7..dd9dc98 100644 --- a/src/config.lua +++ b/src/config.lua @@ -4,8 +4,7 @@ -- ************************* -- Server to connect on joinplayer (string, default "irc.freenode.net") ---mt_irc.server = nil; -mt_irc.server = "localhost"; +mt_irc.server = nil; -- Port to connect on joinplayer (number, default 6667) mt_irc.port = nil; @@ -40,3 +39,11 @@ mt_irc.message_format_in = "<$(name)@IRC> $(message)"; -- Enable debug output (boolean, default false) mt_irc.debug = true; + +-- Whether to automatically join the channed when player joins +-- (boolean, default false) +mt_irc.connect_on_join = false; + +-- Whether to automatically connect to the server on mod load +-- (boolean, default false) +mt_irc.connect_on_load = false; diff --git a/src/init.lua b/src/init.lua index 7b5f098..275f563 100644 --- a/src/init.lua +++ b/src/init.lua @@ -19,6 +19,7 @@ dofile(MODPATH.."/config.lua"); mt_irc.cur_time = 0; mt_irc.buffered_messages = { }; +mt_irc.connected_players = { }; package.path = MODPATH.."/?.lua;"..package.path; package.cpath = MODPATH.."/lib?.so;"..MODPATH.."/?.dll;"..package.cpath; @@ -43,10 +44,19 @@ mt_irc.server = (mt_irc.server or "irc.freenode.net"); mt_irc.port = (mt_irc.port or 6667); mt_irc.channel = (mt_irc.channel or "#minetest-irc-testing"); mt_irc.dtime = (mt_irc.dtime or 0.2); +mt_irc.timeout = (mt_irc.timeout or 60.0); mt_irc.message_format_out = (mt_irc.message_format_out or "<$(nick)> $(message)"); mt_irc.message_format_in = (mt_irc.message_format_in or "<$(name)@IRC[$(channel)]> $(message)"); +if (mt_irc.connect_on_join == nil) then mt_irc.connect_on_join = false; end +if (mt_irc.connect_on_load == nil) then mt_irc.connect_on_load = false; end + +minetest.register_privilege("irc_admin", { + description = "Allow IRC administrative tasks to be performed."; + give_to_singleplayer = true; +}); minetest.register_globalstep(function ( dtime ) + if (not mt_irc.connect_ok) then return; end mt_irc.cur_time = mt_irc.cur_time + dtime; if (mt_irc.cur_time >= mt_irc.dtime) then if (mt_irc.buffered_messages) then @@ -71,17 +81,14 @@ end); minetest.register_on_joinplayer(function ( player ) - if (not mt_irc.connect_ok) then - minetest.chat_send_player(player:get_player_name(), "IRC: Failed to connect to server."); - return; - end - irc.register_callback("connect", function ( ) + if (not mt_irc.connect_ok) then return; end irc.join(mt_irc.channel); irc.say(mt_irc.channel, "*** "..player:get_player_name().." joined the game"); end); - + irc.register_callback("channel_msg", function ( channel, from, message ) + if (not mt_irc.connect_ok) then return; end local t = { name=(from or ""); message=(message or ""); @@ -90,10 +97,13 @@ minetest.register_on_joinplayer(function ( player ) channel=mt_irc.channel; }; local text = mt_irc.message_format_in:gsub("%$%(([^)]+)%)", t) - minetest.chat_send_all(text); + for k, v in pairs(mt_irc.connected_players) do + if (v) then minetest.chat_send_player(k, text); end + end end); irc.register_callback("private_msg", function ( from, message ) + if (not mt_irc.connect_ok) then return; end local player_to; local msg; if (message:sub(1, 1) == ">") then @@ -105,6 +115,10 @@ minetest.register_on_joinplayer(function ( player ) irc.say(from, 'Please use the ">username message" syntax.'); return; end + if (not mt_irc.connected_players[player_to]) then + irc.say(from, "User `"..player_to.."' is not connected to IRC."); + return; + end local t = { name=(from or ""); message=(msg or ""); @@ -117,15 +131,26 @@ minetest.register_on_joinplayer(function ( player ) end); irc.register_callback("nick_change", function ( from, old_nick ) + if (not mt_irc.connect_ok) then return; end end); + mt_irc.connected_players[player:get_player_name()] = mt_irc.connect_on_join; + end); minetest.register_on_leaveplayer(function ( player ) - irc.say(mt_irc.channel, "*** "..player:get_player_name().." left the game"); + if (not mt_irc.connect_ok) then return; end + local name = player:get_player_name(); + mt_irc.connected_players[name] = false; + irc.say(mt_irc.channel, "*** "..name.." left the game"); end); minetest.register_on_chat_message(function ( name, message ) + if (not mt_irc.connected_players[name]) then + minetest.chat_send_player(name, "IRC: You are not connected. Please use /join"); + return; + end + if (not mt_irc.connect_ok) then return; end if (not mt_irc.buffered_messages) then mt_irc.buffered_messages = { }; end @@ -140,6 +165,10 @@ minetest.register_chatcommand("msg", { description = "Send a private message to an IRC user"; privs = { shout=true; }; func = function ( name, param ) + if (not mt_irc.connect_ok) then + minetest.chat_send_player(name, "IRC: You are not connected use /irc_connect."); + return; + end local pos = param:find(" ", 1, true); if (not pos) then return; end local name = param:sub(1, pos - 1); @@ -153,10 +182,64 @@ minetest.register_chatcommand("msg", { end; }); -mt_irc.connect_ok = pcall(irc.connect, { - network = mt_irc.server; - port = mt_irc.port; - nick = mt_irc.server_nick; - pass = mt_irc.password; - timeout = mt_irc.timeout; +minetest.register_chatcommand("irc_connect", { + params = ""; + description = "Connect to the IRC server"; + privs = { irc_admin=true; }; + func = function ( name, param ) + if (mt_irc.connect_ok) then + minetest.chat_send_player(name, "IRC: You are already connected."); + return; + end + mt_irc.connect_ok = pcall(irc.connect, { + network = mt_irc.server; + port = mt_irc.port; + nick = mt_irc.server_nick; + pass = mt_irc.password; + timeout = mt_irc.timeout; + channel = mt_irc.channel; + }); + minetest.chat_send_player(name, "IRC: You are now connected."); + irc.say(mt_irc.channel, name.." joined the channel."); + end; }); + +minetest.register_chatcommand("join", { + params = ""; + description = "Join the IRC channel"; + privs = { shout=true; }; + func = function ( name, param ) + if (mt_irc.connected_players[name]) then + minetest.chat_send_player(name, "IRC: You are already in the channel."); + return; + end + mt_irc.connected_players[name] = true; + minetest.chat_send_player(name, "IRC: You are now in the channel."); + end; +}); + +minetest.register_chatcommand("part", { + params = ""; + description = "Part the IRC channel"; + privs = { shout=true; }; + func = function ( name, param ) + if (not mt_irc.connected_players[name]) then + minetest.chat_send_player(name, "IRC: You are not in the channel."); + return; + end + mt_irc.connected_players[name] = false; + minetest.chat_send_player(name, "IRC: You are now out of the channel."); + irc.say(mt_irc.channel, name.." is no longer in the channel."); + end; +}); + +if (mt_irc.connect_on_load) then + mt_irc.connect_ok = pcall(irc.connect, { + network = mt_irc.server; + port = mt_irc.port; + nick = mt_irc.server_nick; + pass = mt_irc.password; + timeout = mt_irc.timeout; + channel = mt_irc.channel; + }); +end