From 46c1bac9a58805d85e67185dc5b4425a22646b04 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Diego=20Mart=C3=ADnez?= <kaeza@users.sf.net>
Date: Sat, 5 Jan 2013 12:48:35 -0200
Subject: [PATCH] Fixed delay waiting for end of MOTD

---
 src/callback.lua |  4 +--
 src/init.lua     | 63 +++++++++++++++++++++++++++---------------------
 2 files changed, 37 insertions(+), 30 deletions(-)

diff --git a/src/callback.lua b/src/callback.lua
index b95d4c5..163c711 100644
--- a/src/callback.lua
+++ b/src/callback.lua
@@ -7,10 +7,8 @@ minetest.register_on_joinplayer(function ( player )
 end);
 
 irc.register_callback("connect", function ( )
+    mt_irc.got_motd = true;
     irc.join(mt_irc.channel);
-    for _,player in ipairs(minetest.get_connected_players()) do
-        mt_irc.connected_players[player:get_player_name()] = mt_irc.auto_join;
-    end
 end);
 
 irc.register_callback("channel_msg", function ( channel, from, message )
diff --git a/src/init.lua b/src/init.lua
index baa1e3b..16ef077 100644
--- a/src/init.lua
+++ b/src/init.lua
@@ -42,7 +42,7 @@ if (not mt_irc.server_nick) then
 end
 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.channel = (mt_irc.channel or "##mt-irc-mod");
 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)");
@@ -55,32 +55,6 @@ minetest.register_privilege("irc_admin", {
     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
-            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);
-            end
-            mt_irc.buffered_messages = nil;
-        end
-        irc.poll();
-        mt_irc.cur_time = mt_irc.cur_time - mt_irc.dtime;
-        --local plys = minetest.get_connected_players();
-        --if ((#plys <= 0) and (minetest.is_singleplayer())) then
-        --    minetest.after(1.0, function ( )
-        --        irc.quit("Closing.");
-        --    end)
-        --end
-    end
-end);
-
 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.");
@@ -120,7 +94,42 @@ mt_irc.connect = function ( )
         local s = "DEBUG: irc.connect failed";
         minetest.debug(s);
         minetest.chat_send_all(s);
+        return;
     end
+    while (not mt_irc.got_motd) do
+        irc.poll();
+    end
+
+    minetest.register_globalstep(function ( dtime )
+        if (not mt_irc.connect_ok) then return; end
+        if (not mt_irc.players_connected) then
+            for _,player in ipairs(minetest.get_connected_players()) do
+                mt_irc.connected_players[player:get_player_name()] = mt_irc.auto_join;
+            end
+        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
+                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);
+                end
+                mt_irc.buffered_messages = nil;
+            end
+            irc.poll();
+            mt_irc.cur_time = mt_irc.cur_time - mt_irc.dtime;
+            --local plys = minetest.get_connected_players();
+            --if ((#plys <= 0) and (minetest.is_singleplayer())) then
+            --    minetest.after(1.0, function ( )
+            --        irc.quit("Closing.");
+            --    end)
+            --end
+        end
+    end);
 end
 
 mt_irc.say = function ( to, msg )