From 10511d992634ed2749e5f6b1cb27299c86afacc9 Mon Sep 17 00:00:00 2001
From: pandaemonium <pandae@pvpgn.org>
Date: Wed, 8 Aug 2007 18:10:22 +0000
Subject: [PATCH] commit of a modified version of [ Patch #2130 ] games bug fix

---
 pvpgn/src/bnetd/channel.cpp    | 16 +++++++++++-----
 pvpgn/src/bnetd/connection.cpp | 15 +++++----------
 pvpgn/src/bnetd/handle_wol.cpp | 15 ++++++---------
 pvpgn/src/bnetd/irc.cpp        | 33 ++++++++++++++++++---------------
 4 files changed, 40 insertions(+), 39 deletions(-)

diff --git a/pvpgn/src/bnetd/channel.cpp b/pvpgn/src/bnetd/channel.cpp
index e537bf9..250efc9 100644
--- a/pvpgn/src/bnetd/channel.cpp
+++ b/pvpgn/src/bnetd/channel.cpp
@@ -483,14 +483,20 @@ extern int channel_add_connection(t_channel * channel, t_connection * connection
         for (user=channel_get_first(channel); user; user=channel_get_next())
         {
              message_send_text(connection,message_type_adduser,user,NULL);
-	     message_send_text(user,message_type_join,connection,NULL);
+             /* In WOL gamechannels we send JOINGAME ack explicitely to self */
+             if (conn_wol_get_ingame(connection)==0)
+                 message_send_text(user,message_type_join,connection,NULL);
         }
-    else
-	message_send_text(connection,message_type_join,connection,NULL);
+    else {
+        if (conn_wol_get_ingame(connection)==0)
+            message_send_text(connection,message_type_join,connection,NULL);
+    }
 
-    if (conn_is_irc_variant(connection))
+    if (conn_is_irc_variant(connection)) {
+        if (conn_wol_get_ingame(connection)==0)
         message_send_text(connection,message_type_namreply,connection,NULL);
-
+    }
+     conn_wol_set_ingame(connection,0);
     /* please don't remove this notice */
     if (channel->log)
 	message_send_text(connection,message_type_info,connection,prefs_get_log_notice());
diff --git a/pvpgn/src/bnetd/connection.cpp b/pvpgn/src/bnetd/connection.cpp
index abbd0fa..d99690f 100644
--- a/pvpgn/src/bnetd/connection.cpp
+++ b/pvpgn/src/bnetd/connection.cpp
@@ -3812,8 +3812,7 @@ extern void conn_wol_set_ingame(t_connection * c, int ingame)
     	return;
     }
 
-    if (ingame)
-      c->protocol.wol.ingame = ingame;
+    c->protocol.wol.ingame = ingame;
 }
 
 extern int conn_wol_get_ingame(t_connection * c)
@@ -3864,8 +3863,7 @@ extern void conn_wol_set_codepage(t_connection * c, int codepage)
         return;
     }
 
-    if (codepage)
-       c->protocol.wol.codepage = codepage;
+    c->protocol.wol.codepage = codepage;
 }
 
 extern int conn_wol_get_codepage(t_connection * c)
@@ -3887,8 +3885,7 @@ extern void conn_wol_set_game_type(t_connection * c, int gameType)
         return;
     }
 
-    if (gameType)
-        c->protocol.wol.gameType = gameType;
+    c->protocol.wol.gameType = gameType;
 }
 
 extern int conn_wol_get_game_type(t_connection * c)
@@ -3939,8 +3936,7 @@ extern void conn_wol_set_findme(t_connection * c, int findme)
     	return;
     }
 
-    if (findme)
-        c->protocol.wol.findme = findme;
+    c->protocol.wol.findme = findme;
 }
 
 extern int conn_wol_get_findme(t_connection * c)
@@ -3962,8 +3958,7 @@ extern void conn_wol_set_pageme(t_connection * c, int pageme)
     	return;
     }
 
-    if (pageme)
-        c->protocol.wol.pageme = pageme;
+    c->protocol.wol.pageme = pageme;
 }
 
 extern int conn_wol_get_pageme(t_connection * c)
