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



El mié, 26-09-2007 a las 11:28 +0200, Philip Van Hoof escribió:
> Hey Dape,
> 
> You are adding two new functions to one file, yet not making them
> static. Please keep them static in this case (no more need to use the
> underscores too).

	Attached new patch.

> 
> On Wed, 2007-09-26 at 11:17 +0200, Jose Dapena Paz wrote:
> > 	Hi,
> > 
> > 	This patch fixes the capability to create addresses lists when the
> > string you part from has quotes.
> > 
> > 	For example, this string address should be valid:
> > 	"Dapena Paz, Jose" <jdapena igalia com>
> > 
> > 	But this string was converted to two addresses:
> > 	"Dapena Paz
> > 	 Jose " <jdapena igalia com>
> > 
> > 	We also remove the quotes in name temporary buffer as they're added in
> > camel side again. If we don't remove them, the quotes are escaped.
> > _______________________________________________
> > tinymail-devel-list mailing list
> > tinymail-devel-list gnome org
> > http://mail.gnome.org/mailman/listinfo/tinymail-devel-list
-- 
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>
 
+void remove_quotes (gchar *buffer);
+gchar **split_recipients (gchar *buffer);
+
+
 /* TODOL Rename to tny_camel_session_check_operation. */
 /** _tny_session_check_operation:
  * @session: A camel session.
@@ -164,6 +168,59 @@
 }
 
 void
+remove_quotes (gchar *buffer)
+{
+	gchar *tmp = buffer;
+	gboolean first_is_quote = FALSE;
+
+	/* 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';
+
+}
+
+gchar **
+split_recipients (gchar *buffer)
+{
+	gchar *tmp, *start;
+	gboolean is_quoted = FALSE;
+	GPtrArray *array = g_ptr_array_new ();
+
+	start = tmp = buffer;
+
+	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)
 {
 	char *stfnd = NULL;
@@ -193,6 +250,7 @@
 	
 		if (G_LIKELY (*lname == ' '))
 			*lname-- = '\0';
+		remove_quotes (name);
 		camel_internet_address_add (target, name, email);
 	} else {
 		
@@ -215,21 +273,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: libtinymail-camel/tny-camel-common-priv.h
===================================================================
--- libtinymail-camel/tny-camel-common-priv.h	(revisión: 2805)
+++ libtinymail-camel/tny-camel-common-priv.h	(copia de trabajo)
@@ -30,6 +30,8 @@
 #include <tny-camel-folder.h>
 #include <tny-session-camel.h>
 
+void _remove_quotes (gchar *buffer);
+gchar **_split_recipients (gchar *buffer);
 void _string_to_camel_inet_addr (gchar *tok, CamelInternetAddress *target);
 void _foreach_email_add_to_inet_addr (const gchar *emails, CamelInternetAddress *target);
 gboolean _tny_folder_store_query_passes (TnyFolderStoreQuery *query, CamelFolderInfo *finfo);
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]