1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2002 Ximian, Inc. (www.ximian.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
31 #include "string-utils.h"
33 #include "camel-mime-filter-enriched.h"
35 /* text/enriched is rfc1896 */
37 typedef char * (*EnrichedParamParser) (const char *inptr, int inlen);
39 static char *param_parse_colour (const char *inptr, int inlen);
40 static char *param_parse_font (const char *inptr, int inlen);
41 static char *param_parse_lang (const char *inptr, int inlen);
47 EnrichedParamParser parse_param; /* parses *and* validates the input */
49 { "bold", "<b>", FALSE, NULL },
50 { "/bold", "</b>", FALSE, NULL },
51 { "italic", "<i>", FALSE, NULL },
52 { "/italic", "</i>", FALSE, NULL },
53 { "fixed", "<tt>", FALSE, NULL },
54 { "/fixed", "</tt>", FALSE, NULL },
55 { "smaller", "<font size=-1>", FALSE, NULL },
56 { "/smaller", "</font>", FALSE, NULL },
57 { "bigger", "<font size=+1>", FALSE, NULL },
58 { "/bigger", "</font>", FALSE, NULL },
59 { "underline", "<u>", FALSE, NULL },
60 { "/underline", "</u>", FALSE, NULL },
61 { "center", "<p align=center>", FALSE, NULL },
62 { "/center", "</p>", FALSE, NULL },
63 { "flushleft", "<p align=left>", FALSE, NULL },
64 { "/flushleft", "</p>", FALSE, NULL },
65 { "flushright", "<p align=right>", FALSE, NULL },
66 { "/flushright", "</p>", FALSE, NULL },
67 { "excerpt", "<blockquote>", FALSE, NULL },
68 { "/excerpt", "</blockquote>", FALSE, NULL },
69 { "paragraph", "<p>", FALSE, NULL },
70 { "signature", "<address>", FALSE, NULL },
71 { "/signature", "</address>", FALSE, NULL },
72 { "comment", "<!-- ", FALSE, NULL },
73 { "/comment", " -->", FALSE, NULL },
74 { "np", "<hr>", FALSE, NULL },
75 { "fontfamily", "<font face=\"%s\">", TRUE, param_parse_font },
76 { "/fontfamily", "</font>", FALSE, NULL },
77 { "color", "<font color=\"%s\">", TRUE, param_parse_colour },
78 { "/color", "</font>", FALSE, NULL },
79 { "lang", "<span lang=\"%s\">", TRUE, param_parse_lang },
80 { "/lang", "</span>", FALSE, NULL },
82 /* don't handle this tag yet... */
83 { "paraindent", "<!-- ", /* TRUE */ FALSE, NULL },
84 { "/paraindent", " -->", FALSE, NULL },
86 /* as soon as we support all the tags that can have a param
87 * tag argument, these should be unnecessary, but we'll keep
88 * them anyway just in case? */
89 { "param", "<!-- ", FALSE, NULL },
90 { "/param", " -->", FALSE, NULL },
93 #define NUM_ENRICHED_TAGS (sizeof (enriched_tags) / sizeof (enriched_tags[0]))
95 static GHashTable *enriched_hash = NULL;
98 static void camel_mime_filter_enriched_class_init (CamelMimeFilterEnrichedClass *klass);
99 static void camel_mime_filter_enriched_init (CamelMimeFilterEnriched *filter);
100 static void camel_mime_filter_enriched_finalize (CamelObject *obj);
102 static void filter_filter (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
103 char **out, size_t *outlen, size_t *outprespace);
104 static void filter_complete (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
105 char **out, size_t *outlen, size_t *outprespace);
106 static void filter_reset (CamelMimeFilter *filter);
109 static CamelMimeFilterClass *parent_class = NULL;
113 camel_mime_filter_enriched_get_type (void)
115 static CamelType type = CAMEL_INVALID_TYPE;
117 if (type == CAMEL_INVALID_TYPE) {
118 type = camel_type_register (camel_mime_filter_get_type (),
119 "CamelMimeFilterEnriched",
120 sizeof (CamelMimeFilterEnriched),
121 sizeof (CamelMimeFilterEnrichedClass),
122 (CamelObjectClassInitFunc) camel_mime_filter_enriched_class_init,
124 (CamelObjectInitFunc) camel_mime_filter_enriched_init,
125 (CamelObjectFinalizeFunc) camel_mime_filter_enriched_finalize);
132 camel_mime_filter_enriched_class_init (CamelMimeFilterEnrichedClass *klass)
134 CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
137 parent_class = CAMEL_MIME_FILTER_CLASS (camel_mime_filter_get_type ());
139 filter_class->reset = filter_reset;
140 filter_class->filter = filter_filter;
141 filter_class->complete = filter_complete;
143 if (!enriched_hash) {
144 enriched_hash = g_hash_table_new (g_strcase_hash, g_strcase_equal);
145 for (i = 0; i < NUM_ENRICHED_TAGS; i++)
146 g_hash_table_insert (enriched_hash, enriched_tags[i].enriched,
147 enriched_tags[i].html);
152 camel_mime_filter_enriched_finalize (CamelObject *obj)
158 camel_mime_filter_enriched_init (CamelMimeFilterEnriched *filter)
167 enriched_tag_needs_param (const char *tag)
171 for (i = 0; i < NUM_ENRICHED_TAGS; i++)
172 if (!strcasecmp (tag, enriched_tags[i].enriched))
173 return enriched_tags[i].needs_param;
180 html_tag_needs_param (const char *tag)
182 return strstr (tag, "%s") != NULL;
185 static const char *valid_colours[] = {
186 "red", "green", "blue", "yellow", "cyan", "magenta", "black", "white"
189 #define NUM_VALID_COLOURS (sizeof (valid_colours) / sizeof (valid_colours[0]))
192 param_parse_colour (const char *inptr, int inlen)
194 const char *inend, *end;
199 for (i = 0; i < NUM_VALID_COLOURS; i++) {
200 if (!strncasecmp (inptr, valid_colours[i], inlen))
201 return g_strdup (valid_colours[i]);
204 /* check for numeric r/g/b in the format: ####,####,#### */
205 if (inptr[4] != ',' || inptr[9] != ',') {
206 /* okay, mailer must have used a string name that
207 * rfc1896 did not specify? do some simple scanning
208 * action, a colour name MUST be [a-zA-Z] */
210 inend = inptr + inlen;
211 while (end < inend && ((*end >= 'a' && *end <= 'z') || (*end >= 'A' && *end <= 'Z')))
214 return g_strndup (inptr, end - inptr);
217 for (i = 0; i < 3; i++) {
218 v = strtoul (inptr, (char **) &end, 16);
219 if (end != inptr + 4)
223 rgb = (rgb << 8) | (v & 0xff);
228 return g_strdup_printf ("#%.6X", rgb);
232 /* default colour? */
233 return g_strdup ("black");
237 param_parse_font (const char *fontfamily, int inlen)
239 register const char *inptr = fontfamily;
240 const char *inend = inptr + inlen;
242 /* don't allow any of '"', '<', nor '>' */
243 while (inptr < inend && *inptr != '"' && *inptr != '<' && *inptr != '>')
246 return g_strndup (fontfamily, inptr - fontfamily);
250 param_parse_lang (const char *lang, int inlen)
252 register const char *inptr = lang;
253 const char *inend = inptr + inlen;
255 /* don't allow any of '"', '<', nor '>' */
256 while (inptr < inend && *inptr != '"' && *inptr != '<' && *inptr != '>')
259 return g_strndup (lang, inptr - lang);
263 param_parse (const char *enriched, const char *inptr, int inlen)
267 for (i = 0; i < NUM_ENRICHED_TAGS; i++) {
268 if (!strcasecmp (enriched, enriched_tags[i].enriched))
269 return enriched_tags[i].parse_param (inptr, inlen);
272 g_assert_not_reached ();
277 #define IS_RICHTEXT CAMEL_MIME_FILTER_ENRICHED_IS_RICHTEXT
280 enriched_to_html (CamelMimeFilter *filter, char *in, size_t inlen, size_t prespace,
281 char **out, size_t *outlen, size_t *outprespace, gboolean flush)
283 CamelMimeFilterEnriched *enriched = (CamelMimeFilterEnriched *) filter;
284 const char *tag, *inend, *outend;
285 register const char *inptr;
286 register char *outptr;
288 camel_mime_filter_set_size (filter, inlen * 2 + 6, FALSE);
292 outptr = filter->outbuf;
293 outend = filter->outbuf + filter->outsize;
297 while (inptr < inend && outptr < outend && !strchr (" <>&\n", *inptr))
298 *outptr++ = *inptr++;
300 if (outptr == outend)
303 if ((inptr + 1) >= inend)
308 while (inptr < inend && (outptr + 7) < outend && *inptr == ' ') {
309 memcpy (outptr, " ", 6);
319 if (!(enriched->flags & IS_RICHTEXT) && enriched->nofill <= 0) {
321 while (inptr < inend && (outptr + 4) < outend && *inptr == '\n') {
322 memcpy (outptr, "<br>", 4);
332 if ((outptr + 4) < outend) {
333 memcpy (outptr, ">", 4);
341 if ((outptr + 5) < outend) {
342 memcpy (outptr, "&", 5);
350 if (!(enriched->flags & IS_RICHTEXT)) {
353 if ((outptr + 4) < outend) {
354 memcpy (outptr, "<", 4);
365 if ((inend - inptr) >= 3 && (outptr + 4) < outend) {
366 if (strncmp (inptr, "lt>", 3) == 0) {
367 memcpy (outptr, "<", 4);
371 } else if (strncmp (inptr, "nl>", 3) == 0) {
372 memcpy (outptr, "<br>", 4);
384 while (inptr < inend && *inptr != '>')
387 if (inptr == inend) {
392 if (!strncasecmp (tag, "nofill>", 7)) {
393 if ((outptr + 5) < outend) {
394 memcpy (outptr, "<pre>", 5);
401 } else if (!strncasecmp (tag, "/nofill>", 8)) {
402 if ((outptr + 6) < outend) {
403 memcpy (outptr, "</pre>", 6);
411 const char *html_tag;
416 enriched_tag = g_alloca (len + 1);
417 memcpy (enriched_tag, tag, len);
418 enriched_tag[len] = '\0';
420 html_tag = g_hash_table_lookup (enriched_hash, enriched_tag);
423 if (html_tag_needs_param (html_tag)) {
427 while (inptr < inend && *inptr != '<')
430 #define PARAM_TAG_MIN_LEN (sizeof ("<param>") + sizeof ("</param>") - 1)
431 if (inptr == inend || (inend - inptr) <= PARAM_TAG_MIN_LEN) {
436 if (strncasecmp (inptr, "<param>", 7) != 0) {
437 /* ignore the enriched command tag... */
445 while (inptr < inend && *inptr != '<')
448 if (inptr == inend || (inend - inptr) <= 8) {
453 if (strncasecmp (inptr, "</param>", 8) != 0) {
454 /* ignore the enriched command tag... */
460 param = param_parse (enriched_tag, start, len);
461 len = strlen (param);
465 len += strlen (html_tag);
467 if ((outptr + len) < outend) {
468 outptr += snprintf (outptr, len, html_tag, param);
476 len = strlen (html_tag);
477 if ((outptr + len) < outend) {
478 memcpy (outptr, html_tag, len);
494 } while (inptr < inend);
498 /* the reason we ignore @flush here is because if there isn't
499 enough input to parse a tag, then there's nothing we can
503 camel_mime_filter_backup (filter, inptr, (unsigned) (inend - inptr));
505 *out = filter->outbuf;
506 *outlen = outptr - filter->outbuf;
507 *outprespace = filter->outpre;
516 grow = (inend - inptr) * 2 + 20;
517 offset = outptr - filter->outbuf;
518 camel_mime_filter_set_size (filter, filter->outsize + grow, TRUE);
519 outend = filter->outbuf + filter->outsize;
520 outptr = filter->outbuf + offset;
524 camel_mime_filter_backup (filter, inptr, (unsigned) (inend - inptr));
527 *out = filter->outbuf;
528 *outlen = outptr - filter->outbuf;
529 *outprespace = filter->outpre;
533 filter_filter (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
534 char **out, size_t *outlen, size_t *outprespace)
536 enriched_to_html (filter, in, len, prespace, out, outlen, outprespace, FALSE);
540 filter_complete (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
541 char **out, size_t *outlen, size_t *outprespace)
543 enriched_to_html (filter, in, len, prespace, out, outlen, outprespace, TRUE);
547 filter_reset (CamelMimeFilter *filter)
549 CamelMimeFilterEnriched *enriched = (CamelMimeFilterEnriched *) filter;
551 enriched->nofill = 0;
556 * camel_mime_filter_enriched_new:
559 * Creates a new CamelMimeFilterEnriched object.
561 * Returns a new CamelMimeFilter object.
564 camel_mime_filter_enriched_new (guint32 flags)
566 CamelMimeFilterEnriched *new;
568 new = (CamelMimeFilterEnriched *) camel_object_new (CAMEL_TYPE_MIME_FILTER_ENRICHED);
571 return CAMEL_MIME_FILTER (new);