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
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "msgCore.h"
#include "nsIURI.h"
#include "nsParseMailbox.h"
#include "nsIMsgHdr.h"
#include "nsIMsgDatabase.h"
#include "nsMsgMessageFlags.h"
#include "nsIDBFolderInfo.h"
#include "nsIInputStream.h"
#include "nsIFile.h"
#include "nsMsgLocalFolderHdrs.h"
#include "nsMsgBaseCID.h"
#include "nsMsgDBCID.h"
#include "nsIMailboxUrl.h"
#include "nsNetUtil.h"
#include "nsMsgFolderFlags.h"
#include "nsIMsgFolder.h"
#include "nsIURL.h"
#include "nsIMsgMailNewsUrl.h"
#include "nsIMsgFilterList.h"
#include "nsIMsgFilter.h"
#include "nsIIOService.h"
#include "nsNetCID.h"
#include "nsRDFCID.h"
#include "nsIRDFService.h"
#include "nsMsgI18N.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsIMsgLocalMailFolder.h"
#include "nsMsgUtils.h"
#include "prprf.h"
#include "prmem.h"
#include "nsISeekableStream.h"
#include "nsIMimeHeaders.h"
#include "nsIMsgMdnGenerator.h"
#include "nsMsgSearchCore.h"
#include "nsMailHeaders.h"
#include "nsIMsgMailSession.h"
#include "nsIMsgComposeParams.h"
#include "nsMsgCompCID.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDocShell.h"
#include "nsIMsgCompose.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsIMsgComposeService.h"
#include "nsIMsgCopyService.h"
#include "nsICryptoHash.h"
#include "nsIStringBundle.h"
#include "nsIMsgFilterPlugin.h"
#include "nsIMutableArray.h"
#include "nsArrayUtils.h"
#include "nsIMsgFilterCustomAction.h"
#include <ctype.h>
#include "nsIMsgPluggableStore.h"
#include "mozilla/Services.h"
#include "nsQueryObject.h"
#include "nsIOutputStream.h"
#include "mozilla/Attributes.h"
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
/* the following macros actually implement addref, release and query interface for our component. */
NS_IMPL_ISUPPORTS_INHERITED(nsMsgMailboxParser,
nsParseMailMessageState,
nsIStreamListener,
nsIRequestObserver)
// Whenever data arrives from the connection, core netlib notifices the protocol by calling
// OnDataAvailable. We then read and process the incoming data from the input stream.
NS_IMETHODIMP nsMsgMailboxParser::OnDataAvailable(nsIRequest *request, nsISupports *ctxt, nsIInputStream *aIStream, uint64_t sourceOffset, uint32_t aLength)
{
// right now, this really just means turn around and process the url
nsresult rv = NS_OK;
nsCOMPtr<nsIURI> url = do_QueryInterface(ctxt, &rv);
if (NS_SUCCEEDED(rv))
rv = ProcessMailboxInputStream(url, aIStream, aLength);
return rv;
}
NS_IMETHODIMP nsMsgMailboxParser::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
{
m_startTime = PR_Now();
// extract the appropriate event sinks from the url and initialize them in our protocol data
// the URL should be queried for a nsIMailboxURL. If it doesn't support a mailbox URL interface then
// we have an error.
nsresult rv = NS_OK;
nsCOMPtr<nsIIOService> ioServ =
mozilla::services::GetIOService();
NS_ENSURE_TRUE(ioServ, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIMailboxUrl> runningUrl = do_QueryInterface(ctxt, &rv);
nsCOMPtr<nsIMsgMailNewsUrl> url = do_QueryInterface(ctxt);
nsCOMPtr<nsIMsgFolder> folder = do_QueryReferent(m_folder);
if (NS_SUCCEEDED(rv) && runningUrl && folder)
{
url->GetStatusFeedback(getter_AddRefs(m_statusFeedback));
// okay, now fill in our event sinks...Note that each getter ref counts before
// it returns the interface to us...we'll release when we are done
folder->GetName(m_folderName);
nsCOMPtr<nsIFile> path;
folder->GetFilePath(getter_AddRefs(path));
if (path)
{
int64_t fileSize;
path->GetFileSize(&fileSize);
// the size of the mailbox file is our total base line for measuring progress
m_graph_progress_total = fileSize;
UpdateStatusText("buildingSummary");
nsCOMPtr<nsIMsgDBService> msgDBService = do_GetService(NS_MSGDB_SERVICE_CONTRACTID, &rv);
if (msgDBService)
{
// Use OpenFolderDB to always open the db so that db's m_folder
// is set correctly.
rv = msgDBService->OpenFolderDB(folder, true,
getter_AddRefs(m_mailDB));
if (rv == NS_MSG_ERROR_FOLDER_SUMMARY_MISSING)
rv = msgDBService->CreateNewDB(folder,
getter_AddRefs(m_mailDB));
if (m_mailDB)
m_mailDB->AddListener(this);
}
NS_ASSERTION(m_mailDB, "failed to open mail db parsing folder");
// try to get a backup message database
nsresult rvignore = folder->GetBackupMsgDatabase(
getter_AddRefs(m_backupMailDB));
// We'll accept failures and move on, as we're dealing with some
// sort of unknown problem to begin with.
if (NS_FAILED(rvignore))
{
if (m_backupMailDB)
m_backupMailDB->RemoveListener(this);
m_backupMailDB = nullptr;
}
else if (m_backupMailDB)
{
m_backupMailDB->AddListener(this);
}
}
}
// need to get the mailbox name out of the url and call SetMailboxName with it.
// then, we need to open the mail db for this parser.
return rv;
}
// stop binding is a "notification" informing us that the stream associated with aURL is going away.
NS_IMETHODIMP nsMsgMailboxParser::OnStopRequest(nsIRequest *request, nsISupports *ctxt, nsresult aStatus)
{
DoneParsingFolder(aStatus);
// what can we do? we can close the stream?
m_urlInProgress = false; // don't close the connection...we may be re-using it.
if (m_mailDB)
m_mailDB->RemoveListener(this);
// and we want to mark ourselves for deletion or some how inform our protocol manager that we are
// available for another url if there is one....
ReleaseFolderLock();
// be sure to clear any status text and progress info..
m_graph_progress_received = 0;
UpdateProgressPercent();
UpdateStatusText("localStatusDocumentDone");
return NS_OK;
}
NS_IMETHODIMP
nsParseMailMessageState::OnHdrPropertyChanged(nsIMsgDBHdr *aHdrToChange,
bool aPreChange, uint32_t *aStatus, nsIDBChangeListener * aInstigator)
{
return NS_OK;
}
NS_IMETHODIMP
nsParseMailMessageState::OnHdrFlagsChanged(nsIMsgDBHdr *aHdrChanged,
uint32_t aOldFlags, uint32_t aNewFlags, nsIDBChangeListener *aInstigator)
{
return NS_OK;
}
NS_IMETHODIMP
nsParseMailMessageState::OnHdrDeleted(nsIMsgDBHdr *aHdrChanged,
nsMsgKey aParentKey, int32_t aFlags, nsIDBChangeListener *aInstigator)
{
return NS_OK;
}
NS_IMETHODIMP
nsParseMailMessageState::OnHdrAdded(nsIMsgDBHdr *aHdrAdded,
nsMsgKey aParentKey, int32_t aFlags, nsIDBChangeListener *aInstigator)
{
return NS_OK;
}
/* void OnParentChanged (in nsMsgKey aKeyChanged, in nsMsgKey oldParent, in nsMsgKey newParent, in nsIDBChangeListener aInstigator); */
NS_IMETHODIMP
nsParseMailMessageState::OnParentChanged(nsMsgKey aKeyChanged,
nsMsgKey oldParent, nsMsgKey newParent, nsIDBChangeListener *aInstigator)
{
return NS_OK;
}
/* void OnAnnouncerGoingAway (in nsIDBChangeAnnouncer instigator); */
NS_IMETHODIMP
nsParseMailMessageState::OnAnnouncerGoingAway(nsIDBChangeAnnouncer *instigator)
{
if (m_backupMailDB && m_backupMailDB == instigator)
{
m_backupMailDB->RemoveListener(this);
m_backupMailDB = nullptr;
}
else if (m_mailDB)
{
m_mailDB->RemoveListener(this);
m_mailDB = nullptr;
m_newMsgHdr = nullptr;
}
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::OnEvent(nsIMsgDatabase *aDB, const char *aEvent)
{
return NS_OK;
}
/* void OnReadChanged (in nsIDBChangeListener instigator); */
NS_IMETHODIMP
nsParseMailMessageState::OnReadChanged(nsIDBChangeListener *instigator)
{
return NS_OK;
}
/* void OnJunkScoreChanged (in nsIDBChangeListener instigator); */
NS_IMETHODIMP
nsParseMailMessageState::OnJunkScoreChanged(nsIDBChangeListener *instigator)
{
return NS_OK;
}
nsMsgMailboxParser::nsMsgMailboxParser() : nsMsgLineBuffer(nullptr, false)
{
Init();
}
nsMsgMailboxParser::nsMsgMailboxParser(nsIMsgFolder *aFolder) : nsMsgLineBuffer(nullptr, false)
{
m_folder = do_GetWeakReference(aFolder);
}
nsMsgMailboxParser::~nsMsgMailboxParser()
{
ReleaseFolderLock();
}
nsresult nsMsgMailboxParser::Init()
{
m_obuffer = nullptr;
m_obuffer_size = 0;
m_graph_progress_total = 0;
m_graph_progress_received = 0;
return AcquireFolderLock();
}
void nsMsgMailboxParser::UpdateStatusText (const char* stringName)
{
if (m_statusFeedback)
{
nsresult rv;
nsCOMPtr<nsIStringBundleService> bundleService =
mozilla::services::GetStringBundleService();
if (!bundleService)
return;
nsCOMPtr<nsIStringBundle> bundle;
rv = bundleService->CreateBundle("chrome://messenger/locale/localMsgs.properties", getter_AddRefs(bundle));
if (NS_FAILED(rv))
return;
nsString finalString;
const char16_t * stringArray[] = { m_folderName.get() };
rv = bundle->FormatStringFromName(NS_ConvertASCIItoUTF16(stringName).get(),
stringArray, 1, getter_Copies(finalString));
m_statusFeedback->ShowStatusString(finalString);
}
}
void nsMsgMailboxParser::UpdateProgressPercent ()
{
if (m_statusFeedback && m_graph_progress_total != 0)
{
// prevent overflow by dividing both by 100
int64_t progressTotal = m_graph_progress_total / 100;
int64_t progressReceived = m_graph_progress_received / 100;
if (progressTotal > 0)
m_statusFeedback->ShowProgress((100 *(progressReceived)) / progressTotal);
}
}
nsresult nsMsgMailboxParser::ProcessMailboxInputStream(nsIURI* aURL, nsIInputStream *aIStream, uint32_t aLength)
{
nsresult ret = NS_OK;
uint32_t bytesRead = 0;
if (NS_SUCCEEDED(m_inputStream.GrowBuffer(aLength)))
{
// OK, this sucks, but we're going to have to copy into our
// own byte buffer, and then pass that to the line buffering code,
// which means a couple buffer copies.
ret = aIStream->Read(m_inputStream.GetBuffer(), aLength, &bytesRead);
if (NS_SUCCEEDED(ret))
ret = BufferInput(m_inputStream.GetBuffer(), bytesRead);
}
if (m_graph_progress_total > 0)
{
if (NS_SUCCEEDED(ret))
m_graph_progress_received += bytesRead;
}
return (ret);
}
void nsMsgMailboxParser::DoneParsingFolder(nsresult status)
{
/* End of file. Flush out any partial line remaining in the buffer. */
FlushLastLine();
PublishMsgHeader(nullptr);
// only mark the db valid if we've succeeded.
if (NS_SUCCEEDED(status) && m_mailDB) // finished parsing, so flush db folder info
UpdateDBFolderInfo();
else if (m_mailDB)
m_mailDB->SetSummaryValid(false);
// remove the backup database
if (m_backupMailDB)
{
nsCOMPtr<nsIMsgFolder> folder = do_QueryReferent(m_folder);
if (folder)
folder->RemoveBackupMsgDatabase();
m_backupMailDB = nullptr;
}
// if (m_folder != nullptr)
// m_folder->SummaryChanged();
FreeBuffers();
}
void nsMsgMailboxParser::FreeBuffers()
{
/* We're done reading the folder - we don't need these things
any more. */
PR_FREEIF (m_obuffer);
m_obuffer_size = 0;
}
void nsMsgMailboxParser::UpdateDBFolderInfo()
{
UpdateDBFolderInfo(m_mailDB);
}
// update folder info in db so we know not to reparse.
void nsMsgMailboxParser::UpdateDBFolderInfo(nsIMsgDatabase *mailDB)
{
mailDB->SetSummaryValid(true);
}
// Tell the world about the message header (add to db, and view, if any)
int32_t nsMsgMailboxParser::PublishMsgHeader(nsIMsgWindow *msgWindow)
{
FinishHeader();
if (m_newMsgHdr)
{
char storeToken[100];
PR_snprintf(storeToken, sizeof(storeToken), "%lld", m_envelope_pos);
m_newMsgHdr->SetStringProperty("storeToken", storeToken);
uint32_t flags;
(void)m_newMsgHdr->GetFlags(&flags);
if (flags & nsMsgMessageFlags::Expunged)
{
nsCOMPtr<nsIDBFolderInfo> folderInfo;
m_mailDB->GetDBFolderInfo(getter_AddRefs(folderInfo));
uint32_t size;
(void)m_newMsgHdr->GetMessageSize(&size);
folderInfo->ChangeExpungedBytes(size);
m_newMsgHdr = nullptr;
}
else if (m_mailDB)
{
// add hdr but don't notify - shouldn't be requiring notifications
// during summary file rebuilding
m_mailDB->AddNewHdrToDB(m_newMsgHdr, false);
m_newMsgHdr = nullptr;
}
else
NS_ASSERTION(false, "no database while parsing local folder"); // should have a DB, no?
}
else if (m_mailDB)
{
nsCOMPtr<nsIDBFolderInfo> folderInfo;
m_mailDB->GetDBFolderInfo(getter_AddRefs(folderInfo));
if (folderInfo)
folderInfo->ChangeExpungedBytes(m_position - m_envelope_pos);
}
return 0;
}
void nsMsgMailboxParser::AbortNewHeader()
{
if (m_newMsgHdr && m_mailDB)
m_newMsgHdr = nullptr;
}
void nsMsgMailboxParser::OnNewMessage(nsIMsgWindow *msgWindow)
{
PublishMsgHeader(msgWindow);
Clear();
}
nsresult nsMsgMailboxParser::HandleLine(const char *line, uint32_t lineLength)
{
/* If this is the very first line of a non-empty folder, make sure it's an envelope */
if (m_graph_progress_received == 0)
{
/* This is the first block from the file. Check to see if this
looks like a mail file. */
const char *s = line;
const char *end = s + lineLength;
while (s < end && IS_SPACE(*s))
s++;
if ((end - s) < 20 || !IsEnvelopeLine(s, end - s))
{
// char buf[500];
// PR_snprintf (buf, sizeof(buf),
// XP_GetString(MK_MSG_NON_MAIL_FILE_READ_QUESTION),
// folder_name);
// else if (!FE_Confirm (m_context, buf))
// return NS_MSG_NOT_A_MAIL_FOLDER; /* #### NOT_A_MAIL_FILE */
}
}
// m_graph_progress_received += lineLength;
// mailbox parser needs to do special stuff when it finds an envelope
// after parsing a message body. So do that.
if (line[0] == 'F' && IsEnvelopeLine(line, lineLength))
{
// **** This used to be
// PR_ASSERT (m_parseMsgState->m_state == nsMailboxParseBodyState);
// **** I am not sure this is a right thing to do. This happens when
// going online, downloading a message while playing back append
// draft/template offline operation. We are mixing
// nsMailboxParseBodyState &&
// nsMailboxParseHeadersState. David I need your help here too. **** jt
NS_ASSERTION (m_state == nsIMsgParseMailMsgState::ParseBodyState ||
m_state == nsIMsgParseMailMsgState::ParseHeadersState, "invalid parse state"); /* else folder corrupted */
OnNewMessage(nullptr);
nsresult rv = StartNewEnvelope(line, lineLength);
NS_ASSERTION(NS_SUCCEEDED(rv), " error starting envelope parsing mailbox");
// at the start of each new message, update the progress bar
UpdateProgressPercent();
return rv;
}
// otherwise, the message parser can handle it completely.
if (m_mailDB != nullptr) // if no DB, do we need to parse at all?
return ParseFolderLine(line, lineLength);
return NS_ERROR_NULL_POINTER; // need to error out if we don't have a db.
}
void
nsMsgMailboxParser::ReleaseFolderLock()
{
nsresult result;
nsCOMPtr<nsIMsgFolder> folder = do_QueryReferent(m_folder);
if (!folder)
return;
bool haveSemaphore;
nsCOMPtr <nsISupports> supports = do_QueryInterface(static_cast<nsIMsgParseMailMsgState*>(this));
result = folder->TestSemaphore(supports, &haveSemaphore);
if (NS_SUCCEEDED(result) && haveSemaphore)
(void) folder->ReleaseSemaphore(supports);
}
nsresult
nsMsgMailboxParser::AcquireFolderLock()
{
nsCOMPtr<nsIMsgFolder> folder = do_QueryReferent(m_folder);
if (!folder)
return NS_ERROR_NULL_POINTER;
nsCOMPtr<nsISupports> supports = do_QueryObject(this);
return folder->AcquireSemaphore(supports);
}
NS_IMPL_ISUPPORTS(nsParseMailMessageState, nsIMsgParseMailMsgState, nsIDBChangeListener)
nsParseMailMessageState::nsParseMailMessageState()
{
m_position = 0;
m_new_key = nsMsgKey_None;
m_IgnoreXMozillaStatus = false;
m_state = nsIMsgParseMailMsgState::ParseBodyState;
// setup handling of custom db headers, headers that are added to .msf files
// as properties of the nsMsgHdr objects, controlled by the
// pref mailnews.customDBHeaders, a space-delimited list of headers.
// E.g., if mailnews.customDBHeaders is "X-Spam-Score", and we're parsing
// a mail message with the X-Spam-Score header, we'll set the
// "x-spam-score" property of nsMsgHdr to the value of the header.
m_customDBHeaderValues = nullptr;
nsCString customDBHeaders; // not shown in search UI
nsCOMPtr<nsIPrefBranch> pPrefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (pPrefBranch)
{
pPrefBranch->GetCharPref("mailnews.customDBHeaders", getter_Copies(customDBHeaders));
ToLowerCase(customDBHeaders);
if (customDBHeaders.Find("content-base") == -1)
customDBHeaders.Insert(NS_LITERAL_CSTRING("content-base "), 0);
ParseString(customDBHeaders, ' ', m_customDBHeaders);
// now add customHeaders
nsCString customHeadersString; // shown in search UI
nsTArray<nsCString> customHeadersArray;
pPrefBranch->GetCharPref("mailnews.customHeaders", getter_Copies(customHeadersString));
ToLowerCase(customHeadersString);
customHeadersString.StripWhitespace();
ParseString(customHeadersString, ':', customHeadersArray);
for (uint32_t i = 0; i < customHeadersArray.Length(); i++)
{
if (!m_customDBHeaders.Contains(customHeadersArray[i]))
m_customDBHeaders.AppendElement(customHeadersArray[i]);
}
if (m_customDBHeaders.Length())
{
m_customDBHeaderValues = new struct message_header [m_customDBHeaders.Length()];
if (!m_customDBHeaderValues)
m_customDBHeaders.Clear();
}
}
Clear();
}
nsParseMailMessageState::~nsParseMailMessageState()
{
ClearAggregateHeader (m_toList);
ClearAggregateHeader (m_ccList);
delete [] m_customDBHeaderValues;
}
void nsParseMailMessageState::Init(uint64_t fileposition)
{
m_state = nsIMsgParseMailMsgState::ParseBodyState;
m_position = fileposition;
m_newMsgHdr = nullptr;
}
NS_IMETHODIMP nsParseMailMessageState::Clear()
{
m_message_id.length = 0;
m_references.length = 0;
m_date.length = 0;
m_delivery_date.length = 0;
m_from.length = 0;
m_sender.length = 0;
m_newsgroups.length = 0;
m_subject.length = 0;
m_status.length = 0;
m_mozstatus.length = 0;
m_mozstatus2.length = 0;
m_envelope_from.length = 0;
m_envelope_date.length = 0;
m_priority.length = 0;
m_keywords.length = 0;
m_mdn_dnt.length = 0;
m_return_path.length = 0;
m_account_key.length = 0;
m_in_reply_to.length = 0;
m_replyTo.length = 0;
m_content_type.length = 0;
m_mdn_original_recipient.length = 0;
m_bccList.length = 0;
m_body_lines = 0;
m_lastLineBlank = 0;
m_newMsgHdr = nullptr;
m_envelope_pos = 0;
m_new_key = nsMsgKey_None;
ClearAggregateHeader (m_toList);
ClearAggregateHeader (m_ccList);
m_headers.ResetWritePos();
m_envelope.ResetWritePos();
m_receivedTime = 0;
m_receivedValue.Truncate();
for (uint32_t i = 0; i < m_customDBHeaders.Length(); i++)
m_customDBHeaderValues[i].length = 0;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::SetState(nsMailboxParseState aState)
{
m_state = aState;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::GetState(nsMailboxParseState *aState)
{
if (!aState)
return NS_ERROR_NULL_POINTER;
*aState = m_state;
return NS_OK;
}
NS_IMETHODIMP
nsParseMailMessageState::GetEnvelopePos(uint64_t *aEnvelopePos)
{
NS_ENSURE_ARG_POINTER(aEnvelopePos);
*aEnvelopePos = m_envelope_pos;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::SetEnvelopePos(uint64_t aEnvelopePos)
{
m_envelope_pos = aEnvelopePos;
m_position = m_envelope_pos;
m_headerstartpos = m_position;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::GetNewMsgHdr(nsIMsgDBHdr ** aMsgHeader)
{
NS_ENSURE_ARG_POINTER(aMsgHeader);
NS_IF_ADDREF(*aMsgHeader = m_newMsgHdr);
return m_newMsgHdr ? NS_OK : NS_ERROR_NULL_POINTER;
}
NS_IMETHODIMP nsParseMailMessageState::SetNewMsgHdr(nsIMsgDBHdr *aMsgHeader)
{
m_newMsgHdr = aMsgHeader;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::ParseAFolderLine(const char *line, uint32_t lineLength)
{
ParseFolderLine(line, lineLength);
return NS_OK;
}
nsresult nsParseMailMessageState::ParseFolderLine(const char *line, uint32_t lineLength)
{
nsresult rv;
if (m_state == nsIMsgParseMailMsgState::ParseHeadersState)
{
if (EMPTY_MESSAGE_LINE(line))
{
/* End of headers. Now parse them. */
rv = ParseHeaders();
NS_ASSERTION(NS_SUCCEEDED(rv), "error parsing headers parsing mailbox");
NS_ENSURE_SUCCESS(rv, rv);
rv = FinalizeHeaders();
NS_ASSERTION(NS_SUCCEEDED(rv), "error finalizing headers parsing mailbox");
NS_ENSURE_SUCCESS(rv, rv);
m_state = nsIMsgParseMailMsgState::ParseBodyState;
}
else
{
/* Otherwise, this line belongs to a header. So append it to the
header data, and stay in MBOX `MIME_PARSE_HEADERS' state.
*/
m_headers.AppendBuffer(line, lineLength);
}
}
else if ( m_state == nsIMsgParseMailMsgState::ParseBodyState)
{
m_body_lines++;
// See comment in msgCore.h for why we use `IS_MSG_LINEBREAK` rather than
// just comparing `line` to `MSG_LINEBREAK`.
m_lastLineBlank = IS_MSG_LINEBREAK(line);
}
m_position += lineLength;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::SetMailDB(nsIMsgDatabase *mailDB)
{
m_mailDB = mailDB;
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::SetBackupMailDB(nsIMsgDatabase *aBackupMailDB)
{
m_backupMailDB = aBackupMailDB;
if (m_backupMailDB)
m_backupMailDB->AddListener(this);
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::SetNewKey(nsMsgKey aKey)
{
m_new_key = aKey;
return NS_OK;
}
/* #define STRICT_ENVELOPE */
bool
nsParseMailMessageState::IsEnvelopeLine(const char *buf, int32_t buf_size)
{
#ifdef STRICT_ENVELOPE
/* The required format is
From jwz Fri Jul 1 09:13:09 1994
But we should also allow at least:
From jwz Fri, Jul 01 09:13:09 1994
From jwz Fri Jul 1 09:13:09 1994 PST
From jwz Fri Jul 1 09:13:09 1994 (+0700)
We can't easily call XP_ParseTimeString() because the string is not
null terminated (ok, we could copy it after a quick check...) but
XP_ParseTimeString() may be too lenient for our purposes.
DANGER!! The released version of 2.0b1 was (on some systems,
some Unix, some NT, possibly others) writing out envelope lines
like "From - 10/13/95 11:22:33" which STRICT_ENVELOPE will reject!
*/
const char *date, *end;
if (buf_size < 29) return false;
if (*buf != 'F') return false;
if (strncmp(buf, "From ", 5)) return false;
end = buf + buf_size;
date = buf + 5;
/* Skip horizontal whitespace between "From " and user name. */
while ((*date == ' ' || *date == '\t') && date < end)
date++;
/* If at the end, it doesn't match. */
if (IS_SPACE(*date) || date == end)
return false;
/* Skip over user name. */
while (!IS_SPACE(*date) && date < end)
date++;
/* Skip horizontal whitespace between user name and date. */
while ((*date == ' ' || *date == '\t') && date < end)
date++;
/* Don't want this to be localized. */
# define TMP_ISALPHA(x) (((x) >= 'A' && (x) <= 'Z') || \
((x) >= 'a' && (x) <= 'z'))
/* take off day-of-the-week. */
if (date >= end - 3)
return false;
if (!TMP_ISALPHA(date[0]) || !TMP_ISALPHA(date[1]) || !TMP_ISALPHA(date[2]))
return false;
date += 3;
/* Skip horizontal whitespace (and commas) between dotw and month. */
if (*date != ' ' && *date != '\t' && *date != ',')
return false;
while ((*date == ' ' || *date == '\t' || *date == ',') && date < end)
date++;
/* take off month. */
if (date >= end - 3)
return false;
if (!TMP_ISALPHA(date[0]) || !TMP_ISALPHA(date[1]) || !TMP_ISALPHA(date[2]))
return false;
date += 3;
/* Skip horizontal whitespace between month and dotm. */
if (date == end || (*date != ' ' && *date != '\t'))
return false;
while ((*date == ' ' || *date == '\t') && date < end)
date++;
/* Skip over digits and whitespace. */
while (((*date >= '0' && *date <= '9') || *date == ' ' || *date == '\t') &&
date < end)
date++;
/* Next character should be a colon. */
if (date >= end || *date != ':')
return false;
/* Ok, that ought to be enough... */
# undef TMP_ISALPHA
#else /* !STRICT_ENVELOPE */
if (buf_size < 5) return false;
if (*buf != 'F') return false;
if (strncmp(buf, "From ", 5)) return false;
#endif /* !STRICT_ENVELOPE */
return true;
}
// We've found the start of the next message, so finish this one off.
NS_IMETHODIMP nsParseMailMessageState::FinishHeader()
{
if (m_newMsgHdr)
{
m_newMsgHdr->SetMessageOffset(m_envelope_pos);
if (m_lastLineBlank)
m_body_lines--;
m_newMsgHdr->SetMessageSize(m_position - m_envelope_pos - m_lastLineBlank);
m_newMsgHdr->SetLineCount(m_body_lines);
}
return NS_OK;
}
NS_IMETHODIMP nsParseMailMessageState::GetAllHeaders(char ** pHeaders, int32_t *pHeadersSize)
{
if (!pHeaders || !pHeadersSize)
return NS_ERROR_NULL_POINTER;
*pHeaders = m_headers.GetBuffer();
*pHeadersSize = m_headers.GetBufferPos();
return NS_OK;
}
// generate headers as a string, with CRLF between the headers
NS_IMETHODIMP nsParseMailMessageState::GetHeaders(char ** pHeaders)
{
NS_ENSURE_ARG_POINTER(pHeaders);
nsCString crlfHeaders;
char *curHeader = m_headers.GetBuffer();
for (uint32_t headerPos = 0; headerPos < m_headers.GetBufferPos();)
{
crlfHeaders.Append(curHeader);
crlfHeaders.Append(CRLF);
int32_t headerLen = strlen(curHeader);
curHeader += headerLen + 1;
headerPos += headerLen + 1;
}
*pHeaders = ToNewCString(crlfHeaders);
return NS_OK;
}
struct message_header *nsParseMailMessageState::GetNextHeaderInAggregate (nsTArray<struct message_header*> &list)
{
// When parsing a message with multiple To or CC header lines, we're storing each line in a
// list, where the list represents the "aggregate" total of all the header. Here we get a new
// line for the list
struct message_header *header = (struct message_header*) PR_Calloc (1, sizeof(struct message_header));
list.AppendElement (header);
return header;
}
void nsParseMailMessageState::GetAggregateHeader (nsTArray<struct message_header*> &list, struct message_header *outHeader)
{
// When parsing a message with multiple To or CC header lines, we're storing each line in a
// list, where the list represents the "aggregate" total of all the header. Here we combine
// all the lines together, as though they were really all found on the same line
struct message_header *header = nullptr;
int length = 0;
size_t i;
// Count up the bytes required to allocate the aggregated header
for (i = 0; i < list.Length(); i++)
{
header = list.ElementAt(i);
length += (header->length + 1); //+ for ","
}
if (length > 0)
{
char *value = (char*) PR_CALLOC (length + 1); //+1 for null term
if (value)
{
// Catenate all the To lines together, separated by commas
value[0] = '\0';
size_t size = list.Length();
for (i = 0; i < size; i++)
{
header = list.ElementAt(i);
PL_strncat (value, header->value, header->length);
if (i + 1 < size)
PL_strcat (value, ",");
}
outHeader->length = length;
outHeader->value = value;
}
}
else
{
outHeader->length = 0;
outHeader->value = nullptr;
}
}
void nsParseMailMessageState::ClearAggregateHeader (nsTArray<struct message_header*> &list)
{
// Reset the aggregate headers. Free only the message_header struct since
// we don't own the value pointer
for (size_t i = 0; i < list.Length(); i++)
PR_Free (list.ElementAt(i));
list.Clear();
}
// We've found a new envelope to parse.
nsresult nsParseMailMessageState::StartNewEnvelope(const char *line, uint32_t lineLength)
{
m_envelope_pos = m_position;
m_state = nsIMsgParseMailMsgState::ParseHeadersState;
m_position += lineLength;
m_headerstartpos = m_position;
return ParseEnvelope (line, lineLength);
}
/* largely lifted from mimehtml.c, which does similar parsing, sigh...
*/
nsresult nsParseMailMessageState::ParseHeaders ()
{
char *buf = m_headers.GetBuffer();
uint32_t buf_length = m_headers.GetBufferPos();
if (buf_length == 0)
{
// No header of an expected type is present. Consider this a successful
// parse so email still shows on summary and can be accessed and deleted.
return NS_OK;
}
char *buf_end = buf + buf_length;
if (!(buf_length > 1 && (buf[buf_length - 1] == '\r' ||
buf[buf_length - 1] == '\n')))
{
NS_WARNING("Header text should always end in a newline");
return NS_ERROR_UNEXPECTED;
}
while (buf < buf_end)
{
char *colon = PL_strnchr(buf, ':', buf_end - buf);
char *end;
char *value = 0;
struct message_header *header = 0;
struct message_header receivedBy;
if (!colon)
break;
end = colon;
switch (buf [0])
{
case 'B': case 'b':
if (!PL_strncasecmp ("BCC", buf, end - buf))
header = &m_bccList;
break;
case 'C': case 'c':
if (!PL_strncasecmp ("CC", buf, end - buf))
header = GetNextHeaderInAggregate(m_ccList);
else if (!PL_strncasecmp ("Content-Type", buf, end - buf))
header = &m_content_type;
break;
case 'D': case 'd':
if (!PL_strncasecmp ("Date", buf, end - buf))
header = &m_date;
else if (!PL_strncasecmp("Disposition-Notification-To", buf, end - buf))
header = &m_mdn_dnt;
else if (!PL_strncasecmp("Delivery-date", buf, end - buf))
header = &m_delivery_date;
break;
case 'F': case 'f':
if (!PL_strncasecmp ("From", buf, end - buf))
header = &m_from;
break;
case 'I' : case 'i':
if (!PL_strncasecmp ("In-Reply-To", buf, end - buf))
header = &m_in_reply_to;
break;
case 'M': case 'm':
if (!PL_strncasecmp ("Message-ID", buf, end - buf))
header = &m_message_id;
break;
case 'N': case 'n':
if (!PL_strncasecmp ("Newsgroups", buf, end - buf))
header = &m_newsgroups;
break;
case 'O': case 'o':
if (!PL_strncasecmp ("Original-Recipient", buf, end - buf))
header = &m_mdn_original_recipient;
break;
case 'R': case 'r':
if (!PL_strncasecmp ("References", buf, end - buf))
header = &m_references;
else if (!PL_strncasecmp ("Return-Path", buf, end - buf))
header = &m_return_path;
// treat conventional Return-Receipt-To as MDN
// Disposition-Notification-To
else if (!PL_strncasecmp ("Return-Receipt-To", buf, end - buf))
header = &m_mdn_dnt;
else if (!PL_strncasecmp("Reply-To", buf, end - buf))
header = &m_replyTo;
else if (!PL_strncasecmp("Received", buf, end - buf))
{
header = &receivedBy;
header->length = 0;
}
break;
case 'S': case 's':
if (!PL_strncasecmp ("Subject", buf, end - buf) && !m_subject.length)
header = &m_subject;
else if (!PL_strncasecmp ("Sender", buf, end - buf))
header = &m_sender;
else if (!PL_strncasecmp ("Status", buf, end - buf))
header = &m_status;
break;
case 'T': case 't':
if (!PL_strncasecmp ("To", buf, end - buf))
header = GetNextHeaderInAggregate(m_toList);
break;
case 'X':
if (X_MOZILLA_STATUS2_LEN == end - buf &&
!PL_strncasecmp(X_MOZILLA_STATUS2, buf, end - buf) &&
!m_IgnoreXMozillaStatus && !m_mozstatus2.length)
header = &m_mozstatus2;
else if ( X_MOZILLA_STATUS_LEN == end - buf &&
!PL_strncasecmp(X_MOZILLA_STATUS, buf, end - buf) && !m_IgnoreXMozillaStatus
&& !m_mozstatus.length)
header = &m_mozstatus;
else if (!PL_strncasecmp(HEADER_X_MOZILLA_ACCOUNT_KEY, buf, end - buf)
&& !m_account_key.length)
header = &m_account_key;
// we could very well care what the priority header was when we
// remember its value. If so, need to remember it here. Also,
// different priority headers can appear in the same message,
// but we only rememeber the last one that we see.
else if (!PL_strncasecmp("X-Priority", buf, end - buf)
|| !PL_strncasecmp("Priority", buf, end - buf))
header = &m_priority;
else if (!PL_strncasecmp(HEADER_X_MOZILLA_KEYWORDS, buf, end - buf)
&& !m_keywords.length)
header = &m_keywords;
break;
}
if (!header && m_customDBHeaders.Length())
{
#ifdef MOZILLA_INTERNAL_API
nsDependentCSubstring headerStr(buf, end);
#else
nsDependentCSubstring headerStr(buf, end - buf);
#endif
ToLowerCase(headerStr);
size_t customHeaderIndex = m_customDBHeaders.IndexOf(headerStr);
if (customHeaderIndex != m_customDBHeaders.NoIndex)
header = & m_customDBHeaderValues[customHeaderIndex];
}
buf = colon + 1;
uint32_t writeOffset = 0; // number of characters replaced with a folded space
SEARCH_NEWLINE:
// move past any non terminating characters, rewriting them if folding white space
// exists
while (buf < buf_end && *buf != '\r' && *buf != '\n')
{
if (writeOffset)
*(buf - writeOffset) = *buf;
buf++;
}
/* If "\r\n " or "\r\n\t" is next, that doesn't terminate the header. */
if ((buf + 2 < buf_end && (buf[0] == '\r' && buf[1] == '\n') &&
(buf[2] == ' ' || buf[2] == '\t')) ||
/* If "\r " or "\r\t" or "\n " or "\n\t" is next, that doesn't terminate
the header either. */
(buf + 1 < buf_end && (buf[0] == '\r' || buf[0] == '\n') &&
(buf[1] == ' ' || buf[1] == '\t')))
{
// locate the proper location for a folded space by eliminating any
// leading spaces before the end-of-line character
char* foldedSpace = buf;
while (*(foldedSpace - 1) == ' ' || *(foldedSpace - 1) == '\t')
foldedSpace--;
// put a single folded space character
*(foldedSpace - writeOffset) = ' ';
writeOffset += (buf - foldedSpace);
buf++;
// eliminate any additional white space
while (buf < buf_end &&
(*buf == '\n' || *buf == '\r' || *buf == ' ' || *buf == '\t'))
{
buf++;
writeOffset++;
}
// If we get here, the message headers ended in an empty line, like:
// To: blah blah blah<CR><LF> <CR><LF>[end of buffer]. The code below
// requires buf to land on a newline to properly null-terminate the
// string, so back up a tad so that it is pointing to one.
if (buf == buf_end)
{
--buf;
MOZ_ASSERT(*buf == '\n' || *buf == '\r',
"Header text should always end in a newline.");
}
goto SEARCH_NEWLINE;
}
if (header)
{
value = colon + 1;
// eliminate trailing blanks after the colon
while (value < (buf - writeOffset) && (*value == ' ' || *value == '\t'))
value++;
header->value = value;
header->length = buf - header->value - writeOffset;
if (header->length < 0)
header->length = 0;
}
if (*buf == '\r' || *buf == '\n')
{
char *last = buf - writeOffset;
char *saveBuf = buf;
if (*buf == '\r' && buf + 1 < buf_end && buf[1] == '\n')
buf++;
buf++;
// null terminate the left-over slop so we don't confuse msg filters.
*saveBuf = 0;
*last = 0; /* short-circuit const, and null-terminate header. */
}
if (header)
{
/* More const short-circuitry... */
/* strip trailing whitespace */
while (header->length > 0 &&
IS_SPACE (header->value [header->length - 1]))
((char *) header->value) [--header->length] = 0;
if (header == &receivedBy)
{
if (m_receivedTime == 0)
{
// parse Received: header for date.
// We trust the first header as that is closest to recipient,
// and less likely to be spoofed.
nsAutoCString receivedHdr(header->value, header->length);
int32_t lastSemicolon = receivedHdr.RFindChar(';');
if (lastSemicolon != -1)
{
nsAutoCString receivedDate;
receivedDate = Substring(receivedHdr, lastSemicolon + 1);
receivedDate.Trim(" \t\b\r\n");
PRTime resultTime;
if (PR_ParseTimeString (receivedDate.get(), false, &resultTime) == PR_SUCCESS)
m_receivedTime = resultTime;
else
NS_WARNING("PR_ParseTimeString failed in ParseHeaders().");
}
}
// Someone might want the received header saved.
if (m_customDBHeaders.Length())
{
if (m_customDBHeaders.Contains(NS_LITERAL_CSTRING("received")))
{
if (!m_receivedValue.IsEmpty())
m_receivedValue.Append(' ');
m_receivedValue.Append(header->value, header->length);
}
}
}
MOZ_ASSERT(header->value[header->length] == 0,
"Non-null-terminated strings cause very, very bad problems");
}
}
return NS_OK;
}
nsresult nsParseMailMessageState::ParseEnvelope (const char *line, uint32_t line_size)
{
const char *end;
char *s;
m_envelope.AppendBuffer(line, line_size);
end = m_envelope.GetBuffer() + line_size;
s = m_envelope.GetBuffer() + 5;
while (s < end && IS_SPACE (*s))
s++;
m_envelope_from.value = s;
while (s < end && !IS_SPACE (*s))
s++;
m_envelope_from.length = s - m_envelope_from.value;
while (s < end && IS_SPACE (*s))
s++;
m_envelope_date.value = s;
m_envelope_date.length = (uint16_t) (line_size - (s - m_envelope.GetBuffer()));
while (m_envelope_date.length > 0 &&
IS_SPACE (m_envelope_date.value [m_envelope_date.length - 1]))
m_envelope_date.length--;
/* #### short-circuit const */
((char *) m_envelope_from.value) [m_envelope_from.length] = 0;
((char *) m_envelope_date.value) [m_envelope_date.length] = 0;
return NS_OK;
}
nsresult nsParseMailMessageState::InternSubject (struct message_header *header)
{
if (!header || header->length == 0)
{
m_newMsgHdr->SetSubject("");
return NS_OK;
}
const char *key = header->value;
uint32_t flags;
(void)m_newMsgHdr->GetFlags(&flags);
/* strip "Re: " */
/**
We trust the X-Mozilla-Status line to be the smartest in almost
all things. One exception, however, is the HAS_RE flag. Since
we just parsed the subject header anyway, we expect that parsing
to be smartest. (After all, what if someone just went in and
edited the subject line by hand?)
*/
nsCString modifiedSubject;
if (NS_MsgStripRE(nsDependentCString(key), modifiedSubject))
flags |= nsMsgMessageFlags::HasRe;
else
flags &= ~nsMsgMessageFlags::HasRe;
m_newMsgHdr->SetFlags(flags); // this *does not* update the mozilla-status header in the local folder
// Condense the subject text into as few MIME-2 encoded words as possible.
m_newMsgHdr->SetSubject(modifiedSubject.IsEmpty() ? key : modifiedSubject.get());
return NS_OK;
}
// we've reached the end of the envelope, and need to turn all our accumulated message_headers
// into a single nsIMsgDBHdr to store in a database.
nsresult nsParseMailMessageState::FinalizeHeaders()
{
nsresult rv;
struct message_header *sender;
struct message_header *recipient;
struct message_header *subject;
struct message_header *id;
struct message_header *inReplyTo;
struct message_header *replyTo;
struct message_header *references;
struct message_header *date;
struct message_header *deliveryDate;
struct message_header *statush;
struct message_header *mozstatus;
struct message_header *mozstatus2;
struct message_header *priority;
struct message_header *keywords;
struct message_header *account_key;
struct message_header *ccList;
struct message_header *bccList;
struct message_header *mdn_dnt;
struct message_header md5_header;
struct message_header *content_type;
char md5_data [50];
uint32_t flags = 0;
uint32_t delta = 0;
nsMsgPriorityValue priorityFlags = nsMsgPriority::notSet;
uint32_t labelFlags = 0;
if (!m_mailDB) // if we don't have a valid db, skip the header.
return NS_OK;
struct message_header to;
GetAggregateHeader (m_toList, &to);
struct message_header cc;
GetAggregateHeader (m_ccList, &cc);
// we don't aggregate bcc, as we only generate it locally,
// and we don't use multiple lines
sender = (m_from.length ? &m_from :
m_sender.length ? &m_sender :
m_envelope_from.length ? &m_envelope_from :
0);
recipient = (to.length ? &to :
cc.length ? &cc :
m_newsgroups.length ? &m_newsgroups :
0);
ccList = (cc.length ? &cc : 0);
bccList = (m_bccList.length ? &m_bccList : 0);
subject = (m_subject.length ? &m_subject : 0);
id = (m_message_id.length ? &m_message_id : 0);
references = (m_references.length ? &m_references : 0);
statush = (m_status.length ? &m_status : 0);
mozstatus = (m_mozstatus.length ? &m_mozstatus : 0);
mozstatus2 = (m_mozstatus2.length ? &m_mozstatus2 : 0);
date = (m_date.length ? &m_date :
m_envelope_date.length ? &m_envelope_date :
0);
deliveryDate = (m_delivery_date.length ? &m_delivery_date : 0);
priority = (m_priority.length ? &m_priority : 0);
keywords = (m_keywords.length ? &m_keywords : 0);
mdn_dnt = (m_mdn_dnt.length ? &m_mdn_dnt : 0);
inReplyTo = (m_in_reply_to.length ? &m_in_reply_to : 0);
replyTo = (m_replyTo.length ? &m_replyTo : 0);
content_type = (m_content_type.length ? &m_content_type : 0);
account_key = (m_account_key.length ? &m_account_key :0);
if (mozstatus)
{
if (mozstatus->length == 4)
{
NS_ASSERTION(MsgIsHex(mozstatus->value, 4), "Expected 4 hex digits for flags.");
flags = MsgUnhex(mozstatus->value, 4);
// strip off and remember priority bits.
flags &= ~nsMsgMessageFlags::RuntimeOnly;
priorityFlags = (nsMsgPriorityValue) ((flags & nsMsgMessageFlags::Priorities) >> 13);
flags &= ~nsMsgMessageFlags::Priorities;
}
delta = (m_headerstartpos +
(mozstatus->value - m_headers.GetBuffer()) -
(2 + X_MOZILLA_STATUS_LEN) /* 2 extra bytes for ": ". */
) - m_envelope_pos;
}
if (mozstatus2)
{
uint32_t flags2 = 0;
sscanf(mozstatus2->value, " %x ", &flags2);
flags |= flags2;
}
if (!(flags & nsMsgMessageFlags::Expunged)) // message was deleted, don't bother creating a hdr.
{
// We'll need the message id first to recover data from the backup database
nsAutoCString rawMsgId;
/* Take off <> around message ID. */
if (id)
{
if (id->length > 0 && id->value[0] == '<')
id->length--, id->value++;
NS_WARNING_ASSERTION(id->length > 0, "id->length failure in FinalizeHeaders().");
if (id->length > 0 && id->value[id->length - 1] == '>')
/* generate a new null-terminated string without the final > */
rawMsgId.Assign(id->value, id->length - 1);
else
rawMsgId.Assign(id->value);
}
/*
* Try to copy the data from the backup database, referencing the MessageID
* If that fails, just create a new header
*/
nsCOMPtr<nsIMsgDBHdr> oldHeader;
nsresult ret = NS_OK;
if (m_backupMailDB && !rawMsgId.IsEmpty())
ret = m_backupMailDB->GetMsgHdrForMessageID(
rawMsgId.get(), getter_AddRefs(oldHeader));
// m_new_key is set in nsImapMailFolder::ParseAdoptedHeaderLine to be
// the UID of the message, so that the key can get created as UID. That of
// course is extremely confusing, and we really need to clean that up. We
// really should not conflate the meaning of envelope position, key, and
// UID.
if (NS_SUCCEEDED(ret) && oldHeader)
ret = m_mailDB->CopyHdrFromExistingHdr(m_new_key,
oldHeader, false, getter_AddRefs(m_newMsgHdr));
else if (!m_newMsgHdr)
{
// Should assert that this is not a local message
ret = m_mailDB->CreateNewHdr(m_new_key, getter_AddRefs(m_newMsgHdr));
}
if (NS_SUCCEEDED(ret) && m_newMsgHdr)
{
uint32_t origFlags;
(void)m_newMsgHdr->GetFlags(&origFlags);
if (origFlags & nsMsgMessageFlags::HasRe)
flags |= nsMsgMessageFlags::HasRe;
else
flags &= ~nsMsgMessageFlags::HasRe;
flags &= ~nsMsgMessageFlags::Offline; // don't keep nsMsgMessageFlags::Offline for local msgs
if (mdn_dnt && !(origFlags & nsMsgMessageFlags::Read) &&
!(origFlags & nsMsgMessageFlags::MDNReportSent) &&
!(flags & nsMsgMessageFlags::MDNReportSent))
flags |= nsMsgMessageFlags::MDNReportNeeded;
m_newMsgHdr->SetFlags(flags);
if (priorityFlags != nsMsgPriority::notSet)
m_newMsgHdr->SetPriority(priorityFlags);
// if we have a reply to header, and it's different from the from: header,
// set the "replyTo" attribute on the msg hdr.
if (replyTo && (!sender || replyTo->length != sender->length || strncmp(replyTo->value, sender->value, sender->length)))
m_newMsgHdr->SetStringProperty("replyTo", replyTo->value);
// convert the flag values (0xE000000) to label values (0-5)
if (mozstatus2) // only do this if we have a mozstatus2 header
{
labelFlags = ((flags & nsMsgMessageFlags::Labels) >> 25);
m_newMsgHdr->SetLabel(labelFlags);
}
if (delta < 0xffff)
{ /* Only use if fits in 16 bits. */
m_newMsgHdr->SetStatusOffset((uint16_t) delta);
if (!m_IgnoreXMozillaStatus) { // imap doesn't care about X-MozillaStatus
uint32_t offset;
(void)m_newMsgHdr->GetStatusOffset(&offset);
NS_ASSERTION(offset < 10000, "invalid status offset"); /* ### Debugging hack */
}
}
if (sender)
m_newMsgHdr->SetAuthor(sender->value);
if (recipient == &m_newsgroups)
{
/* In the case where the recipient is a newsgroup, truncate the string
at the first comma. This is used only for presenting the thread list,
and newsgroup lines tend to be long and non-shared, and tend to bloat
the string table. So, by only showing the first newsgroup, we can
reduce memory and file usage at the expense of only showing the one
group in the summary list, and only being able to sort on the first
group rather than the whole list. It's worth it. */
char * ch;
ch = PL_strchr(recipient->value, ',');
if (ch)
{
/* generate a new string that terminates before the , */
nsAutoCString firstGroup;
firstGroup.Assign(recipient->value, ch - recipient->value);
m_newMsgHdr->SetRecipients(firstGroup.get());
}
m_newMsgHdr->SetRecipients(recipient->value);
}
else if (recipient)
{
m_newMsgHdr->SetRecipients(recipient->value);
}
if (ccList)
{
m_newMsgHdr->SetCcList(ccList->value);
}
if (bccList)
{
m_newMsgHdr->SetBccList(bccList->value);
}
rv = InternSubject (subject);
if (NS_SUCCEEDED(rv))
{
if (!id)
{
// what to do about this? we used to do a hash of all the headers...
nsAutoCString hash;
const char *md5_b64 = "dummy.message.id";
nsresult rv;
nsCOMPtr<nsICryptoHash> hasher = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
if (NS_SUCCEEDED(rv))
{
if (NS_SUCCEEDED(hasher->Init(nsICryptoHash::MD5)) &&
NS_SUCCEEDED(hasher->Update((const uint8_t*) m_headers.GetBuffer(), m_headers.GetSize())) &&
NS_SUCCEEDED(hasher->Finish(true, hash)))
md5_b64 = hash.get();
}
PR_snprintf (md5_data, sizeof(md5_data), "<md5:%s>", md5_b64);
md5_header.value = md5_data;
md5_header.length = strlen(md5_data);
id = &md5_header;
}
if (!rawMsgId.IsEmpty())
m_newMsgHdr->SetMessageId(rawMsgId.get());
else
m_newMsgHdr->SetMessageId(id->value);
m_mailDB->UpdatePendingAttributes(m_newMsgHdr);
if (!mozstatus && statush)
{
/* Parse a little bit of the Berkeley Mail status header. */
for (const char *s = statush->value; *s; s++) {
uint32_t msgFlags = 0;
(void)m_newMsgHdr->GetFlags(&msgFlags);
switch (*s)
{
case 'R': case 'r':
m_newMsgHdr->SetFlags(msgFlags | nsMsgMessageFlags::Read);
break;
case 'D': case 'd':
/* msg->flags |= nsMsgMessageFlags::Expunged; ### Is this reasonable? */
break;
case 'N': case 'n':
case 'U': case 'u':
m_newMsgHdr->SetFlags(msgFlags & ~nsMsgMessageFlags::Read);
break;
default: // Should check for corrupt file.
NS_ERROR("Corrupt file. Should not happen.");
break;
}
}
}
if (account_key != nullptr)
m_newMsgHdr->SetAccountKey(account_key->value);
// use in-reply-to header as references, if there's no references header
if (references != nullptr)
m_newMsgHdr->SetReferences(references->value);
else if (inReplyTo != nullptr)
m_newMsgHdr->SetReferences(inReplyTo->value);
// 'Received' should be as reliable an indicator of the receipt
// date+time as possible, whilst always giving something *from
// the message*. It won't use PR_Now() under any circumstance.
// Therefore, the fall-thru order for 'Received' is:
// Received: -> Delivery-date: -> date
// 'Date' uses:
// date -> PR_Now()
//
// date is:
// Date: -> m_envelope_date
uint32_t rcvTimeSecs = 0;
if (date)
{ // Date:
PRTime resultTime;
PRStatus timeStatus = PR_ParseTimeString (date->value, false, &resultTime);
if (PR_SUCCESS == timeStatus)
{
m_newMsgHdr->SetDate(resultTime);
PRTime2Seconds(resultTime, &rcvTimeSecs);
}
else
NS_WARNING("PR_ParseTimeString of date failed in FinalizeHeader().");
}
else
{ // PR_Now()
// If there was some problem parsing the Date header *AND* we
// couldn't get a valid envelope date, use now as the time.
// PR_ParseTimeString won't touch resultTime unless it succeeds.
// This doesn't affect local (POP3) messages, because we use the envelope
// date if there's no Date: header, but it will affect IMAP msgs
// w/o a Date: hdr or Received: headers.
PRTime resultTime = PR_Now();
m_newMsgHdr->SetDate(resultTime);
}
if (m_receivedTime != 0)
{ // Upgrade 'Received' to Received: ?
PRTime2Seconds(m_receivedTime, &rcvTimeSecs);
}
else if (deliveryDate)
{ // Upgrade 'Received' to Delivery-date: ?
PRTime resultTime;
PRStatus timeStatus = PR_ParseTimeString (deliveryDate->value, false, &resultTime);
if (PR_SUCCESS == timeStatus)
PRTime2Seconds(resultTime, &rcvTimeSecs);
else // TODO/FIXME: We need to figure out what to do in this case!
NS_WARNING("PR_ParseTimeString of delivery date failed in FinalizeHeader().");
}
m_newMsgHdr->SetUint32Property("dateReceived", rcvTimeSecs);
if (priority)
m_newMsgHdr->SetPriorityString(priority->value);
else if (priorityFlags == nsMsgPriority::notSet)
m_newMsgHdr->SetPriority(nsMsgPriority::none);
if (keywords)
{
// When there are many keywords, some may not have been written
// to the message file, so add extra keywords from the backup
nsAutoCString oldKeywords;
m_newMsgHdr->GetStringProperty("keywords", getter_Copies(oldKeywords));
nsTArray<nsCString> newKeywordArray, oldKeywordArray;
ParseString(Substring(keywords->value, keywords->value + keywords->length), ' ', newKeywordArray);
ParseString(oldKeywords, ' ', oldKeywordArray);
for (uint32_t i = 0; i < oldKeywordArray.Length(); i++)
if (!newKeywordArray.Contains(oldKeywordArray[i]))
newKeywordArray.AppendElement(oldKeywordArray[i]);
nsAutoCString newKeywords;
for (uint32_t i = 0; i < newKeywordArray.Length(); i++)
{
if (i)
newKeywords.Append(" ");
newKeywords.Append(newKeywordArray[i]);
}
m_newMsgHdr->SetStringProperty("keywords", newKeywords.get());
}
for (uint32_t i = 0; i < m_customDBHeaders.Length(); i++)
{
if (m_customDBHeaderValues[i].length)
m_newMsgHdr->SetStringProperty(m_customDBHeaders[i].get(), m_customDBHeaderValues[i].value);
// The received header is accumulated separately
if (m_customDBHeaders[i].EqualsLiteral("received") && !m_receivedValue.IsEmpty())
m_newMsgHdr->SetStringProperty("received", m_receivedValue.get());
}
if (content_type)
{
char *substring = PL_strstr(content_type->value, "charset");
if (substring)
{
char *charset = PL_strchr (substring, '=');
if (charset)
{
charset++;
/* strip leading whitespace and double-quote */
while (*charset && (IS_SPACE (*charset) || '\"' == *charset))
charset++;
/* strip trailing whitespace and double-quote */
char *end = charset;
while (*end && !IS_SPACE (*end) && '\"' != *end && ';' != *end)
end++;
if (*charset)
{
if (*end != '\0') {
// if we're not at the very end of the line, we need
// to generate a new string without the trailing crud
nsAutoCString rawCharSet;
rawCharSet.Assign(charset, end - charset);
m_newMsgHdr->SetCharset(rawCharSet.get());
} else {
m_newMsgHdr->SetCharset(charset);
}
}
}
}
substring = PL_strcasestr(content_type->value, "multipart/mixed");
if (substring)
{
uint32_t newFlags;
m_newMsgHdr->OrFlags(nsMsgMessageFlags::Attachment, &newFlags);
}
}
}
}
else
{
NS_ASSERTION(false, "error creating message header");
rv = NS_ERROR_OUT_OF_MEMORY;
}
}
else
rv = NS_OK;
//### why is this stuff const?
char *tmp = (char*) to.value;
PR_Free(tmp);
tmp = (char*) cc.value;
PR_Free(tmp);
return rv;
}
nsParseNewMailState::nsParseNewMailState()
: m_disableFilters(false)
{
m_ibuffer = nullptr;
m_ibuffer_size = 0;
m_ibuffer_fp = 0;
m_numNotNewMessages = 0;
}
NS_IMPL_ISUPPORTS_INHERITED(nsParseNewMailState, nsMsgMailboxParser, nsIMsgFilterHitNotify)
nsresult
nsParseNewMailState::Init(nsIMsgFolder *serverFolder, nsIMsgFolder *downloadFolder,
nsIMsgWindow *aMsgWindow, nsIMsgDBHdr *aHdr,
nsIOutputStream *aOutputStream)
{
nsresult rv;
Clear();
m_rootFolder = serverFolder;
m_msgWindow = aMsgWindow;
m_downloadFolder = downloadFolder;
m_newMsgHdr = aHdr;
m_outputStream = aOutputStream;
// the new mail parser isn't going to get the stream input, it seems, so we can't use
// the OnStartRequest mechanism the mailbox parser uses. So, let's open the db right now.
nsCOMPtr<nsIMsgDBService> msgDBService = do_GetService(NS_MSGDB_SERVICE_CONTRACTID, &rv);
if (msgDBService && !m_mailDB)
rv = msgDBService->OpenFolderDB(downloadFolder, false,
getter_AddRefs(m_mailDB));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr <nsIMsgFolder> rootMsgFolder = do_QueryInterface(serverFolder, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgIncomingServer> server;
rv = rootMsgFolder->GetServer(getter_AddRefs(server));
if (NS_SUCCEEDED(rv))
{
rv = server->GetFilterList(aMsgWindow, getter_AddRefs(m_filterList));
if (m_filterList)
rv = server->ConfigureTemporaryFilters(m_filterList);
// check if this server defers to another server, in which case
// we'll use that server's filters as well.
nsCOMPtr <nsIMsgFolder> deferredToRootFolder;
server->GetRootMsgFolder(getter_AddRefs(deferredToRootFolder));
if (rootMsgFolder != deferredToRootFolder)
{
nsCOMPtr <nsIMsgIncomingServer> deferredToServer;
deferredToRootFolder->GetServer(getter_AddRefs(deferredToServer));
if (deferredToServer)
deferredToServer->GetFilterList(aMsgWindow, getter_AddRefs(m_deferredToServerFilterList));
}
}
m_disableFilters = false;
return NS_OK;
}
nsParseNewMailState::~nsParseNewMailState()
{
if (m_mailDB)
m_mailDB->Close(true);
if (m_backupMailDB)
m_backupMailDB->ForceClosed();
#ifdef DOING_JSFILTERS
JSFilter_cleanup();
#endif
}
// not an IMETHOD so we don't need to do error checking or return an error.
// We only have one caller.
void nsParseNewMailState::GetMsgWindow(nsIMsgWindow **aMsgWindow)
{
NS_IF_ADDREF(*aMsgWindow = m_msgWindow);
}
// This gets called for every message because libnet calls IncorporateBegin,
// IncorporateWrite (once or more), and IncorporateComplete for every message.
void nsParseNewMailState::DoneParsingFolder(nsresult status)
{
/* End of file. Flush out any partial line remaining in the buffer. */
if (m_ibuffer_fp > 0)
{
ParseFolderLine(m_ibuffer, m_ibuffer_fp);
m_ibuffer_fp = 0;
}
PublishMsgHeader(nullptr);
if (m_mailDB) // finished parsing, so flush db folder info
UpdateDBFolderInfo();
/* We're done reading the folder - we don't need these things
any more. */
PR_FREEIF (m_ibuffer);
m_ibuffer_size = 0;
PR_FREEIF (m_obuffer);
m_obuffer_size = 0;
}
void nsParseNewMailState::OnNewMessage(nsIMsgWindow *msgWindow)
{
}
int32_t nsParseNewMailState::PublishMsgHeader(nsIMsgWindow *msgWindow)
{
bool moved = false;
FinishHeader();
if (m_newMsgHdr)
{
uint32_t newFlags, oldFlags;
m_newMsgHdr->GetFlags(&oldFlags);
if (!(oldFlags & nsMsgMessageFlags::Read)) // don't mark read messages as new.
m_newMsgHdr->OrFlags(nsMsgMessageFlags::New, &newFlags);
if (!m_disableFilters)
{
uint64_t msgOffset;
(void) m_newMsgHdr->GetMessageOffset(&msgOffset);
m_curHdrOffset = msgOffset;
nsCOMPtr<nsIMsgIncomingServer> server;
nsresult rv = m_rootFolder->GetServer(getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv, 0);
int32_t duplicateAction;
server->GetIncomingDuplicateAction(&duplicateAction);
if (duplicateAction != nsIMsgIncomingServer::keepDups)
{
bool isDup;
server->IsNewHdrDuplicate(m_newMsgHdr, &isDup);
if (isDup)
{
// we want to do something similar to applying filter hits.
// if a dup is marked read, it shouldn't trigger biff.
// Same for deleting it or moving it to trash.
switch (duplicateAction)
{
case nsIMsgIncomingServer::deleteDups:
{
nsCOMPtr<nsIMsgPluggableStore> msgStore;
nsresult rv =
m_downloadFolder->GetMsgStore(getter_AddRefs(msgStore));
if (NS_SUCCEEDED(rv))
{
rv = msgStore->DiscardNewMessage(m_outputStream, m_newMsgHdr);
if (NS_FAILED(rv))
m_rootFolder->ThrowAlertMsg("dupDeleteFolderTruncateFailed", msgWindow);
}
m_mailDB->RemoveHeaderMdbRow(m_newMsgHdr);
}
break;
case nsIMsgIncomingServer::moveDupsToTrash:
{
nsCOMPtr <nsIMsgFolder> trash;
GetTrashFolder(getter_AddRefs(trash));
if (trash) {
uint32_t newFlags;
bool msgMoved;
m_newMsgHdr->AndFlags(~nsMsgMessageFlags::New, &newFlags);
nsCOMPtr<nsIMsgPluggableStore> msgStore;
rv = m_downloadFolder->GetMsgStore(getter_AddRefs(msgStore));
if (NS_SUCCEEDED(rv))
rv = msgStore->MoveNewlyDownloadedMessage(m_newMsgHdr, trash, &msgMoved);
if (NS_SUCCEEDED(rv) && !msgMoved) {
rv = MoveIncorporatedMessage(m_newMsgHdr, m_mailDB, trash,
nullptr, msgWindow);
if (NS_SUCCEEDED(rv))
rv = m_mailDB->RemoveHeaderMdbRow(m_newMsgHdr);
}
if (NS_FAILED(rv))
NS_WARNING("moveDupsToTrash failed for some reason.");
}
}
break;
case nsIMsgIncomingServer::markDupsRead:
MarkFilteredMessageRead(m_newMsgHdr);
break;
}
int32_t numNewMessages;
m_downloadFolder->GetNumNewMessages(false, &numNewMessages);
m_downloadFolder->SetNumNewMessages(numNewMessages - 1);
m_newMsgHdr = nullptr;
return 0;
}
}
ApplyFilters(&moved, msgWindow, msgOffset);
}
if (!moved)
{
if (m_mailDB)
{
m_mailDB->AddNewHdrToDB(m_newMsgHdr, true);
nsCOMPtr<nsIMsgFolderNotificationService> notifier(do_GetService(NS_MSGNOTIFICATIONSERVICE_CONTRACTID));
if (notifier)
notifier->NotifyMsgAdded(m_newMsgHdr);
// mark the header as not yet reported classified
nsMsgKey msgKey;
m_newMsgHdr->GetMessageKey(&msgKey);
m_downloadFolder->OrProcessingFlags(
msgKey, nsMsgProcessingFlags::NotReportedClassified);
}
} // if it was moved by imap filter, m_parseMsgState->m_newMsgHdr == nullptr
m_newMsgHdr = nullptr;
}
return 0;
}
// We've found the start of the next message, so finish this one off.
NS_IMETHODIMP nsParseNewMailState::FinishHeader()
{
if (m_newMsgHdr)
{
if (m_lastLineBlank)
m_body_lines--;
m_newMsgHdr->SetMessageSize(m_position - m_envelope_pos - m_lastLineBlank);
m_newMsgHdr->SetLineCount(m_body_lines);
}
return NS_OK;
}
nsresult nsParseNewMailState::GetTrashFolder(nsIMsgFolder **pTrashFolder)
{
nsresult rv=NS_ERROR_UNEXPECTED;
if (!pTrashFolder)
return NS_ERROR_NULL_POINTER;
if (m_downloadFolder)
{
nsCOMPtr <nsIMsgIncomingServer> incomingServer;
m_downloadFolder->GetServer(getter_AddRefs(incomingServer));
nsCOMPtr <nsIMsgFolder> rootMsgFolder;
incomingServer->GetRootMsgFolder(getter_AddRefs(rootMsgFolder));
if (rootMsgFolder)
{
rv = rootMsgFolder->GetFolderWithFlags(nsMsgFolderFlags::Trash, pTrashFolder);
if (!*pTrashFolder)
rv = NS_ERROR_FAILURE;
}
}
return rv;
}
void nsParseNewMailState::ApplyFilters(bool *pMoved, nsIMsgWindow *msgWindow, uint64_t msgOffset)
{
m_msgMovedByFilter = m_msgCopiedByFilter = false;
m_curHdrOffset = msgOffset;
if (!m_disableFilters)
{
nsCOMPtr<nsIMsgDBHdr> msgHdr = m_newMsgHdr;
nsCOMPtr<nsIMsgFolder> downloadFolder = m_downloadFolder;
nsCOMPtr <nsIMsgFolder> rootMsgFolder = do_QueryInterface(m_rootFolder);
if (rootMsgFolder)
{
if (!downloadFolder)
rootMsgFolder->GetFolderWithFlags(nsMsgFolderFlags::Inbox,
getter_AddRefs(downloadFolder));
if (downloadFolder)
downloadFolder->GetURI(m_inboxUri);
char * headers = m_headers.GetBuffer();
uint32_t headersSize = m_headers.GetBufferPos();
if (m_filterList)
(void) m_filterList->
ApplyFiltersToHdr(nsMsgFilterType::InboxRule, msgHdr, downloadFolder,
m_mailDB, headers, headersSize, this, msgWindow);
if (!m_msgMovedByFilter && m_deferredToServerFilterList)
{
(void) m_deferredToServerFilterList->
ApplyFiltersToHdr(nsMsgFilterType::InboxRule, msgHdr, downloadFolder,
m_mailDB, headers, headersSize, this, msgWindow);
}
}
}
if (pMoved)
*pMoved = m_msgMovedByFilter;
}
NS_IMETHODIMP nsParseNewMailState::ApplyFilterHit(nsIMsgFilter *filter, nsIMsgWindow *msgWindow, bool *applyMore)
{
NS_ENSURE_ARG_POINTER(filter);
NS_ENSURE_ARG_POINTER(applyMore);
uint32_t newFlags;
nsresult rv = NS_OK;
*applyMore = true;
nsCOMPtr<nsIMsgDBHdr> msgHdr = m_newMsgHdr;
nsCOMPtr<nsIArray> filterActionList;
rv = filter->GetSortedActionList(getter_AddRefs(filterActionList));
NS_ENSURE_SUCCESS(rv, rv);
uint32_t numActions;
rv = filterActionList->GetLength(&numActions);
NS_ENSURE_SUCCESS(rv, rv);
bool loggingEnabled = false;
if (m_filterList && numActions)
m_filterList->GetLoggingEnabled(&loggingEnabled);
bool msgIsNew = true;
for (uint32_t actionIndex = 0; actionIndex < numActions && *applyMore; actionIndex++)
{
nsCOMPtr<nsIMsgRuleAction> filterAction;
rv = filterActionList->QueryElementAt(actionIndex, NS_GET_IID(nsIMsgRuleAction),
getter_AddRefs(filterAction));
if (NS_FAILED(rv) || !filterAction)
continue;
nsMsgRuleActionType actionType;
if (NS_SUCCEEDED(filterAction->GetType(&actionType)))
{
if (loggingEnabled)
(void)filter->LogRuleHit(filterAction, msgHdr);
nsCString actionTargetFolderUri;
if (actionType == nsMsgFilterAction::MoveToFolder ||
actionType == nsMsgFilterAction::CopyToFolder)
{
rv = filterAction->GetTargetFolderUri(actionTargetFolderUri);
if (NS_FAILED(rv) || actionTargetFolderUri.IsEmpty())
{
NS_ASSERTION(false, "actionTargetFolderUri is empty");
continue;
}
}
switch (actionType)
{
case nsMsgFilterAction::Delete:
{
nsCOMPtr <nsIMsgFolder> trash;
// set value to trash folder
rv = GetTrashFolder(getter_AddRefs(trash));
if (NS_SUCCEEDED(rv) && trash)
rv = trash->GetURI(actionTargetFolderUri);
msgHdr->OrFlags(nsMsgMessageFlags::Read, &newFlags); // mark read in trash.
msgIsNew = false;
}
// FALLTHROUGH
MOZ_FALLTHROUGH;
case nsMsgFilterAction::MoveToFolder:
// if moving to a different file, do it.
if (actionTargetFolderUri.get() && !m_inboxUri.Equals(actionTargetFolderUri,
nsCaseInsensitiveCStringComparator()))
{
nsresult err;
nsCOMPtr<nsIRDFService> rdf(do_GetService(kRDFServiceCID, &err));
NS_ENSURE_SUCCESS(err, err);
nsCOMPtr<nsIRDFResource> res;
err = rdf->GetResource(actionTargetFolderUri, getter_AddRefs(res));
if (NS_FAILED(err))
return err;
nsCOMPtr<nsIMsgFolder> destIFolder(do_QueryInterface(res, &err));
if (NS_FAILED(err))
return err;
bool msgMoved = false;
// If we're moving to an imap folder, or this message has already
// has a pending copy action, use the imap coalescer so that
// we won't truncate the inbox before the copy fires.
if (m_msgCopiedByFilter ||
StringBeginsWith(actionTargetFolderUri, NS_LITERAL_CSTRING("imap:")))
{
if (!m_moveCoalescer)
m_moveCoalescer = new nsImapMoveCoalescer(m_downloadFolder, m_msgWindow);
NS_ENSURE_TRUE(m_moveCoalescer, NS_ERROR_OUT_OF_MEMORY);
nsMsgKey msgKey;
(void) msgHdr->GetMessageKey(&msgKey);
m_moveCoalescer->AddMove(destIFolder, msgKey);
err = NS_OK;
msgIsNew = false;
}
else
{
nsCOMPtr<nsIMsgPluggableStore> msgStore;
err = m_downloadFolder->GetMsgStore(getter_AddRefs(msgStore));
if (NS_SUCCEEDED(err))
err = msgStore->MoveNewlyDownloadedMessage(msgHdr, destIFolder, &msgMoved);
if (NS_SUCCEEDED(err) && !msgMoved)
err = MoveIncorporatedMessage(msgHdr, m_mailDB, destIFolder,
filter, msgWindow);
m_msgMovedByFilter = NS_SUCCEEDED(err);
if (!m_msgMovedByFilter /* == NS_FAILED(err) */)
{
// XXX: Invoke MSG_LOG_TO_CONSOLE once bug 1135265 lands.
if (loggingEnabled)
(void) filter->LogRuleHitFail(filterAction, msgHdr, err, "Move failed");
}
}
}
*applyMore = false;
break;
case nsMsgFilterAction::CopyToFolder:
{
nsCString uri;
rv = m_rootFolder->GetURI(uri);
if (!actionTargetFolderUri.IsEmpty() && !actionTargetFolderUri.Equals(uri))
{
nsCOMPtr<nsIMutableArray> messageArray(do_CreateInstance(NS_ARRAY_CONTRACTID));
messageArray->AppendElement(msgHdr, false);
nsCOMPtr<nsIMsgFolder> dstFolder;
nsCOMPtr<nsIMsgCopyService> copyService;
rv = GetExistingFolder(actionTargetFolderUri,
getter_AddRefs(dstFolder));
if (NS_SUCCEEDED(rv)) {
copyService = do_GetService(NS_MSGCOPYSERVICE_CONTRACTID, &rv);
}
else {
// Let's show a more specific warning.
NS_WARNING("Target Folder does not exist.");
return rv;
}
if (NS_SUCCEEDED(rv))
rv = copyService->CopyMessages(m_downloadFolder, messageArray, dstFolder,
false, nullptr, msgWindow, false);
if (NS_FAILED(rv)) {
// XXX: Invoke MSG_LOG_TO_CONSOLE once bug 1135265 lands.
if (loggingEnabled)
(void) filter->LogRuleHitFail(filterAction, msgHdr, rv, "Copy failed");
}
else
m_msgCopiedByFilter = true;
}
}
break;
case nsMsgFilterAction::MarkRead:
msgIsNew = false;
MarkFilteredMessageRead(msgHdr);
break;
case nsMsgFilterAction::MarkUnread:
msgIsNew = true;
MarkFilteredMessageUnread(msgHdr);
break;
case nsMsgFilterAction::KillThread:
msgHdr->SetUint32Property("ProtoThreadFlags", nsMsgMessageFlags::Ignored);
break;
case nsMsgFilterAction::KillSubthread:
msgHdr->OrFlags(nsMsgMessageFlags::Ignored, &newFlags);
break;
case nsMsgFilterAction::WatchThread:
msgHdr->OrFlags(nsMsgMessageFlags::Watched, &newFlags);
break;
case nsMsgFilterAction::MarkFlagged:
{
nsCOMPtr<nsIMutableArray> messageArray(do_CreateInstance(NS_ARRAY_CONTRACTID));
messageArray->AppendElement(msgHdr, false);
m_downloadFolder->MarkMessagesFlagged(messageArray, true);
}
break;
case nsMsgFilterAction::ChangePriority:
nsMsgPriorityValue filterPriority;
filterAction->GetPriority(&filterPriority);
msgHdr->SetPriority(filterPriority);
break;
case nsMsgFilterAction::AddTag:
{
nsCString keyword;
filterAction->GetStrValue(keyword);
nsCOMPtr<nsIMutableArray> messageArray(do_CreateInstance(NS_ARRAY_CONTRACTID));
messageArray->AppendElement(msgHdr, false);
m_downloadFolder->AddKeywordsToMessages(messageArray, keyword);
break;
}
case nsMsgFilterAction::Label:
nsMsgLabelValue filterLabel;
filterAction->GetLabel(&filterLabel);
nsMsgKey msgKey;
msgHdr->GetMessageKey(&msgKey);
m_mailDB->SetLabel(msgKey, filterLabel);
break;
case nsMsgFilterAction::JunkScore:
{
nsAutoCString junkScoreStr;
int32_t junkScore;
filterAction->GetJunkScore(&junkScore);
junkScoreStr.AppendInt(junkScore);
if (junkScore == nsIJunkMailPlugin::IS_SPAM_SCORE)
msgIsNew = false;
nsMsgKey msgKey;
msgHdr->GetMessageKey(&msgKey);
msgHdr->SetStringProperty("junkscore", junkScoreStr.get());
msgHdr->SetStringProperty("junkscoreorigin", "filter");
break;
}
case nsMsgFilterAction::Forward:
{
nsCString forwardTo;
filterAction->GetStrValue(forwardTo);
m_forwardTo.AppendElement(forwardTo);
m_msgToForwardOrReply = msgHdr;
}
break;
case nsMsgFilterAction::Reply:
{
nsCString replyTemplateUri;
filterAction->GetStrValue(replyTemplateUri);
m_replyTemplateUri.AppendElement(replyTemplateUri);
m_msgToForwardOrReply = msgHdr;
m_ruleAction = filterAction;
m_filter = filter;
}
break;
case nsMsgFilterAction::DeleteFromPop3Server:
{
uint32_t flags = 0;
nsCOMPtr <nsIMsgFolder> downloadFolder;
msgHdr->GetFolder(getter_AddRefs(downloadFolder));
nsCOMPtr <nsIMsgLocalMailFolder> localFolder = do_QueryInterface(downloadFolder);
msgHdr->GetFlags(&flags);
if (localFolder)
{
nsCOMPtr<nsIMutableArray> messages = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
messages->AppendElement(msgHdr, false);
// This action ignores the deleteMailLeftOnServer preference
localFolder->MarkMsgsOnPop3Server(messages, POP3_FORCE_DEL);
// If this is just a header, throw it away. It's useless now
// that the server copy is being deleted.
if (flags & nsMsgMessageFlags::Partial)
{
m_msgMovedByFilter = true;
msgIsNew = false;
}
}
}
break;
case nsMsgFilterAction::FetchBodyFromPop3Server:
{
uint32_t flags = 0;
nsCOMPtr <nsIMsgFolder> downloadFolder;
msgHdr->GetFolder(getter_AddRefs(downloadFolder));
nsCOMPtr <nsIMsgLocalMailFolder> localFolder = do_QueryInterface(downloadFolder);
msgHdr->GetFlags(&flags);
if (localFolder && (flags & nsMsgMessageFlags::Partial))
{
nsCOMPtr<nsIMutableArray> messages = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
messages->AppendElement(msgHdr, false);
localFolder->MarkMsgsOnPop3Server(messages, POP3_FETCH_BODY);
// Don't add this header to the DB, we're going to replace it
// with the full message.
m_msgMovedByFilter = true;
msgIsNew = false;
// Don't do anything else in this filter, wait until we
// have the full message.
*applyMore = false;
}
}
break;
case nsMsgFilterAction::StopExecution:
{
// don't apply any more filters
*applyMore = false;
}
break;
case nsMsgFilterAction::Custom:
{
nsCOMPtr<nsIMsgFilterCustomAction> customAction;
rv = filterAction->GetCustomAction(getter_AddRefs(customAction));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString value;
filterAction->GetStrValue(value);
nsCOMPtr<nsIMutableArray> messageArray(
do_CreateInstance(NS_ARRAY_CONTRACTID, &rv));
NS_ENSURE_TRUE(messageArray, rv);
if (NS_SUCCEEDED(rv))
rv = messageArray->AppendElement(msgHdr, false);
if (NS_SUCCEEDED(rv))
rv = customAction->Apply(messageArray, value, nullptr,
nsMsgFilterType::InboxRule, msgWindow);
if (NS_FAILED(rv)) {
// XXX: Invoke MSG_LOG_TO_CONSOLE once bug 1135265 lands.
if (loggingEnabled)
(void) filter->LogRuleHitFail(filterAction, msgHdr, rv, "Copy failed");
}
}
break;
default:
// XXX should not be reached. Check in debug build.
NS_ERROR("This default should not be reached.");
break;
}
}
}
if (!msgIsNew)
{
int32_t numNewMessages;
m_downloadFolder->GetNumNewMessages(false, &numNewMessages);
if (numNewMessages > 0)
m_downloadFolder->SetNumNewMessages(numNewMessages - 1);
m_numNotNewMessages++;
}
return rv;
}
// this gets run in a second pass, after apply filters to a header.
nsresult nsParseNewMailState::ApplyForwardAndReplyFilter(nsIMsgWindow *msgWindow)
{
nsresult rv = NS_OK;
nsCOMPtr <nsIMsgIncomingServer> server;
uint32_t i;
uint32_t count = m_forwardTo.Length();
for (i = 0; i < count; i++)
{
if (!m_forwardTo[i].IsEmpty())
{
nsAutoString forwardStr;
CopyASCIItoUTF16(m_forwardTo[i], forwardStr);
rv = m_rootFolder->GetServer(getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv, rv);
{
nsCOMPtr<nsIMsgComposeService> compService =
do_GetService (NS_MSGCOMPOSESERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = compService->ForwardMessage(forwardStr, m_msgToForwardOrReply,
msgWindow, server,
nsIMsgComposeService::kForwardAsDefault);
}
}
}
m_forwardTo.Clear();
count = m_replyTemplateUri.Length();
for (i = 0; i < count; i++)
{
if (!m_replyTemplateUri[i].IsEmpty())
{
// copy this and truncate the original, so we don't accidentally re-use it on the next hdr.
rv = m_rootFolder->GetServer(getter_AddRefs(server));
if (server)
{
nsCOMPtr <nsIMsgComposeService> compService = do_GetService (NS_MSGCOMPOSESERVICE_CONTRACTID) ;
if (compService) {
rv = compService->ReplyWithTemplate(m_msgToForwardOrReply,
m_replyTemplateUri[i].get(),
msgWindow, server);
if (NS_FAILED(rv)) {
NS_WARNING("ReplyWithTemplate failed");
if (rv == NS_ERROR_ABORT) {
m_filter->LogRuleHitFail(m_ruleAction, m_msgToForwardOrReply, rv,
"Sending reply aborted");
} else {
m_filter->LogRuleHitFail(m_ruleAction, m_msgToForwardOrReply, rv,
"Error sending reply");
}
}
}
}
}
}
m_replyTemplateUri.Clear();
m_msgToForwardOrReply = nullptr;
return rv;
}
void nsParseNewMailState::MarkFilteredMessageRead(nsIMsgDBHdr *msgHdr)
{
nsCOMPtr<nsIMutableArray> messageArray(do_CreateInstance(NS_ARRAY_CONTRACTID));
messageArray->AppendElement(msgHdr, false);
m_downloadFolder->MarkMessagesRead(messageArray, true);
}
void nsParseNewMailState::MarkFilteredMessageUnread(nsIMsgDBHdr *msgHdr)
{
uint32_t newFlags;
if (m_mailDB)
{
nsMsgKey msgKey;
msgHdr->GetMessageKey(&msgKey);
m_mailDB->AddToNewList(msgKey);
}
else
{
msgHdr->OrFlags(nsMsgMessageFlags::New, &newFlags);
}
nsCOMPtr<nsIMutableArray> messageArray(do_CreateInstance(NS_ARRAY_CONTRACTID));
messageArray->AppendElement(msgHdr, false);
m_downloadFolder->MarkMessagesRead(messageArray, false);
}
nsresult nsParseNewMailState::EndMsgDownload()
{
if (m_moveCoalescer)
m_moveCoalescer->PlaybackMoves();
// need to do this for all folders that had messages filtered into them
uint32_t serverCount = m_filterTargetFolders.Count();
nsresult rv;
nsCOMPtr<nsIMsgMailSession> session =
do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv) && session) // don't use NS_ENSURE_SUCCESS here - we need to release semaphore below
{
for (uint32_t index = 0; index < serverCount; index++)
{
bool folderOpen;
session->IsFolderOpenInWindow(m_filterTargetFolders[index], &folderOpen);
if (!folderOpen)
{
uint32_t folderFlags;
m_filterTargetFolders[index]->GetFlags(&folderFlags);
if (! (folderFlags & (nsMsgFolderFlags::Trash | nsMsgFolderFlags::Inbox)))
{
bool filtersRun;
m_filterTargetFolders[index]->CallFilterPlugins(nullptr, &filtersRun);
if (!filtersRun)
m_filterTargetFolders[index]->SetMsgDatabase(nullptr);
}
}
}
}
m_filterTargetFolders.Clear();
return rv;
}
nsresult nsParseNewMailState::AppendMsgFromStream(nsIInputStream *fileStream,
nsIMsgDBHdr *aHdr,
uint32_t length,
nsIMsgFolder *destFolder)
{
nsCOMPtr <nsISeekableStream> seekableStream = do_QueryInterface(fileStream);
nsCOMPtr<nsIMsgPluggableStore> store;
nsCOMPtr<nsIOutputStream> destOutputStream;
nsresult rv = destFolder->GetMsgStore(getter_AddRefs(store));
NS_ENSURE_SUCCESS(rv, rv);
bool reusable;
rv = store->GetNewMsgOutputStream(destFolder, &aHdr, &reusable,
getter_AddRefs(destOutputStream));
NS_ENSURE_SUCCESS(rv, rv);
if (!m_ibuffer)
{
m_ibuffer_size = FILE_IO_BUFFER_SIZE;
m_ibuffer = (char *) PR_Malloc(m_ibuffer_size);
NS_ASSERTION(m_ibuffer != nullptr, "couldn't get memory to move msg");
}
m_ibuffer_fp = 0;
while (length > 0 && m_ibuffer)
{
uint32_t nRead;
fileStream->Read (m_ibuffer, length > m_ibuffer_size ? m_ibuffer_size : length, &nRead);
if (nRead == 0)
break;
uint32_t bytesWritten;
// Check the number of bytes actually written to the stream.
destOutputStream->Write(m_ibuffer, nRead, &bytesWritten);
if (bytesWritten != nRead)
{
destOutputStream->Close();
return NS_MSG_ERROR_WRITING_MAIL_FOLDER;
}
length -= nRead;
}
NS_ASSERTION(length == 0, "didn't read all of original message in filter move");
// non-reusable streams will get closed by the store.
if (reusable)
destOutputStream->Close();
return store->FinishNewMessage(destOutputStream, aHdr);
}
/*
* Moves message pointed to by mailHdr into folder destIFolder.
* After successful move mailHdr is no longer usable by the caller.
*/
nsresult nsParseNewMailState::MoveIncorporatedMessage(nsIMsgDBHdr *mailHdr,
nsIMsgDatabase *sourceDB,
nsIMsgFolder *destIFolder,
nsIMsgFilter *filter,
nsIMsgWindow *msgWindow)
{
NS_ENSURE_ARG_POINTER(destIFolder);
nsresult rv = NS_OK;
// check if the destination is a real folder (by checking for null parent)
// and if it can file messages (e.g., servers or news folders can't file messages).
// Or read only imap folders...
bool canFileMessages = true;
nsCOMPtr<nsIMsgFolder> parentFolder;
destIFolder->GetParent(getter_AddRefs(parentFolder));
if (parentFolder)
destIFolder->GetCanFileMessages(&canFileMessages);
if (!parentFolder || !canFileMessages)
{
if (filter)
{
filter->SetEnabled(false);
// we need to explicitly save the filter file.
if (m_filterList)
m_filterList->SaveToDefaultFile();
destIFolder->ThrowAlertMsg("filterDisabled", msgWindow);
}
return NS_MSG_NOT_A_MAIL_FOLDER;
}
uint32_t messageLength;
mailHdr->GetMessageSize(&messageLength);
nsCOMPtr<nsIMsgLocalMailFolder> localFolder = do_QueryInterface(destIFolder);
if (localFolder) {
bool destFolderTooBig = true;
rv = localFolder->WarnIfLocalFileTooBig(msgWindow, messageLength,
&destFolderTooBig);
if (NS_FAILED(rv) || destFolderTooBig)
return NS_MSG_ERROR_WRITING_MAIL_FOLDER;
}
nsCOMPtr<nsISupports> myISupports =
do_QueryInterface(static_cast<nsIMsgParseMailMsgState*>(this));
// Make sure no one else is writing into this folder
if (NS_FAILED(rv = destIFolder->AcquireSemaphore (myISupports)))
{
destIFolder->ThrowAlertMsg("filterFolderDeniedLocked", msgWindow);
return rv;
}
nsCOMPtr<nsIInputStream> inputStream;
bool reusable;
rv = m_downloadFolder->GetMsgInputStream(mailHdr, &reusable, getter_AddRefs(inputStream));
if (!inputStream)
{
NS_ERROR("couldn't get source msg input stream in move filter");
destIFolder->ReleaseSemaphore (myISupports);
return NS_MSG_FOLDER_UNREADABLE; // ### dmb
}
nsCOMPtr<nsIMsgDatabase> destMailDB;
if (!localFolder)
return NS_MSG_POP_FILTER_TARGET_ERROR;
// don't force upgrade in place - open the db here before we start writing to the
// destination file because XP_Stat can return file size including bytes written...
rv = localFolder->GetDatabaseWOReparse(getter_AddRefs(destMailDB));
NS_WARNING_ASSERTION(destMailDB && NS_SUCCEEDED(rv),
"failed to open mail db parsing folder");
nsCOMPtr<nsIMsgDBHdr> newHdr;
if (destMailDB)
rv = destMailDB->CopyHdrFromExistingHdr(m_new_key, mailHdr, true,
getter_AddRefs(newHdr));
if (NS_SUCCEEDED(rv) && !newHdr)
rv = NS_ERROR_UNEXPECTED;
if (NS_FAILED(rv))
{
destIFolder->ThrowAlertMsg("filterFolderHdrAddFailed", msgWindow);
} else {
rv = AppendMsgFromStream(inputStream, newHdr, messageLength, destIFolder);
if (NS_FAILED(rv))
destIFolder->ThrowAlertMsg("filterFolderWriteFailed", msgWindow);
}
if (NS_FAILED(rv))
{
if (destMailDB)
destMailDB->Close(true);
destIFolder->ReleaseSemaphore(myISupports);
return NS_MSG_ERROR_WRITING_MAIL_FOLDER;
}
bool movedMsgIsNew = false;
// if we have made it this far then the message has successfully been written to the new folder
// now add the header to the destMailDB.
uint32_t newFlags;
newHdr->GetFlags(&newFlags);
nsMsgKey msgKey;
newHdr->GetMessageKey(&msgKey);
if (!(newFlags & nsMsgMessageFlags::Read))
{
nsCString junkScoreStr;
(void) newHdr->GetStringProperty("junkscore", getter_Copies(junkScoreStr));
if (atoi(junkScoreStr.get()) == nsIJunkMailPlugin::IS_HAM_SCORE)
{
newHdr->OrFlags(nsMsgMessageFlags::New, &newFlags);
destMailDB->AddToNewList(msgKey);
movedMsgIsNew = true;
}
}
nsCOMPtr<nsIMsgFolderNotificationService> notifier(do_GetService(NS_MSGNOTIFICATIONSERVICE_CONTRACTID));
if (notifier)
notifier->NotifyMsgAdded(newHdr);
// mark the header as not yet reported classified
destIFolder->OrProcessingFlags(
msgKey, nsMsgProcessingFlags::NotReportedClassified);
m_msgToForwardOrReply = newHdr;
if (movedMsgIsNew)
destIFolder->SetHasNewMessages(true);
if (!m_filterTargetFolders.Contains(destIFolder))
m_filterTargetFolders.AppendObject(destIFolder);
destIFolder->ReleaseSemaphore (myISupports);
(void) localFolder->RefreshSizeOnDisk();
// Notify the message was moved.
if (notifier) {
nsCOMPtr<nsIMsgFolder> folder;
nsresult rv = mailHdr->GetFolder(getter_AddRefs(folder));
if (NS_SUCCEEDED(rv)) {
notifier->NotifyItemEvent(folder,
NS_LITERAL_CSTRING("UnincorporatedMessageMoved"),
newHdr);
} else {
NS_WARNING("Can't get folder for message that was moved.");
}
}
nsCOMPtr<nsIMsgPluggableStore> store;
rv = m_downloadFolder->GetMsgStore(getter_AddRefs(store));
if (store)
store->DiscardNewMessage(m_outputStream, mailHdr);
if (sourceDB)
sourceDB->RemoveHeaderMdbRow(mailHdr);
// update the folder size so we won't reparse.
UpdateDBFolderInfo(destMailDB);
destIFolder->UpdateSummaryTotals(true);
destMailDB->Commit(nsMsgDBCommitType::kLargeCommit);
return rv;
}
|