changeset 8583:fc27237783ee

[gaim-migrate @ 9333] Added Felipe Contreras's patch to reorganize and clean up the MSN protocol plugin. Thanks, and once again, apologies for the what must have seemed a never-ending delay. committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Mon, 05 Apr 2004 07:11:24 +0000
parents d7c85220c685
children 058efd3cb86f
files ChangeLog src/protocols/msn/Makefile.am src/protocols/msn/Makefile.mingw src/protocols/msn/notification.c src/protocols/msn/notification.h src/protocols/msn/servconn.c src/protocols/msn/servconn.h src/protocols/msn/session.c src/protocols/msn/session.h src/protocols/msn/switchboard.c src/protocols/msn/switchboard.h
diffstat 11 files changed, 126 insertions(+), 107 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Apr 05 06:52:02 2004 +0000
+++ b/ChangeLog	Mon Apr 05 07:11:24 2004 +0000
@@ -4,6 +4,7 @@
 	New Features:
 	* The System Log returns (Ka-Hing Cheung)
 	* Added a conversation-drag-ended signal (Etan Reisner)
+	* Reorganized and cleaned up the MSN protocol plugin (Felipe Contreras)
 
 	Bug Fixes:
 	* Save Conversation works again (Kevin Stange)
--- a/src/protocols/msn/Makefile.am	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/Makefile.am	Mon Apr 05 07:11:24 2004 +0000
@@ -4,8 +4,6 @@
 pkgdir = $(libdir)/gaim
 
 MSNSOURCES = \
-	dispatch.c \
-	dispatch.h \
 	error.c \
 	error.h \
 	group.c \
--- a/src/protocols/msn/Makefile.mingw	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/Makefile.mingw	Mon Apr 05 07:11:24 2004 +0000
@@ -68,8 +68,7 @@
 ##  SOURCES, OBJECTS
 ##
 
-C_SRC =			dispatch.c \
-			error.c \
+C_SRC =			error.c \
 			group.c \
 			httpmethod.c \
 			msg.c \
