changeset 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 12aa8ce77077
children d83f745c997b
files src/protocols/msn/msn.c src/protocols/msn/servconn.c src/protocols/msn/session.c src/protocols/msn/session.h src/protocols/msn/slp.c src/protocols/msn/slpcall.c src/protocols/msn/slplink.c src/protocols/msn/switchboard.c src/protocols/msn/switchboard.h
diffstat 9 files changed, 182 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/msn/msn.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/msn.c	Sat Apr 02 16:38:33 2005 +0000
@@ -338,6 +338,7 @@
 	/* TODO: This might move somewhere else, after USR might be */
 	swboard->chat_id = session->conv_seq++;
 	swboard->conv = serv_got_joined_chat(gc, swboard->chat_id, "MSN Chat");
+	swboard->flag = MSN_SB_FLAG_IM;
 
 	gaim_conv_chat_add_user(GAIM_CONV_CHAT(swboard->conv),
 							gaim_account_get_username(buddy->account), NULL, GAIM_CBFLAGS_NONE, TRUE);
@@ -738,7 +739,7 @@
 		MsnSwitchBoard *swboard;
 
 		session = gc->proto_data;
-		swboard = msn_session_get_swboard(session, who);
+		swboard = msn_session_get_swboard(session, who, MSN_SB_FLAG_IM);
 
 		msn_switchboard_send_msg(swboard, msg, TRUE);
 	}
@@ -799,6 +800,8 @@
 	if (swboard == NULL || !msn_switchboard_can_send(swboard))
 		return 0;
 
+	swboard->flag |= MSN_SB_FLAG_IM;
+
 	msg = msn_message_new(MSN_MSG_TYPING);
 	msn_message_set_content_type(msg, "text/x-msmsgscontrol");
 	msn_message_set_flag(msg, 'U');
@@ -1096,6 +1099,8 @@
 		swboard->conv = gaim_find_chat(gc, id);
 	}
 
+	swboard->flag |= MSN_SB_FLAG_IM;
+
 	msn_switchboard_request_add_user(swboard, who);
 }
 
@@ -1116,7 +1121,7 @@
 
 	conv = swboard->conv;
 
-	msn_switchboard_close(swboard);
+	msn_switchboard_release(swboard, MSN_SB_FLAG_IM);
 
 	/* If other switchboards managed to associate themselves with this
 	 * conv, make sure they know it's gone! */
@@ -1147,6 +1152,8 @@
 	if (!swboard->ready)
 		return 0;
 
+	swboard->flag |= MSN_SB_FLAG_IM;
+
 	msn_import_html(message, &msgformat, &msgtext);
 
 	if (strlen(msgtext) + strlen(msgformat) + strlen(VERSION) > 1564)
@@ -1248,10 +1255,7 @@
 
 	conv = swboard->conv;
 
-	if (!(swboard->flag & MSN_SB_FLAG_FT))
-		msn_switchboard_close(swboard);
-	else
-		swboard->conv = NULL;
+	msn_switchboard_release(swboard, MSN_SB_FLAG_IM);
 
 	/* If other switchboards managed to associate themselves with this
 	 * conv, make sure they know it's gone! */
--- a/src/protocols/msn/servconn.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/servconn.c	Sat Apr 02 16:38:33 2005 +0000
@@ -384,9 +384,9 @@
 		{
 			msn_cmdproc_process_cmd_text(servconn->cmdproc, cur);
 		}
-	} while (servconn->connected && servconn->rx_len > 0);
+	} while (servconn->connected && !servconn->wasted && servconn->rx_len > 0);
 
-	if (servconn->connected)
+	if (servconn->connected && !servconn->wasted)
 	{
 		if (servconn->rx_len > 0)
 			servconn->rx_buf = g_memdup(cur, servconn->rx_len);
--- a/src/protocols/msn/session.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/session.c	Sat Apr 02 16:38:33 2005 +0000
@@ -208,7 +208,8 @@
 }
 
 MsnSwitchBoard *
