* make modest handle mailing lists somewhat correcter (RFC2369):
[modest] / src / modest-text-utils.c
index c054a5e..de70fa4 100644 (file)
  */
 #define HYPERLINKIFY_MAX_LENGTH (1024*50)
 
+
+/*
+ * we mark the ampersand with \007 when converting text->html
+ * because after text->html we do hyperlink detecting, which
+ * could be screwed up by the ampersand
+ */
+#define MARK_AMP '\007'
+#define MARK_AMP_STR "\007"
+
+
 /*
  * we need these regexps to find URLs in plain text e-mails
  */
@@ -100,7 +110,7 @@ const gchar account_title_forbidden_chars[] = {
        '\\', '/', ':', '*', '?', '\'', '<', '>', '|', '^'
 };
 const gchar folder_name_forbidden_chars[] = {
-       '<', '>', ':', '\'', '/', '\\', '|', '?', '*', '^', '%', '$'
+       '<', '>', ':', '\'', '/', '\\', '|', '?', '*', '^', '%', '$', '#', '&'
 };
 const gchar user_name_forbidden_chars[] = {
        '<', '>'
@@ -320,10 +330,48 @@ modest_text_utils_remove_address (const gchar *address_list, const gchar *addres
        return result;
 }
 
+
+gchar*
+modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
+{
+       GSList *addresses, *cursor;
+       GHashTable *table;
+       gchar *new_list;
+       
+       g_return_val_if_fail (address_list, NULL);
+
+       table = g_hash_table_new (g_str_hash, g_str_equal);
+       addresses = modest_text_utils_split_addresses_list (address_list);
+
+       new_list = g_strdup("");
+       cursor = addresses;
+       while (cursor) {
+               const gchar* address = (const gchar*)cursor->data;
+
+               /* ignore the address if already seen */
+               if (g_hash_table_lookup (table, address) == 0) {
+               
+                       gchar *tmp = g_strjoin (",", new_list, address, NULL);
+                       g_free (new_list);
+                       new_list = tmp;
+                       
+                       g_hash_table_insert (table, (gchar*)address, GINT_TO_POINTER(1));
+               }
+               cursor = g_slist_next (cursor);
+       }
+
+       g_hash_table_destroy (table);
+       g_slist_foreach (addresses, (GFunc)g_free, NULL);
+       g_slist_free (addresses);
+
+       return new_list;
+}
+
+
 static void
-modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data, gssize n)
+modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data, gssize n)
 {
-       guint            i;
+       guint           i;
        gboolean        space_seen = FALSE;
        guint           break_dist = 0; /* distance since last break point */
 
@@ -349,16 +397,21 @@ modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data, gssi
                }
                
                switch (kar) {
-               case 0:  break; /* ignore embedded \0s */       
-               case '<'  : g_string_append (html, "&lt;");   break;
-               case '>'  : g_string_append (html, "&gt;");   break;
-               case '&'  : g_string_append (html, "&amp;");  break;
-               case '"'  : g_string_append (html, "&quot;");  break;
+               case 0:
+               case MARK_AMP: /* this is a temp place holder for '&'; we can only
+                               * set the real '&' after hyperlink translation, otherwise
+                               * we might screw that up */
+                       break; /* ignore embedded \0s and MARK_AMP */   
+               case '<'  : g_string_append (html, MARK_AMP_STR "lt;");   break;
+               case '>'  : g_string_append (html, MARK_AMP_STR "gt;");   break;
+               case '&'  : g_string_append (html, MARK_AMP_STR "amp;");  break;
+               case '"'  : g_string_append (html, MARK_AMP_STR "quot;");  break;
 
                /* don't convert &apos; --> wpeditor will try to re-convert it... */    
                //case '\'' : g_string_append (html, "&apos;"); break;
-               case '\n' : g_string_append (html, "<br>\n");              break_dist= 0; break;
-               case '\t' : g_string_append (html, "&nbsp;&nbsp;&nbsp; "); break_dist=0; break; /* note the space at the end*/
+               case '\n' : g_string_append (html, "<br>\n");break_dist= 0; break;
+               case '\t' : g_string_append (html, MARK_AMP_STR "nbsp;" MARK_AMP_STR "nbsp;" MARK_AMP_STR "nbsp; ");
+                       break_dist=0; break; /* note the space at the end*/
                case ' ':
                        break_dist = 0;
                        if (space_seen) { /* second space in a row */
@@ -373,6 +426,18 @@ modest_text_utils_convert_buffer_to_html (GString *html, const gchar *data, gssi
        }
 }
 
+
+static void
+modest_text_utils_convert_buffer_to_html_finish (GString *html)
+{
+       int i;
+       /* replace all our MARK_AMPs with real ones */
+       for (i = 0; i != html->len; ++i)
+               if ((html->str)[i] == MARK_AMP)
+                       (html->str)[i] = '&';
+}
+
+
 gchar*
 modest_text_utils_convert_to_html (const gchar *data)
 {
@@ -393,13 +458,15 @@ modest_text_utils_convert_to_html (const gchar *data)
                                "</head>"
                                "<body>");
 
-       modest_text_utils_convert_buffer_to_html (html, data, -1);
+       modest_text_utils_convert_buffer_to_html_start (html, data, -1);
        
        g_string_append (html, "</body></html>");
 
        if (len <= HYPERLINKIFY_MAX_LENGTH)
                hyperlinkify_plain_text (html);
 
+       modest_text_utils_convert_buffer_to_html_finish (html);
+       
        return g_string_free (html, FALSE);
 }
 