--- a/src/protocols/msn/notification.c	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/notification.c	Mon Apr 05 07:11:24 2004 +0000
@@ -1582,11 +1582,9 @@
 		}
 	}
 	else if (!strcmp(params[1], "NS")) {
-		msn_servconn_destroy(session->notification_conn);
-
-		session->notification_conn = msn_notification_new(session, host, port);
-
-		if (!msn_servconn_connect(session->notification_conn)) {
+		if (!msn_notification_connect(session->notification_conn, host,
+									  port))
+		{
 			gaim_connection_error(gc, _("Unable to transfer to "
 									  "notification server"));
 
@@ -1770,23 +1768,14 @@
 }
 
 static gboolean
-connect_cb(gpointer data, gint source, GaimInputCondition cond)
+connect_cb(MsnServConn *servconn)
 {
-	MsnServConn *notification = data;
-	MsnSession *session = notification->session;
+	MsnSession *session = servconn->session;
 	GaimAccount *account = session->account;
 	GaimConnection *gc = gaim_account_get_connection(account);
 	char proto_vers[256];
 	size_t i;
 
-	if (source == -1) {
-		gaim_connection_error(session->account->gc, _("Unable to connect."));
-		return FALSE;
-	}
-
-	if (notification->fd != source)
-		notification->fd = source;
-
 	proto_vers[0] = '\0';
 
 	for (i = 7; i <= session->protocol_ver; i++)
@@ -1800,7 +1789,7 @@
 
 	strncat(proto_vers, "CVR0", sizeof(proto_vers));
 
-	if (!msn_servconn_send_command(notification, "VER", proto_vers))
+	if (!msn_servconn_send_command(servconn, "VER", proto_vers))
 	{
 		gaim_connection_error(gc, _("Unable to write to server"));
 		return FALSE;
@@ -1815,27 +1804,14 @@
 	return TRUE;
 }
 
-static void
-failed_read_cb(gpointer data, gint source, GaimInputCondition cond)
-{
-	MsnServConn *notification = data;
-	GaimConnection *gc;
-
-	gc = notification->session->account->gc;
-
-	gaim_connection_error(gc, _("Error reading from server"));
-}
-
 MsnServConn *
-msn_notification_new(MsnSession *session, const char *server, int port)
+msn_notification_new(MsnSession *session)
 {
 	MsnServConn *notification;
 
 	notification = msn_servconn_new(session);
 
-	msn_servconn_set_server(notification, server, port);
 	msn_servconn_set_connect_cb(notification, connect_cb);
-	msn_servconn_set_failed_read_cb(notification, failed_read_cb);
 
 	if (session->http_method)
 		notification->http_data->server_type = "NS";
@@ -1902,3 +1878,11 @@
 
 	return notification;
 }
+
+gboolean
+msn_notification_connect(MsnServConn *notification, const char *host, int port)
+{
+	g_return_val_if_fail(notification != NULL, FALSE);
+
+	return msn_servconn_connect(notification, host, port);
+}
--- a/src/protocols/msn/notification.h	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/notification.h	Mon Apr 05 07:11:24 2004 +0000
@@ -25,7 +25,9 @@
 #include "session.h"
 #include "servconn.h"
 
-MsnServConn *msn_notification_new(MsnSession *session, const char *server,
-								  int port);
+MsnServConn *msn_notification_new(MsnSession *session);
+gboolean msn_notification_connect(MsnServConn *notification,
+							  const char *host, int port);
+void msn_notification_disconnect(MsnServConn *notification);
 
 #endif /* _MSN_NOTIFICATION_H_ */
--- a/src/protocols/msn/servconn.c	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/servconn.c	Mon Apr 05 07:11:24 2004 +0000
@@ -23,6 +23,8 @@
 #include "servconn.h"
 #include "error.h"
 
+static void read_cb(gpointer data, gint source, GaimInputCondition cond);
+
 typedef struct
 {
 	char *command;
@@ -196,9 +198,21 @@
 	MsnServConn *servconn = data;
 
 	gaim_debug_info("msn", "In servconn's connect_cb\n");
-	if (servconn->connect_cb(data, source, cond))
+
+	servconn->fd = source;
+
+	if (source > 0)
+	{
+		/* Someone wants to know we connected. */
+		servconn->connect_cb(servconn);
 		servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ,
-										servconn->login_cb, data);
+										read_cb, data);
+	}
+	else
+	{
+		GaimConnection *gc = servconn->session->account->gc;
+		gaim_connection_error(gc, _("Unable to connect."));
+	}
 }
 
 MsnServConn *
@@ -210,7 +224,6 @@
 
 	servconn = g_new0(MsnServConn, 1);
 
-	servconn->login_cb = msn_servconn_parse_data;
 	servconn->session = session;
 
 	if (session->http_method)
@@ -231,28 +244,29 @@
 }
 
 gboolean
-msn_servconn_connect(MsnServConn *servconn)
+msn_servconn_connect(MsnServConn *servconn, const char *host, int port)
 {
 	MsnSession *session;
 	int i;
 
-	g_return_val_if_fail(servconn         != NULL, FALSE);
-	g_return_val_if_fail(servconn->server != NULL, FALSE);
-	g_return_val_if_fail(!servconn->connected,     TRUE);
+	g_return_val_if_fail(servconn != NULL, FALSE);
+	g_return_val_if_fail(host     != NULL, FALSE);
+	g_return_val_if_fail(port      > 0,    FALSE);
 
 	session = servconn->session;
 
+	if (servconn->connected)
+		msn_servconn_disconnect(servconn);
+
 	if (session->http_method)
 	{
-		servconn->http_data->gateway_ip = g_strdup(servconn->server);
-
-		g_free(servconn->server);
-		servconn->server = g_strdup("gateway.messenger.hotmail.com");
-		servconn->port = 80;
+		servconn->http_data->gateway_ip = g_strdup(host);
+		host = "gateway.messenger.hotmail.com";
+		port = 80;
 	}
 
-	i = gaim_proxy_connect(session->account, servconn->server,
-						   servconn->port, connect_cb, servconn);
+	i = gaim_proxy_connect(session->account, host, port, connect_cb,
+						   servconn);
 
 	if (i == 0)
 		servconn->connected = TRUE;
@@ -275,6 +289,8 @@
 
 	close(servconn->fd);
 
+	servconn->connected = FALSE;
+
 	if (servconn->http_data != NULL)
 	{
 		if (servconn->http_data->session_id != NULL)
@@ -308,7 +324,8 @@
 		msn_servconn_unqueue_message(servconn, entry->msg);
 	}
 
-	servconn->connected = FALSE;
+	if (servconn->disconnect_cb)
+		servconn->disconnect_cb(servconn);
 }
 
 void
