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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Author :
* Matt Loper <matt@helixcode.com>
*
* Copyright 2000, Helix Code, Inc. (http://www.helixcode.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#include <config.h>
#include "mail-format.h"
#include "camel/hash-table-utils.h"
#include <libgnome/libgnome.h>
#include <ctype.h> /* for isprint */
#include <string.h> /* for strstr */
static void handle_text_plain (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_text_html (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_image (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_vcard (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_mime_part (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_multipart_mixed (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_multipart_related (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_multipart_alternative(CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static void handle_unknown_type (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
/* encodes some characters into their 'escaped' version;
* so '<' turns into '<', and '"' turns into '"' */
static gchar *text_to_html (const guchar *input,
guint len,
guint *encoded_len_return,
gboolean add_pre,
gboolean convert_newlines_to_br);
/* writes the header info for a mime message into an html stream */
static void write_header_info_to_stream (CamelMimeMessage* mime_message,
GtkHTMLStreamHandle *stream);
/* dispatch html printing via mimetype */
static void call_handler_function (CamelDataWrapper *wrapper,
gchar *mimetype_whole,
gchar *mimetype_main,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
#if 0
/**
* camel_formatter_wrapper_to_html:
* @formatter: the camel formatter object
* @data_wrapper: the data wrapper
* @stream: byte stream where data will be written
*
* Writes a CamelDataWrapper out, as html, into a stream passed in as
* a parameter.
**/
void camel_formatter_wrapper_to_html (CamelFormatter* formatter,
CamelDataWrapper* data_wrapper,
CamelStream* stream_out)
{
CamelFormatterPrivate* fmt = formatter->priv;
gchar *mimetype_whole =
g_strdup_printf ("%s/%s",
data_wrapper->mime_type->type,
data_wrapper->mime_type->subtype);
debug ("camel_formatter_wrapper_to_html: entered\n");
g_assert (formatter && data_wrapper && stream_out);
/* give the root CamelDataWrapper and the stream to the formatter */
initialize_camel_formatter (formatter, data_wrapper, stream_out);
if (stream_out) {
/* write everything to the stream */
camel_stream_write_string (
fmt->stream, "<html><body bgcolor=\"white\">\n");
call_handler_function (
formatter,
data_wrapper,
mimetype_whole,
data_wrapper->mime_type->type);
camel_stream_write_string (fmt->stream, "\n</body></html>\n");
}
g_free (mimetype_whole);
}
#endif
/**
* mail_format_mime_message:
* @mime_message: the input mime message
* @header_stream: HTML stream to write headers to
* @body_stream: HTML stream to write data to
*
* Writes a CamelMimeMessage out, as html, into streams passed in as
* a parameter. Either stream may be #NULL.
**/
void
mail_format_mime_message (CamelMimeMessage *mime_message,
GtkHTMLStreamHandle *header_stream,
GtkHTMLStreamHandle *body_stream)
{
g_return_if_fail (CAMEL_IS_MIME_MESSAGE (mime_message));
/* Write the headers fields out as HTML to the header stream. */
if (header_stream)
write_header_info_to_stream (mime_message, header_stream);
/* Write the contents of the MIME message to the body stream. */
if (body_stream) {
mail_write_html (body_stream, "<html><body>\n");
call_handler_function (CAMEL_DATA_WRAPPER (mime_message),
"message/rfc822",
"message",
body_stream,
CAMEL_DATA_WRAPPER (mime_message));
mail_write_html (body_stream, "\n</body></html>\n");
}
}
/* We're maintaining a hashtable of mimetypes -> functions;
* Those functions have the following signature...
*/
typedef void (*mime_handler_fn) (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root);
static gchar*
lookup_unique_id (CamelDataWrapper *root, CamelDataWrapper *child)
{
/* ** FIXME : replace this with a string representing
the location of the objetc in the tree */
/* TODO: assert our return value != NULL */
gchar *temp_hack_uid;
temp_hack_uid = g_strdup_printf ("%p", camel_data_wrapper_get_output_stream (child));
return temp_hack_uid;
}
static GHashTable *mime_function_table;
/* This tries to create a tag, given a mimetype and the child of a
* mime message. It can return NULL if it can't match the mimetype to
* a bonobo object.
*/
static gchar *
get_bonobo_tag_for_object (CamelDataWrapper *wrapper,
gchar *mimetype, CamelDataWrapper *root)
{
char *uid = lookup_unique_id (root, wrapper);
const char *goad_id = gnome_mime_get_value (mimetype,
"bonobo-goad-id");
if (goad_id) {
return g_strdup_printf ("<object classid=\"%s\"> "
"<param name=\"uid\" "
"value=\"camel://%s\"> </object>",
goad_id, uid);
} else
return NULL;
}
/*
* This takes a mimetype, and tries to map that mimetype to a function
* or a bonobo object.
*
* - If it's mapped to a bonobo object, this function prints a tag
* into the stream, designating the bonobo object and a place that
* the bonobo object can find data to hydrate from
*
* - otherwise, the mimetype is mapped to another function, which can
* print into the stream
*/
static void
call_handler_function (CamelDataWrapper *wrapper,
gchar *mimetype_whole_in, /* ex. "image/jpeg" */
gchar *mimetype_main_in, /* ex. "image" */
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
mime_handler_fn handler_function = NULL;
gchar *mimetype_whole = NULL;
gchar *mimetype_main = NULL;
g_return_if_fail (mimetype_whole_in || mimetype_main_in);
g_return_if_fail (CAMEL_IS_DATA_WRAPPER (wrapper));
g_return_if_fail (CAMEL_IS_DATA_WRAPPER (root));
if (mime_function_table == NULL) {
mime_function_table = g_hash_table_new (g_strcase_hash,
g_strcase_equal);
/* hook up mime types to functions that handle them */
g_hash_table_insert (mime_function_table, "text/plain",
handle_text_plain);
g_hash_table_insert (mime_function_table, "text/richtext",
handle_text_plain);
g_hash_table_insert (mime_function_table, "text/html",
handle_text_html);
g_hash_table_insert (mime_function_table, "multipart/alternative",
handle_multipart_alternative);
g_hash_table_insert (mime_function_table, "multipart/related",
handle_multipart_related);
g_hash_table_insert (mime_function_table, "multipart/mixed",
handle_multipart_mixed);
g_hash_table_insert (mime_function_table, "message/rfc822",
handle_mime_part);
g_hash_table_insert (mime_function_table, "image",
handle_image);
g_hash_table_insert (mime_function_table, "vcard",
handle_vcard);
/* RFC 2046 says unrecognized multipart subtypes should
* be treated like multipart/mixed.
*/
g_hash_table_insert (mime_function_table, "multipart",
handle_multipart_mixed);
/* Body parts don't have mime parts per se, so Camel
* sticks on the following one.
*/
g_hash_table_insert (mime_function_table, "mime/body-part",
handle_mime_part);
}
/* Try to find a handler function in our own lookup table */
if (mimetype_whole_in) {
mimetype_whole = g_strdup (mimetype_whole_in);
g_strdown (mimetype_whole);
handler_function = g_hash_table_lookup (mime_function_table,
mimetype_whole);
}
if (mimetype_main_in && !handler_function) {
mimetype_main = g_strdup (mimetype_main_in);
g_strdown (mimetype_main);
handler_function = g_hash_table_lookup (mime_function_table,
mimetype_main);
}
/* Upon failure, try to find a bonobo object to show the object */
if (!handler_function) {
gchar *bonobo_tag = NULL;
if (mimetype_whole)
bonobo_tag = get_bonobo_tag_for_object (
wrapper, mimetype_whole, root);
if (mimetype_main && !bonobo_tag)
bonobo_tag = get_bonobo_tag_for_object (
wrapper, mimetype_main, root);
if (bonobo_tag) {
/* We can print a tag, and return! */
mail_write_html (stream, bonobo_tag);
g_free (bonobo_tag);
if (mimetype_whole)
g_free (mimetype_whole);
if (mimetype_main)
g_free (mimetype_main);
return;
}
}
/* Use either a handler function we've found, or a default handler. */
if (handler_function)
(*handler_function) (wrapper, stream, root);
else
handle_unknown_type (wrapper, stream, root);
if (mimetype_whole)
g_free (mimetype_whole);
if (mimetype_main)
g_free (mimetype_main);
}
/* Convert plain text in equivalent-looking valid HTML. */
static gchar *
text_to_html (const guchar *input, guint len,
guint *encoded_len_return, gboolean add_pre,
gboolean convert_newlines_to_br)
{
const guchar *cur = input;
guchar *buffer = NULL;
guchar *out = NULL;
gint buffer_size = 0;
/* Allocate a translation buffer. */
buffer_size = len * 2 + 5;
buffer = g_malloc (buffer_size);
out = buffer;
if (add_pre)
out += sprintf (out, "<PRE>\n");
while (len--) {
if (out - buffer > buffer_size - 100) {
gint index = out - buffer;
buffer_size *= 2;
buffer = g_realloc (buffer, buffer_size);
out = buffer + index;
}
switch (*cur) {
case '<':
strcpy (out, "<");
out += 4;
break;
case '>':
strcpy (out, ">");
out += 4;
break;
case '&':
strcpy (out, "&");
out += 5;
break;
case '"':
strcpy (out, """);
out += 6;
break;
case '\n':
*out++ = *cur;
if (convert_newlines_to_br) {
strcpy (out, "<br>");
out += 4;
}
break;
default:
if ((*cur >= 0x20 && *cur < 0x80) ||
(*cur == '\r' || *cur == '\t')) {
/* Default case, just copy. */
*out++ = *cur;
} else
out += g_snprintf(out, 9, "&#%d;", *cur);
break;
}
cur++;
}
if (add_pre)
strcpy (out, "</PRE>");
else
*out = '\0';
if (encoded_len_return)
*encoded_len_return = out - buffer;
return buffer;
}
static void
write_field_to_stream (const gchar *description, const gchar *value,
gboolean bold, GtkHTMLStreamHandle *stream)
{
gchar *s;
gchar *encoded_value;
if (value) {
unsigned char *p;
encoded_value = text_to_html (value, strlen(value),
NULL, FALSE, TRUE);
for (p = (unsigned char *)encoded_value; *p; p++) {
if (!isprint (*p))
*p = '?';
}
} else
encoded_value = g_strdup ("");
s = g_strdup_printf ("<tr valign=top><%s align=right>%s</%s>"
"<td>%s</td></tr>", bold ? "th" : "td",
description, bold ? "th" : "td",
encoded_value);
mail_write_html (stream, s);
g_free (encoded_value);
g_free (s);
}
static void
write_recipients_to_stream (const gchar *recipient_type,
const GList *recipients, gboolean bold,
GtkHTMLStreamHandle *stream)
{
gchar *recipients_string = NULL;
while (recipients) {
gchar *old_string = recipients_string;
recipients_string =
g_strdup_printf ("%s%s%s",
old_string ? old_string : "",
old_string ? "; " : "",
(gchar *)recipients->data);
g_free (old_string);
recipients = recipients->next;
}
write_field_to_stream (recipient_type, recipients_string,
bold, stream);
g_free (recipients_string);
}
static void
write_header_info_to_stream (CamelMimeMessage *mime_message,
GtkHTMLStreamHandle *stream)
{
const GList *recipients;
mail_write_html (stream, "<table>");
/* A few fields will probably be available from the mime_message;
* for each one that's available, write it to the output stream
* with a helper function, 'write_field_to_stream'.
*/
write_field_to_stream ("From:",
camel_mime_message_get_from (mime_message),
TRUE, stream);
if (camel_mime_message_get_reply_to (mime_message)) {
write_field_to_stream ("Reply-To:",
camel_mime_message_get_reply_to (mime_message),
FALSE, stream);
}
write_recipients_to_stream ("To:",
camel_mime_message_get_recipients (mime_message, CAMEL_RECIPIENT_TYPE_TO),
TRUE, stream);
recipients = camel_mime_message_get_recipients (mime_message, CAMEL_RECIPIENT_TYPE_CC);
if (recipients)
write_recipients_to_stream ("Cc:", recipients, TRUE, stream);
write_field_to_stream ("Subject:",
camel_mime_message_get_subject (mime_message),
TRUE, stream);
mail_write_html (stream, "</table>");
}
#define MIME_TYPE_WHOLE(a) (gmime_content_field_get_mime_type ( \
camel_mime_part_get_content_type (CAMEL_MIME_PART (a))))
#define MIME_TYPE_MAIN(a) ((camel_mime_part_get_content_type (CAMEL_MIME_PART (a)))->type)
#define MIME_TYPE_SUB(a) ((camel_mime_part_get_content_type (CAMEL_MIME_PART (a)))->subtype)
/*----------------------------------------------------------------------*
* Mime handling functions
*----------------------------------------------------------------------*/
static void
handle_text_plain (CamelDataWrapper *wrapper, GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
gchar *text;
CamelStream *wrapper_output_stream;
gchar tmp_buffer[4096];
gint nb_bytes_read;
gboolean empty_text = TRUE;
mail_write_html (stream, "\n<!-- text/plain below -->\n");
mail_write_html (stream, "<pre>\n");
/* FIXME: text/richtext is not difficult to translate into HTML */
if (strcmp (wrapper->mime_type->subtype, "richtext") == 0) {
mail_write_html (stream, "<center><b>"
"<table bgcolor=\"b0b0ff\" cellpadding=3>"
"<tr><td>Warning: the following "
"richtext may not be formatted correctly. "
"</b></td></tr></table></center><br>");
}
/* Get the output stream of the data wrapper. */
wrapper_output_stream = camel_data_wrapper_get_output_stream (wrapper);
camel_stream_reset (wrapper_output_stream);
do {
/* Read next chunk of text. */
nb_bytes_read = camel_stream_read (wrapper_output_stream,
tmp_buffer, 4096);
/* If there's any text, write it to the stream. */
if (nb_bytes_read > 0) {
int returned_strlen;
empty_text = FALSE;
/* replace '<' with '<', etc. */
text = text_to_html (tmp_buffer,
nb_bytes_read,
&returned_strlen,
FALSE, FALSE);
mail_write_html (stream, text);
g_free (text);
}
} while (!camel_stream_eos (wrapper_output_stream));
if (empty_text)
mail_write_html (stream, "<b>(empty)</b>");
mail_write_html (stream, "</pre>\n");
}
static void
handle_text_html (CamelDataWrapper *wrapper, GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
CamelStream *wrapper_output_stream;
gchar tmp_buffer[4096];
gint nb_bytes_read;
gboolean empty_text = TRUE;
/* Get the output stream of the data wrapper. */
wrapper_output_stream = camel_data_wrapper_get_output_stream (wrapper);
camel_stream_reset (wrapper_output_stream);
/* Write the header. */
mail_write_html (stream, "\n<!-- text/html below -->\n");
do {
/* Read next chunk of text. */
nb_bytes_read = camel_stream_read (wrapper_output_stream,
tmp_buffer, 4096);
/* If there's any text, write it to the stream */
if (nb_bytes_read > 0) {
empty_text = FALSE;
/* Write the buffer to the html stream */
gtk_html_stream_write (stream, tmp_buffer,
nb_bytes_read);
}
} while (!camel_stream_eos (wrapper_output_stream));
if (empty_text)
mail_write_html (stream, "<b>(empty)</b>");
}
static void
handle_image (CamelDataWrapper *wrapper, GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
gchar *uuid;
gchar *tag;
uuid = lookup_unique_id (root, wrapper);
mail_write_html (stream, "\n<!-- image below -->\n");
tag = g_strdup_printf ("<img src=\"camel://%s\">\n", uuid);
mail_write_html (stream, tag);
g_free (uuid);
g_free (tag);
}
static void
handle_vcard (CamelDataWrapper *wrapper, GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
mail_write_html (stream, "\n<!-- vcard below -->\n");
/* FIXME: do something here. */
}
static void
handle_mime_part (CamelDataWrapper *wrapper, GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
CamelMimePart *mime_part;
CamelDataWrapper *message_contents;
gchar *whole_mime_type;
g_return_if_fail (CAMEL_IS_MIME_PART (wrapper));
mime_part = CAMEL_MIME_PART (wrapper);
message_contents =
camel_medium_get_content_object (CAMEL_MEDIUM (mime_part));
g_assert (message_contents);
mail_write_html (stream, "\n<!-- mime message below -->\n");
// mail_write_html (stream,
// "<table width=95% border=1><tr><td>\n\n");
/* dispatch the correct handler function for the mime type */
whole_mime_type = MIME_TYPE_WHOLE (mime_part);
call_handler_function (message_contents,
whole_mime_type,
MIME_TYPE_MAIN (mime_part),
stream, root);
g_free (whole_mime_type);
/* close up the table we opened */
// mail_write_html (stream,
// "\n\n</td></tr></table>\n\n");
}
/* called for each body part in a multipart/mixed */
static void
display_camel_body_part (CamelMimeBodyPart *body_part,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
gchar *whole_mime_type;
CamelDataWrapper* contents =
camel_medium_get_content_object (CAMEL_MEDIUM (body_part));
whole_mime_type = MIME_TYPE_WHOLE (body_part);
call_handler_function (contents, whole_mime_type,
MIME_TYPE_MAIN (body_part),
stream, root);
g_free (whole_mime_type);
mail_write_html (stream, "\n<hr>\n");
}
/* Our policy here is this:
(1) print text/(plain|html) parts found
(2) print vcards and images inline
(3) treat all other parts as attachments */
static void
handle_multipart_mixed (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream, CamelDataWrapper *root)
{
CamelMultipart *mp;
int i, nparts;
g_return_if_fail (CAMEL_IS_MULTIPART (wrapper));
mp = CAMEL_MULTIPART (wrapper);
nparts = camel_multipart_get_number (mp);
for (i = 0; i < nparts; i++) {
CamelMimeBodyPart *body_part =
camel_multipart_get_part (mp, i);
display_camel_body_part (body_part, stream, root);
}
}
/* As seen in RFC 2387! */
/* FIXME: camel doesn't currently set CamelMultipart::parent, so we
* can use it here.
*/
static void
handle_multipart_related (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
CamelMultipart *mp;
CamelMimeBodyPart *body_part;
#if 0
GMimeContentField *parent_content_type;
const char *start, *cid;
int i, nparts;
#endif
g_return_if_fail (CAMEL_IS_MULTIPART (wrapper));
mp = CAMEL_MULTIPART (wrapper);
#if 0
parent_content_type =
camel_mime_part_get_content_type (
camel_multipart_get_parent (mp));
start = gmime_content_field_get_parameter (parent_content_type,
"start");
if (start) {
nparts = camel_multipart_get_number (mp);
for (i = 0; i < nparts; i++) {
CamelMimeBodyPart *body_part =
camel_multipart_get_part (mp, i);
cid = camel_mime_part_get_content_id (
CAMEL_MIME_PART (body_part));
if (!strcmp (cid, start)) {
display_camel_body_part (body_part, stream,
root);
return;
}
}
/* Oops. Hrmph. */
handle_multipart_mixed (wrapper, stream, root);
}
#endif
/* No start parameter, so it defaults to the first part. */
body_part = camel_multipart_get_part (mp, 0);
display_camel_body_part (body_part, stream, root);
}
/* multipart/alternative helper function --
* Returns NULL if no displayable msg is found
*/
static CamelMimePart *
find_preferred_alternative (CamelMultipart *multipart)
{
int i, nparts;
CamelMimePart* html_part = NULL;
CamelMimePart* plain_part = NULL;
/* Find out out many parts are in it. */
nparts = camel_multipart_get_number (multipart);
/* FIXME: DO LEAF-LOOKUP HERE FOR OTHER MIME-TYPES!!! */
for (i = 0; i < nparts; i++) {
CamelMimeBodyPart *body_part =
camel_multipart_get_part (multipart, i);
if (strcasecmp (MIME_TYPE_MAIN (body_part), "text") != 0)
continue;
if (strcasecmp (MIME_TYPE_SUB (body_part), "plain") == 0)
plain_part = CAMEL_MIME_PART (body_part);
else if (strcasecmp (MIME_TYPE_SUB (body_part), "html") == 0)
html_part = CAMEL_MIME_PART (body_part);
}
if (html_part)
return html_part;
if (plain_part)
return plain_part;
return NULL;
}
/* The current policy for multipart/alternative is this:
*
* if (we find a text/html body part)
* we print it
* else if (we find a text/plain body part)
* we print it
* else
* we print nothing
*/
static void
handle_multipart_alternative (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
CamelMultipart *multipart = CAMEL_MULTIPART (wrapper);
CamelMimePart *mime_part;
gchar *whole_mime_type;
mime_part = find_preferred_alternative (multipart);
if (mime_part) {
CamelDataWrapper *contents =
camel_medium_get_content_object (
CAMEL_MEDIUM (mime_part));
whole_mime_type = MIME_TYPE_WHOLE (mime_part);
call_handler_function (contents, whole_mime_type,
MIME_TYPE_MAIN (mime_part),
stream, root);
g_free (whole_mime_type);
}
}
static void
handle_unknown_type (CamelDataWrapper *wrapper,
GtkHTMLStreamHandle *stream,
CamelDataWrapper *root)
{
gchar *tag;
char *uid = lookup_unique_id (root, wrapper);
tag = g_strdup_printf ("<a href=\"camel://%s\">click-me-to-save</a>\n",
uid);
mail_write_html (stream, tag);
}
void
mail_write_html (GtkHTMLStreamHandle *stream, const char *data)
{
gtk_html_stream_write (stream, data, strlen (data));
}
static char *
get_data_wrapper_text (CamelDataWrapper *data)
{
CamelStream *memstream;
GByteArray *ba;
char *text;
ba = g_byte_array_new ();
memstream = camel_stream_mem_new_with_byte_array (ba, CAMEL_STREAM_MEM_WRITE);
camel_data_wrapper_write_to_stream (data, memstream);
text = g_malloc (ba->len + 1);
memcpy (text, ba->data, ba->len);
text[ba->len] = '\0';
camel_stream_close (memstream);
return text;
}
static char *
reply_body (CamelDataWrapper *data, gboolean *html)
{
CamelMultipart *mp;
CamelMimePart *subpart;
int i, nparts;
char *subtext, *old;
const char *boundary, *disp;
char *text = NULL;
GMimeContentField *mime_type;
/* We only include text, message, and multipart bodies. */
mime_type = camel_data_wrapper_get_mime_type_field (data);
if (strcasecmp (mime_type->type, "message") == 0)
return get_data_wrapper_text (data);
if (strcasecmp (mime_type->type, "text") == 0) {
*html = !strcasecmp (mime_type->subtype, "html");
return get_data_wrapper_text (data);
}
/* If it's not message and it's not text, and it's not
* multipart, we don't want to deal with it.
*/
if (strcasecmp (mime_type->type, "multipart") != 0)
return NULL;
mp = CAMEL_MULTIPART (data);
if (strcasecmp (mime_type->subtype, "alternative") == 0) {
/* Pick our favorite alternative and reply to it. */
subpart = find_preferred_alternative (mp);
if (!subpart)
return NULL;
return reply_body (camel_medium_get_content_object (CAMEL_MEDIUM (subpart)), html);
}
nparts = camel_multipart_get_number (mp);
/* If any subpart is HTML, pull it out and reply to it by itself.
* (If we supported any other non-plain text types, we'd do the
* same for them here.)
*/
for (i = 0; i < nparts; i++) {
subpart = CAMEL_MIME_PART (camel_multipart_get_part (mp, i));
if (strcasecmp (MIME_TYPE_SUB (subpart), "html") == 0)
return reply_body (camel_medium_get_content_object (CAMEL_MEDIUM (subpart)), html);
}
/* Otherwise, concatenate all the parts that:
* - are text/plain or message
* - are not explicitly tagged with non-inline disposition
*/
boundary = camel_multipart_get_boundary (mp);
for (i = 0; i < nparts; i++) {
subpart = CAMEL_MIME_PART (camel_multipart_get_part (mp, i));
disp = camel_mime_part_get_disposition (subpart);
if (disp && strcasecmp (disp, "inline") != 0)
continue;
subtext = get_data_wrapper_text (data);
if (text) {
old = text;
text = g_strdup_printf ("%s\n--%s\n%s", text,
boundary, subtext);
g_free (subtext);
g_free (old);
} else
text = subtext;
}
if (!text)
return NULL;
old = text;
text = g_strdup_printf ("%s\n--%s--\n", text, boundary);
g_free (old);
return text;
}
EMsgComposer *
mail_generate_reply (CamelMimeMessage *message, gboolean to_all)
{
CamelDataWrapper *contents;
char *text, *subject;
EMsgComposer *composer;
gboolean html;
const char *repl_to, *message_id, *references;
GList *to, *cc;
contents = camel_medium_get_content_object (CAMEL_MEDIUM (message));
text = reply_body (contents, &html);
composer = E_MSG_COMPOSER (e_msg_composer_new ());
/* Set the quoted reply text. */
if (text) {
char *repl_text;
if (html) {
repl_text = g_strdup_printf ("<blockquote><i>\n%s\n"
"</i></blockquote>\n",
text);
} else {
char *s, *d, *quoted_text;
int lines, len;
/* Count the number of lines in the body. If
* the text ends with a \n, this will be one
* too high, but that's ok. Allocate enough
* space for the text and the "> "s.
*/
for (s = text, lines = 0; s; s = strchr (s + 1, '\n'))
lines++;
quoted_text = g_malloc (strlen (text) + lines * 2);
s = text;
d = quoted_text;
/* Copy text to quoted_text line by line,
* prepending "> ".
*/
while (1) {
len = strcspn (s, "\n");
if (len == 0 && !*s)
break;
sprintf (d, "> %.*s\n", len, s);
s += len;
if (!*s++)
break;
d += len + 3;
}
/* Now convert that to HTML. */
repl_text = text_to_html (quoted_text,
strlen (quoted_text),
&len, TRUE, FALSE);
g_free (quoted_text);
}
e_msg_composer_set_body_text (composer, repl_text);
g_free (repl_text);
g_free (text);
}
/* Set the recipients */
repl_to = camel_mime_message_get_reply_to (message);
if (!repl_to)
repl_to = camel_mime_message_get_from (message);
to = g_list_append (NULL, repl_to);
if (to_all) {
const GList *recip;
recip = camel_mime_message_get_recipients (message,
CAMEL_RECIPIENT_TYPE_TO);
cc = g_list_copy (recip);
recip = camel_mime_message_get_recipients (message,
CAMEL_RECIPIENT_TYPE_CC);
while (recip) {
cc = g_list_append (cc, recip->data);
recip = recip->next;
}
} else
cc = NULL;
/* Set the subject of the new message. */
subject = camel_mime_message_get_subject (message);
if (!subject)
subject = g_strdup ("");
else if (!strncasecmp (subject, "Re: ", 4))
subject = g_strdup (subject);
else
subject = g_strdup_printf ("Re: %s", subject);
e_msg_composer_set_headers (composer, to, cc, NULL, subject);
g_list_free (to);
g_list_free (cc);
g_free (subject);
/* Add In-Reply-To and References. */
message_id = camel_medium_get_header (CAMEL_MEDIUM (message),
"Message-Id");
references = camel_medium_get_header (CAMEL_MEDIUM (message),
"References");
if (message_id) {
e_msg_composer_add_header (composer, "In-Reply-To",
message_id);
if (references) {
char *reply_refs;
reply_refs = g_strdup_printf ("%s %s", references,
message_id);
e_msg_composer_add_header (composer, "References",
reply_refs);
g_free (reply_refs);
}
} else if (references)
e_msg_composer_add_header (composer, "References", references);
return composer;
}
/* This is part of the temporary kludge below. */
#ifndef HAVE_MKSTEMP
#include <fcntl.h>
#include <sys/stat.h>
#endif
EMsgComposer *
mail_generate_forward (CamelMimeMessage *mime_message,
gboolean forward_as_attachment,
gboolean keep_attachments)
{
EMsgComposer *composer;
char *tmpfile;
int fd;
CamelStream *stream;
if (!forward_as_attachment)
g_warning ("Forward as non-attachment not implemented.");
if (!keep_attachments)
g_warning ("Forwarding without attachments not implemented.");
/* For now, we kludge by writing out a temp file. Later,
* EMsgComposer will support attaching CamelMimeParts directly,
* or something. FIXME.
*/
tmpfile = g_strdup ("/tmp/evolution-kludge-XXXX");
#ifdef HAVE_MKSTEMP
fd = mkstemp (tmpfile);
#else
if (mktemp (tmpfile)) {
fd = open (tmpfile, O_RDWR | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR);
} else
fd = -1;
#endif
if (fd == -1) {
g_warning ("Couldn't create temp file for forwarding");
g_free (tmpfile);
return NULL;
}
stream = camel_stream_fs_new_with_fd (fd);
camel_data_wrapper_write_to_stream (CAMEL_DATA_WRAPPER (mime_message),
stream);
camel_stream_close (stream);
composer = E_MSG_COMPOSER (e_msg_composer_new ());
e_msg_composer_attachment_bar_attach (composer->attachment_bar,
tmpfile);
g_free (tmpfile);
/* FIXME: should we default a subject? */
return composer;
}
|