aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-format.c
blob: 99c71ec1e6ac7baa5c3f0fcf052c1ef2115b84b8 (plain) (blame)
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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
/*
 * 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:
 *      Michael Zucchi <notzed@ximian.com>
 *      Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include <glib/gi18n.h>
#include <gio/gio.h>

#include <libedataserver/e-msgport.h>
#include <camel/camel-url.h>
#include <camel/camel-stream.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-multipart.h>
#include <camel/camel-multipart-encrypted.h>
#include <camel/camel-multipart-signed.h>
#include <camel/camel-medium.h>
#include <camel/camel-mime-message.h>
#include <camel/camel-gpg-context.h>
#include <camel/camel-smime-context.h>
#include <camel/camel-string-utils.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-stream-null.h>
#include <camel/camel-stream-mem.h>
#include <camel/camel-mime-filter-charset.h>
#include <camel/camel-mime-filter-windows.h>
#include <camel/camel-mime-filter-pgp.h>

#include "em-format.h"
#include "em-utils.h"
#include "mail-config.h"

#define d(x)

/* Used to cache various data/info for redraws
   The validity stuff could be cached at a higher level but this is easier
   This absolutely relies on the partid being _globally unique_
   This is still kind of yucky, we should maintian a full tree of all this data,
   along with/as part of the puri tree */
struct _EMFormatCache {
    struct _CamelCipherValidity *valid; /* validity copy */
    struct _CamelMimePart *secured; /* encrypted subpart */

    guint state:2;      /* inline state */

    gchar partid[1];
};

#define INLINE_UNSET (0)
#define INLINE_ON (1)
#define INLINE_OFF (2)

static void emf_builtin_init(EMFormatClass *);

static const EMFormatHandler *emf_find_handler(EMFormat *emf, const gchar *mime_type);
static void emf_format_clone(EMFormat *emf, CamelFolder *folder, const gchar *uid, CamelMimeMessage *msg, EMFormat *emfsource);
static void emf_format_secure(EMFormat *emf, CamelStream *stream, CamelMimePart *part, CamelCipherValidity *valid);
static gboolean emf_busy(EMFormat *emf);
enum {
    EMF_COMPLETE,
    EMF_LAST_SIGNAL
};

extern CamelSession *session;

static guint emf_signals[EMF_LAST_SIGNAL];
static GObjectClass *emf_parent;

static void
emf_free_cache(struct _EMFormatCache *efc)
{
    if (efc->valid)
        camel_cipher_validity_free(efc->valid);
    if (efc->secured)
        camel_object_unref(efc->secured);
    g_free(efc);
}

static struct _EMFormatCache *
emf_insert_cache(EMFormat *emf, const gchar *partid)
{
    struct _EMFormatCache *new;

    new = g_malloc0(sizeof(*new)+strlen(partid));
    strcpy(new->partid, partid);
    g_hash_table_insert(emf->inline_table, new->partid, new);

    return new;
}

static void
emf_init(GObject *o)
{
    EMFormat *emf = (EMFormat *)o;

    emf->inline_table = g_hash_table_new_full (
        g_str_hash, g_str_equal,
        (GDestroyNotify) NULL,
        (GDestroyNotify) emf_free_cache);
    emf->composer = FALSE;
    emf->print = FALSE;
    emf->show_photo = TRUE;
    emf->photo_local = TRUE;
    e_dlist_init(&emf->header_list);
    em_format_default_headers(emf);
    emf->part_id = g_string_new("");
}

static void
emf_finalise(GObject *o)
{
    EMFormat *emf = (EMFormat *)o;

    if (emf->session)
        camel_object_unref(emf->session);

    g_hash_table_destroy(emf->inline_table);

    em_format_clear_headers(emf);
    camel_cipher_validity_free(emf->valid);
    g_free(emf->charset);
    g_free (emf->default_charset);
    g_string_free(emf->part_id, TRUE);

    /* FIXME: check pending jobs */

    ((GObjectClass *)emf_parent)->finalize(o);
}

static void
emf_base_init(EMFormatClass *emfklass)
{
    emfklass->type_handlers = g_hash_table_new(g_str_hash, g_str_equal);
    emf_builtin_init(emfklass);
}

static void
emf_class_init(GObjectClass *klass)
{
    ((EMFormatClass *)klass)->type_handlers = g_hash_table_new(g_str_hash, g_str_equal);
    emf_builtin_init((EMFormatClass *)klass);

    klass->finalize = emf_finalise;
    ((EMFormatClass *)klass)->find_handler = emf_find_handler;
    ((EMFormatClass *)klass)->format_clone = emf_format_clone;
    ((EMFormatClass *)klass)->format_secure = emf_format_secure;
    ((EMFormatClass *)klass)->busy = emf_busy;

    emf_signals[EMF_COMPLETE] =
        g_signal_new("complete",
                 G_OBJECT_CLASS_TYPE (klass),
                 G_SIGNAL_RUN_LAST,
                 G_STRUCT_OFFSET (EMFormatClass, complete),
                 NULL, NULL,
                 g_cclosure_marshal_VOID__VOID,
                 G_TYPE_NONE, 0);
}

GType
em_format_get_type(void)
{
    static GType type = 0;

    if (type == 0) {
        static const GTypeInfo info = {
            sizeof(EMFormatClass),
            (GBaseInitFunc)emf_base_init, NULL,
            (GClassInitFunc)emf_class_init,
            NULL, NULL,
            sizeof(EMFormat), 0,
            (GInstanceInitFunc)emf_init
        };
        emf_parent = g_type_class_ref(G_TYPE_OBJECT);
        type = g_type_register_static(G_TYPE_OBJECT, "EMFormat", &info, 0);
    }

    return type;
}

/**
 * em_format_class_add_handler:
 * @emfc: EMFormatClass
 * @info: Callback information.
 *
 * Add a mime type handler to this class.  This is only used by
 * implementing classes.  The @info.old pointer will automatically be
 * setup to point to the old hanlder if one was already set.  This can
 * be used for overrides a fallback.
 *
 * When a mime type described by @info is encountered, the callback will
 * be invoked.  Note that @info may be extended by sub-classes if
 * they require additional context information.
 *
 * Use a mime type of "foo/ *" to insert a fallback handler for type "foo".
 **/
void
em_format_class_add_handler(EMFormatClass *emfc, EMFormatHandler *info)
{
    d(printf("adding format handler to '%s' '%s'\n",    g_type_name_from_class((GTypeClass *)emfc), info->mime_type));
    info->old = g_hash_table_lookup(emfc->type_handlers, info->mime_type);
    g_hash_table_insert(emfc->type_handlers, (gpointer) info->mime_type, info);
}

struct _class_handlers {
    EMFormatClass *old;
    EMFormatClass *new;
};
static void
merge_missing (gpointer key, gpointer value, gpointer userdata)
{
    struct _class_handlers *classes = (struct _class_handlers *) userdata;
    EMFormatHandler *info, *oldinfo;

    oldinfo = (EMFormatHandler *) value;
    info = g_hash_table_lookup (classes->new->type_handlers, key);
    if (!info) {
        /* Might be from a plugin */
        g_hash_table_insert (classes->new->type_handlers, key, value);
    }

}

