aboutsummaryrefslogtreecommitdiffstats
path: root/mail/mail-ops.c
blob: d0c5e272a12712a31987b859845c496760e347c7 (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
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* mail-ops.c: callbacks for the mail toolbar/menus */

/* 
 * Authors: Dan Winship <danw@ximian.com>
 *          Jeffrey Stedfast <fejj@ximian.com>
 *          Peter Williams <peterw@ximian.com>
 *          Michael Zucchi <notzed@ximian.com>
 *
 * Copyright 2000,2001 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of version 2 of the GNU General Public 
 * License as published by the Free Software Foundation.
 *
 * 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 Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

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

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <string.h>
#include <errno.h>
#include <libgnome/gnome-exec.h>
#include <gal/util/e-util.h>

#include <camel/camel-mime-filter-from.h>
#include <camel/camel-stream-filter.h>
#include <camel/camel-stream-fs.h>
#include <camel/camel-mime-filter-charset.h>
#include <camel/camel-disco-folder.h>
#include <camel/camel-disco-store.h>
#include <camel/camel-operation.h>
#include <camel/camel-vtrash-folder.h>
#include <camel/camel-vee-store.h>
#include <camel/camel-transport.h>

#include "mail-component.h"
#include "mail-config.h"
#include "mail-tools.h"
#include "mail-ops.h"
#include "mail-vfolder.h"
#include "mail-session.h"
#include "composer/e-msg-composer.h"

#include "em-filter-rule.h"

#include "mail-mt.h"

#include "em-utils.h"

#define w(x)
#define d(x) 

/* used for both just filtering a folder + uid's, and for filtering a whole folder */
/* used both for fetching mail, and for filtering mail */
struct _filter_mail_msg {
    struct _mail_msg msg;
    
    CamelFolder *source_folder; /* where they come from */
    GPtrArray *source_uids; /* uids to copy, or NULL == copy all */
    CamelUIDCache *cache;  /* UID cache if we are to cache the uids, NULL otherwise */
    CamelOperation *cancel;
    CamelFilterDriver *driver;
    int delete;     /* delete messages after filtering them? */
    CamelFolder *destination; /* default destination for any messages, NULL for none */
};

/* since fetching also filters, we subclass the data here */
struct _fetch_mail_msg {
    struct _filter_mail_msg fmsg;

    CamelOperation *cancel; /* we have our own cancellation struct, the other should be empty */
    int keep;       /* keep on server? */

    char *source_uri;

    void (*done)(char *source, void *data);
    void *data;
};

static char *
em_filter_folder_element_describe (struct _mail_msg *mm, int complete)
{
    return g_strdup (_("Filtering Folder"));
}

/* filter a folder, or a subset thereof, uses source_folder/source_uids */
/* this is shared with fetch_mail */
static void
em_filter_folder_element_filter (struct _mail_msg *mm)
{
    struct _filter_mail_msg *m = (struct _filter_mail_msg *)mm;
    CamelFolder *folder;
    GPtrArray *uids, *folder_uids = NULL;
    
    if (m->cancel)
        camel_operation_register (m->cancel);
    
    folder = m->source_folder;
    
    if (folder == NULL || camel_folder_get_message_count (folder) == 0) {
        if (m->cancel)
            camel_operation_unregister (m->cancel);
        return;
    }
    
    if (m->destination) {
        camel_folder_freeze (m->destination);
        camel_filter_driver_set_default_folder (m->driver, m->destination);
    }
    
    camel_folder_freeze (folder);
    
    if (m->source_uids)
        uids = m->source_uids;
    else
        folder_uids = uids = camel_folder_get_uids (folder);
    
    camel_filter_driver_filter_folder (m->driver, folder, m->cache, uids, m->delete, &mm->ex);
    camel_filter_driver_flush (m->driver, &mm->ex);
    
    if (folder_uids)
        camel_folder_free_uids (folder, folder_uids);
    
    /* sync our source folder */
    if (!m->cache)
        camel_folder_sync (folder, FALSE, camel_exception_is_set (&mm->ex) ? NULL : &mm->ex);
    camel_folder_thaw (folder);
    
    if (m->destination)
        camel_folder_thaw (m->destination);

    /* this may thaw/unref source folders, do it here so we dont do it in the main thread
       see also fetch_mail_fetch() below */
    camel_object_unref(m->driver);
    m->driver = NULL;
    
    if (m->cancel)
        camel_operation_unregister (m->cancel);
}

static void
em_filter_folder_element_filtered (struct _mail_msg *mm)
{
}

static void
em_filter_folder_element_free (struct _mail_msg *mm)
{
    struct _filter_mail_msg *m = (struct _filter_mail_msg *)mm;
    
    if (m->source_folder)
        camel_object_unref (m->source_folder);
    
    if (m->source_uids)
        em_utils_uids_free (m->source_uids);
    
    if (m->cancel)
        camel_operation_unref (m->cancel);
    
    if (m->destination)
        camel_object_unref (m->destination);
    
    if (m->driver)
        camel_object_unref (m->driver);
    
    mail_session_flush_filter_log ();
}

static struct _mail_msg_op em_filter_folder_element_op = {
    em_filter_folder_element_describe,  /* we do our own progress reporting? */
    em_filter_folder_element_filter,
    em_filter_folder_element_filtered,
    em_filter_folder_element_free,
};

void
mail_filter_folder (CamelFolder *source_folder, GPtrArray *uids,
            const char *type, gboolean notify,
            CamelOperation *cancel)
{
    struct _filter_mail_msg *m;
    
    m = mail_msg_new (&em_filter_folder_element_op, NULL, sizeof (*m));
    m->source_folder = source_folder;
    camel_object_ref (source_folder);
    m->source_uids = uids;
    m->cache = NULL;
    m->delete = FALSE;
    if (cancel) {
        m->cancel = cancel;
        camel_operation_ref (cancel);
    }
    
    m->driver = camel_session_get_filter_driver (session, type, NULL);
    
    if (!notify) {
        /* FIXME: have a #define NOTIFY_FILTER_NAME macro? */
        /* the filter name has to stay in sync with mail-session::get_filter_driver */
        camel_filter_driver_remove_rule_by_name (m->driver, "new-mail-notification");
    }
    
    e_thread_put (mail_thread_new, (EMsg *)m);
}

/* convenience functions for it */
void
mail_filter_on_demand (CamelFolder *folder, GPtrArray *uids)
{
    mail_filter_folder (folder, uids, FILTER_SOURCE_DEMAND, FALSE, NULL);
}

void
mail_filter_junk (CamelFolder *folder, GPtrArray *uids)
{
    mail_filter_folder (folder, uids, FILTER_SOURCE_JUNKTEST, FALSE, NULL);
}

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

/* Temporary workaround for various issues. Gone before 0.11 */
static char *
uid_cachename_hack (CamelStore *store)
{
    CamelURL *url = CAMEL_SERVICE (store)->url;
    char *encoded_url, *filename;
    const char *evolution_dir;
    
    encoded_url = g_strdup_printf ("%s%s%s@%s", url->user,
                       url->authmech ? ";auth=" : "",
                       url->authmech ? url->authmech : "",
                       url->host);
    e_filename_make_safe (encoded_url);
    
    evolution_dir = mail_component_peek_base_directory (mail_component_peek ());
    filename = g_build_filename (evolution_dir, "mail", "pop", encoded_url, "uid-cache", NULL);
    g_free (encoded_url);
    
    return filename;
}

static char *
fetch_mail_describe (struct _mail_msg *mm, int complete)
{
    return g_strdup (_("Fetching Mail"));
}

static void
fetch_mail_fetch (struct _mail_msg *mm)
{
    struct _fetch_mail_msg *m = (struct _fetch_mail_msg *)mm;
    struct _filter_mail_msg *fm = (struct _filter_mail_msg *)mm;
    int i;

    if (m->cancel)
        camel_operation_register (m->cancel);
    
    if ((fm->destination = mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_LOCAL_INBOX)) == NULL)
        goto fail;
    camel_object_ref(fm->destination);

    /* FIXME: this should support keep_on_server too, which would then perform a spool
       access thingy, right?  problem is matching raw messages to uid's etc. */
    if (!strncmp (m->source_uri, "mbox:", 5)) {
        char *path = mail_tool_do_movemail (m->source_uri, &mm->ex);
        
        if (path && !camel_exception_is_set (&mm->ex)) {
            camel_folder_freeze (fm->destination);
            camel_filter_driver_set_default_folder (fm->driver, fm->destination);
            camel_filter_driver_filter_mbox (fm->driver, path, m->source_uri, &mm->ex);
            camel_folder_thaw (fm->destination);
            
            if (!camel_exception_is_set (&mm->ex))
                unlink (path);
        }
        g_free (path);
    } else {
        CamelFolder *folder = fm->source_folder = mail_tool_get_inbox (m->source_uri, &mm->ex);
        
        if (folder) {
            /* this handles 'keep on server' stuff, if we have any new uid's to copy
               across, we need to copy them to a new array 'cause of the way fetch_mail_free works */
            CamelUIDCache *cache = NULL;
            char *cachename;
            
            cachename = uid_cachename_hack (folder->parent_store);
            cache = camel_uid_cache_new (cachename);
            g_free (cachename);
            
            if (cache) {
                GPtrArray *folder_uids, *cache_uids, *uids;
                
                folder_uids = camel_folder_get_uids (folder);
                cache_uids = camel_uid_cache_get_new_uids (cache, folder_uids);
                if (cache_uids) {
                    /* need to copy this, sigh */
                    fm->source_uids = uids = g_ptr_array_new ();
                    g_ptr_array_set_size (uids, cache_uids->len);
                    for (i = 0; i < cache_uids->len; i++)
                        uids->pdata[i] = g_strdup (cache_uids->pdata[i]);
                    camel_uid_cache_free_uids (cache_uids);
                    
                    fm->cache = cache;
                    em_filter_folder_element_filter (mm);
                    
                    /* need to uncancel so writes/etc. don't fail */
                    if (mm->ex.id == CAMEL_EXCEPTION_USER_CANCEL)
                        camel_operation_uncancel(NULL);

                    /* save the cache of uids that we've just downloaded */
                    camel_uid_cache_save (cache);
                }

                if (fm->delete && mm->ex.id == CAMEL_EXCEPTION_NONE) {
                    /* not keep on server - just delete all the actual messages on the server */
                    for (i=0;i<folder_uids->len;i++) {
                        d(printf("force delete uid '%s'\n", (char *)folder_uids->pdata[i]));
                        camel_folder_delete_message(folder, folder_uids->pdata[i]);
                    }
                }

                if (fm->delete || cache_uids) {
                    /* expunge messages (downloaded so far) */
                    camel_folder_sync(folder, fm->delete, NULL);
                }

                camel_uid_cache_destroy (cache);
                camel_folder_free_uids (folder, folder_uids);
            } else {
                em_filter_folder_element_filter (mm);
            }
            
            /* we unref the source folder here since we
               may now block in finalize (we try to
               disconnect cleanly) */
            camel_object_unref (fm->source_folder);
            fm->source_folder = NULL;
        }
    }
fail:   
    if (m->cancel)
        camel_operation_unregister (m->cancel);
    
    /* we unref this here as it may have more work to do (syncing
       folders and whatnot) before we are really done */
    /* should this be cancellable too? (i.e. above unregister above) */
    if (fm->driver) {
        camel_object_unref (fm->driver);
        fm->driver = NULL;
    }
}

