Re: Patch: fix recipients parsing when a recipient list has quotes



El mié, 26-09-2007 a las 11:36 +0200, Philip Van Hoof escribió:

> Put static in front of the function names and remove their prototypes
> from the .h file.

	Wops, patch badly reviewed.

	I also added NULL input validation.
> 
> 
-- 
Jose Dapena Paz <jdapena igalia com>
Igalia
Index: libtinymail-camel/tny-camel-common.c
===================================================================
--- libtinymail-camel/tny-camel-common.c	(revisión: 2805)
+++ libtinymail-camel/tny-camel-common.c	(copia de trabajo)
@@ -30,6 +30,10 @@
 
 #include <tny-folder-store-query.h>
 
+static void remove_quotes (gchar *buffer);
+static gchar **split_recipients (gchar *buffer);
+
+
 /* TODOL Rename to tny_camel_session_check_operation. */
 /** _tny_session_check_operation:
  * @session: A camel session.
@@ -163,6 +167,67 @@
 	return retval;
 }
 
+static void
+remove_quotes (gchar *buffer)
+{
+	gchar *tmp = buffer;
+	gboolean first_is_quote = FALSE;
+
+	if (buffer == NULL)
+		return;
+
+	/* First we remove the first quote */
+	first_is_quote = (buffer[0] == '\"');
+	while (*tmp != '\0') {
+		if ((tmp[1] == '\"') && (tmp[2] == '\0'))
+			tmp[1] = '\0';
+		if (first_is_quote)
+			tmp[0] = tmp[1];
+		tmp++;
+	}
+
+	if ((tmp > buffer) && (*(tmp-1) == '\"'))
+		*(tmp-1) = '\0';
+
+}
+
+static gchar **
+split_recipients (gchar *buffer)
+{
+	gchar *tmp, *start;
+	gboolean is_quoted = FALSE;
+	GPtrArray *array = g_ptr_array_new ();
+
+	start = tmp = buffer;
+
+	if (buffer == NULL) {
+		g_ptr_array_add (array, NULL);
+		return (gchar **) g_ptr_array_free (array, FALSE);
+	}
+
+	while (*tmp != '\0') {
+		if (*tmp == '\"')
+			is_quoted = !is_quoted;
+		if (*tmp == '\\')
+			tmp++;
+		if ((!is_quoted) && ((*tmp == ',') || (*tmp == ';'))) {
+			gchar *part;
+			part = g_strndup (start, tmp - start);
+			g_ptr_array_add (array, part);
+			start = tmp+1;
+		}
+		
+		tmp++;
+	}
+
+	if (start != tmp)
+		g_ptr_array_add (array, g_strdup (start));
+	
+	g_ptr_array_add (array, NULL);
+	return (gchar **) g_ptr_array_free (array, FALSE);
+}
+
+
 void
 _string_to_camel_inet_addr (gchar *tok, CamelInternetAddress *target)
 {
@@ -193,6 +258,7 @@
 	
 		if (G_LIKELY (*lname == ' '))
 			*lname-- = '\0';
+		remove_quotes (name);
 		camel_internet_address_add (target, name, email);
 	} else {
 		
@@ -215,21 +281,23 @@
 _foreach_email_add_to_inet_addr (const gchar *emails, CamelInternetAddress *target)
 {
 	char *dup = g_strdup (emails);
-	char *tok, *save;
+	gchar **parts, **current;
 
 	if (!emails)
 		return;
 
-	tok = strtok_r (dup, ",;", &save);
+	parts = split_recipients (dup);
+	current = parts;
 
-	while (G_LIKELY (tok != NULL))
+	while (G_LIKELY (*current != NULL))
 	{
 		
-		_string_to_camel_inet_addr (tok, target);
+		_string_to_camel_inet_addr (*current, target);
 
-		tok = strtok_r (NULL, ",;", &save);
+		current++;
 	}
 
+	g_strfreev (parts);
 	g_free (dup);
 
 	return;
Index: ChangeLog
===================================================================
--- ChangeLog	(revisión: 2805)
+++ ChangeLog	(copia de trabajo)
@@ -1,3 +1,15 @@
+2007-09-26  Jose Dapena Paz  <jdapena igalia com>
+
+	* libtinymail-camel/tny-camel-common.c,
+	(_foreach_email_add_to_inet_addr): now we don't use
+	strtok_r() to split the string to addresses, as we
+	weren't taking into account the quotes. I implemented
+	a new _split_recipients() method that takes quotes
+	into account.
+	(_string_to_camel_inet_addr): now we remove the quotes
+	in the name part of an address, as they are added by
+	camel part automatically.
+
 2007-09-25  Mark Doffman <mark doffman codethink co uk>
 	
 	* /bindings/python/*: Remove the platform generation


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]