aboutsummaryrefslogtreecommitdiffstats
path: root/libemail-engine/e-mail-session.c
blob: 46ed51567535c4b4518016ea1d073f0e3b0b14d4 (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
/*
 * e-mail-session.c
 *
 * 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:
 *   Jonathon Jongsma <jonathon.jongsma@collabora.co.uk>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 * Copyright (C) 2009 Intel Corporation
 *
 */

/* mail-session.c: handles the session information and resource manipulation */

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

#include <errno.h>
#include <stdlib.h>
#include <string.h>

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

#include <gtk/gtk.h>

#ifdef HAVE_CANBERRA
#include <canberra-gtk.h>
#endif

#include <libedataserver/e-flag.h>
#include <libedataserver/e-proxy.h>
#include <libebackend/e-extensible.h>
#include <libedataserverui/e-passwords.h>
#include <libedataserver/e-data-server-util.h>

#include "libemail-utils/e-account-utils.h"
#include "libemail-utils/mail-mt.h"

#include "e-mail-junk-filter.h"
#include "e-mail-session.h"
#include "e-mail-folder-utils.h"
#include "e-mail-utils.h"
#include "mail-config.h"
#include "mail-ops.h"
#include "mail-tools.h"

#define E_MAIL_SESSION_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_MAIL_SESSION, EMailSessionPrivate))

typedef struct _AsyncContext AsyncContext;

struct _EMailSessionPrivate {
    MailFolderCache *folder_cache;

    EAccountList *account_list;
    gulong account_added_handler_id;

    CamelStore *local_store;

    FILE *filter_logfile;
    GHashTable *junk_filters;
    EProxy *proxy;

    /* Local folder cache. */
    GPtrArray *local_folders;
    GPtrArray *local_folder_uris;
};

struct _AsyncContext {
    /* arguments */
    CamelStoreGetFolderFlags flags;
    gchar *uid;
    gchar *uri;

    /* results */
    CamelFolder *folder;
};

enum {
    PROP_0,
    PROP_FOLDER_CACHE,
    PROP_JUNK_FILTER_NAME,
    PROP_LOCAL_STORE
};

static const gchar *local_folder_names[E_MAIL_NUM_LOCAL_FOLDERS] = {
    N_("Inbox"),        /* E_MAIL_LOCAL_FOLDER_INBOX */
    N_("Drafts"),       /* E_MAIL_LOCAL_FOLDER_DRAFTS */
    N_("Outbox"),       /* E_MAIL_LOCAL_FOLDER_OUTBOX */
    N_("Sent"),     /* E_MAIL_LOCAL_FOLDER_SENT */
    N_("Templates"),    /* E_MAIL_LOCAL_FOLDER_TEMPLATES */
    "Inbox"         /* E_MAIL_LOCAL_FOLDER_LOCAL_INBOX */
};

enum {
    FLUSH_OUTBOX,
    STORE_ADDED,
    STORE_REMOVED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

static gchar *mail_data_dir;
static gchar *mail_cache_dir;
static gchar *mail_config_dir;

G_DEFINE_TYPE_WITH_CODE (
    EMailSession,
    e_mail_session,
    CAMEL_TYPE_SESSION,
    G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE, NULL))

/* Support for CamelSession.alert_user() *************************************/

static GQueue user_message_queue = { NULL, NULL, 0 };

struct _user_message_msg {
    MailMsg base;

    CamelSessionAlertType type;
    gchar *prompt;
    GSList *button_captions;
    EFlag *done;

    gint result;
    guint ismain : 1;
};

static void user_message_exec (struct _user_message_msg *m,
                               GCancellable *cancellable,
                               GError **error);

static void
user_message_response_free (struct _user_message_msg *m)
{

    /* check for pendings */
    if (!g_queue_is_empty (&user_message_queue)) {
        GCancellable *cancellable;

        m = g_queue_pop_head (&user_message_queue);
        cancellable = m->base.cancellable;
        user_message_exec (m, cancellable, &m->base.error);
        mail_msg_unref (m);
    }
}

/* clicked, send back the reply */
static void
user_message_response (struct _user_message_msg *m)
{
    /* if !allow_cancel, then we've already replied */
    if (m->button_captions) {
        m->result = TRUE; //If Accepted
        e_flag_set (m->done);
    }

    user_message_response_free (m);
}

static void
user_message_exec (struct _user_message_msg *m,
                   GCancellable *cancellable,
                   GError **error)
{
    /* XXX This is a case where we need to be able to construct
     *     custom EAlerts without a predefined XML definition. */
    if (m->ismain) {
        /* Use DBUS to raise dialogs in clients and reply back.
         * For now say accept all. */
        user_message_response (m);
    } else
        g_queue_push_tail (&user_message_queue, mail_msg_ref (m));
}

static void
user_message_free (struct _user_message_msg *m)
{
    g_free (m->prompt);
    g_slist_free_full (m->button_captions, g_free);
    e_flag_free (m->done);
}

static MailMsgInfo user_message_info = {
    sizeof (struct _user_message_msg),
    (MailMsgDescFunc) NULL,
    (MailMsgExecFunc) user_message_exec,
    (MailMsgDoneFunc) NULL,
    (MailMsgFreeFunc) user_message_free
};

/* Support for CamelSession.get_filter_driver () *****************************/

static CamelFolder *
get_folder (CamelFilterDriver *d,
            const gchar *uri,
            gpointer user_data,
            GError **error)
{
    EMailSession *session = E_MAIL_SESSION (user_data);

    /* FIXME Not passing a GCancellable here. */
    /* FIXME Need a camel_filter_driver_get_session(). */
    return e_mail_session_uri_to_folder_sync (
        session, uri, 0, NULL, error);
}

static CamelFilterDriver *
main_get_filter_driver (CamelSession *session,
                        const gchar *type,
                        GError **error)
{
    CamelFilterDriver *driver;
    EMailSession *ms = (EMailSession *) session;
    GSettings *settings;

    settings = g_settings_new ("org.gnome.evolution.mail");

    driver = camel_filter_driver_new (session);
    camel_filter_driver_set_folder_func (driver, get_folder, session);

    if (g_settings_get_boolean (settings, "filters-log-actions")) {
        if (ms->priv->filter_logfile == NULL) {
            gchar *filename;

            filename = g_settings_get_string (settings, "filters-log-file");
            if (filename) {
                ms->priv->filter_logfile = g_fopen (filename, "a+");
                g_free (filename);
            }
        }

        if (ms->priv->filter_logfile)
            camel_filter_driver_set_logfile (driver, ms->priv->filter_logfile);
    }

    g_object_unref (settings);

    return driver;
}

/* Support for CamelSession.forward_to () ************************************/

static guint preparing_flush = 0;

static gboolean
forward_to_flush_outbox_cb (EMailSession *session)
{

    preparing_flush = 0;

    /* Connect to this and call mail_send in the main email client.*/
    g_signal_emit (session, signals[FLUSH_OUTBOX], 0);

    return FALSE;
}

static void
ms_forward_to_cb (CamelFolder *folder,
                  GAsyncResult *result,
                  EMailSession *session)
{
    GSettings *settings;

    /* FIXME Poor error handling. */
    if (!e_mail_folder_append_message_finish (folder, result, NULL, NULL))
        return;

    settings = g_settings_new ("org.gnome.evolution.mail");

    /* do not call mail send immediately, just pile them all in the outbox */
    if (preparing_flush || g_settings_get_boolean (
        settings, "flush-outbox")) {
        if (preparing_flush)
            g_source_remove (preparing_flush);

        preparing_flush = g_timeout_add_seconds (
            60, (GSourceFunc)
            forward_to_flush_outbox_cb, session);
    }

    g_object_unref (settings);
}

static void
async_context_free (AsyncContext *context)
{
    if (context->folder != NULL)
        g_object_unref (context->folder);

    g_free (context->uid);
    g_free (context->uri);

    g_slice_free (AsyncContext, context);
}

static gchar *
mail_session_make_key (CamelService *service,
                       const gchar *item)
{
    gchar *key;

    if (service != NULL) {
        CamelURL *url;

        url = camel_service_new_camel_url (service);
        key = camel_url_to_string (url, CAMEL_URL_HIDE_ALL);
        camel_url_free (url);
    } else
        key = g_strdup (item);

    return key;
}

static void
mail_session_check_junk_notify (GSettings *settings,
                                const gchar *key,
                                CamelSession *session)
{
    if (strcmp (key, "junk-check-incoming") == 0)
        camel_session_set_check_junk (
            session, g_settings_get_boolean (settings, key));
}

static const gchar *
mail_session_get_junk_filter_name (EMailSession *session)
{
    CamelJunkFilter *junk_filter;
    GHashTableIter iter;
    gpointer key, value;

    /* XXX This property can be removed once Evolution moves to
     *     GSettings and can use transform functions when binding
     *     properties to settings.  That's why this is private. */

    g_hash_table_iter_init (&iter, session->priv->junk_filters);
    junk_filter = camel_session_get_junk_filter (CAMEL_SESSION (session));

    while (g_hash_table_iter_next (&iter, &key, &value)) {
        if (junk_filter == CAMEL_JUNK_FILTER (value))
            return (const gchar *) key;
    }

    if (junk_filter != NULL)
        g_warning (
            "Camel is using a junk filter "
            "unknown to Evolution of type %s",
            G_OBJECT_TYPE_NAME (junk_filter));

    return "";
}

static void
mail_session_set_junk_filter_name (EMailSession *session,
                                   const gchar *junk_filter_name)
{
    CamelJunkFilter *junk_filter = NULL;

    /* XXX This property can be removed once Evolution moves to
     *     GSettings and can use transform functions when binding
     *     properties to settings.  That's why this is private. */

    /* An empty string is equivalent to a NULL string. */
    if (junk_filter_name != NULL && *junk_filter_name == '\0')
        junk_filter_name = NULL;

    if (junk_filter_name != NULL) {
        junk_filter = g_hash_table_lookup (
            session->priv->junk_filters, junk_filter_name);
        if (junk_filter != NULL) {
            if (!e_mail_junk_filter_available (
                E_MAIL_JUNK_FILTER (junk_filter)))
                junk_filter = NULL;
        } else {
            g_warning (
                "Unrecognized junk filter name "
                "'%s' in GSettings", junk_filter_name);
        }
    }

    camel_session_set_junk_filter (CAMEL_SESSION (session), junk_filter);

    /* XXX We emit the "notify" signal in mail_session_notify(). */
}

static void
mail_session_add_by_account (EMailSession *session,
                             EAccount *account)
{
    CamelService *service = NULL;
    CamelProvider *provider;
    CamelURL *url;
    gboolean transport_only;
    GError *error = NULL;

    /* check whether it's transport-only accounts */
    transport_only =
        (account->source == NULL) ||
        (account->source->url == NULL) ||
        (*account->source->url == '\0');
    if (transport_only)
        goto handle_transport;

    /* Load the service, but don't connect.  Check its provider,
     * and if this belongs in the folder tree model, add it. */

    url = camel_url_new (account->source->url, NULL);
    if (url != NULL) {
        provider = camel_provider_get (url->protocol, NULL);
        camel_url_free (url);
    } else {
        provider = NULL;
    }

    if (provider == NULL) {
        /* In case we do not have a provider here, we handle
         * the special case of having multiple mail identities
         * eg. a dummy account having just SMTP server defined */
        goto handle_transport;
    }

    service = camel_session_add_service (
        CAMEL_SESSION (session),
        account->uid, provider->protocol,
        CAMEL_PROVIDER_STORE, &error);

    if (error != NULL) {
        g_warning (
            "Failed to add service: %s: %s",
            account->name, error->message);
        g_error_free (error);
        return;
    }

    camel_service_set_display_name (service, account->name);

handle_transport:

    /* While we're at it, add the account's transport (if it has one)
     * to the CamelSession.  The transport's UID is a kludge for now.
     * We take the EAccount's UID and tack on "-transport". */

    if (account->transport) {
        GError *transport_error = NULL;

        url = camel_url_new (
            account->transport->url,
            &transport_error);

        if (url != NULL) {
            provider = camel_provider_get (
                url->protocol, &transport_error);
            camel_url_free (url);
        } else
            provider = NULL;

        if (provider != NULL) {
            gchar *transport_uid;

            transport_uid = g_strconcat (
                account->uid, "-transport", NULL);

            camel_session_add_service (
                CAMEL_SESSION (session),
                transport_uid, provider->protocol,
                CAMEL_PROVIDER_TRANSPORT, &transport_error);

            g_free (transport_uid);
        }

        if (transport_error) {
            g_warning (
                "%s: Failed to add transport service: %s",
                G_STRFUNC, transport_error->message);
            g_error_free (transport_error);
        }
    }
}

static void
mail_session_account_added_cb (EAccountList *account_list,
                               EAccount *account,
                               EMailSession *session)
{
    mail_session_add_by_account (session, account);
}

static void
mail_session_add_local_store (EMailSession *session)
{
    CamelLocalSettings *local_settings;
    CamelSession *camel_session;
    CamelSettings *settings;
    CamelService *service;
    const gchar *data_dir;
    gchar *path;
    gint ii;
    GError *error = NULL;

    camel_session = CAMEL_SESSION (session);

    service = camel_session_add_service (
        camel_session, E_MAIL_SESSION_LOCAL_UID,
        "maildir", CAMEL_PROVIDER_STORE, &error);

    /* XXX One could argue this is a fatal error
     *     since we depend on it in so many places. */
    if (error != NULL) {
        g_critical ("%s: %s", G_STRFUNC, error->message);
        g_error_free (error);
        return;
    }

    g_return_if_fail (CAMEL_IS_SERVICE (service));

    camel_service_set_display_name (service, _("On This Computer"));

    settings = camel_service_get_settings (service);
    local_settings = CAMEL_LOCAL_SETTINGS (settings);
    data_dir = camel_session_get_user_data_dir (camel_session);

    path = g_build_filename (data_dir, E_MAIL_SESSION_LOCAL_UID, NULL);
    camel_local_settings_set_path (local_settings, path);
    g_free (path);

    /* Shouldn't need to worry about other mail applications
     * altering files in our local mail store. */
    g_object_set (service, "need-summary-check", FALSE, NULL);

    /* Populate the local folder cache. */
    for (ii = 0; ii < E_MAIL_NUM_LOCAL_FOLDERS; ii++) {
        CamelFolder *folder;
        gchar *folder_uri;
        const gchar *display_name;
        GError *error = NULL;

        display_name = local_folder_names[ii];

        /* XXX This blocks but should be fast. */
        if (ii == E_MAIL_LOCAL_FOLDER_LOCAL_INBOX)
            folder = camel_store_get_inbox_folder_sync (
                CAMEL_STORE (service), NULL, &error);
        else
            folder = camel_store_get_folder_sync (
                CAMEL_STORE (service), display_name,
                CAMEL_STORE_FOLDER_CREATE, NULL, &error);

        folder_uri = e_mail_folder_uri_build (
            CAMEL_STORE (service), display_name);

        /* The arrays take ownership of the items added. */
        g_ptr_array_add (session->priv->local_folders, folder);
        g_ptr_array_add (session->priv->local_folder_uris, folder_uri);

        if (error != NULL) {
            g_critical ("%s: %s", G_STRFUNC, error->message);
            g_error_free (error);
        }
    }

    session->priv->local_store = g_object_ref (service);
}

static void
mail_session_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_JUNK_FILTER_NAME:
            mail_session_set_junk_filter_name (
                E_MAIL_SESSION (object),
                g_value_get_string (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_session_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_FOLDER_CACHE:
            g_value_set_object (
                value,
                e_mail_session_get_folder_cache (
                E_MAIL_SESSION (object)));
            return;

        case PROP_JUNK_FILTER_NAME:
            g_value_set_string (
                value,
                mail_session_get_junk_filter_name (
                E_MAIL_SESSION (object)));
            return;

        case PROP_LOCAL_STORE:
            g_value_set_object (
                value,
                e_mail_session_get_local_store (
                E_MAIL_SESSION (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_session_dispose (GObject *object)
{
    EMailSessionPrivate *priv;

    priv = E_MAIL_SESSION_GET_PRIVATE (object);

    if (priv->folder_cache != NULL) {
        g_object_unref (priv->folder_cache);
        priv->folder_cache = NULL;
    }

    if (priv->account_list != NULL) {
        g_signal_handler_disconnect (
            priv->account_list,
            priv->account_added_handler_id);
        g_object_unref (priv->account_list);
        priv->account_list = NULL;
    }

    if (priv->local_store != NULL) {
        g_object_unref (priv->local_store);
        priv->local_store = NULL;
    }

    g_ptr_array_set_size (priv->local_folders, 0);
    g_ptr_array_set_size (priv->local_folder_uris, 0);

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_mail_session_parent_class)->dispose (object);
}

static void
mail_session_finalize (GObject *object)
{
    EMailSessionPrivate *priv;

    priv = E_MAIL_SESSION_GET_PRIVATE (object);

    g_hash_table_destroy (priv->junk_filters);
    g_object_unref (priv->proxy);

    g_ptr_array_free (priv->local_folders, TRUE);
    g_ptr_array_free (priv->local_folder_uris, TRUE);

    g_free (mail_data_dir);
    g_free (mail_config_dir);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_mail_session_parent_class)->finalize (object);
}

static void
mail_session_notify (GObject *object,
                     GParamSpec *pspec)
{
    /* GObject does not implement this method; do not chain up. */

    /* XXX Delete this once Evolution moves to GSettings and
     *     we're able to get rid of PROP_JUNK_FILTER_NAME. */
    if (g_strcmp0 (pspec->name, "junk-filter") == 0)
        g_object_notify (object, "junk-filter-name");
}

static gboolean
mail_session_initialize_stores_idle (gpointer user_data)
{
    EMailSession *session = user_data;
    EAccountList *account_list;
    EAccount *account;
    EIterator *iter;

    g_return_val_if_fail (session != NULL, FALSE);

    account_list = e_get_account_list ();
    iter = e_list_get_iterator (E_LIST (account_list));

    while (e_iterator_is_valid (iter)) {
        /* XXX EIterator misuses const. */
        account = (EAccount *) e_iterator_get (iter);

        mail_session_add_by_account (session, account);

        e_iterator_next (iter);
    }

    g_object_unref (iter);

    return FALSE;
}

static void
mail_session_constructed (GObject *object)
{
    EMailSession *session;
    EExtensible *extensible;
    GType extension_type;
    GList *list, *link;
    GSettings *settings;
    EAccountList *account_list;
    gulong handler_id;

    session = E_MAIL_SESSION (object);

    /* Chain up to parent's constructed() method. */
    G_OBJECT_CLASS (e_mail_session_parent_class)->constructed (object);

    account_list = e_get_account_list ();
    session->priv->account_list = g_object_ref (account_list);

    /* This must be created after the account store. */
    session->priv->folder_cache = mail_folder_cache_new (session);

    /* Add built-in CamelStores. */
    mail_session_add_local_store (session);

    /* Give it a chance to load user settings, they are not loaded yet.
     *
     * XXX Is this the case where hiding such natural things like loading
     *     user setting into an EExtension strikes back and proves itself
     *     being suboptimal?
     */
    g_idle_add (mail_session_initialize_stores_idle, object);

    /* Listen for account list updates. */

    handler_id = g_signal_connect (
        account_list, "account-added",
        G_CALLBACK (mail_session_account_added_cb), session);
    session->priv->account_added_handler_id = handler_id;

    extensible = E_EXTENSIBLE (object);
    e_extensible_load_extensions (extensible);

    /* Add junk filter extensions to an internal hash table. */

    extension_type = E_TYPE_MAIL_JUNK_FILTER;
    list = e_extensible_list_extensions (extensible, extension_type);

    for (link = list; link != NULL; link = g_list_next (link)) {
        EMailJunkFilter *junk_filter;
        EMailJunkFilterClass *class;

        junk_filter = E_MAIL_JUNK_FILTER (link->data);
        class = E_MAIL_JUNK_FILTER_GET_CLASS (junk_filter);

        if (!CAMEL_IS_JUNK_FILTER (junk_filter)) {
            g_warning (
                "Skipping %s: Does not implement "
                "CamelJunkFilterInterface",
                G_OBJECT_TYPE_NAME (junk_filter));
            continue;
        }

        if (class->filter_name == NULL) {
            g_warning (
                "Skipping %s: filter_name unset",
                G_OBJECT_TYPE_NAME (junk_filter));
            continue;
        }

        if (class->display_name == NULL) {
            g_warning (
                "Skipping %s: display_name unset",
                G_OBJECT_TYPE_NAME (junk_filter));
            continue;
        }

        /* No need to reference the EMailJunkFilter since
         * EMailSession owns the reference to it already. */
        g_hash_table_insert (
            session->priv->junk_filters,
            (gpointer) class->filter_name,
            junk_filter);
    }

    g_list_free (list);

    /* Bind the "junk-default-plugin" GSettings
     * key to our "junk-filter-name" property. */

    settings = g_settings_new ("org.gnome.evolution.mail");
    g_settings_bind (
        settings, "junk-default-plugin",
        object, "junk-filter-name",
        G_SETTINGS_BIND_DEFAULT);
    g_object_unref (settings);
}

static CamelService *
mail_session_add_service (CamelSession *session,
                          const gchar *uid,
                          const gchar *protocol,
                          CamelProviderType type,
                          GError **error)
{
    CamelService *service;

    /* Chain up to parents add_service() method. */
    service = CAMEL_SESSION_CLASS (e_mail_session_parent_class)->
        add_service (session, uid, protocol, type, error);

    /* Initialize the CamelSettings object from CamelURL parameters.
     * This is temporary; soon we'll read settings from key files. */

    if (CAMEL_IS_SERVICE (service)) {
        EAccount *account;
        CamelURL *url = NULL;

        account = e_get_account_by_uid (uid);
        if (account != NULL) {
            const gchar *url_string = NULL;

            switch (type) {
                case CAMEL_PROVIDER_STORE:
                    url_string = account->source->url;
                    break;
                case CAMEL_PROVIDER_TRANSPORT:
                    url_string = account->transport->url;
                    break;
                default:
                    break;
            }

            if (url_string != NULL) {
                url = camel_url_new (url_string, error);
                if (url == NULL) {
                    g_object_unref (service);
                    service = NULL;
                }
            }
        }

        if (url != NULL) {
            CamelSettings *settings;

            settings = camel_service_get_settings (service);
            camel_settings_load_from_url (settings, url);
            camel_url_free (url);

            g_object_notify (G_OBJECT (service), "settings");

            /* Migrate files for this service from its old
             * URL-based directory to a UID-based directory
             * if necessary. */
            camel_service_migrate_files (service);
        }
    }

    return service;
}

static gchar *
mail_session_get_password (CamelSession *session,
                           CamelService *service,
                           const gchar *prompt,
                           const gchar *item,
                           guint32 flags,
                           GError **error)
{
    EAccount *account = NULL;
    const gchar *display_name = NULL;
    const gchar *uid = NULL;
    gchar *ret = NULL;

    if (CAMEL_IS_SERVICE (service)) {
        display_name = camel_service_get_display_name (service);
        uid = camel_service_get_uid (service);
        account = e_get_account_by_uid (uid);
    }

    if (!strcmp(item, "popb4smtp_uid")) {
        /* not 100% mt safe, but should be ok */
        ret = g_strdup ((account != NULL) ? account->uid : uid);
    } else {
        gchar *key = mail_session_make_key (service, item);
        EAccountService *config_service = NULL;

        ret = e_passwords_get_password (NULL, key);
        if (ret == NULL || (flags & CAMEL_SESSION_PASSWORD_REPROMPT)) {
            gboolean remember;

            g_free (ret);
            ret = NULL;

            if (account != NULL) {
                if (CAMEL_IS_STORE (service))
                    config_service = account->source;
                if (CAMEL_IS_TRANSPORT (service))
                    config_service = account->transport;
            }

            remember = config_service ? config_service->save_passwd : FALSE;

            if (!config_service || (config_service &&
                !config_service->get_password_canceled)) {
                guint32 eflags;
                gchar *title;

                if (flags & CAMEL_SESSION_PASSPHRASE) {
                    if (display_name != NULL)
                        title = g_strdup_printf (
                            _("Enter Passphrase for %s"),
                            display_name);
                    else
                        title = g_strdup (
                            _("Enter Passphrase"));
                } else {
                    if (display_name != NULL)
                        title = g_strdup_printf (
                            _("Enter Password for %s"),
                            display_name);
                    else
                        title = g_strdup (
                            _("Enter Password"));
                }
                if ((flags & CAMEL_SESSION_PASSWORD_STATIC) != 0)
                    eflags = E_PASSWORDS_REMEMBER_NEVER;
                else if (config_service == NULL)
                    eflags = E_PASSWORDS_REMEMBER_SESSION;
                else
                    eflags = E_PASSWORDS_REMEMBER_FOREVER;

                if (flags & CAMEL_SESSION_PASSWORD_REPROMPT)
                    eflags |= E_PASSWORDS_REPROMPT;

                if (flags & CAMEL_SESSION_PASSWORD_SECRET)
                    eflags |= E_PASSWORDS_SECRET;

                if (flags & CAMEL_SESSION_PASSPHRASE)
                    eflags |= E_PASSWORDS_PASSPHRASE;

                /* HACK: breaks abstraction ...
                 * e_account_writable() doesn't use the
                 * EAccount, it also uses the same writable
                 * key for source and transport. */
                if (!e_account_writable (NULL, E_ACCOUNT_SOURCE_SAVE_PASSWD))
                    eflags |= E_PASSWORDS_DISABLE_REMEMBER;

                ret = e_passwords_ask_password (
                    title, NULL, key, prompt,
                    eflags, &remember, NULL);

                if (!ret)
                    e_passwords_forget_password (NULL, key);

                g_free (title);

                if (ret && config_service) {
                    config_service->save_passwd = remember;
                    e_account_list_save (e_get_account_list ());
                }

                if (config_service)
                    config_service->get_password_canceled = ret == NULL;
            }
        }

        g_free (key);
    }

    if (ret == NULL)
        g_set_error (
            error, G_IO_ERROR,
            G_IO_ERROR_CANCELLED,
            _("User canceled operation."));

    return ret;
}

static gboolean
mail_session_forget_password (CamelSession *session,
                              CamelService *service,
                              const gchar *item,
                              GError **error)
{
    gchar *key;

    key = mail_session_make_key (service, item);

    e_passwords_forget_password (NULL, key);

    g_free (key);

    return TRUE;
}

static gint
mail_session_alert_user (CamelSession *session,
                         CamelSessionAlertType type,
                         const gchar *prompt,
                         GSList *button_captions)
{
    struct _user_message_msg *m;
    GCancellable *cancellable;
    gint result = -1;
    GSList *iter;

    m = mail_msg_new (&user_message_info);
    m->ismain = mail_in_main_thread ();
    m->type = type;
    m->prompt = g_strdup (prompt);
    m->done = e_flag_new ();
    m->button_captions = g_slist_copy (button_captions);

    for (iter = m->button_captions; iter; iter = iter->next)
        iter->data = g_strdup (iter->data);

    if (g_slist_length (button_captions) > 1)
        mail_msg_ref (m);

    cancellable = m->base.cancellable;

    if (m->ismain)
        user_message_exec (m, cancellable, &m->base.error);
    else
        mail_msg_main_loop_push (m);

    if (g_slist_length (button_captions) > 1) {
        e_flag_wait (m->done);
        result = m->result;
        mail_msg_unref (m);
    } else if (m->ismain)
        mail_msg_unref (m);

    return result;
}

static CamelFilterDriver *
mail_session_get_filter_driver (CamelSession *session,
                                const gchar *type,
                                GError **error)
{
    return (CamelFilterDriver *) mail_call_main (
        MAIL_CALL_p_ppp, (MailMainFunc) main_get_filter_driver,
        session, type, error);
}

static gboolean
mail_session_lookup_addressbook (CamelSession *session,
                                 const gchar *name)
{
    CamelInternetAddress *addr;
    gboolean ret;

    if (!mail_config_get_lookup_book ())
        return FALSE;

    addr = camel_internet_address_new ();
    camel_address_decode ((CamelAddress *) addr, name);
    ret = em_utils_in_addressbook (
        addr, mail_config_get_lookup_book_local_only ());
    g_object_unref (addr);

    return ret;
}

static gboolean
mail_session_forward_to (CamelSession *session,
                         CamelFolder *folder,
                         CamelMimeMessage *message,
                         const gchar *address,
                         GError **error)
{
    EAccount *account;
    CamelMimeMessage *forward;
    CamelStream *mem;
    CamelInternetAddress *addr;
    CamelFolder *out_folder;
    CamelMessageInfo *info;
    CamelMedium *medium;
    const gchar *from_address;
    const gchar *from_name;
    const gchar *header_name;
    struct _camel_header_raw *xev;
    gchar *subject;

    g_return_val_if_fail (folder != NULL, FALSE);
    g_return_val_if_fail (message != NULL, FALSE);
    g_return_val_if_fail (address != NULL, FALSE);

    if (!*address) {
        g_set_error (
            error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            _("No destination address provided, forward "
              "of the message has been cancelled."));
        return FALSE;
    }

    account = em_utils_guess_account_with_recipients (message, folder);
    if (!account) {
        g_set_error (
            error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
            _("No account found to use, forward of the "
              "message has been cancelled."));
        return FALSE;
    }

    from_address = account->id->address;
    from_name = account->id->name;

    forward = camel_mime_message_new ();

    /* make copy of the message, because we are going to modify it */
    mem = camel_stream_mem_new ();
    camel_data_wrapper_write_to_stream_sync (
        CAMEL_DATA_WRAPPER (message), mem, NULL, NULL);
    g_seekable_seek (G_SEEKABLE (mem), 0, G_SEEK_SET, NULL, NULL);
    camel_data_wrapper_construct_from_stream_sync (
        CAMEL_DATA_WRAPPER (forward), mem, NULL, NULL);
    g_object_unref (mem);

    /* clear previous recipients */
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_TO, NULL);
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_CC, NULL);
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_BCC, NULL);
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_RESENT_TO, NULL);
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_RESENT_CC, NULL);
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_RESENT_BCC, NULL);

    medium = CAMEL_MEDIUM (forward);

    /* remove all delivery and notification headers */
    header_name = "Disposition-Notification-To";
    while (camel_medium_get_header (medium, header_name))
        camel_medium_remove_header (medium, header_name);

    header_name = "Delivered-To";
    while (camel_medium_get_header (medium, header_name))
        camel_medium_remove_header (medium, header_name);

    /* remove any X-Evolution-* headers that may have been set */
    xev = mail_tool_remove_xevolution_headers (forward);
    camel_header_raw_clear (&xev);

    /* from */
    addr = camel_internet_address_new ();
    camel_internet_address_add (addr, from_name, from_address);
    camel_mime_message_set_from (forward, addr);
    g_object_unref (addr);

    /* to */
    addr = camel_internet_address_new ();
    camel_address_decode (CAMEL_ADDRESS (addr), address);
    camel_mime_message_set_recipients (
        forward, CAMEL_RECIPIENT_TYPE_TO, addr);
    g_object_unref (addr);

    /* subject */
    subject = mail_tool_generate_forward_subject (message);
    camel_mime_message_set_subject (forward, subject);
    g_free (subject);

    /* and send it */
    info = camel_message_info_new (NULL);
    out_folder = e_mail_session_get_local_folder (
        E_MAIL_SESSION (session), E_MAIL_LOCAL_FOLDER_OUTBOX);
    camel_message_info_set_flags (
        info, CAMEL_MESSAGE_SEEN, CAMEL_MESSAGE_SEEN);

    /* FIXME Pass a GCancellable. */
    e_mail_folder_append_message (
        out_folder, forward, info, G_PRIORITY_DEFAULT, NULL,
        (GAsyncReadyCallback) ms_forward_to_cb, session);

    camel_message_info_free (info);

    return TRUE;
}