void
em_format_merge_handler(EMFormat *new, EMFormat *old)
{
    EMFormatClass *oldc = (EMFormatClass *)G_OBJECT_GET_CLASS(old);
    EMFormatClass *newc = (EMFormatClass *)G_OBJECT_GET_CLASS(new);
    struct _class_handlers fclasses;

    fclasses.old = oldc;
    fclasses.new = newc;

    g_hash_table_foreach (oldc->type_handlers, merge_missing, &fclasses);

}
/**
 * em_format_class_remove_handler:
 * @emfc:
 * @info:
 *
 * Remove a handler.  @info must be a value which was previously
 * added.
 **/
void
em_format_class_remove_handler(EMFormatClass *emfc, EMFormatHandler *info)
{
    EMFormatHandler *current;

    /* TODO: thread issues? */

    current = g_hash_table_lookup(emfc->type_handlers, info->mime_type);
    if (current == info) {
        current = info->old;
        if (current)
            g_hash_table_insert(emfc->type_handlers, (gpointer) current->mime_type, current);
        else
            g_hash_table_remove(emfc->type_handlers, info->mime_type);
    } else {
        while (current && current->old != info)
            current = current->old;
        g_return_if_fail(current != NULL);
        current->old = info->old;
    }
}

/**
 * em_format_find_handler:
 * @emf:
 * @mime_type:
 *
 * Find a format handler by @mime_type.
 *
 * Return value: NULL if no handler is available.
 **/
static const EMFormatHandler *
emf_find_handler(EMFormat *emf, const gchar *mime_type)
{
    EMFormatClass *emfc = (EMFormatClass *)G_OBJECT_GET_CLASS(emf);

    return g_hash_table_lookup(emfc->type_handlers, mime_type);
}

/**
 * em_format_fallback_handler:
 * @emf:
 * @mime_type:
 *
 * Try to find a format handler based on the major type of the @mime_type.
 *
 * The subtype is replaced with "*" and a lookup performed.
 *
 * Return value:
 **/
const EMFormatHandler *
em_format_fallback_handler(EMFormat *emf, const gchar *mime_type)
{
    gchar *mime, *s;

    s = strchr(mime_type, '/');
    if (s == NULL)
        mime = (gchar *)mime_type;
    else {
        gsize len = (s-mime_type)+1;

        mime = alloca(len+2);
        strncpy(mime, mime_type, len);
        strcpy(mime+len, "*");
    }

    return em_format_find_handler(emf, mime);
}

/**
 * em_format_add_puri:
 * @emf:
 * @size:
 * @cid: Override the autogenerated content id.
 * @part:
 * @func:
 *
 * Add a pending-uri handler.  When formatting parts that reference
 * other parts, a pending-uri (PURI) can be used to track the reference.
 *
 * @size is used to allocate the structure, so that it can be directly
 * subclassed by implementors.
 *
 * @cid can be used to override the key used to retreive the PURI, if NULL,
 * then the content-location and the content-id of the @part are stored
 * as lookup keys for the part.
 *
 * FIXME: This may need a free callback.
 *
 * Return value: A new PURI, with a referenced copy of @part, and the cid
 * always set.  The uri will be set if one is available.  Clashes
 * are resolved by forgetting the old PURI in the global index.
 **/
EMFormatPURI *
em_format_add_puri(EMFormat *emf, gsize size, const gchar *cid, CamelMimePart *part, EMFormatPURIFunc func)
{
    EMFormatPURI *puri;
    const gchar *tmp;

    d(printf("adding puri for part: %s\n", emf->part_id->str));

    if (size < sizeof(*puri)) {
        g_warning (
            "size (%" G_GSIZE_FORMAT
            ") less than size of puri\n", size);
        size = sizeof (*puri);
    }

    puri = g_malloc0(size);

    puri->format = emf;
    puri->func = func;
    puri->use_count = 0;
    puri->cid = g_strdup(cid);
    puri->part_id = g_strdup(emf->part_id->str);

    if (part) {
        camel_object_ref(part);
        puri->part = part;
    }

    if (part != NULL && cid == NULL) {
        tmp = camel_mime_part_get_content_id(part);
        if (tmp)
            puri->cid = g_strdup_printf("cid:%s", tmp);
        else
            puri->cid = g_strdup_printf("em-no-cid:%s", emf->part_id->str);

        d(printf("built cid '%s'\n", puri->cid));

        /* not quite same as old behaviour, it also put in the relative uri and a fallback for no parent uri */
        tmp = camel_mime_part_get_content_location(part);
        puri->uri = NULL;
        if (tmp == NULL) {
            /* no location, don't set a uri at all, html parts do this themselves */
        } else {
            if (strchr(tmp, ':') == NULL && emf->base != NULL) {
                CamelURL *uri;

                uri = camel_url_new_with_base(emf->base, tmp);
                puri->uri = camel_url_to_string(uri, 0);
                camel_url_free(uri);
            } else {
                puri->uri = g_strdup(tmp);
            }
        }
    }

    g_return_val_if_fail (puri->cid != NULL, NULL);
    g_return_val_if_fail (emf->pending_uri_level != NULL, NULL);
    g_return_val_if_fail (emf->pending_uri_table != NULL, NULL);

    e_dlist_addtail(&emf->pending_uri_level->uri_list, (EDListNode *)puri);

    if (puri->uri)
        g_hash_table_insert(emf->pending_uri_table, puri->uri, puri);
    g_hash_table_insert(emf->pending_uri_table, puri->cid, puri);

    return puri;
}

/**
 * em_format_push_level:
 * @emf:
 *
 * This is used to build a heirarchy of visible PURI objects based on
 * the structure of the message.  Used by multipart/alternative formatter.
 *
 * FIXME: This could probably also take a uri so it can automaticall update
 * the base location.
 **/
void
em_format_push_level(EMFormat *emf)
{
    struct _EMFormatPURITree *purilist;

    d(printf("em_format_push_level\n"));
    purilist = g_malloc0(sizeof(*purilist));
    e_dlist_init(&purilist->children);
    e_dlist_init(&purilist->uri_list);
    purilist->parent = emf->pending_uri_level;
    if (emf->pending_uri_tree == NULL) {
        emf->pending_uri_tree = purilist;
    } else {
        e_dlist_addtail(&emf->pending_uri_level->children, (EDListNode *)purilist);
    }
    emf->pending_uri_level = purilist;
}

/**
 * em_format_pull_level:
 * @emf:
 *
 * Drop a level of visibility back to the parent.  Note that
 * no PURI values are actually freed.
 **/
void
em_format_pull_level(EMFormat *emf)
{
    d(printf("em_format_pull_level\n"));
    emf->pending_uri_level = emf->pending_uri_level->parent;
}

/**
 * em_format_find_visible_puri:
 * @emf:
 * @uri:
 *
 * Search for a PURI based on the visibility defined by :push_level()
 * and :pull_level().
 *
 * Return value:
 **/
EMFormatPURI *
em_format_find_visible_puri(EMFormat *emf, const gchar *uri)
{
    EMFormatPURI *pw;
    struct _EMFormatPURITree *ptree;

    d(printf("checking for visible uri '%s'\n", uri));

    ptree = emf->pending_uri_level;
    while (ptree) {
        pw = (EMFormatPURI *)ptree->uri_list.head;
        while (pw->next) {
            d(printf(" pw->uri = '%s' pw->cid = '%s\n", pw->uri?pw->uri:"", pw->cid));
            if ((pw->uri && !strcmp(pw->uri, uri)) || !strcmp(pw->cid, uri))
                return pw;
            pw = pw->next;
        }
        ptree = ptree->parent;
    }

    return NULL;
}

