pidgin: 958ca7ce: The hostname used for a bonjour account ...

datallah at pidgin.im datallah at pidgin.im
Sun Oct 3 22:40:53 EDT 2010


----------------------------------------------------------------------
Revision: 958ca7ce54d7b9ca8ca24659a4a1beb1d18a16a8
Parent:   a084eb6aec289bbbf44ac9666db8dc52f3143730
Author:   datallah at pidgin.im
Date:     10/03/10 20:48:25
Branch:   im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/958ca7ce54d7b9ca8ca24659a4a1beb1d18a16a8

Changelog: 

The hostname used for a bonjour account should always be the current machine
name.  This fixes our behavior so the previous statement is always the case.
Unfortunately, this means that if the hostname was previously not the current
machine name, the bonjour jid ends up looking like "username\40otherhost at hostname".

Fixes #12674

Changes against parent a084eb6aec289bbbf44ac9666db8dc52f3143730

  patched  libpurple/protocols/bonjour/bonjour.c
  patched  libpurple/protocols/bonjour/bonjour.h
  patched  libpurple/protocols/bonjour/bonjour_ft.c
  patched  libpurple/protocols/bonjour/jabber.c
  patched  libpurple/protocols/bonjour/mdns_avahi.c
  patched  libpurple/protocols/bonjour/mdns_common.c
  patched  libpurple/protocols/bonjour/mdns_common.h
  patched  libpurple/protocols/bonjour/mdns_win32.c

-------------- next part --------------
============================================================
--- libpurple/protocols/bonjour/bonjour.c	fcdf122a69369c99544f97b892061c53827e787e
+++ libpurple/protocols/bonjour/bonjour.c	9f373f1963c40837750948ec0b072e6eb1abb608
@@ -46,8 +46,15 @@ static char *default_lastname;
 
 static char *default_firstname;
 static char *default_lastname;
-static char *default_hostname;
 
+const char *
+bonjour_get_jid(PurpleAccount *account)
+{
+	PurpleConnection *conn = purple_account_get_connection(account);
+	BonjourData *bd = conn->proto_data;
+	return bd->jid;
+}
+
 static void
 bonjour_removeallfromlocal(PurpleConnection *conn, PurpleGroup *bonjour_group)
 {
@@ -182,6 +189,7 @@ bonjour_close(PurpleConnection *connecti
 		purple_xfer_cancel_local(bd->xfer_lists->data);
 	}
 
+	g_free(bd->jid);
 	g_free(bd);
 	connection->proto_data = NULL;
 }
@@ -389,7 +397,8 @@ bonjour_tooltip_text(PurpleBuddy *buddy,
 		purple_notify_user_info_add_pair(user_info, _("XMPP Account"), bb->jid);
 }
 
-static void bonjour_do_group_change(PurpleBuddy *buddy, const char *new_group) {
+static void
+bonjour_do_group_change(PurpleBuddy *buddy, const char *new_group) {
 	PurpleBlistNodeFlags oldflags;
 
 	if (buddy == NULL)
@@ -445,7 +454,6 @@ plugin_unload(PurplePlugin *plugin)
 
 	g_free(default_firstname);
 	g_free(default_lastname);
-	g_free(default_hostname);
 
 	return TRUE;
 }
@@ -566,7 +574,8 @@ static PurplePluginInfo info =
 };
 
 #ifdef WIN32