@@ -325,12 +342,18 @@
 	if (servconn->connected)
 		msn_servconn_disconnect(servconn);
 
-	if (servconn->server != NULL)
-		g_free(servconn->server);
+#if 0
+	/* shx: not used */
+	if (servconn->host != NULL)
+		g_free(servconn->host);
+#endif
 
 	g_free(servconn);
 }
 
+#if 0
+/* shx: this isn't used */
+
 void
 msn_servconn_set_server(MsnServConn *servconn, const char *server, int port)
 {
@@ -338,11 +361,11 @@
 	g_return_if_fail(server != NULL);
 	g_return_if_fail(port > 0);
 
-	if (servconn->server != NULL)
-		g_free(servconn->server);
+	if (servconn->host != NULL)
+		g_free(servconn->host);
 
-	servconn->server = g_strdup(server);
-	servconn->port   = port;
+	servconn->host = g_strdup(server);
+	servconn->port = port;
 }
 
 const char *
@@ -350,7 +373,7 @@
 {
 	g_return_val_if_fail(servconn != NULL, NULL);
 
-	return servconn->server;
+	return servconn->host;
 }
 
 int
@@ -360,11 +383,11 @@
 
 	return servconn->port;
 }
+#endif
 
 void
 msn_servconn_set_connect_cb(MsnServConn *servconn,
-							gboolean (*connect_cb)(gpointer, gint,
-												   GaimInputCondition))
+							gboolean (*connect_cb)(MsnServConn *servconn))
 {
 	g_return_if_fail(servconn != NULL);
 
@@ -372,13 +395,13 @@
 }
 
 void
-msn_servconn_set_failed_read_cb(MsnServConn *servconn,
-								void (*failed_read_cb)(gpointer, gint,
-													   GaimInputCondition))
+msn_servconn_set_disconnect_cb(MsnServConn *servconn,
+							   void (*disconnect_cb)(MsnServConn
+													 *servconn))
 {
 	g_return_if_fail(servconn != NULL);
 
-	servconn->failed_read_cb = failed_read_cb;
+	servconn->disconnect_cb = disconnect_cb;
 }
 
 size_t
@@ -491,8 +514,19 @@
 	g_hash_table_insert(servconn->msg_types, g_strdup(content_type), cb);
 }
 