-msn_session_get_swboard(MsnSession *session, const char *username)
+msn_session_get_swboard(MsnSession *session, const char *username,
+						MsnSBFlag flag)
 {
 	MsnSwitchBoard *swboard;
 
@@ -222,6 +223,8 @@
 		msn_switchboard_request_add_user(swboard, username);
 	}
 
+	swboard->flag |= flag;
+
 	return swboard;
 }
 
--- a/src/protocols/msn/session.h	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/session.h	Sat Apr 02 16:38:33 2005 +0000
@@ -192,11 +192,12 @@
  *
  * @param session The MSN session.
  * @param username The username to search for.
+ * @param flag The flag of the switchboard
  *
  * @return The switchboard.
  */
 MsnSwitchBoard *msn_session_get_swboard(MsnSession *session,
-										const char *username);
+										const char *username, MsnSBFlag flag);
 
 /**
  * Sets an error for the MSN session.
--- a/src/protocols/msn/slp.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/slp.c	Sat Apr 02 16:38:33 2005 +0000
@@ -31,9 +31,6 @@
 #include "user.h"
 #include "switchboard.h"
 
-/* #include "slplink.h" */
-/* #include "directconn.h" */
-
 static void send_ok(MsnSlpCall *slpcall, const char *branch,
 					const char *type, const char *content);
 
@@ -124,6 +121,8 @@
 
 			g_free(content);
 			msn_slplink_unleash(slpcall->slplink);
+
+			msn_slp_call_destroy(slpcall);
 		}
 	}
 }
@@ -753,7 +752,7 @@
 
 	gc = slpcall->slplink->session->account->gc;
 	who = slpcall->slplink->remote_user;
-	
+
 	conv = gaim_find_conversation_with_account(GAIM_CONV_ANY, who, gc->account);
 
 	/* FIXME: it would be better if we wrote the data as we received it
@@ -762,8 +761,9 @@
 	*/
 	gaim_conv_custom_smiley_write(conv, slpcall->data_info, data, size);
 	gaim_conv_custom_smiley_close(conv, slpcall->data_info );
- 
+#ifdef MSN_DEBUG_UD
 	gaim_debug_info("msn", "Got smiley: %s\n", slpcall->data_info);
+#endif
 }
 
 void
@@ -841,7 +841,9 @@
 
 	g_return_if_fail(userlist != NULL);
 
+#ifdef MSN_DEBUG_UD
 	gaim_debug_info("msn", "Releasing buddy icon request\n");
+#endif
 
 	while (userlist->buddy_icon_window > 0)
 	{
@@ -862,8 +864,10 @@
 		msn_request_user_display(user);
 		userlist->buddy_icon_window--;
 
+#ifdef MSN_DEBUG_UD
 		gaim_debug_info("msn", "buddy_icon_window=%d\n",
 						userlist->buddy_icon_window);
+#endif
 	}
 }
 
@@ -906,13 +910,17 @@
 		userlist = user->userlist;
 		queue = userlist->buddy_icon_requests;
 
+#ifdef MSN_DEBUG_UD
 		gaim_debug_info("msn", "Queueing buddy icon request: %s\n",
 						user->passport);
+#endif
 
 		g_queue_push_tail(queue, user);
 
+#ifdef MSN_DEBUG_UD
 		gaim_debug_info("msn", "buddy_icon_window=%d\n",
 						userlist->buddy_icon_window);
+#endif
 
 		if (userlist->buddy_icon_window > 0)
 			msn_release_buddy_icon_request(userlist);
@@ -931,7 +939,9 @@
 	g_return_if_fail(slpcall != NULL);
 
 	info = slpcall->data_info;
+#ifdef MSN_DEBUG_UD
 	gaim_debug_info("msn", "Got User Display: %s\n", info);
+#endif
 
 	userlist = slpcall->slplink->session->userlist;
 	account = slpcall->slplink->session->account;
@@ -966,7 +976,13 @@
 
 	g_return_if_fail(slpcall != NULL);
 
+#ifdef MSN_DEBUG_UD
 	gaim_debug_info("msn", "End User Display\n");
