1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30 #include "gmime-common.h"
31 #include "gmime-filter-enriched.h"
33 /* text/enriched is rfc1896 */
37 * SECTION: gmime-filter-enriched
38 * @title: GMimeFilterEnriched
39 * @short_description: Convert text/enriched or text/rtf to HTML
40 * @see_also: #GMimeFilter
42 * A #GMimeFilter used for converting text/enriched or text/rtf to HTML.
46 typedef char * (*EnrichedParamParser) (const char *inptr, size_t inlen);
48 static char *param_parse_colour (const char *inptr, size_t inlen);
49 static char *param_parse_font (const char *inptr, size_t inlen);
50 static char *param_parse_lang (const char *inptr, size_t inlen);
56 EnrichedParamParser parse_param; /* parses *and* validates the input */
58 { "bold", "<b>", FALSE, NULL },
59 { "/bold", "</b>", FALSE, NULL },
60 { "italic", "<i>", FALSE, NULL },
61 { "/italic", "</i>", FALSE, NULL },
62 { "fixed", "<tt>", FALSE, NULL },
63 { "/fixed", "</tt>", FALSE, NULL },
64 { "smaller", "<font size=-1>", FALSE, NULL },
65 { "/smaller", "</font>", FALSE, NULL },
66 { "bigger", "<font size=+1>", FALSE, NULL },
67 { "/bigger", "</font>", FALSE, NULL },
68 { "underline", "<u>", FALSE, NULL },
69 { "/underline", "</u>", FALSE, NULL },
70 { "center", "<p align=center>", FALSE, NULL },
71 { "/center", "</p>", FALSE, NULL },
72 { "flushleft", "<p align=left>", FALSE, NULL },
73 { "/flushleft", "</p>", FALSE, NULL },
74 { "flushright", "<p align=right>", FALSE, NULL },
75 { "/flushright", "</p>", FALSE, NULL },
76 { "excerpt", "<blockquote>", FALSE, NULL },
77 { "/excerpt", "</blockquote>", FALSE, NULL },
78 { "paragraph", "<p>", FALSE, NULL },
79 { "signature", "<address>", FALSE, NULL },
80 { "/signature", "</address>", FALSE, NULL },
81 { "comment", "<!-- ", FALSE, NULL },
82 { "/comment", " -->", FALSE, NULL },
83 { "np", "<hr>", FALSE, NULL },
84 { "fontfamily", "<font face=\"%s\">", TRUE, param_parse_font },
85 { "/fontfamily", "</font>", FALSE, NULL },
86 { "color", "<font color=\"%s\">", TRUE, param_parse_colour },
87 { "/color", "</font>", FALSE, NULL },
88 { "lang", "<span lang=\"%s\">", TRUE, param_parse_lang },
89 { "/lang", "</span>", FALSE, NULL },
91 /* don't handle this tag yet... */
92 { "paraindent", "<!-- ", /* TRUE */ FALSE, NULL },
93 { "/paraindent", " -->", FALSE, NULL },
95 /* as soon as we support all the tags that can have a param
96 * tag argument, these should be unnecessary, but we'll keep
97 * them anyway just in case? */
98 { "param", "<!-- ", FALSE, NULL },
99 { "/param", " -->", FALSE, NULL },
102 #define NUM_ENRICHED_TAGS (sizeof (enriched_tags) / sizeof (enriched_tags[0]))
104 static GHashTable *enriched_hash = NULL;
107 static void g_mime_filter_enriched_class_init (GMimeFilterEnrichedClass *klass);
108 static void g_mime_filter_enriched_init (GMimeFilterEnriched *filter, GMimeFilterEnrichedClass *klass);
109 static void g_mime_filter_enriched_finalize (GObject *object);
111 static GMimeFilter *filter_copy (GMimeFilter *filter);
112 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
113 char **out, size_t *outlen, size_t *outprespace);
114 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
115 char **out, size_t *outlen, size_t *outprespace);
116 static void filter_reset (GMimeFilter *filter);
119 static GMimeFilterClass *parent_class = NULL;
123 g_mime_filter_enriched_get_type (void)
125 static GType type = 0;
128 static const GTypeInfo info = {
129 sizeof (GMimeFilterEnrichedClass),
130 NULL, /* base_class_init */
131 NULL, /* base_class_finalize */
132 (GClassInitFunc) g_mime_filter_enriched_class_init,
133 NULL, /* class_finalize */
134 NULL, /* class_data */
135 sizeof (GMimeFilterEnriched),
137 (GInstanceInitFunc) g_mime_filter_enriched_init,
140 type = g_type_register_static (GMIME_TYPE_FILTER, "GMimeFilterEnriched", &info, 0);
147 g_mime_filter_enriched_class_init (GMimeFilterEnrichedClass *klass)
149 GObjectClass *object_class = G_OBJECT_CLASS (klass);
150 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
153 parent_class = g_type_class_ref (GMIME_TYPE_FILTER);
155 object_class->finalize = g_mime_filter_enriched_finalize;
157 filter_class->copy = filter_copy;
158 filter_class->reset = filter_reset;
159 filter_class->filter = filter_filter;
160 filter_class->complete = filter_complete;
162 if (!enriched_hash) {
163 enriched_hash = g_hash_table_new (g_mime_strcase_hash, g_mime_strcase_equal);
164 for (i = 0; i < NUM_ENRICHED_TAGS; i++)
165 g_hash_table_insert (enriched_hash, enriched_tags[i].enriched,
166 enriched_tags[i].html);
171 g_mime_filter_enriched_init (GMimeFilterEnriched *filter, GMimeFilterEnrichedClass *klass)
178 g_mime_filter_enriched_finalize (GObject *object)
180 G_OBJECT_CLASS (parent_class)->finalize (object);
185 filter_copy (GMimeFilter *filter)
187 GMimeFilterEnriched *enriched = (GMimeFilterEnriched *) filter;
189 return g_mime_filter_enriched_new (enriched->flags);
194 enriched_tag_needs_param (const char *tag)
198 for (i = 0; i < NUM_ENRICHED_TAGS; i++)
199 if (!g_ascii_strcasecmp (tag, enriched_tags[i].enriched))
200 return enriched_tags[i].needs_param;
207 html_tag_needs_param (const char *tag)
209 return strstr (tag, "%s") != NULL;
212 static const char *valid_colours[] = {
213 "red", "green", "blue", "yellow", "cyan", "magenta", "black", "white"
216 #define NUM_VALID_COLOURS (sizeof (valid_colours) / sizeof (valid_colours[0]))
219 param_parse_colour (const char *inptr, size_t inlen)
221 const char *inend, *end;
225 for (i = 0; i < NUM_VALID_COLOURS; i++) {
226 if (!g_ascii_strncasecmp (inptr, valid_colours[i], inlen))
227 return g_strdup (valid_colours[i]);
230 /* check for numeric r/g/b in the format: ####,####,#### */
231 if (inptr[4] != ',' || inptr[9] != ',') {
232 /* okay, mailer must have used a string name that
233 * rfc1896 did not specify? do some simple scanning
234 * action, a colour name MUST be [a-zA-Z] */
236 inend = inptr + inlen;
237 while (end < inend && ((*end >= 'a' && *end <= 'z') || (*end >= 'A' && *end <= 'Z')))
240 return g_strndup (inptr, (size_t) (end - inptr));
243 for (i = 0; i < 3; i++) {
244 v = strtoul (inptr, (char **) &end, 16);
245 if (end != inptr + 4)
249 rgb = (rgb << 8) | (v & 0xff);
254 return g_strdup_printf ("#%.6X", rgb);
258 /* default colour? */
259 return g_strdup ("black");
263 param_parse_font (const char *fontfamily, size_t inlen)
265 register const char *inptr = fontfamily;
266 const char *inend = inptr + inlen;
268 /* don't allow any of '"', '<', nor '>' */
269 while (inptr < inend && *inptr != '"' && *inptr != '<' && *inptr != '>')
272 return g_strndup (fontfamily, (size_t) (inptr - fontfamily));
276 param_parse_lang (const char *lang, size_t inlen)
278 register const char *inptr = lang;
279 const char *inend = inptr + inlen;
281 /* don't allow any of '"', '<', nor '>' */
282 while (inptr < inend && *inptr != '"' && *inptr != '<' && *inptr != '>')
285 return g_strndup (lang, (size_t) (inptr - lang));
289 param_parse (const char *enriched, const char *inptr, size_t inlen)
293 for (i = 0; i < NUM_ENRICHED_TAGS; i++) {
294 if (!g_ascii_strcasecmp (enriched, enriched_tags[i].enriched))
295 return enriched_tags[i].parse_param (inptr, inlen);
298 g_assert_not_reached ();
303 #define IS_RICHTEXT GMIME_FILTER_ENRICHED_IS_RICHTEXT
306 enriched_to_html (GMimeFilter *filter, char *in, size_t inlen, size_t prespace,
307 char **out, size_t *outlen, size_t *outprespace, gboolean flush)
309 GMimeFilterEnriched *enriched = (GMimeFilterEnriched *) filter;
310 const char *tag, *inend, *outend;
311 register const char *inptr;
312 register char *outptr;
314 g_mime_filter_set_size (filter, inlen * 2 + 6, FALSE);
318 outptr = filter->outbuf;
319 outend = filter->outbuf + filter->outsize;
323 while (inptr < inend && outptr < outend && !strchr (" <>&\n", *inptr))
324 *outptr++ = *inptr++;
326 if (outptr == outend)
329 if ((inptr + 1) >= inend)
334 while (inptr < inend && (outptr + 7) < outend && *inptr == ' ') {
335 memcpy (outptr, " ", 6);
345 if (!(enriched->flags & IS_RICHTEXT)) {
347 if (enriched->nofill > 0) {
348 if ((outptr + 4) < outend) {
349 memcpy (outptr, "<br>", 4);
355 } else if (*inptr == '\n') {
356 if ((outptr + 4) >= outend) {
361 while (inptr < inend && (outptr + 4) < outend && *inptr == '\n') {
362 memcpy (outptr, "<br>", 4);
375 if ((outptr + 4) < outend) {
376 memcpy (outptr, ">", 4);
384 if ((outptr + 5) < outend) {
385 memcpy (outptr, "&", 5);
393 if (!(enriched->flags & IS_RICHTEXT)) {
396 if ((outptr + 4) < outend) {
397 memcpy (outptr, "<", 4);
408 if ((inend - inptr) >= 3 && (outptr + 4) < outend) {
409 if (strncmp (inptr, "lt>", 3) == 0) {
410 memcpy (outptr, "<", 4);
414 } else if (strncmp (inptr, "nl>", 3) == 0) {
415 memcpy (outptr, "<br>", 4);
427 while (inptr < inend && *inptr != '>')
430 if (inptr == inend) {
435 if (!g_ascii_strncasecmp (tag, "nofill>", 7)) {
436 if ((outptr + 5) < outend) {
442 } else if (!g_ascii_strncasecmp (tag, "/nofill>", 8)) {
443 if ((outptr + 6) < outend) {
450 const char *html_tag;
455 enriched_tag = g_alloca (len + 1);
456 memcpy (enriched_tag, tag, len);
457 enriched_tag[len] = '\0';
459 html_tag = g_hash_table_lookup (enriched_hash, enriched_tag);
462 if (html_tag_needs_param (html_tag)) {
466 while (inptr < inend && *inptr != '<')
469 #define PARAM_TAG_MIN_LEN (sizeof ("<param>") + sizeof ("</param>") - 1)
470 if (inptr == inend || (size_t) (inend - inptr) <= PARAM_TAG_MIN_LEN) {
475 if (g_ascii_strncasecmp (inptr, "<param>", 7) != 0) {
476 /* ignore the enriched command tag... */
484 while (inptr < inend && *inptr != '<')
487 if (inptr == inend || (inend - inptr) <= 8) {
492 if (g_ascii_strncasecmp (inptr, "</param>", 8) != 0) {
493 /* ignore the enriched command tag... */
499 param = param_parse (enriched_tag, start, len);
500 len = strlen (param);
504 len += strlen (html_tag);
506 if ((outptr + len) < outend) {
507 outptr += g_snprintf (outptr, len, html_tag, param);
515 len = strlen (html_tag);
516 if ((outptr + len) < outend) {
517 memcpy (outptr, html_tag, len);
533 } while (inptr < inend);
537 /* the reason we ignore @flush here is because if there isn't
538 enough input to parse a tag, then there's nothing we can
542 g_mime_filter_backup (filter, inptr, (unsigned) (inend - inptr));
544 *out = filter->outbuf;
545 *outlen = outptr - filter->outbuf;
546 *outprespace = filter->outpre;
555 grow = (inend - inptr) * 2 + 20;
556 offset = outptr - filter->outbuf;
557 g_mime_filter_set_size (filter, filter->outsize + grow, TRUE);
558 outend = filter->outbuf + filter->outsize;
559 outptr = filter->outbuf + offset;
563 g_mime_filter_backup (filter, inptr, (unsigned) (inend - inptr));
566 *out = filter->outbuf;
567 *outlen = outptr - filter->outbuf;
568 *outprespace = filter->outpre;
572 filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
573 char **out, size_t *outlen, size_t *outprespace)
575 enriched_to_html (filter, in, len, prespace, out, outlen, outprespace, FALSE);
579 filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
580 char **out, size_t *outlen, size_t *outprespace)
582 enriched_to_html (filter, in, len, prespace, out, outlen, outprespace, TRUE);
586 filter_reset (GMimeFilter *filter)
588 GMimeFilterEnriched *enriched = (GMimeFilterEnriched *) filter;
590 enriched->nofill = 0;
595 * g_mime_filter_enriched_new:
598 * Creates a new GMimeFilterEnriched object.
600 * Returns: a new GMimeFilter object.
603 g_mime_filter_enriched_new (guint32 flags)
605 GMimeFilterEnriched *new;
607 new = g_object_newv (GMIME_TYPE_FILTER_ENRICHED, 0, NULL);
610 return (GMimeFilter *) new;