static void
mail_session_get_socks_proxy (CamelSession *session,
                              const gchar *for_host,
                              gchar **host_ret,
                              gint *port_ret)
{
    EMailSession *mail_session;
    gchar *uri;

    g_return_if_fail (session != NULL);
    g_return_if_fail (for_host != NULL);
    g_return_if_fail (host_ret != NULL);
    g_return_if_fail (port_ret != NULL);

    mail_session = E_MAIL_SESSION (session);
    g_return_if_fail (mail_session != NULL);
    g_return_if_fail (mail_session->priv != NULL);

    *host_ret = NULL;
    *port_ret = 0;

    uri = g_strconcat ("socks://", for_host, NULL);

    if (e_proxy_require_proxy_for_uri (mail_session->priv->proxy, uri)) {
        SoupURI *suri;

        suri = e_proxy_peek_uri_for (mail_session->priv->proxy, uri);
        if (suri) {
            *host_ret = g_strdup (suri->host);
            *port_ret = suri->port;
        }
    }

    g_free (uri);
}

static gboolean
mail_session_authenticate_sync (CamelSession *session,
                                CamelService *service,
                                const gchar *mechanism,
                                GCancellable *cancellable,
                                GError **error)
{
    CamelServiceAuthType *authtype = NULL;
    CamelAuthenticationResult result;
    CamelProvider *provider;
    CamelSettings *settings;
    const gchar *password;
    guint32 password_flags;
    GError *local_error = NULL;

    /* Do not chain up.  Camel's default method is only an example for
     * subclasses to follow.  Instead we mimic most of its logic here. */

    provider = camel_service_get_provider (service);
    settings = camel_service_get_settings (service);

    /* APOP is one case where a non-SASL mechanism name is passed, so
     * don't bail if the CamelServiceAuthType struct comes back NULL. */
    if (mechanism != NULL)
        authtype = camel_sasl_authtype (mechanism);

    /* If the SASL mechanism does not involve a user
     * password, then it gets one shot to authenticate. */
    if (authtype != NULL && !authtype->need_password) {
        result = camel_service_authenticate_sync (
            service, mechanism, cancellable, error);
        if (result == CAMEL_AUTHENTICATION_REJECTED)
            g_set_error (
                error, CAMEL_SERVICE_ERROR,
                CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
                _("%s authentication failed"), mechanism);
        return (result == CAMEL_AUTHENTICATION_ACCEPTED);
    }

    /* Some SASL mechanisms can attempt to authenticate without a
     * user password being provided (e.g. single-sign-on credentials),
     * but can fall back to a user password.  Handle that case next. */
    if (mechanism != NULL) {
        CamelProvider *provider;
        CamelSasl *sasl;
        const gchar *service_name;
        gboolean success = FALSE;

        provider = camel_service_get_provider (service);
        service_name = provider->protocol;

        /* XXX Would be nice if camel_sasl_try_empty_password_sync()
         *     returned CamelAuthenticationResult so it's easier to
         *     detect errors. */
        sasl = camel_sasl_new (service_name, mechanism, service);
        if (sasl != NULL) {
            success = camel_sasl_try_empty_password_sync (
                sasl, cancellable, &local_error);
            g_object_unref (sasl);
        }

        if (success)
            return TRUE;
    }

    /* Abort authentication if we got cancelled.
     * Otherwise clear any errors and press on. */
    if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        return FALSE;

    g_clear_error (&local_error);

    password_flags = CAMEL_SESSION_PASSWORD_SECRET;

retry:
    password = camel_service_get_password (service);

    if (password == NULL) {
        CamelNetworkSettings *network_settings;
        const gchar *host;
        const gchar *user;
        gchar *prompt;
        gchar *new_passwd;

        network_settings = CAMEL_NETWORK_SETTINGS (settings);
        host = camel_network_settings_get_host (network_settings);
        user = camel_network_settings_get_user (network_settings);

        prompt = camel_session_build_password_prompt (
            provider->name, user, host);

        new_passwd = camel_session_get_password (
            session, service, prompt, "password",
            password_flags, &local_error);
        camel_service_set_password (service, new_passwd);
        password = camel_service_get_password (service);
        g_free (new_passwd);

        g_free (prompt);

        if (local_error != NULL) {
            g_propagate_error (error, local_error);
            return FALSE;
        }

        if (password == NULL) {
            g_set_error (
                error, CAMEL_SERVICE_ERROR,
                CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
                _("No password was provided"));
            return FALSE;
        }
    }

    result = camel_service_authenticate_sync (
        service, mechanism, cancellable, error);

    if (result == CAMEL_AUTHENTICATION_REJECTED) {
        password_flags |= CAMEL_SESSION_PASSWORD_REPROMPT;
        camel_service_set_password (service, NULL);
        goto retry;
    }

    return (result == CAMEL_AUTHENTICATION_ACCEPTED);
}

