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 #include <glib/gi18n.h>
36 #include "modest-text-utils.h"
41 #endif /*HAVE_CONFIG_H */
44 static GString *get_next_line (const char *b, const gsize blen, const gchar * iter);
45 static int get_indent_level (const char *l);
46 static void unquote_line (GString * l);
47 static void append_quoted (GString * buf, const int indent,
48 const GString * str, const int cutpoint);
49 static int get_breakpoint_utf8 (const gchar * s, const gint indent,
51 static int get_breakpoint_ascii (const gchar * s, const gint indent,
53 static int get_breakpoint (const gchar * s, const gint indent,
57 get_next_line (const gchar * b, const gsize blen, const gchar * iter)
63 return g_string_new("");
71 gs = g_string_new_len (i0, iter - i0);
75 get_indent_level (const char *l)
92 /* if we hit the signature marker "-- ", we return -(indent + 1). This
95 if (strcmp (l, "-- ") == 0) {
103 unquote_line (GString * l)
118 g_string_erase (l, 0, p - l->str);
122 append_quoted (GString * buf, int indent, const GString * str,
127 indent = indent < 0 ? abs (indent) - 1 : indent;
128 for (i = 0; i <= indent; i++) {
129 g_string_append (buf, "> ");
132 g_string_append_len (buf, str->str, cutpoint);
134 g_string_append (buf, str->str);
136 g_string_append (buf, "\n");
140 get_breakpoint_utf8 (const gchar * s, gint indent, const gint limit)
143 const gchar *pos, *last;
146 indent = indent < 0 ? abs (indent) - 1 : indent;
150 uni = g_utf8_to_ucs4_fast (s, -1, NULL);
152 if ((index + 2 * indent > limit) && last) {
156 if (g_unichar_isspace (uni[index])) {
159 pos = g_utf8_next_char (pos);
167 get_breakpoint_ascii (const gchar * s, const gint indent, const gint limit)
172 if (last + 2 * indent < limit)
175 for (i = strlen (s); i > 0; i--) {
177 if (i + 2 * indent <= limit) {
188 get_breakpoint (const gchar * s, const gint indent, const gint limit)
191 if (g_utf8_validate (s, -1, NULL)) {
192 return get_breakpoint_utf8 (s, indent, limit);
193 } else { /* assume ASCII */
194 //g_warning("invalid UTF-8 in msg");
195 return get_breakpoint_ascii (s, indent, limit);
201 /* just to prevent warnings:
202 * warning: `%x' yields only last 2 digits of year in some locales
205 modest_text_utils_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
207 return strftime(s, max, fmt, tm);
211 cite (const time_t sent_date, const gchar *from)
215 /* format sent_date */
216 modest_text_utils_strftime (sent_str, 100, "%c", localtime (&sent_date));
217 return g_strdup_printf (N_("On %s, %s wrote:\n"), sent_str, from);
222 modest_text_utils_quote (const gchar * to_quote, const gchar * from,
223 const time_t sent_date, const int limit)
226 gint indent, breakpoint, rem_indent = 0;
227 GString *q, *l, *remaining;
231 /* format sent_date */
232 tmp = cite (sent_date, from);
233 q = g_string_new (tmp);
236 /* remaining will store the rest of the line if we have to break it */
237 remaining = g_string_new ("");
240 len = strlen(to_quote);
242 l = get_next_line (to_quote, len, iter);
243 iter = iter + l->len + 1;
244 indent = get_indent_level (l->str);
247 if (remaining->len) {
248 if (l->len && indent == rem_indent) {
249 g_string_prepend (l, " ");
250 g_string_prepend (l, remaining->str);
254 get_breakpoint (remaining-> str,
257 append_quoted (q, rem_indent,
258 remaining, breakpoint);
259 g_string_erase (remaining, 0,
261 if (remaining->str[0] == ' ') {
262 g_string_erase (remaining, 0,
265 } while (remaining->len);
268 g_string_free (remaining, TRUE);
269 breakpoint = get_breakpoint (l->str, indent, limit);
270 remaining = g_string_new (l->str + breakpoint);
271 if (remaining->str[0] == ' ') {
272 g_string_erase (remaining, 0, 1);
275 append_quoted (q, indent, l, breakpoint);
276 g_string_free (l, TRUE);
277 } while ((iter < to_quote + len) || (remaining->str[0]));
279 return g_string_free (q, FALSE);
284 modest_text_utils_derived_subject (const gchar *subject, const gchar *prefix)
289 return g_strdup_printf ("%s ", prefix);
291 tmp = g_strchug (g_strdup (subject));
293 if (!strncmp (tmp, prefix, strlen (prefix))) {
297 return g_strdup_printf ("%s %s", prefix, subject);
303 modest_text_utils_cited_text (const gchar *from,
309 tmp = cite (sent_date, from);
310 retval = g_strdup_printf ("%s%s\n", tmp, text);
318 modest_text_utils_inlined_text (const gchar *from, time_t sent_date,
319 const gchar *to, const gchar *subject,
324 modest_text_utils_strftime (sent_str, 100, "%c", localtime (&sent_date));
326 return g_strdup_printf ("%s\n%s %s\n%s %s\n%s %s\n%s %s\n\n%s",
327 _("-----Forwarded Message-----"),
329 _("Sent:"), sent_str,
331 _("Subject:"), subject,
336 modest_text_utils_remove_address (const gchar *address, const gchar *address_list)
338 char *dup, *token, *ptr, *result;
339 GString *filtered_emails;
344 /* Search for substring */
345 if (!strstr ((const char *) address_list, (const char *) address))
346 return g_strdup (address_list);
348 dup = g_strdup (address_list);
349 filtered_emails = g_string_new (NULL);
351 token = strtok_r (dup, ",", &ptr);
353 while (token != NULL) {
354 /* Add to list if not found */
355 if (!strstr ((const char *) token, (const char *) address)) {
356 if (filtered_emails->len == 0)
357 g_string_append_printf (filtered_emails, "%s", token);
359 g_string_append_printf (filtered_emails, ",%s", token);
361 token = strtok_r (NULL, ",", &ptr);
363 result = filtered_emails->str;
367 g_string_free (filtered_emails, FALSE);
376 * we need these regexps to find URLs in plain text e-mails
378 typedef struct _url_match_pattern_t url_match_pattern_t;
379 struct _url_match_pattern_t {
385 typedef struct _url_match_t url_match_t;
386 struct _url_match_t {
393 #define MAIL_VIEWER_URL_MATCH_PATTERNS { \
394 { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
396 { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
398 { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
400 { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
402 { "mailto:[-_a-z0-9.\\+]+@[-_a-z0-9.]+", \
404 { "[-_a-z0-9.\\+]+@[-_a-z0-9.]+",\
410 cmp_offsets_reverse (const url_match_t *match1, const url_match_t *match2)
412 return match2->offset - match1->offset;
418 * check if the match is inside an existing match... */
420 chk_partial_match (const url_match_t *match, guint* offset)
422 if (*offset >= match->offset && *offset < match->offset + match->len)
427 get_url_matches (GString *txt)
430 guint rv, i, offset = 0;
431 GSList *match_list = NULL;
433 static url_match_pattern_t patterns[] = MAIL_VIEWER_URL_MATCH_PATTERNS;
434 const size_t pattern_num = sizeof(patterns)/sizeof(url_match_pattern_t);
436 /* initalize the regexps */
437 for (i = 0; i != pattern_num; ++i) {
438 patterns[i].preg = g_new0 (regex_t,1);
439 g_assert(regcomp (patterns[i].preg, patterns[i].regex,
440 REG_ICASE|REG_EXTENDED|REG_NEWLINE) == 0);
442 /* find all the matches */
443 for (i = 0; i != pattern_num; ++i) {
447 if ((rv = regexec (patterns[i].preg, txt->str + offset, 1, &rm, 0)) != 0) {
448 g_assert (rv == REG_NOMATCH); /* this should not happen */
449 break; /* try next regexp */
454 /* FIXME: optimize this */
455 /* to avoid partial matches on something that was already found... */
456 /* check_partial_match will put -1 in the data ptr if that is the case */
457 test_offset = offset + rm.rm_so;
458 g_slist_foreach (match_list, (GFunc)chk_partial_match, &test_offset);
460 /* make a list of our matches (<offset, len, prefix> tupels)*/
461 if (test_offset != -1) {
462 url_match_t *match = g_new (url_match_t,1);
463 match->offset = offset + rm.rm_so;
464 match->len = rm.rm_eo - rm.rm_so;
465 match->prefix = patterns[i].prefix;
466 match_list = g_slist_prepend (match_list, match);
472 for (i = 0; i != pattern_num; ++i) {
473 regfree (patterns[i].preg);
474 g_free (patterns[i].preg);
475 } /* don't free patterns itself -- it's static */
477 /* now sort the list, so the matches are in reverse order of occurence.
478 * that way, we can do the replacements starting from the end, so we don't need
479 * to recalculate the offsets
481 match_list = g_slist_sort (match_list,
482 (GCompareFunc)cmp_offsets_reverse);
489 hyperlinkify_plain_text (GString *txt)
492 GSList *match_list = get_url_matches (txt);
494 /* we will work backwards, so the offsets stay valid */
495 for (cursor = match_list; cursor; cursor = cursor->next) {
497 url_match_t *match = (url_match_t*) cursor->data;
498 gchar *url = g_strndup (txt->str + match->offset, match->len);
499 gchar *repl = NULL; /* replacement */
501 /* the prefix is NULL: use the one that is already there */
502 repl = g_strdup_printf ("<a href=\"%s%s\">%s</a>",
503 match->prefix ? match->prefix : "", url, url);
505 /* replace the old thing with our hyperlink
506 * replacement thing */
507 g_string_erase (txt, match->offset, match->len);
508 g_string_insert (txt, match->offset, repl);
513 g_free (cursor->data);
516 g_slist_free (match_list);
522 modest_text_utils_convert_to_html (const gchar *data)
525 gboolean first_space = TRUE;
533 html = g_string_sized_new (len + 100); /* just a guess... */
535 g_string_append_printf (html,
538 "<meta http-equiv=\"content-type\""
539 " content=\"text/html; charset=utf8\">"
543 /* replace with special html chars where needed*/
544 for (i = 0; i != len; ++i) {
548 case 0: break; /* ignore embedded \0s */
549 case '<' : g_string_append (html, "<"); break;
550 case '>' : g_string_append (html, ">"); break;
551 case '&' : g_string_append (html, """); break;
552 case '\n': g_string_append (html, "<br>\n"); break;
555 g_string_append (html, first_space ? " " : " ");
557 } else if (kar == '\t')
558 g_string_append (html, " ");
562 /* optimization trick: accumulate 'normal' chars, then copy */
564 kar = data [++charnum + i];
566 } while ((i + charnum < len) &&
567 (kar > '>' || (kar != '<' && kar != '>'
568 && kar != '&' && kar != ' '
569 && kar != '\n' && kar != '\t')));
570 g_string_append_len (html, &data[i], charnum);
576 g_string_append (html, "</tt></body></html>");
577 hyperlinkify_plain_text (html);
579 return g_string_free (html, FALSE);