-void
-msn_servconn_parse_data(gpointer data, gint source, GaimInputCondition cond)
+static void
+failed_io(MsnServConn *servconn)
+{
+	GaimConnection *gc =
+		gaim_account_get_connection(servconn->session->account);
+
+	gaim_connection_error(gc, _("IO Error."));
+
+	msn_servconn_disconnect(servconn);
+}
+
+static void
+read_cb(gpointer data, gint source, GaimInputCondition cond)
 {
 	MsnServConn *servconn = (MsnServConn *)data;
 	MsnSession *session = servconn->session;
@@ -504,8 +538,7 @@
 
 	if (len <= 0)
 	{
-		if (servconn->failed_read_cb != NULL)
-			servconn->failed_read_cb(data, source, cond);
+		failed_io(servconn);
 
 		return;
 	}
@@ -565,7 +598,7 @@
 
 			close(servconn->fd);
 
-			i = gaim_proxy_connect(session->account, servconn->server,
+			i = gaim_proxy_connect(session->account, servconn->host,
 								   servconn->port, servconn->login_cb,
 								   servconn);
 
--- a/src/protocols/msn/servconn.h	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/servconn.h	Mon Apr 05 07:11:24 2004 +0000
@@ -53,8 +53,11 @@
 
 	MsnHttpMethodData *http_data;
 
-	char *server;
+#if 0
+	/* shx: not used */
+	char *host;
 	int port;
+#endif
 
 	int fd;
 	int inpa;
@@ -75,9 +78,8 @@
 	GHashTable *commands;
 	GHashTable *msg_types;
 
-	gboolean (*connect_cb)(gpointer, gint, GaimInputCondition);
-	void (*failed_read_cb)(gpointer, gint, GaimInputCondition);
-	void (*login_cb)(gpointer, gint, GaimInputCondition);
+	gboolean (*connect_cb)(MsnServConn *servconn);
+	void (*disconnect_cb)(MsnServConn *servconn);
 
 	void *data;
 };
@@ -86,20 +88,24 @@
 
 void msn_servconn_destroy(MsnServConn *servconn);
 
-gboolean msn_servconn_connect(MsnServConn *servconn);
+gboolean msn_servconn_connect(MsnServConn *servconn, const char *host,
+							  int port);
 void msn_servconn_disconnect(MsnServConn *servconn);
 
+#if 0
+/* shx: not used */
 void msn_servconn_set_server(MsnServConn *servconn, const char *server,
 							 int port);
 
 const char *msn_servconn_get_server(const MsnServConn *servconn);
 int msn_servconn_get_port(const MsnServConn *servconn);
+#endif
 
 void msn_servconn_set_connect_cb(MsnServConn *servconn,
-		gboolean (*connect_cb)(gpointer, gint, GaimInputCondition));
+		gboolean (*connect_cb)(MsnServConn *servconn));
 
-void msn_servconn_set_failed_read_cb(MsnServConn *servconn,
-		void (*failed_read_cb)(gpointer, gint, GaimInputCondition));
+void msn_servconn_set_disconnect_cb(MsnServConn *servconn,
+		void (*disconnect_cb)(MsnServConn *servconn));
 
 size_t msn_servconn_write(MsnServConn *servconn, const char *buf,
 						  size_t size);
--- a/src/protocols/msn/session.c	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/session.c	Mon Apr 05 07:11:24 2004 +0000
@@ -21,7 +21,6 @@
  */
 #include "msn.h"
 #include "session.h"
-#include "dispatch.h"
 #include "notification.h"
 
 MsnSession *
@@ -108,20 +107,24 @@
 
 	if (session->http_method)
 	{
-		session->notification_conn =
-			msn_notification_new(session, "gateway.messenger.hotmail.com", 80);
+		session->notification_conn = msn_notification_new(session);
 
-		if (msn_servconn_connect(session->notification_conn))
+		if (msn_notification_connect(session->notification_conn,
+									 "gateway.messenger.hotmail.com", 80))
+		{
 			return TRUE;
+		}
 	}
 	else
 	{
-		session->dispatch_conn = msn_dispatch_new(session,
-												  session->dispatch_server,
-												  session->dispatch_port);
+		session->notification_conn = msn_notification_new(session);
 
-		if (msn_servconn_connect(session->dispatch_conn))
+		if (msn_notification_connect(session->notification_conn,
+									 session->dispatch_server,
+									 session->dispatch_port))
+		{
 			return TRUE;
+		}
 	}
 
 	return FALSE;
@@ -133,11 +136,6 @@
 	g_return_if_fail(session != NULL);
 	g_return_if_fail(session->connected);
 