static void
fetch_mail_fetched (struct _mail_msg *mm)
{
    struct _fetch_mail_msg *m = (struct _fetch_mail_msg *)mm;

    if (m->done)
        m->done (m->source_uri, m->data);
}

static void
fetch_mail_free (struct _mail_msg *mm)
{
    struct _fetch_mail_msg *m = (struct _fetch_mail_msg *)mm;
    
    g_free (m->source_uri);
    if (m->cancel)
        camel_operation_unref (m->cancel);

    em_filter_folder_element_free (mm);
}

static struct _mail_msg_op fetch_mail_op = {
    fetch_mail_describe, /* we do our own progress reporting */
    fetch_mail_fetch,
    fetch_mail_fetched,
    fetch_mail_free,
};

/* ouch, a 'do everything' interface ... */
void
mail_fetch_mail (const char *source, int keep, const char *type, CamelOperation *cancel,
         CamelFilterGetFolderFunc get_folder, void *get_data,
         CamelFilterStatusFunc *status, void *status_data,
         void (*done)(char *source, void *data), void *data)
{
    struct _fetch_mail_msg *m;
    struct _filter_mail_msg *fm;
    
    m = mail_msg_new (&fetch_mail_op, NULL, sizeof (*m));
    fm = (struct _filter_mail_msg *)m;
    m->source_uri = g_strdup (source);
    fm->delete = !keep;
    fm->cache = NULL;
    if (cancel) {
        m->cancel = cancel;
        camel_operation_ref (cancel);
    }
    m->done = done;
    m->data = data;

    fm->driver = camel_session_get_filter_driver (session, type, NULL);
    camel_filter_driver_set_folder_func (fm->driver, get_folder, get_data);
    if (status)
        camel_filter_driver_set_status_func (fm->driver, status, status_data);
    
    e_thread_put (mail_thread_new, (EMsg *)m);
}

/* ********************************************************************** */
/* sending stuff */
/* ** SEND MAIL *********************************************************** */

static char *normal_recipients[] = {
    CAMEL_RECIPIENT_TYPE_TO,
    CAMEL_RECIPIENT_TYPE_CC,
    CAMEL_RECIPIENT_TYPE_BCC
};

static char *resent_recipients[] = {
    CAMEL_RECIPIENT_TYPE_RESENT_TO,
    CAMEL_RECIPIENT_TYPE_RESENT_CC,
    CAMEL_RECIPIENT_TYPE_RESENT_BCC
};

/* send 1 message to a specific transport */
static void
mail_send_message (CamelMimeMessage *message, const char *destination,
           CamelFilterDriver *driver, CamelException *ex)
{
    EAccount *account = NULL;
    const CamelInternetAddress *iaddr;
    CamelAddress *from, *recipients;
    CamelMessageInfo *info;
    CamelTransport *xport = NULL;
    char *transport_url = NULL;
    char *sent_folder_uri = NULL;
    const char *resent_from;
    CamelFolder *folder = NULL;
    GString *err = NULL;
    XEvolution *xev;
    int i;
    
    camel_medium_set_header (CAMEL_MEDIUM (message), "X-Mailer",
                 "Evolution " VERSION SUB_VERSION " " VERSION_COMMENT);
    
    xev = mail_tool_remove_xevolution_headers (message);
    
    if (xev->account) {
        char *name;
        
        name = g_strstrip (g_strdup (xev->account));
        account = mail_config_get_account_by_name (name);
        g_free (name);
        
        if (account) {
            if (account->transport && account->transport->url)
                transport_url = g_strdup (account->transport->url);
            
            sent_folder_uri = g_strdup (account->sent_folder_uri);
        }
    }
    
    if (!account) {
        /* default back to these headers */
        if (xev->transport)
            transport_url = g_strstrip (g_strdup (xev->transport));
        
        if (xev->fcc)
            sent_folder_uri = g_strstrip (g_strdup (xev->fcc));
    }
    
    xport = camel_session_get_transport (session, transport_url ? transport_url : destination, ex);
    g_free (transport_url);
    if (!xport) {
        mail_tool_restore_xevolution_headers (message, xev);
        mail_tool_destroy_xevolution (xev);
        g_free (sent_folder_uri);
        return;
    }
    
    from = (CamelAddress *) camel_internet_address_new ();
    resent_from = camel_medium_get_header (CAMEL_MEDIUM (message), "Resent-From");
    if (resent_from) {
        camel_address_decode (from, resent_from);
    } else {
        iaddr = camel_mime_message_get_from (message);
        camel_address_copy (from, CAMEL_ADDRESS (iaddr));
    }
    
    recipients = (CamelAddress *) camel_internet_address_new ();
    for (i = 0; i < 3; i++) {
        const char *type;
        
        type = resent_from ? resent_recipients[i] : normal_recipients[i];
        iaddr = camel_mime_message_get_recipients (message, type);
        camel_address_cat (recipients, CAMEL_ADDRESS (iaddr));
    }
    
    camel_transport_send_to (xport, message, from, recipients, ex);
    camel_object_unref (recipients);
    camel_object_unref (from);
    
    mail_tool_restore_xevolution_headers (message, xev);
    mail_tool_destroy_xevolution (xev);
    
    camel_object_unref (xport);
    if (camel_exception_is_set (ex)) {
        g_free (sent_folder_uri);
        return;
    }
    
    /* post-process */
    info = camel_message_info_new ();
    info->flags = CAMEL_MESSAGE_SEEN;
    
    if (sent_folder_uri) {
        folder = mail_tool_uri_to_folder (sent_folder_uri, 0, ex);
        camel_exception_clear (ex);
        g_free (sent_folder_uri);
    }
    
    if (!folder) {
        folder = mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_SENT);   
        camel_object_ref(folder);
    }
    
    if (driver) {
        camel_filter_driver_filter_message (driver, message, info,
                            NULL, NULL, NULL, "", ex);
        
        if (camel_exception_is_set (ex)) {
            if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_USER_CANCEL)
                goto exit;
            
            /* save this error */
            err = g_string_new ("");
            g_string_append_printf (err, _("Failed to apply outgoing filters: %s"),
                        camel_exception_get_description (ex));
        }
    }
    
 retry_append:
    camel_exception_clear (ex);
    camel_folder_append_message (folder, message, info, NULL, ex);
    if (camel_exception_is_set (ex)) {
        CamelFolder *sent_folder;

        if (camel_exception_get_id (ex) == CAMEL_EXCEPTION_USER_CANCEL)
            goto exit;

        sent_folder = mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_SENT);

        if (err == NULL)
            err = g_string_new ("");
        else
            g_string_append (err, "\n\n");
        
        if (folder != sent_folder) {
            const char *name;
            
            camel_object_get (folder, NULL, CAMEL_OBJECT_DESCRIPTION, (char **) &name, 0);
            g_string_append_printf (err, _("Failed to append to %s: %s\n"
                        "Appending to local `Sent' folder instead."),
                        name, camel_exception_get_description (ex));
            camel_object_ref (sent_folder);
            camel_object_unref (folder);
            folder = sent_folder;
            
            goto retry_append;
        } else {
            g_string_append_printf (err, _("Failed to append to local `Sent' folder: %s"),
                        camel_exception_get_description (ex));
        }
    }
    
    if (err != NULL) {
        /* set the culmulative exception report */
        camel_exception_set (ex, CAMEL_EXCEPTION_SYSTEM, err->str);
    }
        
 exit:
    
    camel_folder_sync (folder, FALSE, NULL);
    camel_message_info_free (info);
    camel_object_unref (folder);
    
    if (err != NULL)
        g_string_free (err, TRUE);
}