-static gboolean _set_default_name_cb(gpointer data) {
+static gboolean
+_set_default_name_cb(gpointer data) {
 	gchar *fullname = data;
 	const char *splitpoint;
 	GList *tmp = prpl_info.protocol_options;
@@ -603,7 +612,8 @@ static gboolean _set_default_name_cb(gpo
 	return FALSE;
 }
 
-static gpointer _win32_name_lookup_thread(gpointer data) {
+static gpointer
+_win32_name_lookup_thread(gpointer data) {
 	gchar *fullname = NULL;
 	wchar_t username[UNLEN + 1];
 	DWORD dwLenUsername = UNLEN + 1;
@@ -707,24 +717,15 @@ initialize_default_account_values(void)
 	}
 
 	g_free(conv);
-
-	/* Try to figure out a good host name to use */
-	/* TODO: Avoid 'localhost,' if possible */
-	default_hostname = g_strdup(purple_get_host_name());
 }
 
 static void
 init_plugin(PurplePlugin *plugin)
 {
-	PurpleAccountUserSplit *split;
 	PurpleAccountOption *option;
 
 	initialize_default_account_values();
 
-	/* Creating the user splits */
-	split = purple_account_user_split_new(_("Hostname"), default_hostname, '@');
-	prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
-
 	/* Creating the options for the protocol */
 	option = purple_account_option_int_new(_("Local Port"), "port", BONJOUR_DEFAULT_PORT);
 	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, option);
============================================================
--- libpurple/protocols/bonjour/bonjour.h	b12fcaae1b424a4561f7f92b30840cae31eff193
+++ libpurple/protocols/bonjour/bonjour.h	578776000264109acb0af3db48dbda7e74ea463d
@@ -45,6 +45,12 @@ typedef struct _BonjourData
 	BonjourDnsSd *dns_sd_data;
 	BonjourJabber *jabber_data;
 	GSList *xfer_lists;
+	gchar *jid;
 } BonjourData;
 
+/**
+ *  This will always be username at machinename
+ */
+const char *bonjour_get_jid(PurpleAccount *account);
+
 #endif /* _BONJOUR_H_ */
============================================================
--- libpurple/protocols/bonjour/jabber.c	8d3d8f420b2ea8c19da007c2aea6f669f353b31e
+++ libpurple/protocols/bonjour/jabber.c	eed925bef9928a4c65037b5c123fa200b528c6b7
@@ -425,7 +425,7 @@ _client_socket_handler(gpointer data, gi
 			bonjour_jabber_close_conversation(bconv);
 			if (bconv->pb != NULL) {
 				BonjourBuddy *bb = purple_buddy_get_protocol_data(bconv->pb);
-				
+
 				if(bb != NULL)
 					bb->conversation = NULL;
 			}
@@ -534,7 +534,7 @@ static gboolean bonjour_jabber_send_stre
 	if (bname == NULL)
 		bname = "";
 
-	stream_start = g_strdup_printf(DOCTYPE, purple_account_get_username(bconv->account), bname);
+	stream_start = g_strdup_printf(DOCTYPE, bonjour_get_jid(bconv->account), bname);
 	len = strlen(stream_start);
 
 	bconv->sent_stream_start = PARTIALLY_SENT;
@@ -1044,7 +1044,7 @@ bonjour_jabber_send_message(BonjourJabbe
 
 	message_node = xmlnode_new("message");
 	xmlnode_set_attrib(message_node, "to", bb->name);
-	xmlnode_set_attrib(message_node, "from", purple_account_get_username(jdata->account));
+	xmlnode_set_attrib(message_node, "from", bonjour_get_jid(jdata->account));
 	xmlnode_set_attrib(message_node, "type", "chat");
 
 	/* Enclose the message from the UI within a "font" node */
@@ -1259,7 +1259,7 @@ check_if_blocked(PurpleBuddy *pb)
 
 	for(l = acc->deny; l != NULL; l = l->next) {
 		const gchar *name = purple_buddy_get_name(pb);
-		const gchar *username = purple_account_get_username(acc);
+		const gchar *username = bonjour_get_jid(acc);
 
 		if(!purple_utf8_strcasecmp(name, (char *)l->data)) {
 			purple_debug_info("bonjour", "%s has been blocked by %s.\n", name, username);
============================================================
--- libpurple/protocols/bonjour/mdns_common.c	09e9ab96de106f89b84348171bbf6599304c4a68
+++ libpurple/protocols/bonjour/mdns_common.c	5c4193610db1c3e2eec72b99f169f04ce024f083
@@ -252,3 +252,36 @@ void bonjour_dns_sd_stop(BonjourDnsSd *d
 void bonjour_dns_sd_stop(BonjourDnsSd *data) {
 	_mdns_stop(data);
 }
+
+void
+bonjour_dns_sd_set_jid(PurpleAccount *account, const char *hostname)
+{
+	PurpleConnection *conn = purple_account_get_connection(account);
+	BonjourData *bd = conn->proto_data;
+	const char *tmp, *account_name = purple_account_get_username(account);
+
+	/* Previously we allowed the hostname part of the jid to be set
+	 * explicitly when it should always be the current hostname.
+	 * That is what this is intended to deal with.
+	 */
+	if ((tmp = strchr(account_name, '@'))
+	    && strstr(account_name, hostname) == ++tmp
+	    && *(tmp + strlen(hostname)) == '\0')
+		bd->jid = g_strdup(account_name);
+	else {
+		const char *tmp2;
+		GString *str = g_string_new("");
+		/* Escape an '@' in the account name */
+		tmp = account_name;
+		while ((tmp2 = strchr(tmp, '@')) != NULL) {
+			g_string_append_len(str, tmp, tmp2 - tmp);
+			g_string_append(str, "\\40");
+			tmp = tmp2 + 1;
+		}
+		g_string_append(str, tmp);
+		g_string_append_c(str, '@');
+		g_string_append(str, hostname);
+
+		bd->jid = g_string_free(str, FALSE);
+	}
+}
\ No newline at end of file
============================================================
--- libpurple/protocols/bonjour/mdns_common.h	ef0191d8f503ddd6b496ff732c04b3e650b2d1a3
+++ libpurple/protocols/bonjour/mdns_common.h	b1e0f24c25ee11d9a13b3d7c9e2847f28710ed30
@@ -57,4 +57,6 @@ void bonjour_dns_sd_stop(BonjourDnsSd *d
  */
 void bonjour_dns_sd_stop(BonjourDnsSd *data);
 
+void bonjour_dns_sd_set_jid(PurpleAccount *account, const char *hostname);
+
 #endif
============================================================
--- libpurple/protocols/bonjour/mdns_win32.c	7ce6eea61f43d1359e7ffbee2e7971ee86e9527c
+++ libpurple/protocols/bonjour/mdns_win32.c	ee0c98057f6a0f9ae49cfe70bdc0e4e06e350d8d
@@ -26,6 +26,7 @@
 #include "mdns_interface.h"
 #include "dns_sd_proxy.h"
 #include "mdns_common.h"
+#include "bonjour.h"
 
 static GSList *pending_buddies = NULL;
 
@@ -326,7 +327,7 @@ _mdns_service_browse_callback(DNSService
 		purple_debug_error("bonjour", "service browser - callback error (%d)\n", errorCode);
 	else if (flags & kDNSServiceFlagsAdd) {
 		/* A presence service instance has been discovered... check it isn't us! */
-		if (purple_utf8_strcasecmp(serviceName, account->username) != 0) {
+		if (purple_utf8_strcasecmp(serviceName, bonjour_get_jid(account)) != 0) {
 			DNSServiceErrorType resErrorCode;
 			/* OK, lets go ahead and resolve it to add to the buddy list */
 			ResolveCallbackArgs *args = g_new0(ResolveCallbackArgs, 1);
@@ -454,6 +455,9 @@ gboolean _mdns_init_session(BonjourDnsSd
 
 gboolean _mdns_init_session(BonjourDnsSd *data) {
 	data->mdns_impl_data = g_new0(Win32SessionImplData, 1);
+
+	bonjour_dns_sd_set_jid(data->account, purple_get_host_name());
+
 	return TRUE;
 }
 
@@ -486,7 +490,7 @@ gboolean _mdns_publish(BonjourDnsSd *dat
 			case PUBLISH_START:
 				purple_debug_info("bonjour", "Registering presence on port %d\n", data->port_p2pj);
 				errorCode = DNSServiceRegister(&presence_sr, kDNSServiceInterfaceIndexAny,
-					0, purple_account_get_username(data->account), LINK_LOCAL_RECORD_NAME,
+					0, bonjour_get_jid(data->account), LINK_LOCAL_RECORD_NAME,
 					NULL, NULL, htons(data->port_p2pj), TXTRecordGetLength(&dns_data), TXTRecordGetBytesPtr(&dns_data),
 					_mdns_service_register_callback, NULL);
 				break;
============================================================
--- libpurple/protocols/bonjour/mdns_avahi.c	395aa47fd33f71fa0bfafed4411407834ba3a270
+++ libpurple/protocols/bonjour/mdns_avahi.c	7da97ca06c35f9dc1dc1e5b106a7dad3ea2fc107
@@ -252,7 +252,7 @@ _browser_callback(AvahiServiceBrowser *b
 			/* A new peer has joined the network and uses iChat bonjour */
 			purple_debug_info("bonjour", "_browser_callback - new service\n");
 			/* Make sure it isn't us */
-			if (purple_utf8_strcasecmp(name, account->username) != 0) {
+			if (purple_utf8_strcasecmp(name, bonjour_get_jid(account)) != 0) {
 				if (!avahi_service_resolver_new(avahi_service_browser_get_client(b),
 						interface, protocol, name, type, domain, protocol,
 						0, _resolver_callback, account)) {
@@ -421,6 +421,8 @@ gboolean _mdns_init_session(BonjourDnsSd
 
 	data->mdns_impl_data = idata;
 
+	bonjour_dns_sd_set_jid(data->account, avahi_client_get_host_name(idata->client));
+
 	return TRUE;
 }
 
@@ -454,14 +456,14 @@ gboolean _mdns_publish(BonjourDnsSd *dat
 			publish_result = avahi_entry_group_add_service_strlst(
 				idata->group, AVAHI_IF_UNSPEC,
 				AVAHI_PROTO_UNSPEC, 0,
-				purple_account_get_username(data->account),
+				bonjour_get_jid(data->account),
 				LINK_LOCAL_RECORD_NAME, NULL, NULL, data->port_p2pj, lst);
 			break;
 		case PUBLISH_UPDATE:
 			publish_result = avahi_entry_group_update_service_txt_strlst(
 				idata->group, AVAHI_IF_UNSPEC,
 				AVAHI_PROTO_UNSPEC, 0,
-				purple_account_get_username(data->account),
+				bonjour_get_jid(data->account),
 				LINK_LOCAL_RECORD_NAME, NULL, lst);
 			break;
 	}
@@ -535,7 +537,7 @@ gboolean _mdns_set_buddy_icon_data(Bonjo
 		}
 
 		svc_name = g_strdup_printf("%s." LINK_LOCAL_RECORD_NAME "local",
-				purple_account_get_username(data->account));
+				bonjour_get_jid(data->account));
 
 		ret = avahi_entry_group_add_record(idata->buddy_icon_group, AVAHI_IF_UNSPEC,
 			AVAHI_PROTO_UNSPEC, flags, svc_name,
============================================================
--- libpurple/protocols/bonjour/bonjour_ft.c	277080b0283bd73a1b27658613486b6a0e9db626
+++ libpurple/protocols/bonjour/bonjour_ft.c	db4ddb347410a58b2595c1115f6798e5b378d5ab
@@ -53,11 +53,12 @@ xep_ft_si_reject(BonjourData *bd, const 
 	g_return_if_fail(error_code != NULL);
 	g_return_if_fail(error_type != NULL);
 
-	if(!to || !id)
+	if(!to || !id) {
+		purple_debug_info("bonjour", "xep file transfer stream initialization error.\n");
 		return;
+	}
 
-	purple_debug_info("bonjour", "xep file transfer stream initialization error.\n");
-	iq = xep_iq_new(bd, XEP_IQ_ERROR, to, purple_account_get_username(bd->jabber_data->account), id);
+	iq = xep_iq_new(bd, XEP_IQ_ERROR, to, bonjour_get_jid(bd->jabber_data->account), id);
 	if(iq == NULL)
 		return;
 
@@ -195,7 +196,7 @@ xep_ft_si_offer(PurpleXfer *xfer, const 
 	/* Assign stream id. */
 	g_free(xf->iq_id);
 	xf->iq_id = g_strdup_printf("%u", next_id++);
-	iq = xep_iq_new(xf->data, XEP_IQ_SET, to, purple_account_get_username(bd->jabber_data->account), xf->iq_id);
+	iq = xep_iq_new(xf->data, XEP_IQ_SET, to, bonjour_get_jid(bd->jabber_data->account), xf->iq_id);
 	if(iq == NULL)
 		return;
 
@@ -255,7 +256,7 @@ xep_ft_si_result(PurpleXfer *xfer, char 
 	bd = xf->data;
 
 	purple_debug_info("bonjour", "xep file transfer stream initialization result.\n");
-	iq = xep_iq_new(bd, XEP_IQ_RESULT, to, purple_account_get_username(bd->jabber_data->account), xf->iq_id);
+	iq = xep_iq_new(bd, XEP_IQ_RESULT, to, bonjour_get_jid(bd->jabber_data->account), xf->iq_id);
 	if(iq == NULL)
 		return;
 
@@ -763,7 +764,7 @@ bonjour_bytestreams_listen(int sock, gpo
 
 	bd = xf->data;
 
-	iq = xep_iq_new(bd, XEP_IQ_SET, xfer->who, purple_account_get_username(bd->jabber_data->account), xf->sid);
+	iq = xep_iq_new(bd, XEP_IQ_SET, xfer->who, bonjour_get_jid(bd->jabber_data->account), xf->sid);
 
 	query = xmlnode_new_child(iq->node, "query");
 	xmlnode_set_namespace(query, "http://jabber.org/protocol/bytestreams");
@@ -835,7 +836,7 @@ bonjour_bytestreams_connect_cb(gpointer 
 	/* Here, start the file transfer.*/
 
 	/* Notify Initiator of Connection */
-	iq = xep_iq_new(bd, XEP_IQ_RESULT, xfer->who, purple_account_get_username(bd->jabber_data->account), xf->iq_id);
+	iq = xep_iq_new(bd, XEP_IQ_RESULT, xfer->who, bonjour_get_jid(bd->jabber_data->account), xf->iq_id);
 	q_node = xmlnode_new_child(iq->node, "query");
 	xmlnode_set_namespace(q_node, "http://jabber.org/protocol/bytestreams");
 	tmp_node = xmlnode_new_child(q_node, "streamhost-used");
@@ -868,7 +869,7 @@ bonjour_bytestreams_connect(PurpleXfer *
 	name = purple_buddy_get_name(pb);
 	account = purple_buddy_get_account(pb);
 
-	p = g_strdup_printf("%s%s%s", xf->sid, name, purple_account_get_username(account));
+	p = g_strdup_printf("%s%s%s", xf->sid, name, bonjour_get_jid(account));
 	purple_cipher_digest_region("sha1", (guchar *)p, strlen(p),
 				    sizeof(hashval), hashval, NULL);
 	g_free(p);


More information about the Commits mailing list