Added the function that quotes emails
[modest] / src / modest-text-utils.c
index 99cb710..ae7f8eb 100644 (file)
@@ -1699,6 +1699,7 @@ modest_text_utils_validate_recipient (const gchar *recipient, const gchar **inva
 
        /* quoted string */
        if (*current == '\"') {
 
        /* quoted string */
        if (*current == '\"') {
+               gchar *last_quote = NULL;
                current = g_utf8_next_char (current);
                has_error = TRUE;
                for (; *current != '\0'; current = g_utf8_next_char (current)) {
                current = g_utf8_next_char (current);
                has_error = TRUE;
                for (; *current != '\0'; current = g_utf8_next_char (current)) {
@@ -1714,9 +1715,11 @@ modest_text_utils_validate_recipient (const gchar *recipient, const gchar **inva
                        } else if (*current == '\"') {
                                has_error = FALSE;
                                current = g_utf8_next_char (current);
                        } else if (*current == '\"') {
                                has_error = FALSE;
                                current = g_utf8_next_char (current);
-                               break;
+                               last_quote = current;
                        }
                }
                        }
                }
+               if (last_quote)
+                       current = last_quote;
        } else {
                has_error = TRUE;
                for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
        } else {
                has_error = TRUE;
                for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
@@ -1736,6 +1739,9 @@ modest_text_utils_validate_recipient (const gchar *recipient, const gchar **inva
        g_free (stripped);
        right_part = g_strstrip (right_part);
 
        g_free (stripped);
        right_part = g_strstrip (right_part);
 
+       if (g_str_has_suffix (right_part, ",") || g_str_has_suffix (right_part, ";"))
+               right_part [(strlen (right_part) - 1)] = '\0';
+
        if (g_str_has_prefix (right_part, "<") &&
            g_str_has_suffix (right_part, ">")) {
                gchar *address;
        if (g_str_has_prefix (right_part, "<") &&
            g_str_has_suffix (right_part, ">")) {
                gchar *address;
@@ -2078,3 +2084,84 @@ modest_text_utils_get_secure_header (const gchar *value,
 
        return new_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) + 1;
+
+       while (**start == ' ')
+               *start = g_utf8_next_char (*start);
+
+       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 - 1);
+                       *str = g_string_append (*str, ";\n ");
+                       *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, ";\n");
+                       *start = g_utf8_next_char (*cur);
+               }
+       } else {
+               *str = g_string_append_len (*str, *start, (str_len - 1));
+               *str = g_string_append (*str, ";\n");
+               *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);
+}