@@ -417,11 +484,13 @@ modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hy
                n = strlen (data);
        html = g_string_sized_new (1.5 * n);    /* just a  guess... */
 
-       modest_text_utils_convert_buffer_to_html (html, data, n);
+       modest_text_utils_convert_buffer_to_html_start (html, data, n);
 
        if (hyperlinkify && (n < HYPERLINKIFY_MAX_LENGTH))
                hyperlinkify_plain_text (html);
 
+       modest_text_utils_convert_buffer_to_html_finish (html);
+       
        return g_string_free (html, FALSE);
 }
 
@@ -483,6 +552,7 @@ modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_
        return;
 }
 
+#if 0
 GSList *
 modest_text_utils_split_addresses_list (const gchar *addresses)
 {
@@ -525,6 +595,43 @@ modest_text_utils_split_addresses_list (const gchar *addresses)
        return result;
 
 }
+#endif
+
+
+
+
+GSList *
+modest_text_utils_split_addresses_list (const gchar *addresses)
+{
+       GSList *head;
+       const gchar *my_addrs = addresses;
+       const gchar *end;
+       gchar *addr;
+       
+       /* skip any space, ',', ';' at the start */
+       while (my_addrs && (my_addrs[0] == ' ' || my_addrs[0] == ',' || my_addrs[0] == ';'))
+              ++my_addrs;
+
+       /* are we at the end of addresses list? */
+       if (!my_addrs[0])
+               return NULL;
+       
+       /* nope, we are at the start of some address
+        * now, let's find the end of the address */
+       end = my_addrs + 1;
+       while (end[0] && end[0] != ',' && end[0] != ';')
+               ++end;
+
+       /* we got the address; copy it and remove trailing whitespace */
+       addr = g_strndup (my_addrs, end - my_addrs);
+       g_strchomp (addr);
+
+       head = g_slist_append (NULL, addr);
+       head->next = modest_text_utils_split_addresses_list (end); /* recurse */
+
+       return head;
+}
+
 
 void
 modest_text_utils_address_range_at_position (const gchar *recipients_list,
@@ -946,8 +1053,7 @@ get_url_matches (GString *txt)
                                match->len    = rm.rm_eo - rm.rm_so;
                                match->prefix = patterns[i].prefix;
                                match_list = g_slist_prepend (match_list, match);
-                       }
-                               
+                       }               
                        offset += rm.rm_eo;
                }
        }
@@ -1218,7 +1324,7 @@ modest_text_utils_validate_domain_name (const gchar *domain)
 {
        gboolean valid = FALSE;
        regex_t rx;
-       const gchar* domain_regex = "^[a-z0-9]([.]?[a-z0-9-])*[a-z0-9]$";
+       const gchar* domain_regex = "^([a-z0-9-]*[a-z0-9]\\.)+[a-z0-9-]*[a-z0-9]$";
 
        g_return_val_if_fail (domain, FALSE);