/**
 * em_format_find_puri:
 * @emf:
 * @uri:
 *
 * Search for a PURI based on a uri.  Both the content-id
 * and content-location are checked.
 *
 * Return value:
 **/
EMFormatPURI *

em_format_find_puri(EMFormat *emf, const gchar *uri)
{
    return g_hash_table_lookup(emf->pending_uri_table, uri);
}

static void
emf_clear_puri_node(struct _EMFormatPURITree *node)
{
    {
        EMFormatPURI *pw, *pn;

        /* clear puri's at this level */
        pw = (EMFormatPURI *)node->uri_list.head;
        pn = pw->next;
        while (pn) {
            d(printf ("freeing pw %p format:%p\n", pw, pw->format));
            if (pw->free)
                pw->free(pw);
            g_free(pw->uri);
            g_free(pw->cid);
            g_free(pw->part_id);
            if (pw->part)
                camel_object_unref(pw->part);
            g_free(pw);
            pw = pn;
            pn = pn->next;
        }
    }

    {
        struct _EMFormatPURITree *cw, *cn;

        /* clear child nodes */
        cw = (struct _EMFormatPURITree *)node->children.head;
        cn = cw->next;
        while (cn) {
            emf_clear_puri_node(cw);
            cw = cn;
            cn = cn->next;
        }
    }

    g_free(node);
}

/**
 * em_format_clear_puri_tree:
 * @emf:
 *
 * For use by implementors to clear out the message structure
 * data.
 **/
void
em_format_clear_puri_tree(EMFormat *emf)
{
    d(printf("clearing pending uri's\n"));

    if (emf->pending_uri_table) {
        g_hash_table_destroy(emf->pending_uri_table);
        emf_clear_puri_node(emf->pending_uri_tree);
        emf->pending_uri_level = NULL;
        emf->pending_uri_tree = NULL;
    }
    emf->pending_uri_table = g_hash_table_new(g_str_hash, g_str_equal);
    em_format_push_level(emf);
}

/* use mime_type == NULL  to force showing as application/octet-stream */
void
em_format_part_as(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const gchar *mime_type)
{
    const EMFormatHandler *handle = NULL;
    const gchar *snoop_save = emf->snoop_mime_type, *tmp;
    CamelURL *base_save = emf->base, *base = NULL;
    gchar *basestr = NULL;

    d(printf("format_part_as()\n"));

    emf->snoop_mime_type = NULL;

    /* RFC 2110, we keep track of content-base, and absolute content-location headers
       This is actually only required for html, but, *shrug* */
    tmp = camel_medium_get_header((CamelMedium *)part, "Content-Base");
    if (tmp == NULL) {
        tmp = camel_mime_part_get_content_location(part);
        if (tmp && strchr(tmp, ':') == NULL)
            tmp = NULL;
    } else {
        tmp = basestr = camel_header_location_decode(tmp);
    }
    d(printf("content-base is '%s'\n", tmp?tmp:"<unset>"));
    if (tmp
        && (base = camel_url_new(tmp, NULL))) {
        emf->base = base;
        d(printf("Setting content base '%s'\n", tmp));
    }
    g_free(basestr);

    if (mime_type != NULL) {
        if (g_ascii_strcasecmp(mime_type, "application/octet-stream") == 0) {
            emf->snoop_mime_type = mime_type = em_utils_snoop_type(part);
            if (mime_type == NULL)
                mime_type = "application/octet-stream";
        }

        handle = em_format_find_handler(emf, mime_type);
        if (handle == NULL)
            handle = em_format_fallback_handler(emf, mime_type);

        if (handle != NULL
            && !em_format_is_attachment(emf, part)) {
            d(printf("running handler for type '%s'\n", mime_type));
            handle->handler(emf, stream, part, handle);
            goto finish;
        }
        d(printf("this type is an attachment? '%s'\n", mime_type));
    } else {
        mime_type = "application/octet-stream";
    }

    ((EMFormatClass *)G_OBJECT_GET_CLASS(emf))->format_attachment(emf, stream, part, mime_type, handle);
finish:
    emf->base = base_save;
    emf->snoop_mime_type = snoop_save;

    if (base)
        camel_url_free(base);
}

void
em_format_part(EMFormat *emf, CamelStream *stream, CamelMimePart *part)
{
    gchar *mime_type;
    CamelDataWrapper *dw;

    dw = camel_medium_get_content_object((CamelMedium *)part);
    mime_type = camel_data_wrapper_get_mime_type(dw);
    if (mime_type) {
        camel_strdown(mime_type);
        em_format_part_as(emf, stream, part, mime_type);
        g_free(mime_type);
    } else
        em_format_part_as(emf, stream, part, "text/plain");
}

static void
emf_clone_inlines(gpointer key, gpointer val, gpointer data)
{
    struct _EMFormatCache *emfc = val, *new;

    new = emf_insert_cache((EMFormat *)data, emfc->partid);
    new->state = emfc->state;
    if (emfc->valid)
        new->valid = camel_cipher_validity_clone(emfc->valid);
    if (emfc->secured)
        camel_object_ref((new->secured = emfc->secured));
}

static void
emf_format_clone(EMFormat *emf, CamelFolder *folder, const gchar *uid, CamelMimeMessage *msg, EMFormat *emfsource)
{
    em_format_clear_puri_tree(emf);

    if (emf != emfsource) {
        g_hash_table_remove_all(emf->inline_table);
        if (emfsource) {
            struct _EMFormatHeader *h;

            /* We clone the current state here */
            g_hash_table_foreach(emfsource->inline_table, emf_clone_inlines, emf);
            emf->mode = emfsource->mode;
            g_free(emf->charset);
            emf->charset = g_strdup(emfsource->charset);
            g_free (emf->default_charset);
            emf->default_charset = g_strdup (emfsource->default_charset);

            em_format_clear_headers(emf);
            for (h = (struct _EMFormatHeader *)emfsource->header_list.head; h->next; h = h->next)
                em_format_add_header(emf, h->name, h->flags);
        }
    }

    /* what a mess */
    if (folder != emf->folder) {
        if (emf->folder)
            camel_object_unref(emf->folder);
        if (folder)
            camel_object_ref(folder);
        emf->folder = folder;
    }

    if (uid != emf->uid) {
        g_free(emf->uid);
        emf->uid = g_strdup(uid);
    }

    if (msg != emf->message) {
        if (emf->message)
            camel_object_unref(emf->message);
        if (msg)
            camel_object_ref(msg);
        emf->message = msg;
    }

    g_string_truncate(emf->part_id, 0);
    if (folder != NULL)
        /* TODO build some string based on the folder name/location? */
        g_string_append_printf(emf->part_id, ".%p", (gpointer) folder);
    if (uid != NULL)
        g_string_append_printf(emf->part_id, ".%s", uid);
}