/* ** SEND MAIL QUEUE ***************************************************** */

struct _send_queue_msg {
    struct _mail_msg msg;

    CamelFolder *queue;
    char *destination;

    CamelFilterDriver *driver;
    CamelOperation *cancel;

    /* we use camelfilterstatusfunc, even though its not the filter doing it */
    CamelFilterStatusFunc *status;
    void *status_data;

    void (*done)(char *destination, void *data);
    void *data;
};

static void
report_status (struct _send_queue_msg *m, enum camel_filter_status_t status, int pc, const char *desc, ...)
{
    va_list ap;
    char *str;
    
    if (m->status) {
        va_start (ap, desc);
        str = g_strdup_vprintf (desc, ap);
        va_end (ap);
        m->status (m->driver, status, pc, str, m->status_data);
        g_free (str);
    }
}

static void
send_queue_send(struct _mail_msg *mm)
{
    struct _send_queue_msg *m = (struct _send_queue_msg *)mm;
    CamelFolder *sent_folder = mail_component_get_folder(NULL, MAIL_COMPONENT_FOLDER_SENT);
    GPtrArray *uids, *send_uids = NULL;
    CamelException ex;
    int i, j;
    
    d(printf("sending queue\n"));
    
    if (!(uids = camel_folder_get_uids (m->queue)))
        return;
    
    send_uids = g_ptr_array_sized_new (uids->len);
    for (i = 0, j = 0; i < uids->len; i++) {
        CamelMessageInfo *info;
        
        info = camel_folder_get_message_info (m->queue, uids->pdata[i]);
        if (info && info->flags & CAMEL_MESSAGE_DELETED)
            continue;
        
        send_uids->pdata[j++] = uids->pdata[i];
    }
    
    send_uids->len = j;
    if (send_uids->len == 0) {
        /* nothing to send */
        camel_folder_free_uids (m->queue, uids);
        g_ptr_array_free (send_uids, TRUE);
        return;
    }
    
    if (m->cancel)
        camel_operation_register (m->cancel);
    
    camel_exception_init (&ex);
    
    for (i = 0, j = 0; i < send_uids->len; i++) {
        int pc = (100 * i) / send_uids->len;
        CamelMimeMessage *message;
        
        report_status (m, CAMEL_FILTER_STATUS_START, pc, _("Sending message %d of %d"), i+1, send_uids->len);
        
        if (!(message = camel_folder_get_message (m->queue, send_uids->pdata[i], &ex))) {
            /* I guess ignore errors where we can't get the message (should never happen anyway)? */
            camel_exception_clear (&ex);
            continue;
        }
        
        mail_send_message (message, m->destination, m->driver, &ex);
        if (!camel_exception_is_set (&ex)) {
            camel_folder_set_message_flags (m->queue, send_uids->pdata[i], CAMEL_MESSAGE_DELETED|CAMEL_MESSAGE_SEEN, ~0);
        } else if (ex.id != CAMEL_EXCEPTION_USER_CANCEL) {
            /* merge exceptions into one */
            if (camel_exception_is_set (&mm->ex))
                camel_exception_setv (&mm->ex, CAMEL_EXCEPTION_SYSTEM, "%s\n\n%s", mm->ex.desc, ex.desc);
            else
                camel_exception_xfer (&mm->ex, &ex);
            camel_exception_clear (&ex);
            
            /* keep track of the number of failures */
            j++;
        } else {
            /* transfer the USER_CANCEL exeption to the async op exception and then break */
            camel_exception_xfer (&mm->ex, &ex);
            break;
        }
    }
    
    j += (send_uids->len - i);
    
    if (j > 0)
        report_status (m, CAMEL_FILTER_STATUS_END, 100, _("Failed to send %d of %d messages"), j, send_uids->len);
    else if (mm->ex.id == CAMEL_EXCEPTION_USER_CANCEL)
        report_status (m, CAMEL_FILTER_STATUS_END, 100, _("Cancelled."));
    else
        report_status (m, CAMEL_FILTER_STATUS_END, 100, _("Complete."));
    
    if (m->driver) {
        camel_object_unref (m->driver);
        m->driver = NULL;
    }
    
    camel_folder_free_uids (m->queue, uids);
    g_ptr_array_free (send_uids, TRUE);
    
    camel_folder_sync (m->queue, TRUE, &ex);
    camel_exception_clear (&ex);
    
    if (sent_folder) {
        camel_folder_sync (sent_folder, FALSE, &ex);
        camel_exception_clear (&ex);
    }
    
    if (m->cancel)
        camel_operation_unregister (m->cancel);
}

static void
send_queue_sent(struct _mail_msg *mm)
{
    struct _send_queue_msg *m = (struct _send_queue_msg *)mm;

    if (m->done)
        m->done(m->destination, m->data);
}

static void
send_queue_free(struct _mail_msg *mm)
{
    struct _send_queue_msg *m = (struct _send_queue_msg *)mm;
    
    if (m->driver)
        camel_object_unref(m->driver);
    camel_object_unref(m->queue);
    g_free(m->destination);
    if (m->cancel)
        camel_operation_unref(m->cancel);
}

static struct _mail_msg_op send_queue_op = {
    NULL,           /* do our own reporting, as with fetch mail */
    send_queue_send,
    send_queue_sent,
    send_queue_free,
};

/* same interface as fetch_mail, just 'cause i'm lazy today (and we need to run it from the same spot?) */
void
mail_send_queue(CamelFolder *queue, const char *destination,
        const char *type, CamelOperation *cancel,
        CamelFilterGetFolderFunc get_folder, void *get_data,
        CamelFilterStatusFunc *status, void *status_data,
        void (*done)(char *destination, void *data), void *data)
{
    struct _send_queue_msg *m;

    m = mail_msg_new(&send_queue_op, NULL, sizeof(*m));
    m->queue = queue;
    camel_object_ref(queue);
    m->destination = g_strdup(destination);
    if (cancel) {
        m->cancel = cancel;
        camel_operation_ref(cancel);
    }
    m->status = status;
    m->status_data = status_data;
    m->done = done;
    m->data = data;

    m->driver = camel_session_get_filter_driver (session, type, NULL);
    camel_filter_driver_set_folder_func (m->driver, get_folder, get_data);

    e_thread_put(mail_thread_new, (EMsg *)m);
}

/* ** APPEND MESSAGE TO FOLDER ******************************************** */

struct _append_msg {
    struct _mail_msg msg;

        CamelFolder *folder;
    CamelMimeMessage *message;
        CamelMessageInfo *info;
    char *appended_uid;

