* Fixes NB#87165, do not allow folder names like LTP* or COM*
[modest] / src / modest-text-utils.c
index 8deb5cc..6ef698a 100644 (file)
@@ -360,17 +360,24 @@ modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
        
        g_return_val_if_fail (address_list, NULL);
 
        
        g_return_val_if_fail (address_list, NULL);
 
-       table = g_hash_table_new (g_str_hash, g_str_equal);
+       table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
        addresses = modest_text_utils_split_addresses_list (address_list);
 
        cursor = addresses;
        while (cursor) {
                const gchar* address = (const gchar*)cursor->data;
 
        addresses = modest_text_utils_split_addresses_list (address_list);
 
        cursor = addresses;
        while (cursor) {
                const gchar* address = (const gchar*)cursor->data;
 
+               /* We need only the email to just compare it and not
+                  the full address which would make "a <a@a.com>"
+                  different from "a@a.com" */
+               const gchar *email = get_email_from_address (address);
+
                /* ignore the address if already seen */
                /* ignore the address if already seen */
-               if (g_hash_table_lookup (table, address) == 0) {
+               if (g_hash_table_lookup (table, email) == 0) {
                        gchar *tmp;
 
                        gchar *tmp;
 
+                       /* Include the full address and not only the
+                          email in the returned list */
                        if (!new_list) {
                                tmp = g_strdup (address);
                        } else {
                        if (!new_list) {
                                tmp = g_strdup (address);
                        } else {
@@ -379,12 +386,12 @@ modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
                        }
                        new_list = tmp;
                        
                        }
                        new_list = tmp;
                        
-                       g_hash_table_insert (table, (gchar*)address, GINT_TO_POINTER(1));
+                       g_hash_table_insert (table, (gchar*)email, GINT_TO_POINTER(1));
                }
                cursor = g_slist_next (cursor);
        }
 
                }
                cursor = g_slist_next (cursor);
        }
 
-       g_hash_table_destroy (table);
+       g_hash_table_unref (table);
        g_slist_foreach (addresses, (GFunc)g_free, NULL);
        g_slist_free (addresses);
 
        g_slist_foreach (addresses, (GFunc)g_free, NULL);
        g_slist_free (addresses);
 
@@ -1336,9 +1343,8 @@ modest_text_utils_validate_folder_name (const gchar *folder_name)
        gint i;
        const gchar **cursor = NULL;
        const gchar *forbidden_names[] = { /* windows does not like these */
        gint i;
        const gchar **cursor = NULL;
        const gchar *forbidden_names[] = { /* windows does not like these */
-               "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
-               "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
-               ".", "..", "cur", "tmp", "new", NULL /* cur, tmp new  are reserved for Maildir */
+               "CON", "PRN", "AUX", "NUL", ".", "..", "cur", "tmp", "new", 
+               NULL /* cur, tmp, new are reserved for Maildir */
        };
        
        /* cannot be NULL */
        };
        
        /* cannot be NULL */
@@ -1362,6 +1368,27 @@ modest_text_utils_validate_folder_name (const gchar *folder_name)
        for (i = 0; i < len; i++)
                if (modest_text_utils_is_forbidden_char (folder_name[i], FOLDER_NAME_FORBIDDEN_CHARS))
                        return FALSE;
        for (i = 0; i < len; i++)
                if (modest_text_utils_is_forbidden_char (folder_name[i], FOLDER_NAME_FORBIDDEN_CHARS))
                        return FALSE;
+
+       /* Cannot contain Windows port numbers. I'd like to use GRegex
+          but it's still not available in Maemo. sergio */
+       if (g_str_has_prefix (folder_name, "LTP") ||
+           g_str_has_prefix (folder_name, "ltp") ||
+           g_str_has_prefix (folder_name, "COM") ||
+           g_str_has_prefix (folder_name, "com")) {
+               glong val;
+               gchar *endptr;
+
+               /* We skip the first 3 characters for the
+                  comparison */
+               val = strtol(folder_name+3, &endptr, 10);
+
+               /* If the conversion to long succeeded then the string
+                  is not valid for us */
+               if (*endptr == '\0')
+                       return FALSE;
+               else
+                       return TRUE;
+       }
        
        /* cannot contain a forbidden word */
        if (len <= 4) {
        
        /* cannot contain a forbidden word */
        if (len <= 4) {