static void
emf_format_secure(EMFormat *emf, CamelStream *stream, CamelMimePart *part, CamelCipherValidity *valid)
{
    CamelCipherValidity *save = emf->valid_parent;
    gint len;

    /* Note that this also requires support from higher up in the class chain
        - validity needs to be cleared when you start output
        - also needs to be cleared (but saved) whenever you start a new message. */

    if (emf->valid == NULL) {
        emf->valid = valid;
    } else {
        camel_dlist_addtail(&emf->valid_parent->children, (CamelDListNode *)valid);
        camel_cipher_validity_envelope(emf->valid_parent, valid);
    }

    emf->valid_parent = valid;

    len = emf->part_id->len;
    g_string_append_printf(emf->part_id, ".secured");
    em_format_part(emf, stream, part);
    g_string_truncate(emf->part_id, len);

    emf->valid_parent = save;
}

static gboolean
emf_busy(EMFormat *emf)
{
    return FALSE;
}

/**
 * em_format_format_clone:
 * @emf: Mail formatter.
 * @folder: Camel Folder.
 * @uid: Uid of message.
 * @msg: Camel Message.
 * @emfsource: Used as a basis for user-altered layout, e.g. inline viewed
 * attachments.
 *
 * Format a message @msg.  If @emfsource is non NULL, then the status of
 * inlined expansion and so forth is copied direction from @emfsource.
 *
 * By passing the same value for @emf and @emfsource, you can perform
 * a display refresh, or it can be used to generate an identical layout,
 * e.g. to print what the user has shown inline.
 **/
void
em_format_format_clone (EMFormat *emf,
                        CamelFolder *folder,
                        const gchar *uid,
                        CamelMimeMessage *message,
                        EMFormat *source)
{
    EMFormatClass *class;

    g_return_if_fail (EM_IS_FORMAT (emf));
    g_return_if_fail (folder == NULL || CAMEL_IS_FOLDER (folder));
    g_return_if_fail (message == NULL || CAMEL_IS_MIME_MESSAGE (message));
    g_return_if_fail (source == NULL || EM_IS_FORMAT (source));

    class = EM_FORMAT_GET_CLASS (emf);
    g_return_if_fail (class->format_clone != NULL);
    class->format_clone (emf, folder, uid, message, source);
}

void
em_format_format (EMFormat *emf,
                  CamelFolder *folder,
                  const gchar *uid,
                  CamelMimeMessage *message)
{
    /* em_format_format_clone() will check the arguments. */
    em_format_format_clone (emf, folder, uid, message, NULL);
}

void
em_format_redraw (EMFormat *emf)
{
    g_return_if_fail (EM_IS_FORMAT (emf));

    em_format_format_clone (
        emf, emf->folder, emf->uid, emf->message, emf);
}

/**
 * em_format_set_session:
 * @emf:
 * @s:
 *
 * Set the CamelSession to be used for signature verification and decryption
 * purposes.  If this is not set, then signatures cannot be verified or
 * encrypted messages viewed.
 **/
void
em_format_set_session(EMFormat *emf, struct _CamelSession *s)
{
    if (s)
        camel_object_ref(s);
    if (emf->session)
        camel_object_unref(emf->session);
    emf->session = s;
}

/**
 * em_format_set_mode:
 * @emf:
 * @type:
 *
 * Set display mode, EM_FORMAT_SOURCE, EM_FORMAT_ALLHEADERS, or
 * EM_FORMAT_NORMAL.
 **/
void
em_format_set_mode(EMFormat *emf, em_format_mode_t type)
{
    if (emf->mode == type)
        return;

    emf->mode = type;

    /* force redraw if type changed afterwards */
    if (emf->message)
        em_format_redraw(emf);
}

/**
 * em_format_set_charset:
 * @emf:
 * @charset:
 *
 * set override charset on formatter.  message will be redisplayed if
 * required.
 **/
void
em_format_set_charset(EMFormat *emf, const gchar *charset)
{
    if ((emf->charset && charset && g_ascii_strcasecmp(emf->charset, charset) == 0)
        || (emf->charset == NULL && charset == NULL)
        || (emf->charset == charset))
        return;

    g_free(emf->charset);
    emf->charset = g_strdup(charset);

    if (emf->message)
        em_format_redraw(emf);
}

/**
 * em_format_set_default_charset:
 * @emf:
 * @charset:
 *
 * Set the fallback, default system charset to use when no other charsets
 * are present.  Message will be redisplayed if required (and sometimes redisplayed
 * when it isn't).
 **/
void
em_format_set_default_charset(EMFormat *emf, const gchar *charset)
{
    if ((emf->default_charset && charset && g_ascii_strcasecmp(emf->default_charset, charset) == 0)
        || (emf->default_charset == NULL && charset == NULL)
        || (emf->default_charset == charset))
        return;

    g_free(emf->default_charset);
    emf->default_charset = g_strdup(charset);

    if (emf->message && emf->charset == NULL)
        em_format_redraw(emf);
}

/**
 * em_format_clear_headers:
 * @emf:
 *
 * Clear the list of headers to be displayed.  This will force all headers to
 * be shown.
 **/
void
em_format_clear_headers(EMFormat *emf)
{
    EMFormatHeader *eh;

    while ((eh = (EMFormatHeader *)e_dlist_remhead(&emf->header_list)))
        g_free(eh);
}

/* note: also copied in em-mailer-prefs.c */
static const struct {
    const gchar *name;
    guint32 flags;
} default_headers[] = {
    { N_("From"), EM_FORMAT_HEADER_BOLD },
    { N_("Reply-To"), EM_FORMAT_HEADER_BOLD },
    { N_("To"), EM_FORMAT_HEADER_BOLD },
    { N_("Cc"), EM_FORMAT_HEADER_BOLD },
    { N_("Bcc"), EM_FORMAT_HEADER_BOLD },
    { N_("Subject"), EM_FORMAT_HEADER_BOLD },
    { N_("Date"), EM_FORMAT_HEADER_BOLD },
    { N_("Newsgroups"), EM_FORMAT_HEADER_BOLD },
    { N_("Face"), 0 },
};

/**
 * em_format_default_headers:
 * @emf:
 *
 * Set the headers to show to the default list.
 *
 * From, Reply-To, To, Cc, Bcc, Subject and Date.
 **/
void
em_format_default_headers(EMFormat *emf)
{
    gint i;

    em_format_clear_headers(emf);
    for (i=0; i<sizeof(default_headers)/sizeof(default_headers[0]); i++)
        em_format_add_header(emf, default_headers[i].name, default_headers[i].flags);
}

/**
 * em_format_add_header:
 * @emf:
 * @name: The name of the header, as it will appear during output.
 * @flags: EM_FORMAT_HEAD_* defines to control display attributes.
 *
 * Add a specific header to show.  If any headers are set, they will
 * be displayed in the order set by this function.  Certain known
 * headers included in this list will be shown using special
 * formatting routines.
 **/
void em_format_add_header(EMFormat *emf, const gchar *name, guint32 flags)
{
    EMFormatHeader *h;

    h = g_malloc(sizeof(*h) + strlen(name));
    h->flags = flags;
    strcpy(h->name, name);
    e_dlist_addtail(&emf->header_list, (EDListNode *)h);
}

