1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
|
/*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see <http://www.gnu.org/licenses/>
*
*
* Authors:
* Jeffrey Stedfast <fejj@ximian.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <glib/gi18n.h>
#include <gconf/gconf-client.h>
#include "em-stripsig-filter.h"
#include "em-format-quote.h"
struct _EMFormatQuotePrivate {
gint dummy;
};
static void emfq_builtin_init (EMFormatQuoteClass *efhc);
static gpointer parent_class;
static void
emfq_finalize (GObject *object)
{
EMFormatQuote *emfq =(EMFormatQuote *) object;
if (emfq->stream)
g_object_unref (emfq->stream);
g_free (emfq->credits);
/* Chain up to parent's finalize() method. */
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
emfq_format_clone (EMFormat *emf,
CamelFolder *folder,
const gchar *uid,
CamelMimeMessage *msg,
EMFormat *src,
GCancellable *cancellable)
{
EMFormatQuote *emfq = (EMFormatQuote *) emf;
const EMFormatHandler *handle;
GConfClient *gconf;
/* Chain up to parent's format_clone() method. */
EM_FORMAT_CLASS (parent_class)->format_clone (
emf, folder, uid, msg, src, cancellable);
gconf = gconf_client_get_default ();
camel_stream_reset (emfq->stream, NULL);
if (gconf_client_get_bool(gconf, "/apps/evolution/mail/composer/top_signature", NULL))
camel_stream_printf (emfq->stream, "<br>\n");
g_object_unref (gconf);
handle = em_format_find_handler(emf, "x-evolution/message/prefix");
if (handle)
handle->handler (
emf, emfq->stream,
CAMEL_MIME_PART (msg),
handle, cancellable, FALSE);
handle = em_format_find_handler(emf, "x-evolution/message/rfc822");
if (handle)
handle->handler (
emf, emfq->stream,
CAMEL_MIME_PART (msg),
handle, cancellable, FALSE);
camel_stream_flush (emfq->stream, cancellable, NULL);
g_signal_emit_by_name(emf, "complete");
}
static void
emfq_format_error (EMFormat *emf,
CamelStream *stream,
const gchar *errmsg)
{
/* Nothing to do. */
}
static void
emfq_format_source (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
GCancellable *cancellable)
{
CamelStream *filtered_stream;
CamelMimeFilter *html_filter;
filtered_stream = camel_stream_filter_new (stream);
html_filter = camel_mime_filter_tohtml_new (
CAMEL_MIME_FILTER_TOHTML_CONVERT_NL |
CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES |
CAMEL_MIME_FILTER_TOHTML_ESCAPE_8BIT, 0);
camel_stream_filter_add (
CAMEL_STREAM_FILTER (filtered_stream), html_filter);
g_object_unref (html_filter);
em_format_format_text (
emf, filtered_stream,
CAMEL_DATA_WRAPPER (part), cancellable);
g_object_unref (filtered_stream);
}
static void
emfq_format_attachment (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const gchar *mime_type,
const EMFormatHandler *handle,
GCancellable *cancellable)
{
EMFormatQuote *emfq = EM_FORMAT_QUOTE (emf);
gchar *text, *html;
if (!em_format_is_inline (emf, emf->part_id->str, part, handle))
return;
camel_stream_write_string (
stream, "<table border=1 cellspacing=0 cellpadding=0>"
"<tr><td><font size=-1>\n", cancellable, NULL);
/* output some info about it */
text = em_format_describe_part (part, mime_type);
html = camel_text_to_html (
text, emfq->text_html_flags &
CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS, 0);
camel_stream_write_string (stream, html, cancellable, NULL);
g_free (html);
g_free (text);
camel_stream_write_string (
stream, "</font></td></tr></table>", cancellable, NULL);
handle->handler (emf, stream, part, handle, cancellable, FALSE);
}
static void
emfq_base_init (EMFormatQuoteClass *class)
{
emfq_builtin_init (class);
}
static void
emfq_class_init (EMFormatQuoteClass *class)
{
GObjectClass *object_class;
EMFormatClass *format_class;
parent_class = g_type_class_peek_parent (class);
g_type_class_add_private (class, sizeof (EMFormatQuotePrivate));
object_class = G_OBJECT_CLASS (class);
object_class->finalize = emfq_finalize;
format_class = EM_FORMAT_CLASS (class);
format_class->format_clone = emfq_format_clone;
format_class->format_error = emfq_format_error;
format_class->format_source = emfq_format_source;
format_class->format_attachment = emfq_format_attachment;
}
static void
emfq_init (EMFormatQuote *emfq)
{
/* we want to convert url's etc */
emfq->text_html_flags =
CAMEL_MIME_FILTER_TOHTML_PRE |
CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS |
CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES;
}
GType
em_format_quote_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static const GTypeInfo type_info = {
sizeof (EMFormatQuoteClass),
(GBaseInitFunc) emfq_base_init,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) emfq_class_init,
(GClassFinalizeFunc) NULL,
NULL, /* class_data */
sizeof (EMFormatQuote),
0, /* n_preallocs */
(GInstanceInitFunc) emfq_init,
NULL /* value_table */
};
type = g_type_register_static (
EM_TYPE_FORMAT, "EMFormatQuote", &type_info, 0);
}
return type;
}
EMFormatQuote *
em_format_quote_new (const gchar *credits,
CamelStream *stream,
EMFormatQuoteFlags flags)
{
EMFormatQuote *emfq;
g_return_val_if_fail (CAMEL_IS_STREAM (stream), NULL);
emfq = g_object_new (EM_TYPE_FORMAT_QUOTE, NULL);
emfq->credits = g_strdup (credits);
emfq->stream = g_object_ref (stream);
emfq->flags = flags;
return emfq;
}
static void
emfq_format_text_header (EMFormatQuote *emfq,
CamelStream *stream,
const gchar *label,
const gchar *value,
guint32 flags,
gint is_html)
{
const gchar *html;
gchar *mhtml = NULL;
if (value == NULL)
return;
while (*value == ' ')
value++;
if (!is_html)
html = mhtml = camel_text_to_html (value, 0, 0);
else
html = value;
if (flags & EM_FORMAT_HEADER_BOLD)
camel_stream_printf (stream, "<b>%s</b>: %s<br>", label, html);
else
camel_stream_printf (stream, "%s: %s<br>", label, html);
g_free (mhtml);
}
static const gchar *addrspec_hdrs[] = {
"Sender", "From", "Reply-To", "To", "Cc", "Bcc",
"Resent-Sender", "Resent-from", "Resent-Reply-To",
"Resent-To", "Resent-cc", "Resent-Bcc", NULL
};
#if 0
/* FIXME: include Sender and Resent-* headers too? */
/* For Translators only: The following strings are
* used in the header table in the preview pane. */
static gchar *i18n_hdrs[] = {
N_("From"), N_("Reply-To"), N_("To"), N_("Cc"), N_("Bcc")
};
#endif
static void
emfq_format_address (GString *out, struct _camel_header_address *a)
{
guint32 flags = CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES;
gchar *name, *mailto, *addr;
while (a) {
if (a->name)
name = camel_text_to_html (a->name, flags, 0);
else
name = NULL;
switch (a->type) {
case CAMEL_HEADER_ADDRESS_NAME:
if (name && *name) {
gchar *real, *mailaddr;
g_string_append_printf (out, "%s <", name);
/* rfc2368 for mailto syntax and url encoding extras */
if ((real = camel_header_encode_phrase ((guchar *)a->name))) {
mailaddr = g_strdup_printf ("%s <%s>", real, a->v.addr);
g_free (real);
mailto = camel_url_encode (mailaddr, "?=&()");
g_free (mailaddr);
} else {
mailto = camel_url_encode (a->v.addr, "?=&()");
}
} else {
mailto = camel_url_encode (a->v.addr, "?=&()");
}
addr = camel_text_to_html (a->v.addr, flags, 0);
g_string_append_printf (out, "<a href=\"mailto:%s\">%s</a>", mailto, addr);
g_free (mailto);
g_free (addr);
if (name && *name)
g_string_append (out, ">");
break;
case CAMEL_HEADER_ADDRESS_GROUP:
g_string_append_printf (out, "%s: ", name);
emfq_format_address (out, a->v.members);
g_string_append_printf (out, ";");
break;
default:
g_warning ("Invalid address type");
break;
}
g_free (name);
a = a->next;
if (a)
g_string_append (out, ", ");
}
}
static void
canon_header_name (gchar *name)
{
gchar *inptr = name;
/* canonicalise the header name... first letter is
* capitalised and any letter following a '-' also gets
* capitalised */
if (g_ascii_islower (*inptr))
*inptr = g_ascii_toupper (*inptr);
inptr++;
while (*inptr) {
if (inptr[-1] == '-' && g_ascii_islower (*inptr))
*inptr = g_ascii_toupper (*inptr);
else if (g_ascii_isupper (*inptr))
*inptr = g_ascii_tolower (*inptr);
inptr++;
}
}
static void
emfq_format_header (EMFormat *emf,
CamelStream *stream,
CamelMedium *part,
const gchar *namein,
guint32 flags,
const gchar *charset)
{
CamelMimeMessage *msg = (CamelMimeMessage *) part;
EMFormatQuote *emfq = (EMFormatQuote *) emf;
gchar *name, *buf, *value = NULL;
const gchar *txt, *label;
gboolean addrspec = FALSE;
gint is_html = FALSE;
gint i;
name = g_alloca (strlen (namein) + 1);
strcpy (name, namein);
canon_header_name (name);
/* Never quote Bcc headers */
if (g_str_equal (name, "Bcc") || g_str_equal (name, "Resent-Bcc"))
return;
for (i = 0; addrspec_hdrs[i]; i++) {
if (!strcmp (name, addrspec_hdrs[i])) {
addrspec = TRUE;
break;
}
}
label = _(name);
if (addrspec) {
struct _camel_header_address *addrs;
GString *html;
if (!(txt = camel_medium_get_header (part, name)))
return;
buf = camel_header_unfold (txt);
addrs = camel_header_address_decode (
txt, emf->charset ?
emf->charset : emf->default_charset);
if (addrs == NULL) {
g_free (buf);
return;
}
g_free (buf);
html = g_string_new ("");
emfq_format_address (html, addrs);
camel_header_address_unref (addrs);
txt = value = html->str;
g_string_free (html, FALSE);
flags |= EM_FORMAT_HEADER_BOLD;
is_html = TRUE;
} else if (!strcmp (name, "Subject")) {
txt = camel_mime_message_get_subject (msg);
label = _("Subject");
flags |= EM_FORMAT_HEADER_BOLD;
} else if (!strcmp (name, "X-Evolution-Mailer")) { /* pseudo-header */
if (!(txt = camel_medium_get_header (part, "x-mailer")))
if (!(txt = camel_medium_get_header (part, "user-agent")))
if (!(txt = camel_medium_get_header (part, "x-newsreader")))
if (!(txt = camel_medium_get_header (part, "x-mimeole")))
return;
txt = value = camel_header_format_ctext (txt, charset);
label = _("Mailer");
flags |= EM_FORMAT_HEADER_BOLD;
} else if (!strcmp (name, "Date") || !strcmp (name, "Resent-Date")) {
if (!(txt = camel_medium_get_header (part, name)))
return;
flags |= EM_FORMAT_HEADER_BOLD;
} else {
txt = camel_medium_get_header (part, name);
buf = camel_header_unfold (txt);
txt = value = camel_header_decode_string (txt, charset);
g_free (buf);
}
emfq_format_text_header (emfq, stream, label, txt, flags, is_html);
g_free (value);
}
static void
emfq_format_headers (EMFormatQuote *emfq,
CamelStream *stream,
CamelMedium *part)
{
EMFormat *emf = (EMFormat *) emfq;
CamelContentType *ct;
const gchar *charset;
GList *link;
if (!part)
return;
ct = camel_mime_part_get_content_type ((CamelMimePart *) part);
charset = camel_content_type_param (ct, "charset");
charset = camel_iconv_charset_name (charset);
/* dump selected headers */
link = g_queue_peek_head_link (&emf->header_list);
while (link != NULL) {
EMFormatHeader *h = link->data;
emfq_format_header (
emf, stream, part, h->name, h->flags, charset);
link = g_list_next (link);
}
camel_stream_printf(stream, "<br>\n");
}
static void
emfq_format_message_prefix (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const EMFormatHandler *info,
GCancellable *cancellable,
gboolean is_fallback)
{
EMFormatQuote *emfq = (EMFormatQuote *) emf;
if (emfq->credits)
camel_stream_printf(stream, "%s<br>\n", emfq->credits);
}
static void
emfq_format_message (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const EMFormatHandler *info,
GCancellable *cancellable,
gboolean is_fallback)
{
EMFormatQuote *emfq = (EMFormatQuote *) emf;
if (emfq->flags & EM_FORMAT_QUOTE_CITE)
camel_stream_printf (
stream, "<!--+GtkHTML:<DATA class=\"ClueFlow\" "
"key=\"orig\" value=\"1\">-->\n"
"<blockquote type=cite>\n");
if (((CamelMimePart *)emf->message) != part) {
camel_stream_printf (
stream, "%s</br>\n",
_("-------- Forwarded Message --------"));
emfq_format_headers (emfq, stream, (CamelMedium *)part);
} else if (emfq->flags & EM_FORMAT_QUOTE_HEADERS)
emfq_format_headers (emfq, stream, (CamelMedium *)part);
em_format_part (emf, stream, part, cancellable);
if (emfq->flags & EM_FORMAT_QUOTE_CITE)
camel_stream_write_string (
stream, "</blockquote><!--+GtkHTML:"
"<DATA class=\"ClueFlow\" clear=\"orig\">-->",
cancellable, NULL);
}
static void
emfq_text_plain (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const EMFormatHandler *info,
GCancellable *cancellable,
gboolean is_fallback)
{
EMFormatQuote *emfq = EM_FORMAT_QUOTE (emf);
CamelStream *filtered_stream;
CamelMimeFilter *html_filter;
CamelMimeFilter *sig_strip;
CamelContentType *type;
const gchar *format;
guint32 rgb = 0x737373, flags;
if (!part)
return;
flags = emfq->text_html_flags;
/* Check for RFC 2646 flowed text. */
type = camel_mime_part_get_content_type (part);
if (camel_content_type_is(type, "text", "plain")
&& (format = camel_content_type_param(type, "format"))
&& !g_ascii_strcasecmp(format, "flowed"))
flags |= CAMEL_MIME_FILTER_TOHTML_FORMAT_FLOWED;
filtered_stream = camel_stream_filter_new (stream);
if ((emfq->flags & EM_FORMAT_QUOTE_KEEP_SIG) == 0) {
sig_strip = em_stripsig_filter_new ();
camel_stream_filter_add (
CAMEL_STREAM_FILTER (filtered_stream), sig_strip);
g_object_unref (sig_strip);
}
html_filter = camel_mime_filter_tohtml_new (flags, rgb);
camel_stream_filter_add (
CAMEL_STREAM_FILTER (filtered_stream), html_filter);
g_object_unref (html_filter);
em_format_format_text (
EM_FORMAT (emfq), filtered_stream,
CAMEL_DATA_WRAPPER (part), cancellable);
camel_stream_flush (filtered_stream, cancellable, NULL);
g_object_unref (filtered_stream);
}
static void
emfq_text_enriched (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const EMFormatHandler *info,
GCancellable *cancellable,
gboolean is_fallback)
{
CamelStream *filtered_stream;
CamelMimeFilter *enriched;
guint32 flags = 0;
if (g_strcmp0 (info->mime_type, "text/richtext") == 0) {
flags = CAMEL_MIME_FILTER_ENRICHED_IS_RICHTEXT;
camel_stream_write_string (
stream, "\n<!-- text/richtext -->\n",
cancellable, NULL);
} else {
camel_stream_write_string (
stream, "\n<!-- text/enriched -->\n",
cancellable, NULL);
}
enriched = camel_mime_filter_enriched_new (flags);
filtered_stream = camel_stream_filter_new (stream);
camel_stream_filter_add (
CAMEL_STREAM_FILTER (filtered_stream), enriched);
g_object_unref (enriched);
camel_stream_write_string (stream, "<br><hr><br>", cancellable, NULL);
em_format_format_text (
emf, filtered_stream, CAMEL_DATA_WRAPPER (part), cancellable);
g_object_unref (filtered_stream);
}
static void
emfq_text_html (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const EMFormatHandler *info,
GCancellable *cancellable,
gboolean is_fallback)
{
camel_stream_write_string (
stream, "\n<!-- text/html -->\n", cancellable, NULL);
em_format_format_text (
emf, stream, (CamelDataWrapper *)part, cancellable);
}
static void
emfq_ignore (EMFormat *emf,
CamelStream *stream,
CamelMimePart *part,
const EMFormatHandler *info,
GCancellable *cancellable,
gboolean is_fallback)
{
/* NOOP */
}
static EMFormatHandler type_builtin_table[] = {
{ (gchar *) "text/plain", emfq_text_plain },
{ (gchar *) "text/enriched", emfq_text_enriched },
{ (gchar *) "text/richtext", emfq_text_enriched },
{ (gchar *) "text/html", emfq_text_html },
{ (gchar *) "text/*", emfq_text_plain },
{ (gchar *) "message/external-body", emfq_ignore },
{ (gchar *) "multipart/appledouble", emfq_ignore },
/* internal evolution types */
{ (gchar *) "x-evolution/evolution-rss-feed", emfq_text_html },
{ (gchar *) "x-evolution/message/rfc822", emfq_format_message },
{ (gchar *) "x-evolution/message/prefix", emfq_format_message_prefix },
};
static void
emfq_builtin_init (EMFormatQuoteClass *efhc)
{
gint ii;
for (ii = 0; ii < G_N_ELEMENTS (type_builtin_table); ii++)
em_format_class_add_handler (
EM_FORMAT_CLASS (efhc), &type_builtin_table[ii]);
}
|