    void (*done)(CamelFolder *folder, CamelMimeMessage *msg, CamelMessageInfo *info, int ok, const char *appended_uid, void *data);
    void *data;
};

static char *
append_mail_desc (struct _mail_msg *mm, int done)
{
    return g_strdup (_("Saving message to folder"));
}

static void
append_mail_append (struct _mail_msg *mm)
{
    struct _append_msg *m = (struct _append_msg *)mm;

    camel_mime_message_set_date(m->message, CAMEL_MESSAGE_DATE_CURRENT, 0);
    camel_folder_append_message(m->folder, m->message, m->info, &m->appended_uid, &mm->ex);
}

static void
append_mail_appended (struct _mail_msg *mm)
{
    struct _append_msg *m = (struct _append_msg *)mm;

    if (m->done)
        m->done(m->folder, m->message, m->info, !camel_exception_is_set(&mm->ex), m->appended_uid, m->data);
}

static void
append_mail_free (struct _mail_msg *mm)
{
    struct _append_msg *m = (struct _append_msg *)mm;

    camel_object_unref(m->message);
    camel_object_unref(m->folder);
    g_free (m->appended_uid);
}

static struct _mail_msg_op append_mail_op = {
    append_mail_desc,
    append_mail_append,
    append_mail_appended,
    append_mail_free
};

