evolution-3.6.4/e-util/e-html-utils.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Dan Winship <danw@ximian.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include <ctype.h>
 28 #include <stdio.h>
 29 #include <string.h>
 30 
 31 #include "e-html-utils.h"
 32 
 33 static gchar *
 34 check_size (gchar **buffer,
 35             gint *buffer_size,
 36             gchar *out,
 37             gint len)
 38 {
 39 	if (out + len + 1> *buffer + *buffer_size) {
 40 		gint index = out - *buffer;
 41 
 42 		*buffer_size = MAX (index + len + 1, *buffer_size * 2);
 43 		*buffer = g_realloc (*buffer, *buffer_size);
 44 		out = *buffer + index;
 45 	}
 46 	return out;
 47 }
 48 
 49 /* auto-urlification hints: the goal is not to be strictly RFC-compliant,
 50  * but rather to accurately distinguish urls/addresses from non-urls/
 51  * addresses in real-world email.
 52  *
 53  * 1 = non-email-address chars: ()<>@,;:\"[]`'{}|
 54  * 2 = trailing url garbage:    ,.!?;:>)]}`'-_
 55  * 4 = allowed dns chars
 56  * 8 = non-url chars:           "|
 57  */
 58 static gint special_chars[] = {
 59 	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,    /*  nul - 0x0f */
 60 	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,    /* 0x10 - 0x1f */
 61 	9, 2, 9, 0, 0, 0, 0, 3, 1, 3, 0, 0, 3, 6, 6, 0,    /*   sp - /    */
 62 	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 1, 0, 3, 2,    /*    0 - ?    */
 63 	1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,    /*    @ - O    */
 64 	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 3, 0, 2,    /*    P - _    */
 65 	3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,    /*    ` - o    */
 66 	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 9, 3, 0, 3     /*    p - del  */
 67 };
 68 
 69 #define is_addr_char(c) (c < 128 && !(special_chars[c] & 1))
 70 #define is_url_char(c) (c < 128 && !(special_chars[c] & 8))
 71 #define is_trailing_garbage(c) (c > 127 || (special_chars[c] & 2))
 72 #define is_domain_name_char(c) (c < 128 && (special_chars[c] & 4))
 73 
 74 /* (http|https|ftp|nntp)://[^ "|/]+\.([^ "|]*[^ ,.!?;:>)\]}`'"|_-])+ */
 75 /* www\.[A-Za-z0-9.-]+(/([^ "|]*[^ ,.!?;:>)\]}`'"|_-])+)             */
 76 
 77 static gchar *
 78 url_extract (const guchar **text,
 79              gboolean full_url)
 80 {
 81 	const guchar *end = *text, *p;
 82 	gchar *out;
 83 
 84 	while (*end && is_url_char (*end))
 85 		end++;
 86 
 87 	/* Back up if we probably went too far. */
 88 	while (end > *text && is_trailing_garbage (*(end - 1)))
 89 		end--;
 90 
 91 	if (full_url) {
 92 		/* Make sure this really looks like a URL. */
 93 		p = memchr (*text, ':', end - *text);
 94 		if (!p || end - p < 4)
 95 			return NULL;
 96 	} else {
 97 		/* Make sure this really looks like a hostname. */
 98 		p = memchr (*text, '.', end - *text);
 99 		if (!p || p >= end - 2)
100 			return NULL;
101 		p = memchr (p + 2, '.', end - (p + 2));
102 		if (!p || p >= end - 2)
103 			return NULL;
104 	}
105 
106 	out = g_strndup ((gchar *) * text, end - *text);
107 	*text = end;
108 	return out;
109 }
110 
111 static gchar *
112 email_address_extract (const guchar **cur,
113                        gchar **out,
114                        const guchar *linestart)
115 {
116 	const guchar *start, *end, *dot;
117 	gchar *addr;
118 
119 	/* *cur points to the '@'. Look backward for a valid local-part */
120 	for (start = *cur; start - 1 >= linestart && is_addr_char (*(start - 1)); start--)
121 		;
122 	if (start == *cur)
123 		return NULL;
124 	if (start > linestart + 2 &&
125 	    start[-1] == ':' && start[0] == '/' && start[1] == '/')
126 		return NULL;
127 
128 	/* Now look forward for a valid domain part */
129 	for (end = *cur + 1, dot = NULL; is_domain_name_char (*end); end++) {
130 		if (*end == '.' && !dot)
131 			dot = end;
132 	}
133 	if (!dot)
134 		return NULL;
135 
136 	/* Remove trailing garbage */
137 	while (is_trailing_garbage (*(end - 1)))
138 		end--;
139 	if (dot > end)
140 		return NULL;
141 
142 	addr = g_strndup ((gchar *) start, end - start);
143 	*out -= *cur - start;
144 	*cur = end;
145 
146 	return addr;
147 }
148 
149 static gboolean
150 is_citation (const guchar *c,
151              gboolean saw_citation)
152 {
153 	const guchar *p;
154 
155 	if (*c != '>')
156 		return FALSE;
157 
158 	/* A line that starts with a ">" is a citation, unless it's
159 	 * just mbox From-mangling...
160 	 */
161 	if (strncmp ((const gchar *) c, ">From ", 6) != 0)
162 		return TRUE;
163 
164 	/* If the previous line was a citation, then say this
165 	 * one is too.
166 	 */
167 	if (saw_citation)
168 		return TRUE;
169 
170 	/* Same if the next line is */
171 	p = (const guchar *) strchr ((const gchar *) c, '\n');
172 	if (p && *++p == '>')
173 		return TRUE;
174 
175 	/* Otherwise, it was just an isolated ">From" line. */
176 	return FALSE;
177 }
178 
179 /**
180  * e_text_to_html_full:
181  * @input: a NUL-terminated input buffer
182  * @flags: some combination of the E_TEXT_TO_HTML_* flags defined
183  * in e-html-utils.h
184  * @color: color for citation highlighting
185  *
186  * This takes a buffer of text as input and produces a buffer of
187  * "equivalent" HTML, subject to certain transformation rules.
188  *
189  * The set of possible flags is:
190  *
191  *   - E_TEXT_TO_HTML_PRE: wrap the output HTML in &lt;PRE&gt; and
192  *     &lt;/PRE&gt;  Should only be used if @input is the entire
193  *     buffer to be converted. If e_text_to_html is being called with
194  *     small pieces of data, you should wrap the entire result in
195  *     &lt;PRE&gt; yourself.
196  *
197  *   - E_TEXT_TO_HTML_CONVERT_NL: convert "\n" to "&lt;BR&gt;n" on
198  *     output.  (Should not be used with E_TEXT_TO_HTML_PRE, since
199  *     that would result in double-newlines.)
200  *
201  *   - E_TEXT_TO_HTML_CONVERT_SPACES: convert a block of N spaces
202  *     into N-1 non-breaking spaces and one normal space. A space
203  *     at the start of the buffer is always converted to a
204  *     non-breaking space, regardless of the following character,
205  *     which probably means you don't want to use this flag on
206  *     pieces of data that aren't delimited by at least line breaks.
207  *
208  *     If E_TEXT_TO_HTML_CONVERT_NL and E_TEXT_TO_HTML_CONVERT_SPACES
209  *     are both defined, then TABs will also be converted to spaces.
210  *
211  *   - E_TEXT_TO_HTML_CONVERT_URLS: wrap &lt;a href="..."&gt; &lt;/a&gt;
212  *     around strings that look like URLs.
213  *
214  *   - E_TEXT_TO_HTML_CONVERT_ADDRESSES: wrap &lt;a href="mailto:..."&gt;
215  *     &lt;/a&gt; around strings that look like mail addresses.
216  *
217  *   - E_TEXT_TO_HTML_MARK_CITATION: wrap &lt;font color="..."&gt;
218  *     &lt;/font&gt; around citations (lines beginning with "> ", etc).
219  *
220  *   - E_TEXT_TO_HTML_ESCAPE_8BIT: flatten everything to US-ASCII
221  *
222  *   - E_TEXT_TO_HTML_CITE: quote the text with "> " at the start of each
223  *     line.
224  *
225  * Returns: a newly-allocated string containing HTML
226  **/
227 gchar *
228 e_text_to_html_full (const gchar *input,
229                      guint flags,
230                      guint32 color)
231 {
232 	const guchar *cur, *next, *linestart;
233 	gchar *buffer = NULL;
234 	gchar *out = NULL;
235 	gint buffer_size = 0, col;
236 	gboolean colored = FALSE, saw_citation = FALSE;
237 
238 	/* Allocate a translation buffer.  */
239 	buffer_size = strlen (input) * 2 + 5;
240 	buffer = g_malloc (buffer_size);
241 
242 	out = buffer;
243 	if (flags & E_TEXT_TO_HTML_PRE)
244 		out += sprintf (out, "<PRE>");
245 
246 	col = 0;
247 
248 	for (cur = linestart = (const guchar *) input; cur && *cur; cur = next) {
249 		gunichar u;
250 
251 		if (flags & E_TEXT_TO_HTML_MARK_CITATION && col == 0) {
252 			saw_citation = is_citation (cur, saw_citation);
253 			if (saw_citation) {
254 				if (!colored) {
255 					gchar font[25];
256 
257 					g_snprintf (font, 25, "<FONT COLOR=\"#%06x\">", color);
258 
259 					out = check_size (&buffer, &buffer_size, out, 25);
260 					out += sprintf (out, "%s", font);
261 					colored = TRUE;
262 				}
263 			} else if (colored) {
264 				const gchar *no_font = "</FONT>";
265 
266 				out = check_size (&buffer, &buffer_size, out, 9);
267 				out += sprintf (out, "%s", no_font);
268 				colored = FALSE;
269 			}
270 
271 			/* Display mbox-mangled ">From" as "From" */
272 			if (*cur == '>' && !saw_citation)
273 				cur++;
274 		} else if (flags & E_TEXT_TO_HTML_CITE && col == 0) {
275 			out = check_size (&buffer, &buffer_size, out, 5);
276 			out += sprintf (out, "&gt; ");
277 		}
278 
279 		u = g_utf8_get_char ((gchar *) cur);
280 		if (g_unichar_isalpha (u) &&
281 		    (flags & E_TEXT_TO_HTML_CONVERT_URLS)) {
282 			gchar *tmpurl = NULL, *refurl = NULL, *dispurl = NULL;
283 
284 			if (!g_ascii_strncasecmp ((gchar *) cur, "http://", 7) ||
285 			    !g_ascii_strncasecmp ((gchar *) cur, "https://", 8) ||
286 			    !g_ascii_strncasecmp ((gchar *) cur, "ftp://", 6) ||
287 			    !g_ascii_strncasecmp ((gchar *) cur, "nntp://", 7) ||
288 			    !g_ascii_strncasecmp ((gchar *) cur, "mailto:", 7) ||
289 			    !g_ascii_strncasecmp ((gchar *) cur, "news:", 5) ||
290 			    !g_ascii_strncasecmp ((gchar *) cur, "file:", 5) ||
291 			    !g_ascii_strncasecmp ((gchar *) cur, "callto:", 7) ||
292 			    !g_ascii_strncasecmp ((gchar *) cur, "h323:", 5) ||
293 			    !g_ascii_strncasecmp ((gchar *) cur, "sip:", 4) ||
294 			    !g_ascii_strncasecmp ((gchar *) cur, "webcal:", 7)) {
295 				tmpurl = url_extract (&cur, TRUE);
296 				if (tmpurl) {
297 					refurl = e_text_to_html (tmpurl, 0);
298 					dispurl = g_strdup (refurl);
299 				}
300 			} else if (!g_ascii_strncasecmp ((gchar *) cur, "www.", 4) &&
301 				   is_url_char (*(cur + 4))) {
302 				tmpurl = url_extract (&cur, FALSE);
303 				if (tmpurl) {
304 					dispurl = e_text_to_html (tmpurl, 0);
305 					refurl = g_strdup_printf (
306 						"http://%s", dispurl);
307 				}
308 			}
309 
310 			if (tmpurl) {
311 				out = check_size (
312 					&buffer, &buffer_size, out,
313 					strlen (refurl) +
314 					strlen (dispurl) + 15);
315 				out += sprintf (out,
316 						"<a href=\"%s\">%s</a>",
317 						refurl, dispurl);
318 				col += strlen (tmpurl);
319 				g_free (tmpurl);
320 				g_free (refurl);
321 				g_free (dispurl);
322 			}
323 
324 			if (!*cur)
325 				break;
326 			u = g_utf8_get_char ((gchar *) cur);
327 		}
328 
329 		if (u == '@' && (flags & E_TEXT_TO_HTML_CONVERT_ADDRESSES)) {
330 			gchar *addr, *dispaddr, *outaddr;
331 
332 			addr = email_address_extract (&cur, &out, linestart);
333 			if (addr) {
334 				dispaddr = e_text_to_html (addr, 0);
335 				outaddr = g_strdup_printf (
336 					"<a href=\"mailto:%s\">%s</a>",
337 					addr, dispaddr);
338 				out = check_size (&buffer, &buffer_size, out, strlen (outaddr));
339 				out += sprintf (out, "%s", outaddr);
340 				col += strlen (addr);
341 				g_free (addr);
342 				g_free (dispaddr);
343 				g_free (outaddr);
344 
345 				if (!*cur)
346 					break;
347 				u = g_utf8_get_char ((gchar *) cur);
348 			}
349 		}
350 
351 		if (!g_unichar_validate (u)) {
352 			/* Sigh. Someone sent undeclared 8-bit data.
353 			 * Assume it's iso-8859-1.
354 			 */
355 			u = *cur;
356 			next = cur + 1;
357 		} else
358 			next = (const guchar *) g_utf8_next_char (cur);
359 
360 		out = check_size (&buffer, &buffer_size, out, 10);
361 
362 		switch (u) {
363 		case '<':
364 			strcpy (out, "&lt;");
365 			out += 4;
366 			col++;
367 			break;
368 
369 		case '>':
370 			strcpy (out, "&gt;");
371 			out += 4;
372 			col++;
373 			break;
374 
375 		case '&':
376 			strcpy (out, "&amp;");
377 			out += 5;
378 			col++;
379 			break;
380 
381 		case '"':
382 			strcpy (out, "&quot;");
383 			out += 6;
384 			col++;
385 			break;
386 
387 		case '\n':
388 			if (flags & E_TEXT_TO_HTML_CONVERT_NL) {
389 				strcpy (out, "<br>");
390 				out += 4;
391 			}
392 			*out++ = *cur;
393 			linestart = cur;
394 			col = 0;
395 			break;
396 
397 		case '\t':
398 			if (flags & (E_TEXT_TO_HTML_CONVERT_SPACES |
399 				     E_TEXT_TO_HTML_CONVERT_NL)) {
400 				do {
401 					out = check_size (
402 						&buffer, &buffer_size, out, 7);
403 					strcpy (out, "&nbsp;");
404 					out += 6;
405 					col++;
406 				} while (col % 8);
407 				break;
408 			}
409 			/* otherwise, FALL THROUGH */
410 
411 		case ' ':
412 			if (flags & E_TEXT_TO_HTML_CONVERT_SPACES) {
413 				if (cur == (const guchar *) input ||
414 				    *(cur + 1) == ' ' || *(cur + 1) == '\t' ||
415 				    *(cur - 1) == '\n') {
416 					strcpy (out, "&nbsp;");
417 					out += 6;
418 					col++;
419 					break;
420 				}
421 			}
422 			/* otherwise, FALL THROUGH */
423 
424 		default:
425 			if ((u >= 0x20 && u < 0x80) ||
426 			    (u == '\r' || u == '\t')) {
427 				/* Default case, just copy. */
428 				*out++ = u;
429 			} else {
430 				if (flags & E_TEXT_TO_HTML_ESCAPE_8BIT)
431 					*out++ = '?';
432 				else
433 					out += g_snprintf (out, 9, "&#%d;", u);
434 			}
435 			col++;
436 			break;
437 		}
438 	}
439 
440 	out = check_size (&buffer, &buffer_size, out, 7);
441 	if (flags & E_TEXT_TO_HTML_PRE)
442 		strcpy (out, "</PRE>");
443 	else
444 		*out = '\0';
445 
446 	return buffer;
447 }
448 
449 gchar *
450 e_text_to_html (const gchar *input,
451                 guint flags)
452 {
453 	return e_text_to_html_full (input, flags, 0);
454 }
455 
456 #ifdef E_HTML_UTILS_TEST
457 
458 struct {
459 	gchar *text, *url;
460 } url_tests[] = {
461 	{ "bob@foo.com", "mailto:bob@foo.com" },
462 	{ "Ends with bob@foo.com", "mailto:bob@foo.com" },
463 	{ "bob@foo.com at start", "mailto:bob@foo.com" },
464 	{ "bob@foo.com.", "mailto:bob@foo.com" },
465 	{ "\"bob@foo.com\"", "mailto:bob@foo.com" },
466 	{ "<bob@foo.com>", "mailto:bob@foo.com" },
467 	{ "(bob@foo.com)", "mailto:bob@foo.com" },
468 	{ "bob@foo.com, 555-9999", "mailto:bob@foo.com" },
469 	{ "|bob@foo.com|555-9999|", "mailto:bob@foo.com" },
470 	{ "bob@ no match bob@", NULL },
471 	{ "@foo.com no match @foo.com", NULL },
472 	{ "\"bob\"@foo.com", NULL },
473 	{ "M@ke money fast!", NULL },
474 	{ "ASCII art @_@ @>->-", NULL },
475 
476 	{ "http://www.foo.com", "http://www.foo.com" },
477 	{ "Ends with http://www.foo.com", "http://www.foo.com" },
478 	{ "http://www.foo.com at start", "http://www.foo.com" },
479 	{ "http://www.foo.com.", "http://www.foo.com" },
480 	{ "http://www.foo.com/.", "http://www.foo.com/" },
481 	{ "<http://www.foo.com>", "http://www.foo.com" },
482 	{ "(http://www.foo.com)", "http://www.foo.com" },
483 	{ "http://www.foo.com, 555-9999", "http://www.foo.com" },
484 	{ "|http://www.foo.com|555-9999|", "http://www.foo.com" },
485 	{ "foo http://www.foo.com/ bar", "http://www.foo.com/" },
486 	{ "foo http://www.foo.com/index.html bar",
487 	  "http://www.foo.com/index.html" },
488 	{ "foo http://www.foo.com/q?99 bar", "http://www.foo.com/q?99" },
489 	{ "foo http://www.foo.com/;foo=bar&baz=quux bar",
490 	  "http://www.foo.com/;foo=bar&baz=quux" },
491 	{ "foo http://www.foo.com/index.html#anchor bar",
492 	  "http://www.foo.com/index.html#anchor" },
493 	{ "http://www.foo.com/index.html; foo",
494 	  "http://www.foo.com/index.html" },
495 	{ "http://www.foo.com/index.html: foo",
496 	  "http://www.foo.com/index.html" },
497 	{ "http://www.foo.com/index.html-- foo",
498 	  "http://www.foo.com/index.html" },
499 	{ "http://www.foo.com/index.html?",
500 	  "http://www.foo.com/index.html" },
501 	{ "http://www.foo.com/index.html!",
502 	  "http://www.foo.com/index.html" },
503 	{ "\"http://www.foo.com/index.html\"",
504 	  "http://www.foo.com/index.html" },
505 	{ "'http://www.foo.com/index.html'",
506 	  "http://www.foo.com/index.html" },
507 	{ "http://bob@www.foo.com/bar/baz/",
508 	  "http://bob@www.foo.com/bar/baz/" },
509 	{ "http no match http", NULL },
510 	{ "http: no match http:", NULL },
511 	{ "http:// no match http://", NULL },
512 	{ "unrecognized://bob@foo.com/path", NULL },
513 
514 	{ "src/www.c", NULL },
515 	{ "Ewwwwww.Gross.", NULL },
516 
517 };
518 gint num_url_tests = G_N_ELEMENTS (url_tests);
519 
520 gint
521 main (gint argc,
522       gchar **argv)
523 {
524 	gint i, errors = 0;
525 	gchar *html, *url, *p;
526 
527 	for (i = 0; i < num_url_tests; i++) {
528 		html = e_text_to_html (
529 			url_tests[i].text,
530 			E_TEXT_TO_HTML_CONVERT_URLS |
531 			E_TEXT_TO_HTML_CONVERT_ADDRESSES);
532 
533 		url = strstr (html, "href=\"");
534 		if (url) {
535 			url += 6;
536 			p = strchr (url, '"');
537 			if (p)
538 				*p = '\0';
539 
540 			while ((p = strstr (url, "&amp;")))
541 				memmove (p + 1, p + 5, strlen (p + 5) + 1);
542 		}
543 
544 		if ((url && (!url_tests[i].url || strcmp (url, url_tests[i].url) != 0)) ||
545 		    (!url && url_tests[i].url)) {
546 			printf (
547 				"FAILED on \"%s\" -> %s\n  (got %s)\n\n",
548 				url_tests[i].text,
549 				url_tests[i].url ? url_tests[i].url : "(nothing)",
550 				url ? url : "(nothing)");
551 			errors++;
552 		}
553 
554 		g_free (html);
555 	}
556 
557 	printf ("\n%d errors\n", errors);
558 	return errors;
559 }
560 #endif