1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
34 #endif /*_GNU_SOURCE*/
35 #include <string.h> /* for strcasestr */
40 #include <glib/gi18n.h>
42 #include <modest-tny-platform-factory.h>
43 #include <modest-text-utils.h>
44 #include <modest-runtime.h>
49 #endif /*HAVE_CONFIG_H */
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 ""
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,
65 #define HYPERLINKIFY_MAX_LENGTH (1024*50)
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
74 #define MARK_AMP '\007'
75 #define MARK_AMP_STR "\007"
77 /* mark & separately, because they are parts of urls.
78 * ie. a&b => 1\008amp;b
80 #define MARK_AMP_URI '\006'
81 #define MARK_AMP_URI_STR "\006"
85 * we need these regexps to find URLs in plain text e-mails
87 typedef struct _url_match_pattern_t url_match_pattern_t;
88 struct _url_match_pattern_t {
94 typedef struct _url_match_t url_match_t;
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 "=?/~#]",\
105 { "www\\.[-a-z0-9_$.+!*(),;:@%=?/~#" MARK_AMP_URI_STR "]+[-a-z0-9_$%" MARK_AMP_URI_STR "=?/~#]",\
107 { "ftp\\.[-a-z0-9_$.+!*(),;:@%=?/~#" MARK_AMP_URI_STR "]+[-a-z0-9_$%" MARK_AMP_URI_STR "=?/~#]",\
109 { "(jabberto|voipto|sipto|sip|chatto|xmpp):[-_a-z@0-9.+]+", \
111 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
113 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
117 const gchar account_title_forbidden_chars[] = {
118 '\\', '/', ':', '*', '?', '\'', '<', '>', '|', '^'
120 const gchar folder_name_forbidden_chars[] = {
121 '<', '>', ':', '\'', '/', '\\', '|', '?', '*', '^', '%', '$', '#', '&'
123 const gchar user_name_forbidden_chars[] = {
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);
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);
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,
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);
145 static gchar* modest_text_utils_quote_plain_text (const gchar *text,
147 const gchar *signature,
151 static gchar* modest_text_utils_quote_html (const gchar *text,
153 const gchar *signature,
156 static gchar* get_email_from_address (const gchar *address);
159 /* ******************************************************************* */
160 /* ************************* PUBLIC FUNCTIONS ************************ */
161 /* ******************************************************************* */
164 modest_text_utils_quote (const gchar *text,
165 const gchar *content_type,
166 const gchar *signature,
168 const time_t sent_date,
172 gchar *retval, *cited;
174 g_return_val_if_fail (text, NULL);
175 g_return_val_if_fail (content_type, NULL);
177 cited = cite (sent_date, from);
179 if (content_type && strcmp (content_type, "text/html") == 0)
180 /* TODO: extract the <body> of the HTML and pass it to
182 retval = modest_text_utils_quote_html (text, cited, signature, attachments, limit);
184 retval = modest_text_utils_quote_plain_text (text, cited, signature, attachments, limit);
193 modest_text_utils_cite (const gchar *text,
194 const gchar *content_type,
195 const gchar *signature,
202 g_return_val_if_fail (text, NULL);
203 g_return_val_if_fail (content_type, NULL);
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);
212 retval = g_strconcat (text, "\n", signature, NULL);
219 forward_cite (const gchar *from,
222 const gchar *subject)
224 return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n",
226 FROM_STRING, (from)?from:"",
228 TO_STRING, (to)?to:"",
229 SUBJECT_STRING, (subject)?subject:"");
233 modest_text_utils_inline (const gchar *text,
234 const gchar *content_type,
235 const gchar *signature,
239 const gchar *subject)
245 g_return_val_if_fail (text, NULL);
246 g_return_val_if_fail (content_type, NULL);
248 modest_text_utils_strftime (sent_str, 100, "%c", sent_date);
250 cited = forward_cite (from, sent_str, to, subject);
252 if (content_type && strcmp (content_type, "text/html") == 0)
253 retval = modest_text_utils_quote_html (text, cited, signature, NULL, 80);
255 retval = modest_text_utils_quote_plain_text (text, cited, signature, NULL, 80);
261 /* just to prevent warnings:
262 * warning: `%x' yields only last 2 digits of year in some locales
265 modest_text_utils_strftime(char *s, gsize max, const char *fmt, time_t timet)
269 /* does not work on old maemo glib:
270 * g_date_set_time_t (&date, timet);
272 localtime_r (&timet, &tm);
273 return strftime(s, max, fmt, &tm);
277 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
281 g_return_val_if_fail (prefix, NULL);
283 if (!subject || subject[0] == '\0')
284 subject = _("mail_va_no_subject");
286 tmp = g_strchug (g_strdup (subject));
288 if (!strncmp (tmp, prefix, strlen (prefix))) {
292 return g_strdup_printf ("%s %s", prefix, subject);
297 modest_text_utils_remove_address (const gchar *address_list, const gchar *address)
299 gchar *dup, *token, *ptr = NULL, *result;
300 GString *filtered_emails;
301 gchar *email_address;
303 g_return_val_if_fail (address_list, NULL);
306 return g_strdup (address_list);
308 email_address = get_email_from_address (address);
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);
316 dup = g_strdup (address_list);
317 filtered_emails = g_string_new (NULL);
319 token = strtok_r (dup, ",", &ptr);
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));
327 g_string_append_printf (filtered_emails, ",%s", g_strstrip (token));
329 token = strtok_r (NULL, ",", &ptr);
331 result = filtered_emails->str;
334 g_free (email_address);
336 g_string_free (filtered_emails, FALSE);
343 modest_text_utils_remove_duplicate_addresses (const gchar *address_list)
345 GSList *addresses, *cursor;
349 g_return_val_if_fail (address_list, NULL);
351 table = g_hash_table_new (g_str_hash, g_str_equal);
352 addresses = modest_text_utils_split_addresses_list (address_list);
354 new_list = g_strdup("");
357 const gchar* address = (const gchar*)cursor->data;
359 /* ignore the address if already seen */
360 if (g_hash_table_lookup (table, address) == 0) {
362 gchar *tmp = g_strjoin (",", new_list, address, NULL);
366 g_hash_table_insert (table, (gchar*)address, GINT_TO_POINTER(1));
368 cursor = g_slist_next (cursor);
371 g_hash_table_destroy (table);
372 g_slist_foreach (addresses, (GFunc)g_free, NULL);
373 g_slist_free (addresses);
380 modest_text_utils_convert_buffer_to_html_start (GString *html, const gchar *data, gssize n)
383 gboolean space_seen = FALSE;
384 guint break_dist = 0; /* distance since last break point */
389 /* replace with special html chars where needed*/
390 for (i = 0; i != n; ++i) {
393 if (space_seen && kar != ' ') {
394 g_string_append_c (html, ' ');
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
402 if (++break_dist == 256) {
403 g_string_append_c (html, '\n');
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;
420 /* don't convert ' --> wpeditor will try to re-convert it... */
421 //case '\'' : g_string_append (html, "'"); 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*/
427 if (space_seen) { /* second space in a row */
428 g_string_append (html, " ");
434 g_string_append_c (html, kar);
441 modest_text_utils_convert_buffer_to_html_finish (GString *html)
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] = '&';
452 modest_text_utils_convert_to_html (const gchar *data)
457 g_return_val_if_fail (data, NULL);
463 html = g_string_sized_new (1.5 * len); /* just a guess... */
465 g_string_append_printf (html,
467 "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf8\">"
471 modest_text_utils_convert_buffer_to_html_start (html, data, -1);
473 g_string_append (html, "</body></html>");
475 if (len <= HYPERLINKIFY_MAX_LENGTH)
476 hyperlinkify_plain_text (html);
478 modest_text_utils_convert_buffer_to_html_finish (html);
480 return g_string_free (html, FALSE);
484 modest_text_utils_convert_to_html_body (const gchar *data, gssize n, gboolean hyperlinkify)
488 g_return_val_if_fail (data, NULL);
495 html = g_string_sized_new (1.5 * n); /* just a guess... */
497 modest_text_utils_convert_buffer_to_html_start (html, data, n);
499 if (hyperlinkify && (n < HYPERLINKIFY_MAX_LENGTH))
500 hyperlinkify_plain_text (html);
502 modest_text_utils_convert_buffer_to_html_finish (html);
504 return g_string_free (html, FALSE);
508 modest_text_utils_get_addresses_indexes (const gchar *addresses, GSList **start_indexes, GSList **end_indexes)
510 gchar *current, *start, *last_blank;
511 gint start_offset = 0, current_offset = 0;
513 g_return_if_fail (start_indexes != NULL);
514 g_return_if_fail (end_indexes != NULL);
516 start = (gchar *) addresses;
520 while (*current != '\0') {
521 if ((start == current)&&((*current == ' ')||(*current == ',')||(*current == ';'))) {
522 start = g_utf8_next_char (start);
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;
536 } else if (*current == '"') {
537 current = g_utf8_next_char (current);
539 while ((*current != '"')&&(*current != '\0')) {
540 current = g_utf8_next_char (current);
545 current = g_utf8_next_char (current);
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);
559 *start_indexes = g_slist_reverse (*start_indexes);
560 *end_indexes = g_slist_reverse (*end_indexes);
567 modest_text_utils_split_addresses_list (const gchar *addresses)
570 const gchar *my_addrs = addresses;
574 /* skip any space, ',', ';' at the start */
575 while (my_addrs && (my_addrs[0] == ' ' || my_addrs[0] == ',' || my_addrs[0] == ';'))
578 /* are we at the end of addresses list? */
582 /* nope, we are at the start of some address
583 * now, let's find the end of the address */
585 while (end[0] && end[0] != ',' && end[0] != ';')
588 /* we got the address; copy it and remove trailing whitespace */
589 addr = g_strndup (my_addrs, end - my_addrs);
592 head = g_slist_append (NULL, addr);
593 head->next = modest_text_utils_split_addresses_list (end); /* recurse */
600 modest_text_utils_address_range_at_position (const gchar *recipients_list,
605 gchar *current = NULL;
606 gint range_start = 0;
609 gboolean is_quoted = FALSE;
611 g_return_if_fail (recipients_list);
612 g_return_if_fail (position < g_utf8_strlen(recipients_list, -1));
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);
619 if ((c == ',') && (!is_quoted)) {
620 if (index < position) {
621 range_start = index + 1;
625 } else if (c == '\"') {
626 is_quoted = !is_quoted;
627 } else if ((c == ' ') &&(range_start == index)) {
635 *start = range_start;
641 /* ******************************************************************* */
642 /* ************************* UTILIY FUNCTIONS ************************ */
643 /* ******************************************************************* */
646 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
652 return g_string_new("");
660 gs = g_string_new_len (i0, iter - i0);
664 get_indent_level (const char *l)
681 /* if we hit the signature marker "-- ", we return -(indent + 1). This
682 * stops reformatting.
684 if (strcmp (l, "-- ") == 0) {
692 unquote_line (GString * l)
707 g_string_erase (l, 0, p - l->str);
711 append_quoted (GString * buf, int indent, const GString * str,
716 indent = indent < 0 ? abs (indent) - 1 : indent;
717 for (i = 0; i <= indent; i++) {
718 g_string_append (buf, "> ");
721 g_string_append_len (buf, str->str, cutpoint);
723 g_string_append (buf, str->str);
725 g_string_append (buf, "\n");
729 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
732 const gchar *pos, *last;
735 indent = indent < 0 ? abs (indent) - 1 : indent;
739 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
741 if ((index + 2 * indent > limit) && last) {
745 if (g_unichar_isspace (uni[index])) {
748 pos = g_utf8_next_char (pos);
756 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
761 if (last + 2 * indent < limit)
764 for (i = strlen (s); i > 0; i--) {
766 if (i + 2 * indent <= limit) {
777 get_breakpoint (const gchar * s, const gint indent, const gint limit)
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);
789 cite (const time_t sent_date, const gchar *from)
791 return g_strdup (_("mcen_ia_editor_original_message"));
795 quoted_attachments (GList *attachments)
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);
804 return g_string_free (result, FALSE);
809 modest_text_utils_quote_plain_text (const gchar *text,
811 const gchar *signature,
816 gint indent, breakpoint, rem_indent = 0;
817 GString *q, *l, *remaining;
819 gchar *attachments_string = NULL;
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 ("");
830 l = get_next_line (text, len, iter);
831 iter = iter + l->len + 1;
832 indent = get_indent_level (l->str);
835 if (remaining->len) {
836 if (l->len && indent == rem_indent) {
837 g_string_prepend (l, " ");
838 g_string_prepend (l, remaining->str);
842 get_breakpoint (remaining->str,
845 append_quoted (q, rem_indent,
846 remaining, breakpoint);
847 g_string_erase (remaining, 0,
849 if (remaining->str[0] == ' ') {
850 g_string_erase (remaining, 0,
853 } while (remaining->len);
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);
863 append_quoted (q, indent, l, breakpoint);
864 g_string_free (l, TRUE);
865 } while ((iter < text + len) || (remaining->str[0]));
867 attachments_string = quoted_attachments (attachments);
868 q = g_string_append (q, attachments_string);
869 g_free (attachments_string);
871 if (signature != NULL) {
872 q = g_string_append_c (q, '\n');
873 q = g_string_append (q, signature);
876 return g_string_free (q, FALSE);
880 modest_text_utils_quote_html (const gchar *text,
882 const gchar *signature,
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" \
893 "<pre>%s<br/>%s<br/>%s</pre>\n" \
896 gchar *attachments_string = NULL;
897 gchar *q_attachments_string = NULL;
898 gchar *q_cite = NULL;
899 gchar *html_text = NULL;
901 if (signature == NULL)
902 signature_result = g_strdup ("");
904 signature_result = modest_text_utils_convert_to_html_body (signature, -1, TRUE);
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);
913 g_free (attachments_string);
914 g_free (q_attachments_string);
915 g_free (signature_result);
921 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
923 return match2->offset - match1->offset;
926 static gboolean url_matches_block = 0;
927 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
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);
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);
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 */
960 modest_text_utils_hyperlinkify_begin (void)
962 if (url_matches_block == 0)
964 url_matches_block ++;
968 modest_text_utils_hyperlinkify_end (void)
971 if (url_matches_block <= 0)
977 get_url_matches (GString *txt)
980 guint rv, i, offset = 0;
981 GSList *match_list = NULL;
983 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
985 /* initalize the regexps */
986 modest_text_utils_hyperlinkify_begin ();
988 /* find all the matches */
989 for (i = 0; i != pattern_num; ++i) {
993 gboolean is_submatch;
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 */
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);
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);
1027 modest_text_utils_hyperlinkify_end ();
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
1033 match_list = g_slist_sort (match_list,
1034 (GCompareFunc)cmp_offsets_reverse);
1040 /* replace all occurences of needle in haystack with repl*/
1042 replace_string (const gchar *haystack, const gchar *needle, gchar repl)
1044 gchar *str, *cursor;
1046 if (!haystack || !needle || strlen(needle) == 0)
1047 return haystack ? g_strdup(haystack) : NULL;
1049 str = g_strdup (haystack);
1051 for (cursor = str; cursor && *cursor; ++cursor) {
1052 if (g_str_has_prefix (cursor, needle)) {
1054 memmove (cursor + 1,
1055 cursor + strlen (needle),
1056 strlen (cursor + strlen (needle)) + 1);
1064 hyperlinkify_plain_text (GString *txt)
1067 GSList *match_list = get_url_matches (txt);
1069 /* we will work backwards, so the offsets stay valid */
1070 for (cursor = match_list; cursor; cursor = cursor->next) {
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 */
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
1081 gchar *href_url = replace_string (url, MARK_AMP_URI_STR "amp;", '&');
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,
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);
1097 g_slice_free (url_match_t, match);
1100 g_slist_free (match_list);
1104 /* for optimization reasons, we change the string in-place */
1106 modest_text_utils_get_display_address (gchar *address)
1110 g_return_if_fail (address);
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);
1120 /* remove leading whitespace */
1121 if (address[0] == ' ')
1122 g_strchug (address);
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 */
1129 address[i] = '\0'; /* terminate the string here */
1141 modest_text_utils_get_email_address (const gchar *full_address)
1143 const gchar *left, *right;
1145 g_return_val_if_fail (full_address, NULL);
1150 g_return_val_if_fail (g_utf8_validate (full_address, -1, NULL), NULL);
1152 left = g_strrstr_len (full_address, strlen(full_address), "<");
1154 return g_strdup (full_address);
1156 right = g_strstr_len (left, strlen(left), ">");
1158 return g_strdup (full_address);
1160 return g_strndup (left + 1, right - left - 1);
1164 modest_text_utils_get_subject_prefix_len (const gchar *sub)
1166 gint prefix_len = 0;
1168 g_return_val_if_fail (sub, 0);
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')
1176 else if (sub[0] && sub[1] != 'e' && sub[1] != 'E' && sub[1] != 'w' && sub[1] != 'W')
1183 /* skip over a [...] block */
1184 if (sub[prefix_len] == '[') {
1185 int c = prefix_len + 1;
1186 while (sub[c] && sub[c] != ']')
1189 return 0; /* no end to the ']' found */
1194 /* did we find the ':' ? */
1195 if (sub[prefix_len] == ':') {
1197 if (sub[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); */
1208 modest_text_utils_utf8_strcmp (const gchar* s1, const gchar *s2, gboolean insensitive)
1211 /* work even when s1 and/or s2 == NULL */
1212 if (G_UNLIKELY(s1 == s2))
1214 if (G_UNLIKELY(!s1))
1216 if (G_UNLIKELY(!s2))
1219 /* if it's not case sensitive */
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];
1226 return g_utf8_collate (s1, s2);
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]);
1236 n1 = g_utf8_strdown (s1, -1);
1237 n2 = g_utf8_strdown (s2, -1);
1239 result = g_utf8_collate (n1, n2);
1250 modest_text_utils_get_display_date (time_t date)
1252 #define DATE_BUF_SIZE 64
1253 static gchar date_buf[DATE_BUF_SIZE];
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);
1261 /* if it's today, show the time, if it's not today, show the date instead */
1263 if (day == date_day) /* is the date today? */
1264 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%X", date);
1266 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, "%x", date);
1268 return date_buf; /* this is a static buffer, don't free! */
1274 modest_text_utils_validate_folder_name (const gchar *folder_name)
1276 /* based on http://msdn2.microsoft.com/en-us/library/aa365247.aspx,
1277 * with some extras */
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",
1288 /* cannot be NULL */
1292 /* cannot be empty */
1293 len = strlen(folder_name);
1297 /* cannot start or end with a space */
1298 if (g_ascii_isspace(folder_name[0]) || g_ascii_isspace(folder_name[len - 1]))
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))
1306 /* cannot contain a forbidden word */
1308 for (cursor = forbidden_names; cursor && *cursor; ++cursor) {
1309 if (g_ascii_strcasecmp (folder_name, *cursor) == 0)
1314 return TRUE; /* it's valid! */
1320 modest_text_utils_validate_domain_name (const gchar *domain)
1322 gboolean valid = FALSE;
1324 const gchar* domain_regex = "^([a-z0-9-]*[a-z0-9]\\.)+[a-z0-9-]*[a-z0-9]$";
1326 g_return_val_if_fail (domain, FALSE);
1331 memset (&rx, 0, sizeof(regex_t)); /* coverity wants this... */
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");
1340 valid = (regexec (&rx, domain, 1, NULL, 0) == 0);
1349 modest_text_utils_validate_email_address (const gchar *email_address,
1350 const gchar **invalid_char_position)
1353 const gchar *c = NULL, *domain = NULL;
1354 static gchar *rfc822_specials = "()<>@,;:\\\"[]&";
1356 if (invalid_char_position)
1357 *invalid_char_position = NULL;
1359 g_return_val_if_fail (email_address, FALSE);
1361 /* check that the email adress contains exactly one @ */
1362 if (!strstr(email_address, "@") ||
1363 (strstr(email_address, "@") != g_strrstr(email_address, "@")))
1366 /* first we validate the name portion (name@domain) */
1367 for (c = email_address; *c; c++) {
1369 (c == email_address ||
1371 *(c - 1) == '\"')) {
1375 if (*c == '\\' && (*++c == ' '))
1377 if (*c <= ' ' || *c >= 127)
1390 if (*c <= ' ' || *c >= 127)
1392 if (strchr(rfc822_specials, *c)) {
1393 if (invalid_char_position)
1394 *invalid_char_position = c;
1398 if (c == email_address || *(c - 1) == '.')
1401 /* next we validate the domain portion (name@domain) */
1402 if (!*(domain = ++c))
1406 if (c == domain || *(c - 1) == '.' || *(c + 1) == '\0')
1410 if (*c <= ' ' || *c >= 127)
1412 if (strchr(rfc822_specials, *c)) {
1413 if (invalid_char_position)
1414 *invalid_char_position = c;
1419 return (count >= 1) ? TRUE : FALSE;
1423 modest_text_utils_validate_recipient (const gchar *recipient, const gchar **invalid_char_position)
1425 gchar *stripped, *current;
1427 gboolean has_error = FALSE;
1429 if (invalid_char_position)
1430 *invalid_char_position = NULL;
1432 g_return_val_if_fail (recipient, FALSE);
1434 if (modest_text_utils_validate_email_address (recipient, invalid_char_position))
1437 stripped = g_strdup (recipient);
1438 stripped = g_strstrip (stripped);
1441 if (*current == '\0') {
1447 if (*current == '\"') {
1448 current = g_utf8_next_char (current);
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.
1455 if (current[1] <0) {
1460 } else if (*current == '\"') {
1462 current = g_utf8_next_char (current);
1468 for (current = stripped ; *current != '\0'; current = g_utf8_next_char (current)) {
1469 if (*current == '<') {
1481 right_part = g_strdup (current);
1483 right_part = g_strstrip (right_part);
1485 if (g_str_has_prefix (right_part, "<") &&
1486 g_str_has_suffix (right_part, ">")) {
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);
1496 g_free (right_part);
1503 modest_text_utils_get_display_size (guint64 size)
1505 const guint KB=1024;
1506 const guint MB=1024 * KB;
1507 const guint GB=1024 * MB;
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);
1522 return g_strdup_printf (_FM("sfil_li_size_1gb_or_greater"), (float) size / GB);
1526 get_email_from_address (const gchar * address)
1528 gchar *left_limit, *right_limit;
1530 left_limit = strstr (address, "<");
1531 right_limit = g_strrstr (address, ">");
1533 if ((left_limit == NULL)||(right_limit == NULL)|| (left_limit > right_limit))
1534 return g_strdup (address);
1536 return g_strndup (left_limit + 1, (right_limit - left_limit) - 1);
1540 modest_text_utils_get_color_string (GdkColor *color)
1542 g_return_val_if_fail (color, NULL);
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);
1554 modest_text_utils_text_buffer_get_text (GtkTextBuffer *buffer)
1556 GtkTextIter start, end;
1557 gchar *slice, *current;
1558 GString *result = g_string_new ("");
1560 g_return_val_if_fail (buffer && GTK_IS_TEXT_BUFFER (buffer), NULL);
1562 gtk_text_buffer_get_start_iter (buffer, &start);
1563 gtk_text_buffer_get_end_iter (buffer, &end);
1565 slice = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
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);
1573 gchar *next = g_utf8_strchr (current, -1, 0xFFFC);
1575 result = g_string_append (result, current);
1577 result = g_string_append_len (result, current, next - current);
1584 return g_string_free (result, FALSE);
1589 modest_text_utils_is_forbidden_char (const gchar character,
1590 ModestTextUtilsForbiddenCharType type)
1593 const gchar *forbidden_chars = NULL;
1595 /* We need to get the length in the switch because the
1596 compiler needs to know the size at compile time */
1598 case ACCOUNT_TITLE_FORBIDDEN_CHARS:
1599 forbidden_chars = account_title_forbidden_chars;
1600 len = G_N_ELEMENTS (account_title_forbidden_chars);
1602 case FOLDER_NAME_FORBIDDEN_CHARS:
1603 forbidden_chars = folder_name_forbidden_chars;
1604 len = G_N_ELEMENTS (folder_name_forbidden_chars);
1606 case USER_NAME_FORBIDDEN_NAMES:
1607 forbidden_chars = user_name_forbidden_chars;
1608 len = G_N_ELEMENTS (user_name_forbidden_chars);
1611 g_return_val_if_reached (TRUE);
1614 for (i = 0; i < len ; i++)
1615 if (forbidden_chars[i] == character)
1618 return FALSE; /* it's valid! */