+#endif
+
+	/* Maybe the slplink was destroyed. */
+	if (slpcall->slplink == NULL)
+		return;
 
 	userlist = slpcall->slplink->session->userlist;
 
@@ -977,8 +993,10 @@
 	/* Free one window slot */
 	userlist->buddy_icon_window++;
 
+#ifdef MSN_DEBUG_UD
 	gaim_debug_info("msn", "buddy_icon_window=%d\n",
 					userlist->buddy_icon_window);
+#endif
 
 	msn_release_buddy_icon_request(userlist);
 }
@@ -1016,7 +1034,9 @@
 		const char *my_info = NULL;
 		GSList *sl;
 
+#ifdef MSN_DEBUG_UD
 		gaim_debug_info("msn", "Requesting our own user display\n");
+#endif
 
 		my_obj = msn_user_get_object(session->user);
 
@@ -1044,8 +1064,10 @@
 		/* Free one window slot */
 		session->userlist->buddy_icon_window++;
 
+#ifdef MSN_DEBUG_UD
 		gaim_debug_info("msn", "buddy_icon_window=%d\n",
 						session->userlist->buddy_icon_window);
+#endif
 
 		msn_release_buddy_icon_request(session->userlist);
 	}
--- a/src/protocols/msn/slpcall.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/slpcall.c	Sat Apr 02 16:38:33 2005 +0000
@@ -27,6 +27,8 @@
 
 #include "slp.h"
 
+/* #define MSN_DEBUG_SLPCALL */
+
 /**************************************************************************
  * Util
  **************************************************************************/
@@ -58,6 +60,10 @@
 
 	slpcall = g_new0(MsnSlpCall, 1);
 
+#ifdef MSN_DEBUG_SLPCALL
+	gaim_debug_info("msn", "slpcall_new: slpcall(%p)\n", slpcall);
+#endif
+
 	slpcall->slplink = slplink;
 
 	msn_slplink_add_slpcall(slplink, slpcall);
@@ -72,6 +78,10 @@
 {
 	GList *e;
 
+#ifdef MSN_DEBUG_SLPCALL
+	gaim_debug_info("msn", "slpcall_destroy: slpcall(%p)\n", slpcall);
+#endif
+
 	g_return_if_fail(slpcall != NULL);
 
 	if (slpcall->timer)
@@ -86,17 +96,13 @@
 	if (slpcall->data_info != NULL)
 		g_free(slpcall->data_info);
 
-	msn_slplink_remove_slpcall(slpcall->slplink, slpcall);
-
 	for (e = slpcall->slplink->slp_msgs; e != NULL; )
 	{
 		MsnSlpMessage *slpmsg = e->data;
 		e = e->next;
 
-		g_return_if_fail(slpmsg != NULL);
-
-#if 0
-		gaim_debug_info("msn", "slpcall destroy: trying slp_msg (%p)\n",
+#ifdef MSN_DEBUG_SLPCALL_VERBOSE
+		gaim_debug_info("msn", "slpcall_destroy: trying slpmsg(%p)\n",
 						slpmsg);
 #endif
 
@@ -106,6 +112,8 @@
 		}
 	}
 
+	msn_slplink_remove_slpcall(slpcall->slplink, slpcall);
+
 	if (slpcall->end_cb != NULL)
 		slpcall->end_cb(slpcall);
 
@@ -164,6 +172,7 @@
 
 	slpmsg = msn_slpmsg_sip_new(slpcall, 0, header, slpcall->branch,
 								"application/x-msnmsgr-sessionreqbody", content);
+
 #ifdef MSN_DEBUG_SLP
 	slpmsg->info = "SLP INVITE";
 	slpmsg->text_body = TRUE;
@@ -193,7 +202,9 @@
 
 	slpcall = data;
 
-	gaim_debug_info("msn", "slpcall timeout (%p)\n", slpcall);
+#ifdef MSN_DEBUG_SLPCALL
+	gaim_debug_info("msn", "slpcall_timeout: slpcall(%p)\n", slpcall);
+#endif
 
 	if (!slpcall->pending && !slpcall->progress)
 	{
@@ -232,12 +243,6 @@
 
 			slpcall->cb(slpcall, body, body_len);
 
-			/* TODO: Shall we send a BYE? I don't think so*/
-#if 0
-			send_bye(slpcall, "application/x-msnmsgr-sessionclosebody");
-			msn_slplink_unleash(slpcall->slplink);
-#endif
-
 			slpcall->wasted = TRUE;
 		}
 	}
