Increase the char limit for considering a header as "secure"
[modest] / src / modest-text-utils.c
index 15405e8..7bceaf0 100644 (file)
@@ -604,59 +604,52 @@ modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hy
 void
 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
 {
-       gchar *current, *start, *last_blank;
-       gint start_offset = 0, current_offset = 0;
-
-       g_return_if_fail (start_indexes != NULL);
-       g_return_if_fail (end_indexes != NULL);
-
-       start = (gchar *) addresses;
-       current = start;
-       last_blank = start;
-
-       while (*current != '\0') {
-               if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
-                       start = g_utf8_next_char (start);
-                       start_offset++;
-                       last_blank = current;
-               } else if ((*current == ',')||(*current == ';')) {
+       GString *str;
+       gchar *start, *cur;
+
+       if (!addresses)
+               return;
+
+       if (strlen (addresses) == 0)
+               return;
+
+       str = g_string_new ("");
+       start = (gchar*) addresses;
+       cur = (gchar*) addresses;
+
+       for (cur = start; *cur != '\0'; cur = g_utf8_next_char (cur)) {
+               if (*cur == ',' || *cur == ';') {
                        gint *start_index, *end_index;
-                       start_index = g_new0(gint, 1);
-                       end_index = g_new0(gint, 1);
-                       *start_index = start_offset;
-                       *end_index = current_offset;
+                       gchar *next_char = g_utf8_next_char (cur);
+
+                       if (!g_utf8_strchr (start, (cur - start + 1), g_utf8_get_char ("@")) &&
+                           next_char && *next_char != '\n')
+                               continue;
+
+                       start_index = g_new0 (gint, 1);
+                       end_index = g_new0 (gint, 1);
+                       *start_index = g_utf8_pointer_to_offset (addresses, start);
+                       *end_index = g_utf8_pointer_to_offset (addresses, cur);;
                        *start_indexes = g_slist_prepend (*start_indexes, start_index);
                        *end_indexes = g_slist_prepend (*end_indexes, end_index);
-                       start = g_utf8_next_char (current);
-                       start_offset = current_offset + 1;
-                       last_blank = start;
-               } else if (*current == '"') {
-                       current = g_utf8_next_char (current);
-                       current_offset ++;
-                       while ((*current != '"')&&(*current != '\0')) {
-                               current = g_utf8_next_char (current);
-                               current_offset ++;
-                       }
+                       start = g_utf8_next_char (cur);
                }
-                               
-               current = g_utf8_next_char (current);
-               current_offset ++;
        }
 
-       if (start != current) {
-                       gint *start_index, *end_index;
-                       start_index = g_new0(gint, 1);
-                       end_index = g_new0(gint, 1);
-                       *start_index = start_offset;
-                       *end_index = current_offset;
-                       *start_indexes = g_slist_prepend (*start_indexes, start_index);
-                       *end_indexes = g_slist_prepend (*end_indexes, end_index);
+       if (start != cur) {
+               gint *start_index, *end_index;
+               start_index = g_new0 (gint, 1);
+               end_index = g_new0 (gint, 1);
+               *start_index = g_utf8_pointer_to_offset (addresses, start);
+               *end_index = g_utf8_pointer_to_offset (addresses, cur);;
+               *start_indexes = g_slist_prepend (*start_indexes, start_index);
+               *end_indexes = g_slist_prepend (*end_indexes, end_index);
        }
-       
-       *start_indexes = g_slist_reverse (*start_indexes);
-       *end_indexes = g_slist_reverse (*end_indexes);
 
-       return;
+       if (*start_indexes)
+               *start_indexes = g_slist_reverse (*start_indexes);
+       if (*end_indexes)
+               *end_indexes = g_slist_reverse (*end_indexes);
 }
 
 
@@ -1719,7 +1712,7 @@ modest_text_utils_validate_recipient (const gchar *recipient, const gchar **inva
                        }
                }
                if (last_quote)
-                       current = last_quote;
+                       current = g_utf8_next_char (last_quote);
        } else {
                has_error = TRUE;
                for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
@@ -2054,7 +2047,7 @@ gchar *
 modest_text_utils_get_secure_header (const gchar *value,
                                     const gchar *header)
 {
-       const gint max_len = 128;
+       const gint max_len = 16384;
        gchar *new_value = NULL;
        gchar *needle = g_strrstr (value, header);
 
@@ -2084,3 +2077,86 @@ modest_text_utils_get_secure_header (const gchar *value,
 
        return new_value;
 }
+
+static gboolean
+is_quoted (const char *start, const gchar *end)
+{
+       gchar *c;
+
+       c = (gchar *) start;
+       while (*c == ' ')
+               c = g_utf8_next_char (c);
+
+       if (*c == '\0' || *c != '\"')
+               return FALSE;
+
+       c = (gchar *) end;
+       while (*c == ' ' && c != start)
+               c = g_utf8_prev_char (c);
+
+       if (c == start || *c != '\"')
+               return FALSE;
+
+       return TRUE;
+}
+
+
+static void
+quote_name_part (GString **str, gchar **cur, gchar **start)
+{
+       gchar *blank;
+       gint str_len = g_utf8_pointer_to_offset (*start, *cur) -
+               g_utf8_pointer_to_offset (*start, *start);
+
+       while (**start == ' ') {
+               *start = g_utf8_next_char (*start);
+               str_len--;
+       }
+
+       blank = g_utf8_strrchr (*start, str_len, g_utf8_get_char (" "));
+       if (blank && (blank != *start)) {
+               if (is_quoted (*start, blank - 1)) {
+                       *str = g_string_append_len (*str, *start, str_len);
+                       *str = g_string_append (*str, ";");
+                       *start = g_utf8_next_char (*cur);
+               } else {
+                       *str = g_string_append_c (*str, '"');
+                       *str = g_string_append_len (*str, *start,
+                                                   (g_utf8_pointer_to_offset (*start, blank) -
+                                                    g_utf8_pointer_to_offset (*start, *start)));
+                       *str = g_string_append_c (*str, '"');
+                       *str = g_string_append_len (*str, blank,
+                                                   (g_utf8_pointer_to_offset (*start, *cur) -
+                                                    g_utf8_pointer_to_offset (*start, blank)));
+                       *str = g_string_append (*str, ";");
+                       *start = g_utf8_next_char (*cur);
+               }
+       } else {
+               *str = g_string_append_len (*str, *start, str_len);
+               *str = g_string_append (*str, ";");
+               *start = g_utf8_next_char (*cur);
+       }
+}
+
+gchar *
+modest_text_utils_quote_names (const gchar *recipients)
+{
+       GString *str;
+       gchar *start, *cur;
+
+       str = g_string_new ("");
+       start = (gchar*) recipients;
+       cur = (gchar*) recipients;
+
+       for (cur = start; *cur != '\0'; cur = g_utf8_next_char (cur)) {
+               if (*cur == ',' || *cur == ';') {
+                       if (!g_utf8_strchr (start, (cur - start + 1), g_utf8_get_char ("@")))
+                               continue;
+                       quote_name_part (&str, &cur, &start);
+               }
+       }
+
+       quote_name_part (&str, &cur, &start);
+
+       return g_string_free (str, FALSE);
+}