2012-12-01 04:06:15 +01:00
|
|
|
-- IRC Mod for Minetest
|
|
|
|
-- (C) 2012 Diego Martínez <kaeza@users.sf.net>
|
|
|
|
--
|
|
|
|
-- This mod allows to tie a Minetest server to an IRC channel.
|
|
|
|
--
|
|
|
|
-- This program is free software. It comes without any warranty, to
|
|
|
|
-- the extent permitted by applicable law. You can redistribute it
|
|
|
|
-- and/or modify it under the terms of the Do What The Fuck You Want
|
|
|
|
-- To Public License, Version 2, as published by Sam Hocevar. See
|
|
|
|
-- http://sam.zoy.org/wtfpl/COPYING for more details.
|
|
|
|
--
|
|
|
|
|
2012-12-14 23:33:44 +01:00
|
|
|
local MODPATH = minetest.get_modpath("irc");
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2012-12-14 23:33:44 +01:00
|
|
|
mt_irc = { };
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2012-12-14 23:33:44 +01:00
|
|
|
dofile(MODPATH.."/config.lua");
|
2012-12-01 04:06:15 +01:00
|
|
|
|
2012-12-14 23:33:44 +01:00
|
|
|
mt_irc.cur_time = 0;
|
|
|
|
mt_irc.buffered_messages = { };
|
2012-12-16 16:56:02 +01:00
|
|
|
mt_irc.connected_players = { };
|
2012-12-01 04:06:15 +01:00
|
|
|
|
|
|
|
package.path = MODPATH.."/?.lua;"..package.path;
|
|
|
|
package.cpath = MODPATH.."/lib?.so;"..MODPATH.."/?.dll;"..package.cpath;
|
|
|
|
|
|
|
|
local irc = require 'irc';
|
|
|
|
|
2012-12-14 23:33:44 +01:00
|
|
|
irc.DEBUG = ((mt_irc.debug and true) or false);
|
|
|
|
|
|
|
|
-- Set defaults if not specified.
|
|
|
|
if (not mt_irc.server_nick) then
|
|
|
|
local pr = PseudoRandom(os.time());
|
|
|
|
-- Workaround for bad distribution in minetest PRNG implementation.
|
|
|
|
local fmt = "minetest-%02X%02X%02X%02X";
|
|
|
|
mt_irc.server_nick = fmt:format(
|
|
|
|
pr:next(0, 255),
|
|
|
|
pr:next(0, 255),
|
|
|
|
pr:next(0, 255),
|
|
|
|
pr:next(0, 255)
|
|
|
|
);
|
|
|
|
end
|
|
|
|
mt_irc.server = (mt_irc.server or "irc.freenode.net");
|
|
|
|
mt_irc.port = (mt_irc.port or 6667);
|
2012-12-17 07:28:37 +01:00
|
|
|
mt_irc.channel = (mt_irc.channel or "#mtgamechatforservers");
|
2012-12-14 23:33:44 +01:00
|
|
|
mt_irc.dtime = (mt_irc.dtime or 0.2);
|
2012-12-16 16:56:02 +01:00
|
|
|
mt_irc.timeout = (mt_irc.timeout or 60.0);
|
2012-12-14 23:33:44 +01:00
|
|
|
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)");
|
2012-12-16 16:56:02 +01:00
|
|
|
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;
|
|
|
|
});
|
2012-12-01 04:06:15 +01:00
|
|
|
|
|
|
|
minetest.register_globalstep(function ( dtime )
|
2012-12-16 16:56:02 +01:00
|
|
|
if (not mt_irc.connect_ok) then return; end
|
2012-12-01 04:06:15 +01:00
|
|
|
mt_irc.cur_time = mt_irc.cur_time + dtime;
|
2012-12-14 23:33:44 +01:00
|
|
|
if (mt_irc.cur_time >= mt_irc.dtime) then
|
2012-12-01 04:06:15 +01:00
|
|
|
if (mt_irc.buffered_messages) then
|
2012-12-14 23:33:44 +01:00
|
|
|
for _, msg in ipairs(mt_irc.buffered_messages) do
|
|
|
|
local t = {
|
|
|
|
name=(msg.name or "<BUG:no one is saying this>");
|
|
|
|
message=(msg.message or "<BUG:there is no message>");
|
|
|
|
};
|
|
|
|
local text = mt_irc.message_format_out:gsub("%$%(([^)]+)%)", t)
|
|
|
|
irc.say(mt_irc.channel, text);
|
2012-12-01 04:06:15 +01:00
|
|
|
end
|
|
|
|
mt_irc.buffered_messages = nil;
|
|
|
|
end
|
|
|
|
irc.poll();
|
2012-12-14 23:33:44 +01:00
|
|
|
mt_irc.cur_time = mt_irc.cur_time - mt_irc.dtime;
|
2012-12-19 02:15:48 +01:00
|
|
|
--local plys = minetest.get_connected_players();
|
|
|
|
--if ((#plys <= 0) and (minetest.is_singleplayer())) then
|
|
|
|
-- minetest.after(1.0, function ( )
|
|
|
|
-- irc.quit("Closing.");
|
|
|
|
-- end)
|
|
|
|
--end
|
2012-12-01 04:06:15 +01:00
|
|
|
end
|
|
|
|
end);
|
|
|
|
|
2012-12-19 02:15:48 +01:00
|
|
|
local function do_connect ( )
|
|
|
|
end
|
|
|
|
|
2012-12-01 04:06:15 +01:00
|
|
|
minetest.register_on_joinplayer(function ( player )
|
|
|
|
|
|
|
|
irc.register_callback("connect", function ( )
|
2012-12-14 23:33:44 +01:00
|
|
|
irc.join(mt_irc.channel);
|
|
|
|
irc.say(mt_irc.channel, "*** "..player:get_player_name().." joined the game");
|
2012-12-18 06:03:44 +01:00
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
mt_irc.connected_players[player:get_player_name()] = mt_irc.connect_on_join;
|
|
|
|
end
|
2012-12-01 04:06:15 +01:00
|
|
|
end);
|
2012-12-16 16:56:02 +01:00
|
|
|
|
2012-12-01 04:06:15 +01:00
|
|
|
irc.register_callback("channel_msg", function ( channel, from, message )
|
2012-12-16 16:56:02 +01:00
|
|
|
if (not mt_irc.connect_ok) then return; end
|
2012-12-14 23:33:44 +01:00
|
|
|
local t = {
|
|
|
|
name=(from or "<BUG:no one is saying this>");
|
|
|
|
message=(message or "<BUG:there is no message>");
|
|
|
|
server=mt_irc.server;
|
|
|
|
port=mt_irc.port;
|
|
|
|
channel=mt_irc.channel;
|
|
|
|
};
|
|
|
|
local text = mt_irc.message_format_in:gsub("%$%(([^)]+)%)", t)
|
2012-12-16 16:56:02 +01:00
|
|
|
for k, v in pairs(mt_irc.connected_players) do
|
|
|
|
if (v) then minetest.chat_send_player(k, text); end
|
|
|
|
end
|
2012-12-01 04:06:15 +01:00
|
|
|
end);
|
2012-12-14 23:33:44 +01:00
|
|
|
|
2012-12-01 04:06:15 +01:00
|
|
|
irc.register_callback("private_msg", function ( from, message )
|
2012-12-16 16:56:02 +01:00
|
|
|
if (not mt_irc.connect_ok) then return; end
|
2012-12-14 23:33:44 +01:00
|
|
|
local player_to;
|
|
|
|
local msg;
|
|
|
|
if (message:sub(1, 1) == ">") then
|
|
|
|
local pos = message:find(" ", 1, true);
|
|
|
|
if (not pos) then return; end
|
|
|
|
player_to = message:sub(2, pos - 1);
|
|
|
|
msg = message:sub(pos + 1);
|
|
|
|
else
|
|
|
|
irc.say(from, 'Please use the ">username message" syntax.');
|
|
|
|
return;
|
|
|
|
end
|
2012-12-16 16:56:02 +01:00
|
|
|
if (not mt_irc.connected_players[player_to]) then
|
|
|
|
irc.say(from, "User `"..player_to.."' is not connected to IRC.");
|
|
|
|
return;
|
|
|
|
end
|
2012-12-14 23:33:44 +01:00
|
|
|
local t = {
|
|
|
|
name=(from or "<BUG:no one is saying this>");
|
|
|
|
message=(msg or "<BUG:there is no message>");
|
|
|
|
server=mt_irc.server;
|
|
|
|
port=mt_irc.port;
|
|
|
|
channel=mt_irc.channel;
|
|
|
|
};
|
|
|
|
local text = mt_irc.message_format_in:gsub("%$%(([^)]+)%)", t)
|
|
|
|
minetest.chat_send_player(player_to, "PRIVATE: "..text);
|
2012-12-01 04:06:15 +01:00
|
|
|
end);
|
2012-12-14 23:33:44 +01:00
|
|
|
|
2012-12-01 04:06:15 +01:00
|
|
|
irc.register_callback("nick_change", function ( from, old_nick )
|
2012-12-16 16:56:02 +01:00
|
|
|
if (not mt_irc.connect_ok) then return; end
|
2012-12-01 04:06:15 +01:00
|
|
|
end);
|
|
|
|
|
|
|
|
end);
|
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function ( player )
|
2012-12-16 16:56:02 +01:00
|
|
|
local name = player:get_player_name();
|
|
|
|
mt_irc.connected_players[name] = false;
|
2012-12-18 06:03:44 +01:00
|
|
|
if (not mt_irc.connect_ok) then return; end
|
2012-12-16 16:56:02 +01:00
|
|
|
irc.say(mt_irc.channel, "*** "..name.." left the game");
|
2012-12-01 04:06:15 +01:00
|
|
|
end);
|
|
|
|
|
|
|
|
minetest.register_on_chat_message(function ( name, message )
|
2012-12-16 16:56:02 +01:00
|
|
|
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
|
2012-12-01 04:06:15 +01:00
|
|
|
if (not mt_irc.buffered_messages) then
|
|
|
|
mt_irc.buffered_messages = { };
|
|
|
|
end
|
|
|
|
mt_irc.buffered_messages[#mt_irc.buffered_messages + 1] = {
|
|
|
|
name = name;
|
|
|
|
message = message;
|
|
|
|
};
|
|
|
|
end);
|
|
|
|
|
2012-12-14 23:33:44 +01:00
|
|
|
minetest.register_chatcommand("msg", {
|
|
|
|
params = "<name> <message>";
|
|
|
|
description = "Send a private message to an IRC user";
|
|
|
|
privs = { shout=true; };
|
|
|
|
func = function ( name, param )
|
2012-12-16 16:56:02 +01:00
|
|
|
if (not mt_irc.connect_ok) then
|
|
|
|
minetest.chat_send_player(name, "IRC: You are not connected use /irc_connect.");
|
|
|
|
return;
|
|
|
|
end
|
2012-12-14 23:33:44 +01:00
|
|
|
local pos = param:find(" ", 1, true);
|
|
|
|
if (not pos) then return; end
|
2012-12-15 18:00:40 +01:00
|
|
|
local name = param:sub(1, pos - 1);
|
2012-12-14 23:33:44 +01:00
|
|
|
local msg = param:sub(pos + 1);
|
|
|
|
local t = {
|
2012-12-18 05:50:53 +01:00
|
|
|
name=name;
|
2012-12-14 23:33:44 +01:00
|
|
|
message=msg;
|
|
|
|
};
|
|
|
|
local text = mt_irc.message_format_out:gsub("%$%(([^)]+)%)", t)
|
2012-12-15 18:00:40 +01:00
|
|
|
irc.send("PRIVMSG", name, text);
|
2012-12-14 23:33:44 +01:00
|
|
|
end;
|
|
|
|
});
|
|
|
|
|
2012-12-16 16:56:02 +01:00
|
|
|
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;
|
2012-12-01 04:06:15 +01:00
|
|
|
});
|
2012-12-16 16:56:02 +01:00
|
|
|
|
|
|
|
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;
|
2012-12-17 07:00:13 +01:00
|
|
|
-- Best way I could get bot to autojoin channel was to add the irc.join function here.
|
|
|
|
-- Bot won't connect until the first user joins. The bot will not disconect if last player leaves.
|
2012-12-19 02:26:58 +01:00
|
|
|
irc.join(mt_irc.channel);
|
2012-12-16 16:56:02 +01:00
|
|
|
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;
|
|
|
|
});
|
2012-12-18 05:50:53 +01:00
|
|
|
if (not mt_irc.connect_ok) then
|
|
|
|
local s = "DEBUG: irc.connect failed";
|
|
|
|
minetest.chat_send_all(s);
|
|
|
|
end
|
2012-12-16 16:56:02 +01:00
|
|
|
end
|