/**
 * em_format_is_attachment:
 * @emf:
 * @part: Part to check.
 *
 * Returns true if the part is an attachment.
 *
 * A part is not considered an attachment if it is a
 * multipart, or a text part with no filename.  It is used
 * to determine if an attachment header should be displayed for
 * the part.
 *
 * Content-Disposition is not checked.
 *
 * Return value: TRUE/FALSE
 **/
gint em_format_is_attachment(EMFormat *emf, CamelMimePart *part)
{
    /*CamelContentType *ct = camel_mime_part_get_content_type(part);*/
    CamelDataWrapper *dw = camel_medium_get_content_object((CamelMedium *)part);

    if (!dw)
        return 0;

    /*printf("checking is attachment %s/%s\n", ct->type, ct->subtype);*/
    return !(camel_content_type_is (dw->mime_type, "multipart", "*")
         || camel_content_type_is(dw->mime_type, "application", "x-pkcs7-mime")
         || camel_content_type_is(dw->mime_type, "application", "pkcs7-mime")
         || camel_content_type_is(dw->mime_type, "application", "x-inlinepgp-signed")
         || camel_content_type_is(dw->mime_type, "application", "x-inlinepgp-encrypted")
         || camel_content_type_is(dw->mime_type, "x-evolution", "evolution-rss-feed")
         || (camel_content_type_is (dw->mime_type, "text", "*")
             && camel_mime_part_get_filename(part) == NULL));
}

/**
 * em_format_is_inline:
 * @emf:
 * @part:
 * @partid: format->part_id part id of this part.
 * @handle: handler for this part
 *
 * Returns true if the part should be displayed inline.  Any part with
 * a Content-Disposition of inline, or if the @handle has a default
 * inline set, will be shown inline.
 *
 * :set_inline() called on the same part will override any calculated
 * value.
 *
 * Return value:
 **/
gint em_format_is_inline(EMFormat *emf, const gchar *partid, CamelMimePart *part, const EMFormatHandler *handle)
{
    struct _EMFormatCache *emfc;
    const gchar *tmp;

    if (handle == NULL)
        return FALSE;

    emfc = g_hash_table_lookup(emf->inline_table, partid);
    if (emfc && emfc->state != INLINE_UNSET)
        return emfc->state & 1;

    /* some types need to override the disposition, e.g. application/x-pkcs7-mime */
    if (handle->flags & EM_FORMAT_HANDLER_INLINE_DISPOSITION)
        return TRUE;

    tmp = camel_mime_part_get_disposition(part);
    if (tmp)
        return g_ascii_strcasecmp(tmp, "inline") == 0;

    /* otherwise, use the default for this handler type */
    return (handle->flags & EM_FORMAT_HANDLER_INLINE) != 0;
}

/**
 * em_format_set_inline:
 * @emf:
 * @partid: id of part
 * @state:
 *
 * Force the attachment @part to be expanded or hidden explictly to match
 * @state.  This is used only to record the change for a redraw or
 * cloned layout render and does not force a redraw.
 **/
void em_format_set_inline(EMFormat *emf, const gchar *partid, gint state)
{
    struct _EMFormatCache *emfc;

    emfc = g_hash_table_lookup(emf->inline_table, partid);
    if (emfc == NULL) {
        emfc = emf_insert_cache(emf, partid);
    } else if (emfc->state != INLINE_UNSET && (emfc->state & 1) == state)
        return;

    emfc->state = state?INLINE_ON:INLINE_OFF;

    if (emf->message)
        em_format_redraw(emf);
}

void em_format_format_error(EMFormat *emf, CamelStream *stream, const gchar *fmt, ...)
{
    va_list ap;
    gchar *txt;

    va_start(ap, fmt);
    txt = g_strdup_vprintf(fmt, ap);
    ((EMFormatClass *)G_OBJECT_GET_CLASS(emf))->format_error((emf), (stream), (txt));
    g_free(txt);
}

void
em_format_format_secure(EMFormat *emf, struct _CamelStream *stream, struct _CamelMimePart *part, struct _CamelCipherValidity *valid)
{
    ((EMFormatClass *)G_OBJECT_GET_CLASS(emf))->format_secure(emf, stream, part, valid);

    if (emf->valid_parent == NULL && emf->valid != NULL) {
        camel_cipher_validity_free(emf->valid);
        emf->valid = NULL;
    }
}

/* should this be virtual? */
void
em_format_format_content(EMFormat *emf, CamelStream *stream, CamelMimePart *part)
{
    CamelDataWrapper *dw = camel_medium_get_content_object((CamelMedium *)part);

    if (camel_content_type_is (dw->mime_type, "text", "*"))
        em_format_format_text(emf, stream, (CamelDataWrapper *)part);
    else
        camel_data_wrapper_decode_to_stream(dw, stream);
}

/**
 * em_format_format_content:
 * @emf:
 * @stream: Where to write the converted text
 * @part: Part whose container is to be formatted
 *
 * Decode/output a part's content to @stream.
 **/
void
em_format_format_text(EMFormat *emf, CamelStream *stream, CamelDataWrapper *dw)
{
    CamelStreamFilter *filter_stream;
    CamelMimeFilterCharset *filter;
    const gchar *charset = NULL;
    CamelMimeFilterWindows *windows = NULL;
    CamelStream *mem_stream = NULL;
    gsize size;
    gsize max;

    if (emf->charset) {
        charset = emf->charset;
    } else if (dw->mime_type
           && (charset = camel_content_type_param (dw->mime_type, "charset"))
           && g_ascii_strncasecmp(charset, "iso-8859-", 9) == 0) {
        CamelStream *null;

        /* Since a few Windows mailers like to claim they sent
         * out iso-8859-# encoded text when they really sent
         * out windows-cp125#, do some simple sanity checking
         * before we move on... */

        null = camel_stream_null_new();
        filter_stream = camel_stream_filter_new_with_stream(null);
        camel_object_unref(null);

        windows = (CamelMimeFilterWindows *)camel_mime_filter_windows_new(charset);
        camel_stream_filter_add(filter_stream, (CamelMimeFilter *)windows);

        camel_data_wrapper_decode_to_stream(dw, (CamelStream *)filter_stream);
        camel_stream_flush((CamelStream *)filter_stream);
        camel_object_unref(filter_stream);

        charset = camel_mime_filter_windows_real_charset (windows);
    } else if (charset == NULL) {
        charset = emf->default_charset;
    }

    mem_stream = (CamelStream *)camel_stream_mem_new ();
    filter_stream = camel_stream_filter_new_with_stream(mem_stream);

    if ((filter = camel_mime_filter_charset_new_convert(charset, "UTF-8"))) {
        camel_stream_filter_add(filter_stream, (CamelMimeFilter *) filter);
        camel_object_unref(filter);
    }

    max = mail_config_get_message_limit();
    size = camel_data_wrapper_decode_to_stream(emf->mode == EM_FORMAT_SOURCE ? (CamelDataWrapper *)dw: camel_medium_get_content_object((CamelMedium *)dw), (CamelStream *)filter_stream);
    camel_stream_flush((CamelStream *)filter_stream);
    camel_object_unref(filter_stream);
    camel_stream_reset (mem_stream);

    if (max == -1 || size == -1 || size < (max * 1024) || emf->composer) {
        camel_stream_write_to_stream(mem_stream, (CamelStream *)stream);
        camel_stream_flush((CamelStream *)stream);
    } else {
        ((EMFormatClass *)G_OBJECT_GET_CLASS(emf))->format_optional(emf, stream, (CamelMimePart *)dw, mem_stream);
    }

    if (windows)
        camel_object_unref(windows);
}

