changeset 17504:c921f901c7ea

Improve bonjour buddy TXT record parsing.
author Daniel Atallah <daniel.atallah@gmail.com>
date Tue, 05 Jun 2007 22:23:10 +0000
parents ba71c53186e9
children 9ccec98b8ca9
files libpurple/protocols/bonjour/buddy.c libpurple/protocols/bonjour/buddy.h libpurple/protocols/bonjour/mdns_howl.c libpurple/protocols/bonjour/mdns_win32.c
diffstat 4 files changed, 75 insertions(+), 142 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/bonjour/buddy.c	Tue Jun 05 22:13:23 2007 +0000
+++ b/libpurple/protocols/bonjour/buddy.c	Tue Jun 05 22:23:10 2007 +0000
@@ -38,43 +38,42 @@
 }
 
 void
-set_bonjour_buddy_value(BonjourBuddy* buddy, bonjour_buddy_member member, const char* value, uint32_t len)
-{
-	gchar **key = NULL;
-	switch (member)
-	{
-		case E_BUDDY_FIRST:
-			key = &buddy->first;
-			break;
-		case E_BUDDY_LAST:
-			key = &buddy->last;
-			break;
-		case E_BUDDY_STATUS:
-			key = &buddy->status;
-			break;
-		case E_BUDDY_EMAIL:
-			key = &buddy->email;
-			break;
-		case E_BUDDY_PHSH:
-			key = &buddy->phsh;
-			break;
-		case E_BUDDY_JID:
-			key = &buddy->jid;
-			break;
-		case E_BUDDY_AIM:
-			key = &buddy->AIM;
-			break;
-		case E_BUDDY_VC:
-			key = &buddy->vc;
-			break;
-		case E_BUDDY_MSG:
-			key = &buddy->msg;
-			break;
-	}
+set_bonjour_buddy_value(BonjourBuddy* buddy, const char *record_key, const char *value, uint32_t len){
+	gchar **fld = NULL;
 
-	g_free(*key);
-	*key = NULL;
-	*key = g_strndup(value, len);
+	if (!strcmp(record_key, "1st"))
+		fld = &buddy->first;
+	else if(!strcmp(record_key, "email"))
+		fld = &buddy->email;
+	else if(!strcmp(record_key, "ext"))
+		fld = &buddy->ext;
+	else if(!strcmp(record_key, "jid"))
+		fld = &buddy->jid;
+	else if(!strcmp(record_key, "last"))
+		fld = &buddy->last;
+	else if(!strcmp(record_key, "msg"))
+		fld = &buddy->msg;
+	else if(!strcmp(record_key, "nick"))
+		fld = &buddy->nick;
+	else if(!strcmp(record_key, "node"))
+		fld = &buddy->node;
+	else if(!strcmp(record_key, "phsh"))
+		fld = &buddy->phsh;
+	else if(!strcmp(record_key, "status"))
+		fld = &buddy->status;
+	else if(!strcmp(record_key, "vc"))
+		fld = &buddy->vc;
+	else if(!strcmp(record_key, "ver"))
+		fld = &buddy->ver;
+	else if(!strcmp(record_key, "AIM"))
+		fld = &buddy->AIM;
+
+	if(fld == NULL)
+		return;
+
+	g_free(*fld);
+	*fld = NULL;
+	*fld = g_strndup(value, len);
 }
 
 /**
--- a/libpurple/protocols/bonjour/buddy.h	Tue Jun 05 22:13:23 2007 +0000
+++ b/libpurple/protocols/bonjour/buddy.h	Tue Jun 05 22:23:10 2007 +0000
@@ -34,8 +34,10 @@
 	PurpleAccount *account;
 
 	gchar *name;
+	gchar *ip;
+	gint port_p2pj;
+
 	gchar *first;
-	gint port_p2pj;
 	gchar *phsh;
 	gchar *status;
 	gchar *email;
@@ -43,40 +45,49 @@
 	gchar *jid;
 	gchar *AIM;
 	gchar *vc;
-	gchar *ip;
 	gchar *msg;
-	
+	gchar *ext;
+	gchar *nick;
+	gchar *node;
+	gchar *ver;
+
 	BonjourJabberConversation *conversation;
-	
+
 #ifdef USE_BONJOUR_APPLE
 	DNSServiceRef txt_query;
 	int txt_query_fd;
 #endif
-	
+
 } BonjourBuddy;
 
-typedef enum _bonjour_buddy_member
-{
-	E_BUDDY_FIRST,
-	E_BUDDY_LAST,
-	E_BUDDY_STATUS,
-	E_BUDDY_EMAIL,
-	E_BUDDY_PHSH,
-	E_BUDDY_JID,
-	E_BUDDY_AIM,
-	E_BUDDY_VC,
-	E_BUDDY_MSG
-} bonjour_buddy_member;
+static const char *const buddy_TXT_records[] = {
+	"1st",
+	"email",
+	"ext",
+	"jid",
+	"last",
+	"msg",
+	"nick",
+	"node",
+	"phsh",
+/*	"port.p2pj", Deprecated - MUST ignore */
+	"status",
+/*	"txtvers", Deprecated - hardcoded to 1 */
+	"vc",
+	"ver",
+	"AIM", /* non standard */
+	NULL
+};
 
 /**
  * Creates a new buddy.
  */
