changeset 31439:dc996aa83f49

Combine the encode_spaces function and the stuff for the display name since they're basically the same. Also fixes #13034.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 03 Dec 2010 04:51:48 +0000
parents 703dd6f433a9
children 3cff7a5e0351
files libpurple/protocols/msn/msn.c libpurple/protocols/msn/msnutils.c libpurple/protocols/msn/msnutils.h
diffstat 3 files changed, 57 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/msn.c	Fri Dec 03 03:49:40 2010 +0000
+++ b/libpurple/protocols/msn/msn.c	Fri Dec 03 04:51:48 2010 +0000
@@ -250,7 +250,7 @@
 	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 @@
 	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;
--- a/libpurple/protocols/msn/msnutils.c	Fri Dec 03 03:49:40 2010 +0000
+++ b/libpurple/protocols/msn/msnutils.c	Fri Dec 03 04:51:48 2010 +0000
@@ -182,29 +182,40 @@
  * 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 @@
 	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 @@
 	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;
 
--- a/libpurple/protocols/msn/msnutils.h	Fri Dec 03 03:49:40 2010 +0000
+++ b/libpurple/protocols/msn/msnutils.h	Fri Dec 03 04:51:48 2010 +0000
@@ -33,6 +33,18 @@
 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.