/**
 * em_format_describe_part:
 * @part:
 * @mimetype:
 *
 * Generate a simple textual description of a part, @mime_type represents the
 * the content.
 *
 * Return value:
 **/
gchar *
em_format_describe_part(CamelMimePart *part, const gchar *mime_type)
{
    GString *stext;
    const gchar *filename, *description;
    gchar *content_type, *desc;

    stext = g_string_new("");
    content_type = g_content_type_from_mime_type (mime_type);
    desc = g_content_type_get_description (content_type ? content_type : mime_type);
    g_free (content_type);
    g_string_append_printf (stext, _("%s attachment"), desc ? desc : mime_type);
    g_free (desc);
    if ((filename = camel_mime_part_get_filename (part)))
        g_string_append_printf(stext, " (%s)", filename);
    if ((description = camel_mime_part_get_description(part)) &&
        (*description != 0) &&
        !(filename && (strcmp(filename, description) == 0)))
        g_string_append_printf(stext, ", \"%s\"", description);

    return g_string_free (stext, FALSE);
}

/* ********************************************************************** */

#ifdef ENABLE_SMIME
static void
emf_application_xpkcs7mime(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelCipherContext *context;
    CamelException *ex;
    CamelMimePart *opart;
    CamelCipherValidity *valid;
    struct _EMFormatCache *emfc;

    /* should this perhaps run off a key of ".secured" ? */
    emfc = g_hash_table_lookup(emf->inline_table, emf->part_id->str);
    if (emfc && emfc->valid) {
        em_format_format_secure(emf, stream, emfc->secured, camel_cipher_validity_clone(emfc->valid));
        return;
    }

    ex = camel_exception_new();

    context = camel_smime_context_new(session);

    opart = camel_mime_part_new();
    valid = camel_cipher_decrypt(context, part, opart, ex);
    if (valid == NULL) {
        em_format_format_error(emf, stream, "%s", ex->desc?ex->desc:_("Could not parse S/MIME message: Unknown error"));
        em_format_part_as(emf, stream, part, NULL);
    } else {
        if (emfc == NULL)
            emfc = emf_insert_cache(emf, emf->part_id->str);

        emfc->valid = camel_cipher_validity_clone(valid);
        camel_object_ref((emfc->secured = opart));

        em_format_format_secure(emf, stream, opart, valid);
    }

    camel_object_unref(opart);
    camel_object_unref(context);
    camel_exception_free(ex);
}
#endif

/* RFC 1740 */
static void
emf_multipart_appledouble(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelMultipart *mp = (CamelMultipart *)camel_medium_get_content_object((CamelMedium *)part);
    CamelMimePart *mime_part;
    gint len;

    if (!CAMEL_IS_MULTIPART(mp)) {
        em_format_format_source(emf, stream, part);
        return;
    }

    mime_part = camel_multipart_get_part(mp, 1);
    if (mime_part) {
        /* try the data fork for something useful, doubtful but who knows */
        len = emf->part_id->len;
        g_string_append_printf(emf->part_id, ".appledouble.1");
        em_format_part(emf, stream, mime_part);
        g_string_truncate(emf->part_id, len);
    } else
        em_format_format_source(emf, stream, part);

}

/* RFC ??? */
static void
emf_multipart_mixed(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelMultipart *mp = (CamelMultipart *)camel_medium_get_content_object((CamelMedium *)part);
    gint i, nparts, len;

    if (!CAMEL_IS_MULTIPART(mp)) {
        em_format_format_source(emf, stream, part);
        return;
    }

    len = emf->part_id->len;
    nparts = camel_multipart_get_number(mp);
    for (i = 0; i < nparts; i++) {
        part = camel_multipart_get_part(mp, i);
        g_string_append_printf(emf->part_id, ".mixed.%d", i);
        em_format_part(emf, stream, part);
        g_string_truncate(emf->part_id, len);
    }
}

/* RFC 1740 */
static void
emf_multipart_alternative(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelMultipart *mp = (CamelMultipart *)camel_medium_get_content_object((CamelMedium *)part);
    gint i, nparts, bestid = 0;
    CamelMimePart *best = NULL;

    if (!CAMEL_IS_MULTIPART(mp)) {
        em_format_format_source(emf, stream, part);
        return;
    }

    /* as per rfc, find the last part we know how to display */
    nparts = camel_multipart_get_number(mp);
    for (i = 0; i < nparts; i++) {
        CamelContentType *type;
        gchar *mime_type;

        /* is it correct to use the passed in *part here? */
        part = camel_multipart_get_part(mp, i);

        if (!part)
            continue;

        type = camel_mime_part_get_content_type (part);
        mime_type = camel_content_type_simple (type);

        camel_strdown (mime_type);

        /*if (want_plain && !strcmp (mime_type, "text/plain"))
          return part;*/

        if (em_format_find_handler(emf, mime_type)
            || (best == NULL && em_format_fallback_handler(emf, mime_type))) {
            best = part;
            bestid = i;
        }

        g_free(mime_type);
    }

    if (best) {
        gint len = emf->part_id->len;

        g_string_append_printf(emf->part_id, ".alternative.%d", bestid);
        em_format_part(emf, stream, best);
        g_string_truncate(emf->part_id, len);
    } else
        emf_multipart_mixed(emf, stream, part, info);
}

static void
emf_multipart_encrypted(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelCipherContext *context;
    CamelException *ex;
    const gchar *protocol;
    CamelMimePart *opart;
    CamelCipherValidity *valid;
    CamelMultipartEncrypted *mpe;
    struct _EMFormatCache *emfc;

    /* should this perhaps run off a key of ".secured" ? */
    emfc = g_hash_table_lookup(emf->inline_table, emf->part_id->str);
    if (emfc && emfc->valid) {
        em_format_format_secure(emf, stream, emfc->secured, camel_cipher_validity_clone(emfc->valid));
        return;
    }

    mpe = (CamelMultipartEncrypted*)camel_medium_get_content_object((CamelMedium *)part);
    if (!CAMEL_IS_MULTIPART_ENCRYPTED(mpe)) {
        em_format_format_error(emf, stream, _("Could not parse MIME message. Displaying as source."));
        em_format_format_source(emf, stream, part);
        return;
    }

    /* Currently we only handle RFC2015-style PGP encryption. */
    protocol = camel_content_type_param(((CamelDataWrapper *)mpe)->mime_type, "protocol");
    if (!protocol || g_ascii_strcasecmp (protocol, "application/pgp-encrypted") != 0) {
        em_format_format_error(emf, stream, _("Unsupported encryption type for multipart/encrypted"));
        em_format_part_as(emf, stream, part, "multipart/mixed");
        return;
    }

    ex = camel_exception_new();
    context = camel_gpg_context_new(emf->session);
    opart = camel_mime_part_new();
    valid = camel_cipher_decrypt(context, part, opart, ex);
    if (valid == NULL) {
        em_format_format_error(emf, stream, ex->desc?_("Could not parse PGP/MIME message"):_("Could not parse PGP/MIME message: Unknown error"));
        if (ex->desc)
            em_format_format_error(emf, stream, "%s", ex->desc);
        em_format_part_as(emf, stream, part, "multipart/mixed");
    } else {
        if (emfc == NULL)
            emfc = emf_insert_cache(emf, emf->part_id->str);

        emfc->valid = camel_cipher_validity_clone(valid);
        camel_object_ref((emfc->secured = opart));

        em_format_format_secure(emf, stream, opart, valid);
    }

    /* TODO: Make sure when we finalise this part, it is zero'd out */
    camel_object_unref(opart);
    camel_object_unref(context);
    camel_exception_free(ex);
}