--- a/src/protocols/msn/slplink.c	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/slplink.c	Sat Apr 02 16:38:33 2005 +0000
@@ -67,6 +67,10 @@
 
 	slplink = g_new0(MsnSlpLink, 1);
 
+#ifdef MSN_DEBUG_SLPLINK
+	gaim_debug_info("msn", "slplink_new: slplink(%p)\n", slplink);
+#endif
+
 	slplink->session = session;
 	slplink->slp_seq_id = rand() % 0xFFFFFF00 + 4;
 
@@ -86,6 +90,10 @@
 {
 	MsnSession *session;
 
+#ifdef MSN_DEBUG_SLPLINK
+	gaim_debug_info("msn", "slplink_destroy: slplink(%p)\n", slplink);
+#endif
+
 	g_return_if_fail(slplink != NULL);
 
 	if (slplink->swboard != NULL)
@@ -173,11 +181,18 @@
 {
 	slplink->slp_calls = g_list_remove(slplink->slp_calls, slpcall);
 
-	/* The slplink has no slpcalls in it, maybe we should destroy it. */
+	/* The slplink has no slpcalls in it. If no one is using it, we might
+	 * destroy the switchboard, but we should be careful not to use the slplink
+	 * again. */
 	if (slplink->slp_calls == NULL)
 	{
 		if (slplink->swboard != NULL)
-			slplink->swboard->flag &= ~MSN_SB_FLAG_FT;
+		{
+			if (msn_switchboard_release(slplink->swboard, MSN_SB_FLAG_FT))
+				/* I'm not sure this is the best thing to do, but it's better
+				 * than nothing. */
+				slpcall->slplink = NULL;
+		}
 	}
 }
 
@@ -230,7 +245,7 @@
 		if (slplink->swboard == NULL)
 		{
 			slplink->swboard = msn_session_get_swboard(slplink->session,
-													   slplink->remote_user);
+													   slplink->remote_user, MSN_SB_FLAG_FT);
 
 			if (slplink->swboard == NULL)
 				return;
--- 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
  **************************************************************************/
--- a/src/protocols/msn/switchboard.h	Sat Apr 02 16:07:20 2005 +0000
+++ b/src/protocols/msn/switchboard.h	Sat Apr 02 16:38:33 2005 +0000
@@ -54,7 +54,8 @@
  */
 typedef enum
 {
-	MSN_SB_FLAG_FT = 0x01, /**< This switchboard is being used for file transfer. */
+	MSN_SB_FLAG_IM = 0x01, /**< This switchboard is being used for a conversation. */
+	MSN_SB_FLAG_FT = 0x02, /**< This switchboard is being used for file transfer. */
 
 } MsnSBFlag;
 
@@ -86,6 +87,8 @@
 							  ready to be used. */
 	gboolean closed;		/**< A flag that states if the switchboard has
 							  been closed by the user. */
+	gboolean destroying;	/**< A flag that states if the switchboard is
+							  alredy on the process of destruction. */
 
 	int current_users;
 	int total_users;
@@ -207,6 +210,16 @@
 void msn_switchboard_close(MsnSwitchBoard *swboard);
 
 /**
+ * Release a switchboard from a certain function.
+ *
+ * @param swboard The switchboard to release.
+ * @param flag The flag that states the function.
+ *
+ * @return @c TRUE if the switchboard was closed, @c FALSE otherwise.
+ */
+gboolean msn_switchboard_release(MsnSwitchBoard *swboard, MsnSBFlag flag);
+
+/**
  * Returns whether or not we currently can send a message through this
  * switchboard.
  *