aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-format.c
diff options
context:
space:
mode:
authorDan Winship <danw@src.gnome.org>2000-04-28 01:18:55 +0800
committerDan Winship <danw@src.gnome.org>2000-04-28 01:18:55 +0800
commit61e58833c2a77ed4b3db33c598c804c7831c33ab (patch)
treea18d709e613892c835af92b41481a2195a45a5a6 /mail/mail-format.c
parent917d28e2dfdade1f8ec46122ec5e27c9409b2380 (diff)
downloadgsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.tar
gsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.tar.gz
gsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.tar.bz2
gsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.tar.lz
gsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.tar.xz
gsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.tar.zst
gsoc2013-evolution-61e58833c2a77ed4b3db33c598c804c7831c33ab.zip
Improve URL converstion code. Recognize https, recognize "www\..*" without
* mail-format.c (text_to_html): Improve URL converstion code. Recognize https, recognize "www\..*" without a prefixed "http://". Properly escape &, <, >, etc in URL strings. Don't be fooled by "mailto:", "http://", etc with no following data. svn path=/trunk/; revision=2660
Diffstat (limited to 'mail/mail-format.c')
-rw-r--r--mail/mail-format.c62
1 files changed, 48 insertions, 14 deletions
diff --git a/mail/mail-format.c b/mail/mail-format.c
index 033440e876..f8d9085d64 100644
--- a/mail/mail-format.c
+++ b/mail/mail-format.c
@@ -380,6 +380,31 @@ check_size (char **buffer, int *buffer_size, char *out, int len)
return out;
}
+char *
+url_extract (const char **text, gboolean check)
+{
+ const char *end = *text, *p;
+ char *out;
+
+ while (*end && !isspace (*end) && *end != '"')
+ end++;
+
+ /* Back up if we probably went too far. */
+ while (end > *text && strchr (",.!?;:>", *(end - 1)))
+ end--;
+
+ if (check) {
+ /* Make sure we weren't fooled. */
+ p = memchr (*text, ':', end - *text);
+ if (!p || end - p < 3)
+ return NULL;
+ }
+
+ out = g_strndup (*text, end - *text);
+ *text = end;
+ return out;
+}
+
/* Convert plain text into equivalent-looking valid HTML. */
static char *
text_to_html (const unsigned char *input, unsigned int flags)
@@ -399,28 +424,37 @@ text_to_html (const unsigned char *input, unsigned int flags)
while (*cur) {
if (isalpha (*cur) && (flags & TEXT_TO_HTML_CONVERT_URLS)) {
+ char *tmpurl = NULL, *refurl, *dispurl;
+
if (!strncasecmp (cur, "http://", 7) ||
+ !strncasecmp (cur, "https://", 8) ||
!strncasecmp (cur, "ftp://", 6) ||
!strncasecmp (cur, "nntp://", 7) ||
!strncasecmp (cur, "mailto:", 7) ||
!strncasecmp (cur, "news:", 5)) {
- /* Skip to end of URL. */
- end = cur;
- while (*end && !isspace (*end) && *end != '"')
- end++;
-
- /* Back up if we probably went too far. */
- while (end > cur &&
- strchr (",.!?;:>", *(end - 1)))
- end--;
+ tmpurl = url_extract (&cur, TRUE);
+ if (tmpurl) {
+ refurl = text_to_html (tmpurl, 0);
+ dispurl = g_strdup (refurl);
+ }
+ } else if (!strncasecmp (cur, "www.", 4) &&
+ isalnum (*(cur + 4))) {
+ tmpurl = url_extract (&cur, FALSE);
+ dispurl = text_to_html (tmpurl, 0);
+ refurl = g_strdup_printf ("http://%s",
+ dispurl);
+ }
+ if (tmpurl) {
out = check_size (&buffer, &buffer_size, out,
- (end - cur) * 2 + 15);
+ strlen (refurl) +
+ strlen (dispurl) + 15);
out += sprintf (out,
- "<a href=\"%.*s\">%.*s</a>",
- end - cur, cur,
- end - cur, cur);
- cur = end;
+ "<a href=\"%s\">%s</a>",
+ refurl, dispurl);
+ g_free (tmpurl);
+ g_free (refurl);
+ g_free (dispurl);
}
}