diff src/protocols/msn/switchboard.c @ 10773:888d4c328be5

[gaim-migrate @ 12382] Patch 1149564 from Felipe Contreras: "[MSN] Cleanups and fixes I know it fixes a multiple conversation close notification but it probably fixes more stuff. I don't know exactly what are the diferences with what Stu did." I munged it a bit, to make it compile and stuff. Please test this, I might have broken it. committer: Tailor Script <tailor@pidgin.im>
author Stu Tomlinson <stu@nosnilmot.com>
date Sat, 02 Apr 2005 16:38:33 +0000
parents c4cb90065e1d
children 58c84363c8ae
line wrap: on
line diff
--- a/src/protocols/msn/switchboard.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/switchboard.c	Sat Apr 02 16:38:33 2005 +0000
@@ -29,6 +29,8 @@
 
 #include "error.h"
 
+/* #define MSN_DEBUG_SWBOARD */
+
 static MsnTable *cbs_table;
 
 static void msg_error_helper(MsnCmdProc *cmdproc, MsnMessage *msg,
@@ -70,8 +72,17 @@
 	MsnMessage *msg;
 	GList *l;
 
+#ifdef MSN_DEBUG_SWBOARD
+	gaim_debug_info("msn", "switchboard_destroy: swboard(%p)\n", swboard);
+#endif
+
 	g_return_if_fail(swboard != NULL);
 
+	if (swboard->destroying)
+		return;
+
+	swboard->destroying = TRUE;
+
 	/* If it linked us is because its looking for trouble */
 	if (swboard->slplink != NULL)
 		msn_slplink_destroy(swboard->slplink);
@@ -210,8 +221,19 @@
 	swboard->current_users++;
 	swboard->empty = FALSE;
 
-	/* gaim_debug_info("msn", "user=[%s], total=%d\n", user,
-	 * swboard->current_users); */
+#ifdef MSN_DEBUG_CHAT
+	gaim_debug_info("msn", "user=[%s], total=%d\n", user,
+					swboard->current_users);
+#endif
+
+	if (!(swboard->flag & MSN_SB_FLAG_IM))
+	{
+		/* This is a helper switchboard. */
+		if (swboard->conv != NULL)
+			gaim_debug_error("msn", "switchboard_add_user: conv != NULL\n");
+
+		return;
+	}
 
 	if ((swboard->conv != NULL) &&
 		(gaim_conversation_get_type(swboard->conv) == GAIM_CONV_CHAT))
@@ -226,7 +248,10 @@
 		{
 			GList *l;
 
-			/* gaim_debug_info("msn", "[chat] Switching to chat.\n"); */
+#ifdef MSN_DEBUG_CHAT
+			gaim_debug_info("msn", "[chat] Switching to chat.\n");
+#endif
+
 #if 0
 			/* this is bad - it causes msn_switchboard_close to be called on the
 			 * switchboard we're in the middle of using :( */
@@ -247,14 +272,17 @@
 
 				tmp_user = l->data;
 
-				/* gaim_debug_info("msn", "[chat] Adding [%s].\n",
-				 * tmp_user); */
+#ifdef MSN_DEBUG_CHAT
+				gaim_debug_info("msn", "[chat] Adding [%s].\n", tmp_user);
+#endif
 
 				gaim_conv_chat_add_user(GAIM_CONV_CHAT(swboard->conv),
 										tmp_user, NULL, GAIM_CBFLAGS_NONE, TRUE);
 			}
 
-			/* gaim_debug_info("msn", "[chat] We add ourselves.\n"); */
+#ifdef MSN_DEBUG_CHAT
+			gaim_debug_info("msn", "[chat] We add ourselves.\n");
+#endif
 
 			gaim_conv_chat_add_user(GAIM_CONV_CHAT(swboard->conv),
 									gaim_account_get_username(account),
@@ -272,8 +300,7 @@
 	}
 	else
 	{
-		gaim_debug_warning("msn", "This should not happen!"
-						   "(msn_switchboard_add_user)\n");
+		gaim_debug_warning("msn", "switchboard_add_user: This should not happen!\n");
 	}
 }
 
@@ -357,6 +384,9 @@
 
 	swboard = cmdproc->data;
 
+	/* This is not good, and should be fixed somewhere else. */
+	g_return_if_fail(swboard != NULL);
+
 	if (msg->type == MSN_MSG_TEXT)
 	{
 		const char *format, *str_reason;
@@ -613,6 +643,20 @@
 	swboard = cmdproc->data;
 	user = cmd->params[0];
 
+#if 0
+	if (!(swboard->flag & MSN_SB_FLAG_IM))
+	{
+		/* TODO: This is a helper switchboard. It would be better if
+		 * swboard->conv is NULL, but it isn't. */
+		/* Umm? I think swboard->conv is NULL for all helper switchboards now? */
+		msn_switchboard_destroy(swboard);
+		return;
+	}
+#else
+	if (!(swboard->flag & MSN_SB_FLAG_IM))
+		gaim_debug_error("msn_switchboard", "bye_cmd: helper bug\n");
+#endif
+
 	if (swboard->conv == NULL)
 	{
 		/* This is a helper switchboard */
@@ -847,21 +891,35 @@
 		body_final = body_enc;
 	}
 
+	swboard->flag |= MSN_SB_FLAG_IM;
+
 	if (swboard->current_users > 1 ||
 		((swboard->conv != NULL) &&
 		 gaim_conversation_get_type(swboard->conv) == GAIM_CONV_CHAT))
 	{
+		/* If current_users is always ok as it should then there is no need to
+		 * check if this is a chat. */
+		if (swboard->current_users <= 1)
+			gaim_debug_misc("msn", "plain_msg: current_users(%d)\n",
+							swboard->current_users);
+
 		serv_got_chat_in(gc, swboard->chat_id, passport, 0, body_final,
 						 time(NULL));
 		if (swboard->conv == NULL)
+		{
 			swboard->conv = gaim_find_chat(gc, swboard->chat_id);
+			swboard->flag |= MSN_SB_FLAG_IM;
+		}
 	}
 	else
 	{
 		serv_got_im(gc, passport, body_final, 0, time(NULL));
 		if (swboard->conv == NULL)
+		{
 			swboard->conv = gaim_find_conversation_with_account(GAIM_CONV_IM,
 									passport, gaim_connection_get_account(gc));
+			swboard->flag |= MSN_SB_FLAG_IM;
+		}
 	}
 
 	g_free(body_final);
@@ -1101,9 +1159,6 @@
 {
 	g_return_if_fail(swboard != NULL);
 
-	 /* forget any conversation that used to be associated with this swboard */
-	swboard->conv = NULL;
-
 	if (swboard->error != MSN_SB_ERROR_NONE)
 	{
 		msn_switchboard_destroy(swboard);
@@ -1123,6 +1178,27 @@
 	}
 }
 
+gboolean
+msn_switchboard_release(MsnSwitchBoard *swboard, MsnSBFlag flag)
+{
+	g_return_val_if_fail(swboard != NULL, FALSE);
+
+	swboard->flag &= ~flag;
+
+	if (flag == MSN_SB_FLAG_IM)
+		/* Forget any conversation that used to be associated with this
+		 * swboard. */
+		swboard->conv = NULL;
+
+	if (swboard->flag == 0)
+	{
+		msn_switchboard_close(swboard);
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
 /**************************************************************************
  * Init stuff
  **************************************************************************/