-BonjourBuddy *bonjour_buddy_new(const gchar *name, PurpleAccount* account);
+BonjourBuddy *bonjour_buddy_new(const gchar *name, PurpleAccount *account);
 
 /**
  * Sets a value in the BonjourBuddy struct, destroying the old value
  */
-void set_bonjour_buddy_value(BonjourBuddy* buddy, bonjour_buddy_member member, const char* value, uint32_t len);
+void set_bonjour_buddy_value(BonjourBuddy* buddy, const char *record_key, const char *value, uint32_t len);
 
 /**
  * Check if all the compulsory buddy data is present.
--- a/libpurple/protocols/bonjour/mdns_howl.c	Tue Jun 05 22:13:23 2007 +0000
+++ b/libpurple/protocols/bonjour/mdns_howl.c	Tue Jun 05 22:23:10 2007 +0000
@@ -55,8 +55,6 @@
 {
 	BonjourBuddy *buddy;
 	PurpleAccount *account = (PurpleAccount*)extra;
-	/*gchar *txtvers = NULL;*/
-	/*gchar *version = NULL;*/
 	gint address_length = 16;
 	sw_text_record_iterator iterator;
 	char key[SW_TEXT_RECORD_MAX_LEN];
@@ -79,42 +77,9 @@
 	{
 		sw_text_record_iterator_init(&iterator, text_record, text_record_len);
 		while (sw_text_record_iterator_next(iterator, key, (sw_octet *)value, &value_length) == SW_OKAY)
-		{
-			/* Compare the keys with the possible ones and save them on */
-			/* the appropiate place of the buddy_list */
-			if (strcmp(key, "txtvers") == 0) {
-				/*txtvers = g_strdup(value);*/
-			} else if (strcmp(key, "version") == 0) {
-				/*version = g_strdup(value);*/
-			} else if (strcmp(key, "1st") == 0) {
-				g_free(buddy->first);
-				buddy->first = g_strdup(value);
-			} else if (strcmp(key, "status") == 0) {
-				g_free(buddy->status);
-				buddy->status = g_strdup(value);
-			} else if (strcmp(key, "email") == 0) {
-				g_free(buddy->email);
-				buddy->email = g_strdup(value);
-			} else if (strcmp(key, "last") == 0) {
-				g_free(buddy->last);
-				buddy->last = g_strdup(value);
-			} else if (strcmp(key, "jid") == 0) {
-				g_free(buddy->jid);
-				buddy->jid = g_strdup(value);
-			} else if (strcmp(key, "AIM") == 0) {
-				g_free(buddy->AIM);
-				buddy->AIM = g_strdup(value);
-			} else if (strcmp(key, "vc") == 0) {
-				g_free(buddy->vc);
-				buddy->vc = g_strdup(value);
-			} else if (strcmp(key, "phsh") == 0) {
-				g_free(buddy->phsh);
-				buddy->phsh = g_strdup(value);
-			} else if (strcmp(key, "msg") == 0) {
-				g_free(buddy->msg);
-				buddy->msg = g_strdup(value);
-	}
-		}
+			set_bonjour_buddy_value(buddy, key, value, value_length);
+
+		sw_text_record_iterator_fina(iterator);
 	}
 
 	if (!bonjour_buddy_check(buddy))
@@ -126,10 +91,6 @@
 	/* Add or update the buddy in our buddy list */
 	bonjour_buddy_add_to_purple(buddy);
 
-	/* Free all the temporal strings */
-	/*g_free(txtvers);*/
-	/*g_free(version);*/
-
 	return SW_OKAY;
 }
 
--- a/libpurple/protocols/bonjour/mdns_win32.c	Tue Jun 05 22:13:23 2007 +0000
+++ b/libpurple/protocols/bonjour/mdns_win32.c	Tue Jun 05 22:23:10 2007 +0000
@@ -177,56 +177,18 @@
 	}
 }
 
-void 
+void
 _mdns_parse_text_record(BonjourBuddy* buddy, const char* record, uint16_t record_len)
 {
-	char *txt_entry;
+	const char *txt_entry;
 	uint8_t txt_len;
+	int i;
 
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "1st", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_FIRST, txt_entry, txt_len);
-	}
-		
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "last", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_LAST, txt_entry, txt_len);
-	}
-			
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "status", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_STATUS, txt_entry, txt_len);
-	}
-				
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "email", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_EMAIL, txt_entry, txt_len);
+	for (i = 0; buddy_TXT_records[i] != NULL; i++) {
+		txt_entry = TXTRecordGetValuePtr(record_len, record, buddy_TXT_records[i], &txt_len);
+		if (txt_entry != NULL)
+			set_bonjour_buddy_value(buddy, buddy_TXT_records[i], txt_entry, txt_len);
 	}
-		
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "jid", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_JID, txt_entry, txt_len);
-	}
-		
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "AIM", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_AIM, txt_entry, txt_len);
-	}
-	
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "VC", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_VC, txt_entry, txt_len);
-	}
-	
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "phsh", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_PHSH, txt_entry, txt_len);
-	}
-	
-	if (NULL != (txt_entry = (char*)TXTRecordGetValuePtr(record_len, record, "msg", &txt_len)))
-	{
-		set_bonjour_buddy_value(buddy, E_BUDDY_MSG, txt_entry, txt_len);
-	}	
 }
 
 int