Code Refactoring

This commit is contained in:
Diego Martínez 2012-12-22 01:16:28 -02:00
parent 5c624d97a5
commit c6bf59e203
7 changed files with 203 additions and 158 deletions

View File

@ -133,10 +133,14 @@ add_custom_target(copy_files_mt_irc
COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
COMMAND ${CMAKE_COMMAND} -E copy src/init.lua ${dir}
COMMAND ${CMAKE_COMMAND} -E copy src/config.lua ${dir}
COMMAND ${CMAKE_COMMAND} -E copy src/callback.lua ${dir}
COMMAND ${CMAKE_COMMAND} -E copy src/friends.lua ${dir}
COMMAND ${CMAKE_COMMAND} -E copy src/chatcmds.lua ${dir}
COMMAND ${CMAKE_COMMAND} -E copy README.txt ${dir}
COMMAND ${CMAKE_COMMAND} -E copy doc/LICENSE.txt ${dir}
COMMAND ${CMAKE_COMMAND} -E copy doc/CHANGES.txt ${dir}
COMMAND ${CMAKE_COMMAND} -E copy doc/TODO.txt ${dir}
COMMAND ${CMAKE_COMMAND} -E copy doc/TESTED_ON.txt ${dir}
)
add_custom_target(pack_mod

View File

@ -1,11 +1,15 @@
This was successfully tested on the following systems and found to compile, install and work properly in minetest.
This was successfully tested on the following systems and found to compile,
install and work properly in minetest.
ArchLinx
--------
GCC 4.7.2
CMAKE 2.8.10.1
Ubuntu 12.04
------
GCC 4.6.3-1ubuntu5
CMake 2.8.7
Ubuntu 12.10
------
@ -13,11 +17,9 @@ GCC 4.7.2-2ubuntu1
CMAKE 2.8.9
Fails on:
LinuxMint
---------
GCC 4.5.2
CMAKE 2.8.3

View File

@ -8,5 +8,5 @@ cd ..;
"$dir/zipmod.sh";
echo cp -f dists/* ~/Dropbox/Public/;
cp -f dists/* ~/Dropbox/Public/;
echo cp -f dists/* ~/Dropbox/Public/minetest/mods/;
cp -f dists/* ~/Dropbox/Public/minetest/mods/;

82
src/callback.lua Normal file
View File

@ -0,0 +1,82 @@
minetest.register_on_joinplayer(function ( player )
irc.say(mt_irc.channel, "*** "..player:get_player_name().." joined the game");
end);
irc.register_callback("connect", function ( )
irc.join(mt_irc.channel);
for _,player in ipairs(minetest.get_connected_players()) do
mt_irc.connected_players[player:get_player_name()] = mt_irc.connect_on_join;
end
end);
irc.register_callback("channel_msg", function ( channel, from, message )
if (not mt_irc.connect_ok) then return; end
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)
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
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
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 "<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);
end);
irc.register_callback("nick_change", function ( from, old_nick )
if (not mt_irc.connect_ok) then return; 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.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
mt_irc.buffered_messages[#mt_irc.buffered_messages + 1] = {
name = name;
message = message;
};
end);

72
src/chatcmds.lua Normal file
View File

@ -0,0 +1,72 @@
minetest.register_chatcommand("msg", {
params = "<name> <message>";
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);
local msg = param:sub(pos + 1);
local t = {
name=name;
message=msg;
};
local text = mt_irc.message_format_out:gsub("%$%(([^)]+)%)", t)
irc.send("PRIVMSG", name, text);
end;
});
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 )
mt_irc.join(name);
end;
});
minetest.register_chatcommand("part", {
params = "";
description = "Part the IRC channel";
privs = { shout=true; };
func = function ( name, param )
mt_irc.part(name);
end;
});
minetest.register_chatcommand("me", {
params = "<action>";
description = "chat action (eg. /me orders a pizza)";
privs = { shout=true };
func = function(name, param)
minetest.chat_send_all("* "..name.." "..param);
mt_irc.send({from=name; action=param;});
end,
})

10
src/friends.lua Normal file
View File

@ -0,0 +1,10 @@
-- TODO
--[[
local MODPATH = mt_irc.modpath;
local function load_friends_list ( )
end
]]

View File

@ -19,6 +19,7 @@ dofile(MODPATH.."/config.lua");
mt_irc.cur_time = 0;
mt_irc.buffered_messages = { };
mt_irc.connected_players = { };
mt_irc.modpath = MODPATH;
package.path = MODPATH.."/?.lua;"..package.path;
package.cpath = MODPATH.."/lib?.so;"..MODPATH.."/?.dll;"..package.cpath;
@ -80,168 +81,33 @@ minetest.register_globalstep(function ( dtime )
end
end);
local function do_connect ( )
mt_irc.part = function ( name )
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.send(mt_irc.channel, name.." is no longer in the channel.");
mt_irc.send(name.." is no longer in the channel.");
end
minetest.register_on_joinplayer(function ( player )
irc.say(mt_irc.channel, "*** "..player:get_player_name().." joined the game");
end);
irc.register_callback("connect", function ( )
irc.join(mt_irc.channel);
for _,player in ipairs(minetest.get_connected_players()) do
mt_irc.connected_players[player:get_player_name()] = mt_irc.connect_on_join;
end
end);
irc.register_callback("channel_msg", function ( channel, from, message )
if (not mt_irc.connect_ok) then return; end
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)
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
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
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 "<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);
end);
irc.register_callback("nick_change", function ( from, old_nick )
if (not mt_irc.connect_ok) then return; 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.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
mt_irc.buffered_messages[#mt_irc.buffered_messages + 1] = {
name = name;
message = message;
};
end);
minetest.register_chatcommand("msg", {
params = "<name> <message>";
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);
local msg = param:sub(pos + 1);
local t = {
name=name;
message=msg;
};
local text = mt_irc.message_format_out:gsub("%$%(([^)]+)%)", t)
irc.send("PRIVMSG", name, text);
end;
});
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 )
mt_irc.join = function ( name )
local function do_join ( name )
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;
-- 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.
irc.join(mt_irc.channel);
mt_irc.join(mt_irc.channel);
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
end
if (not pcall(do_join, name)) then
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;
});
end
end
if (mt_irc.connect_on_load) then
mt_irc.connect = function ( )
mt_irc.connect_ok = pcall(irc.connect, {
network = mt_irc.server;
port = mt_irc.port;
@ -252,6 +118,15 @@ if (mt_irc.connect_on_load) then
});
if (not mt_irc.connect_ok) then
local s = "DEBUG: irc.connect failed";
minetest.debug(s);
minetest.chat_send_all(s);
end
end
dofile(MODPATH.."/callback.lua");
dofile(MODPATH.."/chatcmds.lua");
dofile(MODPATH.."/friends.lua");
if (mt_irc.auto_connect) then
mt_irc.connect()
end