static void
e_mail_session_class_init (EMailSessionClass *class)
{
    GObjectClass *object_class;
    CamelSessionClass *session_class;

    g_type_class_add_private (class, sizeof (EMailSessionPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = mail_session_set_property;
    object_class->get_property = mail_session_get_property;
    object_class->dispose = mail_session_dispose;
    object_class->finalize = mail_session_finalize;
    object_class->notify = mail_session_notify;
    object_class->constructed = mail_session_constructed;

    session_class = CAMEL_SESSION_CLASS (class);
    session_class->add_service = mail_session_add_service;
    session_class->get_password = mail_session_get_password;
    session_class->forget_password = mail_session_forget_password;
    session_class->alert_user = mail_session_alert_user;
    session_class->get_filter_driver = mail_session_get_filter_driver;
    session_class->lookup_addressbook = mail_session_lookup_addressbook;
    session_class->forward_to = mail_session_forward_to;
    session_class->get_socks_proxy = mail_session_get_socks_proxy;
    session_class->authenticate_sync = mail_session_authenticate_sync;

    g_object_class_install_property (
        object_class,
        PROP_FOLDER_CACHE,
        g_param_spec_object (
            "folder-cache",
            NULL,
            NULL,
            MAIL_TYPE_FOLDER_CACHE,
            G_PARAM_READABLE |
            G_PARAM_STATIC_STRINGS));

    /* XXX This property can be removed once Evolution moves to
     *     GSettings and can use transform functions when binding
     *     properties to settings. */
    g_object_class_install_property (
        object_class,
        PROP_JUNK_FILTER_NAME,
        g_param_spec_string (
            "junk-filter-name",
            NULL,
            NULL,
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_STATIC_STRINGS));

    g_object_class_install_property (
        object_class,
        PROP_LOCAL_STORE,
        g_param_spec_object (
            "local-store",
            "Local Store",
            "Built-in local store",
            CAMEL_TYPE_STORE,
            G_PARAM_READABLE |
            G_PARAM_STATIC_STRINGS));

    /**
     * EMailSession::flush-outbox
     * @session: the email session
     *
     * Emitted if the send folder should be flushed.
     **/
    signals[FLUSH_OUTBOX] = g_signal_new (
        "flush-outbox",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST,
        0, /* struct offset */
        NULL, NULL, /* accumulator */
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);

    /**
     * EMailSession::store-added
     * @session: the email session
     * @store: the CamelStore
     *
     * Emitted when a store is added
     **/
    signals[STORE_ADDED] = g_signal_new (
        "store-added",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST,
        0, /* struct offset */
        NULL, NULL, /* accumulator */
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        CAMEL_TYPE_STORE);

    /**
     * EMailSession::store-removed
     * @session: the email session
     * @store: the CamelStore
     *
     * Emitted when a store is removed 
     **/
    signals[STORE_REMOVED] = g_signal_new (
        "store-removed",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_FIRST,
        0, /* struct offset */
        NULL, NULL, /* accumulator */
        g_cclosure_marshal_VOID__OBJECT,
        G_TYPE_NONE, 1,
        CAMEL_TYPE_STORE);

}

static void
e_mail_session_init (EMailSession *session)
{
    GSettings *settings;
    GHashTable *junk_filters;

    junk_filters = g_hash_table_new (
        (GHashFunc) g_str_hash,
        (GEqualFunc) g_str_equal);

    session->priv = E_MAIL_SESSION_GET_PRIVATE (session);
    session->priv->junk_filters = junk_filters;
    session->priv->proxy = e_proxy_new ();

    session->priv->local_folders =
        g_ptr_array_new_with_free_func (
        (GDestroyNotify) g_object_unref);
    session->priv->local_folder_uris =
        g_ptr_array_new_with_free_func (
        (GDestroyNotify) g_free);

    /* Initialize the EAccount setup. */
    e_account_writable (NULL, E_ACCOUNT_SOURCE_SAVE_PASSWD);

    settings = g_settings_new ("org.gnome.evolution.mail");

    camel_session_set_check_junk (
        CAMEL_SESSION (session), g_settings_get_boolean (
        settings, "junk-check-incoming"));
    g_signal_connect (
        settings, "changed",
        G_CALLBACK (mail_session_check_junk_notify), session);

    mail_config_reload_junk_headers (session);

    e_proxy_setup_proxy (session->priv->proxy);

    g_object_unref (settings);
}

EMailSession *
e_mail_session_new (void)
{
    const gchar *user_data_dir;
    const gchar *user_cache_dir;

    user_data_dir = mail_session_get_data_dir ();
    user_cache_dir = mail_session_get_cache_dir ();

    return g_object_new (
        E_TYPE_MAIL_SESSION,
        "user-data-dir", user_data_dir,
        "user-cache-dir", user_cache_dir,
        NULL);
}

MailFolderCache *
e_mail_session_get_folder_cache (EMailSession *session)
{
    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    return session->priv->folder_cache;
}

CamelStore *
e_mail_session_get_local_store (EMailSession *session)
{
    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    return session->priv->local_store;
}

CamelFolder *
e_mail_session_get_local_folder (EMailSession *session,
                                 EMailLocalFolder type)
{
    GPtrArray *local_folders;
    CamelFolder *folder;

    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    local_folders = session->priv->local_folders;
    g_return_val_if_fail (type < local_folders->len, NULL);

    folder = g_ptr_array_index (local_folders, type);
    g_return_val_if_fail (CAMEL_IS_FOLDER (folder), NULL);

    return folder;
}

const gchar *
e_mail_session_get_local_folder_uri (EMailSession *session,
                                     EMailLocalFolder type)
{
    GPtrArray *local_folder_uris;
    const gchar *folder_uri;

    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    local_folder_uris = session->priv->local_folder_uris;
    g_return_val_if_fail (type < local_folder_uris->len, NULL);

    folder_uri = g_ptr_array_index (local_folder_uris, type);
    g_return_val_if_fail (folder_uri != NULL, NULL);

    return folder_uri;
}

GList *
e_mail_session_get_available_junk_filters (EMailSession *session)
{
    GList *list, *link;
    GQueue trash = G_QUEUE_INIT;

    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);

    list = g_hash_table_get_values (session->priv->junk_filters);

    /* Discard unavailable junk filters.  (e.g. Junk filter
     * requires Bogofilter but Bogofilter is not installed,
     * hence the junk filter is unavailable.) */

    for (link = list; link != NULL; link = g_list_next (link)) {
        EMailJunkFilter *junk_filter;

        junk_filter = E_MAIL_JUNK_FILTER (link->data);
        if (!e_mail_junk_filter_available (junk_filter))
            g_queue_push_tail (&trash, link);
    }

    while ((link = g_queue_pop_head (&trash)) != NULL)
        list = g_list_delete_link (list, link);

    /* Sort the remaining junk filters by display name. */

    return g_list_sort (list, (GCompareFunc) e_mail_junk_filter_compare);
}

static void
mail_session_get_inbox_thread (GSimpleAsyncResult *simple,
                               EMailSession *session,
                               GCancellable *cancellable)
{
    AsyncContext *context;
    GError *error = NULL;

    context = g_simple_async_result_get_op_res_gpointer (simple);

    context->folder = e_mail_session_get_inbox_sync (
        session, context->uid, cancellable, &error);

    if (error != NULL)
        g_simple_async_result_take_error (simple, error);
}

CamelFolder *
e_mail_session_get_inbox_sync (EMailSession *session,
                               const gchar *service_uid,
                               GCancellable *cancellable,
                               GError **error)
{
    CamelService *service;

    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
    g_return_val_if_fail (service_uid != NULL, NULL);

    service = camel_session_get_service (
        CAMEL_SESSION (session), service_uid);

    if (!CAMEL_IS_STORE (service))
        return NULL;

    if (!em_utils_connect_service_sync (service, cancellable, error))
        return NULL;

    return camel_store_get_inbox_folder_sync (
        CAMEL_STORE (service), cancellable, error);
}

void
e_mail_session_get_inbox (EMailSession *session,
                          const gchar *service_uid,
                          gint io_priority,
                          GCancellable *cancellable,
                          GAsyncReadyCallback callback,
                          gpointer user_data)
{
    GSimpleAsyncResult *simple;
    AsyncContext *context;

    g_return_if_fail (E_IS_MAIL_SESSION (session));
    g_return_if_fail (service_uid != NULL);

    context = g_slice_new0 (AsyncContext);
    context->uid = g_strdup (service_uid);

    simple = g_simple_async_result_new (
        G_OBJECT (session), callback,
        user_data, e_mail_session_get_inbox);

    g_simple_async_result_set_op_res_gpointer (
        simple, context, (GDestroyNotify) async_context_free);

    g_simple_async_result_run_in_thread (
        simple, (GSimpleAsyncThreadFunc)
        mail_session_get_inbox_thread,
        io_priority, cancellable);

    g_object_unref (simple);
}

CamelFolder *
e_mail_session_get_inbox_finish (EMailSession *session,
                                 GAsyncResult *result,
                                 GError **error)
{
    GSimpleAsyncResult *simple;
    AsyncContext *context;

    g_return_val_if_fail (
        g_simple_async_result_is_valid (
        result, G_OBJECT (session),
        e_mail_session_get_inbox), NULL);

    simple = G_SIMPLE_ASYNC_RESULT (result);
    context = g_simple_async_result_get_op_res_gpointer (simple);

    if (g_simple_async_result_propagate_error (simple, error))
        return NULL;

    g_return_val_if_fail (CAMEL_IS_FOLDER (context->folder), NULL);

    return g_object_ref (context->folder);
}

static void
mail_session_get_trash_thread (GSimpleAsyncResult *simple,
                               EMailSession *session,
                               GCancellable *cancellable)
{
    AsyncContext *context;
    GError *error = NULL;

    context = g_simple_async_result_get_op_res_gpointer (simple);

    context->folder = e_mail_session_get_trash_sync (
        session, context->uid, cancellable, &error);

    if (error != NULL)
        g_simple_async_result_take_error (simple, error);
}

CamelFolder *
e_mail_session_get_trash_sync (EMailSession *session,
                               const gchar *service_uid,
                               GCancellable *cancellable,
                               GError **error)
{
    CamelService *service;

    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
    g_return_val_if_fail (service_uid != NULL, NULL);

    service = camel_session_get_service (
        CAMEL_SESSION (session), service_uid);

    if (!CAMEL_IS_STORE (service))
        return NULL;

    if (!em_utils_connect_service_sync (service, cancellable, error))
        return NULL;

    return camel_store_get_trash_folder_sync (
        CAMEL_STORE (service), cancellable, error);
}

void
e_mail_session_get_trash (EMailSession *session,
                          const gchar *service_uid,
                          gint io_priority,
                          GCancellable *cancellable,
                          GAsyncReadyCallback callback,
                          gpointer user_data)
{
    GSimpleAsyncResult *simple;
    AsyncContext *context;

    g_return_if_fail (E_IS_MAIL_SESSION (session));
    g_return_if_fail (service_uid != NULL);

    context = g_slice_new0 (AsyncContext);
    context->uid = g_strdup (service_uid);

    simple = g_simple_async_result_new (
        G_OBJECT (session), callback,
        user_data, e_mail_session_get_trash);

    g_simple_async_result_set_op_res_gpointer (
        simple, context, (GDestroyNotify) async_context_free);

    g_simple_async_result_run_in_thread (
        simple, (GSimpleAsyncThreadFunc)
        mail_session_get_trash_thread,
        io_priority, cancellable);

    g_object_unref (simple);
}

CamelFolder *
e_mail_session_get_trash_finish (EMailSession *session,
                                 GAsyncResult *result,
                                 GError **error)
{
    GSimpleAsyncResult *simple;
    AsyncContext *context;

    g_return_val_if_fail (
        g_simple_async_result_is_valid (
        result, G_OBJECT (session),
        e_mail_session_get_trash), NULL);

    simple = G_SIMPLE_ASYNC_RESULT (result);
    context = g_simple_async_result_get_op_res_gpointer (simple);

    if (g_simple_async_result_propagate_error (simple, error))
        return NULL;

    g_return_val_if_fail (CAMEL_IS_FOLDER (context->folder), NULL);

    return g_object_ref (context->folder);
}

static void
mail_session_uri_to_folder_thread (GSimpleAsyncResult *simple,
                                   EMailSession *session,
                                   GCancellable *cancellable)
{
    AsyncContext *context;
    GError *error = NULL;

    context = g_simple_async_result_get_op_res_gpointer (simple);

    context->folder = e_mail_session_uri_to_folder_sync (
        session, context->uri, context->flags,
        cancellable, &error);

    if (error != NULL)
        g_simple_async_result_take_error (simple, error);
}

CamelFolder *
e_mail_session_uri_to_folder_sync (EMailSession *session,
                                   const gchar *folder_uri,
                                   CamelStoreGetFolderFlags flags,
                                   GCancellable *cancellable,
                                   GError **error)
{
    CamelStore *store;
    CamelFolder *folder;
    gchar *folder_name;
    gboolean success;

    g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
    g_return_val_if_fail (folder_uri != NULL, NULL);

    success = e_mail_folder_uri_parse (
        CAMEL_SESSION (session), folder_uri,
        &store, &folder_name, error);

    if (!success)
        return NULL;

    folder = camel_store_get_folder_sync (
        store, folder_name, flags, cancellable, error);

    if (folder != NULL) {
        MailFolderCache *folder_cache;
        folder_cache = e_mail_session_get_folder_cache (session);
        mail_folder_cache_note_folder (folder_cache, folder);
    }

    g_free (folder_name);
    g_object_unref (store);

    return folder;
}

void
e_mail_session_uri_to_folder (EMailSession *session,
                              const gchar *folder_uri,
                              CamelStoreGetFolderFlags flags,
                              gint io_priority,
                              GCancellable *cancellable,
                              GAsyncReadyCallback callback,
                              gpointer user_data)
{
    GSimpleAsyncResult *simple;
    AsyncContext *context;

    g_return_if_fail (E_IS_MAIL_SESSION (session));
    g_return_if_fail (folder_uri != NULL);

    context = g_slice_new0 (AsyncContext);
    context->uri = g_strdup (folder_uri);
    context->flags = flags;

    simple = g_simple_async_result_new (
        G_OBJECT (session), callback,
        user_data, e_mail_session_uri_to_folder);

    g_simple_async_result_set_op_res_gpointer (
        simple, context, (GDestroyNotify) async_context_free);

    g_simple_async_result_run_in_thread (
        simple, (GSimpleAsyncThreadFunc)
        mail_session_uri_to_folder_thread,
        io_priority, cancellable);

    g_object_unref (simple);
}

CamelFolder *
e_mail_session_uri_to_folder_finish (EMailSession *session,
                                     GAsyncResult *result,
                                     GError **error)
{
    GSimpleAsyncResult *simple;
    AsyncContext *context;

    g_return_val_if_fail (
        g_simple_async_result_is_valid (
        result, G_OBJECT (session),
        e_mail_session_uri_to_folder), NULL);

    simple = G_SIMPLE_ASYNC_RESULT (result);
    context = g_simple_async_result_get_op_res_gpointer (simple);

    if (g_simple_async_result_propagate_error (simple, error))
        return NULL;

    g_return_val_if_fail (CAMEL_IS_FOLDER (context->folder), NULL);

    return g_object_ref (context->folder);
}

/******************************** Legacy API *********************************/

void
mail_session_flush_filter_log (EMailSession *session)
{
    g_return_if_fail (E_IS_MAIL_SESSION (session));

    if (session->priv->filter_logfile)
        fflush (session->priv->filter_logfile);
}

const gchar *
mail_session_get_data_dir (void)
{
    if (G_UNLIKELY (mail_data_dir == NULL))
        mail_data_dir = g_build_filename (
            e_get_user_data_dir (), "mail", NULL);

    return mail_data_dir;
}

const gchar *
mail_session_get_cache_dir (void)
{
    if (G_UNLIKELY (mail_cache_dir == NULL))
        mail_cache_dir = g_build_filename (
            e_get_user_cache_dir (), "mail", NULL);

    return mail_cache_dir;
}

const gchar *
mail_session_get_config_dir (void)
{
    if (G_UNLIKELY (mail_config_dir == NULL))
        mail_config_dir = g_build_filename (
            e_get_user_config_dir (), "mail", NULL);

    return mail_config_dir;
}