diff --git a/pvpgn/src/bnetd/handle_wol.cpp b/pvpgn/src/bnetd/handle_wol.cpp
index 151f1eb..7c0f43c 100644
--- a/pvpgn/src/bnetd/handle_wol.cpp
+++ b/pvpgn/src/bnetd/handle_wol.cpp
@@ -711,7 +711,6 @@ static int _handle_list_command(t_connection * conn, int numparams, char ** para
 
     if (numparams == 0) {
         /* This is Westwood Chat LIST command */
-        /* FIXME: We send only chat channels (no gamechannels for CnC, RedAlert and CnC Sole Survivor) */
         eventlog(eventlog_level_debug,__FUNCTION__,"[** WOL **] LIST WCHAT");
 
         LIST_TRAVERSE_CONST(channellist(),curr) {
@@ -846,13 +845,7 @@ static int _handle_join_command(t_connection * conn, int numparams, char ** para
 				irc_send(conn,ERR_NOSUCHCHANNEL,":JOIN failed"); /* FIXME: be more precise; what is the real error code for that? */
 			}
 			else {
-    			char temp[MAX_IRC_MESSAGE_LEN];
-				t_channel * channel;
-				channel = conn_get_channel(conn);
-
 				channel_set_userflags(conn);
-				wolname=irc_convert_channel(channel);
-
 			}
 		}
     		if (e)
@@ -1286,7 +1279,8 @@ static int _handle_joingame_command(t_connection * conn, int numparams, char **
 	    			char temp[MAX_IRC_MESSAGE_LEN];
 
    					channel_set_userflags(conn);
-   					message_send_text(conn,message_wol_joingame,conn,gameOptions); /* we have to send the JOINGAME acknowledgement */
+//   					message_send_text(conn,message_wol_joingame,conn,gameOptions); /* we have to send the JOINGAME acknowledgement */
+   					channel_message_send(channel,message_wol_joingame,conn,gameOptions);
    					wolname=irc_convert_channel(channel);
 
 					if ((topic = channel_get_topic(channel_get_name(channel)))) {
@@ -1332,6 +1326,8 @@ static int _handle_joingame_command(t_connection * conn, int numparams, char **
         }
 	    eventlog(eventlog_level_debug,__FUNCTION__,"[** WOL **] JOINGAME [Game Options] (%s)",_temp);
 
+		conn_wol_set_ingame(conn,1);
+
 	    e = irc_get_listelems(params[0]);
 	    if ((e)&&(e[0])) {
     		char const * wolname = irc_convert_ircname(e[0]);
@@ -1366,7 +1362,8 @@ static int _handle_joingame_command(t_connection * conn, int numparams, char **
 					channel_set_max(channel,std::atoi(params[2]));
 					channel_wol_set_game_type(channel,std::atoi(params[3]));
 					channel_wol_set_game_tournament(channel,std::atoi(params[6]));
-                    channel_wol_set_game_extension(channel,params[7]);
+                    if (params[7])
+                        channel_wol_set_game_extension(channel,params[7]);
 
 					message_send_text(conn,message_wol_joingame,conn,_temp); /* we have to send the JOINGAME acknowledgement */
 					wolname=irc_convert_channel(channel);
diff --git a/pvpgn/src/bnetd/irc.cpp b/pvpgn/src/bnetd/irc.cpp
index add1759..21f84ec 100644
--- a/pvpgn/src/bnetd/irc.cpp
+++ b/pvpgn/src/bnetd/irc.cpp
@@ -650,14 +650,13 @@ extern int irc_message_format(t_packet * packet, t_message_type type, t_connecti
 
         channel = conn_get_channel(me);
 
-	if((conn_get_wol(dst) == 1) && (conn_get_clienttag(dst) != CLIENTTAG_WCHAT_UINT))
-	{
+        if((conn_get_wol(dst) == 1) && (conn_get_clienttag(dst) != CLIENTTAG_WCHAT_UINT)) {
             char temp[MAX_IRC_MESSAGE_LEN];
     	    t_clan * clan;
     	    unsigned int clanid = 0;
     	    std::memset(temp,0,sizeof(temp));
-	    
-	    /**
+
+            /**
             *  For WOLv2 the channel JOIN output must be like the following:
     	    *  user!WWOL@hostname JOIN :clanID,longIP channelName
             */
@@ -675,11 +674,10 @@ extern int irc_message_format(t_packet * packet, t_message_type type, t_connecti
     	    std::memset(temp,0,sizeof(temp));
 
             if (conn_wol_get_ingame(me) == 1) {
-                std::sprintf(temp,"2 %u %u 1 1 %u :%s", channel_get_length(channel), channel_wol_get_game_type(channel), channel_wol_get_game_tournament(channel), irc_convert_channel(channel));
+                std::sprintf(temp,"2 %u %u 1 1 %u :", channel_get_length(channel), channel_wol_get_game_type(channel), channel_wol_get_game_tournament(channel));
                 msg = irc_message_preformat(&from,"JOINGAME",temp,irc_convert_channel(channel));
             }
-            else
-	    {
+            else {
                 msg = irc_message_preformat(&from,"JOIN","\r",irc_convert_channel(channel));
 	    }
         }
@@ -931,7 +929,6 @@ int irc_send_rpl_namreply_internal(t_connection * c, t_channel const * channel){
 		std::strcat(flg,"+");
 	if ((std::strlen(temp)+((!first)?(1):(0))+std::strlen(flg)+std::strlen(name)+1)<=sizeof(temp)) {
 	    if (!first) std::strcat(temp," ");
-
             if((conn_get_wol(c) == 1)) {
                 if ((channel_wol_get_game_owner(channel) != NULL) && (std::strcmp(channel_wol_get_game_owner(channel),name) == 0)) {
                        std::strcat(temp,"@");
@@ -942,20 +939,22 @@ int irc_send_rpl_namreply_internal(t_connection * c, t_channel const * channel){
                 }
                 if (conn_get_clienttag(c) != CLIENTTAG_WCHAT_UINT) {
                     /* BATTLECLAN Support */
+                    char _temp[MAX_IRC_MESSAGE_LEN];
+                    std::memset(_temp,0,sizeof(_temp));
                     t_clan * clan = account_get_clan(conn_get_account(m));
                     unsigned int clanid = 0;
 
                     if (clan)
                         clanid = clan_get_clanid(clan);
 
-                    std::sprintf(temp,"%s%s,%u,%u",temp,name,clanid,conn_get_addr(m));
+                    std::sprintf(_temp,"%s,%u,%u",name,clanid,conn_get_addr(m));
+                    std::strcat(temp,_temp);
                 }
                 else {
-                    std::sprintf(temp,"%s%s",temp,name);
+                    std::strcat(temp,name);
                 }
     	    }
-    	    else
-    	    {
+    	    else {
 	            std::strcat(temp,flg);
 	            std::strcat(temp,name);
     	    }
@@ -1105,6 +1104,7 @@ extern int irc_send_motd(t_connection * conn)
     char * line, * formatted_line;
     char send_line[MAX_IRC_MESSAGE_LEN];
     char motd_failed = 0;
+    bool first = true;
 
     if (!conn) {
 	   eventlog(eventlog_level_error,__FUNCTION__,"got NULL connection");
@@ -1113,14 +1113,17 @@ extern int irc_send_motd(t_connection * conn)
 
     tempname = conn_get_loggeduser(conn);
 
-    irc_send(conn,RPL_MOTDSTART,":-");
-
     if ((filename = prefs_get_motdfile())) {
 	 if ((fp = std::fopen(filename,"r"))) {
 	  while ((line=file_get_line(fp))) {
 		if ((formatted_line = message_format_line(conn,line))) {
 		  formatted_line[0]=' ';
 		  std::sprintf(send_line,":-%s",formatted_line);
+		  if (first) {
+		      irc_send(conn,RPL_MOTDSTART,send_line);
+		      first = false;
+          }
+          else
 		  irc_send(conn,RPL_MOTD,send_line);
 		  xfree(formatted_line);
 		}
@@ -1136,7 +1139,7 @@ extern int irc_send_motd(t_connection * conn)
       motd_failed = 1;
 
    if (motd_failed) {
-      irc_send(conn,RPL_MOTD,":- Failed to load motd, sending default motd              ");
+      irc_send(conn,RPL_MOTDSTART,":- Failed to load motd, sending default motd              ");
       irc_send(conn,RPL_MOTD,":- ====================================================== ");
       irc_send(conn,RPL_MOTD,":-                 http://www.pvpgn.org                   ");
       irc_send(conn,RPL_MOTD,":- ====================================================== ");