c8b0539cc024a6ec69c67e79bdbbab0489a523a1
[modest] / src / modest-text-utils.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30
31
32 #ifndef _GNU_SOURCE
33 #define _GNU_SOURCE
34 #endif /*_GNU_SOURCE*/
35 #include <string.h> /* for strcasestr */
36
37
38 #include <glib.h>
39 #include <stdlib.h>
40 #include <glib/gi18n.h>
41 #include <regex.h>
42 #include <modest-tny-platform-factory.h>
43 #include <modest-text-utils.h>
44 #include <modest-runtime.h>
45 #include <ctype.h>
46
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif /*HAVE_CONFIG_H */
50
51 /* defines */
52 #define FORWARD_STRING _("mcen_ia_editor_original_message")
53 #define FROM_STRING _("mail_va_from")
54 #define SENT_STRING _("mcen_fi_message_properties_sent")
55 #define TO_STRING _("mail_va_to")
56 #define SUBJECT_STRING _("mail_va_subject")
57 #define EMPTY_STRING ""
58
59 /*
60  * do the hyperlinkification only for texts < 50 Kb,
61  * as it's quite slow. Without this, e.g. mail with
62  * an uuencoded part (which is not recognized as attachment,
63  * will hang modest
64  */
65 #define HYPERLINKIFY_MAX_LENGTH (1024*50)
66
67
68 /*
69  * we mark the ampersand with \007 when converting text->html
70  * because after text->html we do hyperlink detecting, which
71  * could be screwed up by the ampersand.
72  * ie. 1<3 ==> 1\007lt;3
73  */
74 #define MARK_AMP '\007'
75 #define MARK_AMP_STR "\007"
76
77 /* mark &amp; separately, because they are parts of urls.
78  * ie. a&b => 1\008amp;b
79  */
80 #define MARK_AMP_URI '\006'
81 #define MARK_AMP_URI_STR "\006"
82
83
84 /*
85  * we need these regexps to find URLs in plain text e-mails
86  */
87 typedef struct _url_match_pattern_t url_match_pattern_t;
88 struct _url_match_pattern_t {
89         gchar   *regex;
90         regex_t *preg;
91         gchar   *prefix;
92 };
93
94 typedef struct _url_match_t url_match_t;
95 struct _url_match_t {
96         guint offset;
97         guint len;
98         const gchar* prefix;
99 };
100
101 /* note: match MARK_AMP_URI_STR as well, because after txt->html, a '&' will look like $(MARK_AMP_URI_STR)"amp;" */
102 #define MAIL_VIEWER_URL_MATCH_PATTERNS  {                               \
103         { "(file|rtsp|http|ftp|https|mms|mmsh|rtsp|rdp|lastfm)://[-a-z0-9_$.+!*(),;:@%=?/~#" MARK_AMP_URI_STR "]+[-a-z0-9_$%" MARK_AMP_URI_STR "=?/~#]",\
104           NULL, NULL },\
105         { "www\\.[-a-z0-9_$.+!*(),;:@%=?/~#" MARK_AMP_URI_STR "]+[-a-z0-9_$%" MARK_AMP_URI_STR "=?/~#]",\
106                         NULL, "http://" },                              \
107         { "ftp\\.[-a-z0-9_$.+!*(),;:@%=?/~#" MARK_AMP_URI_STR "]+[-a-z0-9_$%" MARK_AMP_URI_STR "=?/~#]",\
108           NULL, "ftp://" },\
109         { "(jabberto|voipto|sipto|sip|chatto|xmpp):[-_a-z@0-9.+]+", \
110            NULL, NULL},                                             \
111         { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+",                    \
112           NULL, NULL},\
113         { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
114           NULL, "mailto:"}\
115         }
116
117 const gchar account_title_forbidden_chars[] = {
118         '\\', '/', ':', '*', '?', '\'', '<', '>', '|', '^'
119 };
120 const gchar folder_name_forbidden_chars[] = {
121         '<', '>', ':', '\'', '/', '\\', '|', '?', '*', '^', '%', '$', '#', '&'
122 };
123 const gchar user_name_forbidden_chars[] = {
124         '<', '>'
125 };
126 const guint ACCOUNT_TITLE_FORBIDDEN_CHARS_LENGTH = G_N_ELEMENTS (account_title_forbidden_chars);
127 const guint FOLDER_NAME_FORBIDDEN_CHARS_LENGTH = G_N_ELEMENTS (folder_name_forbidden_chars);
128 const guint USER_NAME_FORBIDDEN_CHARS_LENGTH = G_N_ELEMENTS (user_name_forbidden_chars);
129
130 /* private */
131 static gchar*   cite                    (const time_t sent_date, const gchar *from);
132 static void     hyperlinkify_plain_text (GString *txt);
133 static gint     cmp_offsets_reverse     (const url_match_t *match1, const url_match_t *match2);
134 static GSList*  get_url_matches         (GString *txt);
135
136 static GString* get_next_line           (const char *b, const gsize blen, const gchar * iter);
137 static int      get_indent_level        (const char *l);
138 static void     unquote_line            (GString * l);
139 static void     append_quoted           (GString * buf, const int indent, const GString * str, 
140                                          const int cutpoint);
141 static int      get_breakpoint_utf8     (const gchar * s, const gint indent, const gint limit);
142 static int      get_breakpoint_ascii    (const gchar * s, const gint indent, const gint limit);
143 static int      get_breakpoint          (const gchar * s, const gint indent, const gint limit);
144
145 static gchar*   modest_text_utils_quote_plain_text (const gchar *text, 
146                                                     const gchar *cite, 
147                                                     const gchar *signature,
148                                                     GList *attachments, 
149                                                     int limit);
150
151 static gchar*   modest_text_utils_quote_html       (const gchar *text, 
152                                                     const gchar *cite,
153                                                     const gchar *signature,
154                                                     GList *attachments,
155                                                     int limit);
156 static gchar*   get_email_from_address (const gchar *address);
157
158
159 /* ******************************************************************* */
160 /* ************************* PUBLIC FUNCTIONS ************************ */
161 /* ******************************************************************* */
162
163 gchar *
164 modest_text_utils_quote (const gchar *text, 
165                          const gchar *content_type,
166                          const gchar *signature,
167                          const gchar *from,
168                          const time_t sent_date, 
169                          GList *attachments,
170                          int limit)
171 {
172         gchar *retval, *cited;
173
174         g_return_val_if_fail (text, NULL);
175         g_return_val_if_fail (content_type, NULL);
176
177         cited = cite (sent_date, from);
178         
179         if (content_type && strcmp (content_type, "text/html") == 0)
180                 /* TODO: extract the <body> of the HTML and pass it to
181                    the function */
182                 retval = modest_text_utils_quote_html (text, cited, signature, attachments, limit);
183         else
184                 retval = modest_text_utils_quote_plain_text (text, cited, signature, attachments, limit);
185         
186         g_free (cited);
187         
188         return retval;
189 }
190
191
192 gchar *
193 modest_text_utils_cite (const gchar *text,
194                         const gchar *content_type,
195                         const gchar *signature,
196                         const gchar *from,
197                         time_t sent_date)
198 {
199         gchar *retval;
200         gchar *tmp_sig;
201         
202         g_return_val_if_fail (text, NULL);
203         g_return_val_if_fail (content_type, NULL);
204         
205         if (!signature)
206                 retval = g_strdup ("");
207         else if (strcmp(content_type, "text/html") == 0) {
208                 tmp_sig = g_strconcat ("\n", signature, NULL);
209                 retval = modest_text_utils_convert_to_html_body(tmp_sig, -1, TRUE);
210                 g_free (tmp_sig);
211         } else {
212                 retval = g_strconcat (text, "\n", signature, NULL);
213         }
214
215         return retval;
216 }
217
218 static gchar *
219 forward_cite (const gchar *from,
220               const gchar *sent,
221               const gchar *to,
222               const gchar *subject)
223 {
224         return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n", 
225                                 FORWARD_STRING, 
226                                 FROM_STRING, (from)?from:"",
227                                 SENT_STRING, sent,
228                                 TO_STRING, (to)?to:"",
229                                 SUBJECT_STRING, (subject)?subject:"");
230 }
231
232 gchar * 
233 modest_text_utils_inline (const gchar *text,
234                           const gchar *content_type,
235                           const gchar *signature,
236                           const gchar *from,
237                           time_t sent_date,
238                           const gchar *to,
239                           const gchar *subject)
240 {
241         gchar sent_str[101];
242         gchar *cited;
243         gchar *retval;
244         
245         g_return_val_if_fail (text, NULL);
246         g_return_val_if_fail (content_type, NULL);
247         
248         modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
249
250         cited = forward_cite (from, sent_str, to, subject);
251         
252         if (content_type && strcmp (content_type, "text/html") == 0)
253                 retval = modest_text_utils_quote_html (text, cited, signature, NULL, 80);
254         else
255                 retval = modest_text_utils_quote_plain_text (text, cited, signature, NULL, 80);
256         
257         g_free (cited);
258         return retval;
259 }
260
261 /* just to prevent warnings:
262  * warning: `%x' yields only last 2 digits of year in some locales
263  */
264 gsize
265 modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
266 {
267         struct tm tm;
268
269         /* does not work on old maemo glib: 
270          *   g_date_set_time_t (&date, timet);
271          */
272         localtime_r (&timet, &tm);
273         return strftime(s, max, fmt, &tm);
274 }
275
276 gchar *
277 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
278 {
279         gchar *tmp;
280
281         g_return_val_if_fail (prefix, NULL);
282
283         if (!subject || subject[0] == '\0')
284                 subject = _("mail_va_no_subject");
285
286         tmp = g_strchug (g_strdup (subject));
287
288         if (!strncmp (tmp, prefix, strlen (prefix))) {
289                 return tmp;
290         } else {
291                 g_free (tmp);
292                 return g_strdup_printf ("%s %s", prefix, subject);
293         }
294 }
295
296 gchar*
297 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
298 {
299         gchar *dup, *token, *ptr = NULL, *result;
300         GString *filtered_emails;
301         gchar *email_address;
302
303         g_return_val_if_fail (address_list, NULL);
304         
305         if (!address)
306                 return g_strdup (address_list);
307
308         email_address = get_email_from_address (address);
309         
310         /* search for substring */
311         if (!strstr ((const char *) address_list, (const char *) email_address)) {
312                 g_free (email_address);
313                 return g_strdup (address_list);
314         }
315
316         dup = g_strdup (address_list);
317         filtered_emails = g_string_new (NULL);
318         
319         token = strtok_r (dup, ",", &ptr);
320
321         while (token != NULL) {
322                 /* Add to list if not found */
323                 if (!strstr ((const char *) token, (const char *) email_address)) {
324                         if (filtered_emails->len == 0)
325                                 g_string_append_printf (filtered_emails, "%s", g_strstrip (token));
326                         else
327                                 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
328                 }
329                 token = strtok_r (NULL, ",", &ptr);
330         }
331         result = filtered_emails->str;
332
333         /* Clean */
334         g_free (email_address);
335         g_free (dup);
336         g_string_free (filtered_emails, FALSE);
337
338         return result;
339 }
340
341
342 gchar*
343 modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
344 {
345         GSList *addresses, *cursor;
346         GHashTable *table;
347         gchar *new_list;
348         
349         g_return_val_if_fail (address_list, NULL);
350
351         table = g_hash_table_new (g_str_hash, g_str_equal);
352         addresses = modest_text_utils_split_addresses_list (address_list);
353
354         new_list = g_strdup("");
355         cursor = addresses;
356         while (cursor) {
357                 const gchar* address = (const gchar*)cursor->data;
358
359                 /* ignore the address if already seen */
360                 if (g_hash_table_lookup (table, address) == 0) {
361                 
362                         gchar *tmp = g_strjoin (",", new_list, address, NULL);
363                         g_free (new_list);
364                         new_list = tmp;
365                         
366                         g_hash_table_insert (table, (gchar*)address, GINT_TO_POINTER(1));
367                 }
368                 cursor = g_slist_next (cursor);
369         }
370
371         g_hash_table_destroy (table);
372         g_slist_foreach (addresses, (GFunc)g_free, NULL);
373         g_slist_free (addresses);
374
375         return new_list;
376 }
377
378
379 static void
380 modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data, gssize n)
381 {
382         guint           i;
383         gboolean        space_seen = FALSE;
384         guint           break_dist = 0; /* distance since last break point */
385
386         if (n == -1)
387                 n = strlen (data);
388
389         /* replace with special html chars where needed*/
390         for (i = 0; i != n; ++i)  {
391                 char kar = data[i];
392                 
393                 if (space_seen && kar != ' ') {
394                         g_string_append_c (html, ' ');
395                         space_seen = FALSE;
396                 }
397                 
398                 /* we artificially insert a breakpoint (newline)
399                  * after 256, to make sure our lines are not so long
400                  * they will DOS the regexping later
401                  */
402                 if (++break_dist == 256) {
403                         g_string_append_c (html, '\n');
404                         break_dist = 0;
405                 }
406                 
407                 switch (kar) {
408                 case 0:
409                 case MARK_AMP:
410                 case MARK_AMP_URI:      
411                         /* this is a temp place holder for '&'; we can only
412                                 * set the real '&' after hyperlink translation, otherwise
413                                 * we might screw that up */
414                         break; /* ignore embedded \0s and MARK_AMP */   
415                 case '<'  : g_string_append (html, MARK_AMP_STR "lt;");   break;
416                 case '>'  : g_string_append (html, MARK_AMP_STR "gt;");   break;
417                 case '&'  : g_string_append (html, MARK_AMP_URI_STR "amp;");  break; /* special case */
418                 case '"'  : g_string_append (html, MARK_AMP_STR "quot;");  break;
419
420                 /* don't convert &apos; --> wpeditor will try to re-convert it... */    
421                 //case '\'' : g_string_append (html, "&apos;"); break;
422                 case '\n' : g_string_append (html, "<br>\n");break_dist= 0; break;
423                 case '\t' : g_string_append (html, MARK_AMP_STR "nbsp;" MARK_AMP_STR "nbsp;" MARK_AMP_STR "nbsp; ");
424                         break_dist=0; break; /* note the space at the end*/
425                 case ' ':
426                         break_dist = 0;
427                         if (space_seen) { /* second space in a row */
428                                 g_string_append (html, "&nbsp; ");
429                                 space_seen = FALSE;
430                         } else
431                                 space_seen = TRUE;
432                         break;
433                 default:
434                         g_string_append_c (html, kar);
435                 }
436         }
437 }
438
439
440 static void
441 modest_text_utils_convert_buffer_to_html_finish (GString *html)
442 {
443         int i;
444         /* replace all our MARK_AMPs with real ones */
445         for (i = 0; i != html->len; ++i)
446                 if ((html->str)[i] == MARK_AMP || (html->str)[i] == MARK_AMP_URI)
447                         (html->str)[i] = '&';
448 }
449
450
451 gchar*
452 modest_text_utils_convert_to_html (const gchar *data)
453 {
454         GString         *html;      
455         gsize           len;
456
457         g_return_val_if_fail (data, NULL);
458         
459         if (!data)
460                 return NULL;
461
462         len = strlen (data);
463         html = g_string_sized_new (1.5 * len);  /* just a  guess... */
464
465         g_string_append_printf (html,
466                                 "<html><head>"
467                                 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
468                                 "</head>"
469                                 "<body>");
470
471         modest_text_utils_convert_buffer_to_html_start (html, data, -1);
472         
473         g_string_append (html, "</body></html>");
474
475         if (len <= HYPERLINKIFY_MAX_LENGTH)
476                 hyperlinkify_plain_text (html);
477
478         modest_text_utils_convert_buffer_to_html_finish (html);
479         
480         return g_string_free (html, FALSE);
481 }
482
483 gchar *
484 modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hyperlinkify)
485 {
486         GString         *html;      
487
488         g_return_val_if_fail (data, NULL);
489
490         if (!data)
491                 return NULL;
492
493         if (n == -1) 
494                 n = strlen (data);
495         html = g_string_sized_new (1.5 * n);    /* just a  guess... */
496
497         modest_text_utils_convert_buffer_to_html_start (html, data, n);
498
499         if (hyperlinkify && (n < HYPERLINKIFY_MAX_LENGTH))
500                 hyperlinkify_plain_text (html);
501
502         modest_text_utils_convert_buffer_to_html_finish (html);
503         
504         return g_string_free (html, FALSE);
505 }
506
507 void
508 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
509 {
510         gchar *current, *start, *last_blank;
511         gint start_offset = 0, current_offset = 0;
512
513         g_return_if_fail (start_indexes != NULL);
514         g_return_if_fail (end_indexes != NULL);
515
516         start = (gchar *) addresses;
517         current = start;
518         last_blank = start;
519
520         while (*current != '\0') {
521                 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
522                         start = g_utf8_next_char (start);
523                         start_offset++;
524                         last_blank = current;
525                 } else if ((*current == ',')||(*current == ';')) {
526                         gint *start_index, *end_index;
527                         start_index = g_new0(gint, 1);
528                         end_index = g_new0(gint, 1);
529                         *start_index = start_offset;
530                         *end_index = current_offset;
531                         *start_indexes = g_slist_prepend (*start_indexes, start_index);
532                         *end_indexes = g_slist_prepend (*end_indexes, end_index);
533                         start = g_utf8_next_char (current);
534                         start_offset = current_offset + 1;
535                         last_blank = start;
536                 } else if (*current == '"') {
537                         current = g_utf8_next_char (current);
538                         current_offset ++;
539                         while ((*current != '"')&&(*current != '\0')) {
540                                 current = g_utf8_next_char (current);
541                                 current_offset ++;
542                         }
543                 }
544                                 
545                 current = g_utf8_next_char (current);
546                 current_offset ++;
547         }
548
549         if (start != current) {
550                         gint *start_index, *end_index;
551                         start_index = g_new0(gint, 1);
552                         end_index = g_new0(gint, 1);
553                         *start_index = start_offset;
554                         *end_index = current_offset;
555                         *start_indexes = g_slist_prepend (*start_indexes, start_index);
556                         *end_indexes = g_slist_prepend (*end_indexes, end_index);
557         }
558         
559         *start_indexes = g_slist_reverse (*start_indexes);
560         *end_indexes = g_slist_reverse (*end_indexes);
561
562         return;
563 }
564
565
566 GSList *
567 modest_text_utils_split_addresses_list (const gchar *addresses)
568 {
569         GSList *head;
570         const gchar *my_addrs = addresses;
571         const gchar *end;
572         gchar *addr;
573         
574         /* skip any space, ',', ';' at the start */
575         while (my_addrs && (my_addrs[0] == ' ' || my_addrs[0] == ',' || my_addrs[0] == ';'))
576                ++my_addrs;
577
578         /* are we at the end of addresses list? */
579         if (!my_addrs[0])
580                 return NULL;
581         
582         /* nope, we are at the start of some address
583          * now, let's find the end of the address */
584         end = my_addrs + 1;
585         while (end[0] && end[0] != ',' && end[0] != ';')
586                 ++end;
587
588         /* we got the address; copy it and remove trailing whitespace */
589         addr = g_strndup (my_addrs, end - my_addrs);
590         g_strchomp (addr);
591
592         head = g_slist_append (NULL, addr);
593         head->next = modest_text_utils_split_addresses_list (end); /* recurse */
594
595         return head;
596 }
597
598
599 void
600 modest_text_utils_address_range_at_position (const gchar *recipients_list,
601                                              guint position,
602                                              guint *start,
603                                              guint *end)
604 {
605         gchar *current = NULL;
606         gint range_start = 0;
607         gint range_end = 0;
608         gint index;
609         gboolean is_quoted = FALSE;
610
611         g_return_if_fail (recipients_list);
612         g_return_if_fail (position < g_utf8_strlen(recipients_list, -1));
613                 
614         index = 0;
615         for (current = (gchar *) recipients_list; *current != '\0';
616              current = g_utf8_find_next_char (current, NULL)) {
617                 gunichar c = g_utf8_get_char (current);
618
619                 if ((c == ',') && (!is_quoted)) {
620                         if (index < position) {
621                                 range_start = index + 1;
622                         } else {
623                                 break;
624                         }
625                 } else if (c == '\"') {
626                         is_quoted = !is_quoted;
627                 } else if ((c == ' ') &&(range_start == index)) {
628                         range_start ++;
629                 }
630                 index ++;
631                 range_end = index;
632         }
633
634         if (start)
635                 *start = range_start;
636         if (end)
637                 *end = range_end;
638 }
639
640
641 /* ******************************************************************* */
642 /* ************************* UTILIY FUNCTIONS ************************ */
643 /* ******************************************************************* */
644
645 static GString *
646 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
647 {
648         GString *gs;
649         const gchar *i0;
650         
651         if (iter > b + blen)
652                 return g_string_new("");
653         
654         i0 = iter;
655         while (iter[0]) {
656                 if (iter[0] == '\n')
657                         break;
658                 iter++;
659         }
660         gs = g_string_new_len (i0, iter - i0);
661         return gs;
662 }
663 static int
664 get_indent_level (const char *l)
665 {
666         int indent = 0;
667
668         while (l[0]) {
669                 if (l[0] == '>') {
670                         indent++;
671                         if (l[1] == ' ') {
672                                 l++;
673                         }
674                 } else {
675                         break;
676                 }
677                 l++;
678
679         }
680
681         /*      if we hit the signature marker "-- ", we return -(indent + 1). This
682          *      stops reformatting.
683          */
684         if (strcmp (l, "-- ") == 0) {
685                 return -1 - indent;
686         } else {
687                 return indent;
688         }
689 }
690
691 static void
692 unquote_line (GString * l)
693 {
694         gchar *p;
695
696         p = l->str;
697         while (p[0]) {
698                 if (p[0] == '>') {
699                         if (p[1] == ' ') {
700                                 p++;
701                         }
702                 } else {
703                         break;
704                 }
705                 p++;
706         }
707         g_string_erase (l, 0, p - l->str);
708 }
709
710 static void
711 append_quoted (GString * buf, int indent, const GString * str,
712                const int cutpoint)
713 {
714         int i;
715
716         indent = indent < 0 ? abs (indent) - 1 : indent;
717         for (i = 0; i <= indent; i++) {
718                 g_string_append (buf, "> ");
719         }
720         if (cutpoint > 0) {
721                 g_string_append_len (buf, str->str, cutpoint);
722         } else {
723                 g_string_append (buf, str->str);
724         }
725         g_string_append (buf, "\n");
726 }
727
728 static int
729 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
730 {
731         gint index = 0;
732         const gchar *pos, *last;
733         gunichar *uni;
734
735         indent = indent < 0 ? abs (indent) - 1 : indent;
736
737         last = NULL;
738         pos = s;
739         uni = g_utf8_to_ucs4_fast (s, -1, NULL);
740         while (pos[0]) {
741                 if ((index + 2 * indent > limit) && last) {
742                         g_free (uni);
743                         return last - s;
744                 }
745                 if (g_unichar_isspace (uni[index])) {
746                         last = pos;
747                 }
748                 pos = g_utf8_next_char (pos);
749                 index++;
750         }
751         g_free (uni);
752         return strlen (s);
753 }
754
755 static int
756 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
757 {
758         gint i, last;
759
760         last = strlen (s);
761         if (last + 2 * indent < limit)
762                 return last;
763
764         for (i = strlen (s); i > 0; i--) {
765                 if (s[i] == ' ') {
766                         if (i + 2 * indent <= limit) {
767                                 return i;
768                         } else {
769                                 last = i;
770                         }
771                 }
772         }
773         return last;
774 }
775
776 static int
777 get_breakpoint (const gchar * s, const gint indent, const gint limit)
778 {
779
780         if (g_utf8_validate (s, -1, NULL)) {
781                 return get_breakpoint_utf8 (s, indent, limit);
782         } else {                /* assume ASCII */
783                 //g_warning("invalid UTF-8 in msg");
784                 return get_breakpoint_ascii (s, indent, limit);
785         }
786 }
787
788 static gchar *
789 cite (const time_t sent_date, const gchar *from)
790 {
791         return g_strdup (_("mcen_ia_editor_original_message"));
792 }
793
794 static gchar *
795 quoted_attachments (GList *attachments)
796 {
797         GList *node = NULL;
798         GString *result = g_string_new ("");
799         for (node = attachments; node != NULL; node = g_list_next (node)) {
800                 gchar *filename = (gchar *) node->data;
801                 g_string_append_printf ( result, "%s %s\n", _("mcen_ia_editor_attach_filename"), filename);
802         }
803
804         return g_string_free (result, FALSE);
805
806 }
807
808 static gchar *
809 modest_text_utils_quote_plain_text (const gchar *text, 
810                                     const gchar *cite, 
811                                     const gchar *signature,
812                                     GList *attachments,
813                                     int limit)
814 {
815         const gchar *iter;
816         gint indent, breakpoint, rem_indent = 0;
817         GString *q, *l, *remaining;
818         gsize len;
819         gchar *attachments_string = NULL;
820
821         /* remaining will store the rest of the line if we have to break it */
822         q = g_string_new ("\n");
823         q = g_string_append (q, cite);
824         q = g_string_append_c (q, '\n');
825         remaining = g_string_new ("");
826
827         iter = text;
828         len = strlen(text);
829         do {
830                 l = get_next_line (text, len, iter);
831                 iter = iter + l->len + 1;
832                 indent = get_indent_level (l->str);
833                 unquote_line (l);
834
835                 if (remaining->len) {
836                         if (l->len && indent == rem_indent) {
837                                 g_string_prepend (l, " ");
838                                 g_string_prepend (l, remaining->str);
839                         } else {
840                                 do {
841                                         breakpoint =
842                                                 get_breakpoint (remaining->str,
843                                                                 rem_indent,
844                                                                 limit);
845                                         append_quoted (q, rem_indent,
846                                                        remaining, breakpoint);
847                                         g_string_erase (remaining, 0,
848                                                         breakpoint);
849                                         if (remaining->str[0] == ' ') {
850                                                 g_string_erase (remaining, 0,
851                                                                 1);
852                                         }
853                                 } while (remaining->len);
854                         }
855                 }
856                 g_string_free (remaining, TRUE);
857                 breakpoint = get_breakpoint (l->str, indent, limit);
858                 remaining = g_string_new (l->str + breakpoint);
859                 if (remaining->str[0] == ' ') {
860                         g_string_erase (remaining, 0, 1);
861                 }
862                 rem_indent = indent;
863                 append_quoted (q, indent, l, breakpoint);
864                 g_string_free (l, TRUE);
865         } while ((iter < text + len) || (remaining->str[0]));
866
867         attachments_string = quoted_attachments (attachments);
868         q = g_string_append (q, attachments_string);
869         g_free (attachments_string);
870
871         if (signature != NULL) {
872                 q = g_string_append_c (q, '\n');
873                 q = g_string_append (q, signature);
874         }
875
876         return g_string_free (q, FALSE);
877 }
878
879 static gchar*
880 modest_text_utils_quote_html (const gchar *text, 
881                               const gchar *cite, 
882                               const gchar *signature,
883                               GList *attachments,
884                               int limit)
885 {
886         gchar *result = NULL;
887         gchar *signature_result = NULL;
888         const gchar *format = \
889                 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" \
890                 "<html>\n" \
891                 "<body>\n" \
892                 "<br/>%s<br/>" \
893                 "<pre>%s<br/>%s<br/>%s</pre>\n" \
894                 "</body>\n" \
895                 "</html>\n";
896         gchar *attachments_string = NULL;
897         gchar *q_attachments_string = NULL;
898         gchar *q_cite = NULL;
899         gchar *html_text = NULL;
900
901         if (signature == NULL)
902                 signature_result = g_strdup ("");
903         else
904                 signature_result = modest_text_utils_convert_to_html_body (signature, -1, TRUE);
905
906         attachments_string = quoted_attachments (attachments);
907         q_attachments_string = modest_text_utils_convert_to_html_body (attachments_string, -1, TRUE);
908         q_cite = modest_text_utils_convert_to_html_body (cite, -1, TRUE);
909         html_text = modest_text_utils_convert_to_html_body (text, -1, TRUE);
910         result = g_strdup_printf (format, signature_result, q_cite, html_text, q_attachments_string);
911         g_free (q_cite);
912         g_free (html_text);
913         g_free (attachments_string);
914         g_free (q_attachments_string);
915         g_free (signature_result);
916         
917         return result;
918 }
919
920 static gint 
921 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
922 {
923         return match2->offset - match1->offset;
924 }
925
926 static gboolean url_matches_block = 0;
927 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
928
929
930 static gboolean
931 compile_patterns ()
932 {
933         guint i;
934         const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
935         for (i = 0; i != pattern_num; ++i) {
936                 patterns[i].preg = g_slice_new0 (regex_t);
937                 
938                 /* this should not happen */
939                 if (regcomp (patterns[i].preg, patterns[i].regex,
940                              REG_ICASE|REG_EXTENDED|REG_NEWLINE) != 0) {
941                         g_warning ("%s: error in regexp:\n%s\n", __FUNCTION__, patterns[i].regex);
942                         return FALSE;
943                 }
944         }
945         return TRUE;
946 }
947
948 static void 
949 free_patterns ()
950 {
951         guint i;
952         const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
953         for (i = 0; i != pattern_num; ++i) {
954                 regfree (patterns[i].preg);
955                 g_slice_free  (regex_t, patterns[i].preg);
956         } /* don't free patterns itself -- it's static */
957 }
958
959 void
960 modest_text_utils_hyperlinkify_begin (void)
961 {
962         if (url_matches_block == 0)
963                 compile_patterns ();
964         url_matches_block ++;
965 }
966
967 void
968 modest_text_utils_hyperlinkify_end (void)
969 {
970         url_matches_block--;
971         if (url_matches_block <= 0)
972                 free_patterns ();
973 }
974
975
976 static GSList*
977 get_url_matches (GString *txt)
978 {
979         regmatch_t rm;
980         guint rv, i, offset = 0;
981         GSList *match_list = NULL;
982
983         const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
984
985         /* initalize the regexps */
986         modest_text_utils_hyperlinkify_begin ();
987
988         /* find all the matches */
989         for (i = 0; i != pattern_num; ++i) {
990                 offset     = 0; 
991                 while (1) {
992                         url_match_t *match;
993                         gboolean is_submatch;
994                         GSList *cursor;
995                         
996                         if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
997                                 g_return_val_if_fail (rv == REG_NOMATCH, NULL); /* this should not happen */
998                                 break; /* try next regexp */ 
999                         }
1000                         if (rm.rm_so == -1)
1001                                 break;
1002                         
1003                         is_submatch = FALSE;
1004                         /* check  old matches to see if this has already been matched */
1005                         cursor = match_list;
1006                         while (cursor && !is_submatch) {
1007                                 const url_match_t *old_match =
1008                                         (const url_match_t *) cursor->data;
1009                                 guint new_offset = offset + rm.rm_so;
1010                                 is_submatch = (new_offset >  old_match->offset &&
1011                                                new_offset <  old_match->offset + old_match->len);
1012                                 cursor = g_slist_next (cursor);
1013                         }
1014
1015                         if (!is_submatch) {
1016                                 /* make a list of our matches (<offset, len, prefix> tupels)*/
1017                                 match = g_slice_new (url_match_t);
1018                                 match->offset = offset + rm.rm_so;
1019                                 match->len    = rm.rm_eo - rm.rm_so;
1020                                 match->prefix = patterns[i].prefix;
1021                                 match_list = g_slist_prepend (match_list, match);
1022                         }               
1023                         offset += rm.rm_eo;
1024                 }
1025         }
1026
1027         modest_text_utils_hyperlinkify_end ();
1028         
1029         /* now sort the list, so the matches are in reverse order of occurence.
1030          * that way, we can do the replacements starting from the end, so we don't need
1031          * to recalculate the offsets
1032          */
1033         match_list = g_slist_sort (match_list,
1034                                    (GCompareFunc)cmp_offsets_reverse); 
1035         return match_list;      
1036 }
1037
1038
1039
1040 /* replace all occurences of needle in haystack with repl*/
1041 static gchar*
1042 replace_string (const gchar *haystack, const gchar *needle, gchar repl)
1043 {
1044         gchar *str, *cursor;
1045
1046         if (!haystack || !needle || strlen(needle) == 0)
1047                 return haystack ? g_strdup(haystack) : NULL;
1048         
1049         str = g_strdup (haystack);
1050
1051         for (cursor = str; cursor && *cursor; ++cursor) {
1052                 if (g_str_has_prefix (cursor, needle)) {
1053                         cursor[0] = repl;
1054                         memmove (cursor + 1,
1055                                  cursor + strlen (needle),
1056                                  strlen (cursor + strlen (needle)) + 1);
1057                 }
1058         }
1059         
1060         return str;
1061 }
1062
1063 static void
1064 hyperlinkify_plain_text (GString *txt)
1065 {
1066         GSList *cursor;
1067         GSList *match_list = get_url_matches (txt);
1068
1069         /* we will work backwards, so the offsets stay valid */
1070         for (cursor = match_list; cursor; cursor = cursor->next) {
1071
1072                 url_match_t *match = (url_match_t*) cursor->data;
1073                 gchar *url  = g_strndup (txt->str + match->offset, match->len);
1074                 gchar *repl = NULL; /* replacement  */
1075
1076                 /* the string still contains $(MARK_AMP_URI_STR)"amp;" for each
1077                  * '&' in the original, because of the text->html conversion.
1078                  * in the href-URL (and only there), we must convert that back to
1079                  * '&'
1080                  */
1081                 gchar *href_url = replace_string (url, MARK_AMP_URI_STR "amp;", '&');
1082                 
1083                 /* the prefix is NULL: use the one that is already there */
1084                 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
1085                                         match->prefix ? match->prefix : EMPTY_STRING, 
1086                                         href_url, url);
1087
1088                 /* replace the old thing with our hyperlink
1089                  * replacement thing */
1090                 g_string_erase  (txt, match->offset, match->len);
1091                 g_string_insert (txt, match->offset, repl);
1092                 
1093                 g_free (url);
1094                 g_free (repl);
1095                 g_free (href_url);
1096
1097                 g_slice_free (url_match_t, match);      
1098         }
1099         
1100         g_slist_free (match_list);
1101 }
1102
1103
1104 /* for optimization reasons, we change the string in-place */
1105 void
1106 modest_text_utils_get_display_address (gchar *address)
1107 {
1108         int i;
1109
1110         g_return_if_fail (address);
1111         
1112         if (!address)
1113                 return;
1114         
1115         /* should not be needed, and otherwise, we probably won't screw up the address
1116          * more than it already is :) 
1117          * g_return_val_if_fail (g_utf8_validate (address, -1, NULL), NULL);
1118          * */
1119         
1120         /* remove leading whitespace */
1121         if (address[0] == ' ')
1122                 g_strchug (address);
1123                 
1124         for (i = 0; address[i]; ++i) {
1125                 if (address[i] == '<') {
1126                         if (G_UNLIKELY(i == 0))
1127                                 return; /* there's nothing else, leave it */
1128                         else {
1129                                 address[i] = '\0'; /* terminate the string here */
1130                                 return;
1131                         }
1132                 }
1133         }
1134 }
1135
1136
1137
1138
1139
1140 gchar *
1141 modest_text_utils_get_email_address (const gchar *full_address)
1142 {
1143         const gchar *left, *right;
1144
1145         g_return_val_if_fail (full_address, NULL);
1146         
1147         if (!full_address)
1148                 return NULL;
1149         
1150         g_return_val_if_fail (g_utf8_validate (full_address, -1, NULL), NULL);
1151         
1152         left = g_strrstr_len (full_address, strlen(full_address), "<");
1153         if (left == NULL)
1154                 return g_strdup (full_address);
1155
1156         right = g_strstr_len (left, strlen(left), ">");
1157         if (right == NULL)
1158                 return g_strdup (full_address);
1159
1160         return g_strndup (left + 1, right - left - 1);
1161 }
1162
1163 gint 
1164 modest_text_utils_get_subject_prefix_len (const gchar *sub)
1165 {
1166         gint prefix_len = 0;    
1167
1168         g_return_val_if_fail (sub, 0);
1169
1170         if (!sub)
1171                 return 0;
1172         
1173         /* optimization: "Re", "RE", "re","Fwd", "FWD", "fwd","FW","Fw", "fw" */
1174         if (sub[0] != 'R' && sub[0] != 'F' && sub[0] != 'r' && sub[0] != 'f')
1175                 return 0;
1176         else if (sub[0] && sub[1] != 'e' && sub[1] != 'E' && sub[1] != 'w' && sub[1] != 'W')
1177                 return 0;
1178
1179         prefix_len = 2;
1180         if (sub[2] == 'd')
1181                 ++prefix_len;
1182
1183         /* skip over a [...] block */
1184         if (sub[prefix_len] == '[') {
1185                 int c = prefix_len + 1;
1186                 while (sub[c] && sub[c] != ']')
1187                         ++c;
1188                 if (sub[c])
1189                         return 0; /* no end to the ']' found */
1190                 else
1191                         prefix_len = c + 1;
1192         }
1193
1194         /* did we find the ':' ? */
1195         if (sub[prefix_len] == ':') {
1196                 ++prefix_len;
1197                 if (sub[prefix_len] == ' ')
1198                         ++prefix_len;
1199                 prefix_len += modest_text_utils_get_subject_prefix_len (sub + prefix_len);
1200 /*              g_warning ("['%s','%s']", sub, (char*) sub + prefix_len); */
1201                 return prefix_len;
1202         } else
1203                 return 0;
1204 }
1205
1206
1207 gint
1208 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
1209 {
1210
1211 /* work even when s1 and/or s2 == NULL */
1212         if (G_UNLIKELY(s1 == s2))
1213                 return 0;
1214         if (G_UNLIKELY(!s1))
1215                 return -1;
1216         if (G_UNLIKELY(!s2))
1217                 return 1;
1218         
1219         /* if it's not case sensitive */
1220         if (!insensitive) {
1221
1222                 /* optimization: short cut if first char is ascii */ 
1223                 if (((s1[0] & 0xf0)== 0) && ((s2[0] & 0xf0) == 0)) 
1224                         return s1[0] - s2[0];
1225                 
1226                 return g_utf8_collate (s1, s2);
1227
1228         } else {
1229                 gint result;
1230                 gchar *n1, *n2;
1231
1232                 /* optimization: short cut iif first char is ascii */ 
1233                 if (((s1[0] & 0xf0) == 0) && ((s2[0] & 0xf0) == 0)) 
1234                         return tolower(s1[0]) - tolower(s2[0]);
1235                 
1236                 n1 = g_utf8_strdown (s1, -1);
1237                 n2 = g_utf8_strdown (s2, -1);
1238                 
1239                 result = g_utf8_collate (n1, n2);
1240                 
1241                 g_free (n1);
1242                 g_free (n2);
1243         
1244                 return result;
1245         }
1246 }
1247
1248
1249 const gchar*
1250 modest_text_utils_get_display_date (time_t date)
1251 {
1252 #define DATE_BUF_SIZE 64 
1253         static gchar date_buf[DATE_BUF_SIZE];
1254         
1255         /* calculate the # of days since epoch for 
1256          * for today and for the date provided 
1257          * based on idea from pvanhoof */
1258         int day      = time(NULL) / (24 * 60 * 60);
1259         int date_day = date       / (24 * 60 * 60);
1260
1261         /* if it's today, show the time, if it's not today, show the date instead */
1262
1263         if (day == date_day) /* is the date today? */
1264                 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%X", date);
1265         else 
1266                 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%x", date); 
1267
1268         return date_buf; /* this is a static buffer, don't free! */
1269 }
1270
1271
1272
1273 gboolean
1274 modest_text_utils_validate_folder_name (const gchar *folder_name)
1275 {
1276         /* based on http://msdn2.microsoft.com/en-us/library/aa365247.aspx,
1277          * with some extras */
1278         
1279         guint len;
1280         gint i;
1281         const gchar **cursor = NULL;
1282         const gchar *forbidden_names[] = { /* windows does not like these */
1283                 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
1284                 "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
1285                 ".", "..", NULL
1286         };
1287         
1288         /* cannot be NULL */
1289         if (!folder_name) 
1290                 return FALSE;
1291
1292         /* cannot be empty */
1293         len = strlen(folder_name);
1294         if (len == 0)
1295                 return FALSE;
1296         
1297         /* cannot start or end with a space */
1298         if (g_ascii_isspace(folder_name[0]) || g_ascii_isspace(folder_name[len - 1]))
1299                 return FALSE; 
1300
1301         /* cannot contain a forbidden char */   
1302         for (i = 0; i < len; i++)
1303                 if (modest_text_utils_is_forbidden_char (folder_name[i], FOLDER_NAME_FORBIDDEN_CHARS))
1304                         return FALSE;
1305         
1306         /* cannot contain a forbidden word */
1307         if (len <= 4) {
1308                 for (cursor = forbidden_names; cursor && *cursor; ++cursor) {
1309                         if (g_ascii_strcasecmp (folder_name, *cursor) == 0)
1310                                 return FALSE;
1311                 }
1312         }
1313
1314         return TRUE; /* it's valid! */
1315 }
1316
1317
1318
1319 gboolean
1320 modest_text_utils_validate_domain_name (const gchar *domain)
1321 {
1322         gboolean valid = FALSE;
1323         regex_t rx;
1324         const gchar* domain_regex = "^([a-z0-9-]*[a-z0-9]\\.)+[a-z0-9-]*[a-z0-9]$";
1325
1326         g_return_val_if_fail (domain, FALSE);
1327         
1328         if (!domain)
1329                 return FALSE;
1330         
1331         memset (&rx, 0, sizeof(regex_t)); /* coverity wants this... */
1332                 
1333         /* domain name: all alphanum or '-' or '.',
1334          * but beginning/ending in alphanum */  
1335         if (regcomp (&rx, domain_regex, REG_ICASE|REG_EXTENDED|REG_NOSUB)) {
1336                 g_warning ("BUG: error in regexp");
1337                 return FALSE;
1338         }
1339         
1340         valid = (regexec (&rx, domain, 1, NULL, 0) == 0);
1341         regfree (&rx);
1342                 
1343         return valid;
1344 }
1345
1346
1347
1348 gboolean
1349 modest_text_utils_validate_email_address (const gchar *email_address,
1350                                           const gchar **invalid_char_position)
1351 {
1352         int count = 0;
1353         const gchar *c = NULL, *domain = NULL;
1354         static gchar *rfc822_specials = "()<>@,;:\\\"[]&";
1355         
1356         if (invalid_char_position)
1357                 *invalid_char_position = NULL;
1358         
1359         g_return_val_if_fail (email_address, FALSE);
1360         
1361         /* check that the email adress contains exactly one @ */
1362         if (!strstr(email_address, "@") || 
1363                         (strstr(email_address, "@") != g_strrstr(email_address, "@"))) 
1364                 return FALSE;
1365         
1366         /* first we validate the name portion (name@domain) */
1367         for (c = email_address;  *c;  c++) {
1368                 if (*c == '\"' && 
1369                     (c == email_address || 
1370                      *(c - 1) == '.' || 
1371                      *(c - 1) == '\"')) {
1372                         while (*++c) {
1373                                 if (*c == '\"') 
1374                                         break;
1375                                 if (*c == '\\' && (*++c == ' ')) 
1376                                         continue;
1377                                 if (*c <= ' ' || *c >= 127) 
1378                                         return FALSE;
1379                         }
1380                         if (!*c++) 
1381                                 return FALSE;
1382                         if (*c == '@') 
1383                                 break;
1384                         if (*c != '.') 
1385                                 return FALSE;
1386                         continue;
1387                 }
1388                 if (*c == '@') 
1389                         break;
1390                 if (*c <= ' ' || *c >= 127) 
1391                         return FALSE;
1392                 if (strchr(rfc822_specials, *c)) {
1393                         if (invalid_char_position)
1394                                 *invalid_char_position = c;
1395                         return FALSE;
1396                 }
1397         }
1398         if (c == email_address || *(c - 1) == '.') 
1399                 return FALSE;
1400
1401         /* next we validate the domain portion (name@domain) */
1402         if (!*(domain = ++c)) 
1403                 return FALSE;
1404         do {
1405                 if (*c == '.') {
1406                         if (c == domain || *(c - 1) == '.' || *(c + 1) == '\0') 
1407                                 return FALSE;
1408                         count++;
1409                 }
1410                 if (*c <= ' ' || *c >= 127) 
1411                         return FALSE;
1412                 if (strchr(rfc822_specials, *c)) {
1413                         if (invalid_char_position)
1414                                 *invalid_char_position = c;
1415                         return FALSE;
1416                 }
1417         } while (*++c);
1418
1419         return (count >= 1) ? TRUE : FALSE;
1420 }
1421
1422 gboolean 
1423 modest_text_utils_validate_recipient (const gchar *recipient, const gchar **invalid_char_position)
1424 {
1425         gchar *stripped, *current;
1426         gchar *right_part;
1427         gboolean has_error = FALSE;
1428
1429         if (invalid_char_position)
1430                 *invalid_char_position = NULL;
1431         
1432         g_return_val_if_fail (recipient, FALSE);
1433         
1434         if (modest_text_utils_validate_email_address (recipient, invalid_char_position))
1435                 return TRUE;
1436
1437         stripped = g_strdup (recipient);
1438         stripped = g_strstrip (stripped);
1439         current = stripped;
1440
1441         if (*current == '\0') {
1442                 g_free (stripped);
1443                 return FALSE;
1444         }
1445
1446         /* quoted string */
1447         if (*current == '\"') {
1448                 current = g_utf8_next_char (current);
1449                 has_error = TRUE;
1450                 for (; *current != '\0'; current = g_utf8_next_char (current)) {
1451                         if (*current == '\\') {
1452                                 /* TODO: This causes a warning, which breaks the build, 
1453                                  * because a gchar cannot be < 0.
1454                                  * murrayc. 
1455                                 if (current[1] <0) {
1456                                         has_error = TRUE;
1457                                         break;
1458                                 }
1459                                 */
1460                         } else if (*current == '\"') {
1461                                 has_error = FALSE;
1462                                 current = g_utf8_next_char (current);
1463                                 break;
1464                         }
1465                 }
1466         } else {
1467                 has_error = TRUE;
1468                 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1469                         if (*current == '<') {
1470                                 has_error = FALSE;
1471                                 break;
1472                         }
1473                 }
1474         }
1475                 
1476         if (has_error) {
1477                 g_free (stripped);
1478                 return FALSE;
1479         }
1480
1481         right_part = g_strdup (current);
1482         g_free (stripped);
1483         right_part = g_strstrip (right_part);
1484
1485         if (g_str_has_prefix (right_part, "<") &&
1486             g_str_has_suffix (right_part, ">")) {
1487                 gchar *address;
1488                 gboolean valid;
1489
1490                 address = g_strndup (right_part+1, strlen (right_part) - 2);
1491                 g_free (right_part);
1492                 valid = modest_text_utils_validate_email_address (address, invalid_char_position);
1493                 g_free (address);
1494                 return valid;
1495         } else {
1496                 g_free (right_part);
1497                 return FALSE;
1498         }
1499 }
1500
1501
1502 gchar *
1503 modest_text_utils_get_display_size (guint64 size)
1504 {
1505         const guint KB=1024;
1506         const guint MB=1024 * KB;
1507         const guint GB=1024 * MB;
1508
1509         if (size == 0)
1510                 return g_strdup_printf(_FM("sfil_li_size_kb"), 0);
1511         if (0 < size && size < KB)
1512                 return g_strdup_printf (_FM("sfil_li_size_kb"), 1);
1513         else if (KB <= size && size < 100 * KB)
1514                 return g_strdup_printf (_FM("sfil_li_size_1kb_99kb"), size / KB);
1515         else if (100*KB <= size && size < MB)
1516                 return g_strdup_printf (_FM("sfil_li_size_100kb_1mb"), (float) size / MB);
1517         else if (MB <= size && size < 10*MB)
1518                 return g_strdup_printf (_FM("sfil_li_size_1mb_10mb"), (float) size / MB);
1519         else if (10*MB <= size && size < GB)
1520                 return g_strdup_printf (_FM("sfil_li_size_10mb_1gb"), size / MB);
1521         else
1522                 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB); 
1523 }
1524
1525 static gchar *
1526 get_email_from_address (const gchar * address)
1527 {
1528         gchar *left_limit, *right_limit;
1529
1530         left_limit = strstr (address, "<");
1531         right_limit = g_strrstr (address, ">");
1532
1533         if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1534                 return g_strdup (address);
1535         else
1536                 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);
1537 }
1538
1539 gchar *      
1540 modest_text_utils_get_color_string (GdkColor *color)
1541 {
1542         g_return_val_if_fail (color, NULL);
1543         
1544         return g_strdup_printf ("#%x%x%x%x%x%x%x%x%x%x%x%x",
1545                                 (color->red >> 12)   & 0xf, (color->red >> 8)   & 0xf,
1546                                 (color->red >>  4)   & 0xf, (color->red)        & 0xf,
1547                                 (color->green >> 12) & 0xf, (color->green >> 8) & 0xf,
1548                                 (color->green >>  4) & 0xf, (color->green)      & 0xf,
1549                                 (color->blue >> 12)  & 0xf, (color->blue >> 8)  & 0xf,
1550                                 (color->blue >>  4)  & 0xf, (color->blue)       & 0xf);
1551 }
1552
1553 gchar *
1554 modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer)
1555 {
1556         GtkTextIter start, end;
1557         gchar *slice, *current;
1558         GString *result = g_string_new ("");
1559
1560         g_return_val_if_fail (buffer && GTK_IS_TEXT_BUFFER (buffer), NULL);
1561         
1562         gtk_text_buffer_get_start_iter (buffer, &start);
1563         gtk_text_buffer_get_end_iter (buffer, &end);
1564
1565         slice = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
1566         current = slice;
1567
1568         while (current && current != '\0') {
1569                 if (g_utf8_get_char (current) == 0xFFFC) {
1570                         result = g_string_append_c (result, ' ');
1571                         current = g_utf8_next_char (current);
1572                 } else {
1573                         gchar *next = g_utf8_strchr (current, -1, 0xFFFC);
1574                         if (next == NULL) {
1575                                 result = g_string_append (result, current);
1576                         } else {
1577                                 result = g_string_append_len (result, current, next - current);
1578                         }
1579                         current = next;
1580                 }
1581         }
1582         g_free (slice);
1583
1584         return g_string_free (result, FALSE);
1585         
1586 }
1587
1588 gboolean
1589 modest_text_utils_is_forbidden_char (const gchar character,
1590                                      ModestTextUtilsForbiddenCharType type)
1591 {
1592         gint i, len;
1593         const gchar *forbidden_chars = NULL;
1594         
1595         /* We need to get the length in the switch because the
1596            compiler needs to know the size at compile time */
1597         switch (type) {
1598         case ACCOUNT_TITLE_FORBIDDEN_CHARS:
1599                 forbidden_chars = account_title_forbidden_chars;
1600                 len = G_N_ELEMENTS (account_title_forbidden_chars);
1601                 break;
1602         case FOLDER_NAME_FORBIDDEN_CHARS:
1603                 forbidden_chars = folder_name_forbidden_chars;
1604                 len = G_N_ELEMENTS (folder_name_forbidden_chars);
1605                 break;
1606         case USER_NAME_FORBIDDEN_NAMES:
1607                 forbidden_chars = user_name_forbidden_chars;
1608                 len = G_N_ELEMENTS (user_name_forbidden_chars);
1609                 break;
1610         default:
1611                 g_return_val_if_reached (TRUE);
1612         }
1613
1614         for (i = 0; i < len ; i++)
1615                 if (forbidden_chars[i] == character)
1616                         return TRUE;
1617
1618         return FALSE; /* it's valid! */
1619 }