Make LmConnection accept not having a server set as long as JID is set.
authorMikael Hallendal <micke@imendio.com>
Sat, 24 Feb 2007 16:51:06 +0100
changeset 245 df2655dbe873
parent 244 8a05b79bad24
child 246 ef2e388b8412
Make LmConnection accept not having a server set as long as JID is set. Extracted the server from JID code into connection_get_server_from_jid. Also make connection_do_connect accept that server is not set as long as it can extract this from a set JID.
loudmouth/lm-connection.c
loudmouth/lm-socket.c
--- a/loudmouth/lm-connection.c	Sat Feb 24 16:23:01 2007 +0100
+++ b/loudmouth/lm-connection.c	Sat Feb 24 16:51:06 2007 +0100
@@ -55,6 +55,7 @@
 	gchar        *server;
 	gchar        *jid;
 	guint         port;
+	gboolean      use_srv;
 
 	LmSocket     *socket;
 	LmSSL        *ssl;
@@ -151,6 +152,8 @@
 static void      connection_incoming_data        (LmSocket        *socket, 
 						  const gchar     *buf,
 						  LmConnection    *connection);
+static gboolean  connection_get_server_from_jid  (const gchar     *jid,
+						  gchar          **server);
 
 static void
 connection_free (LmConnection *connection)
@@ -394,6 +397,8 @@
 static gboolean
 connection_do_open (LmConnection *connection, GError **error) 
 {
+	gchar *connect_server;
+
 	if (lm_connection_is_open (connection)) {
 		g_set_error (error,
 			     LM_ERROR,
@@ -403,11 +408,24 @@
 	}
 
 	if (!connection->server) {
-		g_set_error (error,
-			     LM_ERROR,
-			     LM_ERROR_CONNECTION_FAILED,
-			     "You need to set the server hostname in the call to lm_connection_new()");
-		return FALSE;
+		if (!connection->use_srv) {
+			g_set_error (error,
+				     LM_ERROR,
+				     LM_ERROR_CONNECTION_FAILED,
+				     "You need to set the server hostname in the call to lm_connection_new()");
+			return FALSE;
+		} else {
+			if (!connection_get_server_from_jid (connection->jid,
+							     &connect_server)) {
+				g_set_error (error,
+					     LM_ERROR,
+					     LM_ERROR_CONNECTION_FAILED,
+					     "You need to either set server hostname or jid");
+				return FALSE;
+			}
+		}
+	} else {
+		connect_server = g_strdup (connection->server);
 	}
 
 	lm_message_queue_attach (connection->queue, connection->context);
@@ -423,12 +441,14 @@
 					       connection,
 					       connection,
 					       connection->blocking,
-					       connection->server,
+					       connect_server,
 					       connection->port,
-					       FALSE,
+					       connection->use_srv,
 					       connection->ssl,
 					       connection->proxy,
 					       error);
+	g_free (connect_server);
+
 	if (!connection->socket) {
 		return FALSE;
 	}
@@ -729,6 +749,27 @@
 	lm_parser_parse (connection->parser, buf);
 }
 
+static gboolean
+connection_get_server_from_jid (const gchar     *jid,
+				gchar          **server)
+{
+	gchar *ch;
+	gchar *ch_end;
+
+	if (jid != NULL && (ch = strchr (jid, '@')) != NULL) {
+		ch_end = strchr(ch + 1, '/');
+		if (ch_end != NULL) {
+			*server = g_strndup (ch + 1, ch_end - ch - 1);
+		} else {
+			*server = g_strdup (ch + 1);
+		}
+
+		return TRUE;
+	} 
+	
+	return FALSE;
+}
+
 void 
 _lm_connection_socket_result (LmConnection *connection, gboolean result)
 {
@@ -942,9 +983,11 @@
 	connection = g_new0 (LmConnection, 1);
 
 	if (server) {
-		connection->server = _lm_utils_hostname_to_punycode (server);
+		connection->server  = _lm_utils_hostname_to_punycode (server);
+		connection->use_srv = FALSE;
 	} else {
-		connection->server = NULL;
+		connection->server  = NULL;
+		connection->use_srv = TRUE;
 	}
 
 	connection->context           = NULL;
@@ -1149,8 +1192,7 @@
 			       gboolean success,
 			       const gchar *reason)
 {
-	gchar *server_from_jid;
-	gchar *ch;
+	gchar     *server_from_jid;
 	LmMessage *m;
 
 	if (!success) {
@@ -1159,10 +1201,8 @@
 		return;
 	}
 
-	if (connection->jid != NULL && (ch = strchr (connection->jid, '@')) != NULL) {
-		server_from_jid = ch + 1;
-	} else {
-		server_from_jid = connection->server;
+	if (!connection_get_server_from_jid (connection->jid, &server_from_jid)) {
+		server_from_jid = g_strdup (connection->server);
 	}
 
 	m = lm_message_new (server_from_jid, LM_MESSAGE_TYPE_STREAM);
@@ -1173,6 +1213,8 @@
 					"version", "1.0",
 					NULL);
 
+	g_free (server_from_jid);
+
 	lm_verbose ("Reopening XMPP 1.0 stream...");
 
 	if (!lm_connection_send (connection, m, NULL)) {
@@ -1451,6 +1493,7 @@
 	
 	g_free (connection->server);
 	connection->server = _lm_utils_hostname_to_punycode (server);
+	connection->use_srv = FALSE;
 }
 
 /**
--- a/loudmouth/lm-socket.c	Sat Feb 24 16:23:01 2007 +0100
+++ b/loudmouth/lm-socket.c	Sat Feb 24 16:51:06 2007 +0100
@@ -736,7 +736,10 @@
 
 		res_init ();
 
+
 		srv = g_strdup_printf ("_xmpp-client._tcp.%s", socket->server);
+		lm_verbose ("Performing a SRV lookup for %s\n", srv);
+
 		err = res_query (srv, C_IN, T_SRV, srv_ans, SRV_LEN);
 		if (err > 0) {
 			gchar    *new_server;
@@ -753,6 +756,8 @@
 			}
 		}
 		g_free (srv);
+	} else {
+		lm_verbose ("SRV lookup disabled for %s\n", socket->server);
 	}
 
 	if (context) {