static void
emf_write_related(EMFormat *emf, CamelStream *stream, EMFormatPURI *puri)
{
    em_format_format_content(emf, stream, puri->part);
    camel_stream_close(stream);
}

/* RFC 2387 */
static void
emf_multipart_related(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelMultipart *mp = (CamelMultipart *)camel_medium_get_content_object((CamelMedium *)part);
    CamelMimePart *body_part, *display_part = NULL;
    CamelContentType *content_type;
    const gchar *start;
    gint i, nparts, partidlen, displayid = 0;
    gchar *oldpartid;
    struct _EMFormatPURITree *ptree;
    EMFormatPURI *puri, *purin;

    if (!CAMEL_IS_MULTIPART(mp)) {
        em_format_format_source(emf, stream, part);
        return;
    }

    /* FIXME: put this stuff in a shared function */
    nparts = camel_multipart_get_number(mp);
    content_type = camel_mime_part_get_content_type(part);
    start = camel_content_type_param (content_type, "start");
    if (start && strlen(start)>2) {
        gint len;
        const gchar *cid;

        /* strip <>'s */
        len = strlen (start) - 2;
        start++;

        for (i=0; i<nparts; i++) {
            body_part = camel_multipart_get_part(mp, i);
            cid = camel_mime_part_get_content_id(body_part);

            if (cid && !strncmp(cid, start, len) && strlen(cid) == len) {
                display_part = body_part;
                displayid = i;
                break;
            }
        }
    } else {
        display_part = camel_multipart_get_part(mp, 0);
    }

    if (display_part == NULL) {
        emf_multipart_mixed(emf, stream, part, info);
        return;
    }

    em_format_push_level(emf);

    oldpartid = g_strdup(emf->part_id->str);
    partidlen = emf->part_id->len;

    /* queue up the parts for possible inclusion */
    for (i = 0; i < nparts; i++) {
        body_part = camel_multipart_get_part(mp, i);
        if (body_part != display_part) {
            /* set the partid since add_puri uses it */
            g_string_append_printf(emf->part_id, ".related.%d", i);
            puri = em_format_add_puri(emf, sizeof(EMFormatPURI), NULL, body_part, emf_write_related);
            g_string_truncate(emf->part_id, partidlen);
            d(printf(" part '%s' '%s' added\n", puri->uri?puri->uri:"", puri->cid));
        }
    }

    g_string_append_printf(emf->part_id, ".related.%d", displayid);
    em_format_part(emf, stream, display_part);
    g_string_truncate(emf->part_id, partidlen);
    camel_stream_flush(stream);

    ptree = emf->pending_uri_level;
    puri = (EMFormatPURI *)ptree->uri_list.head;
    purin = puri->next;
    while (purin) {
        if (puri->use_count == 0) {
            d(printf("part '%s' '%s' used '%d'\n", puri->uri?puri->uri:"", puri->cid, puri->use_count));
            if (puri->func == emf_write_related) {
                g_string_printf(emf->part_id, "%s", puri->part_id);
                em_format_part(emf, stream, puri->part);
            } else {
                d(printf("unreferenced uri generated by format code: %s\n", puri->uri?puri->uri:puri->cid));
            }
        }
        puri = purin;
        purin = purin->next;
    }

    g_string_printf(emf->part_id, "%s", oldpartid);
    g_free(oldpartid);

    em_format_pull_level(emf);
}

static void
emf_multipart_signed(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelMimePart *cpart;
    CamelMultipartSigned *mps;
    CamelCipherContext *cipher = NULL;
    struct _EMFormatCache *emfc;

    /* should this perhaps run off a key of ".secured" ? */
    emfc = g_hash_table_lookup(emf->inline_table, emf->part_id->str);
    if (emfc && emfc->valid) {
        em_format_format_secure(emf, stream, emfc->secured, camel_cipher_validity_clone(emfc->valid));
        return;
    }

    mps = (CamelMultipartSigned *)camel_medium_get_content_object((CamelMedium *)part);
    if (!CAMEL_IS_MULTIPART_SIGNED(mps)
        || (cpart = camel_multipart_get_part((CamelMultipart *)mps, CAMEL_MULTIPART_SIGNED_CONTENT)) == NULL) {
        em_format_format_error(emf, stream, _("Could not parse MIME message. Displaying as source."));
        em_format_format_source(emf, stream, part);
        return;
    }

    /* FIXME: Should be done via a plugin interface */
    /* FIXME: duplicated in em-format-html-display.c */
    if (mps->protocol) {
#ifdef ENABLE_SMIME
        if (g_ascii_strcasecmp("application/x-pkcs7-signature", mps->protocol) == 0
            || g_ascii_strcasecmp("application/pkcs7-signature", mps->protocol) == 0)
            cipher = camel_smime_context_new(emf->session);
        else
#endif
            if (g_ascii_strcasecmp("application/pgp-signature", mps->protocol) == 0)
                cipher = camel_gpg_context_new(emf->session);
    }

    if (cipher == NULL) {
        em_format_format_error(emf, stream, _("Unsupported signature format"));
        em_format_part_as(emf, stream, part, "multipart/mixed");
    } else {
        CamelException *ex = camel_exception_new();
        CamelCipherValidity *valid;

        valid = camel_cipher_verify(cipher, part, ex);
        if (valid == NULL) {
            em_format_format_error(emf, stream, ex->desc?_("Error verifying signature"):_("Unknown error verifying signature"));
            if (ex->desc)
                em_format_format_error(emf, stream, "%s", ex->desc);
            em_format_part_as(emf, stream, part, "multipart/mixed");
        } else {
            if (emfc == NULL)
                emfc = emf_insert_cache(emf, emf->part_id->str);

            emfc->valid = camel_cipher_validity_clone(valid);
            camel_object_ref((emfc->secured = cpart));

            em_format_format_secure(emf, stream, cpart, valid);
        }

        camel_exception_free(ex);
        camel_object_unref(cipher);
    }
}

static void
emf_message_rfc822(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    CamelDataWrapper *dw = camel_medium_get_content_object((CamelMedium *)part);
    const EMFormatHandler *handle;
    gint len;

    if (!CAMEL_IS_MIME_MESSAGE(dw)) {
        em_format_format_source(emf, stream, part);
        return;
    }

    len = emf->part_id->len;
    g_string_append_printf(emf->part_id, ".rfc822");

    handle = em_format_find_handler(emf, "x-evolution/message/rfc822");
    if (handle)
        handle->handler(emf, stream, (CamelMimePart *)dw, handle);

    g_string_truncate(emf->part_id, len);
}

