pidgin: b1607e19: Combine the encode_spaces function and t...

qulogic at pidgin.im qulogic at pidgin.im
Fri Dec 3 00:35:58 EST 2010


----------------------------------------------------------------------
Revision: b1607e19c7482752502c484458a301037441c720
Parent:   d608a133197c453522b566b803c9dc10a9519863
Author:   qulogic at pidgin.im
Date:     12/02/10 23:51:48
Branch:   im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/b1607e19c7482752502c484458a301037441c720

Changelog: 

Combine the encode_spaces function and the stuff for the display name
since they're basically the same.

Also fixes #13034.

Changes against parent d608a133197c453522b566b803c9dc10a9519863

  patched  libpurple/protocols/msn/msn.c
  patched  libpurple/protocols/msn/msnutils.c
  patched  libpurple/protocols/msn/msnutils.h

-------------- next part --------------
============================================================
--- libpurple/protocols/msn/msn.c	bda1a923042947b60e41a2b29391fa688e18eb76
+++ libpurple/protocols/msn/msn.c	40175280d4403feb789de2b167360465082f226a
@@ -250,7 +250,7 @@ msn_set_public_alias(PurpleConnection *p
 	MsnSession *session;
 	MsnTransaction *trans;
 	PurpleAccount *account;
-	char real_alias[BUDDY_ALIAS_MAXLEN+1];
+	char real_alias[BUDDY_ALIAS_MAXLEN + 1];
 	struct public_alias_closure *closure;
 
 	session = purple_connection_get_protocol_data(pc);
@@ -258,53 +258,25 @@ msn_set_public_alias(PurpleConnection *p
 	account = purple_connection_get_account(pc);
 
 	if (alias && *alias) {
-		int i = 0;
-		while (isspace(*alias))
-			alias++;
-
-		for (; *alias && i < BUDDY_ALIAS_MAXLEN; alias++) {
-			if (*alias == '%') {
-				if (i > BUDDY_ALIAS_MAXLEN - 4)
-					break;
-				real_alias[i++] = '%';
-				real_alias[i++] = '2';
-				real_alias[i++] = '5';
-			} else if (*alias == ' ') {
-				if (i > BUDDY_ALIAS_MAXLEN - 4)
-					break;
-				real_alias[i++] = '%';
-				real_alias[i++] = '2';
-				real_alias[i++] = '0';
+		if (!msn_encode_spaces(alias, real_alias, BUDDY_ALIAS_MAXLEN + 1)) {
+			if (failure_cb) {
+				struct public_alias_closure *closure =
+					g_new0(struct public_alias_closure, 1);
+				closure->account = account;
+				closure->failure_cb = failure_cb;
+				purple_timeout_add(0, set_public_alias_length_error, closure);
 			} else {
-				real_alias[i++] = *alias;
+				purple_notify_error(pc, NULL,
+				                    _("Your new MSN friendly name is too long."),
+				                    NULL);
 			}
+			return;
 		}
 
-		while (i && isspace(real_alias[i - 1]))
-			i--;
-
-		real_alias[i] = '\0';
+		if (real_alias[0] == '\0')
+			strcpy(real_alias, purple_account_get_username(account));
 	} else
-		real_alias[0] = '\0';
-
-	if (*alias) {
-		if (failure_cb) {
-			struct public_alias_closure *closure =
-				g_new0(struct public_alias_closure, 1);
-			closure->account = account;
-			closure->failure_cb = failure_cb;
-			purple_timeout_add(0, set_public_alias_length_error, closure);
-		} else {
-			purple_notify_error(pc, NULL,
-			                    _("Your new MSN friendly name is too long."),
-			                    NULL);
-		}
-		return;
-	}
-
-	if (real_alias[0] == '\0') {
 		strcpy(real_alias, purple_account_get_username(account));
-	}
 
 	closure = g_new0(struct public_alias_closure, 1);
 	closure->account = account;
============================================================
--- libpurple/protocols/msn/msnutils.c	f8731993195da7a946e959bb3a3c0c11d8adafd4
+++ libpurple/protocols/msn/msnutils.c	51389fecf7a89471581f579bf9b8fa9b6fc1b9c3
@@ -182,29 +182,40 @@ msn_encode_mime(const char *str)
  * We need this because we're only supposed to encode spaces in the font
  * names. purple_url_encode() isn't acceptable.
  */
-static const char *
-encode_spaces(const char *str)
+gboolean
+msn_encode_spaces(const char *str, char *buf, size_t len)
 {
-	static char buf[BUF_LEN];
-	const char *c;
-	char *d;
+	char *nonspace = buf;
 
-	g_return_val_if_fail(str != NULL, NULL);
+	while (isspace(*str))
+		str++;
 
-	for (c = str, d = buf; *c != '\0'; c++)
-	{
-		if (*c == ' ')
-		{
-			*d++ = '%';
-			*d++ = '2';
-			*d++ = '0';
+	for (; *str && len > 1; str++) {
+		if (*str == '%') {
+			if (len < 4)
+				break;
+			*buf++ = '%';
+			*buf++ = '2';
+			*buf++ = '5';
+			len -= 3;
+			nonspace = buf;
+		} else if (*str == ' ') {
+			if (len < 4)
+				break;
+			*buf++ = '%';
+			*buf++ = '2';
+			*buf++ = '0';
+			len -= 3;
+		} else {
+			*buf++ = *str;
+			len--;
+			nonspace = buf;
 		}
-		else
-			*d++ = *c;
 	}
-	*d = '\0';
 
-	return buf;
+	*nonspace = '\0';
+
+	return (*str == '\0');
 }
 
 /*
@@ -223,6 +234,7 @@ msn_import_html(const char *html, char *
 	const char *c;
 	char *msg;
 	char *fontface = NULL;
+	char fontface_encoded[BUF_LEN];
 	char fonteffect[5];
 	char fontcolor[7];
 	char direction = '0';
@@ -449,8 +461,9 @@ msn_import_html(const char *html, char *
 	if (fontface == NULL)
 		fontface = g_strdup("Segoe UI");
 
+	msn_encode_spaces(fontface, fontface_encoded, BUF_LEN);
 	*attributes = g_strdup_printf("FN=%s; EF=%s; CO=%s; PF=0; RL=%c",
-								  encode_spaces(fontface),
+								  fontface_encoded,
 								  fonteffect, fontcolor, direction);
 	*message = msg;
 
============================================================
--- libpurple/protocols/msn/msnutils.h	232933b74215d8024d248ceaf0ec3545ef33f0c7
+++ libpurple/protocols/msn/msnutils.h	d540e1fff6ce209faaff8ede79fb611c0a91f141
@@ -33,6 +33,18 @@ char *rand_guid(void);
 char *rand_guid(void);
 
 /**
+ * Encodes the spaces in a string
+ *
+ * @param str The string to be encoded.
+ * @param buf The buffer to hold the encoded string.
+ * @param len The maximum length (including NUL) to put in @buf.
+ *
+ * @return Whether @str was able to fit in @buf.
+ */
+gboolean
+msn_encode_spaces(const char *str, char *buf, size_t len);
+
+/**
  * Parses the MSN message formatting into a format compatible with Purple.
  *
  * @param mime     The mime header with the formatting.


More information about the Commits mailing list