void
mail_append_mail (CamelFolder *folder, CamelMimeMessage *message, CamelMessageInfo *info,
          void (*done)(CamelFolder *folder, CamelMimeMessage *msg, CamelMessageInfo *info, int ok, const char *appended_uid, void *data),
          void *data)
{
    struct _append_msg *m;
    
    g_assert(CAMEL_IS_FOLDER (folder));
    g_assert(CAMEL_IS_MIME_MESSAGE (message));
    
    if (!camel_medium_get_header (CAMEL_MEDIUM (message), "X-Mailer"))
        camel_medium_set_header (CAMEL_MEDIUM (message), "X-Mailer",
                     "Evolution " VERSION SUB_VERSION " " VERSION_COMMENT);

    m = mail_msg_new (&append_mail_op, NULL, sizeof (*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->message = message;
    camel_object_ref(message);
    m->info = info;
    
    m->done = done;
    m->data = data;
    
    e_thread_put (mail_thread_new, (EMsg *)m);
}

/* ** TRANSFER MESSAGES **************************************************** */

struct _transfer_msg {
    struct _mail_msg msg;

    CamelFolder *source;
    GPtrArray *uids;
    gboolean delete;
    char *dest_uri;
    guint32 dest_flags;
    
    void (*done)(gboolean ok, void *data);
    void *data;
};

static char *
transfer_messages_desc (struct _mail_msg *mm, int done)
{
    struct _transfer_msg *m = (struct _transfer_msg *)mm;

    return g_strdup_printf(m->delete?_("Moving messages to %s"):_("Copying messages to %s"),
                   m->dest_uri);
                   
}

static void
transfer_messages_transfer (struct _mail_msg *mm)
{
    struct _transfer_msg *m = (struct _transfer_msg *)mm;
    CamelFolder *dest;

    dest = mail_tool_uri_to_folder (m->dest_uri, m->dest_flags, &mm->ex);
    if (camel_exception_is_set (&mm->ex))
        return;

    if (dest == m->source) {
        camel_object_unref(dest);
        /* no-op */
        return;
    }

    camel_folder_freeze (m->source);
    camel_folder_freeze (dest);

    camel_folder_transfer_messages_to (m->source, m->uids, dest, NULL, m->delete, &mm->ex);

    /* make sure all deleted messages are marked as seen */

    if (m->delete) {
        int i;

        for (i = 0; i < m->uids->len; i++)
            camel_folder_set_message_flags (m->source, m->uids->pdata[i], 
                            CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);
    }

    camel_folder_thaw (m->source);
    camel_folder_thaw (dest);
    camel_folder_sync (dest, FALSE, NULL);
    camel_object_unref (dest);
}

static void
transfer_messages_transferred (struct _mail_msg *mm)
{
    struct _transfer_msg *m = (struct _transfer_msg *)mm;
    
    if (m->done)
        m->done (!camel_exception_is_set (&mm->ex), m->data);
}

static void
transfer_messages_free (struct _mail_msg *mm)
{
    struct _transfer_msg *m = (struct _transfer_msg *)mm;
    
    camel_object_unref (m->source);
    g_free (m->dest_uri);
    em_utils_uids_free (m->uids);
}

static struct _mail_msg_op transfer_messages_op = {
    transfer_messages_desc,
    transfer_messages_transfer,
    transfer_messages_transferred,
    transfer_messages_free,
};

void
mail_transfer_messages (CamelFolder *source, GPtrArray *uids,
            gboolean delete_from_source,
            const char *dest_uri,
            guint32 dest_flags,
            void (*done) (gboolean ok, void *data),
            void *data)
{
    struct _transfer_msg *m;
    
    g_assert(CAMEL_IS_FOLDER (source));
    g_assert(uids != NULL);
    g_assert(dest_uri != NULL);
    
    m = mail_msg_new(&transfer_messages_op, NULL, sizeof(*m));
    m->source = source;
    camel_object_ref (source);
    m->uids = uids;
    m->delete = delete_from_source;
    m->dest_uri = g_strdup (dest_uri);
    m->dest_flags = dest_flags;
    m->done = done;
    m->data = data;
    
    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

/* ** SCAN SUBFOLDERS ***************************************************** */

struct _get_folderinfo_msg {
    struct _mail_msg msg;

    CamelStore *store;
    CamelFolderInfo *info;
    void (*done)(CamelStore *store, CamelFolderInfo *info, void *data);
    void *data;
};

static char *
get_folderinfo_desc (struct _mail_msg *mm, int done)
{
    struct _get_folderinfo_msg *m = (struct _get_folderinfo_msg *)mm;
    char *ret, *name;

    name = camel_service_get_name((CamelService *)m->store, TRUE);
    ret = g_strdup_printf(_("Scanning folders in \"%s\""), name);
    g_free(name);
    return ret;
}

static void
get_folderinfo_get (struct _mail_msg *mm)
{
    struct _get_folderinfo_msg *m = (struct _get_folderinfo_msg *)mm;
    guint32 flags = CAMEL_STORE_FOLDER_INFO_RECURSIVE;
    
    if (camel_store_supports_subscriptions (m->store))
        flags |= CAMEL_STORE_FOLDER_INFO_SUBSCRIBED;
    
    m->info = camel_store_get_folder_info (m->store, NULL, flags, &mm->ex);
}

static void
get_folderinfo_got (struct _mail_msg *mm)
{
    struct _get_folderinfo_msg *m = (struct _get_folderinfo_msg *)mm;
    
    if (camel_exception_is_set (&mm->ex)) {
        char *url;
        
        url = camel_service_get_url (CAMEL_SERVICE (m->store));
        w(g_warning ("Error getting folder info from store at %s: %s",
                 url, camel_exception_get_description (&mm->ex)));
        g_free (url);
    }
    
    if (m->done)
        m->done (m->store, m->info, m->data);
}

static void
get_folderinfo_free (struct _mail_msg *mm)
{
    struct _get_folderinfo_msg *m = (struct _get_folderinfo_msg *)mm;

    if (m->info)
        camel_store_free_folder_info(m->store, m->info);
    camel_object_unref(m->store);
}

static struct _mail_msg_op get_folderinfo_op = {
    get_folderinfo_desc,
    get_folderinfo_get,
    get_folderinfo_got,
    get_folderinfo_free,
};

int
mail_get_folderinfo (CamelStore *store, CamelOperation *op, void (*done)(CamelStore *store, CamelFolderInfo *info, void *data), void *data)
{
    struct _get_folderinfo_msg *m;
    int id;

    m = mail_msg_new(&get_folderinfo_op, NULL, sizeof(*m));
    if (op) {
        camel_operation_unref(m->msg.cancel);
        m->msg.cancel = op;
        camel_operation_ref(op);
    }
    m->store = store;
    camel_object_ref(store);
    m->done = done;
    m->data = data;
    id = m->msg.seq;

    e_thread_put(mail_thread_new, (EMsg *)m);

    return id;
}

/* ** ATTACH MESSAGES ****************************************************** */

struct _build_data {
    void (*done)(CamelFolder *folder, GPtrArray *uids, CamelMimePart *part, char *subject, void *data);
    void *data;
};

static void
do_build_attachment (CamelFolder *folder, GPtrArray *uids, GPtrArray *messages, void *data)
{
    struct _build_data *d = data;
    CamelMultipart *multipart;
    CamelMimePart *part;
    char *subject;
    int i;

    if (messages->len == 0) {
        d->done(folder, messages, NULL, NULL, d->data);
        g_free(d);
        return;
    }

    if (messages->len == 1) {
        part = mail_tool_make_message_attachment(messages->pdata[0]);
    } else {
        multipart = camel_multipart_new();
        camel_data_wrapper_set_mime_type(CAMEL_DATA_WRAPPER (multipart), "multipart/digest");
        camel_multipart_set_boundary(multipart, NULL);

        for (i=0;i<messages->len;i++) {
            part = mail_tool_make_message_attachment(messages->pdata[i]);
            camel_multipart_add_part(multipart, part);
            camel_object_unref(part);
        }
        part = camel_mime_part_new();
        camel_medium_set_content_object(CAMEL_MEDIUM (part), CAMEL_DATA_WRAPPER(multipart));
        camel_object_unref(multipart);

        camel_mime_part_set_description(part, _("Forwarded messages"));
    }

    subject = mail_tool_generate_forward_subject(messages->pdata[0]);
    d->done(folder, messages, part, subject, d->data);
    g_free(subject);
    camel_object_unref(part);

    g_free(d);
}

void
mail_build_attachment(CamelFolder *folder, GPtrArray *uids,
              void (*done)(CamelFolder *folder, GPtrArray *messages, CamelMimePart *part, char *subject, void *data), void *data)
{
    struct _build_data *d;

    d = g_malloc(sizeof(*d));
    d->done = done;
    d->data = data;
    mail_get_messages(folder, uids, do_build_attachment, d);
}

/* ** LOAD FOLDER ********************************************************* */

/* there should be some way to merge this and create folder, since both can
   presumably create a folder ... */

struct _get_folder_msg {
    struct _mail_msg msg;
    
    char *uri;
    guint32 flags;
    CamelFolder *folder;
    void (*done) (char *uri, CamelFolder *folder, void *data);
    void *data;
};

static char *
get_folder_desc (struct _mail_msg *mm, int done)
{
    struct _get_folder_msg *m = (struct _get_folder_msg *)mm;
    
    return g_strdup_printf(_("Opening folder %s"), m->uri);
}

static void
get_folder_get (struct _mail_msg *mm)
{
    struct _get_folder_msg *m = (struct _get_folder_msg *)mm;
    
    m->folder = mail_tool_uri_to_folder (m->uri, m->flags, &mm->ex);
}

static void
get_folder_got (struct _mail_msg *mm)
{
    struct _get_folder_msg *m = (struct _get_folder_msg *)mm;
    
    if (m->done)
        m->done (m->uri, m->folder, m->data);
}

static void
get_folder_free (struct _mail_msg *mm)
{
    struct _get_folder_msg *m = (struct _get_folder_msg *)mm;
    
    g_free (m->uri);
    if (m->folder)
        camel_object_unref (m->folder);
}

static struct _mail_msg_op get_folder_op = {
    get_folder_desc,
    get_folder_get,
    get_folder_got,
    get_folder_free,
};

int
mail_get_folder (const char *uri, guint32 flags,
         void (*done)(char *uri, CamelFolder *folder, void *data),
         void *data, EThread *thread)
{
    struct _get_folder_msg *m;
    int id;
    
    m = mail_msg_new(&get_folder_op, NULL, sizeof(*m));
    m->uri = g_strdup (uri);
    m->flags = flags;
    m->data = data;
    m->done = done;
    
    id = m->msg.seq;
    e_thread_put(thread, (EMsg *)m);
    return id;
}

/* ** GET STORE ******************************************************* */

struct _get_store_msg {
    struct _mail_msg msg;

    char *uri;
    CamelStore *store;
    void (*done) (char *uri, CamelStore *store, void *data);
    void *data;
};

static char *
get_store_desc (struct _mail_msg *mm, int done)
{
    struct _get_store_msg *m = (struct _get_store_msg *)mm;
    
    return g_strdup_printf(_("Opening store %s"), m->uri);
}

static void
get_store_get (struct _mail_msg *mm)
{
    struct _get_store_msg *m = (struct _get_store_msg *)mm;
    
    /*camel_session_get_store connects us, which we don't want to do on startup. */

    m->store = (CamelStore *) camel_session_get_service (session, m->uri,
                                 CAMEL_PROVIDER_STORE,
                                 &mm->ex);
}

static void
get_store_got (struct _mail_msg *mm)
{
    struct _get_store_msg *m = (struct _get_store_msg *)mm;

    if (m->done)
        m->done (m->uri, m->store, m->data);
}

static void
get_store_free (struct _mail_msg *mm)
{
    struct _get_store_msg *m = (struct _get_store_msg *)mm;
    
    g_free (m->uri);
    if (m->store)
        camel_object_unref (m->store);
}

static struct _mail_msg_op get_store_op = {
    get_store_desc,
    get_store_get,
    get_store_got,
    get_store_free,
};

int
mail_get_store (const char *uri, CamelOperation *op, void (*done) (char *uri, CamelStore *store, void *data), void *data)
{
    struct _get_store_msg *m;
    int id;
    
    m = mail_msg_new (&get_store_op, NULL, sizeof (*m));
    if (op) {
        camel_operation_unref(m->msg.cancel);
        m->msg.cancel = op;
        camel_operation_ref(op);
    }
    m->uri = g_strdup (uri);
    m->data = data;
    m->done = done;
    
    id = m->msg.seq;
    e_thread_put (mail_thread_new, (EMsg *)m);
    return id;
}

/* ** REMOVE FOLDER ******************************************************* */

struct _remove_folder_msg {
    struct _mail_msg msg;

    char *uri;
    gboolean removed;
    void (*done) (char *uri, gboolean removed, void *data);
    void *data;
};

static char *
remove_folder_desc (struct _mail_msg *mm, int done)
{
    struct _remove_folder_msg *m = (struct _remove_folder_msg *)mm;
    
    return g_strdup_printf (_("Removing folder %s"), m->uri);
}

static void
remove_folder_get (struct _mail_msg *mm)
{
    struct _remove_folder_msg *m = (struct _remove_folder_msg *)mm;
    CamelStore *store;
    CamelFolder *folder;
    GPtrArray *uids;
    int i;
    
    m->removed = FALSE;
    
    folder = mail_tool_uri_to_folder (m->uri, 0, &mm->ex);
    if (!folder)
        return;
    
    store = folder->parent_store;
    
    /* Delete every message in this folder, then expunge it */
    uids = camel_folder_get_uids (folder);
    camel_folder_freeze(folder);
    for (i = 0; i < uids->len; i++)
        camel_folder_delete_message (folder, uids->pdata[i]);
    camel_folder_sync (folder, TRUE, NULL);
    camel_folder_thaw(folder);
    camel_folder_free_uids (folder, uids);
    
    /* if the store supports subscriptions, unsubscribe from this folder... */
    if (camel_store_supports_subscriptions (store))
        camel_store_unsubscribe_folder (store, folder->full_name, NULL);
    
    /* Then delete the folder from the store */
    camel_store_delete_folder (store, folder->full_name, &mm->ex);
    m->removed = !camel_exception_is_set (&mm->ex);
    camel_object_unref (folder);
}

static void
remove_folder_got (struct _mail_msg *mm)
{
    struct _remove_folder_msg *m = (struct _remove_folder_msg *)mm;

    if (m->removed) {
        /* FIXME: Remove this folder from the folder cache ??? */
    }

    if (m->done)
        m->done (m->uri, m->removed, m->data);
}

static void
remove_folder_free (struct _mail_msg *mm)
{
    struct _remove_folder_msg *m = (struct _remove_folder_msg *)mm;
    
    g_free (m->uri);
}

static struct _mail_msg_op remove_folder_op = {
    remove_folder_desc,
    remove_folder_get,
    remove_folder_got,
    remove_folder_free,
};

void
mail_remove_folder (const char *uri, void (*done) (char *uri, gboolean removed, void *data), void *data)
{
    struct _remove_folder_msg *m;
    
    m = mail_msg_new (&remove_folder_op, NULL, sizeof (*m));
    m->uri = g_strdup (uri);
    m->data = data;
    m->done = done;
    
    e_thread_put (mail_thread_new, (EMsg *)m);
}

/* ** SYNC FOLDER ********************************************************* */

struct _sync_folder_msg {
    struct _mail_msg msg;

    CamelFolder *folder;
    void (*done) (CamelFolder *folder, void *data);
    void *data;
};

static char *sync_folder_desc(struct _mail_msg *mm, int done)
{
    struct _sync_folder_msg *m = (struct _sync_folder_msg *)mm;

    return g_strdup_printf (_("Storing folder \'%s\'"), 
                   camel_folder_get_full_name (m->folder));
}

static void sync_folder_sync(struct _mail_msg *mm)
{
    struct _sync_folder_msg *m = (struct _sync_folder_msg *)mm;

    camel_folder_sync(m->folder, FALSE, &mm->ex);
}

static void sync_folder_synced(struct _mail_msg *mm)
{
    struct _sync_folder_msg *m = (struct _sync_folder_msg *)mm;

    if (m->done)
        m->done(m->folder, m->data);
}

static void sync_folder_free(struct _mail_msg *mm)
{
    struct _sync_folder_msg *m = (struct _sync_folder_msg *)mm;

    camel_object_unref((CamelObject *)m->folder);
}

static struct _mail_msg_op sync_folder_op = {
    sync_folder_desc,
    sync_folder_sync,
    sync_folder_synced,
    sync_folder_free,
};

void
mail_sync_folder(CamelFolder *folder, void (*done) (CamelFolder *folder, void *data), void *data)
{
    struct _sync_folder_msg *m;

    m = mail_msg_new(&sync_folder_op, NULL, sizeof(*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

/* ** SYNC STORE ********************************************************* */

struct _sync_store_msg {
    struct _mail_msg msg;

    CamelStore *store;
    int expunge;
    void (*done) (CamelStore *store, void *data);
    void *data;
};

static char *sync_store_desc(struct _mail_msg *mm, int done)
{
    struct _sync_store_msg *m = (struct _sync_store_msg *)mm;
    char *uri, *res;

    uri = camel_url_to_string(((CamelService *)m->store)->url, CAMEL_URL_HIDE_ALL);
    res = g_strdup_printf(m->expunge
                  ?_("Expunging and storing account '%s'")
                  :_("Storing account '%s'"),
                  uri);
    g_free(uri);

    return res;
}

static void sync_store_sync(struct _mail_msg *mm)
{
    struct _sync_store_msg *m = (struct _sync_store_msg *)mm;

    camel_store_sync(m->store, m->expunge, &mm->ex);
}

static void sync_store_synced(struct _mail_msg *mm)
{
    struct _sync_store_msg *m = (struct _sync_store_msg *)mm;

    if (m->done)
        m->done(m->store, m->data);
}

static void sync_store_free(struct _mail_msg *mm)
{
    struct _sync_store_msg *m = (struct _sync_store_msg *)mm;

    camel_object_unref(m->store);
}

static struct _mail_msg_op sync_store_op = {
    sync_store_desc,
    sync_store_sync,
    sync_store_synced,
    sync_store_free,
};

void
mail_sync_store(CamelStore *store, int expunge, void (*done) (CamelStore *store, void *data), void *data)
{
    struct _sync_store_msg *m;

    m = mail_msg_new(&sync_store_op, NULL, sizeof(*m));
    m->store = store;
    m->expunge = expunge;
    camel_object_ref(store);
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

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

static char *refresh_folder_desc(struct _mail_msg *mm, int done)
{
    return g_strdup(_("Refreshing folder"));
}

static void refresh_folder_refresh(struct _mail_msg *mm)
{
    struct _sync_folder_msg *m = (struct _sync_folder_msg *)mm;

    camel_folder_refresh_info(m->folder, &mm->ex);
}

/* we just use the sync stuff where we can, since it would be the same */
static struct _mail_msg_op refresh_folder_op = {
    refresh_folder_desc,
    refresh_folder_refresh,
    sync_folder_synced,
    sync_folder_free,
};

void
mail_refresh_folder(CamelFolder *folder, void (*done) (CamelFolder *folder, void *data), void *data)
{
    struct _sync_folder_msg *m;

    m = mail_msg_new(&refresh_folder_op, NULL, sizeof(*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

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

static char *expunge_folder_desc(struct _mail_msg *mm, int done)
{
    return g_strdup(_("Expunging folder"));
}

static void expunge_folder_expunge(struct _mail_msg *mm)
{
    struct _sync_folder_msg *m = (struct _sync_folder_msg *)mm;

    camel_folder_expunge(m->folder, &mm->ex);
}

/* we just use the sync stuff where we can, since it would be the same */
static struct _mail_msg_op expunge_folder_op = {
    expunge_folder_desc,
    expunge_folder_expunge,
    sync_folder_synced,
    sync_folder_free,
};

void
mail_expunge_folder(CamelFolder *folder, void (*done) (CamelFolder *folder, void *data), void *data)
{
    struct _sync_folder_msg *m;

    m = mail_msg_new(&expunge_folder_op, NULL, sizeof(*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

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

struct _empty_trash_msg {
    struct _mail_msg msg;

    EAccount *account;
    void (*done) (EAccount *account, void *data);
    void *data;
};

static char *empty_trash_desc(struct _mail_msg *mm, int done)
{
    /* FIXME after 1.4 is out and we're not in string freeze any more. */
#if 0
    struct _empty_trash_msg *m = (struct _empty_trash_msg *)mm;

    return g_strdup_printf (_("Emptying trash in \'%s\'"), 
                m->account ? m->account->name : _("Local Folders"));
#else
    return g_strdup(_("Expunging folder"));
#endif
}

static void empty_trash_empty(struct _mail_msg *mm)
{
    struct _empty_trash_msg *m = (struct _empty_trash_msg *)mm;
    const char *evolution_dir;
    CamelFolder *trash;
    char *uri;
    
    if (m->account) {
        trash = mail_tool_get_trash (m->account->source->url, FALSE, &mm->ex);
    } else {
        evolution_dir = mail_component_peek_base_directory (mail_component_peek ());
        uri = g_strdup_printf ("mbox:%s/mail/local", evolution_dir);
        trash = mail_tool_get_trash (uri, TRUE, &mm->ex);
        g_free (uri);
    }
    
    if (trash)
        camel_folder_expunge (trash, &mm->ex);
    
    camel_object_unref (trash);
}

static void empty_trash_emptied(struct _mail_msg *mm)
{
    struct _empty_trash_msg *m = (struct _empty_trash_msg *)mm;

    if (m->done)
        m->done(m->account, m->data);
}

static void empty_trash_free(struct _mail_msg *mm)
{
    struct _empty_trash_msg *m = (struct _empty_trash_msg *)mm;

    if (m->account)
        g_object_unref(m->account);
}

static struct _mail_msg_op empty_trash_op = {
    empty_trash_desc,
    empty_trash_empty,
    empty_trash_emptied,
    empty_trash_free,
};

void
mail_empty_trash(EAccount *account, void (*done) (EAccount *account, void *data), void *data)
{
    struct _empty_trash_msg *m;

    m = mail_msg_new(&empty_trash_op, NULL, sizeof(*m));
    m->account = account;
    if (account)
        g_object_ref(account);
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

/* ** GET MESSAGE(s) ***************************************************** */

struct _get_message_msg {
    struct _mail_msg msg;

    CamelFolder *folder;
    char *uid;
    void (*done) (CamelFolder *folder, const char *uid, CamelMimeMessage *msg, void *data);
    void *data;
    CamelMimeMessage *message;
    CamelOperation *cancel;
};

static char *get_message_desc(struct _mail_msg *mm, int done)
{
    struct _get_message_msg *m = (struct _get_message_msg *)mm;

    return g_strdup_printf(_("Retrieving message %s"), m->uid);
}

static void get_message_get(struct _mail_msg *mm)
{
    struct _get_message_msg *m = (struct _get_message_msg *)mm;

    m->message = camel_folder_get_message(m->folder, m->uid, &mm->ex);
}

static void get_message_got(struct _mail_msg *mm)
{
    struct _get_message_msg *m = (struct _get_message_msg *)mm;

    if (m->done)
        m->done(m->folder, m->uid, m->message, m->data);
}

static void get_message_free(struct _mail_msg *mm)
{
    struct _get_message_msg *m = (struct _get_message_msg *)mm;
    
    g_free (m->uid);
    camel_object_unref (m->folder);
    camel_operation_unref (m->cancel);
    
    if (m->message)
        camel_object_unref (m->message);
}

static struct _mail_msg_op get_message_op = {
    get_message_desc,
    get_message_get,
    get_message_got,
    get_message_free,
};

void
mail_get_message(CamelFolder *folder, const char *uid, void (*done) (CamelFolder *folder, const char *uid,
                                     CamelMimeMessage *msg, void *data),
         void *data, EThread *thread)
{
    struct _get_message_msg *m;
    
    m = mail_msg_new(&get_message_op, NULL, sizeof(*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->uid = g_strdup(uid);
    m->data = data;
    m->done = done;
    m->cancel = camel_operation_new(NULL, NULL);
    
    e_thread_put(thread, (EMsg *)m);
}

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

struct _get_messages_msg {
    struct _mail_msg msg;

    CamelFolder *folder;
    GPtrArray *uids;
    GPtrArray *messages;

    void (*done) (CamelFolder *folder, GPtrArray *uids, GPtrArray *msgs, void *data);
    void *data;
};

static char * get_messages_desc(struct _mail_msg *mm, int done)
{
    struct _get_messages_msg *m = (struct _get_messages_msg *)mm;

    return g_strdup_printf(ngettext("Retrieving %d message",
                    "Retrieving %d messages", m->uids->len), 
                   m->uids->len);
}

static void get_messages_get(struct _mail_msg *mm)
{
    struct _get_messages_msg *m = (struct _get_messages_msg *)mm;
    int i;
    CamelMimeMessage *message;

    for (i=0; i<m->uids->len; i++) {
        int pc = ((i+1) * 100) / m->uids->len;

        message = camel_folder_get_message(m->folder, m->uids->pdata[i], &mm->ex);
        camel_operation_progress(mm->cancel, pc);
        if (message == NULL)
            break;

        g_ptr_array_add(m->messages, message);
    }
}

static void get_messages_got(struct _mail_msg *mm)
{
    struct _get_messages_msg *m = (struct _get_messages_msg *)mm;

    if (m->done)
        m->done(m->folder, m->uids, m->messages, m->data);
}

static void get_messages_free(struct _mail_msg *mm)
{
    struct _get_messages_msg *m = (struct _get_messages_msg *)mm;
    int i;
    
    em_utils_uids_free (m->uids);
    for (i=0;i<m->messages->len;i++) {
        if (m->messages->pdata[i])
            camel_object_unref(m->messages->pdata[i]);
    }
    g_ptr_array_free(m->messages, TRUE);
    camel_object_unref(m->folder);
}

static struct _mail_msg_op get_messages_op = {
    get_messages_desc,
    get_messages_get,
    get_messages_got,
    get_messages_free,
};

void
mail_get_messages(CamelFolder *folder, GPtrArray *uids,
          void (*done) (CamelFolder *folder, GPtrArray *uids, GPtrArray *msgs, void *data),
          void *data)
{
    struct _get_messages_msg *m;

    m = mail_msg_new(&get_messages_op, NULL, sizeof(*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->uids = uids;
    m->messages = g_ptr_array_new();
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_new, (EMsg *)m);
}

/* ** SAVE MESSAGES ******************************************************* */

struct _save_messages_msg {
    struct _mail_msg msg;

    CamelFolder *folder;
    GPtrArray *uids;
    char *path;
    void (*done)(CamelFolder *folder, GPtrArray *uids, char *path, void *data);
    void *data;
};

static char *save_messages_desc(struct _mail_msg *mm, int done)
{
    struct _save_messages_msg *m = (struct _save_messages_msg *)mm;

    return g_strdup_printf(ngettext("Saving %d message", 
                    "Saving %d messsages", m->uids->len), 
                   m->uids->len);
}

static void
save_prepare_part (CamelMimePart *mime_part)
{
    CamelDataWrapper *wrapper;
    int parts, i;
    
    wrapper = camel_medium_get_content_object (CAMEL_MEDIUM (mime_part));
    if (!wrapper)
        return;
    
    if (CAMEL_IS_MULTIPART (wrapper)) {
        parts = camel_multipart_get_number (CAMEL_MULTIPART (wrapper));
        for (i = 0; i < parts; i++) {
            CamelMimePart *part = camel_multipart_get_part (CAMEL_MULTIPART (wrapper), i);
            
            save_prepare_part (part);
        }
    } else {
        if (CAMEL_IS_MIME_MESSAGE (wrapper)) {
            /* prepare the message parts' subparts */
            save_prepare_part (CAMEL_MIME_PART (wrapper));
        } else {
            CamelContentType *type;
            
            /* We want to save textual parts as 8bit instead of encoded */
            type = camel_data_wrapper_get_mime_type_field (wrapper);
            if (camel_content_type_is (type, "text", "*"))
                camel_mime_part_set_encoding (mime_part, CAMEL_TRANSFER_ENCODING_8BIT);
        }
    }
}

static void
save_messages_save (struct _mail_msg *mm)
{
    struct _save_messages_msg *m = (struct _save_messages_msg *)mm;
    CamelStreamFilter *filtered_stream;
    CamelMimeFilterFrom *from_filter;
    CamelStream *stream;
    int fd, i;
    char *from;
    
    fd = open (m->path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd == -1) {
        camel_exception_setv(&mm->ex, CAMEL_EXCEPTION_SYSTEM,
                     _("Unable to create output file: %s\n %s"), m->path, strerror(errno));
        return;
    }
    
    stream = camel_stream_fs_new_with_fd(fd);
    from_filter = camel_mime_filter_from_new();
    filtered_stream = camel_stream_filter_new_with_stream(stream);
    camel_stream_filter_add(filtered_stream, (CamelMimeFilter *)from_filter);
    camel_object_unref(from_filter);
    
    for (i=0; i<m->uids->len; i++) {
        CamelMimeMessage *message;
        int pc = ((i+1) * 100) / m->uids->len;

        message = camel_folder_get_message(m->folder, m->uids->pdata[i], &mm->ex);
        camel_operation_progress(mm->cancel, pc);
        if (message == NULL)
            break;
        
        save_prepare_part (CAMEL_MIME_PART (message));
        
        /* we need to flush after each stream write since we are writing to the same fd */
        from = camel_mime_message_build_mbox_from(message);
        if (camel_stream_write_string(stream, from) == -1
            || camel_stream_flush(stream) == -1
            || camel_data_wrapper_write_to_stream((CamelDataWrapper *)message, (CamelStream *)filtered_stream) == -1
            || camel_stream_flush((CamelStream *)filtered_stream) == -1) {
            camel_exception_setv(&mm->ex, CAMEL_EXCEPTION_SYSTEM,
                         _("Error saving messages to: %s:\n %s"), m->path, strerror(errno));
            g_free(from);
            camel_object_unref((CamelObject *)message);
            break;
        }
        g_free(from);
        camel_object_unref(message);
    }

    camel_object_unref(filtered_stream);
    camel_object_unref(stream);
}

static void save_messages_saved(struct _mail_msg *mm)
{
    struct _save_messages_msg *m = (struct _save_messages_msg *)mm;

    if (m->done)
        m->done(m->folder, m->uids, m->path, m->data);
}

static void save_messages_free(struct _mail_msg *mm)
{
    struct _save_messages_msg *m = (struct _save_messages_msg *)mm;
    
    em_utils_uids_free (m->uids);
    camel_object_unref(m->folder);
    g_free(m->path);
}

static struct _mail_msg_op save_messages_op = {
    save_messages_desc,
    save_messages_save,
    save_messages_saved,
    save_messages_free,
};

int
mail_save_messages(CamelFolder *folder, GPtrArray *uids, const char *path,
           void (*done) (CamelFolder *folder, GPtrArray *uids, char *path, void *data), void *data)
{
    struct _save_messages_msg *m;
    int id;

    m = mail_msg_new(&save_messages_op, NULL, sizeof(*m));
    m->folder = folder;
    camel_object_ref(folder);
    m->uids = uids;
    m->path = g_strdup(path);
    m->data = data;
    m->done = done;

    id = m->msg.seq;
    e_thread_put(mail_thread_new, (EMsg *)m);

    return id;
}

/* ** SAVE PART ******************************************************* */

struct _save_part_msg {
    struct _mail_msg msg;

    CamelMimePart *part;
    char *path;
    void (*done)(CamelMimePart *part, char *path, int saved, void *data);
    void *data;
};

static char *save_part_desc(struct _mail_msg *mm, int done)
{
    return g_strdup(_("Saving attachment"));
}

static void
save_part_save (struct _mail_msg *mm)
{
    struct _save_part_msg *m = (struct _save_part_msg *)mm;
    CamelDataWrapper *content;
    CamelStream *stream;
    
    if (!(stream = camel_stream_fs_new_with_name (m->path, O_WRONLY | O_CREAT | O_TRUNC, 0666))) {
        camel_exception_setv (&mm->ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Cannot create output file: %s:\n %s"),
                      m->path, g_strerror (errno));
        return;
    }
    
    content = camel_medium_get_content_object (CAMEL_MEDIUM (m->part));
    
    if (camel_data_wrapper_decode_to_stream (content, stream) == -1
        || camel_stream_flush (stream) == -1)
        camel_exception_setv (&mm->ex, CAMEL_EXCEPTION_SYSTEM,
                      _("Could not write data: %s"),
                      g_strerror (errno));
    
    camel_object_unref (stream);
}

static void
save_part_saved (struct _mail_msg *mm)
{
    struct _save_part_msg *m = (struct _save_part_msg *)mm;
    
    if (m->done)
        m->done (m->part, m->path, !camel_exception_is_set (&mm->ex), m->data);
}

static void
save_part_free (struct _mail_msg *mm)
{
    struct _save_part_msg *m = (struct _save_part_msg *)mm;

    camel_object_unref (m->part);
    g_free (m->path);
}

static struct _mail_msg_op save_part_op = {
    save_part_desc,
    save_part_save,
    save_part_saved,
    save_part_free,
};

int
mail_save_part (CamelMimePart *part, const char *path,
        void (*done)(CamelMimePart *part, char *path, int saved, void *data), void *data)
{
    struct _save_part_msg *m;
    int id;
    
    m = mail_msg_new (&save_part_op, NULL, sizeof (*m));
    m->part = part;
    camel_object_ref (part);
    m->path = g_strdup (path);
    m->data = data;
    m->done = done;
    
    id = m->msg.seq;
    e_thread_put (mail_thread_new, (EMsg *)m);
    
    return id;
}


/* ** PREPARE OFFLINE ***************************************************** */

struct _prep_offline_msg {
    struct _mail_msg msg;

    CamelOperation *cancel;
    char *uri;
    void (*done)(const char *uri, void *data);
    void *data;
};

static void prep_offline_do(struct _mail_msg *mm)
{
    struct _prep_offline_msg *m = (struct _prep_offline_msg *)mm;
    CamelFolder *folder;

    if (m->cancel)
        camel_operation_register(m->cancel);

    folder = mail_tool_uri_to_folder(m->uri, 0, &mm->ex);
    if (folder) {
        if (CAMEL_IS_DISCO_FOLDER(folder)) {
            camel_disco_folder_prepare_for_offline((CamelDiscoFolder *)folder,
                                   "(match-all)",
                                   &mm->ex);
        }
        /* prepare_for_offline should do this? */
        /* of course it should all be atomic, but ... */
        camel_folder_sync(folder, FALSE, NULL);
        camel_object_unref(folder);
    }

    if (m->cancel)
        camel_operation_unregister(m->cancel);
}

static void prep_offline_done(struct _mail_msg *mm)
{
    struct _prep_offline_msg *m = (struct _prep_offline_msg *)mm;

    if (m->done)
        m->done(m->uri, m->data);
}

static void prep_offline_free(struct _mail_msg *mm)
{
    struct _prep_offline_msg *m = (struct _prep_offline_msg *)mm;

    if (m->cancel)
        camel_operation_unref(m->cancel);
    g_free(m->uri);
}

static struct _mail_msg_op prep_offline_op = {
    NULL, /* DO NOT CHANGE THIS, IT MUST BE NULL FOR CANCELLATION TO WORK */
    prep_offline_do,
    prep_offline_done,
    prep_offline_free,
};

void
mail_prep_offline(const char *uri,
          CamelOperation *cancel,
          void (*done)(const char *, void *data),
          void *data)
{
    struct _prep_offline_msg *m;

    m = mail_msg_new(&prep_offline_op, NULL, sizeof(*m));
    m->cancel = cancel;
    if (cancel)
        camel_operation_ref(cancel);
    m->uri = g_strdup(uri);
    m->data = data;
    m->done = done;

    e_thread_put(mail_thread_queued_slow, (EMsg *)m);
}

/* ** GO OFFLINE ***************************************************** */

struct _set_offline_msg {
    struct _mail_msg msg;

    CamelStore *store;
    gboolean offline;
    void (*done)(CamelStore *store, void *data);
    void *data;
};

static char *set_offline_desc(struct _mail_msg *mm, int done)
{
    struct _set_offline_msg *m = (struct _set_offline_msg *)mm;
    char *service_name = camel_service_get_name (CAMEL_SERVICE (m->store), TRUE);
    char *msg;

    msg = g_strdup_printf(m->offline?_("Disconnecting from %s"):_("Reconnecting to %s"),
                  service_name);
    g_free(service_name);
    return msg;
}

static void set_offline_do(struct _mail_msg *mm)
{
    struct _set_offline_msg *m = (struct _set_offline_msg *)mm;

    if (CAMEL_IS_DISCO_STORE (m->store)) {
        if (!m->offline) {
            camel_disco_store_set_status (CAMEL_DISCO_STORE (m->store),
                              CAMEL_DISCO_STORE_ONLINE,
                              &mm->ex);
            return;
        } else if (camel_disco_store_can_work_offline (CAMEL_DISCO_STORE (m->store))) {
            camel_disco_store_set_status (CAMEL_DISCO_STORE (m->store),
                              CAMEL_DISCO_STORE_OFFLINE,
                              &mm->ex);
            return;
        }
    }

    if (m->offline)
        camel_service_disconnect (CAMEL_SERVICE (m->store),
                      TRUE, &mm->ex);
}

static void set_offline_done(struct _mail_msg *mm)
{
    struct _set_offline_msg *m = (struct _set_offline_msg *)mm;

    if (m->done)
        m->done(m->store, m->data);
}

static void set_offline_free(struct _mail_msg *mm)
{
    struct _set_offline_msg *m = (struct _set_offline_msg *)mm;

    camel_object_unref(m->store);
}

static struct _mail_msg_op set_offline_op = {
    set_offline_desc,
    set_offline_do,
    set_offline_done,
    set_offline_free,
};

int
mail_store_set_offline (CamelStore *store, gboolean offline,
            void (*done)(CamelStore *, void *data),
            void *data)
{
    struct _set_offline_msg *m;
    int id;

    /* Cancel any pending connect first so the set_offline_op
     * thread won't get queued behind a hung connect op.
     */
    if (offline)
        camel_service_cancel_connect (CAMEL_SERVICE (store));

    m = mail_msg_new(&set_offline_op, NULL, sizeof(*m));
    m->store = store;
    camel_object_ref(store);
    m->offline = offline;
    m->data = data;
    m->done = done;

    id = m->msg.seq;
    e_thread_put(mail_thread_new, (EMsg *)m);

    return id;
}

/* ** Execute Shell Command ***************************************************** */

void
mail_execute_shell_command (CamelFilterDriver *driver, int argc, char **argv, void *data)
{
    if (argc <= 0)
        return;
    
    gnome_execute_async_fds (NULL, argc, argv, TRUE);
}

/* Async service-checking/authtype-lookup code. */
struct _check_msg {
    struct _mail_msg msg;

    char *url;
    CamelProviderType type;
    GList *authtypes;

    void (*done)(const char *url, CamelProviderType type, GList *types, void *data);
    void *data;
};

static char *
check_service_describe(struct _mail_msg *mm, int complete)
{
    return g_strdup(_("Checking Service"));
}

static void
check_service_check(struct _mail_msg *mm)
{
    struct _check_msg *m = (struct _check_msg *)mm;
    CamelService *service;

    service = camel_session_get_service(session, m->url, m->type, &mm->ex);
    if (!service) {
        camel_operation_unregister(mm->cancel);
        return;
    }

    m->authtypes = camel_service_query_auth_types(service, &mm->ex);
    camel_object_unref(service);
}

static void
check_service_done(struct _mail_msg *mm)
{
    struct _check_msg *m = (struct _check_msg *)mm;

    if (m->done)
        m->done(m->url, m->type, m->authtypes, m->data);
}

static void
check_service_free(struct _mail_msg *mm)
{
    struct _check_msg *m = (struct _check_msg *)mm;

    g_free(m->url);
    g_list_free(m->authtypes);
}

static struct _mail_msg_op check_service_op = {
    check_service_describe,
    check_service_check,
    check_service_done,
    check_service_free,
};

int
mail_check_service(const char *url, CamelProviderType type, void (*done)(const char *url, CamelProviderType type, GList *authtypes, void *data), void *data)
{
    struct _check_msg *m;
    int id;
    
    m = mail_msg_new (&check_service_op, NULL, sizeof(*m));
    m->url = g_strdup(url);
    m->type = type;
    m->done = done;
    m->data = data;
    
    id = m->msg.seq;
    e_thread_put(mail_thread_new, (EMsg *)m);

    return id;
}