static void
emf_message_deliverystatus(EMFormat *emf, CamelStream *stream, CamelMimePart *part, const EMFormatHandler *info)
{
    em_format_format_text(emf, stream, (CamelDataWrapper *)part);
}

static void
emf_inlinepgp_signed(EMFormat *emf, CamelStream *stream, CamelMimePart *ipart, EMFormatHandler *info)
{
    CamelStreamFilter *filtered_stream;
    CamelMimeFilterPgp *pgp_filter;
    CamelContentType *content_type;
    CamelCipherContext *cipher;
    CamelCipherValidity *valid;
    CamelDataWrapper *dw;
    CamelMimePart *opart;
    CamelStream *ostream;
    CamelException *ex;
    gchar *type;

    if (!ipart) {
        em_format_format_error(emf, stream, _("Unknown error verifying signature"));
        return;
    }

    ex = camel_exception_new();
    cipher = camel_gpg_context_new(emf->session);
    /* Verify the signature of the message */
    valid = camel_cipher_verify(cipher, ipart, ex);
    if (!valid) {
        em_format_format_error(emf, stream, ex->desc?_("Error verifying signature"):_("Unknown error verifying signature"));
        if (ex->desc)
            em_format_format_error(emf, stream, "%s", ex->desc);
        em_format_format_source(emf, stream, ipart);
        /* I think this will loop: em_format_part_as(emf, stream, part, "text/plain"); */
        camel_exception_free(ex);
        camel_object_unref(cipher);
        return;
    }

    /* Setup output stream */
    ostream = camel_stream_mem_new();
    filtered_stream = camel_stream_filter_new_with_stream(ostream);

    /* Add PGP header / footer filter */
    pgp_filter = (CamelMimeFilterPgp *)camel_mime_filter_pgp_new();
    camel_stream_filter_add(filtered_stream, (CamelMimeFilter *)pgp_filter);
    camel_object_unref(pgp_filter);

    /* Pass through the filters that have been setup */
    dw = camel_medium_get_content_object((CamelMedium *)ipart);
    camel_data_wrapper_decode_to_stream(dw, (CamelStream *)filtered_stream);
    camel_stream_flush((CamelStream *)filtered_stream);
    camel_object_unref(filtered_stream);

    /* Create a new text/plain MIME part containing the signed content preserving the original part's Content-Type params */
    content_type = camel_mime_part_get_content_type (ipart);
    type = camel_content_type_format (content_type);
    content_type = camel_content_type_decode (type);
    g_free (type);

    g_free (content_type->type);
    content_type->type = g_strdup ("text");
    g_free (content_type->subtype);
    content_type->subtype = g_strdup ("plain");
    type = camel_content_type_format (content_type);
    camel_content_type_unref (content_type);

    dw = camel_data_wrapper_new ();
    camel_data_wrapper_construct_from_stream (dw, ostream);
    camel_data_wrapper_set_mime_type (dw, type);
    g_free (type);

    opart = camel_mime_part_new ();
    camel_medium_set_content_object ((CamelMedium *) opart, dw);
    camel_data_wrapper_set_mime_type_field ((CamelDataWrapper *) opart, dw->mime_type);

    /* Pass it off to the real formatter */
    em_format_format_secure(emf, stream, opart, valid);

    /* Clean Up */
    camel_object_unref(dw);
    camel_object_unref(opart);
    camel_object_unref(ostream);
    camel_object_unref(cipher);
    camel_exception_free(ex);
}

static void
emf_inlinepgp_encrypted(EMFormat *emf, CamelStream *stream, CamelMimePart *ipart, EMFormatHandler *info)
{
    CamelCipherContext *cipher;
    CamelCipherValidity *valid;
    CamelException *ex;
    CamelMimePart *opart;
    CamelDataWrapper *dw;
    gchar *mime_type;

    cipher = camel_gpg_context_new(emf->session);
    ex = camel_exception_new();
    opart = camel_mime_part_new();
    /* Decrypt the message */
    valid = camel_cipher_decrypt (cipher, ipart, opart, ex);
    if (!valid) {
        em_format_format_error(emf, stream, ex->desc?_("Could not parse PGP message"):_("Could not parse PGP message: Unknown error"));
        if (ex->desc)
            em_format_format_error(emf, stream, "%s", ex->desc);
        em_format_format_source(emf, stream, ipart);
        /* I think this will loop: em_format_part_as(emf, stream, part, "text/plain"); */
        camel_exception_free(ex);
        camel_object_unref(cipher);
        camel_object_unref(opart);
        return;
    }

    dw = camel_medium_get_content_object ((CamelMedium *)opart);
    mime_type = camel_data_wrapper_get_mime_type (dw);

    /* this ensures to show the 'opart' as inlined, if possible */
    if (mime_type && g_ascii_strcasecmp (mime_type, "application/octet-stream") == 0) {
        const gchar *snoop = em_utils_snoop_type (opart);

        if (snoop)
            camel_data_wrapper_set_mime_type (dw, snoop);
    }

    g_free (mime_type);

    /* Pass it off to the real formatter */
    em_format_format_secure(emf, stream, opart, valid);

    /* Clean Up */
    camel_object_unref(opart);
    camel_object_unref (cipher);
    camel_exception_free (ex);
}

static EMFormatHandler type_builtin_table[] = {
#ifdef ENABLE_SMIME
    { (gchar *) "application/x-pkcs7-mime", (EMFormatFunc)emf_application_xpkcs7mime, EM_FORMAT_HANDLER_INLINE_DISPOSITION },
#endif
    { (gchar *) "multipart/alternative", emf_multipart_alternative },
    { (gchar *) "multipart/appledouble", emf_multipart_appledouble },
    { (gchar *) "multipart/encrypted", emf_multipart_encrypted },
    { (gchar *) "multipart/mixed", emf_multipart_mixed },
    { (gchar *) "multipart/signed", emf_multipart_signed },
    { (gchar *) "multipart/related", emf_multipart_related },
    { (gchar *) "multipart/*", emf_multipart_mixed },
    { (gchar *) "message/rfc822", emf_message_rfc822, EM_FORMAT_HANDLER_INLINE },
    { (gchar *) "message/news", emf_message_rfc822, EM_FORMAT_HANDLER_INLINE },
    { (gchar *) "message/delivery-status", emf_message_deliverystatus },
    { (gchar *) "message/*", emf_message_rfc822, EM_FORMAT_HANDLER_INLINE },

    /* Insert brokenly-named parts here */
#ifdef ENABLE_SMIME
    { (gchar *) "application/pkcs7-mime", (EMFormatFunc)emf_application_xpkcs7mime, EM_FORMAT_HANDLER_INLINE_DISPOSITION },
#endif

    /* internal types */
    { (gchar *) "application/x-inlinepgp-signed", (EMFormatFunc)emf_inlinepgp_signed },
    { (gchar *) "application/x-inlinepgp-encrypted", (EMFormatFunc)emf_inlinepgp_encrypted },
};

static void
emf_builtin_init(EMFormatClass *klass)
{
    gint i;

    for (i=0;i<sizeof(type_builtin_table)/sizeof(type_builtin_table[0]);i++)
        g_hash_table_insert(klass->type_handlers, (gpointer) type_builtin_table[i].mime_type, &type_builtin_table[i]);
}