-	if (session->dispatch_conn != NULL) {
-		msn_servconn_destroy(session->dispatch_conn);
-		session->dispatch_conn = NULL;
-	}
-
 	while (session->switches != NULL) {
 		MsnSwitchBoard *board = (MsnSwitchBoard *)session->switches->data;
 
--- a/src/protocols/msn/session.h	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/session.h	Mon Apr 05 07:11:24 2004 +0000
@@ -45,7 +45,6 @@
 
 	gboolean connected;
 
-	MsnServConn *dispatch_conn;
 	MsnServConn *notification_conn;
 
 	MsnNexus *nexus;
--- a/src/protocols/msn/switchboard.c	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/switchboard.c	Mon Apr 05 07:11:24 2004 +0000
@@ -416,16 +416,12 @@
  * Connect stuff
  **************************************************************************/
 static gboolean
-connect_cb(gpointer data, gint source, GaimInputCondition cond)
+connect_cb(MsnServConn *servconn)
 {
-	MsnServConn *servconn = data;
 	GaimAccount *account = servconn->session->account;
 	MsnSwitchBoard *swboard = servconn->data;
 	char outparams[MSN_BUF_LEN];
 
-	if (servconn->fd != source)
-		servconn->fd = source;
-
 	swboard->in_use = TRUE;
 
 	gaim_debug(GAIM_DEBUG_INFO, "msn", "Connecting to switchboard...\n");
@@ -456,11 +452,13 @@
 }
 
 static void
-failed_read_cb(gpointer data, gint source, GaimInputCondition cond)
+disconnect_cb(MsnServConn *servconn)
 {
-	MsnServConn *servconn = data;
+	MsnSwitchBoard *swboard;
 
-	msn_switchboard_destroy(servconn->data);
+	swboard = servconn->data;
+	if (!swboard->destroying)
+		msn_switchboard_destroy(swboard);
 }
 
 MsnSwitchBoard *
@@ -475,7 +473,7 @@
 
 	swboard->servconn = servconn = msn_servconn_new(session);
 	msn_servconn_set_connect_cb(servconn, connect_cb);
-	msn_servconn_set_failed_read_cb(servconn, failed_read_cb);
+	msn_servconn_set_disconnect_cb(servconn, disconnect_cb);
 
 	if (session->http_method)
 		swboard->servconn->http_data->server_type = "SB";
@@ -533,7 +531,9 @@
 	MsnSession *session;
 
 	g_return_if_fail(swboard != NULL);
+	g_return_if_fail(!swboard->destroying);
 
+	swboard->destroying = TRUE;
 	session = swboard->servconn->session;
 
 	if (swboard->servconn->connected)
@@ -627,13 +627,11 @@
 }
 
 gboolean
-msn_switchboard_connect(MsnSwitchBoard *swboard, const char *server, int port)
+msn_switchboard_connect(MsnSwitchBoard *swboard, const char *host, int port)
 {
 	g_return_val_if_fail(swboard != NULL, FALSE);
 
-	msn_servconn_set_server(swboard->servconn, server, port);
-
-	if (msn_servconn_connect(swboard->servconn))
+	if (msn_servconn_connect(swboard->servconn, host, port))
 		swboard->in_use = TRUE;
 
 	return swboard->in_use;
--- a/src/protocols/msn/switchboard.h	Mon Apr 05 06:52:02 2004 +0000
+++ b/src/protocols/msn/switchboard.h	Mon Apr 05 07:11:24 2004 +0000
@@ -40,6 +40,7 @@
 	char *session_id;
 
 	gboolean invited;
+	gboolean destroying;
 
 	GaimConversation *chat;
 
@@ -146,13 +147,13 @@
  * Connects to a switchboard.
  *
  * @param swboard The switchboard.
- * @param server  The server.
+ * @param host    The host.
  * @param port    The port.
  *
  * @return @c TRUE if able to connect, or @c FALSE otherwise.
  */
 gboolean msn_switchboard_connect(MsnSwitchBoard *swboard,
-								 const char *server, int port);
+								 const char *host, int port);
 
 /**
  * Disconnects from a switchboard.