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
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
|
/* -*- Mode: C++; tab-width: 4; 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/. */
// as does this
#include "msgCore.h" // for pre-compiled headers
#include "nsMsgUtils.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsIStringBundle.h"
#include "nsVersionComparator.h"
#include "nsMsgImapCID.h"
#include "nsThreadUtils.h"
#include "nsIMsgStatusFeedback.h"
#include "nsImapCore.h"
#include "nsImapProtocol.h"
#include "nsIMsgMailNewsUrl.h"
#include "nsIMAPHostSessionList.h"
#include "nsIMAPBodyShell.h"
#include "nsImapMailFolder.h"
#include "nsIMsgAccountManager.h"
#include "nsImapServerResponseParser.h"
#include "nspr.h"
#include "plbase64.h"
#include "nsIImapService.h"
#include "nsISocketTransportService.h"
#include "nsIStreamListenerTee.h"
#include "nsNetUtil.h"
#include "nsIDBFolderInfo.h"
#include "nsIPipe.h"
#include "nsIMsgFolder.h"
#include "nsMsgMessageFlags.h"
#include "nsImapStringBundle.h"
#include "nsICopyMsgStreamListener.h"
#include "nsTextFormatter.h"
#include "nsIMsgHdr.h"
#include "nsMsgI18N.h"
// for the memory cache...
#include "nsICacheEntry.h"
#include "nsICacheStorage.h"
#include "nsICacheEntryOpenCallback.h"
#include "nsIPrompt.h"
#include "nsIDocShell.h"
#include "nsIDocShellLoadInfo.h"
#include "nsILoadInfo.h"
#include "nsIMessengerWindowService.h"
#include "nsIWindowMediator.h"
#include "nsIWindowWatcher.h"
#include "nsCOMPtr.h"
#include "nsMimeTypes.h"
#include "nsIInterfaceRequestor.h"
#include "nsXPCOMCIDInternal.h"
#include "nsIXULAppInfo.h"
#include "nsSyncRunnableHelpers.h"
PRLogModuleInfo *IMAP;
// netlib required files
#include "nsIStreamListener.h"
#include "nsIMsgIncomingServer.h"
#include "nsIImapIncomingServer.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsIPrefLocalizedString.h"
#include "nsImapUtils.h"
#include "nsIStreamConverterService.h"
#include "nsIProxyInfo.h"
#include "nsISSLSocketControl.h"
#include "nsProxyRelease.h"
#include "nsDebug.h"
#include "nsMsgCompressIStream.h"
#include "nsMsgCompressOStream.h"
#include "mozilla/Logging.h"
#include "mozilla/Attributes.h"
#include "nsIPrincipal.h"
#include "nsContentSecurityManager.h"
using namespace mozilla;
#define ONE_SECOND ((uint32_t)1000) // one second
#define OUTPUT_BUFFER_SIZE (4096*2) // mscott - i should be able to remove this if I can use nsMsgLineBuffer???
#define IMAP_ENV_HEADERS "From To Cc Bcc Subject Date Message-ID "
#define IMAP_DB_HEADERS "Priority X-Priority References Newsgroups In-Reply-To Content-Type Reply-To"
#define IMAP_ENV_AND_DB_HEADERS IMAP_ENV_HEADERS IMAP_DB_HEADERS
static const PRIntervalTime kImapSleepTime = PR_MillisecondsToInterval(60000);
static int32_t gPromoteNoopToCheckCount = 0;
static const uint32_t kFlagChangesBeforeCheck = 10;
static const int32_t kMaxSecondsBeforeCheck = 600;
class AutoProxyReleaseMsgWindow
{
public:
AutoProxyReleaseMsgWindow()
: mMsgWindow()
{}
~AutoProxyReleaseMsgWindow()
{
NS_ReleaseOnMainThread(dont_AddRef(mMsgWindow));
}
nsIMsgWindow** StartAssignment()
{
MOZ_ASSERT(!mMsgWindow);
return &mMsgWindow;
}
operator nsIMsgWindow*()
{
return mMsgWindow;
}
private:
nsIMsgWindow* mMsgWindow;
};
nsIMsgWindow**
getter_AddRefs(AutoProxyReleaseMsgWindow& aSmartPtr)
{
return aSmartPtr.StartAssignment();
}
NS_IMPL_ISUPPORTS(nsMsgImapHdrXferInfo, nsIImapHeaderXferInfo)
nsMsgImapHdrXferInfo::nsMsgImapHdrXferInfo()
: m_hdrInfos(kNumHdrsToXfer)
{
m_nextFreeHdrInfo = 0;
}
nsMsgImapHdrXferInfo::~nsMsgImapHdrXferInfo()
{
}
NS_IMETHODIMP nsMsgImapHdrXferInfo::GetNumHeaders(int32_t *aNumHeaders)
{
*aNumHeaders = m_nextFreeHdrInfo;
return NS_OK;
}
NS_IMETHODIMP nsMsgImapHdrXferInfo::GetHeader(int32_t hdrIndex, nsIImapHeaderInfo **aResult)
{
// If the header index is more than (or equal to) our next free pointer, then
// its a header we haven't really got and the caller has done something
// wrong.
NS_ENSURE_TRUE(hdrIndex < m_nextFreeHdrInfo, NS_ERROR_NULL_POINTER);
*aResult = m_hdrInfos.SafeObjectAt(hdrIndex);
if (!*aResult)
return NS_ERROR_NULL_POINTER;
NS_ADDREF(*aResult);
return NS_OK;
}
static const int32_t kInitLineHdrCacheSize = 512; // should be about right
nsIImapHeaderInfo* nsMsgImapHdrXferInfo::StartNewHdr()
{
if (m_nextFreeHdrInfo >= kNumHdrsToXfer)
return nullptr;
nsIImapHeaderInfo *result = m_hdrInfos.SafeObjectAt(m_nextFreeHdrInfo++);
if (result)
return result;
nsMsgImapLineDownloadCache *lineCache = new nsMsgImapLineDownloadCache();
if (!lineCache)
return nullptr;
lineCache->GrowBuffer(kInitLineHdrCacheSize);
m_hdrInfos.AppendObject(lineCache);
return lineCache;
}
// maybe not needed...
void nsMsgImapHdrXferInfo::FinishCurrentHdr()
{
// nothing to do?
}
void nsMsgImapHdrXferInfo::ResetAll()
{
int32_t count = m_hdrInfos.Count();
for (int32_t i = 0; i < count; i++)
{
nsIImapHeaderInfo *hdrInfo = m_hdrInfos[i];
if (hdrInfo)
hdrInfo->ResetCache();
}
m_nextFreeHdrInfo = 0;
}
void nsMsgImapHdrXferInfo::ReleaseAll()
{
m_hdrInfos.Clear();
m_nextFreeHdrInfo = 0;
}
NS_IMPL_ISUPPORTS(nsMsgImapLineDownloadCache, nsIImapHeaderInfo)
// **** helper class for downloading line ****
nsMsgImapLineDownloadCache::nsMsgImapLineDownloadCache()
{
fLineInfo = (msg_line_info *) PR_CALLOC(sizeof( msg_line_info));
fLineInfo->uidOfMessage = nsMsgKey_None;
m_msgSize = 0;
}
nsMsgImapLineDownloadCache::~nsMsgImapLineDownloadCache()
{
PR_Free( fLineInfo);
}
uint32_t nsMsgImapLineDownloadCache::CurrentUID()
{
return fLineInfo->uidOfMessage;
}
uint32_t nsMsgImapLineDownloadCache::SpaceAvailable()
{
return kDownLoadCacheSize - m_bufferPos;
}
msg_line_info *nsMsgImapLineDownloadCache::GetCurrentLineInfo()
{
AppendBuffer("", 1); // null terminate the buffer
fLineInfo->adoptedMessageLine = GetBuffer();
return fLineInfo;
}
NS_IMETHODIMP nsMsgImapLineDownloadCache::ResetCache()
{
ResetWritePos();
return NS_OK;
}
bool nsMsgImapLineDownloadCache::CacheEmpty()
{
return m_bufferPos == 0;
}
NS_IMETHODIMP nsMsgImapLineDownloadCache::CacheLine(const char *line, uint32_t uid)
{
NS_ASSERTION((PL_strlen(line) + 1) <= SpaceAvailable(),
"Oops... line length greater than space available");
fLineInfo->uidOfMessage = uid;
AppendString(line);
return NS_OK;
}
/* attribute nsMsgKey msgUid; */
NS_IMETHODIMP nsMsgImapLineDownloadCache::GetMsgUid(nsMsgKey *aMsgUid)
{
*aMsgUid = fLineInfo->uidOfMessage;
return NS_OK;
}
NS_IMETHODIMP nsMsgImapLineDownloadCache::SetMsgUid(nsMsgKey aMsgUid)
{
fLineInfo->uidOfMessage = aMsgUid;
return NS_OK;
}
/* attribute long msgSize; */
NS_IMETHODIMP nsMsgImapLineDownloadCache::GetMsgSize(int32_t *aMsgSize)
{
*aMsgSize = m_msgSize;
return NS_OK;
}
NS_IMETHODIMP nsMsgImapLineDownloadCache::SetMsgSize(int32_t aMsgSize)
{
m_msgSize = aMsgSize;
return NS_OK;
}
/* attribute string msgHdrs; */
NS_IMETHODIMP nsMsgImapLineDownloadCache::GetMsgHdrs(const char **aMsgHdrs)
{
// this doesn't copy the string
AppendBuffer("", 1); // null terminate the buffer
*aMsgHdrs = GetBuffer();
return NS_OK;
}
/* the following macros actually implement addref, release and query interface for our component. */
NS_IMPL_ADDREF_INHERITED(nsImapProtocol, nsMsgProtocol)
NS_IMPL_RELEASE_INHERITED(nsImapProtocol, nsMsgProtocol )
NS_INTERFACE_MAP_BEGIN(nsImapProtocol)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIImapProtocol)
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
NS_INTERFACE_MAP_ENTRY(nsIImapProtocol)
NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIImapProtocolSink)
NS_INTERFACE_MAP_ENTRY(nsIMsgAsyncPromptListener)
NS_INTERFACE_MAP_END_THREADSAFE
static int32_t gTooFastTime = 2;
static int32_t gIdealTime = 4;
static int32_t gChunkAddSize = 16384;
static int32_t gChunkSize = 250000;
static int32_t gChunkThreshold = gChunkSize + gChunkSize/2;
static bool gChunkSizeDirty = false;
static bool gFetchByChunks = true;
static bool gInitialized = false;
static bool gHideUnusedNamespaces = true;
static bool gHideOtherUsersFromList = false;
static bool gUseEnvelopeCmd = false;
static bool gUseLiteralPlus = true;
static bool gExpungeAfterDelete = false;
static bool gCheckDeletedBeforeExpunge = false; //bug 235004
static int32_t gResponseTimeout = 60;
static nsCString gForceSelectDetect;
static nsTArray<nsCString> gForceSelectServersArray;
// let delete model control expunging, i.e., don't ever expunge when the
// user chooses the imap delete model, otherwise, expunge when over the
// threshhold. This is the normal TB behavior.
static const int32_t kAutoExpungeDeleteModel = 0;
// Expunge whenever the folder is opened
static const int32_t kAutoExpungeAlways = 1;
// Expunge when over the threshhold, independent of the delete model.
static const int32_t kAutoExpungeOnThreshold = 2;
static int32_t gExpungeOption = kAutoExpungeDeleteModel;
static int32_t gExpungeThreshold = 20;
const int32_t kAppBufSize = 100;
// can't use static nsCString because it shows up as a leak.
static char gAppName[kAppBufSize];
static char gAppVersion[kAppBufSize];
nsresult nsImapProtocol::GlobalInitialization(nsIPrefBranch *aPrefBranch)
{
gInitialized = true;
aPrefBranch->GetIntPref("mail.imap.chunk_fast", &gTooFastTime); // secs we read too little too fast
aPrefBranch->GetIntPref("mail.imap.chunk_ideal", &gIdealTime); // secs we read enough in good time
aPrefBranch->GetIntPref("mail.imap.chunk_add", &gChunkAddSize); // buffer size to add when wasting time
aPrefBranch->GetIntPref("mail.imap.chunk_size", &gChunkSize);
aPrefBranch->GetIntPref("mail.imap.min_chunk_size_threshold",
&gChunkThreshold);
aPrefBranch->GetBoolPref("mail.imap.hide_other_users",
&gHideOtherUsersFromList);
aPrefBranch->GetBoolPref("mail.imap.hide_unused_namespaces",
&gHideUnusedNamespaces);
aPrefBranch->GetIntPref("mail.imap.noop_check_count",
&gPromoteNoopToCheckCount);
aPrefBranch->GetBoolPref("mail.imap.use_envelope_cmd",
&gUseEnvelopeCmd);
aPrefBranch->GetBoolPref("mail.imap.use_literal_plus", &gUseLiteralPlus);
aPrefBranch->GetBoolPref("mail.imap.expunge_after_delete",
&gExpungeAfterDelete);
aPrefBranch->GetBoolPref("mail.imap.check_deleted_before_expunge",
&gCheckDeletedBeforeExpunge);
aPrefBranch->GetIntPref("mail.imap.expunge_option", &gExpungeOption);
aPrefBranch->GetIntPref("mail.imap.expunge_threshold_number",
&gExpungeThreshold);
aPrefBranch->GetIntPref("mailnews.tcptimeout", &gResponseTimeout);
aPrefBranch->GetCharPref("mail.imap.force_select_detect",
getter_Copies(gForceSelectDetect));
ParseString(gForceSelectDetect, ';', gForceSelectServersArray);
nsCOMPtr<nsIXULAppInfo> appInfo(do_GetService(XULAPPINFO_SERVICE_CONTRACTID));
if (appInfo)
{
nsCString appName, appVersion;
appInfo->GetName(appName);
appInfo->GetVersion(appVersion);
PL_strncpyz(gAppName, appName.get(), kAppBufSize);
PL_strncpyz(gAppVersion, appVersion.get(), kAppBufSize);
}
return NS_OK;
}
nsImapProtocol::nsImapProtocol() : nsMsgProtocol(nullptr),
m_dataAvailableMonitor("imapDataAvailable"),
m_urlReadyToRunMonitor("imapUrlReadyToRun"),
m_pseudoInterruptMonitor("imapPseudoInterrupt"),
m_dataMemberMonitor("imapDataMember"),
m_threadDeathMonitor("imapThreadDeath"),
m_waitForBodyIdsMonitor("imapWaitForBodyIds"),
m_fetchBodyListMonitor("imapFetchBodyList"),
m_passwordReadyMonitor("imapPasswordReady"),
mLock("nsImapProtocol.mLock"),
m_parser(*this)
{
m_urlInProgress = false;
m_idle = false;
m_retryUrlOnError = false;
m_useIdle = true; // by default, use it
m_useCondStore = true;
m_useCompressDeflate = true;
m_ignoreExpunges = false;
m_prefAuthMethods = kCapabilityUndefined;
m_failedAuthMethods = 0;
m_currentAuthMethod = kCapabilityUndefined;
m_socketType = nsMsgSocketType::trySTARTTLS;
m_connectionStatus = NS_OK;
m_safeToCloseConnection = false;
m_hostSessionList = nullptr;
m_flagState = nullptr;
m_fetchBodyIdList = nullptr;
m_isGmailServer = false;
m_fetchingWholeMessage = false;
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
NS_ASSERTION(prefBranch, "FAILED to create the preference service");
// read in the accept languages preference
if (prefBranch)
{
if (!gInitialized)
GlobalInitialization(prefBranch);
nsCOMPtr<nsIPrefLocalizedString> prefString;
prefBranch->GetComplexValue("intl.accept_languages",
NS_GET_IID(nsIPrefLocalizedString),
getter_AddRefs(prefString));
if (prefString)
prefString->ToString(getter_Copies(mAcceptLanguages));
nsCString customDBHeaders;
prefBranch->GetCharPref("mailnews.customDBHeaders",
getter_Copies(customDBHeaders));
ParseString(customDBHeaders, ' ', mCustomDBHeaders);
prefBranch->GetBoolPref("mailnews.display.prefer_plaintext", &m_preferPlainText);
nsAutoCString customHeaders;;
prefBranch->GetCharPref("mailnews.customHeaders",
getter_Copies(customHeaders));
customHeaders.StripWhitespace();
ParseString(customHeaders, ':', mCustomHeaders);
}
// ***** Thread support *****
m_thread = nullptr;
m_imapThreadIsRunning = false;
m_currentServerCommandTagNumber = 0;
m_active = false;
m_folderNeedsSubscribing = false;
m_folderNeedsACLRefreshed = false;
m_threadShouldDie = false;
m_inThreadShouldDie = false;
m_pseudoInterrupted = false;
m_nextUrlReadyToRun = false;
m_trackingTime = false;
m_curFetchSize = 0;
m_startTime = 0;
m_endTime = 0;
m_lastActiveTime = 0;
m_lastProgressTime = 0;
ResetProgressInfo();
m_tooFastTime = 0;
m_idealTime = 0;
m_chunkAddSize = 0;
m_chunkStartSize = 0;
m_fetchByChunks = true;
m_sendID = true;
m_chunkSize = 0;
m_chunkThreshold = 0;
m_fromHeaderSeen = false;
m_closeNeededBeforeSelect = false;
m_needNoop = false;
m_noopCount = 0;
m_fetchBodyListIsNew = false;
m_flagChangeCount = 0;
m_lastCheckTime = PR_Now();
m_checkForNewMailDownloadsHeaders = true; // this should be on by default
m_hierarchyNameState = kNoOperationInProgress;
m_discoveryStatus = eContinue;
m_overRideUrlConnectionInfo = false;
// m_dataOutputBuf is used by Send Data
m_dataOutputBuf = (char *) PR_CALLOC(sizeof(char) * OUTPUT_BUFFER_SIZE);
m_allocatedSize = OUTPUT_BUFFER_SIZE;
// used to buffer incoming data by ReadNextLine
m_inputStreamBuffer = new nsMsgLineStreamBuffer(OUTPUT_BUFFER_SIZE, true /* allocate new lines */, false /* leave CRLFs on the returned string */);
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_Unknown;
m_progressStringName.Truncate();
// since these are embedded in the nsImapProtocol object, but passed
// through proxied xpcom methods, just AddRef them here.
m_hdrDownloadCache = new nsMsgImapHdrXferInfo();
m_downloadLineCache = new nsMsgImapLineDownloadCache();
// subscription
m_autoSubscribe = true;
m_autoUnsubscribe = true;
m_autoSubscribeOnOpen = true;
m_deletableChildren = nullptr;
mFolderLastModSeq = 0;
Configure(gTooFastTime, gIdealTime, gChunkAddSize, gChunkSize,
gChunkThreshold, gFetchByChunks);
m_forceSelect = false;
// where should we do this? Perhaps in the factory object?
if (!IMAP)
IMAP = PR_NewLogModule("IMAP");
}
nsresult nsImapProtocol::Configure(int32_t TooFastTime, int32_t IdealTime,
int32_t ChunkAddSize, int32_t ChunkSize, int32_t ChunkThreshold,
bool FetchByChunks)
{
m_tooFastTime = TooFastTime; // secs we read too little too fast
m_idealTime = IdealTime; // secs we read enough in good time
m_chunkAddSize = ChunkAddSize; // buffer size to add when wasting time
m_chunkStartSize = m_chunkSize = ChunkSize;
m_chunkThreshold = ChunkThreshold;
m_fetchByChunks = FetchByChunks;
return NS_OK;
}
NS_IMETHODIMP
nsImapProtocol::Initialize(nsIImapHostSessionList * aHostSessionList,
nsIImapIncomingServer *aServer)
{
NS_PRECONDITION(aHostSessionList && aServer,
"oops...trying to initialize with a null host session list or server!");
if (!aHostSessionList || !aServer)
return NS_ERROR_NULL_POINTER;
nsresult rv = m_downloadLineCache->GrowBuffer(kDownLoadCacheSize);
NS_ENSURE_SUCCESS(rv, rv);
m_flagState = new nsImapFlagAndUidState(kImapFlagAndUidStateSize);
if (!m_flagState)
return NS_ERROR_OUT_OF_MEMORY;
aServer->GetUseIdle(&m_useIdle);
aServer->GetForceSelect(m_forceSelectValue);
aServer->GetUseCondStore(&m_useCondStore);
aServer->GetUseCompressDeflate(&m_useCompressDeflate);
NS_ADDREF(m_flagState);
m_hostSessionList = aHostSessionList; // no ref count...host session list has life time > connection
m_parser.SetHostSessionList(aHostSessionList);
m_parser.SetFlagState(m_flagState);
// Initialize the empty mime part string on the main thread.
nsCOMPtr<nsIStringBundle> bundle;
rv = IMAPGetStringBundle(getter_AddRefs(bundle));
NS_ENSURE_SUCCESS(rv, rv);
rv = bundle->GetStringFromName(u"imapEmptyMimePart",
getter_Copies(m_emptyMimePartString));
NS_ENSURE_SUCCESS(rv, rv);
// Now initialize the thread for the connection
if (m_thread == nullptr)
{
nsresult rv = NS_NewThread(getter_AddRefs(m_iThread), this);
if (NS_FAILED(rv))
{
NS_ASSERTION(m_iThread, "Unable to create imap thread.\n");
return rv;
}
m_iThread->GetPRThread(&m_thread);
}
return NS_OK;
}
nsImapProtocol::~nsImapProtocol()
{
PR_Free(m_fetchBodyIdList);
NS_IF_RELEASE(m_flagState);
PR_Free(m_dataOutputBuf);
// **** We must be out of the thread main loop function
NS_ASSERTION(!m_imapThreadIsRunning, "Oops, thread is still running.\n");
}
const nsCString&
nsImapProtocol::GetImapHostName()
{
if (m_runningUrl && m_hostName.IsEmpty())
{
nsCOMPtr<nsIURI> url = do_QueryInterface(m_runningUrl);
url->GetAsciiHost(m_hostName);
}
return m_hostName;
}
const nsCString&
nsImapProtocol::GetImapUserName()
{
if (m_userName.IsEmpty() && m_imapServerSink)
{
m_imapServerSink->GetOriginalUsername(m_userName);
}
return m_userName;
}
const char*
nsImapProtocol::GetImapServerKey()
{
if (m_serverKey.IsEmpty() && m_imapServerSink)
{
m_imapServerSink->GetServerKey(m_serverKey);
}
return m_serverKey.get();
}
nsresult
nsImapProtocol::SetupSinkProxy()
{
nsresult res;
if (m_runningUrl)
{
if (!m_imapMailFolderSink)
{
nsCOMPtr<nsIImapMailFolderSink> aImapMailFolderSink;
(void) m_runningUrl->GetImapMailFolderSink(getter_AddRefs(aImapMailFolderSink));
if (aImapMailFolderSink)
{
m_imapMailFolderSink = new ImapMailFolderSinkProxy(aImapMailFolderSink);
}
}
if (!m_imapMessageSink)
{
nsCOMPtr<nsIImapMessageSink> aImapMessageSink;
(void) m_runningUrl->GetImapMessageSink(getter_AddRefs(aImapMessageSink));
if (aImapMessageSink) {
m_imapMessageSink = new ImapMessageSinkProxy(aImapMessageSink);
} else {
return NS_ERROR_ILLEGAL_VALUE;
}
}
if (!m_imapServerSink)
{
nsCOMPtr<nsIImapServerSink> aImapServerSink;
res = m_runningUrl->GetImapServerSink(getter_AddRefs(aImapServerSink));
if (aImapServerSink) {
m_imapServerSink = new ImapServerSinkProxy(aImapServerSink);
} else {
return NS_ERROR_ILLEGAL_VALUE;
}
}
if (!m_imapProtocolSink)
{
nsCOMPtr<nsIImapProtocolSink> anImapProxyHelper(do_QueryInterface(NS_ISUPPORTS_CAST(nsIImapProtocolSink*, this), &res));
m_imapProtocolSink = new ImapProtocolSinkProxy(anImapProxyHelper);
}
}
return NS_OK;
}
static void SetSecurityCallbacksFromChannel(nsISocketTransport* aTrans, nsIChannel* aChannel)
{
nsCOMPtr<nsIInterfaceRequestor> callbacks;
aChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
nsCOMPtr<nsILoadGroup> loadGroup;
aChannel->GetLoadGroup(getter_AddRefs(loadGroup));
nsCOMPtr<nsIInterfaceRequestor> securityCallbacks;
MsgNewNotificationCallbacksAggregation(callbacks, loadGroup,
getter_AddRefs(securityCallbacks));
if (securityCallbacks)
aTrans->SetSecurityCallbacks(securityCallbacks);
}
// Setup With Url is intended to set up data which is held on a PER URL basis and not
// a per connection basis. If you have data which is independent of the url we are currently
// running, then you should put it in Initialize().
// This is only ever called from the UI thread. It is called from LoadUrl, right
// before the url gets run - i.e., the url is next in line to run.
nsresult nsImapProtocol::SetupWithUrl(nsIURI * aURL, nsISupports* aConsumer)
{
nsresult rv = NS_ERROR_FAILURE;
NS_PRECONDITION(aURL, "null URL passed into Imap Protocol");
if (aURL)
{
m_runningUrl = do_QueryInterface(aURL, &rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl);
nsCOMPtr<nsIMsgIncomingServer> server = do_QueryReferent(m_server);
if (!server)
{
rv = mailnewsUrl->GetServer(getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv, rv);
m_server = do_GetWeakReference(server);
}
nsCOMPtr <nsIMsgFolder> folder;
mailnewsUrl->GetFolder(getter_AddRefs(folder));
mFolderLastModSeq = 0;
mFolderTotalMsgCount = 0;
mFolderHighestUID = 0;
m_uidValidity = kUidUnknown;
if (folder)
{
nsCOMPtr<nsIMsgDatabase> folderDB;
nsCOMPtr<nsIDBFolderInfo> folderInfo;
folder->GetDBFolderInfoAndDB(getter_AddRefs(folderInfo), getter_AddRefs(folderDB));
if (folderInfo)
{
nsCString modSeqStr;
folderInfo->GetCharProperty(kModSeqPropertyName, modSeqStr);
mFolderLastModSeq = ParseUint64Str(modSeqStr.get());
folderInfo->GetNumMessages(&mFolderTotalMsgCount);
folderInfo->GetUint32Property(kHighestRecordedUIDPropertyName, 0, &mFolderHighestUID);
folderInfo->GetImapUidValidity(&m_uidValidity);
}
}
nsCOMPtr<nsIImapIncomingServer> imapServer = do_QueryInterface(server);
nsCOMPtr<nsIStreamListener> aRealStreamListener = do_QueryInterface(aConsumer);
m_runningUrl->GetMockChannel(getter_AddRefs(m_mockChannel));
imapServer->GetIsGMailServer(&m_isGmailServer);
if (!m_mockChannel)
{
nsCOMPtr<nsIPrincipal> nullPrincipal =
do_CreateInstance("@mozilla.org/nullprincipal;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
// there are several imap operations that aren't initiated via a nsIChannel::AsyncOpen call on the mock channel.
// such as selecting a folder. nsImapProtocol now insists on a mock channel when processing a url.
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel),
aURL,
nullPrincipal,
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
nsIContentPolicy::TYPE_OTHER);
m_mockChannel = do_QueryInterface(channel);
// Certain imap operations (not initiated by the IO Service via AsyncOpen) can be interrupted by the stop button on the toolbar.
// We do this by using the loadgroup of the docshell for the message pane. We really shouldn't be doing this..
// See the comment in nsMsgMailNewsUrl::GetLoadGroup.
nsCOMPtr<nsILoadGroup> loadGroup;
mailnewsUrl->GetLoadGroup(getter_AddRefs(loadGroup)); // get the message pane load group
nsCOMPtr<nsIRequest> ourRequest = do_QueryInterface(m_mockChannel);
if (loadGroup)
loadGroup->AddRequest(ourRequest, nullptr /* context isupports */);
}
if (m_mockChannel)
{
m_mockChannel->SetImapProtocol(this);
// if we have a listener from a mock channel, over-ride the consumer that was passed in
nsCOMPtr<nsIStreamListener> channelListener;
m_mockChannel->GetChannelListener(getter_AddRefs(channelListener));
if (channelListener) // only over-ride if we have a non null channel listener
aRealStreamListener = channelListener;
m_mockChannel->GetChannelContext(getter_AddRefs(m_channelContext));
nsCOMPtr<nsIMsgWindow> msgWindow;
GetMsgWindow(getter_AddRefs(msgWindow));
if (!msgWindow)
GetTopmostMsgWindow(getter_AddRefs(msgWindow));
if (msgWindow)
{
nsCOMPtr<nsIDocShell> docShell;
msgWindow->GetMessageWindowDocShell(getter_AddRefs(docShell));
nsCOMPtr<nsIInterfaceRequestor> ir(do_QueryInterface(docShell));
nsCOMPtr<nsIInterfaceRequestor> interfaceRequestor;
msgWindow->GetNotificationCallbacks(getter_AddRefs(interfaceRequestor));
nsCOMPtr<nsIInterfaceRequestor> aggregateIR;
MsgNewInterfaceRequestorAggregation(interfaceRequestor, ir, getter_AddRefs(aggregateIR));
m_mockChannel->SetNotificationCallbacks(aggregateIR);
}
}
// since we'll be making calls directly from the imap thread to the channel listener,
// we need to turn it into a proxy object....we'll assume that the listener is on the same thread
// as the event sink queue
if (aRealStreamListener)
{
NS_ASSERTION(!m_channelListener, "shouldn't already have a channel listener");
m_channelListener = new StreamListenerProxy(aRealStreamListener);
}
server->GetRealHostName(m_realHostName);
int32_t authMethod;
(void) server->GetAuthMethod(&authMethod);
#ifdef MOZ_MAILNEWS_OAUTH2
InitPrefAuthMethods(authMethod, server);
#else
InitPrefAuthMethods(authMethod);
#endif
(void) server->GetSocketType(&m_socketType);
bool shuttingDown;
(void) imapServer->GetShuttingDown(&shuttingDown);
if (!shuttingDown)
(void) imapServer->GetUseIdle(&m_useIdle);
else
m_useIdle = false;
imapServer->GetFetchByChunks(&m_fetchByChunks);
imapServer->GetSendID(&m_sendID);
nsAutoString trashFolderName;
if (NS_SUCCEEDED(imapServer->GetTrashFolderName(trashFolderName)))
CopyUTF16toMUTF7(trashFolderName, m_trashFolderName);
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefBranch)
{
bool preferPlainText;
prefBranch->GetBoolPref("mailnews.display.prefer_plaintext", &preferPlainText);
// If the pref has changed since the last time we ran a url,
// clear the shell cache for this host.
if (preferPlainText != m_preferPlainText)
{
m_hostSessionList->ClearShellCacheForHost(GetImapServerKey());
m_preferPlainText = preferPlainText;
}
}
if ( m_runningUrl && !m_transport /* and we don't have a transport yet */)
{
// extract the file name and create a file transport...
int32_t port=-1;
server->GetPort(&port);
if (port <= 0)
{
int32_t socketType;
// Be a bit smarter about setting the default port
port = (NS_SUCCEEDED(server->GetSocketType(&socketType)) &&
socketType == nsMsgSocketType::SSL) ?
nsIImapUrl::DEFAULT_IMAPS_PORT : nsIImapUrl::DEFAULT_IMAP_PORT;
}
nsCOMPtr<nsISocketTransportService> socketService =
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv) && aURL)
{
aURL->GetPort(&port);
Log("SetupWithUrl", nullptr, "clearing IMAP_CONNECTION_IS_OPEN");
ClearFlag(IMAP_CONNECTION_IS_OPEN);
const char *connectionType = nullptr;
if (m_socketType == nsMsgSocketType::SSL)
connectionType = "ssl";
else if (m_socketType == nsMsgSocketType::alwaysSTARTTLS)
connectionType = "starttls";
// This can go away once we think everyone is migrated
// away from the trySTARTTLS socket type.
else if (m_socketType == nsMsgSocketType::trySTARTTLS)
connectionType = "starttls";
nsCOMPtr<nsIProxyInfo> proxyInfo;
if (m_mockChannel)
rv = MsgExamineForProxy(m_mockChannel, getter_AddRefs(proxyInfo));
if (NS_FAILED(rv))
proxyInfo = nullptr;
const nsACString *socketHost;
uint16_t socketPort;
if (m_overRideUrlConnectionInfo)
{
socketHost = &m_logonHost;
socketPort = m_logonPort;
}
else
{
socketHost = &m_realHostName;
socketPort = port;
}
rv = socketService->CreateTransport(&connectionType, connectionType != nullptr,
*socketHost, socketPort, proxyInfo,
getter_AddRefs(m_transport));
if (NS_FAILED(rv) && m_socketType == nsMsgSocketType::trySTARTTLS)
{
connectionType = nullptr;
m_socketType = nsMsgSocketType::plain;
rv = socketService->CreateTransport(&connectionType, connectionType != nullptr,
*socketHost, socketPort, proxyInfo,
getter_AddRefs(m_transport));
}
// remember so we can know whether we can issue a start tls or not...
m_connectionType = connectionType;
if (m_transport && m_mockChannel)
{
uint8_t qos;
rv = GetQoSBits(&qos);
if (NS_SUCCEEDED(rv))
m_transport->SetQoSBits(qos);
// Ensure that the socket can get the notification callbacks
SetSecurityCallbacksFromChannel(m_transport, m_mockChannel);
// open buffered, blocking input stream
rv = m_transport->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(m_inputStream));
if (NS_FAILED(rv)) return rv;
// open buffered, blocking output stream
rv = m_transport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(m_outputStream));
if (NS_FAILED(rv)) return rv;
SetFlag(IMAP_CONNECTION_IS_OPEN);
}
}
} // if m_runningUrl
if (m_transport && m_mockChannel)
{
m_transport->SetTimeout(nsISocketTransport::TIMEOUT_CONNECT, gResponseTimeout + 60);
int32_t readWriteTimeout = gResponseTimeout;
if (m_runningUrl)
{
m_runningUrl->GetImapAction(&m_imapAction);
// this is a silly hack, but the default of 100 seconds is way too long
// for things like APPEND, which should come back immediately.
if (m_imapAction == nsIImapUrl::nsImapAppendMsgFromFile ||
m_imapAction == nsIImapUrl::nsImapAppendDraftFromFile)
{
readWriteTimeout = 20;
}
else if (m_imapAction == nsIImapUrl::nsImapOnlineMove ||
m_imapAction == nsIImapUrl::nsImapOnlineCopy)
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
uint32_t copyCount = CountMessagesInIdString(messageIdString.get());
// If we're move/copying a large number of messages,
// which should be rare, increase the timeout based on number
// of messages. 40 messages per second should be sufficiently slow.
if (copyCount > 2400) // 40 * 60, 60 is default read write timeout
readWriteTimeout = std::max(readWriteTimeout, (int32_t)copyCount/40);
}
}
m_transport->SetTimeout(nsISocketTransport::TIMEOUT_READ_WRITE, readWriteTimeout);
// set the security info for the mock channel to be the security status for our underlying transport.
nsCOMPtr<nsISupports> securityInfo;
m_transport->GetSecurityInfo(getter_AddRefs(securityInfo));
m_mockChannel->SetSecurityInfo(securityInfo);
SetSecurityCallbacksFromChannel(m_transport, m_mockChannel);
nsCOMPtr<nsITransportEventSink> sink = do_QueryInterface(m_mockChannel);
if (sink) {
nsCOMPtr<nsIThread> thread = do_GetMainThread();
m_transport->SetEventSink(sink, thread);
}
// and if we have a cache entry that we are saving the message to, set the security info on it too.
// since imap only uses the memory cache, passing this on is the right thing to do.
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl);
if (mailnewsUrl)
{
nsCOMPtr<nsICacheEntry> cacheEntry;
mailnewsUrl->GetMemCacheEntry(getter_AddRefs(cacheEntry));
if (cacheEntry)
cacheEntry->SetSecurityInfo(securityInfo);
}
}
} // if aUR
return rv;
}
// when the connection is done processing the current state, free any per url state data...
void nsImapProtocol::ReleaseUrlState(bool rerunning)
{
// clear out the socket's reference to the notification callbacks for this transaction
{
MutexAutoLock mon(mLock);
if (m_transport)
{
m_transport->SetSecurityCallbacks(nullptr);
m_transport->SetEventSink(nullptr, nullptr);
}
}
if (m_mockChannel && !rerunning)
{
// Proxy the close of the channel to the ui thread.
if (m_imapMailFolderSink)
m_imapMailFolderSink->CloseMockChannel(m_mockChannel);
else
m_mockChannel->Close();
{
// grab a lock so m_mockChannel doesn't get cleared out
// from under us.
MutexAutoLock mon(mLock);
if (m_mockChannel)
{
// Proxy the release of the channel to the main thread. This is something
// that the xpcom proxy system should do for us!
NS_ReleaseOnMainThread(m_mockChannel.forget());
}
}
}
m_channelContext = nullptr; // this might be the url - null it out before the final release of the url
m_imapMessageSink = nullptr;
// Proxy the release of the listener to the main thread. This is something
// that the xpcom proxy system should do for us!
{
// grab a lock so the m_channelListener doesn't get cleared.
MutexAutoLock mon(mLock);
if (m_channelListener)
{
NS_ReleaseOnMainThread(m_channelListener.forget());
}
}
m_channelInputStream = nullptr;
m_channelOutputStream = nullptr;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsurl;
nsCOMPtr<nsIImapMailFolderSink> saveFolderSink;
{
MutexAutoLock mon(mLock);
if (m_runningUrl)
{
mailnewsurl = do_QueryInterface(m_runningUrl);
// It is unclear what 'saveFolderSink' is used for, most likely to hold
// a reference for a little longer. See bug 1324893 and bug 391259.
saveFolderSink = m_imapMailFolderSink;
m_runningUrl = nullptr; // force us to release our last reference on the url
m_urlInProgress = false;
}
}
// Need to null this out whether we have an m_runningUrl or not
m_imapMailFolderSink = nullptr;
// we want to make sure the imap protocol's last reference to the url gets released
// back on the UI thread. This ensures that the objects the imap url hangs on to
// properly get released back on the UI thread.
if (mailnewsurl)
{
NS_ReleaseOnMainThread(mailnewsurl.forget());
}
saveFolderSink = nullptr;
}
class nsImapThreadShutdownEvent : public mozilla::Runnable {
public:
explicit nsImapThreadShutdownEvent(nsIThread *thread) : mThread(thread) {
}
NS_IMETHOD Run() {
mThread->Shutdown();
return NS_OK;
}
private:
nsCOMPtr<nsIThread> mThread;
};
NS_IMETHODIMP nsImapProtocol::Run()
{
PR_CEnterMonitor(this);
NS_ASSERTION(!m_imapThreadIsRunning,
"Oh. oh. thread is already running. What's wrong here?");
if (m_imapThreadIsRunning)
{
PR_CExitMonitor(this);
return NS_OK;
}
m_imapThreadIsRunning = true;
PR_CExitMonitor(this);
// call the platform specific main loop ....
ImapThreadMainLoop();
if (m_runningUrl)
{
NS_ReleaseOnMainThread(m_runningUrl.forget());
}
// close streams via UI thread if it's not already done
if (m_imapProtocolSink)
m_imapProtocolSink->CloseStreams();
m_imapMailFolderSink = nullptr;
m_imapMessageSink = nullptr;
// shutdown this thread, but do it from the main thread
nsCOMPtr<nsIRunnable> ev = new nsImapThreadShutdownEvent(m_iThread);
if (NS_FAILED(NS_DispatchToMainThread(ev)))
NS_WARNING("Failed to dispatch nsImapThreadShutdownEvent");
m_iThread = nullptr;
// Release protocol object on the main thread to avoid destruction of 'this'
// on the IMAP thread, which causes grief for weak references.
nsCOMPtr<nsIImapProtocol> releaseOnMain(this);
NS_ReleaseOnMainThread(releaseOnMain.forget());
return NS_OK;
}
//
// Must be called from UI thread only
//
NS_IMETHODIMP nsImapProtocol::CloseStreams()
{
// make sure that it is called by the UI thread
MOZ_ASSERT(NS_IsMainThread(), "CloseStreams() should not be called from an off UI thread");
{
MutexAutoLock mon(mLock);
if (m_transport)
{
// make sure the transport closes (even if someone is still indirectly
// referencing it).
m_transport->Close(NS_ERROR_ABORT);
m_transport = nullptr;
}
m_inputStream = nullptr;
m_outputStream = nullptr;
m_channelListener = nullptr;
m_channelContext = nullptr;
if (m_mockChannel)
{
m_mockChannel->Close();
m_mockChannel = nullptr;
}
m_channelInputStream = nullptr;
m_channelOutputStream = nullptr;
// Close scope because we must let go of the monitor before calling
// RemoveConnection to unblock anyone who tries to get a monitor to the
// protocol object while holding onto a monitor to the server.
}
nsCOMPtr<nsIMsgIncomingServer> me_server = do_QueryReferent(m_server);
if (me_server)
{
nsresult result;
nsCOMPtr<nsIImapIncomingServer>
aImapServer(do_QueryInterface(me_server, &result));
if (NS_SUCCEEDED(result))
aImapServer->RemoveConnection(this);
me_server = nullptr;
}
m_server = nullptr;
// take this opportunity of being on the UI thread to
// persist chunk prefs if they've changed
if (gChunkSizeDirty)
{
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefBranch)
{
prefBranch->SetIntPref("mail.imap.chunk_size", gChunkSize);
prefBranch->SetIntPref("mail.imap.min_chunk_size_threshold", gChunkThreshold);
gChunkSizeDirty = false;
}
}
return NS_OK;
}
NS_IMETHODIMP nsImapProtocol::GetUrlWindow(nsIMsgMailNewsUrl *aUrl,
nsIMsgWindow **aMsgWindow)
{
NS_ENSURE_ARG_POINTER(aUrl);
NS_ENSURE_ARG_POINTER(aMsgWindow);
return aUrl->GetMsgWindow(aMsgWindow);
}
NS_IMETHODIMP nsImapProtocol::SetupMainThreadProxies()
{
return SetupSinkProxy();
}
NS_IMETHODIMP nsImapProtocol::OnInputStreamReady(nsIAsyncInputStream *inStr)
{
// should we check if it's a close vs. data available?
if (m_idle)
{
uint64_t bytesAvailable = 0;
(void) inStr->Available(&bytesAvailable);
// check if data available - might be a close
if (bytesAvailable != 0)
{
ReentrantMonitorAutoEnter mon(m_urlReadyToRunMonitor);
m_lastActiveTime = PR_Now();
m_nextUrlReadyToRun = true;
mon.Notify();
}
}
return NS_OK;
}
// this is to be called from the UI thread. It sets m_threadShouldDie,
// and then signals the imap thread, which, when it wakes up, should exit.
// The imap thread cleanup code will check m_safeToCloseConnection.
NS_IMETHODIMP
nsImapProtocol::TellThreadToDie(bool aIsSafeToClose)
{
NS_WARNING_ASSERTION(NS_IsMainThread(),
"TellThreadToDie(aIsSafeToClose) should only be called from UI thread");
MutexAutoLock mon(mLock);
nsCOMPtr<nsIMsgIncomingServer> me_server = do_QueryReferent(m_server);
if (me_server)
{
nsresult rv;
nsCOMPtr<nsIImapIncomingServer>
aImapServer(do_QueryInterface(me_server, &rv));
if (NS_SUCCEEDED(rv))
aImapServer->RemoveConnection(this);
m_server = nullptr;
me_server = nullptr;
}
{
ReentrantMonitorAutoEnter deathMon(m_threadDeathMonitor);
m_safeToCloseConnection = aIsSafeToClose;
m_threadShouldDie = true;
}
ReentrantMonitorAutoEnter readyMon(m_urlReadyToRunMonitor);
m_nextUrlReadyToRun = true;
readyMon.Notify();
return NS_OK;
}
void
nsImapProtocol::TellThreadToDie()
{
nsresult rv = NS_OK;
NS_WARNING_ASSERTION(!NS_IsMainThread(),
"TellThreadToDie() should not be called from UI thread");
// prevent re-entering this method because it may lock the UI.
if (m_inThreadShouldDie)
return;
m_inThreadShouldDie = true;
// This routine is called only from the imap protocol thread.
// The UI thread causes this to be called by calling TellThreadToDie.
// In that case, m_safeToCloseConnection will be FALSE if it's dropping a
// timed out connection, true when closing a cached connection.
// We're using PR_CEnter/ExitMonitor because Monitors don't like having
// us to hold one monitor and call code that gets a different monitor. And
// some of the methods we call here use Monitors.
PR_CEnterMonitor(this);
m_urlInProgress = true; // let's say it's busy so no one tries to use
// this about to die connection.
bool urlWritingData = false;
bool connectionIdle = !m_runningUrl;
if (!connectionIdle)
urlWritingData = m_imapAction == nsIImapUrl::nsImapAppendMsgFromFile
|| m_imapAction == nsIImapUrl::nsImapAppendDraftFromFile;
bool closeNeeded = GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kFolderSelected && m_safeToCloseConnection;
nsCString command;
// if a url is writing data, we can't even logout, so we're just
// going to close the connection as if the user pressed stop.
if (m_currentServerCommandTagNumber > 0 && !urlWritingData)
{
bool isAlive = false;
if (m_transport)
rv = m_transport->IsAlive(&isAlive);
if (TestFlag(IMAP_CONNECTION_IS_OPEN) && m_idle && isAlive)
EndIdle(false);
if (NS_SUCCEEDED(rv) && isAlive && closeNeeded && GetDeleteIsMoveToTrash() &&
TestFlag(IMAP_CONNECTION_IS_OPEN) && m_outputStream)
Close(true, connectionIdle);
if (NS_SUCCEEDED(rv) && isAlive && TestFlag(IMAP_CONNECTION_IS_OPEN) &&
NS_SUCCEEDED(GetConnectionStatus()) && m_outputStream)
Logout(true, connectionIdle);
}
PR_CExitMonitor(this);
// close streams via UI thread
if (m_imapProtocolSink)
{
m_imapProtocolSink->CloseStreams();
m_imapProtocolSink = nullptr;
}
Log("TellThreadToDie", nullptr, "close socket connection");
{
ReentrantMonitorAutoEnter mon(m_threadDeathMonitor);
m_threadShouldDie = true;
}
{
ReentrantMonitorAutoEnter dataMon(m_dataAvailableMonitor);
dataMon.Notify();
}
ReentrantMonitorAutoEnter urlReadyMon(m_urlReadyToRunMonitor);
urlReadyMon.NotifyAll();
}
NS_IMETHODIMP
nsImapProtocol::GetLastActiveTimeStamp(PRTime* aTimeStamp)
{
if (aTimeStamp)
*aTimeStamp = m_lastActiveTime;
return NS_OK;
}
static void DoomCacheEntry(nsIMsgMailNewsUrl *url);
NS_IMETHODIMP
nsImapProtocol::PseudoInterruptMsgLoad(nsIMsgFolder *aImapFolder, nsIMsgWindow *aMsgWindow, bool *interrupted)
{
NS_ENSURE_ARG (interrupted);
*interrupted = false;
PR_CEnterMonitor(this);
if (m_runningUrl && !TestFlag(IMAP_CLEAN_UP_URL_STATE))
{
nsImapAction imapAction;
m_runningUrl->GetImapAction(&imapAction);
if (imapAction == nsIImapUrl::nsImapMsgFetch)
{
nsresult rv = NS_OK;
nsCOMPtr<nsIImapUrl> runningImapURL;
rv = GetRunningImapURL(getter_AddRefs(runningImapURL));
if (NS_SUCCEEDED(rv) && runningImapURL)
{
nsCOMPtr <nsIMsgFolder> runningImapFolder;
nsCOMPtr <nsIMsgWindow> msgWindow;
nsCOMPtr <nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(runningImapURL);
mailnewsUrl->GetMsgWindow(getter_AddRefs(msgWindow));
mailnewsUrl->GetFolder(getter_AddRefs(runningImapFolder));
if (aImapFolder == runningImapFolder && msgWindow == aMsgWindow)
{
PseudoInterrupt(true);
*interrupted = true;
}
// If we're interrupted, doom any incomplete cache entry.
DoomCacheEntry(mailnewsUrl);
}
}
}
PR_CExitMonitor(this);
#ifdef DEBUG_bienvenu
printf("interrupt msg load : %s\n", (*interrupted) ? "TRUE" : "FALSE");
#endif
return NS_OK;
}
void
nsImapProtocol::ImapThreadMainLoop()
{
MOZ_LOG(IMAP, LogLevel::Debug, ("ImapThreadMainLoop entering [this=%x]\n", this));
PRIntervalTime sleepTime = kImapSleepTime;
while (!DeathSignalReceived())
{
nsresult rv = NS_OK;
bool readyToRun;
// wait for an URL to process...
{
ReentrantMonitorAutoEnter mon(m_urlReadyToRunMonitor);
while (NS_SUCCEEDED(rv) && !DeathSignalReceived() &&
!m_nextUrlReadyToRun && !m_threadShouldDie)
rv = mon.Wait(sleepTime);
readyToRun = m_nextUrlReadyToRun;
m_nextUrlReadyToRun = false;
}
// This will happen if the UI thread signals us to die
if (m_threadShouldDie)
{
TellThreadToDie();
break;
}
if (NS_FAILED(rv) && PR_PENDING_INTERRUPT_ERROR == PR_GetError())
{
printf("error waiting for monitor\n");
break;
}
if (readyToRun && m_runningUrl)
{
if (m_currentServerCommandTagNumber && m_transport)
{
bool isAlive;
rv = m_transport->IsAlive(&isAlive);
// if the transport is not alive, and we've ever sent a command with this connection, kill it.
// otherwise, we've probably just not finished setting it so don't kill it!
if (NS_FAILED(rv) || !isAlive)
{
// This says we never started running the url, which is the case.
m_runningUrl->SetRerunningUrl(false);
RetryUrl();
return;
}
}
//
// NOTE: Though we cleared m_nextUrlReadyToRun above, it may have been
// set by LoadImapUrl, which runs on the main thread. Because of this,
// we must not try to clear m_nextUrlReadyToRun here.
//
if (ProcessCurrentURL())
{
m_nextUrlReadyToRun = true;
m_imapMailFolderSink = nullptr;
}
else
{
// see if we want to go into idle mode. Might want to check a pref here too.
if (m_useIdle && !m_urlInProgress && GetServerStateParser().GetCapabilityFlag() & kHasIdleCapability
&& GetServerStateParser().GetIMAPstate()
== nsImapServerResponseParser::kFolderSelected)
{
Idle(); // for now, lets just do it. We'll probably want to use a timer
}
else // if not idle, don't need to remember folder sink
m_imapMailFolderSink = nullptr;
}
}
else if (m_idle && !m_threadShouldDie)
{
HandleIdleResponses();
}
if (!GetServerStateParser().Connected())
break;
#ifdef DEBUG_bienvenu
else
printf("ready to run but no url and not idle\n");
#endif
// This can happen if the UI thread closes cached connections in the
// OnStopRunningUrl notification.
if (m_threadShouldDie)
TellThreadToDie();
}
m_imapThreadIsRunning = false;
MOZ_LOG(IMAP, LogLevel::Debug, ("ImapThreadMainLoop leaving [this=%x]\n", this));
}
void nsImapProtocol::HandleIdleResponses()
{
// int32_t oldRecent = GetServerStateParser().NumberOfRecentMessages();
nsAutoCString commandBuffer(GetServerCommandTag());
commandBuffer.Append(" IDLE" CRLF);
do
{
ParseIMAPandCheckForNewMail(commandBuffer.get());
}
while (m_inputStreamBuffer->NextLineAvailable() && GetServerStateParser().Connected());
// if (oldRecent != GetServerStateParser().NumberOfRecentMessages())
// We might check that something actually changed, but for now we can
// just assume it. OnNewIdleMessages must run a url, so that
// we'll go back into asyncwait mode.
if (GetServerStateParser().Connected() && m_imapMailFolderSink)
m_imapMailFolderSink->OnNewIdleMessages();
}
void nsImapProtocol::EstablishServerConnection()
{
#define ESC_LENGTH(x) (sizeof(x) - 1)
#define ESC_OK "* OK"
#define ESC_OK_LEN ESC_LENGTH(ESC_OK)
#define ESC_PREAUTH "* PREAUTH"
#define ESC_PREAUTH_LEN ESC_LENGTH(ESC_PREAUTH)
#define ESC_CAPABILITY_STAR "* "
#define ESC_CAPABILITY_STAR_LEN ESC_LENGTH(ESC_CAPABILITY_STAR)
#define ESC_CAPABILITY_OK "* OK ["
#define ESC_CAPABILITY_OK_LEN ESC_LENGTH(ESC_CAPABILITY_OK)
#define ESC_CAPABILITY_GREETING (ESC_CAPABILITY_OK "CAPABILITY")
#define ESC_CAPABILITY_GREETING_LEN ESC_LENGTH(ESC_CAPABILITY_GREETING)
char * serverResponse = CreateNewLineFromSocket(); // read in the greeting
// record the fact that we've received a greeting for this connection so we don't ever
// try to do it again..
if (serverResponse)
SetFlag(IMAP_RECEIVED_GREETING);
if (!PL_strncasecmp(serverResponse, ESC_OK, ESC_OK_LEN))
{
SetConnectionStatus(NS_OK);
if (!PL_strncasecmp(serverResponse, ESC_CAPABILITY_GREETING, ESC_CAPABILITY_GREETING_LEN))
{
nsAutoCString tmpstr(serverResponse);
int32_t endIndex = tmpstr.FindChar(']', ESC_CAPABILITY_GREETING_LEN);
if (endIndex >= 0)
{
// Allocate the new buffer here. This buffer will be passed to
// ParseIMAPServerResponse() where it will be used to fill the
// fCurrentLine field and will be freed by the next call to
// ResetLexAnalyzer().
char *fakeServerResponse = (char*)PR_Malloc(PL_strlen(serverResponse));
// Munge the greeting into something that would pass for an IMAP
// server's response to a "CAPABILITY" command.
strcpy(fakeServerResponse, ESC_CAPABILITY_STAR);
strcat(fakeServerResponse, serverResponse + ESC_CAPABILITY_OK_LEN);
fakeServerResponse[endIndex - ESC_CAPABILITY_OK_LEN + ESC_CAPABILITY_STAR_LEN] = '\0';
// Tell the response parser that we just issued a "CAPABILITY" and
// got the following back.
GetServerStateParser().ParseIMAPServerResponse("1 CAPABILITY", true, fakeServerResponse);
}
}
}
else if (!PL_strncasecmp(serverResponse, ESC_PREAUTH, ESC_PREAUTH_LEN))
{
// we've been pre-authenticated.
// we can skip the whole password step, right into the
// kAuthenticated state
GetServerStateParser().PreauthSetAuthenticatedState();
if (GetServerStateParser().GetCapabilityFlag() == kCapabilityUndefined)
Capability();
if ( !(GetServerStateParser().GetCapabilityFlag() &
(kIMAP4Capability | kIMAP4rev1Capability | kIMAP4other) ) )
{
// AlertUserEvent_UsingId(MK_MSG_IMAP_SERVER_NOT_IMAP4);
SetConnectionStatus(NS_ERROR_FAILURE); // stop netlib
}
else
{
// let's record the user as authenticated.
m_imapServerSink->SetUserAuthenticated(true);
ProcessAfterAuthenticated();
// the connection was a success
SetConnectionStatus(NS_OK);
}
}
PR_Free(serverResponse); // we don't care about the greeting yet...
#undef ESC_LENGTH
#undef ESC_OK
#undef ESC_OK_LEN
#undef ESC_PREAUTH
#undef ESC_PREAUTH_LEN
#undef ESC_CAPABILITY_STAR
#undef ESC_CAPABILITY_STAR_LEN
#undef ESC_CAPABILITY_OK
#undef ESC_CAPABILITY_OK_LEN
#undef ESC_CAPABILITY_GREETING
#undef ESC_CAPABILITY_GREETING_LEN
}
// This can get called from the UI thread or an imap thread.
// It makes sure we don't get left with partial messages in
// the memory cache.
static void DoomCacheEntry(nsIMsgMailNewsUrl *url)
{
bool readingFromMemCache = false;
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(url);
imapUrl->GetMsgLoadingFromCache(&readingFromMemCache);
if (!readingFromMemCache)
{
nsCOMPtr<nsICacheEntry> cacheEntry;
url->GetMemCacheEntry(getter_AddRefs(cacheEntry));
if (cacheEntry)
cacheEntry->AsyncDoom(nullptr);
}
}
// returns true if another url was run, false otherwise.
bool nsImapProtocol::ProcessCurrentURL()
{
nsresult rv = NS_OK;
if (m_idle)
EndIdle();
if (m_retryUrlOnError)
{
// we clear this flag if we're re-running immediately, because that
// means we never sent a start running url notification, and later we
// don't send start running notification if we think we're rerunning
// the url (see first call to SetUrlState below). This means we won't
// send a start running notification, which means our stop running
// notification will be ignored because we don't think we were running.
m_runningUrl->SetRerunningUrl(false);
return RetryUrl();
}
Log("ProcessCurrentURL", nullptr, "entering");
(void) GetImapHostName(); // force m_hostName to get set.
bool logonFailed = false;
bool anotherUrlRun = false;
bool rerunningUrl = false;
bool isExternalUrl;
bool validUrl = true;
PseudoInterrupt(false); // clear this if left over from previous url.
m_runningUrl->GetRerunningUrl(&rerunningUrl);
m_runningUrl->GetExternalLinkUrl(&isExternalUrl);
m_runningUrl->GetValidUrl(&validUrl);
m_runningUrl->GetImapAction(&m_imapAction);
if (isExternalUrl)
{
if (m_imapAction == nsIImapUrl::nsImapSelectFolder)
{
// we need to send a start request so that the doc loader
// will call HandleContent on the imap service so we
// can abort this url, and run a new url in a new msg window
// to run the folder load url and get off this crazy merry-go-round.
if (m_channelListener)
{
nsCOMPtr<nsIRequest> request = do_QueryInterface(m_mockChannel);
m_channelListener->OnStartRequest(request, m_channelContext);
}
return false;
}
}
if (!m_imapMailFolderSink && m_imapProtocolSink)
{
// This occurs when running another URL in the main thread loop
rv = m_imapProtocolSink->SetupMainThreadProxies();
NS_ENSURE_SUCCESS(rv, false);
}
// Reinitialize the parser
GetServerStateParser().InitializeState();
GetServerStateParser().SetConnected(true);
// acknowledge that we are running the url now..
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsurl = do_QueryInterface(m_runningUrl, &rv);
nsAutoCString urlSpec;
rv = mailnewsurl->GetSpec(urlSpec);
NS_ENSURE_SUCCESS(rv, false);
Log("ProcessCurrentURL", urlSpec.get(), (validUrl) ? " = currentUrl\n" : " is not valid\n");
if (!validUrl)
return false;
if (NS_SUCCEEDED(rv) && mailnewsurl && m_imapMailFolderSink && !rerunningUrl)
m_imapMailFolderSink->SetUrlState(this, mailnewsurl, true, false,
NS_OK);
// if we are set up as a channel, we should notify our channel listener that we are starting...
// so pass in ourself as the channel and not the underlying socket or file channel the protocol
// happens to be using
if (m_channelListener) // ### not sure we want to do this if rerunning url...
{
nsCOMPtr<nsIRequest> request = do_QueryInterface(m_mockChannel);
m_channelListener->OnStartRequest(request, m_channelContext);
}
// If we haven't received the greeting yet, we need to make sure we strip
// it out of the input before we start to do useful things...
if (!TestFlag(IMAP_RECEIVED_GREETING))
EstablishServerConnection();
// Step 1: If we have not moved into the authenticated state yet then do so
// by attempting to logon.
if (!DeathSignalReceived() && NS_SUCCEEDED(GetConnectionStatus()) &&
(GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kNonAuthenticated))
{
/* if we got here, the server's greeting should not have been PREAUTH */
if (GetServerStateParser().GetCapabilityFlag() == kCapabilityUndefined)
Capability();
if ( !(GetServerStateParser().GetCapabilityFlag() & (kIMAP4Capability | kIMAP4rev1Capability |
kIMAP4other) ) )
{
if (!DeathSignalReceived() && NS_SUCCEEDED(GetConnectionStatus()))
AlertUserEventUsingName("imapServerNotImap4");
SetConnectionStatus(NS_ERROR_FAILURE); // stop netlib
}
else
{
if ((m_connectionType.Equals("starttls")
&& (m_socketType == nsMsgSocketType::trySTARTTLS
&& (GetServerStateParser().GetCapabilityFlag() & kHasStartTLSCapability)))
|| m_socketType == nsMsgSocketType::alwaysSTARTTLS)
{
StartTLS();
if (GetServerStateParser().LastCommandSuccessful())
{
nsCOMPtr<nsISupports> secInfo;
nsCOMPtr<nsISocketTransport> strans = do_QueryInterface(m_transport, &rv);
if (NS_FAILED(rv))
return false;
rv = strans->GetSecurityInfo(getter_AddRefs(secInfo));
if (NS_SUCCEEDED(rv) && secInfo)
{
nsCOMPtr<nsISSLSocketControl> sslControl = do_QueryInterface(secInfo, &rv);
if (NS_SUCCEEDED(rv) && sslControl)
{
rv = sslControl->StartTLS();
if (NS_SUCCEEDED(rv))
{
if (m_socketType == nsMsgSocketType::trySTARTTLS)
m_imapServerSink->UpdateTrySTARTTLSPref(true);
// force re-issue of "capability", because servers may
// enable other auth features (e.g. remove LOGINDISABLED
// and add AUTH=PLAIN) after we upgraded to SSL.
Capability();
eIMAPCapabilityFlags capabilityFlag = GetServerStateParser().GetCapabilityFlag();
// Courier imap doesn't return STARTTLS capability if we've done
// a STARTTLS! But we need to remember this capability so we'll
// try to use STARTTLS next time.
if (!(capabilityFlag & kHasStartTLSCapability))
{
capabilityFlag |= kHasStartTLSCapability;
GetServerStateParser().SetCapabilityFlag(capabilityFlag);
CommitCapability();
}
}
}
}
if (NS_FAILED(rv))
{
nsAutoCString logLine("STARTTLS negotiation failed. Error 0x");
logLine.AppendInt(static_cast<uint32_t>(rv), 16);
Log("ProcessCurrentURL", nullptr, logLine.get());
if (m_socketType == nsMsgSocketType::alwaysSTARTTLS)
{
SetConnectionStatus(rv); // stop netlib
m_transport->Close(rv);
}
else if (m_socketType == nsMsgSocketType::trySTARTTLS)
m_imapServerSink->UpdateTrySTARTTLSPref(false);
}
}
else if (m_socketType == nsMsgSocketType::alwaysSTARTTLS)
{
SetConnectionStatus(NS_ERROR_FAILURE); // stop netlib
if (m_transport)
m_transport->Close(rv);
}
else if (m_socketType == nsMsgSocketType::trySTARTTLS)
{
// STARTTLS failed, so downgrade socket type
m_imapServerSink->UpdateTrySTARTTLSPref(false);
}
}
else if (m_socketType == nsMsgSocketType::trySTARTTLS)
{
// we didn't know the server supported TLS when we created
// the socket, so we're going to retry with a STARTTLS socket
if (GetServerStateParser().GetCapabilityFlag() & kHasStartTLSCapability)
{
ClearFlag(IMAP_CONNECTION_IS_OPEN);
TellThreadToDie();
SetConnectionStatus(NS_ERROR_FAILURE);
return RetryUrl();
}
else
{
// trySTARTTLS set, but server doesn't have TLS capability,
// so downgrade socket type
m_imapServerSink->UpdateTrySTARTTLSPref(false);
m_socketType = nsMsgSocketType::plain;
}
}
logonFailed = !TryToLogon();
if (m_retryUrlOnError)
return RetryUrl();
}
} // if death signal not received
if (!DeathSignalReceived() && (NS_SUCCEEDED(GetConnectionStatus())))
{
// if the server supports a language extension then we should
// attempt to issue the language extension.
if ( GetServerStateParser().GetCapabilityFlag() & kHasLanguageCapability)
Language();
if (m_runningUrl)
FindMailboxesIfNecessary();
nsImapState imapState = nsIImapUrl::ImapStatusNone;
if (m_runningUrl)
m_runningUrl->GetRequiredImapState(&imapState);
if (imapState == nsIImapUrl::nsImapAuthenticatedState)
ProcessAuthenticatedStateURL();
else // must be a url that requires us to be in the selected state
ProcessSelectedStateURL();
if (m_retryUrlOnError)
return RetryUrl();
// The URL has now been processed
if ((!logonFailed && NS_FAILED(GetConnectionStatus())) ||
DeathSignalReceived())
HandleCurrentUrlError();
}
else if (!logonFailed)
HandleCurrentUrlError();
// if we are set up as a channel, we should notify our channel listener that we are stopping...
// so pass in ourself as the channel and not the underlying socket or file channel the protocol
// happens to be using
if (m_channelListener)
{
nsCOMPtr<nsIRequest> request = do_QueryInterface(m_mockChannel);
NS_ASSERTION(request, "no request");
if (request) {
nsresult status;
request->GetStatus(&status);
if (!GetServerStateParser().LastCommandSuccessful() && NS_SUCCEEDED(status))
status = NS_MSG_ERROR_IMAP_COMMAND_FAILED;
rv = m_channelListener->OnStopRequest(request, m_channelContext, status);
}
}
bool suspendUrl = false;
m_runningUrl->GetMoreHeadersToDownload(&suspendUrl);
if (mailnewsurl && m_imapMailFolderSink)
{
if (logonFailed)
rv = NS_ERROR_FAILURE;
else if (GetServerStateParser().CommandFailed())
rv = NS_MSG_ERROR_IMAP_COMMAND_FAILED;
else
rv = GetConnectionStatus();
// we are done with this url.
m_imapMailFolderSink->SetUrlState(this, mailnewsurl, false, suspendUrl,
rv);
// doom the cache entry
if (NS_FAILED(rv) && DeathSignalReceived() && m_mockChannel)
DoomCacheEntry(mailnewsurl);
}
else
{
// That's seen at times in debug sessions.
NS_WARNING("missing url or sink");
}
// disable timeouts before caching connection.
if (m_transport)
m_transport->SetTimeout(nsISocketTransport::TIMEOUT_READ_WRITE, PR_UINT32_MAX);
SetFlag(IMAP_CLEAN_UP_URL_STATE);
nsCOMPtr <nsISupports> copyState;
if (m_runningUrl)
m_runningUrl->GetCopyState(getter_AddRefs(copyState));
// this is so hokey...we MUST clear any local references to the url
// BEFORE calling ReleaseUrlState
mailnewsurl = nullptr;
if (suspendUrl)
m_imapServerSink->SuspendUrl(m_runningUrl);
// save the imap folder sink since we need it to do the CopyNextStreamMessage
RefPtr<ImapMailFolderSinkProxy> imapMailFolderSink = m_imapMailFolderSink;
// release the url as we are done with it...
ReleaseUrlState(false);
ResetProgressInfo();
ClearFlag(IMAP_CLEAN_UP_URL_STATE);
if (imapMailFolderSink)
{
if (copyState)
{
rv = imapMailFolderSink->CopyNextStreamMessage(GetServerStateParser().LastCommandSuccessful() &&
NS_SUCCEEDED(GetConnectionStatus()),
copyState);
if (NS_FAILED(rv))
MOZ_LOG(IMAP, LogLevel::Info, ("CopyNextStreamMessage failed:%lx\n", rv));
NS_ReleaseOnMainThread(copyState.forget());
}
// we might need this to stick around for IDLE support
m_imapMailFolderSink = imapMailFolderSink;
imapMailFolderSink = nullptr;
}
else
MOZ_LOG(IMAP, LogLevel::Info, ("null imapMailFolderSink\n"));
// now try queued urls, now that we've released this connection.
if (m_imapServerSink)
{
if (NS_SUCCEEDED(GetConnectionStatus()))
rv = m_imapServerSink->LoadNextQueuedUrl(this, &anotherUrlRun);
else // if we don't do this, they'll just sit and spin until
// we run some other url on this server.
{
Log("ProcessCurrentURL", nullptr, "aborting queued urls");
rv = m_imapServerSink->AbortQueuedUrls();
}
}
// if we didn't run another url, release the server sink to
// cut circular refs.
if (!anotherUrlRun)
m_imapServerSink = nullptr;
if (NS_FAILED(GetConnectionStatus()) || !GetServerStateParser().Connected()
|| GetServerStateParser().SyntaxError())
{
if (m_imapServerSink)
m_imapServerSink->RemoveServerConnection(this);
if (!DeathSignalReceived())
{
TellThreadToDie();
}
}
else
{
if (m_imapServerSink)
{
bool shuttingDown;
m_imapServerSink->GetServerShuttingDown(&shuttingDown);
if (shuttingDown)
m_useIdle = false;
}
}
return anotherUrlRun;
}
bool nsImapProtocol::RetryUrl()
{
nsCOMPtr <nsIImapUrl> kungFuGripImapUrl = m_runningUrl;
nsCOMPtr <nsIImapMockChannel> saveMockChannel;
// the mock channel might be null - that's OK.
if (m_imapServerSink)
(void) m_imapServerSink->PrepareToRetryUrl(kungFuGripImapUrl, getter_AddRefs(saveMockChannel));
ReleaseUrlState(true);
if (m_imapServerSink)
{
m_imapServerSink->RemoveServerConnection(this);
m_imapServerSink->RetryUrl(kungFuGripImapUrl, saveMockChannel);
}
return (m_imapServerSink != nullptr); // we're running a url (the same url)
}
// ignoreBadAndNOResponses --> don't throw a error dialog if this command results in a NO or Bad response
// from the server..in other words the command is "exploratory" and we don't really care if it succeeds or fails.
void nsImapProtocol::ParseIMAPandCheckForNewMail(const char* commandString, bool aIgnoreBadAndNOResponses)
{
if (commandString)
GetServerStateParser().ParseIMAPServerResponse(commandString, aIgnoreBadAndNOResponses);
else
GetServerStateParser().ParseIMAPServerResponse(m_currentCommand.get(), aIgnoreBadAndNOResponses);
// **** fix me for new mail biff state *****
}
/////////////////////////////////////////////////////////////////////////////////////////////
// End of nsIStreamListenerSupport
//////////////////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP
nsImapProtocol::GetRunningUrl(nsIURI **result)
{
if (result && m_runningUrl)
return m_runningUrl->QueryInterface(NS_GET_IID(nsIURI), (void**)
result);
else
return NS_ERROR_NULL_POINTER;
}
NS_IMETHODIMP nsImapProtocol::GetRunningImapURL(nsIImapUrl **aImapUrl)
{
if (aImapUrl && m_runningUrl)
return m_runningUrl->QueryInterface(NS_GET_IID(nsIImapUrl), (void**) aImapUrl);
else
return NS_ERROR_NULL_POINTER;
}
/*
* Writes the data contained in dataBuffer into the current output stream. It also informs
* the transport layer that this data is now available for transmission.
* Returns a positive number for success, 0 for failure (not all the bytes were written to the
* stream, etc). We need to make another pass through this file to install an error system (mscott)
*/
nsresult nsImapProtocol::SendData(const char * dataBuffer, bool aSuppressLogging)
{
nsresult rv = NS_ERROR_NULL_POINTER;
if (!m_transport)
{
Log("SendData", nullptr, "clearing IMAP_CONNECTION_IS_OPEN");
// the connection died unexpectedly! so clear the open connection flag
ClearFlag(IMAP_CONNECTION_IS_OPEN);
TellThreadToDie();
SetConnectionStatus(NS_ERROR_FAILURE);
return NS_ERROR_FAILURE;
}
if (dataBuffer && m_outputStream)
{
m_currentCommand = dataBuffer;
if (!aSuppressLogging)
Log("SendData", nullptr, dataBuffer);
else
Log("SendData", nullptr, "Logging suppressed for this command (it probably contained authentication information)");
{
// don't allow someone to close the stream/transport out from under us
// this can happen when the ui thread calls TellThreadToDie.
PR_CEnterMonitor(this);
uint32_t n;
if (m_outputStream)
rv = m_outputStream->Write(dataBuffer, PL_strlen(dataBuffer), &n);
PR_CExitMonitor(this);
}
if (NS_FAILED(rv))
{
Log("SendData", nullptr, "clearing IMAP_CONNECTION_IS_OPEN");
// the connection died unexpectedly! so clear the open connection flag
ClearFlag(IMAP_CONNECTION_IS_OPEN);
TellThreadToDie();
SetConnectionStatus(rv);
if (m_runningUrl && !m_retryUrlOnError)
{
bool alreadyRerunningUrl;
m_runningUrl->GetRerunningUrl(&alreadyRerunningUrl);
if (!alreadyRerunningUrl)
{
m_runningUrl->SetRerunningUrl(true);
m_retryUrlOnError = true;
}
}
}
}
return rv;
}
/////////////////////////////////////////////////////////////////////////////////////////////
// Begin protocol state machine functions...
//////////////////////////////////////////////////////////////////////////////////////////////
// ProcessProtocolState - we override this only so we'll link - it should never get called.
nsresult nsImapProtocol::ProcessProtocolState(nsIURI * url, nsIInputStream * inputStream,
uint64_t sourceOffset, uint32_t length)
{
return NS_OK;
}
class UrlListenerNotifierEvent : public mozilla::Runnable
{
public:
UrlListenerNotifierEvent(nsIMsgMailNewsUrl *aUrl, nsIImapProtocol *aProtocol)
: mUrl(aUrl), mProtocol(aProtocol)
{}
NS_IMETHOD Run()
{
if (mUrl)
{
nsCOMPtr<nsIMsgFolder> folder;
mUrl->GetFolder(getter_AddRefs(folder));
NS_ENSURE_TRUE(folder, NS_OK);
nsCOMPtr<nsIImapMailFolderSink> folderSink(do_QueryInterface(folder));
// This causes the url listener to get OnStart and Stop notifications.
folderSink->SetUrlState(mProtocol, mUrl, true, false, NS_OK);
folderSink->SetUrlState(mProtocol, mUrl, false, false, NS_OK);
}
return NS_OK;
}
private:
nsCOMPtr<nsIMsgMailNewsUrl> mUrl;
nsCOMPtr<nsIImapProtocol> mProtocol;
};
bool nsImapProtocol::TryToRunUrlLocally(nsIURI *aURL, nsISupports *aConsumer)
{
nsresult rv;
nsCOMPtr<nsIImapUrl> imapUrl(do_QueryInterface(aURL, &rv));
NS_ENSURE_SUCCESS(rv, false);
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(aURL);
nsCString messageIdString;
imapUrl->GetListOfMessageIds(messageIdString);
bool useLocalCache = false;
if (!messageIdString.IsEmpty() && !HandlingMultipleMessages(messageIdString))
{
nsImapAction action;
imapUrl->GetImapAction(&action);
nsCOMPtr <nsIMsgFolder> folder;
mailnewsUrl->GetFolder(getter_AddRefs(folder));
NS_ENSURE_TRUE(folder, false);
folder->HasMsgOffline(strtoul(messageIdString.get(), nullptr, 10), &useLocalCache);
mailnewsUrl->SetMsgIsInLocalCache(useLocalCache);
// We're downloading a single message for offline use, and it's
// already offline. So we shouldn't do anything, but we do
// need to notify the url listener.
if (useLocalCache && action == nsIImapUrl::nsImapMsgDownloadForOffline)
{
nsCOMPtr<nsIRunnable> event = new UrlListenerNotifierEvent(mailnewsUrl,
this);
// Post this as an event because it can lead to re-entrant calls to
// LoadNextQueuedUrl if the listener runs a new url.
if (event)
NS_DispatchToCurrentThread(event);
return true;
}
}
if (!useLocalCache)
return false;
nsCOMPtr<nsIImapMockChannel> mockChannel;
imapUrl->GetMockChannel(getter_AddRefs(mockChannel));
if (!mockChannel)
return false;
nsImapMockChannel *imapChannel = static_cast<nsImapMockChannel *>(mockChannel.get());
if (!imapChannel)
return false;
nsCOMPtr <nsILoadGroup> loadGroup;
imapChannel->GetLoadGroup(getter_AddRefs(loadGroup));
if (!loadGroup) // if we don't have one, the url will snag one from the msg window...
mailnewsUrl->GetLoadGroup(getter_AddRefs(loadGroup));
if (loadGroup)
loadGroup->RemoveRequest((nsIRequest *) mockChannel, nullptr /* context isupports */, NS_OK);
if (imapChannel->ReadFromLocalCache())
{
(void) imapChannel->NotifyStartEndReadFromCache(true);
return true;
}
return false;
}
// LoadImapUrl takes a url, initializes all of our url specific data by calling SetupUrl.
// If we don't have a connection yet, we open the connection. Finally, we signal the
// url to run monitor to let the imap main thread loop process the current url (it is waiting
// on this monitor). There is a contract that the imap thread has already been started b4 we
// attempt to load a url....
NS_IMETHODIMP nsImapProtocol::LoadImapUrl(nsIURI * aURL, nsISupports * aConsumer)
{
nsresult rv;
if (aURL)
{
#ifdef DEBUG_bienvenu
printf("loading url %s\n", aURL->GetSpecOrDefault().get());
#endif
if (TryToRunUrlLocally(aURL, aConsumer))
return NS_OK;
m_urlInProgress = true;
m_imapMailFolderSink = nullptr;
rv = SetupWithUrl(aURL, aConsumer);
NS_ASSERTION(NS_SUCCEEDED(rv), "error setting up imap url");
if (NS_FAILED(rv))
return rv;
rv = SetupSinkProxy(); // generate proxies for all of the event sinks in the url
if (NS_FAILED(rv)) // URL can be invalid.
return rv;
m_lastActiveTime = PR_Now();
if (m_transport && m_runningUrl)
{
nsImapAction imapAction;
m_runningUrl->GetImapAction(&imapAction);
// if we're shutting down, and not running the kinds of urls we run at
// shutdown, then this should fail because running urls during
// shutdown will very likely fail and potentially hang.
nsCOMPtr<nsIMsgAccountManager> accountMgr = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
bool shuttingDown = false;
(void) accountMgr->GetShutdownInProgress(&shuttingDown);
if (shuttingDown && imapAction != nsIImapUrl::nsImapExpungeFolder &&
imapAction != nsIImapUrl::nsImapDeleteAllMsgs &&
imapAction != nsIImapUrl::nsImapDeleteFolder)
return NS_ERROR_FAILURE;
// if we're running a select or delete all, do a noop first.
// this should really be in the connection cache code when we know
// we're pulling out a selected state connection, but maybe we
// can get away with this.
m_needNoop = (imapAction == nsIImapUrl::nsImapSelectFolder || imapAction == nsIImapUrl::nsImapDeleteAllMsgs);
// We now have a url to run so signal the monitor for url ready to be processed...
ReentrantMonitorAutoEnter urlReadyMon(m_urlReadyToRunMonitor);
m_nextUrlReadyToRun = true;
urlReadyMon.Notify();
} // if we have an imap url and a transport
else
NS_ASSERTION(false, "missing channel or running url");
} // if we received a url!
else
rv = NS_ERROR_UNEXPECTED;
return rv;
}
NS_IMETHODIMP nsImapProtocol::IsBusy(bool *aIsConnectionBusy,
bool *isInboxConnection)
{
if (!aIsConnectionBusy || !isInboxConnection)
return NS_ERROR_NULL_POINTER;
nsresult rv = NS_OK;
*aIsConnectionBusy = false;
*isInboxConnection = false;
if (!m_transport)
{
// this connection might not be fully set up yet.
rv = NS_ERROR_FAILURE;
}
else
{
if (m_urlInProgress) // do we have a url? That means we're working on it...
*aIsConnectionBusy = true;
if (GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kFolderSelected && GetServerStateParser().GetSelectedMailboxName() &&
PL_strcasecmp(GetServerStateParser().GetSelectedMailboxName(),
"Inbox") == 0)
*isInboxConnection = true;
}
return rv;
}
#define IS_SUBSCRIPTION_RELATED_ACTION(action) (action == nsIImapUrl::nsImapSubscribe\
|| action == nsIImapUrl::nsImapUnsubscribe || action == nsIImapUrl::nsImapDiscoverAllBoxesUrl || action == nsIImapUrl::nsImapListFolder)
// canRunUrl means the connection is not busy, and is in the selected state
// for the desired folder (or authenticated).
// has to wait means it's in the right selected state, but busy.
NS_IMETHODIMP nsImapProtocol::CanHandleUrl(nsIImapUrl * aImapUrl,
bool * aCanRunUrl,
bool * hasToWait)
{
if (!aCanRunUrl || !hasToWait || !aImapUrl)
return NS_ERROR_NULL_POINTER;
nsresult rv = NS_OK;
MutexAutoLock mon(mLock);
*aCanRunUrl = false; // assume guilty until proven otherwise...
*hasToWait = false;
if (DeathSignalReceived())
return NS_ERROR_FAILURE;
bool isBusy = false;
bool isInboxConnection = false;
if (!m_transport)
{
// this connection might not be fully set up yet.
return NS_ERROR_FAILURE;
}
IsBusy(&isBusy, &isInboxConnection);
bool inSelectedState = GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kFolderSelected;
nsAutoCString curSelectedUrlFolderName;
nsAutoCString pendingUrlFolderName;
if (inSelectedState)
curSelectedUrlFolderName = GetServerStateParser().GetSelectedMailboxName();
if (isBusy)
{
nsImapState curUrlImapState;
NS_ASSERTION(m_runningUrl,"isBusy, but no running url.");
if (m_runningUrl)
{
m_runningUrl->GetRequiredImapState(&curUrlImapState);
if (curUrlImapState == nsIImapUrl::nsImapSelectedState)
{
char *folderName = GetFolderPathString();
if (!curSelectedUrlFolderName.Equals(folderName))
pendingUrlFolderName.Assign(folderName);
inSelectedState = true;
PR_Free(folderName);
}
}
}
nsImapState imapState;
nsImapAction actionForProposedUrl;
aImapUrl->GetImapAction(&actionForProposedUrl);
aImapUrl->GetRequiredImapState(&imapState);
// OK, this is a bit of a hack - we're going to pretend that
// these types of urls requires a selected state connection on
// the folder in question. This isn't technically true,
// but we would much rather use that connection for several reasons,
// one is that some UW servers require us to use that connection
// the other is that we don't want to leave a connection dangling in
// the selected state for the deleted folder.
// If we don't find a connection in that selected state,
// we'll fall back to the first free connection.
bool isSelectedStateUrl = imapState == nsIImapUrl::nsImapSelectedState
|| actionForProposedUrl == nsIImapUrl::nsImapDeleteFolder || actionForProposedUrl == nsIImapUrl::nsImapRenameFolder
|| actionForProposedUrl == nsIImapUrl::nsImapMoveFolderHierarchy
|| actionForProposedUrl == nsIImapUrl::nsImapAppendDraftFromFile
|| actionForProposedUrl == nsIImapUrl::nsImapAppendMsgFromFile
|| actionForProposedUrl == nsIImapUrl::nsImapFolderStatus;
nsCOMPtr<nsIMsgMailNewsUrl> msgUrl = do_QueryInterface(aImapUrl);
nsCOMPtr<nsIMsgIncomingServer> server;
rv = msgUrl->GetServer(getter_AddRefs(server));
if (NS_SUCCEEDED(rv))
{
// compare host/user between url and connection.
nsCString urlHostName;
nsCString urlUserName;
rv = server->GetHostName(urlHostName);
NS_ENSURE_SUCCESS(rv, rv);
rv = server->GetUsername(urlUserName);
NS_ENSURE_SUCCESS(rv, rv);
if ((GetImapHostName().IsEmpty() ||
urlHostName.Equals(GetImapHostName(), nsCaseInsensitiveCStringComparator())) &&
(GetImapUserName().IsEmpty() ||
urlUserName.Equals(GetImapUserName(), nsCaseInsensitiveCStringComparator())))
{
if (isSelectedStateUrl)
{
if (inSelectedState)
{
// *** jt - in selected state can only run url with
// matching foldername
char *folderNameForProposedUrl = nullptr;
rv = aImapUrl->CreateServerSourceFolderPathString(
&folderNameForProposedUrl);
if (NS_SUCCEEDED(rv) && folderNameForProposedUrl)
{
bool isInbox =
PL_strcasecmp("Inbox", folderNameForProposedUrl) == 0;
if (!curSelectedUrlFolderName.IsEmpty() || !pendingUrlFolderName.IsEmpty())
{
bool matched = isInbox ?
PL_strcasecmp(curSelectedUrlFolderName.get(),
folderNameForProposedUrl) == 0 :
PL_strcmp(curSelectedUrlFolderName.get(),
folderNameForProposedUrl) == 0;
if (!matched && !pendingUrlFolderName.IsEmpty())
{
matched = isInbox ?
PL_strcasecmp(pendingUrlFolderName.get(),
folderNameForProposedUrl) == 0 :
PL_strcmp(pendingUrlFolderName.get(),
folderNameForProposedUrl) == 0;
}
if (matched)
{
if (isBusy)
*hasToWait = true;
else
*aCanRunUrl = true;
}
}
}
MOZ_LOG(IMAP, LogLevel::Debug,
("proposed url = %s folder for connection %s has To Wait = %s can run = %s",
folderNameForProposedUrl, curSelectedUrlFolderName.get(),
(*hasToWait) ? "TRUE" : "FALSE", (*aCanRunUrl) ? "TRUE" : "FALSE"));
PR_FREEIF(folderNameForProposedUrl);
}
}
else // *** jt - an authenticated state url can be run in either
// authenticated or selected state
{
nsImapAction actionForRunningUrl;
// If proposed url is subscription related, and we are currently running
// a subscription url, then we want to queue the proposed url after the current url.
// Otherwise, we can run this url if we're not busy.
// If we never find a running subscription-related url, the caller will
// just use whatever free connection it can find, which is what we want.
if (IS_SUBSCRIPTION_RELATED_ACTION(actionForProposedUrl))
{
if (isBusy && m_runningUrl)
{
m_runningUrl->GetImapAction(&actionForRunningUrl);
if (IS_SUBSCRIPTION_RELATED_ACTION(actionForRunningUrl))
{
*aCanRunUrl = false;
*hasToWait = true;
}
}
}
else
{
if (!isBusy)
*aCanRunUrl = true;
}
}
}
}
return rv;
}
// Command tag handling stuff
void nsImapProtocol::IncrementCommandTagNumber()
{
sprintf(m_currentServerCommandTag, "%u", ++m_currentServerCommandTagNumber);
}
const char *nsImapProtocol::GetServerCommandTag()
{
return m_currentServerCommandTag;
}
void nsImapProtocol::ProcessSelectedStateURL()
{
nsCString mailboxName;
bool bMessageIdsAreUids = true;
bool moreHeadersToDownload;
imapMessageFlagsType msgFlags = 0;
nsCString urlHost;
// this can't fail, can it?
nsresult res;
res = m_runningUrl->GetImapAction(&m_imapAction);
m_runningUrl->MessageIdsAreUids(&bMessageIdsAreUids);
m_runningUrl->GetMsgFlags(&msgFlags);
m_runningUrl->GetMoreHeadersToDownload(&moreHeadersToDownload);
res = CreateServerSourceFolderPathString(getter_Copies(mailboxName));
if (NS_FAILED(res))
Log("ProcessSelectedStateURL", nullptr, "error getting source folder path string");
if (NS_SUCCEEDED(res) && !DeathSignalReceived())
{
bool selectIssued = false;
if (GetServerStateParser().GetIMAPstate() == nsImapServerResponseParser::kFolderSelected)
{
if (GetServerStateParser().GetSelectedMailboxName() &&
PL_strcmp(GetServerStateParser().GetSelectedMailboxName(),
mailboxName.get()))
{ // we are selected in another folder
if (m_closeNeededBeforeSelect)
Close();
if (GetServerStateParser().LastCommandSuccessful())
{
selectIssued = true;
SelectMailbox(mailboxName.get());
}
}
else if (!GetServerStateParser().GetSelectedMailboxName())
{ // why are we in the selected state with no box name?
SelectMailbox(mailboxName.get());
selectIssued = true;
}
else if (moreHeadersToDownload && m_imapMailFolderSink) // we need to fetch older headers
{
nsMsgKey *msgIdList = nullptr;
uint32_t msgCount = 0;
bool more;
m_imapMailFolderSink->GetMsgHdrsToDownload(&more, &m_progressCount,
&msgCount, &msgIdList);
if (msgIdList)
{
FolderHeaderDump(msgIdList, msgCount);
NS_Free(msgIdList);
m_runningUrl->SetMoreHeadersToDownload(more);
// We're going to be re-running this url.
if (more)
m_runningUrl->SetRerunningUrl(true);
}
HeaderFetchCompleted();
}
else
{
// get new message counts, if any, from server
if (m_needNoop)
{
// For some IMAP servers, to detect new email we must send imap
// SELECT even if already SELECTed on the same mailbox. For other
// servers that simply don't support IDLE, doing select here will
// cause emails to be properly marked "read" after they have been
// read in another email client.
if (m_forceSelect)
{
SelectMailbox(mailboxName.get());
selectIssued = true;
}
m_noopCount++;
if ((gPromoteNoopToCheckCount > 0 && (m_noopCount % gPromoteNoopToCheckCount) == 0) ||
CheckNeeded())
Check();
else
Noop(); // I think this is needed when we're using a cached connection
m_needNoop = false;
}
}
}
else
{
// go to selected state
SelectMailbox(mailboxName.get());
selectIssued = GetServerStateParser().LastCommandSuccessful();
}
if (selectIssued)
RefreshACLForFolderIfNecessary(mailboxName.get());
bool uidValidityOk = true;
if (GetServerStateParser().LastCommandSuccessful() && selectIssued &&
(m_imapAction != nsIImapUrl::nsImapSelectFolder) && (m_imapAction != nsIImapUrl::nsImapLiteSelectFolder))
{
// error on the side of caution, if the fe event fails to set uidStruct->returnValidity, then assume that UIDVALIDITY
// did not roll. This is a common case event for attachments that are fetched within a browser context.
if (!DeathSignalReceived())
uidValidityOk = m_uidValidity == kUidUnknown ||
m_uidValidity == GetServerStateParser().FolderUID();
}
if (!uidValidityOk)
Log("ProcessSelectedStateURL", nullptr, "uid validity not ok");
if (GetServerStateParser().LastCommandSuccessful() && !DeathSignalReceived() && (uidValidityOk || m_imapAction == nsIImapUrl::nsImapDeleteAllMsgs))
{
if (GetServerStateParser().CurrentFolderReadOnly())
{
Log("ProcessSelectedStateURL", nullptr, "current folder read only");
if (m_imapAction == nsIImapUrl::nsImapAddMsgFlags ||
m_imapAction == nsIImapUrl::nsImapSubtractMsgFlags)
{
bool canChangeFlag = false;
if (GetServerStateParser().ServerHasACLCapability() && m_imapMailFolderSink)
{
uint32_t aclFlags = 0;
if (NS_SUCCEEDED(m_imapMailFolderSink->GetAclFlags(&aclFlags))
&& aclFlags != 0) // make sure we have some acl flags
canChangeFlag = ((msgFlags & kImapMsgSeenFlag) && (aclFlags & IMAP_ACL_STORE_SEEN_FLAG));
}
else
canChangeFlag = (GetServerStateParser().SettablePermanentFlags() & msgFlags) == msgFlags;
if (!canChangeFlag)
return;
}
if (m_imapAction == nsIImapUrl::nsImapExpungeFolder || m_imapAction == nsIImapUrl::nsImapDeleteMsg ||
m_imapAction == nsIImapUrl::nsImapDeleteAllMsgs)
return;
}
switch (m_imapAction)
{
case nsIImapUrl::nsImapLiteSelectFolder:
if (GetServerStateParser().LastCommandSuccessful() &&
m_imapMailFolderSink && !moreHeadersToDownload)
{
m_imapMailFolderSink->SetUidValidity(GetServerStateParser().FolderUID());
ProcessMailboxUpdate(false); // handle uidvalidity change
}
break;
case nsIImapUrl::nsImapSaveMessageToDisk:
case nsIImapUrl::nsImapMsgFetch:
case nsIImapUrl::nsImapMsgFetchPeek:
case nsIImapUrl::nsImapMsgDownloadForOffline:
case nsIImapUrl::nsImapMsgPreview:
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
// we don't want to send the flags back in a group
if (HandlingMultipleMessages(messageIdString) || m_imapAction == nsIImapUrl::nsImapMsgDownloadForOffline
|| m_imapAction == nsIImapUrl::nsImapMsgPreview)
{
// multiple messages, fetch them all
SetProgressString("imapFolderReceivingMessageOf2");
m_progressIndex = 0;
m_progressCount = CountMessagesInIdString(messageIdString.get());
// we need to set this so we'll get the msg from the memory cache.
if (m_imapAction == nsIImapUrl::nsImapMsgFetchPeek)
SetContentModified(IMAP_CONTENT_NOT_MODIFIED);
FetchMessage(messageIdString,
(m_imapAction == nsIImapUrl::nsImapMsgPreview)
? kBodyStart : kEveryThingRFC822Peek);
if (m_imapAction == nsIImapUrl::nsImapMsgPreview)
HeaderFetchCompleted();
SetProgressString(nullptr);
}
else
{
// A single message ID
nsIMAPeFetchFields whatToFetch = kEveryThingRFC822;
if(m_imapAction == nsIImapUrl::nsImapMsgFetchPeek)
whatToFetch = kEveryThingRFC822Peek;
// First, let's see if we're requesting a specific MIME part
char *imappart = nullptr;
m_runningUrl->GetImapPartToFetch(&imappart);
if (imappart)
{
if (bMessageIdsAreUids)
{
// We actually want a specific MIME part of the message.
// The Body Shell will generate it, even though we haven't downloaded it yet.
IMAP_ContentModifiedType modType = GetShowAttachmentsInline() ?
IMAP_CONTENT_MODIFIED_VIEW_INLINE :
IMAP_CONTENT_MODIFIED_VIEW_AS_LINKS ;
RefPtr<nsIMAPBodyShell> foundShell;
res = m_hostSessionList->FindShellInCacheForHost(GetImapServerKey(),
GetServerStateParser().GetSelectedMailboxName(),
messageIdString.get(), modType, getter_AddRefs(foundShell));
if (!foundShell)
{
// The shell wasn't in the cache. Deal with this case later.
Log("SHELL",NULL,"Loading part, shell not found in cache!");
//MOZ_LOG(IMAP, out, ("BODYSHELL: Loading part, shell not found in cache!"));
// The parser will extract the part number from the current URL.
SetContentModified(modType);
Bodystructure(messageIdString, bMessageIdsAreUids);
}
else
{
Log("SHELL", NULL, "Loading Part, using cached shell.");
//MOZ_LOG(IMAP, out, ("BODYSHELL: Loading part, using cached shell."));
SetContentModified(modType);
foundShell->SetConnection(this);
GetServerStateParser().UseCachedShell(foundShell);
//Set the current uid in server state parser (in case it was used for new mail msgs earlier).
GetServerStateParser().SetCurrentResponseUID(strtoul(messageIdString.get(), nullptr, 10));
foundShell->Generate(imappart);
GetServerStateParser().UseCachedShell(NULL);
}
}
else
{
// Message IDs are not UIDs.
NS_ASSERTION(false, "message ids aren't uids");
}
PR_Free(imappart);
}
else
{
// downloading a single message: try to do it by bodystructure, and/or do it by chunks
uint32_t messageSize = GetMessageSize(messageIdString.get(), bMessageIdsAreUids);
// We need to check the format_out bits to see if we are allowed to leave out parts,
// or if we are required to get the whole thing. Some instances where we are allowed
// to do it by parts: when viewing a message, replying to a message, or viewing its source
// Some times when we're NOT allowed: when forwarding a message, saving it, moving it, etc.
// need to set a flag in the url, I guess, equiv to allow_content_changed.
bool allowedToBreakApart = true; // (ce && !DeathSignalReceived()) ? ce->URL_s->allow_content_change : false;
bool mimePartSelectorDetected;
bool urlOKToFetchByParts = false;
m_runningUrl->GetMimePartSelectorDetected(&mimePartSelectorDetected);
m_runningUrl->GetFetchPartsOnDemand(&urlOKToFetchByParts);
#ifdef PR_LOGGING
{
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsurl = do_QueryInterface(m_runningUrl);
nsAutoCString urlSpec;
if (mailnewsurl)
urlSpec = mailnewsurl->GetSpecOrDefault();
MOZ_LOG(IMAP, LogLevel::Debug,
("SHELL: URL %s, OKToFetchByParts %d, allowedToBreakApart %d, ShouldFetchAllParts %d",
urlSpec.get(), urlOKToFetchByParts, allowedToBreakApart,
GetShouldFetchAllParts()));
}
#endif
if (urlOKToFetchByParts &&
allowedToBreakApart &&
!GetShouldFetchAllParts() &&
GetServerStateParser().ServerHasIMAP4Rev1Capability() /* &&
!mimePartSelectorDetected */) // if a ?part=, don't do BS.
{
// OK, we're doing bodystructure
// Before fetching the bodystructure, let's check our body shell cache to see if
// we already have it around.
RefPtr<nsIMAPBodyShell> foundShell;
IMAP_ContentModifiedType modType = GetShowAttachmentsInline() ?
IMAP_CONTENT_MODIFIED_VIEW_INLINE :
IMAP_CONTENT_MODIFIED_VIEW_AS_LINKS ;
bool wasStoringMsgOffline;
m_runningUrl->GetStoreResultsOffline(&wasStoringMsgOffline);
m_runningUrl->SetStoreOfflineOnFallback(wasStoringMsgOffline);
m_runningUrl->SetStoreResultsOffline(false);
SetContentModified(modType); // This will be looked at by the cache
if (bMessageIdsAreUids)
{
res = m_hostSessionList->FindShellInCacheForHost(GetImapServerKey(),
GetServerStateParser().GetSelectedMailboxName(),
messageIdString.get(), modType, getter_AddRefs(foundShell));
if (foundShell)
{
Log("SHELL",NULL,"Loading message, using cached shell.");
//MOZ_LOG(IMAP, out, ("BODYSHELL: Loading message, using cached shell."));
foundShell->SetConnection(this);
GetServerStateParser().UseCachedShell(foundShell);
//Set the current uid in server state parser (in case it was used for new mail msgs earlier).
GetServerStateParser().SetCurrentResponseUID(strtoul(messageIdString.get(), nullptr, 10));
foundShell->Generate(NULL);
GetServerStateParser().UseCachedShell(NULL);
}
}
if (!foundShell)
Bodystructure(messageIdString, bMessageIdsAreUids);
}
else
{
// Not doing bodystructure. Fetch the whole thing, and try to do
// it in chunks.
SetContentModified(IMAP_CONTENT_NOT_MODIFIED);
FetchTryChunking(messageIdString, whatToFetch,
bMessageIdsAreUids, NULL, messageSize, true);
}
}
if (GetServerStateParser().LastCommandSuccessful()
&& m_imapAction != nsIImapUrl::nsImapMsgPreview
&& m_imapAction != nsIImapUrl::nsImapMsgFetchPeek)
{
uint32_t uid = strtoul(messageIdString.get(), nullptr, 10);
int32_t index;
bool foundIt;
imapMessageFlagsType flags = m_flagState->GetMessageFlagsFromUID(uid, &foundIt, &index);
if (foundIt)
{
flags |= kImapMsgSeenFlag;
m_flagState->SetMessageFlags(index, flags);
}
}
}
}
break;
case nsIImapUrl::nsImapExpungeFolder:
Expunge();
// note fall through to next cases.
MOZ_FALLTHROUGH;
case nsIImapUrl::nsImapSelectFolder:
case nsIImapUrl::nsImapSelectNoopFolder:
if (!moreHeadersToDownload)
ProcessMailboxUpdate(true);
break;
case nsIImapUrl::nsImapMsgHeader:
{
nsCString messageIds;
m_runningUrl->GetListOfMessageIds(messageIds);
FetchMessage(messageIds,
kHeadersRFC822andUid);
// if we explicitly ask for headers, as opposed to getting them as a result
// of selecting the folder, or biff, send the headerFetchCompleted notification
// to flush out the header cache.
HeaderFetchCompleted();
}
break;
case nsIImapUrl::nsImapSearch:
{
nsAutoCString searchCriteriaString;
m_runningUrl->CreateSearchCriteriaString(getter_Copies(searchCriteriaString));
Search(searchCriteriaString.get(), bMessageIdsAreUids);
// drop the results on the floor for now
}
break;
case nsIImapUrl::nsImapUserDefinedMsgCommand:
{
nsCString messageIdString;
nsCString command;
m_runningUrl->GetCommand(command);
m_runningUrl->GetListOfMessageIds(messageIdString);
IssueUserDefinedMsgCommand(command.get(), messageIdString.get());
}
break;
case nsIImapUrl::nsImapUserDefinedFetchAttribute:
{
nsCString messageIdString;
nsCString attribute;
m_runningUrl->GetCustomAttributeToFetch(attribute);
m_runningUrl->GetListOfMessageIds(messageIdString);
FetchMsgAttribute(messageIdString, attribute);
}
break;
case nsIImapUrl::nsImapMsgStoreCustomKeywords:
{
// if the server doesn't support user defined flags, don't try to set them.
uint16_t userFlags;
GetSupportedUserFlags(&userFlags);
if (! (userFlags & kImapMsgSupportUserFlag))
break;
nsCString messageIdString;
nsCString addFlags;
nsCString subtractFlags;
m_runningUrl->GetListOfMessageIds(messageIdString);
m_runningUrl->GetCustomAddFlags(addFlags);
m_runningUrl->GetCustomSubtractFlags(subtractFlags);
if (!addFlags.IsEmpty())
{
nsAutoCString storeString("+FLAGS (");
storeString.Append(addFlags);
storeString.Append(")");
Store(messageIdString, storeString.get(), true);
}
if (!subtractFlags.IsEmpty())
{
nsAutoCString storeString("-FLAGS (");
storeString.Append(subtractFlags);
storeString.Append(")");
Store(messageIdString, storeString.get(), true);
}
}
break;
case nsIImapUrl::nsImapDeleteMsg:
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
ProgressEventFunctionUsingName(HandlingMultipleMessages(messageIdString) ?
"imapDeletingMessages" :
"imapDeletingMessage");
Store(messageIdString, "+FLAGS (\\Deleted)", bMessageIdsAreUids);
if (GetServerStateParser().LastCommandSuccessful())
{
//delete_message_struct *deleteMsg = (delete_message_struct *) PR_Malloc (sizeof(delete_message_struct));
// convert name back from utf7
nsCString canonicalName;
const char *selectedMailboxName = GetServerStateParser().GetSelectedMailboxName();
if (selectedMailboxName)
{
m_runningUrl->AllocateCanonicalPath(selectedMailboxName,
kOnlineHierarchySeparatorUnknown, getter_Copies(canonicalName));
}
if (m_imapMessageSink)
m_imapMessageSink->NotifyMessageDeleted(canonicalName.get(), false, messageIdString.get());
// notice we don't wait for this to finish...
}
else
HandleMemoryFailure();
}
break;
case nsIImapUrl::nsImapDeleteFolderAndMsgs:
DeleteFolderAndMsgs(mailboxName.get());
break;
case nsIImapUrl::nsImapDeleteAllMsgs:
{
uint32_t numberOfMessages = GetServerStateParser().NumberOfMessages();
if (numberOfMessages)
{
Store(NS_LITERAL_CSTRING("1:*"), "+FLAGS.SILENT (\\Deleted)",
false); // use sequence #'s
if (GetServerStateParser().LastCommandSuccessful())
Expunge(); // expunge messages with deleted flag
if (GetServerStateParser().LastCommandSuccessful())
{
// convert name back from utf7
nsCString canonicalName;
const char *selectedMailboxName = GetServerStateParser().GetSelectedMailboxName();
if (selectedMailboxName )
{
m_runningUrl->AllocateCanonicalPath(selectedMailboxName,
kOnlineHierarchySeparatorUnknown, getter_Copies(canonicalName));
}
if (m_imapMessageSink)
m_imapMessageSink->NotifyMessageDeleted(canonicalName.get(), true, nullptr);
}
}
bool deleteSelf = false;
DeleteSubFolders(mailboxName.get(), deleteSelf); // don't delete self
}
break;
case nsIImapUrl::nsImapAppendDraftFromFile:
{
OnAppendMsgFromFile();
}
break;
case nsIImapUrl::nsImapAddMsgFlags:
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
ProcessStoreFlags(messageIdString, bMessageIdsAreUids,
msgFlags, true);
}
break;
case nsIImapUrl::nsImapSubtractMsgFlags:
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
ProcessStoreFlags(messageIdString, bMessageIdsAreUids,
msgFlags, false);
}
break;
case nsIImapUrl::nsImapSetMsgFlags:
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
ProcessStoreFlags(messageIdString, bMessageIdsAreUids,
msgFlags, true);
ProcessStoreFlags(messageIdString, bMessageIdsAreUids,
~msgFlags, false);
}
break;
case nsIImapUrl::nsImapBiff:
PeriodicBiff();
break;
case nsIImapUrl::nsImapOnlineCopy:
case nsIImapUrl::nsImapOnlineMove:
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
char *destinationMailbox = OnCreateServerDestinationFolderPathString();
if (destinationMailbox)
{
if (m_imapAction == nsIImapUrl::nsImapOnlineMove)
{
if (HandlingMultipleMessages(messageIdString))
ProgressEventFunctionUsingNameWithString("imapMovingMessages", destinationMailbox);
else
ProgressEventFunctionUsingNameWithString("imapMovingMessage", destinationMailbox);
}
else {
if (HandlingMultipleMessages(messageIdString))
ProgressEventFunctionUsingNameWithString("imapCopyingMessages", destinationMailbox);
else
ProgressEventFunctionUsingNameWithString("imapCopyingMessage", destinationMailbox);
}
Copy(messageIdString.get(), destinationMailbox, bMessageIdsAreUids);
PR_FREEIF( destinationMailbox);
ImapOnlineCopyState copyState;
if (DeathSignalReceived())
copyState = ImapOnlineCopyStateType::kInterruptedState;
else
copyState = GetServerStateParser().LastCommandSuccessful() ?
(ImapOnlineCopyState) ImapOnlineCopyStateType::kSuccessfulCopy :
(ImapOnlineCopyState) ImapOnlineCopyStateType::kFailedCopy;
if (m_imapMailFolderSink)
m_imapMailFolderSink->OnlineCopyCompleted(this, copyState);
// Don't mark message 'Deleted' for AOL servers or standard imap servers
// that support MOVE since we already issued an 'xaol-move' or 'move' command.
if (GetServerStateParser().LastCommandSuccessful() &&
(m_imapAction == nsIImapUrl::nsImapOnlineMove) &&
!(GetServerStateParser().ServerIsAOLServer() ||
GetServerStateParser().GetCapabilityFlag() & kHasMoveCapability))
{
// Simulate MOVE for servers that don't support MOVE: do COPY-DELETE-EXPUNGE.
Store(messageIdString, "+FLAGS (\\Deleted \\Seen)",
bMessageIdsAreUids);
bool storeSuccessful = GetServerStateParser().LastCommandSuccessful();
if (storeSuccessful)
{
if(gExpungeAfterDelete)
{
// This will expunge all emails marked as deleted in mailbox,
// not just the ones marked as deleted above.
Expunge();
}
else
{
// Check if UIDPLUS capable so we can just expunge emails we just
// copied and marked as deleted. This prevents expunging emails
// that other clients may have marked as deleted in the mailbox
// and don't want them to disappear.
// Only do UidExpunge() when user selected delete method is "Move
// it to this folder" or "Remove it immediately", not when the
// delete method is "Just mark it as deleted".
if (!GetShowDeletedMessages() &&
(GetServerStateParser().GetCapabilityFlag() & kUidplusCapability))
{
UidExpunge(messageIdString);
}
}
}
if (m_imapMailFolderSink)
{
copyState = storeSuccessful ? (ImapOnlineCopyState) ImapOnlineCopyStateType::kSuccessfulDelete
: (ImapOnlineCopyState) ImapOnlineCopyStateType::kFailedDelete;
m_imapMailFolderSink->OnlineCopyCompleted(this, copyState);
}
}
}
else
HandleMemoryFailure();
}
break;
case nsIImapUrl::nsImapOnlineToOfflineCopy:
case nsIImapUrl::nsImapOnlineToOfflineMove:
{
nsCString messageIdString;
nsresult rv = m_runningUrl->GetListOfMessageIds(messageIdString);
if (NS_SUCCEEDED(rv))
{
SetProgressString("imapFolderReceivingMessageOf2");
m_progressIndex = 0;
m_progressCount = CountMessagesInIdString(messageIdString.get());
FetchMessage(messageIdString, kEveryThingRFC822Peek);
SetProgressString(nullptr);
if (m_imapMailFolderSink)
{
ImapOnlineCopyState copyStatus;
copyStatus = GetServerStateParser().LastCommandSuccessful() ?
ImapOnlineCopyStateType::kSuccessfulCopy : ImapOnlineCopyStateType::kFailedCopy;
m_imapMailFolderSink->OnlineCopyCompleted(this, copyStatus);
if (GetServerStateParser().LastCommandSuccessful() &&
(m_imapAction == nsIImapUrl::nsImapOnlineToOfflineMove))
{
Store(messageIdString, "+FLAGS (\\Deleted \\Seen)",bMessageIdsAreUids);
if (GetServerStateParser().LastCommandSuccessful())
{
copyStatus = ImapOnlineCopyStateType::kSuccessfulDelete;
if (gExpungeAfterDelete)
Expunge();
}
else
copyStatus = ImapOnlineCopyStateType::kFailedDelete;
m_imapMailFolderSink->OnlineCopyCompleted(this, copyStatus);
}
}
}
else
HandleMemoryFailure();
}
break;
default:
if (GetServerStateParser().LastCommandSuccessful() && !uidValidityOk)
ProcessMailboxUpdate(false); // handle uidvalidity change
break;
}
}
}
else if (!DeathSignalReceived())
HandleMemoryFailure();
}
nsresult nsImapProtocol::BeginMessageDownLoad(
uint32_t total_message_size, // for user, headers and body
const char *content_type)
{
nsresult rv = NS_OK;
char *sizeString = PR_smprintf("OPEN Size: %ld", total_message_size);
Log("STREAM",sizeString,"Begin Message Download Stream");
PR_Free(sizeString);
// start counting how many bytes we see in this message after all transformations
m_bytesToChannel = 0;
if (content_type)
{
m_fromHeaderSeen = false;
if (GetServerStateParser().GetDownloadingHeaders())
{
// if we get multiple calls to BeginMessageDownload w/o intervening
// calls to NormalEndMessageDownload or Abort, then we're just
// going to fake a NormalMessageEndDownload. This will most likely
// cause an empty header to get written to the db, and the user
// will have to delete the empty header themselves, which
// should remove the message from the server as well.
if (m_curHdrInfo)
NormalMessageEndDownload();
if (!m_curHdrInfo)
m_curHdrInfo = m_hdrDownloadCache->StartNewHdr();
if (m_curHdrInfo)
m_curHdrInfo->SetMsgSize(total_message_size);
return NS_OK;
}
// if we have a mock channel, that means we have a channel listener who wants the
// message. So set up a pipe. We'll write the messsage into one end of the pipe
// and they will read it out of the other end.
else if (m_channelListener)
{
// create a pipe to pump the message into...the output will go to whoever
// is consuming the message display
// we create an "infinite" pipe in case we get extremely long lines from the imap server,
// and the consumer is waiting for a whole line
nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");
rv = pipe->Init(false, false, 4096, PR_UINT32_MAX);
NS_ENSURE_SUCCESS(rv, rv);
// These always succeed because the pipe is initialized above.
MOZ_ALWAYS_SUCCEEDS(pipe->GetInputStream(getter_AddRefs(m_channelInputStream)));
MOZ_ALWAYS_SUCCEEDS(pipe->GetOutputStream(getter_AddRefs(m_channelOutputStream)));
}
// else, if we are saving the message to disk!
else if (m_imapMessageSink /* && m_imapAction == nsIImapUrl::nsImapSaveMessageToDisk */)
{
// we get here when download the inbox for offline use
nsCOMPtr<nsIFile> file;
bool addDummyEnvelope = true;
nsCOMPtr<nsIMsgMessageUrl> msgurl = do_QueryInterface(m_runningUrl);
msgurl->GetMessageFile(getter_AddRefs(file));
msgurl->GetAddDummyEnvelope(&addDummyEnvelope);
if (file)
rv = m_imapMessageSink->SetupMsgWriteStream(file, addDummyEnvelope);
}
if (m_imapMailFolderSink && m_runningUrl)
{
nsCOMPtr <nsISupports> copyState;
if (m_runningUrl)
{
m_runningUrl->GetCopyState(getter_AddRefs(copyState));
if (copyState) // only need this notification during copy
{
nsCOMPtr<nsIMsgMailNewsUrl> mailurl = do_QueryInterface(m_runningUrl);
m_imapMailFolderSink->StartMessage(mailurl);
}
}
}
}
else
HandleMemoryFailure();
return rv;
}
void
nsImapProtocol::GetShouldDownloadAllHeaders(bool *aResult)
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->GetShouldDownloadAllHeaders(aResult);
}
void
nsImapProtocol::GetArbitraryHeadersToDownload(nsCString &aResult)
{
if (m_imapServerSink)
m_imapServerSink->GetArbitraryHeaders(aResult);
}
void
nsImapProtocol::AdjustChunkSize()
{
int32_t deltaInSeconds;
PRTime2Seconds(m_endTime - m_startTime, &deltaInSeconds);
m_trackingTime = false;
if (deltaInSeconds < 0)
return; // bogus for some reason
if (deltaInSeconds <= m_tooFastTime && m_curFetchSize >= m_chunkSize)
{
m_chunkSize += m_chunkAddSize;
m_chunkThreshold = m_chunkSize + (m_chunkSize / 2);
// we used to have a max for the chunk size - I don't think that's needed.
}
else if (deltaInSeconds <= m_idealTime)
return;
else
{
if (m_chunkSize > m_chunkStartSize)
m_chunkSize = m_chunkStartSize;
else if (m_chunkSize > (m_chunkAddSize * 2))
m_chunkSize -= m_chunkAddSize;
m_chunkThreshold = m_chunkSize + (m_chunkSize / 2);
}
// remember these new values globally so new connections
// can take advantage of them.
if (gChunkSize != m_chunkSize)
{
// will cause chunk size pref to be written in CloseStream.
gChunkSizeDirty = true;
gChunkSize = m_chunkSize;
gChunkThreshold = m_chunkThreshold;
}
}
// authenticated state commands
// escape any backslashes or quotes. Backslashes are used a lot with our NT server
void nsImapProtocol::CreateEscapedMailboxName(const char *rawName, nsCString &escapedName)
{
escapedName.Assign(rawName);
for (int32_t strIndex = 0; *rawName; strIndex++)
{
char currentChar = *rawName++;
if ((currentChar == '\\') || (currentChar == '\"'))
escapedName.Insert('\\', strIndex++);
}
}
void nsImapProtocol::SelectMailbox(const char *mailboxName)
{
ProgressEventFunctionUsingNameWithString("imapStatusSelectingMailbox", mailboxName);
IncrementCommandTagNumber();
m_closeNeededBeforeSelect = false; // initial value
GetServerStateParser().ResetFlagInfo();
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
nsCString commandBuffer(GetServerCommandTag());
commandBuffer.Append(" select \"");
commandBuffer.Append(escapedName.get());
commandBuffer.Append("\"");
if (UseCondStore())
commandBuffer.Append(" (CONDSTORE)");
commandBuffer.Append(CRLF);
nsresult res;
res = SendData(commandBuffer.get());
if (NS_FAILED(res)) return;
ParseIMAPandCheckForNewMail();
int32_t numOfMessagesInFlagState = 0;
nsImapAction imapAction;
m_flagState->GetNumberOfMessages(&numOfMessagesInFlagState);
res = m_runningUrl->GetImapAction(&imapAction);
// if we've selected a mailbox, and we're not going to do an update because of the
// url type, but don't have the flags, go get them!
if (GetServerStateParser().LastCommandSuccessful() && NS_SUCCEEDED(res) &&
imapAction != nsIImapUrl::nsImapSelectFolder && imapAction != nsIImapUrl::nsImapExpungeFolder
&& imapAction != nsIImapUrl::nsImapLiteSelectFolder &&
imapAction != nsIImapUrl::nsImapDeleteAllMsgs &&
((GetServerStateParser().NumberOfMessages() != numOfMessagesInFlagState) && (numOfMessagesInFlagState == 0)))
{
ProcessMailboxUpdate(false);
}
}
// Please call only with a single message ID
void nsImapProtocol::Bodystructure(const nsCString &messageId, bool idIsUid)
{
IncrementCommandTagNumber();
nsCString commandString(GetServerCommandTag());
if (idIsUid)
commandString.Append(" UID");
commandString.Append(" fetch ");
commandString.Append(messageId);
commandString.Append(" (BODYSTRUCTURE)" CRLF);
nsresult rv = SendData(commandString.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(commandString.get());
}
void nsImapProtocol::PipelinedFetchMessageParts(const char *uid, nsIMAPMessagePartIDArray *parts)
{
// assumes no chunking
// build up a string to fetch
nsCString stringToFetch, what;
uint32_t currentPartNum = 0;
while ((parts->GetNumParts() > currentPartNum) && !DeathSignalReceived())
{
nsIMAPMessagePartID *currentPart = parts->GetPart(currentPartNum);
if (currentPart)
{
// Do things here depending on the type of message part
// Append it to the fetch string
if (currentPartNum > 0)
stringToFetch.Append(" ");
switch (currentPart->GetFields())
{
case kMIMEHeader:
what = "BODY.PEEK[";
what.Append(currentPart->GetPartNumberString());
what.Append(".MIME]");
stringToFetch.Append(what);
break;
case kRFC822HeadersOnly:
if (currentPart->GetPartNumberString())
{
what = "BODY.PEEK[";
what.Append(currentPart->GetPartNumberString());
what.Append(".HEADER]");
stringToFetch.Append(what);
}
else
{
// headers for the top-level message
stringToFetch.Append("BODY.PEEK[HEADER]");
}
break;
default:
NS_ASSERTION(false, "we should only be pipelining MIME headers and Message headers");
break;
}
}
currentPartNum++;
}
// Run the single, pipelined fetch command
if ((parts->GetNumParts() > 0) && !DeathSignalReceived() && !GetPseudoInterrupted() && stringToFetch.get())
{
IncrementCommandTagNumber();
nsCString commandString(GetServerCommandTag());
commandString.Append(" UID fetch ");
commandString.Append(uid, 10);
commandString.Append(" (");
commandString.Append(stringToFetch);
commandString.Append(")" CRLF);
nsresult rv = SendData(commandString.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(commandString.get());
}
}
void nsImapProtocol::FetchMsgAttribute(const nsCString &messageIds, const nsCString &attribute)
{
IncrementCommandTagNumber();
nsAutoCString commandString (GetServerCommandTag());
commandString.Append(" UID fetch ");
commandString.Append(messageIds);
commandString.Append(" (");
commandString.Append(attribute);
commandString.Append(")" CRLF);
nsresult rv = SendData(commandString.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(commandString.get());
GetServerStateParser().SetFetchingFlags(false);
// Always clear this flag after every fetch.
m_fetchingWholeMessage = false;
}
// this routine is used to fetch a message or messages, or headers for a
// message...
void nsImapProtocol::FallbackToFetchWholeMsg(const nsCString &messageId, uint32_t messageSize)
{
if (m_imapMessageSink && m_runningUrl)
{
bool shouldStoreMsgOffline;
m_runningUrl->GetStoreOfflineOnFallback(&shouldStoreMsgOffline);
m_runningUrl->SetStoreResultsOffline(shouldStoreMsgOffline);
}
FetchTryChunking(messageId,
m_imapAction == nsIImapUrl::nsImapMsgFetchPeek ?
kEveryThingRFC822Peek : kEveryThingRFC822,
true, nullptr, messageSize, true);
}
void
nsImapProtocol::FetchMessage(const nsCString &messageIds,
nsIMAPeFetchFields whatToFetch,
const char *fetchModifier,
uint32_t startByte, uint32_t numBytes,
char *part)
{
IncrementCommandTagNumber();
nsCString commandString;
commandString = "%s UID fetch";
switch (whatToFetch) {
case kEveryThingRFC822:
m_flagChangeCount++;
m_fetchingWholeMessage = true;
if (m_trackingTime)
AdjustChunkSize(); // we started another segment
m_startTime = PR_Now(); // save start of download time
m_trackingTime = true;
MOZ_LOG(IMAP, LogLevel::Debug, ("FetchMessage everything: curFetchSize %u numBytes %u",
m_curFetchSize, numBytes));
if (numBytes > 0)
m_curFetchSize = numBytes;
if (GetServerStateParser().ServerHasIMAP4Rev1Capability())
{
if (GetServerStateParser().GetCapabilityFlag() & kHasXSenderCapability)
commandString.Append(" %s (XSENDER UID RFC822.SIZE BODY[]");
else
commandString.Append(" %s (UID RFC822.SIZE BODY[]");
}
else
{
if (GetServerStateParser().GetCapabilityFlag() & kHasXSenderCapability)
commandString.Append(" %s (XSENDER UID RFC822.SIZE RFC822");
else
commandString.Append(" %s (UID RFC822.SIZE RFC822");
}
if (numBytes > 0)
{
// if we are retrieving chunks
char *byterangeString = PR_smprintf("<%ld.%ld>",startByte, numBytes);
if (byterangeString)
{
commandString.Append(byterangeString);
PR_Free(byterangeString);
}
}
commandString.Append(")");
break;
case kEveryThingRFC822Peek:
{
MOZ_LOG(IMAP, LogLevel::Debug, ("FetchMessage peek: curFetchSize %u numBytes %u",
m_curFetchSize, numBytes));
if (numBytes > 0)
m_curFetchSize = numBytes;
const char *formatString = "";
eIMAPCapabilityFlags server_capabilityFlags = GetServerStateParser().GetCapabilityFlag();
m_fetchingWholeMessage = true;
if (server_capabilityFlags & kIMAP4rev1Capability)
{
// use body[].peek since rfc822.peek is not in IMAP4rev1
if (server_capabilityFlags & kHasXSenderCapability)
formatString = " %s (XSENDER UID RFC822.SIZE BODY.PEEK[]";
else
formatString = " %s (UID RFC822.SIZE BODY.PEEK[]";
}
else
{
if (server_capabilityFlags & kHasXSenderCapability)
formatString = " %s (XSENDER UID RFC822.SIZE RFC822.peek";
else
formatString = " %s (UID RFC822.SIZE RFC822.peek";
}
commandString.Append(formatString);
if (numBytes > 0)
{
// if we are retrieving chunks
char *byterangeString = PR_smprintf("<%ld.%ld>",startByte, numBytes);
if (byterangeString)
{
commandString.Append(byterangeString);
PR_Free(byterangeString);
}
}
commandString.Append(")");
}
break;
case kHeadersRFC822andUid:
if (GetServerStateParser().ServerHasIMAP4Rev1Capability())
{
eIMAPCapabilityFlags server_capabilityFlags = GetServerStateParser().GetCapabilityFlag();
bool aolImapServer = ((server_capabilityFlags & kAOLImapCapability) != 0);
bool downloadAllHeaders = false;
// checks if we're filtering on "any header" or running a spam filter requiring all headers
GetShouldDownloadAllHeaders(&downloadAllHeaders);
if (!downloadAllHeaders) // if it's ok -- no filters on any header, etc.
{
char *headersToDL = nullptr;
char *what = nullptr;
const char *dbHeaders = (gUseEnvelopeCmd) ? IMAP_DB_HEADERS : IMAP_ENV_AND_DB_HEADERS;
nsCString arbitraryHeaders;
GetArbitraryHeadersToDownload(arbitraryHeaders);
for (uint32_t i = 0; i < mCustomDBHeaders.Length(); i++)
{
if (arbitraryHeaders.Find(mCustomDBHeaders[i], CaseInsensitiveCompare) == kNotFound)
{
if (!arbitraryHeaders.IsEmpty())
arbitraryHeaders.Append(' ');
arbitraryHeaders.Append(mCustomDBHeaders[i]);
}
}
for (uint32_t i = 0; i < mCustomHeaders.Length(); i++)
{
if (arbitraryHeaders.Find(mCustomHeaders[i], CaseInsensitiveCompare) == kNotFound)
{
if (!arbitraryHeaders.IsEmpty())
arbitraryHeaders.Append(' ');
arbitraryHeaders.Append(mCustomHeaders[i]);
}
}
if (arbitraryHeaders.IsEmpty())
headersToDL = strdup(dbHeaders);
else
headersToDL = PR_smprintf("%s %s",dbHeaders, arbitraryHeaders.get());
if (gUseEnvelopeCmd)
what = PR_smprintf(" ENVELOPE BODY.PEEK[HEADER.FIELDS (%s)])", headersToDL);
else
what = PR_smprintf(" BODY.PEEK[HEADER.FIELDS (%s)])",headersToDL);
NS_Free(headersToDL);
if (what)
{
commandString.Append(" %s (UID ");
if (m_isGmailServer)
commandString.Append("X-GM-MSGID X-GM-THRID X-GM-LABELS ");
if (aolImapServer)
commandString.Append(" XAOL.SIZE") ;
else
commandString.Append("RFC822.SIZE");
commandString.Append(" FLAGS");
commandString.Append(what);
PR_Free(what);
}
else
{
commandString.Append(" %s (UID RFC822.SIZE BODY.PEEK[HEADER] FLAGS)");
}
}
else
commandString.Append(" %s (UID RFC822.SIZE BODY.PEEK[HEADER] FLAGS)");
}
else
commandString.Append(" %s (UID RFC822.SIZE RFC822.HEADER FLAGS)");
break;
case kUid:
commandString.Append(" %s (UID)");
break;
case kFlags:
GetServerStateParser().SetFetchingFlags(true);
commandString.Append(" %s (FLAGS)");
break;
case kRFC822Size:
commandString.Append(" %s (RFC822.SIZE)");
break;
case kBodyStart:
{
int32_t numBytesToFetch;
m_runningUrl->GetNumBytesToFetch(&numBytesToFetch);
commandString.Append(" %s (UID BODY.PEEK[HEADER.FIELDS (Content-Type Content-Transfer-Encoding)] BODY.PEEK[TEXT]<0.");
commandString.AppendInt(numBytesToFetch);
commandString.Append(">)");
}
break;
case kRFC822HeadersOnly:
if (GetServerStateParser().ServerHasIMAP4Rev1Capability())
{
if (part)
{
commandString.Append(" %s (BODY[");
char *what = PR_smprintf("%s.HEADER])", part);
if (what)
{
commandString.Append(what);
PR_Free(what);
}
else
HandleMemoryFailure();
}
else
{
// headers for the top-level message
commandString.Append(" %s (BODY[HEADER])");
}
}
else
commandString.Append(" %s (RFC822.HEADER)");
break;
case kMIMEPart:
commandString.Append(" %s (BODY.PEEK[%s]");
if (numBytes > 0)
{
// if we are retrieving chunks
char *byterangeString = PR_smprintf("<%ld.%ld>",startByte, numBytes);
if (byterangeString)
{
commandString.Append(byterangeString);
PR_Free(byterangeString);
}
}
commandString.Append(")");
break;
case kMIMEHeader:
commandString.Append(" %s (BODY[%s.MIME])");
break;
};
if (fetchModifier)
commandString.Append(fetchModifier);
commandString.Append(CRLF);
// since messageIds can be infinitely long, use a dynamic buffer rather than the fixed one
const char *commandTag = GetServerCommandTag();
int protocolStringSize = commandString.Length() + messageIds.Length() + PL_strlen(commandTag) + 1 +
(part ? PL_strlen(part) : 0);
char *protocolString = (char *) PR_CALLOC( protocolStringSize );
if (protocolString)
{
char *cCommandStr = ToNewCString(commandString);
if ((whatToFetch == kMIMEPart) ||
(whatToFetch == kMIMEHeader))
{
PR_snprintf(protocolString, // string to create
protocolStringSize, // max size
cCommandStr, // format string
commandTag, // command tag
messageIds.get(),
part);
}
else
{
PR_snprintf(protocolString, // string to create
protocolStringSize, // max size
cCommandStr, // format string
commandTag, // command tag
messageIds.get());
}
nsresult rv = SendData(protocolString);
free(cCommandStr);
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(protocolString);
PR_Free(protocolString);
GetServerStateParser().SetFetchingFlags(false);
// Always clear this flag after every fetch.
m_fetchingWholeMessage = false;
if (GetServerStateParser().LastCommandSuccessful() && CheckNeeded())
Check();
}
else
HandleMemoryFailure();
}
void nsImapProtocol::FetchTryChunking(const nsCString &messageIds,
nsIMAPeFetchFields whatToFetch,
bool idIsUid,
char *part,
uint32_t downloadSize,
bool tryChunking)
{
GetServerStateParser().SetTotalDownloadSize(downloadSize);
MOZ_LOG(IMAP, LogLevel::Debug, ("FetchTryChunking: curFetchSize %u", downloadSize));
m_curFetchSize = downloadSize; // we'll change this if chunking.
if (m_fetchByChunks && tryChunking &&
GetServerStateParser().ServerHasIMAP4Rev1Capability() &&
(downloadSize > (uint32_t) m_chunkThreshold))
{
uint32_t startByte = 0;
m_curFetchSize = m_chunkSize;
GetServerStateParser().ClearLastFetchChunkReceived();
while (!DeathSignalReceived() && !GetPseudoInterrupted() &&
!GetServerStateParser().GetLastFetchChunkReceived() &&
GetServerStateParser().ContinueParse())
{
FetchMessage(messageIds,
whatToFetch,
nullptr,
startByte, m_chunkSize,
part);
startByte += m_chunkSize;
}
// Only abort the stream if this is a normal message download
// Otherwise, let the body shell abort the stream.
if ((whatToFetch == kEveryThingRFC822)
&&
((startByte > 0 && (startByte < downloadSize) &&
(DeathSignalReceived() || GetPseudoInterrupted())) ||
!GetServerStateParser().ContinueParse()))
{
AbortMessageDownLoad();
PseudoInterrupt(false);
}
}
else
{
// small message, or (we're not chunking and not doing bodystructure),
// or the server is not rev1.
// Just fetch the whole thing.
FetchMessage(messageIds, whatToFetch, nullptr, 0, 0, part);
}
}
void nsImapProtocol::PipelinedFetchMessageParts(nsCString &uid, nsIMAPMessagePartIDArray *parts)
{
// assumes no chunking
// build up a string to fetch
nsCString stringToFetch;
nsCString what;
uint32_t currentPartNum = 0;
while ((parts->GetNumParts() > currentPartNum) && !DeathSignalReceived())
{
nsIMAPMessagePartID *currentPart = parts->GetPart(currentPartNum);
if (currentPart)
{
// Do things here depending on the type of message part
// Append it to the fetch string
if (currentPartNum > 0)
stringToFetch += " ";
switch (currentPart->GetFields())
{
case kMIMEHeader:
what = "BODY.PEEK[";
what += currentPart->GetPartNumberString();
what += ".MIME]";
stringToFetch += what;
break;
case kRFC822HeadersOnly:
if (currentPart->GetPartNumberString())
{
what = "BODY.PEEK[";
what += currentPart->GetPartNumberString();
what += ".HEADER]";
stringToFetch += what;
}
else
{
// headers for the top-level message
stringToFetch += "BODY.PEEK[HEADER]";
}
break;
default:
NS_ASSERTION(false, "we should only be pipelining MIME headers and Message headers");
break;
}
}
currentPartNum++;
}
// Run the single, pipelined fetch command
if ((parts->GetNumParts() > 0) && !DeathSignalReceived() && !GetPseudoInterrupted() && stringToFetch.get())
{
IncrementCommandTagNumber();
char *commandString = PR_smprintf("%s UID fetch %s (%s)%s",
GetServerCommandTag(), uid.get(),
stringToFetch.get(), CRLF);
if (commandString)
{
nsresult rv = SendData(commandString);
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(commandString);
PR_Free(commandString);
}
else
HandleMemoryFailure();
}
}
void
nsImapProtocol::PostLineDownLoadEvent(const char *line, uint32_t uidOfMessage)
{
if (!GetServerStateParser().GetDownloadingHeaders())
{
uint32_t byteCount = PL_strlen(line);
bool echoLineToMessageSink = false;
// if we have a channel listener, then just spool the message
// directly to the listener
if (m_channelListener)
{
uint32_t count = 0;
if (m_channelOutputStream)
{
nsresult rv = m_channelOutputStream->Write(line, byteCount, &count);
NS_ASSERTION(count == byteCount, "IMAP channel pipe couldn't buffer entire write");
if (NS_SUCCEEDED(rv))
{
nsCOMPtr<nsIRequest> request = do_QueryInterface(m_mockChannel);
m_channelListener->OnDataAvailable(request, m_channelContext, m_channelInputStream, 0, count);
}
// else some sort of explosion?
}
}
if (m_runningUrl)
m_runningUrl->GetStoreResultsOffline(&echoLineToMessageSink);
m_bytesToChannel += byteCount;
if (m_imapMessageSink && line && echoLineToMessageSink && !GetPseudoInterrupted())
m_imapMessageSink->ParseAdoptedMsgLine(line, uidOfMessage, m_runningUrl);
}
// ***** We need to handle the pseudo interrupt here *****
}
// Handle a line seen by the parser.
// * The argument |lineCopy| must be nullptr or should contain the same string as
// |line|. |lineCopy| will be modified.
// * A line may be passed by parts, e.g., "part1 part2\r\n" may be passed as
// HandleMessageDownLoadLine("part 1 ", 1);
// HandleMessageDownLoadLine("part 2\r\n", 0);
// However, it is assumed that a CRLF or a CRCRLF is never split (i.e., this is
// ensured *before* invoking this method).
void nsImapProtocol::HandleMessageDownLoadLine(const char *line, bool isPartialLine,
char *lineCopy)
{
NS_ENSURE_TRUE_VOID(line);
NS_PRECONDITION(lineCopy == nullptr || !PL_strcmp(line, lineCopy),
"line and lineCopy must contain the same string");
const char *messageLine = line;
uint32_t lineLength = strlen(messageLine);
const char *cEndOfLine = messageLine + lineLength;
char *localMessageLine = nullptr;
// If we obtain a partial line (due to fetching by chunks), we do not
// add/modify the end-of-line terminator.
if (!isPartialLine)
{
// Change this line to native line termination, duplicate if necessary.
// Do not assume that the line really ends in CRLF
// to start with, even though it is supposed to be RFC822
// normalize line endings to CRLF unless we are saving the message to disk
bool canonicalLineEnding = true;
nsCOMPtr<nsIMsgMessageUrl> msgUrl = do_QueryInterface(m_runningUrl);
if (m_imapAction == nsIImapUrl::nsImapSaveMessageToDisk && msgUrl)
msgUrl->GetCanonicalLineEnding(&canonicalLineEnding);
NS_PRECONDITION(MSG_LINEBREAK_LEN == 1 ||
(MSG_LINEBREAK_LEN == 2 && !PL_strcmp(CRLF, MSG_LINEBREAK)),
"violated assumptions on MSG_LINEBREAK");
if (MSG_LINEBREAK_LEN == 1 && !canonicalLineEnding)
{
bool lineEndsWithCRorLF = lineLength >= 1 &&
(cEndOfLine[-1] == '\r' || cEndOfLine[-1] == '\n');
char *endOfLine;
if (lineCopy && lineEndsWithCRorLF) // true for most lines
{
endOfLine = lineCopy + lineLength;
messageLine = lineCopy;
}
else
{
// leave enough room for one more char, MSG_LINEBREAK[0]
localMessageLine = (char *) PR_MALLOC(lineLength + 2);
if (!localMessageLine) // memory failure
return;
PL_strcpy(localMessageLine, line);
endOfLine = localMessageLine + lineLength;
messageLine = localMessageLine;
}
if (lineLength >= 2 &&
endOfLine[-2] == '\r' &&
endOfLine[-1] == '\n')
{
if(lineLength>=3 && endOfLine[-3] == '\r') // CRCRLF
{
endOfLine--;
lineLength--;
}
/* CRLF -> CR or LF */
endOfLine[-2] = MSG_LINEBREAK[0];
endOfLine[-1] = '\0';
lineLength--;
}
else if (lineLength >= 1 &&
((endOfLine[-1] == '\r') || (endOfLine[-1] == '\n')))
{
/* CR -> LF or LF -> CR */
endOfLine[-1] = MSG_LINEBREAK[0];
}
else // no eol characters at all
{
endOfLine[0] = MSG_LINEBREAK[0]; // CR or LF
endOfLine[1] = '\0';
lineLength++;
}
}
else // enforce canonical CRLF linebreaks
{
if (lineLength==0 || (lineLength == 1 && cEndOfLine[-1] == '\n'))
{
messageLine = CRLF;
lineLength = 2;
}
else if (cEndOfLine[-1] != '\n' || cEndOfLine[-2] != '\r' ||
(lineLength >=3 && cEndOfLine[-3] == '\r'))
{
// The line does not end in CRLF (or it ends in CRCRLF).
// Copy line and leave enough room for two more chars (CR and LF).
localMessageLine = (char *) PR_MALLOC(lineLength + 3);
if (!localMessageLine) // memory failure
return;
PL_strcpy(localMessageLine, line);
char *endOfLine = localMessageLine + lineLength;
messageLine = localMessageLine;
if (lineLength>=3 && endOfLine[-1] == '\n' &&
endOfLine[-2] == '\r')
{
// CRCRLF -> CRLF
endOfLine[-2] = '\n';
endOfLine[-1] = '\0';
lineLength--;
}
else if ((endOfLine[-1] == '\r') || (endOfLine[-1] == '\n'))
{
// LF -> CRLF or CR -> CRLF
endOfLine[-1] = '\r';
endOfLine[0] = '\n';
endOfLine[1] = '\0';
lineLength++;
}
else // no eol characters at all
{
endOfLine[0] = '\r';
endOfLine[1] = '\n';
endOfLine[2] = '\0';
lineLength += 2;
}
}
}
}
NS_ASSERTION(lineLength == PL_strlen(messageLine), "lineLength not accurate");
// check if sender obtained via XSENDER server extension matches "From:" field
const char *xSenderInfo = GetServerStateParser().GetXSenderInfo();
if (xSenderInfo && *xSenderInfo && !m_fromHeaderSeen)
{
if (!PL_strncmp("From: ", messageLine, 6))
{
m_fromHeaderSeen = true;
if (PL_strstr(messageLine, xSenderInfo) != NULL)
// Adding a X-Mozilla-Status line here is not very elegant but it
// works. Another X-Mozilla-Status line is added to the message when
// downloading to a local folder; this new line will also contain the
// 'authed' flag we are adding here. (If the message is again
// uploaded to the server, this flag is lost.)
// 0x0200 == nsMsgMessageFlags::SenderAuthed
HandleMessageDownLoadLine("X-Mozilla-Status: 0200\r\n", false);
GetServerStateParser().FreeXSenderInfo();
}
}
if (GetServerStateParser().GetDownloadingHeaders())
{
if (!m_curHdrInfo)
BeginMessageDownLoad(GetServerStateParser().SizeOfMostRecentMessage(), MESSAGE_RFC822);
if (m_curHdrInfo)
m_curHdrInfo->CacheLine(messageLine, GetServerStateParser().CurrentResponseUID());
PR_Free(localMessageLine);
return;
}
// if this line is for a different message, or the incoming line is too big
if (((m_downloadLineCache->CurrentUID() != GetServerStateParser().CurrentResponseUID()) && !m_downloadLineCache->CacheEmpty()) ||
(m_downloadLineCache->SpaceAvailable() < lineLength + 1) )
FlushDownloadCache();
// so now the cache is flushed, but this string might still be to big
if (m_downloadLineCache->SpaceAvailable() < lineLength + 1)
PostLineDownLoadEvent(messageLine, GetServerStateParser().CurrentResponseUID());
else
m_downloadLineCache->CacheLine(messageLine, GetServerStateParser().CurrentResponseUID());
PR_Free(localMessageLine);
}
void nsImapProtocol::FlushDownloadCache()
{
if (!m_downloadLineCache->CacheEmpty())
{
msg_line_info *downloadLine = m_downloadLineCache->GetCurrentLineInfo();
PostLineDownLoadEvent(downloadLine->adoptedMessageLine,
downloadLine->uidOfMessage);
m_downloadLineCache->ResetCache();
}
}
void nsImapProtocol::NormalMessageEndDownload()
{
Log("STREAM", "CLOSE", "Normal Message End Download Stream");
if (m_trackingTime)
AdjustChunkSize();
if (m_imapMailFolderSink && m_curHdrInfo && GetServerStateParser().GetDownloadingHeaders())
{
m_curHdrInfo->SetMsgSize(GetServerStateParser().SizeOfMostRecentMessage());
m_curHdrInfo->SetMsgUid(GetServerStateParser().CurrentResponseUID());
m_hdrDownloadCache->FinishCurrentHdr();
int32_t numHdrsCached;
m_hdrDownloadCache->GetNumHeaders(&numHdrsCached);
if (numHdrsCached == kNumHdrsToXfer)
{
m_imapMailFolderSink->ParseMsgHdrs(this, m_hdrDownloadCache);
m_hdrDownloadCache->ResetAll();
}
}
FlushDownloadCache();
if (!GetServerStateParser().GetDownloadingHeaders())
{
int32_t updatedMessageSize = -1;
if (m_fetchingWholeMessage)
{
updatedMessageSize = m_bytesToChannel;
#ifdef PR_LOGGING
if (m_bytesToChannel != GetServerStateParser().SizeOfMostRecentMessage()) {
MOZ_LOG(IMAP, LogLevel::Debug, ("STREAM:CLOSE Server's RFC822.SIZE %u, actual size %u",
GetServerStateParser().SizeOfMostRecentMessage(),
m_bytesToChannel));
}
#endif
}
// need to know if we're downloading for display or not. We'll use action == nsImapMsgFetch for now
nsImapAction imapAction = nsIImapUrl::nsImapSelectFolder; // just set it to some legal value
if (m_runningUrl)
m_runningUrl->GetImapAction(&imapAction);
if (m_imapMessageSink)
m_imapMessageSink->NormalEndMsgWriteStream(m_downloadLineCache->CurrentUID(), imapAction == nsIImapUrl::nsImapMsgFetch, m_runningUrl, updatedMessageSize);
if (m_runningUrl && m_imapMailFolderSink)
{
nsCOMPtr <nsISupports> copyState;
m_runningUrl->GetCopyState(getter_AddRefs(copyState));
if (copyState) // only need this notification during copy
{
nsCOMPtr<nsIMsgMailNewsUrl> mailUrl (do_QueryInterface(m_runningUrl));
m_imapMailFolderSink->EndMessage(mailUrl, m_downloadLineCache->CurrentUID());
}
}
}
m_curHdrInfo = nullptr;
}
void nsImapProtocol::AbortMessageDownLoad()
{
Log("STREAM", "CLOSE", "Abort Message Download Stream");
if (m_trackingTime)
AdjustChunkSize();
FlushDownloadCache();
if (GetServerStateParser().GetDownloadingHeaders())
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->AbortHeaderParseStream(this);
}
else if (m_imapMessageSink)
m_imapMessageSink->AbortMsgWriteStream();
m_curHdrInfo = nullptr;
}
void nsImapProtocol::ProcessMailboxUpdate(bool handlePossibleUndo)
{
if (DeathSignalReceived())
return;
// Update quota information
char *boxName;
GetSelectedMailboxName(&boxName);
GetQuotaDataIfSupported(boxName);
PR_Free(boxName);
// fetch the flags and uids of all existing messages or new ones
if (!DeathSignalReceived() && GetServerStateParser().NumberOfMessages())
{
if (handlePossibleUndo)
{
// undo any delete flags we may have asked to
nsCString undoIdsStr;
nsAutoCString undoIds;
GetCurrentUrl()->GetListOfMessageIds(undoIdsStr);
undoIds.Assign(undoIdsStr);
if (!undoIds.IsEmpty())
{
char firstChar = (char) undoIds.CharAt(0);
undoIds.Cut(0, 1); // remove first character
// if this string started with a '-', then this is an undo of a delete
// if its a '+' its a redo
if (firstChar == '-')
Store(undoIds, "-FLAGS (\\Deleted)", true); // most servers will fail silently on a failure, deal with it?
else if (firstChar == '+')
Store(undoIds, "+FLAGS (\\Deleted)", true); // most servers will fail silently on a failure, deal with it?
else
NS_ASSERTION(false, "bogus undo Id's");
}
}
// make the parser record these flags
nsCString fetchStr;
int32_t added = 0, deleted = 0;
m_flagState->GetNumberOfMessages(&added);
deleted = m_flagState->NumberOfDeletedMessages();
bool flagStateEmpty = !added;
// Figure out if we need to do any kind of sync.
bool needFolderSync = (flagStateEmpty || added == deleted) && (!UseCondStore() ||
(GetServerStateParser().fHighestModSeq != mFolderLastModSeq) ||
(GetShowDeletedMessages() &&
GetServerStateParser().NumberOfMessages() != mFolderTotalMsgCount));
// Figure out if we need to do a full sync (UID Fetch Flags 1:*),
// a partial sync using CHANGEDSINCE, or a sync from the previous
// highwater mark.
// if the folder doesn't know about the highest uid, or the flag state
// is empty, and we're not using CondStore, we need a full sync.
bool needFullFolderSync = !mFolderHighestUID || (flagStateEmpty && !UseCondStore());
if (needFullFolderSync || needFolderSync)
{
nsCString idsToFetch("1:*");
char fetchModifier[40] = "";
if (!needFullFolderSync && !GetShowDeletedMessages() && UseCondStore())
PR_snprintf(fetchModifier, sizeof(fetchModifier), " (CHANGEDSINCE %llu)",
mFolderLastModSeq);
else
m_flagState->SetPartialUIDFetch(false);
FetchMessage(idsToFetch, kFlags, fetchModifier);
// lets see if we should expunge during a full sync of flags.
if (GetServerStateParser().LastCommandSuccessful())
{
// if we did a CHANGEDSINCE fetch, do a sanity check on the msg counts
// to see if some other client may have done an expunge.
if (m_flagState->GetPartialUIDFetch())
{
if (m_flagState->NumberOfDeletedMessages() +
mFolderTotalMsgCount != GetServerStateParser().NumberOfMessages())
{
// sanity check failed - fall back to full flag sync
m_flagState->Reset();
m_flagState->SetPartialUIDFetch(false);
FetchMessage(NS_LITERAL_CSTRING("1:*"), kFlags);
}
}
int32_t numDeleted = m_flagState->NumberOfDeletedMessages();
// Don't do expunge when we are lite selecting folder because we
// could be doing undo.
// Expunge if we're always expunging, or the number of deleted messages
// is over the threshhold, and we're either always respecting the
// threshhold, or we're expunging based on the delete model, and
// the delete model is not the imap delete model.
if (m_imapAction != nsIImapUrl::nsImapLiteSelectFolder &&
(gExpungeOption == kAutoExpungeAlways ||
(numDeleted >= gExpungeThreshold &&
(gExpungeOption == kAutoExpungeOnThreshold ||
(gExpungeOption == kAutoExpungeDeleteModel && !GetShowDeletedMessages())))))
Expunge();
}
}
else
{
uint32_t highestRecordedUID = GetServerStateParser().HighestRecordedUID();
// if we're using CONDSTORE, and the parser hasn't seen any UIDs, use
// the highest UID we've seen from the folder.
if (UseCondStore() && !highestRecordedUID)
highestRecordedUID = mFolderHighestUID;
AppendUid(fetchStr, highestRecordedUID + 1);
fetchStr.Append(":*");
FetchMessage(fetchStr, kFlags); // only new messages please
}
}
else if (GetServerStateParser().LastCommandSuccessful())
{
GetServerStateParser().ResetFlagInfo();
// the flag state is empty, but not partial.
m_flagState->SetPartialUIDFetch(false);
}
if (GetServerStateParser().LastCommandSuccessful())
{
nsImapAction imapAction;
nsresult res = m_runningUrl->GetImapAction(&imapAction);
if (NS_SUCCEEDED(res) && imapAction == nsIImapUrl::nsImapLiteSelectFolder)
return;
}
bool entered_waitForBodyIdsMonitor = false;
uint32_t *msgIdList = nullptr;
uint32_t msgCount = 0;
nsImapMailboxSpec *new_spec = GetServerStateParser().CreateCurrentMailboxSpec();
if (new_spec && GetServerStateParser().LastCommandSuccessful())
{
nsImapAction imapAction;
nsresult res = m_runningUrl->GetImapAction(&imapAction);
if (NS_SUCCEEDED(res) && imapAction == nsIImapUrl::nsImapExpungeFolder)
new_spec->mBoxFlags |= kJustExpunged;
m_waitForBodyIdsMonitor.Enter();
entered_waitForBodyIdsMonitor = true;
if (m_imapMailFolderSink)
{
bool more;
m_imapMailFolderSink->UpdateImapMailboxInfo(this, new_spec);
m_imapMailFolderSink->GetMsgHdrsToDownload(&more, &m_progressCount,
&msgCount, &msgIdList);
m_progressIndex = 0;
m_runningUrl->SetMoreHeadersToDownload(more);
// We're going to be re-running this url if there are more headers.
if (more)
m_runningUrl->SetRerunningUrl(true);
}
}
else if (!new_spec)
HandleMemoryFailure();
if (GetServerStateParser().LastCommandSuccessful())
{
if (entered_waitForBodyIdsMonitor)
m_waitForBodyIdsMonitor.Exit();
if (msgIdList && !DeathSignalReceived() && GetServerStateParser().LastCommandSuccessful())
{
FolderHeaderDump(msgIdList, msgCount);
NS_Free( msgIdList);
}
HeaderFetchCompleted();
// this might be bogus, how are we going to do pane notification and stuff when we fetch bodies without
// headers!
}
else if (entered_waitForBodyIdsMonitor) // need to exit this monitor if death signal received
m_waitForBodyIdsMonitor.Exit();
// wait for a list of bodies to fetch.
if (GetServerStateParser().LastCommandSuccessful())
{
WaitForPotentialListOfBodysToFetch(&msgIdList, msgCount);
if ( msgCount && GetServerStateParser().LastCommandSuccessful())
{
// Tell the url that it should store the msg fetch results offline,
// while we're dumping the messages, and then restore the setting.
bool wasStoringOffline;
m_runningUrl->GetStoreResultsOffline(&wasStoringOffline);
m_runningUrl->SetStoreResultsOffline(true);
m_progressIndex = 0;
m_progressCount = msgCount;
FolderMsgDump(msgIdList, msgCount, kEveryThingRFC822Peek);
m_runningUrl->SetStoreResultsOffline(wasStoringOffline);
}
}
if (!GetServerStateParser().LastCommandSuccessful())
GetServerStateParser().ResetFlagInfo();
NS_IF_RELEASE(new_spec);
}
void nsImapProtocol::FolderHeaderDump(uint32_t *msgUids, uint32_t msgCount)
{
FolderMsgDump(msgUids, msgCount, kHeadersRFC822andUid);
}
void nsImapProtocol::FolderMsgDump(uint32_t *msgUids, uint32_t msgCount, nsIMAPeFetchFields fields)
{
// lets worry about this progress stuff later.
switch (fields) {
case kHeadersRFC822andUid:
SetProgressString("imapReceivingMessageHeaders2");
break;
case kFlags:
SetProgressString("imapReceivingMessageFlags2");
break;
default:
SetProgressString("imapFolderReceivingMessageOf2");
break;
}
FolderMsgDumpLoop(msgUids, msgCount, fields);
SetProgressString(nullptr);
}
void nsImapProtocol::WaitForPotentialListOfBodysToFetch(uint32_t **msgIdList, uint32_t &msgCount)
{
PRIntervalTime sleepTime = kImapSleepTime;
ReentrantMonitorAutoEnter fetchListMon(m_fetchBodyListMonitor);
while(!m_fetchBodyListIsNew && !DeathSignalReceived())
fetchListMon.Wait(sleepTime);
m_fetchBodyListIsNew = false;
*msgIdList = m_fetchBodyIdList;
msgCount = m_fetchBodyCount;
}
// libmsg uses this to notify a running imap url about message bodies it should download.
// why not just have libmsg explicitly download the message bodies?
NS_IMETHODIMP nsImapProtocol::NotifyBodysToDownload(uint32_t *keys, uint32_t keyCount)
{
ReentrantMonitorAutoEnter fetchListMon(m_fetchBodyListMonitor);
PR_FREEIF(m_fetchBodyIdList);
m_fetchBodyIdList = (uint32_t *) PR_MALLOC(keyCount * sizeof(uint32_t));
if (m_fetchBodyIdList)
memcpy(m_fetchBodyIdList, keys, keyCount * sizeof(uint32_t));
m_fetchBodyCount = keyCount;
m_fetchBodyListIsNew = true;
fetchListMon.Notify();
return NS_OK;
}
NS_IMETHODIMP nsImapProtocol::GetFlagsForUID(uint32_t uid, bool *foundIt, imapMessageFlagsType *resultFlags, char **customFlags)
{
int32_t i;
imapMessageFlagsType flags = m_flagState->GetMessageFlagsFromUID(uid, foundIt, &i);
if (*foundIt)
{
*resultFlags = flags;
if ((flags & kImapMsgCustomKeywordFlag) && customFlags)
m_flagState->GetCustomFlags(uid, customFlags);
}
return NS_OK;
}
NS_IMETHODIMP nsImapProtocol::GetFlagAndUidState(nsIImapFlagAndUidState **aFlagState)
{
NS_ENSURE_ARG_POINTER(aFlagState);
NS_IF_ADDREF(*aFlagState = m_flagState);
return NS_OK;
}
NS_IMETHODIMP nsImapProtocol::GetSupportedUserFlags(uint16_t *supportedFlags)
{
if (!supportedFlags)
return NS_ERROR_NULL_POINTER;
*supportedFlags = m_flagState->GetSupportedUserFlags();
return NS_OK;
}
void nsImapProtocol::FolderMsgDumpLoop(uint32_t *msgUids, uint32_t msgCount, nsIMAPeFetchFields fields)
{
int32_t msgCountLeft = msgCount;
uint32_t msgsDownloaded = 0;
do
{
nsCString idString;
uint32_t msgsToDownload = msgCountLeft;
AllocateImapUidString(msgUids + msgsDownloaded, msgsToDownload, m_flagState, idString); // 20 * 200
FetchMessage(idString, fields);
msgsDownloaded += msgsToDownload;
msgCountLeft -= msgsToDownload;
}
while (msgCountLeft > 0 && !DeathSignalReceived());
}
void nsImapProtocol::HeaderFetchCompleted()
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->ParseMsgHdrs(this, m_hdrDownloadCache);
m_hdrDownloadCache->ReleaseAll();
if (m_imapMailFolderSink)
m_imapMailFolderSink->HeaderFetchCompleted(this);
}
// Use the noop to tell the server we are still here, and therefore we are willing to receive
// status updates. The recent or exists response from the server could tell us that there is
// more mail waiting for us, but we need to check the flags of the mail and the high water mark
// to make sure that we do not tell the user that there is new mail when perhaps they have
// already read it in another machine.
void nsImapProtocol::PeriodicBiff()
{
nsMsgBiffState startingState = m_currentBiffState;
if (GetServerStateParser().GetIMAPstate() == nsImapServerResponseParser::kFolderSelected)
{
Noop(); // check the latest number of messages
int32_t numMessages = 0;
m_flagState->GetNumberOfMessages(&numMessages);
if (GetServerStateParser().NumberOfMessages() != numMessages)
{
uint32_t id = GetServerStateParser().HighestRecordedUID() + 1;
nsCString fetchStr; // only update flags
uint32_t added = 0, deleted = 0;
deleted = m_flagState->NumberOfDeletedMessages();
added = numMessages;
if (!added || (added == deleted)) // empty keys, get them all
id = 1;
//sprintf(fetchStr, "%ld:%ld", id, id + GetServerStateParser().NumberOfMessages() - fFlagState->GetNumberOfMessages());
AppendUid(fetchStr, id);
fetchStr.Append(":*");
FetchMessage(fetchStr, kFlags);
if (((uint32_t) m_flagState->GetHighestNonDeletedUID() >= id) && m_flagState->IsLastMessageUnseen())
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_NewMail;
else
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_NoMail;
}
else
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_NoMail;
}
else
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_Unknown;
if (startingState != m_currentBiffState)
SendSetBiffIndicatorEvent(m_currentBiffState);
}
void nsImapProtocol::SendSetBiffIndicatorEvent(nsMsgBiffState newState)
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->SetBiffStateAndUpdate(newState);
}
// We get called to see if there is mail waiting for us at the server, even if it may have been
// read elsewhere. We just want to know if we should download headers or not.
bool nsImapProtocol::CheckNewMail()
{
return m_checkForNewMailDownloadsHeaders;
}
/* static */ void nsImapProtocol::LogImapUrl(const char *logMsg, nsIImapUrl *imapUrl)
{
// nsImapProtocol is not always constructed before this static method is called
if (!IMAP)
IMAP = PR_NewLogModule("IMAP");
if (MOZ_LOG_TEST(IMAP, LogLevel::Info))
{
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(imapUrl);
if (mailnewsUrl)
{
nsAutoCString urlSpec, unescapedUrlSpec;
nsresult rv = mailnewsUrl->GetSpec(urlSpec);
if (NS_FAILED(rv))
return;
MsgUnescapeString(urlSpec, 0, unescapedUrlSpec);
MOZ_LOG(IMAP, LogLevel::Info, ("%s:%s", logMsg, unescapedUrlSpec.get()));
}
}
}
// log info including current state...
void nsImapProtocol::Log(const char *logSubName, const char *extraInfo, const char *logData)
{
if (MOZ_LOG_TEST(IMAP, LogLevel::Info))
{
static const char nonAuthStateName[] = "NA";
static const char authStateName[] = "A";
static const char selectedStateName[] = "S";
const nsCString& hostName = GetImapHostName(); // initilize to empty string
int32_t logDataLen = PL_strlen(logData); // PL_strlen checks for null
nsCString logDataLines;
const char *logDataToLog;
int32_t lastLineEnd;
const int kLogDataChunkSize = 400; // nspr line length is 512, and we
// allow some space for the log preamble.
// break up buffers > 400 bytes on line boundaries.
if (logDataLen > kLogDataChunkSize)
{
logDataLines.Assign(logData);
lastLineEnd = MsgRFindChar(logDataLines, '\n', kLogDataChunkSize);
// null terminate the last line
if (lastLineEnd == kNotFound)
lastLineEnd = kLogDataChunkSize - 1;
logDataLines.Insert( '\0', lastLineEnd + 1);
logDataToLog = logDataLines.get();
}
else
{
logDataToLog = logData;
lastLineEnd = logDataLen;
}
switch (GetServerStateParser().GetIMAPstate())
{
case nsImapServerResponseParser::kFolderSelected:
if (extraInfo)
MOZ_LOG(IMAP, LogLevel::Info, ("%x:%s:%s-%s:%s:%s: %.400s", this, hostName.get(),
selectedStateName, GetServerStateParser().GetSelectedMailboxName(),
logSubName, extraInfo, logDataToLog));
else
MOZ_LOG(IMAP, LogLevel::Info, ("%x:%s:%s-%s:%s: %.400s", this, hostName.get(),
selectedStateName, GetServerStateParser().GetSelectedMailboxName(),
logSubName, logDataToLog));
break;
case nsImapServerResponseParser::kNonAuthenticated:
case nsImapServerResponseParser::kAuthenticated:
{
const char *stateName = (GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kNonAuthenticated)
? nonAuthStateName : authStateName;
if (extraInfo)
MOZ_LOG(IMAP, LogLevel::Info, ("%x:%s:%s:%s:%s: %.400s", this,
hostName.get(),stateName,logSubName,extraInfo,logDataToLog));
else
MOZ_LOG(IMAP, LogLevel::Info, ("%x:%s:%s:%s: %.400s",this,
hostName.get(),stateName,logSubName,logDataToLog));
}
}
// dump the rest of the string in < 400 byte chunks
while (logDataLen > kLogDataChunkSize)
{
logDataLines.Cut(0, lastLineEnd + 2); // + 2 to account for the LF and the '\0' we added
logDataLen = logDataLines.Length();
lastLineEnd = (logDataLen > kLogDataChunkSize) ? MsgRFindChar(logDataLines, '\n', kLogDataChunkSize) : kNotFound;
// null terminate the last line
if (lastLineEnd == kNotFound)
lastLineEnd = kLogDataChunkSize - 1;
logDataLines.Insert( '\0', lastLineEnd + 1);
logDataToLog = logDataLines.get();
MOZ_LOG(IMAP, LogLevel::Info, ("%.400s", logDataToLog));
}
}
}
// In 4.5, this posted an event back to libmsg and blocked until it got a response.
// We may still have to do this.It would be nice if we could preflight this value,
// but we may not always know when we'll need it.
uint32_t nsImapProtocol::GetMessageSize(const char * messageId, bool idsAreUids)
{
const char *folderFromParser = GetServerStateParser().GetSelectedMailboxName();
if (!folderFromParser || !messageId || !m_runningUrl || !m_hostSessionList)
return 0;
char *folderName = nullptr;
uint32_t size;
nsIMAPNamespace *nsForMailbox = nullptr;
m_hostSessionList->GetNamespaceForMailboxForHost(GetImapServerKey(),
folderFromParser,
nsForMailbox);
m_runningUrl->AllocateCanonicalPath(folderFromParser,
nsForMailbox
? nsForMailbox->GetDelimiter()
: kOnlineHierarchySeparatorUnknown,
&folderName);
if (folderName && m_imapMessageSink)
m_imapMessageSink->GetMessageSizeFromDB(messageId, &size);
PR_FREEIF(folderName);
if (DeathSignalReceived())
size = 0;
return size;
}
// message id string utility functions
/* static */bool nsImapProtocol::HandlingMultipleMessages(const nsCString & messageIdString)
{
return (MsgFindCharInSet(messageIdString, ",:") != kNotFound);
}
uint32_t nsImapProtocol::CountMessagesInIdString(const char *idString)
{
uint32_t numberOfMessages = 0;
char *uidString = PL_strdup(idString);
if (uidString)
{
// This is in the form <id>,<id>, or <id1>:<id2>
char curChar = *uidString;
bool isRange = false;
int32_t curToken;
int32_t saveStartToken=0;
for (char *curCharPtr = uidString; curChar && *curCharPtr;)
{
char *currentKeyToken = curCharPtr;
curChar = *curCharPtr;
while (curChar != ':' && curChar != ',' && curChar != '\0')
curChar = *curCharPtr++;
*(curCharPtr - 1) = '\0';
curToken = atol(currentKeyToken);
if (isRange)
{
while (saveStartToken < curToken)
{
numberOfMessages++;
saveStartToken++;
}
}
numberOfMessages++;
isRange = (curChar == ':');
if (isRange)
saveStartToken = curToken + 1;
}
PR_Free(uidString);
}
return numberOfMessages;
}
// It would be really nice not to have to use this method nearly as much as we did
// in 4.5 - we need to think about this some. Some of it may just go away in the new world order
bool nsImapProtocol::DeathSignalReceived()
{
// ignore mock channel status if we've been pseudo interrupted
// ### need to make sure we clear pseudo interrupted status appropriately.
if (!GetPseudoInterrupted() && m_mockChannel)
{
nsCOMPtr<nsIRequest> request = do_QueryInterface(m_mockChannel);
if (request)
{
nsresult returnValue;
request->GetStatus(&returnValue);
if (NS_FAILED(returnValue))
return false;
}
}
// Check the other way of cancelling.
ReentrantMonitorAutoEnter threadDeathMon(m_threadDeathMonitor);
return m_threadShouldDie;
}
NS_IMETHODIMP nsImapProtocol::ResetToAuthenticatedState()
{
GetServerStateParser().PreauthSetAuthenticatedState();
return NS_OK;
}
NS_IMETHODIMP nsImapProtocol::GetSelectedMailboxName(char ** folderName)
{
if (!folderName) return NS_ERROR_NULL_POINTER;
if (GetServerStateParser().GetSelectedMailboxName())
*folderName =
PL_strdup((GetServerStateParser().GetSelectedMailboxName()));
return NS_OK;
}
bool nsImapProtocol::GetPseudoInterrupted()
{
ReentrantMonitorAutoEnter pseudoInterruptMon(m_pseudoInterruptMonitor);
return m_pseudoInterrupted;
}
void nsImapProtocol::PseudoInterrupt(bool the_interrupt)
{
ReentrantMonitorAutoEnter pseudoInterruptMon(m_pseudoInterruptMonitor);
m_pseudoInterrupted = the_interrupt;
if (the_interrupt)
Log("CONTROL", NULL, "PSEUDO-Interrupted");
}
void nsImapProtocol::SetActive(bool active)
{
ReentrantMonitorAutoEnter dataMemberMon(m_dataMemberMonitor);
m_active = active;
}
bool nsImapProtocol::GetActive()
{
ReentrantMonitorAutoEnter dataMemberMon(m_dataMemberMonitor);
return m_active;
}
bool nsImapProtocol::GetShowAttachmentsInline()
{
bool showAttachmentsInline = true;
if (m_imapServerSink)
m_imapServerSink->GetShowAttachmentsInline(&showAttachmentsInline);
return showAttachmentsInline;
}
void nsImapProtocol::SetContentModified(IMAP_ContentModifiedType modified)
{
if (m_runningUrl && m_imapMessageSink)
m_imapMessageSink->SetContentModified(m_runningUrl, modified);
}
bool nsImapProtocol::GetShouldFetchAllParts()
{
if (m_runningUrl && !DeathSignalReceived())
{
nsImapContentModifiedType contentModified;
if (NS_SUCCEEDED(m_runningUrl->GetContentModified(&contentModified)))
return (contentModified == IMAP_CONTENT_FORCE_CONTENT_NOT_MODIFIED);
}
return true;
}
// Adds a set of rights for a given user on a given mailbox on the current host.
// if userName is NULL, it means "me," or MYRIGHTS.
void nsImapProtocol::AddFolderRightsForUser(const char *mailboxName, const char *userName, const char *rights)
{
if (!userName)
userName = "";
if (m_imapServerSink)
m_imapServerSink->AddFolderRights(nsDependentCString(mailboxName), nsDependentCString(userName),
nsDependentCString(rights));
}
void nsImapProtocol::SetCopyResponseUid(const char *msgIdString)
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->SetCopyResponseUid(msgIdString, m_runningUrl);
}
void nsImapProtocol::CommitNamespacesForHostEvent()
{
if (m_imapServerSink)
m_imapServerSink->CommitNamespaces();
}
// notifies libmsg that we have new capability data for the current host
void nsImapProtocol::CommitCapability()
{
if (m_imapServerSink)
{
m_imapServerSink->SetCapability(GetServerStateParser().GetCapabilityFlag());
}
}
// rights is a single string of rights, as specified by RFC2086, the IMAP ACL extension.
// Clears all rights for a given folder, for all users.
void nsImapProtocol::ClearAllFolderRights()
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->ClearFolderRights();
}
char* nsImapProtocol::CreateNewLineFromSocket()
{
bool needMoreData = false;
char * newLine = nullptr;
uint32_t numBytesInLine = 0;
nsresult rv = NS_OK;
// we hold a ref to the input stream in case we get cancelled from the
// ui thread, which releases our ref to the input stream, and can
// cause the pipe to get deleted before the monitor the read is
// blocked on gets notified. When that happens, the imap thread
// will stay blocked.
nsCOMPtr <nsIInputStream> kungFuGrip = m_inputStream;
do
{
newLine = m_inputStreamBuffer->ReadNextLine(m_inputStream, numBytesInLine, needMoreData, &rv);
MOZ_LOG(IMAP, LogLevel::Debug, ("ReadNextLine [stream=%x nb=%u needmore=%u]\n",
m_inputStream.get(), numBytesInLine, needMoreData));
} while (!newLine && NS_SUCCEEDED(rv) && !DeathSignalReceived()); // until we get the next line and haven't been interrupted
kungFuGrip = nullptr;
if (NS_FAILED(rv))
{
switch (rv)
{
case NS_ERROR_UNKNOWN_HOST:
case NS_ERROR_UNKNOWN_PROXY_HOST:
AlertUserEventUsingName("imapUnknownHostError");
break;
case NS_ERROR_CONNECTION_REFUSED:
case NS_ERROR_PROXY_CONNECTION_REFUSED:
AlertUserEventUsingName("imapConnectionRefusedError");
break;
case NS_ERROR_NET_TIMEOUT:
case NS_ERROR_NET_RESET:
case NS_BASE_STREAM_CLOSED:
case NS_ERROR_NET_INTERRUPT:
// we should retry on RESET, especially for SSL...
if ((TestFlag(IMAP_RECEIVED_GREETING) || rv == NS_ERROR_NET_RESET) &&
m_runningUrl && !m_retryUrlOnError)
{
bool rerunningUrl;
nsImapAction imapAction;
m_runningUrl->GetRerunningUrl(&rerunningUrl);
m_runningUrl->GetImapAction(&imapAction);
// don't rerun if we already were rerunning. And don't rerun
// online move/copies that timeout.
if (!rerunningUrl && (rv != NS_ERROR_NET_TIMEOUT ||
(imapAction != nsIImapUrl::nsImapOnlineCopy &&
imapAction != nsIImapUrl::nsImapOnlineMove)))
{
m_runningUrl->SetRerunningUrl(true);
m_retryUrlOnError = true;
break;
}
}
if (rv == NS_ERROR_NET_TIMEOUT)
AlertUserEventUsingName("imapNetTimeoutError");
else
AlertUserEventUsingName(TestFlag(IMAP_RECEIVED_GREETING) ?
"imapServerDisconnected" :
"imapServerDroppedConnection");
break;
default:
break;
}
nsAutoCString logMsg("clearing IMAP_CONNECTION_IS_OPEN - rv = ");
logMsg.AppendInt(static_cast<uint32_t>(rv), 16);
Log("CreateNewLineFromSocket", nullptr, logMsg.get());
ClearFlag(IMAP_CONNECTION_IS_OPEN);
TellThreadToDie();
}
Log("CreateNewLineFromSocket", nullptr, newLine);
SetConnectionStatus(newLine && numBytesInLine ? NS_OK : rv); // set > 0 if string is not null or empty
return newLine;
}
nsresult
nsImapProtocol::GetConnectionStatus()
{
return m_connectionStatus;
}
void
nsImapProtocol::SetConnectionStatus(nsresult status)
{
m_connectionStatus = status;
}
void
nsImapProtocol::NotifyMessageFlags(imapMessageFlagsType flags,
const nsACString &keywords,
nsMsgKey key, uint64_t highestModSeq)
{
if (m_imapMessageSink)
{
// if we're selecting the folder, don't need to report the flags; we've already fetched them.
if (m_imapAction != nsIImapUrl::nsImapSelectFolder &&
(m_imapAction != nsIImapUrl::nsImapMsgFetch ||
(flags & ~kImapMsgRecentFlag) != kImapMsgSeenFlag))
m_imapMessageSink->NotifyMessageFlags(flags, keywords, key, highestModSeq);
}
}
void
nsImapProtocol::NotifySearchHit(const char * hitLine)
{
nsresult rv;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl, &rv);
if (m_imapMailFolderSink)
m_imapMailFolderSink->NotifySearchHit(mailnewsUrl, hitLine);
}
void nsImapProtocol::SetMailboxDiscoveryStatus(EMailboxDiscoverStatus status)
{
ReentrantMonitorAutoEnter mon(m_dataMemberMonitor);
m_discoveryStatus = status;
}
EMailboxDiscoverStatus nsImapProtocol::GetMailboxDiscoveryStatus( )
{
ReentrantMonitorAutoEnter mon(m_dataMemberMonitor);
return m_discoveryStatus;
}
bool
nsImapProtocol::GetSubscribingNow()
{
// ***** code me *****
return false;// ***** for now
}
void
nsImapProtocol::DiscoverMailboxSpec(nsImapMailboxSpec * adoptedBoxSpec)
{
nsIMAPNamespace *ns = nullptr;
NS_ASSERTION (m_hostSessionList, "fatal null host session list");
if (!m_hostSessionList)
return;
m_hostSessionList->GetDefaultNamespaceOfTypeForHost(
GetImapServerKey(), kPersonalNamespace, ns);
const char *nsPrefix = ns ? ns->GetPrefix() : 0;
if (m_specialXListMailboxes.Count() > 0)
{
int32_t hashValue = 0;
nsCString strHashKey(adoptedBoxSpec->mAllocatedPathName);
m_specialXListMailboxes.Get(strHashKey, &hashValue);
adoptedBoxSpec->mBoxFlags |= hashValue;
}
switch (m_hierarchyNameState)
{
case kXListing:
if (adoptedBoxSpec->mBoxFlags &
(kImapXListTrash|kImapAllMail|kImapInbox|kImapSent|kImapSpam|kImapDrafts))
{
nsCString mailboxName(adoptedBoxSpec->mAllocatedPathName);
m_specialXListMailboxes.Put(mailboxName, adoptedBoxSpec->mBoxFlags);
// Remember hierarchy delimiter in case this is the first time we've
// connected to the server and we need it to be correct for the two-level
// XLIST we send (INBOX is guaranteed to be in the first response).
if (adoptedBoxSpec->mBoxFlags & kImapInbox)
m_runningUrl->SetOnlineSubDirSeparator(adoptedBoxSpec->mHierarchySeparator);
}
NS_IF_RELEASE(adoptedBoxSpec);
break;
case kListingForFolderFlags:
{
// store mailbox flags from LIST for use by LSUB
nsCString mailboxName(adoptedBoxSpec->mAllocatedPathName);
m_standardListMailboxes.Put(mailboxName, adoptedBoxSpec->mBoxFlags);
}
NS_IF_RELEASE(adoptedBoxSpec);
break;
case kListingForCreate:
case kNoOperationInProgress:
case kDiscoverTrashFolderInProgress:
case kListingForInfoAndDiscovery:
{
// standard mailbox specs are stored in m_standardListMailboxes
// because LSUB does necessarily return all mailbox flags.
// count should be > 0 only when we are looking at response of LSUB
if (m_standardListMailboxes.Count() > 0)
{
int32_t hashValue = 0;
nsCString strHashKey(adoptedBoxSpec->mAllocatedPathName);
if (m_standardListMailboxes.Get(strHashKey, &hashValue))
adoptedBoxSpec->mBoxFlags |= hashValue;
else
// if mailbox is not in hash list, then it is subscribed but does not
// exist, so we make sure it can't be selected
adoptedBoxSpec->mBoxFlags |= kNoselect;
}
if (ns && nsPrefix) // if no personal namespace, there can be no Trash folder
{
bool onlineTrashFolderExists = false;
if (m_hostSessionList)
{
if (adoptedBoxSpec->mBoxFlags & (kImapTrash|kImapXListTrash))
{
m_hostSessionList->SetOnlineTrashFolderExistsForHost(GetImapServerKey(), true);
onlineTrashFolderExists = true;
}
else
{
m_hostSessionList->GetOnlineTrashFolderExistsForHost(
GetImapServerKey(), onlineTrashFolderExists);
}
}
// Don't set the Trash flag if not using the Trash model
if (GetDeleteIsMoveToTrash() && !onlineTrashFolderExists &&
adoptedBoxSpec->mAllocatedPathName.Find(m_trashFolderName, CaseInsensitiveCompare) != -1)
{
bool trashExists = false;
nsCString trashMatch(CreatePossibleTrashName(nsPrefix));
nsCString serverTrashName;
m_runningUrl->AllocateCanonicalPath(trashMatch.get(),
ns->GetDelimiter(),
getter_Copies(serverTrashName));
if (StringBeginsWith(serverTrashName,
NS_LITERAL_CSTRING("INBOX/"),
nsCaseInsensitiveCStringComparator()))
{
nsAutoCString pathName(adoptedBoxSpec->mAllocatedPathName.get() + 6);
trashExists =
StringBeginsWith(adoptedBoxSpec->mAllocatedPathName,
serverTrashName,
nsCaseInsensitiveCStringComparator()) && /* "INBOX/" */
pathName.Equals(Substring(serverTrashName, 6), nsCaseInsensitiveCStringComparator());
}
else
trashExists = adoptedBoxSpec->mAllocatedPathName.Equals(serverTrashName, nsCaseInsensitiveCStringComparator());
if (m_hostSessionList)
m_hostSessionList->SetOnlineTrashFolderExistsForHost(GetImapServerKey(), trashExists);
if (trashExists)
adoptedBoxSpec->mBoxFlags |= kImapTrash;
}
}
// Discover the folder (shuttle over to libmsg, yay)
// Do this only if the folder name is not empty (i.e. the root)
if (!adoptedBoxSpec->mAllocatedPathName.IsEmpty())
{
if (m_hierarchyNameState == kListingForCreate)
adoptedBoxSpec->mBoxFlags |= kNewlyCreatedFolder;
if (m_imapServerSink)
{
bool newFolder;
m_imapServerSink->PossibleImapMailbox(adoptedBoxSpec->mAllocatedPathName,
adoptedBoxSpec->mHierarchySeparator,
adoptedBoxSpec->mBoxFlags, &newFolder);
// if it's a new folder to the server sink, setting discovery status to
// eContinueNew will cause us to get the ACL for the new folder.
if (newFolder)
SetMailboxDiscoveryStatus(eContinueNew);
bool useSubscription = false;
if (m_hostSessionList)
m_hostSessionList->GetHostIsUsingSubscription(GetImapServerKey(),
useSubscription);
if ((GetMailboxDiscoveryStatus() != eContinue) &&
(GetMailboxDiscoveryStatus() != eContinueNew) &&
(GetMailboxDiscoveryStatus() != eListMyChildren))
{
SetConnectionStatus(NS_ERROR_FAILURE);
}
else if (!adoptedBoxSpec->mAllocatedPathName.IsEmpty() &&
(GetMailboxDiscoveryStatus() == eListMyChildren) &&
(!useSubscription || GetSubscribingNow()))
{
NS_ASSERTION (false,
"we should never get here anymore");
SetMailboxDiscoveryStatus(eContinue);
}
else if (GetMailboxDiscoveryStatus() == eContinueNew)
{
if (m_hierarchyNameState == kListingForInfoAndDiscovery &&
!adoptedBoxSpec->mAllocatedPathName.IsEmpty() &&
!(adoptedBoxSpec->mBoxFlags & kNameSpace))
{
// remember the info here also
nsIMAPMailboxInfo *mb = new nsIMAPMailboxInfo(adoptedBoxSpec->mAllocatedPathName, adoptedBoxSpec->mHierarchySeparator);
m_listedMailboxList.AppendElement(mb);
}
SetMailboxDiscoveryStatus(eContinue);
}
}
}
}
NS_IF_RELEASE( adoptedBoxSpec);
break;
case kDiscoverBaseFolderInProgress:
break;
case kDeleteSubFoldersInProgress:
{
NS_ASSERTION(m_deletableChildren, "Oops .. null m_deletableChildren\n");
m_deletableChildren->AppendElement(ToNewCString(adoptedBoxSpec->mAllocatedPathName));
NS_IF_RELEASE(adoptedBoxSpec);
}
break;
case kListingForInfoOnly:
{
//UpdateProgressWindowForUpgrade(adoptedBoxSpec->allocatedPathName);
ProgressEventFunctionUsingNameWithString("imapDiscoveringMailbox",
adoptedBoxSpec->mAllocatedPathName.get());
nsIMAPMailboxInfo *mb = new nsIMAPMailboxInfo(adoptedBoxSpec->mAllocatedPathName,
adoptedBoxSpec->mHierarchySeparator);
m_listedMailboxList.AppendElement(mb);
NS_IF_RELEASE(adoptedBoxSpec);
}
break;
case kDiscoveringNamespacesOnly:
{
NS_IF_RELEASE(adoptedBoxSpec);
}
break;
default:
NS_ASSERTION (false, "we aren't supposed to be here");
break;
}
}
void
nsImapProtocol::AlertUserEventUsingName(const char* aMessageName)
{
if (m_imapServerSink)
{
bool suppressErrorMsg = false;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl);
if (mailnewsUrl)
mailnewsUrl->GetSuppressErrorMsgs(&suppressErrorMsg);
if (!suppressErrorMsg)
m_imapServerSink->FEAlertWithName(aMessageName,
mailnewsUrl);
}
}
void
nsImapProtocol::AlertUserEvent(const char * message)
{
if (m_imapServerSink)
{
bool suppressErrorMsg = false;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl);
if (mailnewsUrl)
mailnewsUrl->GetSuppressErrorMsgs(&suppressErrorMsg);
if (!suppressErrorMsg)
m_imapServerSink->FEAlert(NS_ConvertASCIItoUTF16(message), mailnewsUrl);
}
}
void
nsImapProtocol::AlertUserEventFromServer(const char * aServerEvent)
{
if (m_imapServerSink && aServerEvent)
{
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl);
m_imapServerSink->FEAlertFromServer(nsDependentCString(aServerEvent),
mailnewsUrl);
}
}
void nsImapProtocol::ResetProgressInfo()
{
m_lastProgressTime = 0;
m_lastPercent = -1;
m_lastProgressStringName.Truncate();
}
void nsImapProtocol::SetProgressString(const char * stringName)
{
m_progressStringName.Assign(stringName);
if (!m_progressStringName.IsEmpty() && m_imapServerSink)
m_imapServerSink->GetImapStringByName(stringName,
m_progressString);
}
void
nsImapProtocol::ShowProgress()
{
if (!m_progressString.IsEmpty() && !m_progressStringName.IsEmpty())
{
char16_t *progressString = NULL;
const char *mailboxName = GetServerStateParser().GetSelectedMailboxName();
nsString unicodeMailboxName;
nsresult rv = CopyMUTF7toUTF16(nsDependentCString(mailboxName),
unicodeMailboxName);
if (NS_SUCCEEDED(rv))
{
// ### should convert mailboxName to char16_t and change %s to %S in msg text
progressString = nsTextFormatter::smprintf(m_progressString.get(),
unicodeMailboxName.get(), ++m_progressIndex, m_progressCount);
if (progressString)
{
PercentProgressUpdateEvent(progressString, m_progressIndex, m_progressCount);
nsTextFormatter::smprintf_free(progressString);
}
}
}
}
void
nsImapProtocol::ProgressEventFunctionUsingName(const char* aMsgName)
{
if (m_imapMailFolderSink && !m_lastProgressStringName.Equals(aMsgName))
{
m_imapMailFolderSink->ProgressStatusString(this, aMsgName, nullptr);
m_lastProgressStringName.Assign(aMsgName);
// who's going to free this? Does ProgressStatusString complete synchronously?
}
}
void
nsImapProtocol::ProgressEventFunctionUsingNameWithString(const char* aMsgName,
const char * aExtraInfo)
{
if (m_imapMailFolderSink)
{
nsString unicodeStr;
nsresult rv = CopyMUTF7toUTF16(nsDependentCString(aExtraInfo), unicodeStr);
if (NS_SUCCEEDED(rv))
m_imapMailFolderSink->ProgressStatusString(this, aMsgName, unicodeStr.get());
}
}
void
nsImapProtocol::PercentProgressUpdateEvent(char16_t *message, int64_t currentProgress, int64_t maxProgress)
{
int64_t nowMS = 0;
int32_t percent = (100 * currentProgress) / maxProgress;
if (percent == m_lastPercent)
return; // hasn't changed, right? So just return. Do we need to clear this anywhere?
if (percent < 100) // always need to do 100%
{
nowMS = PR_IntervalToMilliseconds(PR_IntervalNow());
if (nowMS - m_lastProgressTime < 750)
return;
}
m_lastPercent = percent;
m_lastProgressTime = nowMS;
// set our max progress on the running URL
if (m_runningUrl)
{
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl(do_QueryInterface(m_runningUrl));
mailnewsUrl->SetMaxProgress(maxProgress);
}
if (m_imapMailFolderSink)
m_imapMailFolderSink->PercentProgress(this, message, currentProgress, maxProgress);
}
// imap commands issued by the parser
void
nsImapProtocol::Store(const nsCString &messageList, const char * messageData,
bool idsAreUid)
{
// turn messageList back into key array and then back into a message id list,
// but use the flag state to handle ranges correctly.
nsCString messageIdList;
nsTArray<nsMsgKey> msgKeys;
if (idsAreUid)
ParseUidString(messageList.get(), msgKeys);
int32_t msgCountLeft = msgKeys.Length();
uint32_t msgsHandled = 0;
do
{
nsCString idString;
uint32_t msgsToHandle = msgCountLeft;
if (idsAreUid)
AllocateImapUidString(msgKeys.Elements() + msgsHandled, msgsToHandle, m_flagState, idString); // 20 * 200
else
idString.Assign(messageList);
msgsHandled += msgsToHandle;
msgCountLeft -= msgsToHandle;
IncrementCommandTagNumber();
const char *formatString;
if (idsAreUid)
formatString = "%s uid store %s %s\015\012";
else
formatString = "%s store %s %s\015\012";
// we might need to close this mailbox after this
m_closeNeededBeforeSelect = GetDeleteIsMoveToTrash() &&
(PL_strcasestr(messageData, "\\Deleted"));
const char *commandTag = GetServerCommandTag();
int protocolStringSize = PL_strlen(formatString) +
messageList.Length() + PL_strlen(messageData) +
PL_strlen(commandTag) + 1;
char *protocolString = (char *) PR_CALLOC( protocolStringSize );
if (protocolString)
{
PR_snprintf(protocolString, // string to create
protocolStringSize, // max size
formatString, // format string
commandTag, // command tag
idString.get(),
messageData);
nsresult rv = SendData(protocolString);
if (NS_SUCCEEDED(rv))
{
m_flagChangeCount++;
ParseIMAPandCheckForNewMail(protocolString);
if (GetServerStateParser().LastCommandSuccessful() && CheckNeeded())
Check();
}
PR_Free(protocolString);
}
else
HandleMemoryFailure();
}
while (msgCountLeft > 0 && !DeathSignalReceived());
}
void
nsImapProtocol::IssueUserDefinedMsgCommand(const char *command, const char * messageList)
{
IncrementCommandTagNumber();
const char *formatString;
formatString = "%s uid %s %s\015\012";
const char *commandTag = GetServerCommandTag();
int protocolStringSize = PL_strlen(formatString) +
PL_strlen(messageList) + PL_strlen(command) +
PL_strlen(commandTag) + 1;
char *protocolString = (char *) PR_CALLOC( protocolStringSize );
if (protocolString)
{
PR_snprintf(protocolString, // string to create
protocolStringSize, // max size
formatString, // format string
commandTag, // command tag
command,
messageList);
nsresult rv = SendData(protocolString);
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(protocolString);
PR_Free(protocolString);
}
else
HandleMemoryFailure();
}
void
nsImapProtocol::UidExpunge(const nsCString &messageSet)
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" uid expunge ");
command.Append(messageSet);
command.Append(CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void
nsImapProtocol::Expunge()
{
uint32_t aclFlags = 0;
if (GetServerStateParser().ServerHasACLCapability() && m_imapMailFolderSink)
m_imapMailFolderSink->GetAclFlags(&aclFlags);
if (aclFlags && !(aclFlags & IMAP_ACL_EXPUNGE_FLAG))
return;
ProgressEventFunctionUsingName("imapStatusExpungingMailbox");
if(gCheckDeletedBeforeExpunge)
{
GetServerStateParser().ResetSearchResultSequence();
Search("SEARCH DELETED", false, false);
if (GetServerStateParser().LastCommandSuccessful())
{
nsImapSearchResultIterator *search = GetServerStateParser().CreateSearchResultIterator();
nsMsgKey key = search->GetNextMessageNumber();
delete search;
if (key == 0)
return; //no deleted messages to expunge (bug 235004)
}
}
IncrementCommandTagNumber();
nsAutoCString command(GetServerCommandTag());
command.Append(" expunge" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void
nsImapProtocol::HandleMemoryFailure()
{
PR_CEnterMonitor(this);
// **** jefft fix me!!!!!! ******
// m_imapThreadIsRunning = false;
// SetConnectionStatus(-1);
PR_CExitMonitor(this);
}
void nsImapProtocol::HandleCurrentUrlError()
{
// This is to handle a move/copy failing, especially because the user
// cancelled the password prompt.
(void) m_runningUrl->GetImapAction(&m_imapAction);
if (m_imapAction == nsIImapUrl::nsImapOfflineToOnlineMove || m_imapAction == nsIImapUrl::nsImapAppendMsgFromFile
|| m_imapAction == nsIImapUrl::nsImapAppendDraftFromFile)
{
if (m_imapMailFolderSink)
m_imapMailFolderSink->OnlineCopyCompleted(this, ImapOnlineCopyStateType::kFailedCopy);
}
}
void nsImapProtocol::StartTLS()
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" STARTTLS" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Capability()
{
ProgressEventFunctionUsingName("imapStatusCheckCompat");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" capability" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::ID()
{
if (!gAppName[0])
return;
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" ID (\"name\" \"");
command.Append(gAppName);
command.Append("\" \"version\" \"");
command.Append(gAppVersion);
command.Append("\")" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::EnableCondStore()
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" ENABLE CONDSTORE" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::StartCompressDeflate()
{
// only issue a compression request if we haven't already
if (!TestFlag(IMAP_ISSUED_COMPRESS_REQUEST))
{
SetFlag(IMAP_ISSUED_COMPRESS_REQUEST);
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" COMPRESS DEFLATE" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
{
ParseIMAPandCheckForNewMail();
if (GetServerStateParser().LastCommandSuccessful())
{
rv = BeginCompressing();
if (NS_FAILED(rv))
{
Log("CompressDeflate", nullptr, "failed to enable compression");
// we can't use this connection without compression any more, so die
ClearFlag(IMAP_CONNECTION_IS_OPEN);
TellThreadToDie();
SetConnectionStatus(rv);
return;
}
}
}
}
}
nsresult nsImapProtocol::BeginCompressing()
{
// wrap the streams in compression layers that compress or decompress
// all traffic.
RefPtr<nsMsgCompressIStream> new_in = new nsMsgCompressIStream();
if (!new_in)
return NS_ERROR_OUT_OF_MEMORY;
nsresult rv = new_in->InitInputStream(m_inputStream);
NS_ENSURE_SUCCESS(rv, rv);
m_inputStream = new_in;
RefPtr<nsMsgCompressOStream> new_out = new nsMsgCompressOStream();
if (!new_out)
return NS_ERROR_OUT_OF_MEMORY;
rv = new_out->InitOutputStream(m_outputStream);
NS_ENSURE_SUCCESS(rv, rv);
m_outputStream = new_out;
return rv;
}
void nsImapProtocol::Language()
{
// only issue the language request if we haven't done so already...
if (!TestFlag(IMAP_ISSUED_LANGUAGE_REQUEST))
{
SetFlag(IMAP_ISSUED_LANGUAGE_REQUEST);
ProgressEventFunctionUsingName("imapStatusCheckCompat");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
// extract the desired language attribute from prefs
nsresult rv = NS_OK;
// we need to parse out the first language out of this comma separated list....
// i.e if we have en,ja we only want to send en to the server.
if (mAcceptLanguages.get())
{
nsAutoCString extractedLanguage;
LossyCopyUTF16toASCII(mAcceptLanguages, extractedLanguage);
int32_t pos = extractedLanguage.FindChar(',');
if (pos > 0) // we have a comma separated list of languages...
extractedLanguage.SetLength(pos); // truncate everything after the first comma (including the comma)
if (extractedLanguage.IsEmpty())
return;
command.Append(" LANGUAGE ");
command.Append(extractedLanguage);
command.Append(CRLF);
rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(nullptr, true /* ignore bad or no result from the server for this command */);
}
}
}
void nsImapProtocol::EscapeUserNamePasswordString(const char *strToEscape, nsCString *resultStr)
{
if (strToEscape)
{
uint32_t i = 0;
uint32_t escapeStrlen = strlen(strToEscape);
for (i=0; i<escapeStrlen; i++)
{
if (strToEscape[i] == '\\' || strToEscape[i] == '\"')
{
resultStr->Append('\\');
}
resultStr->Append(strToEscape[i]);
}
}
}
#ifdef MOZ_MAILNEWS_OAUTH2
void nsImapProtocol::InitPrefAuthMethods(int32_t authMethodPrefValue,
nsIMsgIncomingServer *aServer)
#else
void nsImapProtocol::InitPrefAuthMethods(int32_t authMethodPrefValue)
#endif
{
// for m_prefAuthMethods, using the same flags as server capablities.
switch (authMethodPrefValue)
{
case nsMsgAuthMethod::none:
m_prefAuthMethods = kHasAuthNoneCapability;
break;
case nsMsgAuthMethod::old:
m_prefAuthMethods = kHasAuthOldLoginCapability;
break;
case nsMsgAuthMethod::passwordCleartext:
m_prefAuthMethods = kHasAuthOldLoginCapability |
kHasAuthLoginCapability | kHasAuthPlainCapability;
break;
case nsMsgAuthMethod::passwordEncrypted:
m_prefAuthMethods = kHasCRAMCapability;
break;
case nsMsgAuthMethod::NTLM:
m_prefAuthMethods = kHasAuthNTLMCapability | kHasAuthMSNCapability;
break;
case nsMsgAuthMethod::GSSAPI:
m_prefAuthMethods = kHasAuthGssApiCapability;
break;
case nsMsgAuthMethod::External:
m_prefAuthMethods = kHasAuthExternalCapability;
break;
case nsMsgAuthMethod::secure:
m_prefAuthMethods = kHasCRAMCapability |
kHasAuthGssApiCapability |
kHasAuthNTLMCapability | kHasAuthMSNCapability;
break;
default:
NS_ASSERTION(false, "IMAP: authMethod pref invalid");
// TODO log to error console
MOZ_LOG(IMAP, LogLevel::Error,
("IMAP: bad pref authMethod = %d\n", authMethodPrefValue));
// fall to any
MOZ_FALLTHROUGH;
case nsMsgAuthMethod::anything:
m_prefAuthMethods = kHasAuthOldLoginCapability |
kHasAuthLoginCapability | kHasAuthPlainCapability |
kHasCRAMCapability | kHasAuthGssApiCapability |
kHasAuthNTLMCapability | kHasAuthMSNCapability |
#ifdef MOZ_MAILNEWS_OAUTH2
kHasAuthExternalCapability | kHasXOAuth2Capability;
break;
case nsMsgAuthMethod::OAuth2:
m_prefAuthMethods = kHasXOAuth2Capability;
#else
kHasAuthExternalCapability;
#endif
break;
}
if (m_prefAuthMethods & kHasXOAuth2Capability)
mOAuth2Support = new mozilla::mailnews::OAuth2ThreadHelper(aServer);
// Disable OAuth2 support if we don't have the prefs installed.
if (m_prefAuthMethods & kHasXOAuth2Capability &&
(!mOAuth2Support || !mOAuth2Support->SupportsOAuth2()))
m_prefAuthMethods &= ~kHasXOAuth2Capability;
NS_ASSERTION(m_prefAuthMethods != kCapabilityUndefined,
"IMAP: InitPrefAuthMethods() didn't work");
}
/**
* Changes m_currentAuthMethod to pick the best remaining one
* which is allowed by server and prefs and not marked failed.
* The order of preference and trying of auth methods is encoded here.
*/
nsresult nsImapProtocol::ChooseAuthMethod()
{
eIMAPCapabilityFlags serverCaps = GetServerStateParser().GetCapabilityFlag();
eIMAPCapabilityFlags availCaps = serverCaps & m_prefAuthMethods & ~m_failedAuthMethods;
MOZ_LOG(IMAP, LogLevel::Debug, ("IMAP auth: server caps 0x%llx, pref 0x%llx, failed 0x%llx, avail caps 0x%llx",
serverCaps, m_prefAuthMethods, m_failedAuthMethods, availCaps));
#ifdef MOZ_MAILNEWS_OAUTH2
MOZ_LOG(IMAP, LogLevel::Debug, ("(GSSAPI = 0x%llx, CRAM = 0x%llx, NTLM = 0x%llx, "
"MSN = 0x%llx, PLAIN = 0x%llx,\n LOGIN = 0x%llx, old-style IMAP login = 0x%llx"
", auth external IMAP login = 0x%llx, OAUTH2 = 0x%llx)",
kHasAuthGssApiCapability, kHasCRAMCapability, kHasAuthNTLMCapability,
kHasAuthMSNCapability, kHasAuthPlainCapability, kHasAuthLoginCapability,
kHasAuthOldLoginCapability, kHasAuthExternalCapability, kHasXOAuth2Capability));
#else
MOZ_LOG(IMAP, LogLevel::Debug, ("(GSSAPI = 0x%llx, CRAM = 0x%llx, NTLM = 0x%llx, "
"MSN = 0x%llx, PLAIN = 0x%llx,\n LOGIN = 0x%llx, old-style IMAP login = 0x%llx"
", auth external IMAP login = 0x%llx",
kHasAuthGssApiCapability, kHasCRAMCapability, kHasAuthNTLMCapability,
kHasAuthMSNCapability, kHasAuthPlainCapability, kHasAuthLoginCapability,
kHasAuthOldLoginCapability, kHasAuthExternalCapability));
#endif
if (kHasAuthExternalCapability & availCaps)
m_currentAuthMethod = kHasAuthExternalCapability;
else if (kHasAuthGssApiCapability & availCaps)
m_currentAuthMethod = kHasAuthGssApiCapability;
else if (kHasCRAMCapability & availCaps)
m_currentAuthMethod = kHasCRAMCapability;
else if (kHasAuthNTLMCapability & availCaps)
m_currentAuthMethod = kHasAuthNTLMCapability;
else if (kHasAuthMSNCapability & availCaps)
m_currentAuthMethod = kHasAuthMSNCapability;
#ifdef MOZ_MAILNEWS_OAUTH2
else if (kHasXOAuth2Capability & availCaps)
m_currentAuthMethod = kHasXOAuth2Capability;
#endif
else if (kHasAuthPlainCapability & availCaps)
m_currentAuthMethod = kHasAuthPlainCapability;
else if (kHasAuthLoginCapability & availCaps)
m_currentAuthMethod = kHasAuthLoginCapability;
else if (kHasAuthOldLoginCapability & availCaps)
m_currentAuthMethod = kHasAuthOldLoginCapability;
else
{
MOZ_LOG(IMAP, LogLevel::Debug, ("no remaining auth method"));
m_currentAuthMethod = kCapabilityUndefined;
return NS_ERROR_FAILURE;
}
MOZ_LOG(IMAP, LogLevel::Debug, ("trying auth method 0x%llx", m_currentAuthMethod));
return NS_OK;
}
void nsImapProtocol::MarkAuthMethodAsFailed(eIMAPCapabilityFlags failedAuthMethod)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("marking auth method 0x%llx failed", failedAuthMethod));
m_failedAuthMethods |= failedAuthMethod;
}
/**
* Start over, trying all auth methods again
*/
void nsImapProtocol::ResetAuthMethods()
{
MOZ_LOG(IMAP, LogLevel::Debug, ("resetting (failed) auth methods"));
m_currentAuthMethod = kCapabilityUndefined;
m_failedAuthMethods = 0;
}
nsresult nsImapProtocol::AuthLogin(const char *userName, const nsCString &password, eIMAPCapabilityFlag flag)
{
ProgressEventFunctionUsingName("imapStatusSendingAuthLogin");
IncrementCommandTagNumber();
char * currentCommand=nullptr;
nsresult rv;
MOZ_LOG(IMAP, LogLevel::Debug, ("IMAP: trying auth method 0x%llx", m_currentAuthMethod));
if (flag & kHasAuthExternalCapability)
{
char *base64UserName = PL_Base64Encode(userName, strlen(userName), nullptr);
nsAutoCString command (GetServerCommandTag());
command.Append(" authenticate EXTERNAL " );
command.Append(base64UserName);
command.Append(CRLF);
PR_Free(base64UserName);
rv = SendData(command.get());
ParseIMAPandCheckForNewMail();
nsImapServerResponseParser &parser = GetServerStateParser();
if (parser.LastCommandSuccessful())
return NS_OK;
parser.SetCapabilityFlag(parser.GetCapabilityFlag() & ~kHasAuthExternalCapability);
}
else if (flag & kHasCRAMCapability)
{
NS_ENSURE_TRUE(m_imapServerSink, NS_ERROR_NULL_POINTER);
MOZ_LOG(IMAP, LogLevel::Debug, ("MD5 auth"));
// inform the server that we want to begin a CRAM authentication procedure...
nsAutoCString command (GetServerCommandTag());
command.Append(" authenticate CRAM-MD5" CRLF);
rv = SendData(command.get());
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail();
if (GetServerStateParser().LastCommandSuccessful())
{
char *digest = nullptr;
char *cramDigest = GetServerStateParser().fAuthChallenge;
char *decodedChallenge = PL_Base64Decode(cramDigest,
strlen(cramDigest), nullptr);
rv = m_imapServerSink->CramMD5Hash(decodedChallenge, password.get(), &digest);
PR_Free(decodedChallenge);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(digest, NS_ERROR_NULL_POINTER);
nsAutoCString encodedDigest;
char hexVal[8];
for (uint32_t j=0; j<16; j++)
{
PR_snprintf (hexVal,8, "%.2x", 0x0ff & (unsigned short)(digest[j]));
encodedDigest.Append(hexVal);
}
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s %s", userName, encodedDigest.get());
char *base64Str = PL_Base64Encode(m_dataOutputBuf, strlen(m_dataOutputBuf), nullptr);
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s" CRLF, base64Str);
PR_Free(base64Str);
PR_Free(digest);
rv = SendData(m_dataOutputBuf);
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail(command.get());
}
} // if CRAM response was received
else if (flag & kHasAuthGssApiCapability)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("MD5 auth"));
// Only try GSSAPI once - if it fails, its going to be because we don't
// have valid credentials
//MarkAuthMethodAsFailed(kHasAuthGssApiCapability);
// We do step1 first, so we don't try GSSAPI against a server which
// we can't get credentials for.
nsAutoCString response;
nsAutoCString service("imap@");
service.Append(m_realHostName);
rv = DoGSSAPIStep1(service.get(), userName, response);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString command (GetServerCommandTag());
command.Append(" authenticate GSSAPI" CRLF);
rv = SendData(command.get());
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail("AUTH GSSAPI");
if (GetServerStateParser().LastCommandSuccessful())
{
response += CRLF;
rv = SendData(response.get());
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail(command.get());
nsresult gssrv = NS_OK;
while (GetServerStateParser().LastCommandSuccessful() &&
NS_SUCCEEDED(gssrv) && gssrv != NS_SUCCESS_AUTH_FINISHED)
{
nsCString challengeStr(GetServerStateParser().fAuthChallenge);
gssrv = DoGSSAPIStep2(challengeStr, response);
if (NS_SUCCEEDED(gssrv))
{
response += CRLF;
rv = SendData(response.get());
}
else
rv = SendData("*" CRLF);
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail(command.get());
}
// TODO: whether it worked or not is shown by LastCommandSuccessful(), not gssrv, right?
}
}
else if (flag & (kHasAuthNTLMCapability | kHasAuthMSNCapability))
{
MOZ_LOG(IMAP, LogLevel::Debug, ("NTLM auth"));
nsAutoCString command (GetServerCommandTag());
command.Append((flag & kHasAuthNTLMCapability) ? " authenticate NTLM" CRLF
: " authenticate MSN" CRLF);
rv = SendData(command.get());
ParseIMAPandCheckForNewMail("AUTH NTLM"); // this just waits for ntlm step 1
if (GetServerStateParser().LastCommandSuccessful())
{
nsAutoCString cmd;
rv = DoNtlmStep1(userName, password.get(), cmd);
NS_ENSURE_SUCCESS(rv, rv);
cmd += CRLF;
rv = SendData(cmd.get());
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail(command.get());
if (GetServerStateParser().LastCommandSuccessful())
{
nsCString challengeStr(GetServerStateParser().fAuthChallenge);
nsCString response;
rv = DoNtlmStep2(challengeStr, response);
NS_ENSURE_SUCCESS(rv, rv);
response += CRLF;
rv = SendData(response.get());
ParseIMAPandCheckForNewMail(command.get());
}
}
}
else if (flag & kHasAuthPlainCapability)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("PLAIN auth"));
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s authenticate PLAIN" CRLF, GetServerCommandTag());
rv = SendData(m_dataOutputBuf);
NS_ENSURE_SUCCESS(rv, rv);
currentCommand = PL_strdup(m_dataOutputBuf); /* StrAllocCopy(currentCommand, GetOutputBuffer()); */
ParseIMAPandCheckForNewMail();
if (GetServerStateParser().LastCommandSuccessful())
{
// RFC 4616
char plainstr[512]; // placeholder for "<NUL>userName<NUL>password" TODO nsAutoCString
int len = 1; // count for first <NUL> char
memset(plainstr, 0, 512);
PR_snprintf(&plainstr[1], 510, "%s", userName);
len += PL_strlen(userName);
len++; // count for second <NUL> char
PR_snprintf(&plainstr[len], 511-len, "%s", password.get());
len += password.Length();
char *base64Str = PL_Base64Encode(plainstr, len, nullptr);
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s" CRLF, base64Str);
PR_Free(base64Str);
bool isResend = false;
while (true)
{
// Send authentication string (true: suppress logging the string).
rv = SendData(m_dataOutputBuf, true);
if (NS_FAILED(rv))
break;
ParseIMAPandCheckForNewMail(currentCommand);
if (!GetServerStateParser().WaitingForMoreClientInput())
break;
// Server is asking for authentication string again. So we send the
// same string again although we already know that it will be
// rejected again. We do that to get a firm authentication failure
// instead of a resend request. That keeps things in order before
// failing "authenticate PLAIN" and trying another method if capable.
if (isResend)
{
rv = NS_ERROR_FAILURE;
break;
}
isResend = true;
}
} // if the last command succeeded
} // if auth plain capability
else if (flag & kHasAuthLoginCapability)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("LOGIN auth"));
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s authenticate LOGIN" CRLF, GetServerCommandTag());
rv = SendData(m_dataOutputBuf);
NS_ENSURE_SUCCESS(rv, rv);
currentCommand = PL_strdup(m_dataOutputBuf);
ParseIMAPandCheckForNewMail();
if (GetServerStateParser().LastCommandSuccessful())
{
char *base64Str = PL_Base64Encode(userName, PL_strlen(userName), nullptr);
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s" CRLF, base64Str);
PR_Free(base64Str);
rv = SendData(m_dataOutputBuf, true /* suppress logging */);
if (NS_SUCCEEDED(rv))
{
ParseIMAPandCheckForNewMail(currentCommand);
if (GetServerStateParser().LastCommandSuccessful())
{
base64Str = PL_Base64Encode(password.get(), password.Length(), nullptr);
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE, "%s" CRLF, base64Str);
PR_Free(base64Str);
rv = SendData(m_dataOutputBuf, true /* suppress logging */);
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(currentCommand);
} // if last command successful
} // if last command successful
} // if last command successful
} // if has auth login capability
else if (flag & kHasAuthOldLoginCapability)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("old-style auth"));
ProgressEventFunctionUsingName("imapStatusSendingLogin");
IncrementCommandTagNumber();
nsCString command (GetServerCommandTag());
nsAutoCString escapedUserName;
command.Append(" login \"");
EscapeUserNamePasswordString(userName, &escapedUserName);
command.Append(escapedUserName);
command.Append("\" \"");
// if the password contains a \, login will fail
// turn foo\bar into foo\\bar
nsAutoCString correctedPassword;
EscapeUserNamePasswordString(password.get(), &correctedPassword);
command.Append(correctedPassword);
command.Append("\"" CRLF);
rv = SendData(command.get(), true /* suppress logging */);
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail();
}
#ifdef MOZ_MAILNEWS_OAUTH2
else if (flag & kHasXOAuth2Capability)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("XOAUTH2 auth"));
// Get the XOAuth2 base64 string.
NS_ASSERTION(mOAuth2Support,
"What are we doing here without OAuth2 helper?");
if (!mOAuth2Support)
return NS_ERROR_UNEXPECTED;
nsAutoCString base64Str;
mOAuth2Support->GetXOAuth2String(base64Str);
mOAuth2Support = nullptr; // Its purpose has been served.
if (base64Str.IsEmpty())
{
MOZ_LOG(IMAP, LogLevel::Debug, ("OAuth2 failed"));
return NS_ERROR_FAILURE;
}
// Send the data on the network.
nsAutoCString command (GetServerCommandTag());
command += " AUTHENTICATE XOAUTH2 ";
command += base64Str;
command += CRLF;
rv = SendData(command.get(), true /* suppress logging */);
NS_ENSURE_SUCCESS(rv, rv);
ParseIMAPandCheckForNewMail();
}
#endif
else if (flag & kHasAuthNoneCapability)
{
// TODO What to do? "login <username>" like POP?
return NS_ERROR_NOT_IMPLEMENTED;
}
else
{
MOZ_LOG(IMAP, LogLevel::Error, ("flags param has no auth scheme selected"));
return NS_ERROR_ILLEGAL_VALUE;
}
PR_Free(currentCommand);
NS_ENSURE_SUCCESS(rv, rv);
return GetServerStateParser().LastCommandSuccessful() ?
NS_OK : NS_ERROR_FAILURE;
}
void nsImapProtocol::OnLSubFolders()
{
// **** use to find out whether Drafts, Sent, & Templates folder
// exists or not even the user didn't subscribe to it
char *mailboxName = OnCreateServerSourceFolderPathString();
if (mailboxName)
{
ProgressEventFunctionUsingName("imapStatusLookingForMailbox");
IncrementCommandTagNumber();
PR_snprintf(m_dataOutputBuf, OUTPUT_BUFFER_SIZE,"%s list \"\" \"%s\"" CRLF, GetServerCommandTag(), mailboxName);
nsresult rv = SendData(m_dataOutputBuf);
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
PR_Free(mailboxName);
}
else
{
HandleMemoryFailure();
}
}
void nsImapProtocol::OnAppendMsgFromFile()
{
nsCOMPtr<nsIFile> file;
nsresult rv = NS_OK;
rv = m_runningUrl->GetMsgFile(getter_AddRefs(file));
if (NS_SUCCEEDED(rv) && file)
{
char *mailboxName = OnCreateServerSourceFolderPathString();
if (mailboxName)
{
imapMessageFlagsType flagsToSet = 0;
uint32_t msgFlags = 0;
PRTime date = 0;
nsCString keywords;
if (m_imapMessageSink)
m_imapMessageSink->GetCurMoveCopyMessageInfo(m_runningUrl, &date,
keywords, &msgFlags);
if (msgFlags & nsMsgMessageFlags::Read)
flagsToSet |= kImapMsgSeenFlag;
if (msgFlags & nsMsgMessageFlags::MDNReportSent)
flagsToSet |= kImapMsgMDNSentFlag;
// convert msg flag label (0xE000000) to imap flag label (0x0E00)
if (msgFlags & nsMsgMessageFlags::Labels)
flagsToSet |= (msgFlags & nsMsgMessageFlags::Labels) >> 16;
if (msgFlags & nsMsgMessageFlags::Marked)
flagsToSet |= kImapMsgFlaggedFlag;
if (msgFlags & nsMsgMessageFlags::Replied)
flagsToSet |= kImapMsgAnsweredFlag;
if (msgFlags & nsMsgMessageFlags::Forwarded)
flagsToSet |= kImapMsgForwardedFlag;
// If the message copied was a draft, flag it as such
nsImapAction imapAction;
rv = m_runningUrl->GetImapAction(&imapAction);
if (NS_SUCCEEDED(rv) && (imapAction == nsIImapUrl::nsImapAppendDraftFromFile))
flagsToSet |= kImapMsgDraftFlag;
UploadMessageFromFile(file, mailboxName, date, flagsToSet, keywords);
PR_Free( mailboxName );
}
else
{
HandleMemoryFailure();
}
}
}
void nsImapProtocol::UploadMessageFromFile (nsIFile* file,
const char* mailboxName,
PRTime date,
imapMessageFlagsType flags,
nsCString &keywords)
{
if (!file || !mailboxName) return;
IncrementCommandTagNumber();
int64_t fileSize = 0;
int64_t totalSize;
uint32_t readCount;
char *dataBuffer = nullptr;
nsCString command(GetServerCommandTag());
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
nsresult rv;
bool eof = false;
nsCString flagString;
nsCOMPtr <nsIInputStream> fileInputStream;
if (!escapedName.IsEmpty())
{
command.Append(" append \"");
command.Append(escapedName);
command.Append("\"");
if (flags || keywords.Length())
{
command.Append(" (");
if (flags)
{
SetupMessageFlagsString(flagString, flags,
GetServerStateParser().SupportsUserFlags());
command.Append(flagString);
}
if (keywords.Length())
{
if (flags)
command.Append(' ');
command.Append(keywords);
}
command.Append(")");
}
// date should never be 0, but just in case...
if (date)
{
/* Use PR_FormatTimeUSEnglish() to format the date in US English format,
then figure out what our local GMT offset is, and append it (since
PR_FormatTimeUSEnglish() can't do that.) Generate four digit years as
per RFC 1123 (superceding RFC 822.)
*/
char szDateTime[64];
char dateStr[100];
PRExplodedTime exploded;
PR_ExplodeTime(date, PR_LocalTimeParameters, &exploded);
PR_FormatTimeUSEnglish(szDateTime, sizeof(szDateTime), "%d-%b-%Y %H:%M:%S", &exploded);
PRExplodedTime now;
PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &now);
int gmtoffset = (now.tm_params.tp_gmt_offset + now.tm_params.tp_dst_offset) / 60;
PR_snprintf(dateStr, sizeof(dateStr),
" \"%s %c%02d%02d\"",
szDateTime,
(gmtoffset >= 0 ? '+' : '-'),
((gmtoffset >= 0 ? gmtoffset : -gmtoffset) / 60),
((gmtoffset >= 0 ? gmtoffset : -gmtoffset) % 60));
command.Append(dateStr);
}
command.Append(" {");
dataBuffer = (char*) PR_CALLOC(COPY_BUFFER_SIZE+1);
if (!dataBuffer) goto done;
rv = file->GetFileSize(&fileSize);
NS_ASSERTION(fileSize, "got empty file in UploadMessageFromFile");
if (NS_FAILED(rv) || !fileSize) goto done;
rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), file);
if (NS_FAILED(rv) || !fileInputStream) goto done;
command.AppendInt((int32_t)fileSize);
// Set useLiteralPlus to true if server has capability LITERAL+ and
// LITERAL+ useage is enabled in the config editor,
// i.e., "mail.imap.use_literal_plus" = true.
bool useLiteralPlus = (GetServerStateParser().GetCapabilityFlag() &
kLiteralPlusCapability) && gUseLiteralPlus;
if (useLiteralPlus)
command.Append("+}" CRLF);
else
command.Append("}" CRLF);
rv = SendData(command.get());
if (NS_FAILED(rv)) goto done;
if (!useLiteralPlus)
{
ParseIMAPandCheckForNewMail();
if (!GetServerStateParser().LastCommandSuccessful())
goto done;
}
totalSize = fileSize;
readCount = 0;
while(NS_SUCCEEDED(rv) && !eof && totalSize > 0)
{
rv = fileInputStream->Read(dataBuffer, COPY_BUFFER_SIZE, &readCount);
if (NS_SUCCEEDED(rv) && !readCount)
rv = NS_ERROR_FAILURE;
if (NS_SUCCEEDED(rv))
{
NS_ASSERTION(readCount <= (uint32_t) totalSize, "got more bytes than there should be");
dataBuffer[readCount] = 0;
rv = SendData(dataBuffer);
totalSize -= readCount;
PercentProgressUpdateEvent(nullptr, fileSize - totalSize, fileSize);
}
}
if (NS_SUCCEEDED(rv))
{
rv = SendData(CRLF); // complete the append
ParseIMAPandCheckForNewMail(command.get());
nsImapAction imapAction;
m_runningUrl->GetImapAction(&imapAction);
if (GetServerStateParser().LastCommandSuccessful() && (
imapAction == nsIImapUrl::nsImapAppendDraftFromFile || imapAction==nsIImapUrl::nsImapAppendMsgFromFile))
{
if (GetServerStateParser().GetCapabilityFlag() &
kUidplusCapability)
{
nsMsgKey newKey = GetServerStateParser().CurrentResponseUID();
if (m_imapMailFolderSink)
m_imapMailFolderSink->SetAppendMsgUid(newKey, m_runningUrl);
// Courier imap server seems to have problems with recently
// appended messages. Noop seems to clear its confusion.
if (FolderIsSelected(mailboxName))
Noop();
nsCString oldMsgId;
rv = m_runningUrl->GetListOfMessageIds(oldMsgId);
if (NS_SUCCEEDED(rv) && !oldMsgId.IsEmpty())
{
bool idsAreUids = true;
m_runningUrl->MessageIdsAreUids(&idsAreUids);
Store(oldMsgId, "+FLAGS (\\Deleted)", idsAreUids);
UidExpunge(oldMsgId);
}
}
// for non UIDPLUS servers,
// this code used to check for imapAction==nsIImapUrl::nsImapAppendMsgFromFile, which
// meant we'd get into this code whenever sending a message, as well
// as when copying messages to an imap folder from local folders or an other imap server.
// This made sending a message slow when there was a large sent folder. I don't believe
// this code worked anyway.
else if (m_imapMailFolderSink && imapAction == nsIImapUrl::nsImapAppendDraftFromFile )
{ // *** code me to search for the newly appended message
// go to selected state
nsCString messageId;
rv = m_imapMailFolderSink->GetMessageId(m_runningUrl, messageId);
if (NS_SUCCEEDED(rv) && !messageId.IsEmpty() &&
GetServerStateParser().LastCommandSuccessful())
{
// if the appended to folder isn't selected in the connection,
// select it.
if (!FolderIsSelected(mailboxName))
SelectMailbox(mailboxName);
else
Noop(); // See if this makes SEARCH work on the newly appended msg.
if (GetServerStateParser().LastCommandSuccessful())
{
command = "SEARCH UNDELETED HEADER Message-ID ";
command.Append(messageId);
// Clean up result sequence before issuing the cmd.
GetServerStateParser().ResetSearchResultSequence();
Search(command.get(), true, false);
if (GetServerStateParser().LastCommandSuccessful())
{
nsMsgKey newkey = nsMsgKey_None;
nsImapSearchResultIterator *searchResult =
GetServerStateParser().CreateSearchResultIterator();
newkey = searchResult->GetNextMessageNumber();
delete searchResult;
if (newkey != nsMsgKey_None)
m_imapMailFolderSink->SetAppendMsgUid(newkey, m_runningUrl);
}
}
}
}
}
}
}
done:
PR_Free(dataBuffer);
if (fileInputStream)
fileInputStream->Close();
}
//caller must free using PR_Free
char * nsImapProtocol::OnCreateServerSourceFolderPathString()
{
char *sourceMailbox = nullptr;
char hierarchyDelimiter = 0;
char onlineDelimiter = 0;
m_runningUrl->GetOnlineSubDirSeparator(&hierarchyDelimiter);
if (m_imapMailFolderSink)
m_imapMailFolderSink->GetOnlineDelimiter(&onlineDelimiter);
if (onlineDelimiter != kOnlineHierarchySeparatorUnknown &&
onlineDelimiter != hierarchyDelimiter)
m_runningUrl->SetOnlineSubDirSeparator(onlineDelimiter);
m_runningUrl->CreateServerSourceFolderPathString(&sourceMailbox);
return sourceMailbox;
}
//caller must free using PR_Free, safe to call from ui thread
char * nsImapProtocol::GetFolderPathString()
{
char *sourceMailbox = nullptr;
char onlineSubDirDelimiter = 0;
char hierarchyDelimiter = 0;
nsCOMPtr <nsIMsgFolder> msgFolder;
m_runningUrl->GetOnlineSubDirSeparator(&onlineSubDirDelimiter);
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_runningUrl);
mailnewsUrl->GetFolder(getter_AddRefs(msgFolder));
if (msgFolder)
{
nsCOMPtr <nsIMsgImapMailFolder> imapFolder = do_QueryInterface(msgFolder);
if (imapFolder)
{
imapFolder->GetHierarchyDelimiter(&hierarchyDelimiter);
if (hierarchyDelimiter != kOnlineHierarchySeparatorUnknown &&
onlineSubDirDelimiter != hierarchyDelimiter)
m_runningUrl->SetOnlineSubDirSeparator(hierarchyDelimiter);
}
}
m_runningUrl->CreateServerSourceFolderPathString(&sourceMailbox);
return sourceMailbox;
}
nsresult nsImapProtocol::CreateServerSourceFolderPathString(char **result)
{
NS_ENSURE_ARG(result);
*result = OnCreateServerSourceFolderPathString();
return (*result) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
//caller must free using PR_Free
char * nsImapProtocol::OnCreateServerDestinationFolderPathString()
{
char *destinationMailbox = nullptr;
char hierarchyDelimiter = 0;
char onlineDelimiter = 0;
m_runningUrl->GetOnlineSubDirSeparator(&hierarchyDelimiter);
if (m_imapMailFolderSink)
m_imapMailFolderSink->GetOnlineDelimiter(&onlineDelimiter);
if (onlineDelimiter != kOnlineHierarchySeparatorUnknown &&
onlineDelimiter != hierarchyDelimiter)
m_runningUrl->SetOnlineSubDirSeparator(onlineDelimiter);
m_runningUrl->CreateServerDestinationFolderPathString(&destinationMailbox);
return destinationMailbox;
}
void nsImapProtocol::OnCreateFolder(const char * aSourceMailbox)
{
bool created = CreateMailboxRespectingSubscriptions(aSourceMailbox);
if (created)
{
m_hierarchyNameState = kListingForCreate;
nsCString mailboxWODelim(aSourceMailbox);
RemoveHierarchyDelimiter(mailboxWODelim);
List(mailboxWODelim.get(), false);
m_hierarchyNameState = kNoOperationInProgress;
}
else
FolderNotCreated(aSourceMailbox);
}
void nsImapProtocol::OnEnsureExistsFolder(const char * aSourceMailbox)
{
List(aSourceMailbox, false); // how to tell if that succeeded?
bool exists = false;
// try converting aSourceMailbox to canonical format
nsIMAPNamespace *nsForMailbox = nullptr;
m_hostSessionList->GetNamespaceForMailboxForHost(GetImapServerKey(),
aSourceMailbox, nsForMailbox);
// NS_ASSERTION (nsForMailbox, "Oops .. null nsForMailbox\n");
nsCString name;
if (nsForMailbox)
m_runningUrl->AllocateCanonicalPath(aSourceMailbox,
nsForMailbox->GetDelimiter(),
getter_Copies(name));
else
m_runningUrl->AllocateCanonicalPath(aSourceMailbox,
kOnlineHierarchySeparatorUnknown,
getter_Copies(name));
if (m_imapServerSink)
m_imapServerSink->FolderVerifiedOnline(name, &exists);
if (exists)
{
Subscribe(aSourceMailbox);
}
else
{
bool created = CreateMailboxRespectingSubscriptions(aSourceMailbox);
if (created)
{
List(aSourceMailbox, false);
}
}
if (!GetServerStateParser().LastCommandSuccessful())
FolderNotCreated(aSourceMailbox);
}
void nsImapProtocol::OnSubscribe(const char * sourceMailbox)
{
Subscribe(sourceMailbox);
}
void nsImapProtocol::OnUnsubscribe(const char * sourceMailbox)
{
// When we try to auto-unsubscribe from \Noselect folders,
// some servers report errors if we were already unsubscribed
// from them.
bool lastReportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false);
Unsubscribe(sourceMailbox);
GetServerStateParser().SetReportingErrors(lastReportingErrors);
}
void nsImapProtocol::RefreshACLForFolderIfNecessary(const char *mailboxName)
{
if (GetServerStateParser().ServerHasACLCapability())
{
if (!m_folderNeedsACLRefreshed && m_imapMailFolderSink)
m_imapMailFolderSink->GetFolderNeedsACLListed(&m_folderNeedsACLRefreshed);
if (m_folderNeedsACLRefreshed)
{
RefreshACLForFolder(mailboxName);
m_folderNeedsACLRefreshed = false;
}
}
}
void nsImapProtocol::RefreshACLForFolder(const char *mailboxName)
{
nsIMAPNamespace *ns = nullptr;
m_hostSessionList->GetNamespaceForMailboxForHost(GetImapServerKey(), mailboxName, ns);
if (ns)
{
switch (ns->GetType())
{
case kPersonalNamespace:
// It's a personal folder, most likely.
// I find it hard to imagine a server that supports ACL that doesn't support NAMESPACE,
// so most likely we KNOW that this is a personal, rather than the default, namespace.
// First, clear what we have.
ClearAllFolderRights();
// Now, get the new one.
GetMyRightsForFolder(mailboxName);
if (m_imapMailFolderSink)
{
uint32_t aclFlags = 0;
if (NS_SUCCEEDED(m_imapMailFolderSink->GetAclFlags(&aclFlags)) && aclFlags & IMAP_ACL_ADMINISTER_FLAG)
GetACLForFolder(mailboxName);
}
// We're all done, refresh the icon/flags for this folder
RefreshFolderACLView(mailboxName, ns);
break;
default:
// We know it's a public folder or other user's folder.
// We only want our own rights
// First, clear what we have
ClearAllFolderRights();
// Now, get the new one.
GetMyRightsForFolder(mailboxName);
// We're all done, refresh the icon/flags for this folder
RefreshFolderACLView(mailboxName, ns);
break;
}
}
else
{
// no namespace, not even default... can this happen?
NS_ASSERTION(false, "couldn't get namespace");
}
}
void nsImapProtocol::RefreshFolderACLView(const char *mailboxName, nsIMAPNamespace *nsForMailbox)
{
nsCString canonicalMailboxName;
if (nsForMailbox)
m_runningUrl->AllocateCanonicalPath(mailboxName, nsForMailbox->GetDelimiter(), getter_Copies(canonicalMailboxName));
else
m_runningUrl->AllocateCanonicalPath(mailboxName, kOnlineHierarchySeparatorUnknown, getter_Copies(canonicalMailboxName));
if (m_imapServerSink)
m_imapServerSink->RefreshFolderRights(canonicalMailboxName);
}
void nsImapProtocol::GetACLForFolder(const char *mailboxName)
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
command.Append(" getacl \"");
command.Append(escapedName);
command.Append("\"" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::OnRefreshAllACLs()
{
m_hierarchyNameState = kListingForInfoOnly;
nsIMAPMailboxInfo *mb = NULL;
// This will fill in the list
List("*", true);
int32_t total = m_listedMailboxList.Length(), count = 0;
GetServerStateParser().SetReportingErrors(false);
for (int32_t i = 0; i < total; i++)
{
mb = m_listedMailboxList.ElementAt(i);
if (mb) // paranoia
{
char *onlineName = nullptr;
m_runningUrl->AllocateServerPath(PromiseFlatCString(mb->GetMailboxName()).get(), mb->GetDelimiter(), &onlineName);
if (onlineName)
{
RefreshACLForFolder(onlineName);
NS_Free(onlineName);
}
PercentProgressUpdateEvent(NULL, count, total);
delete mb;
count++;
}
}
m_listedMailboxList.Clear();
PercentProgressUpdateEvent(NULL, 100, 100);
GetServerStateParser().SetReportingErrors(true);
m_hierarchyNameState = kNoOperationInProgress;
}
// any state commands
void nsImapProtocol::Logout(bool shuttingDown /* = false */,
bool waitForResponse /* = true */)
{
if (!shuttingDown)
ProgressEventFunctionUsingName("imapStatusLoggingOut");
/******************************************************************
* due to the undo functionality we cannot issule a close when logout; there
* is no way to do an undo if the message has been permanently expunge
* jt - 07/12/1999
bool closeNeeded = GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kFolderSelected;
if (closeNeeded && GetDeleteIsMoveToTrash())
Close();
********************/
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" logout" CRLF);
nsresult rv = SendData(command.get());
if (m_transport && shuttingDown)
m_transport->SetTimeout(nsISocketTransport::TIMEOUT_READ_WRITE, 5);
// the socket may be dead before we read the response, so drop it.
if (NS_SUCCEEDED(rv) && waitForResponse)
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Noop()
{
//ProgressUpdateEvent("noop...");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" noop" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::XServerInfo()
{
ProgressEventFunctionUsingName("imapGettingServerInfo");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" XSERVERINFO MANAGEACCOUNTURL MANAGELISTSURL MANAGEFILTERSURL" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Netscape()
{
ProgressEventFunctionUsingName("imapGettingServerInfo");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" netscape" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::XMailboxInfo(const char *mailboxName)
{
ProgressEventFunctionUsingName("imapGettingMailboxInfo");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" XMAILBOXINFO \"");
command.Append(mailboxName);
command.Append("\" MANAGEURL POSTURL" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Namespace()
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" namespace" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::MailboxData()
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" mailboxdata" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::GetMyRightsForFolder(const char *mailboxName)
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
if (MailboxIsNoSelectMailbox(escapedName.get()))
return; // Don't issue myrights on Noselect folder
command.Append(" myrights \"");
command.Append(escapedName);
command.Append("\"" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
bool nsImapProtocol::FolderIsSelected(const char *mailboxName)
{
return (GetServerStateParser().GetIMAPstate() ==
nsImapServerResponseParser::kFolderSelected && GetServerStateParser().GetSelectedMailboxName() &&
PL_strcmp(GetServerStateParser().GetSelectedMailboxName(),
mailboxName) == 0);
}
void nsImapProtocol::OnStatusForFolder(const char *mailboxName)
{
if (FolderIsSelected(mailboxName))
{
int32_t prevNumMessages = GetServerStateParser().NumberOfMessages();
Noop();
// OnNewIdleMessages will cause the ui thread to update the folder
if (m_imapMailFolderSink && (GetServerStateParser().NumberOfRecentMessages()
|| prevNumMessages != GetServerStateParser().NumberOfMessages()))
m_imapMailFolderSink->OnNewIdleMessages();
return;
}
IncrementCommandTagNumber();
nsAutoCString command(GetServerCommandTag());
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
command.Append(" STATUS \"");
command.Append(escapedName);
command.Append("\" (UIDNEXT MESSAGES UNSEEN RECENT)" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
if (GetServerStateParser().LastCommandSuccessful())
{
nsImapMailboxSpec *new_spec = GetServerStateParser().CreateCurrentMailboxSpec(mailboxName);
if (new_spec && m_imapMailFolderSink)
m_imapMailFolderSink->UpdateImapMailboxStatus(this, new_spec);
NS_IF_RELEASE(new_spec);
}
}
void nsImapProtocol::OnListFolder(const char * aSourceMailbox, bool aBool)
{
List(aSourceMailbox, aBool);
}
// Returns true if the mailbox is a NoSelect mailbox.
// If we don't know about it, returns false.
bool nsImapProtocol::MailboxIsNoSelectMailbox(const char *mailboxName)
{
bool rv = false;
nsIMAPNamespace *nsForMailbox = nullptr;
m_hostSessionList->GetNamespaceForMailboxForHost(GetImapServerKey(),
mailboxName, nsForMailbox);
// NS_ASSERTION (nsForMailbox, "Oops .. null nsForMailbox\n");
nsCString name;
if (nsForMailbox)
m_runningUrl->AllocateCanonicalPath(mailboxName,
nsForMailbox->GetDelimiter(),
getter_Copies(name));
else
m_runningUrl->AllocateCanonicalPath(mailboxName,
kOnlineHierarchySeparatorUnknown,
getter_Copies(name));
if (name.IsEmpty())
return false;
NS_ASSERTION(m_imapServerSink, "unexpected, no imap server sink, see bug #194335");
if (m_imapServerSink)
m_imapServerSink->FolderIsNoSelect(name, &rv);
return rv;
}
nsresult nsImapProtocol::SetFolderAdminUrl(const char *mailboxName)
{
nsresult rv = NS_ERROR_NULL_POINTER; // if m_imapServerSink is null, rv will be this.
nsIMAPNamespace *nsForMailbox = nullptr;
m_hostSessionList->GetNamespaceForMailboxForHost(GetImapServerKey(),
mailboxName, nsForMailbox);
nsCString name;
if (nsForMailbox)
m_runningUrl->AllocateCanonicalPath(mailboxName,
nsForMailbox->GetDelimiter(),
getter_Copies(name));
else
m_runningUrl->AllocateCanonicalPath(mailboxName,
kOnlineHierarchySeparatorUnknown,
getter_Copies(name));
if (m_imapServerSink)
rv = m_imapServerSink->SetFolderAdminURL(name, nsDependentCString(GetServerStateParser().GetManageFolderUrl()));
return rv;
}
// returns true is the delete succeeded (regardless of subscription changes)
bool nsImapProtocol::DeleteMailboxRespectingSubscriptions(const char *mailboxName)
{
bool rv = true;
if (!MailboxIsNoSelectMailbox(mailboxName))
{
// Only try to delete it if it really exists
DeleteMailbox(mailboxName);
rv = GetServerStateParser().LastCommandSuccessful();
}
// We can unsubscribe even if the mailbox doesn't exist.
if (rv && m_autoUnsubscribe) // auto-unsubscribe is on
{
bool reportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false);
Unsubscribe(mailboxName);
GetServerStateParser().SetReportingErrors(reportingErrors);
}
return (rv);
}
// returns true is the rename succeeded (regardless of subscription changes)
// reallyRename tells us if we should really do the rename (true) or if we should just move subscriptions (false)
bool nsImapProtocol::RenameMailboxRespectingSubscriptions(const char *existingName, const char *newName, bool reallyRename)
{
bool rv = true;
if (reallyRename && !MailboxIsNoSelectMailbox(existingName))
{
RenameMailbox(existingName, newName);
rv = GetServerStateParser().LastCommandSuccessful();
}
if (rv)
{
if (m_autoSubscribe) // if auto-subscribe is on
{
bool reportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false);
Subscribe(newName);
GetServerStateParser().SetReportingErrors(reportingErrors);
}
if (m_autoUnsubscribe) // if auto-unsubscribe is on
{
bool reportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false);
Unsubscribe(existingName);
GetServerStateParser().SetReportingErrors(reportingErrors);
}
}
return (rv);
}
bool nsImapProtocol::RenameHierarchyByHand(const char *oldParentMailboxName,
const char *newParentMailboxName)
{
bool renameSucceeded = true;
char onlineDirSeparator = kOnlineHierarchySeparatorUnknown;
m_deletableChildren = new nsTArray<char*>();
bool nonHierarchicalRename =
((GetServerStateParser().GetCapabilityFlag() & kNoHierarchyRename)
|| MailboxIsNoSelectMailbox(oldParentMailboxName));
if (m_deletableChildren)
{
m_hierarchyNameState = kDeleteSubFoldersInProgress;
nsIMAPNamespace *ns = nullptr;
m_hostSessionList->GetNamespaceForMailboxForHost(GetImapServerKey(),
oldParentMailboxName,
ns); // for delimiter
if (!ns)
{
if (!PL_strcasecmp(oldParentMailboxName, "INBOX"))
m_hostSessionList->GetDefaultNamespaceOfTypeForHost(GetImapServerKey(),
kPersonalNamespace,
ns);
}
if (ns)
{
nsCString pattern(oldParentMailboxName);
pattern += ns->GetDelimiter();
pattern += "*";
bool isUsingSubscription = false;
m_hostSessionList->GetHostIsUsingSubscription(GetImapServerKey(),
isUsingSubscription);
if (isUsingSubscription)
Lsub(pattern.get(), false);
else
List(pattern.get(), false);
}
m_hierarchyNameState = kNoOperationInProgress;
if (GetServerStateParser().LastCommandSuccessful())
renameSucceeded = // rename this, and move subscriptions
RenameMailboxRespectingSubscriptions(oldParentMailboxName,
newParentMailboxName, true);
size_t numberToDelete = m_deletableChildren->Length();
size_t childIndex;
for (childIndex = 0;
(childIndex < numberToDelete) && renameSucceeded; childIndex++)
{
// the imap parser has already converted to a non UTF7 string in the canonical
// format so convert it back
char *currentName = m_deletableChildren->ElementAt(childIndex);
if (currentName)
{
char *serverName = nullptr;
m_runningUrl->AllocateServerPath(currentName,
onlineDirSeparator,
&serverName);
PR_FREEIF(currentName);
currentName = serverName;
}
// calculate the new name and do the rename
nsCString newChildName(newParentMailboxName);
newChildName += (currentName + PL_strlen(oldParentMailboxName));
// Pass in 'nonHierarchicalRename' to determine if we should really
// reanme, or just move subscriptions.
renameSucceeded =
RenameMailboxRespectingSubscriptions(currentName,
newChildName.get(),
nonHierarchicalRename);
PR_FREEIF(currentName);
}
delete m_deletableChildren;
m_deletableChildren = nullptr;
}
return renameSucceeded;
}
bool nsImapProtocol::DeleteSubFolders(const char* selectedMailbox, bool &aDeleteSelf)
{
bool deleteSucceeded = true;
m_deletableChildren = new nsTArray<char*>();
if (m_deletableChildren)
{
bool folderDeleted = false;
m_hierarchyNameState = kDeleteSubFoldersInProgress;
nsCString pattern(selectedMailbox);
char onlineDirSeparator = kOnlineHierarchySeparatorUnknown;
m_runningUrl->GetOnlineSubDirSeparator(&onlineDirSeparator);
pattern.Append(onlineDirSeparator);
pattern.Append('*');
if (!pattern.IsEmpty())
{
List(pattern.get(), false);
}
m_hierarchyNameState = kNoOperationInProgress;
// this should be a short list so perform a sequential search for the
// longest name mailbox. Deleting the longest first will hopefully
// prevent the server from having problems about deleting parents
// ** jt - why? I don't understand this.
size_t numberToDelete = m_deletableChildren->Length();
size_t outerIndex, innerIndex;
// intelligently decide if myself(either plain format or following the dir-separator)
// is in the sub-folder list
bool folderInSubfolderList = false; // For Performance
char *selectedMailboxDir = nullptr;
{
int32_t length = strlen(selectedMailbox);
selectedMailboxDir = (char *)PR_MALLOC(length+2);
if( selectedMailboxDir ) // only do the intelligent test if there is enough memory
{
strcpy(selectedMailboxDir, selectedMailbox);
selectedMailboxDir[length] = onlineDirSeparator;
selectedMailboxDir[length+1] = '\0';
size_t i;
for( i=0; i<numberToDelete && !folderInSubfolderList; i++ )
{
char *currentName = m_deletableChildren->ElementAt(i);
if( !strcmp(currentName, selectedMailbox) || !strcmp(currentName, selectedMailboxDir) )
folderInSubfolderList = true;
}
}
}
deleteSucceeded = GetServerStateParser().LastCommandSuccessful();
for (outerIndex = 0;
(outerIndex < numberToDelete) && deleteSucceeded;
outerIndex++)
{
char* longestName = nullptr;
size_t longestIndex = 0; // fix bogus warning by initializing
for (innerIndex = 0;
innerIndex < m_deletableChildren->Length();
innerIndex++)
{
char *currentName = m_deletableChildren->ElementAt(innerIndex);
if (!longestName || strlen(longestName) < strlen(currentName))
{
longestName = currentName;
longestIndex = innerIndex;
}
}
// the imap parser has already converted to a non UTF7 string in
// the canonical format so convert it back
if (longestName)
{
char *serverName = nullptr;
m_deletableChildren->RemoveElementAt(longestIndex);
m_runningUrl->AllocateServerPath(longestName,
onlineDirSeparator,
&serverName);
PR_FREEIF(longestName);
longestName = serverName;
}
// some imap servers include the selectedMailbox in the list of
// subfolders of the selectedMailbox. Check for this so we don't
// delete the selectedMailbox (usually the trash and doing an
// empty trash)
// The Cyrus imap server ignores the "INBOX.Trash" constraining
// string passed to the list command. Be defensive and make sure
// we only delete children of the trash
if (longestName &&
strcmp(selectedMailbox, longestName) &&
!strncmp(selectedMailbox, longestName, strlen(selectedMailbox)))
{
if( selectedMailboxDir && !strcmp(selectedMailboxDir, longestName) ) // just myself
{
if( aDeleteSelf )
{
bool deleted = DeleteMailboxRespectingSubscriptions(longestName);
if (deleted)
FolderDeleted(longestName);
folderDeleted = deleted;
deleteSucceeded = deleted;
}
}
else
{
if (m_imapServerSink)
m_imapServerSink->ResetServerConnection(nsDependentCString(longestName));
bool deleted = false;
if( folderInSubfolderList ) // for performance
{
nsTArray<char*> *pDeletableChildren = m_deletableChildren;
m_deletableChildren = nullptr;
bool folderDeleted = true;
deleted = DeleteSubFolders(longestName, folderDeleted);
// longestName may have subfolder list including itself
if( !folderDeleted )
{
if (deleted)
deleted = DeleteMailboxRespectingSubscriptions(longestName);
if (deleted)
FolderDeleted(longestName);
}
m_deletableChildren = pDeletableChildren;
}
else
{
deleted = DeleteMailboxRespectingSubscriptions(longestName);
if (deleted)
FolderDeleted(longestName);
}
deleteSucceeded = deleted;
}
}
PR_FREEIF(longestName);
}
aDeleteSelf = folderDeleted; // feedback if myself is deleted
PR_Free(selectedMailboxDir);
delete m_deletableChildren;
m_deletableChildren = nullptr;
}
return deleteSucceeded;
}
void nsImapProtocol::FolderDeleted(const char *mailboxName)
{
char onlineDelimiter = kOnlineHierarchySeparatorUnknown;
nsCString orphanedMailboxName;
if (mailboxName)
{
m_runningUrl->AllocateCanonicalPath(mailboxName, onlineDelimiter,
getter_Copies(orphanedMailboxName));
if (m_imapServerSink)
m_imapServerSink->OnlineFolderDelete(orphanedMailboxName);
}
}
void nsImapProtocol::FolderNotCreated(const char *folderName)
{
if (folderName && m_imapServerSink)
m_imapServerSink->OnlineFolderCreateFailed(nsDependentCString(folderName));
}
void nsImapProtocol::FolderRenamed(const char *oldName,
const char *newName)
{
char onlineDelimiter = kOnlineHierarchySeparatorUnknown;
if ((m_hierarchyNameState == kNoOperationInProgress) ||
(m_hierarchyNameState == kListingForInfoAndDiscovery))
{
nsCString canonicalOldName, canonicalNewName;
m_runningUrl->AllocateCanonicalPath(oldName,
onlineDelimiter,
getter_Copies(canonicalOldName));
m_runningUrl->AllocateCanonicalPath(newName,
onlineDelimiter,
getter_Copies(canonicalNewName));
AutoProxyReleaseMsgWindow msgWindow;
GetMsgWindow(getter_AddRefs(msgWindow));
m_imapServerSink->OnlineFolderRename(msgWindow, canonicalOldName, canonicalNewName);
}
}
void nsImapProtocol::OnDeleteFolder(const char * sourceMailbox)
{
// intelligently delete the folder
bool folderDeleted = true;
bool deleted = DeleteSubFolders(sourceMailbox, folderDeleted);
if( !folderDeleted )
{
if (deleted)
deleted = DeleteMailboxRespectingSubscriptions(sourceMailbox);
if (deleted)
FolderDeleted(sourceMailbox);
}
}
void nsImapProtocol::RemoveMsgsAndExpunge()
{
uint32_t numberOfMessages = GetServerStateParser().NumberOfMessages();
if (numberOfMessages)
{
// Remove all msgs and expunge the folder (ie, compact it).
Store(NS_LITERAL_CSTRING("1:*"), "+FLAGS.SILENT (\\Deleted)", false); // use sequence #'s
if (GetServerStateParser().LastCommandSuccessful())
Expunge();
}
}
void nsImapProtocol::DeleteFolderAndMsgs(const char * sourceMailbox)
{
RemoveMsgsAndExpunge();
if (GetServerStateParser().LastCommandSuccessful())
{
// All msgs are deleted successfully - let's remove the folder itself.
bool reportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false);
OnDeleteFolder(sourceMailbox);
GetServerStateParser().SetReportingErrors(reportingErrors);
}
}
void nsImapProtocol::OnRenameFolder(const char * sourceMailbox)
{
char *destinationMailbox = OnCreateServerDestinationFolderPathString();
if (destinationMailbox)
{
bool renamed = RenameHierarchyByHand(sourceMailbox, destinationMailbox);
if (renamed)
FolderRenamed(sourceMailbox, destinationMailbox);
PR_Free( destinationMailbox);
}
else
HandleMemoryFailure();
}
void nsImapProtocol::OnMoveFolderHierarchy(const char * sourceMailbox)
{
char *destinationMailbox = OnCreateServerDestinationFolderPathString();
if (destinationMailbox)
{
nsCString newBoxName;
newBoxName.Adopt(destinationMailbox);
char onlineDirSeparator = kOnlineHierarchySeparatorUnknown;
m_runningUrl->GetOnlineSubDirSeparator(&onlineDirSeparator);
nsCString oldBoxName(sourceMailbox);
int32_t leafStart = oldBoxName.RFindChar(onlineDirSeparator);
nsCString leafName;
if (-1 == leafStart)
leafName = oldBoxName; // this is a root level box
else
leafName = Substring(oldBoxName, leafStart+1);
if ( !newBoxName.IsEmpty() )
newBoxName.Append(onlineDirSeparator);
newBoxName.Append(leafName);
bool renamed = RenameHierarchyByHand(sourceMailbox,
newBoxName.get());
if (renamed)
FolderRenamed(sourceMailbox, newBoxName.get());
}
else
HandleMemoryFailure();
}
void nsImapProtocol::FindMailboxesIfNecessary()
{
// biff should not discover mailboxes
bool foundMailboxesAlready = false;
nsImapAction imapAction;
// need to do this for every connection in order to see folders.
(void) m_runningUrl->GetImapAction(&imapAction);
nsresult rv = m_hostSessionList->GetHaveWeEverDiscoveredFoldersForHost(GetImapServerKey(), foundMailboxesAlready);
if (NS_SUCCEEDED(rv) && !foundMailboxesAlready &&
(imapAction != nsIImapUrl::nsImapBiff) &&
(imapAction != nsIImapUrl::nsImapVerifylogon) &&
(imapAction != nsIImapUrl::nsImapDiscoverAllBoxesUrl) &&
(imapAction != nsIImapUrl::nsImapUpgradeToSubscription) &&
!GetSubscribingNow())
DiscoverMailboxList();
}
void nsImapProtocol::DiscoverAllAndSubscribedBoxes()
{
// used for subscribe pane
// iterate through all namespaces
uint32_t count = 0;
m_hostSessionList->GetNumberOfNamespacesForHost(GetImapServerKey(), count);
for (uint32_t i = 0; i < count; i++ )
{
nsIMAPNamespace *ns = nullptr;
m_hostSessionList->GetNamespaceNumberForHost(GetImapServerKey(), i,
ns);
if (ns &&
gHideOtherUsersFromList ? (ns->GetType() != kOtherUsersNamespace)
: true)
{
const char *prefix = ns->GetPrefix();
if (prefix)
{
nsAutoCString inboxNameWithDelim("INBOX");
inboxNameWithDelim.Append(ns->GetDelimiter());
if (!gHideUnusedNamespaces && *prefix &&
PL_strcasecmp(prefix, inboxNameWithDelim.get())) /* only do it for
non-empty namespace prefixes */
{
// Explicitly discover each Namespace, just so they're
// there in the subscribe UI
nsImapMailboxSpec *boxSpec = new nsImapMailboxSpec;
if (boxSpec)
{
NS_ADDREF(boxSpec);
boxSpec->mFolderSelected = false;
boxSpec->mHostName.Assign(GetImapHostName());
boxSpec->mConnection = this;
boxSpec->mFlagState = nullptr;
boxSpec->mDiscoveredFromLsub = true;
boxSpec->mOnlineVerified = true;
boxSpec->mBoxFlags = kNoselect;
boxSpec->mHierarchySeparator = ns->GetDelimiter();
m_runningUrl->AllocateCanonicalPath(ns->GetPrefix(), ns->GetDelimiter(),
getter_Copies(boxSpec->mAllocatedPathName));
boxSpec->mNamespaceForFolder = ns;
boxSpec->mBoxFlags |= kNameSpace;
switch (ns->GetType())
{
case kPersonalNamespace:
boxSpec->mBoxFlags |= kPersonalMailbox;
break;
case kPublicNamespace:
boxSpec->mBoxFlags |= kPublicMailbox;
break;
case kOtherUsersNamespace:
boxSpec->mBoxFlags |= kOtherUsersMailbox;
break;
default: // (kUnknownNamespace)
break;
}
DiscoverMailboxSpec(boxSpec);
}
else
HandleMemoryFailure();
}
nsAutoCString allPattern(prefix);
allPattern += '*';
nsAutoCString topLevelPattern(prefix);
topLevelPattern += '%';
nsAutoCString secondLevelPattern;
char delimiter = ns->GetDelimiter();
if (delimiter)
{
// Hierarchy delimiter might be NIL, in which case there's no hierarchy anyway
secondLevelPattern = prefix;
secondLevelPattern += '%';
secondLevelPattern += delimiter;
secondLevelPattern += '%';
}
if (!m_imapServerSink) return;
if (!allPattern.IsEmpty())
{
m_imapServerSink->SetServerDoingLsub(true);
Lsub(allPattern.get(), true); // LSUB all the subscribed
}
if (!topLevelPattern.IsEmpty())
{
m_imapServerSink->SetServerDoingLsub(false);
List(topLevelPattern.get(), true); // LIST the top level
}
if (!secondLevelPattern.IsEmpty())
{
m_imapServerSink->SetServerDoingLsub(false);
List(secondLevelPattern.get(), true); // LIST the second level
}
}
}
}
}
// DiscoverMailboxList() is used to actually do the discovery of folders
// for a host. This is used both when we initially start up (and re-sync)
// and also when the user manually requests a re-sync, by collapsing and
// expanding a host in the folder pane. This is not used for the subscribe
// pane.
// DiscoverMailboxList() also gets the ACLs for each newly discovered folder
void nsImapProtocol::DiscoverMailboxList()
{
bool usingSubscription = false;
m_hostSessionList->GetHostIsUsingSubscription(GetImapServerKey(), usingSubscription);
// Pretend that the Trash folder doesn't exist, so we will rediscover it if we need to.
m_hostSessionList->SetOnlineTrashFolderExistsForHost(GetImapServerKey(), false);
// should we check a pref here, to be able to turn off XList?
bool hasXLIST = GetServerStateParser().GetCapabilityFlag() & kHasXListCapability;
if (hasXLIST && usingSubscription)
{
m_hierarchyNameState = kXListing;
nsAutoCString pattern("%");
List("%", true, true);
// We list the first and second levels since special folders are unlikely
// to be more than 2 levels deep.
char separator = 0;
m_runningUrl->GetOnlineSubDirSeparator(&separator);
pattern.Append(separator);
pattern += '%';
List(pattern.get(), true, true);
}
SetMailboxDiscoveryStatus(eContinue);
if (GetServerStateParser().ServerHasACLCapability())
m_hierarchyNameState = kListingForInfoAndDiscovery;
else
m_hierarchyNameState = kNoOperationInProgress;
// iterate through all namespaces and LSUB them.
uint32_t count = 0;
m_hostSessionList->GetNumberOfNamespacesForHost(GetImapServerKey(), count);
for (uint32_t i = 0; i < count; i++ )
{
nsIMAPNamespace * ns = nullptr;
m_hostSessionList->GetNamespaceNumberForHost(GetImapServerKey(),i,ns);
if (ns)
{
const char *prefix = ns->GetPrefix();
if (prefix)
{
nsAutoCString inboxNameWithDelim("INBOX");
inboxNameWithDelim.Append(ns->GetDelimiter());
// static bool gHideUnusedNamespaces = true;
// mscott -> WARNING!!! i where are we going to get this
// global variable for unused name spaces from???
// dmb - we should get this from a per-host preference,
// I'd say. But for now, just make it true;
if (!gHideUnusedNamespaces && *prefix &&
PL_strcasecmp(prefix, inboxNameWithDelim.get())) // only do it for
// non-empty namespace prefixes, and for non-INBOX prefix
{
// Explicitly discover each Namespace, so that we can
// create subfolders of them,
nsImapMailboxSpec *boxSpec = new nsImapMailboxSpec;
if (boxSpec)
{
NS_ADDREF(boxSpec);
boxSpec->mFolderSelected = false;
boxSpec->mHostName = GetImapHostName();
boxSpec->mConnection = this;
boxSpec->mFlagState = nullptr;
boxSpec->mDiscoveredFromLsub = true;
boxSpec->mOnlineVerified = true;
boxSpec->mBoxFlags = kNoselect;
boxSpec->mHierarchySeparator = ns->GetDelimiter();
// Until |AllocateCanonicalPath()| gets updated:
m_runningUrl->AllocateCanonicalPath(
ns->GetPrefix(), ns->GetDelimiter(),
getter_Copies(boxSpec->mAllocatedPathName));
boxSpec->mNamespaceForFolder = ns;
boxSpec->mBoxFlags |= kNameSpace;
switch (ns->GetType())
{
case kPersonalNamespace:
boxSpec->mBoxFlags |= kPersonalMailbox;
break;
case kPublicNamespace:
boxSpec->mBoxFlags |= kPublicMailbox;
break;
case kOtherUsersNamespace:
boxSpec->mBoxFlags |= kOtherUsersMailbox;
break;
default: // (kUnknownNamespace)
break;
}
DiscoverMailboxSpec(boxSpec);
}
else
HandleMemoryFailure();
}
// now do the folders within this namespace
nsCString pattern;
nsCString pattern2;
if (usingSubscription)
{
pattern.Append(prefix);
pattern.Append("*");
}
else
{
pattern.Append(prefix);
pattern.Append("%"); // mscott just need one percent right?
// pattern = PR_smprintf("%s%%", prefix);
char delimiter = ns->GetDelimiter();
if (delimiter)
{
// delimiter might be NIL, in which case there's no hierarchy anyway
pattern2 = prefix;
pattern2 += "%";
pattern2 += delimiter;
pattern2 += "%";
// pattern2 = PR_smprintf("%s%%%c%%", prefix, delimiter);
}
}
// Note: It is important to make sure we are respecting the server_sub_directory
// preference when calling List and Lsub (2nd arg = true), otherwise
// we end up with performance issues or even crashes when connecting to
// servers that expose the users entire home directory (like UW-IMAP).
if (usingSubscription) { // && !GetSubscribingNow()) should never get here from subscribe pane
if (GetServerStateParser().GetCapabilityFlag() & kHasListExtendedCapability)
Lsub(pattern.get(), true); // do LIST (SUBSCRIBED)
else {
// store mailbox flags from LIST
EMailboxHierarchyNameState currentState = m_hierarchyNameState;
m_hierarchyNameState = kListingForFolderFlags;
List(pattern.get(), true);
m_hierarchyNameState = currentState;
// then do LSUB using stored flags
Lsub(pattern.get(), true);
m_standardListMailboxes.Clear();
}
}
else
{
List(pattern.get(), true, hasXLIST);
List(pattern2.get(), true, hasXLIST);
}
}
}
}
// explicitly LIST the INBOX if (a) we're not using subscription, or (b) we are using subscription and
// the user wants us to always show the INBOX.
bool listInboxForHost = false;
m_hostSessionList->GetShouldAlwaysListInboxForHost(GetImapServerKey(), listInboxForHost);
if (!usingSubscription || listInboxForHost)
List("INBOX", true);
m_hierarchyNameState = kNoOperationInProgress;
MailboxDiscoveryFinished();
// Get the ACLs for newly discovered folders
if (GetServerStateParser().ServerHasACLCapability())
{
int32_t total = m_listedMailboxList.Length(), cnt = 0;
// Let's not turn this off here, since we don't turn it on after
// GetServerStateParser().SetReportingErrors(false);
if (total)
{
ProgressEventFunctionUsingName("imapGettingACLForFolder");
nsIMAPMailboxInfo * mb = nullptr;
do
{
if (m_listedMailboxList.Length() == 0)
break;
mb = m_listedMailboxList[0]; // get top element
m_listedMailboxList.RemoveElementAt(0); // XP_ListRemoveTopObject(fListedMailboxList);
if (mb)
{
if (FolderNeedsACLInitialized(PromiseFlatCString(mb->GetMailboxName()).get()))
{
char *onlineName = nullptr;
m_runningUrl->AllocateServerPath(PromiseFlatCString(mb->GetMailboxName()).get(),
mb->GetDelimiter(), &onlineName);
if (onlineName)
{
RefreshACLForFolder(onlineName);
PR_Free(onlineName);
}
}
PercentProgressUpdateEvent(NULL, cnt, total);
delete mb; // this is the last time we're using the list, so delete the entries here
cnt++;
}
} while (mb && !DeathSignalReceived());
}
}
}
bool nsImapProtocol::FolderNeedsACLInitialized(const char *folderName)
{
bool rv = false;
m_imapServerSink->FolderNeedsACLInitialized(nsDependentCString(folderName), &rv);
return rv;
}
void nsImapProtocol::MailboxDiscoveryFinished()
{
if (!DeathSignalReceived() && !GetSubscribingNow() &&
((m_hierarchyNameState == kNoOperationInProgress) ||
(m_hierarchyNameState == kListingForInfoAndDiscovery)))
{
nsIMAPNamespace *ns = nullptr;
m_hostSessionList->GetDefaultNamespaceOfTypeForHost(GetImapServerKey(), kPersonalNamespace, ns);
const char *personalDir = ns ? ns->GetPrefix() : 0;
bool trashFolderExists = false;
bool usingSubscription = false;
m_hostSessionList->GetOnlineTrashFolderExistsForHost(GetImapServerKey(), trashFolderExists);
m_hostSessionList->GetHostIsUsingSubscription(GetImapServerKey(),usingSubscription);
if (!trashFolderExists && GetDeleteIsMoveToTrash() && usingSubscription)
{
// maybe we're not subscribed to the Trash folder
if (personalDir)
{
nsCString originalTrashName(CreatePossibleTrashName(personalDir));
m_hierarchyNameState = kDiscoverTrashFolderInProgress;
List(originalTrashName.get(), true);
m_hierarchyNameState = kNoOperationInProgress;
}
}
// There is no Trash folder (either LIST'd or LSUB'd), and we're using the
// Delete-is-move-to-Trash model, and there is a personal namespace
if (!trashFolderExists && GetDeleteIsMoveToTrash() && ns)
{
nsCString trashName(CreatePossibleTrashName(ns->GetPrefix()));
nsCString onlineTrashName;
m_runningUrl->AllocateServerPath(trashName.get(), ns->GetDelimiter(),
getter_Copies(onlineTrashName));
GetServerStateParser().SetReportingErrors(false);
bool created = CreateMailboxRespectingSubscriptions(onlineTrashName.get());
GetServerStateParser().SetReportingErrors(true);
// force discovery of new trash folder.
if (created)
{
m_hierarchyNameState = kDiscoverTrashFolderInProgress;
List(onlineTrashName.get(), false);
m_hierarchyNameState = kNoOperationInProgress;
}
else
m_hostSessionList->SetOnlineTrashFolderExistsForHost(GetImapServerKey(), true);
} //if trash folder doesn't exist
m_hostSessionList->SetHaveWeEverDiscoveredFoldersForHost(GetImapServerKey(), true);
// notify front end that folder discovery is complete....
if (m_imapServerSink)
m_imapServerSink->DiscoveryDone();
}
}
// returns the mailboxName with the IMAP delimiter removed from the tail end
void nsImapProtocol::RemoveHierarchyDelimiter(nsCString &mailboxName)
{
char onlineDelimiter[2] = {0, 0};
if (m_imapMailFolderSink)
m_imapMailFolderSink->GetOnlineDelimiter(&onlineDelimiter[0]);
// take the hierarchy delimiter off the end, if any.
if (onlineDelimiter[0])
mailboxName.Trim(onlineDelimiter, false, true);
}
// returns true is the create succeeded (regardless of subscription changes)
bool nsImapProtocol::CreateMailboxRespectingSubscriptions(const char *mailboxName)
{
CreateMailbox(mailboxName);
bool rv = GetServerStateParser().LastCommandSuccessful();
if (rv && m_autoSubscribe) // auto-subscribe is on
{
// create succeeded - let's subscribe to it
bool reportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false);
nsCString mailboxWODelim(mailboxName);
RemoveHierarchyDelimiter(mailboxWODelim);
OnSubscribe(mailboxWODelim.get());
GetServerStateParser().SetReportingErrors(reportingErrors);
}
return rv;
}
void nsImapProtocol::CreateMailbox(const char *mailboxName)
{
ProgressEventFunctionUsingName("imapStatusCreatingMailbox");
IncrementCommandTagNumber();
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
nsCString command(GetServerCommandTag());
command += " create \"";
command += escapedName;
command += "\"" CRLF;
nsresult rv = SendData(command.get());
if(NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
// If that failed, let's list the parent folder to see if
// it allows inferiors, so we won't try to create sub-folders
// of the parent folder again in the current session.
if (GetServerStateParser().CommandFailed())
{
// Figure out parent folder name.
nsCString parentName(mailboxName);
char hierarchyDelimiter;
m_runningUrl->GetOnlineSubDirSeparator(&hierarchyDelimiter);
int32_t leafPos = parentName.RFindChar(hierarchyDelimiter);
if (leafPos > 0)
{
parentName.SetLength(leafPos);
List(parentName.get(), false);
// We still want the caller to know the create failed, so restore that.
GetServerStateParser().SetCommandFailed(true);
}
}
}
void nsImapProtocol::DeleteMailbox(const char *mailboxName)
{
// check if this connection currently has the folder to be deleted selected.
// If so, we should close it because at least some UW servers don't like you deleting
// a folder you have open.
if (FolderIsSelected(mailboxName))
Close();
ProgressEventFunctionUsingNameWithString("imapStatusDeletingMailbox", mailboxName);
IncrementCommandTagNumber();
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
nsCString command(GetServerCommandTag());
command += " delete \"";
command += escapedName;
command += "\"" CRLF;
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::RenameMailbox(const char *existingName,
const char *newName)
{
// just like DeleteMailbox; Some UW servers don't like it.
if (FolderIsSelected(existingName))
Close();
ProgressEventFunctionUsingNameWithString("imapStatusRenamingMailbox", existingName);
IncrementCommandTagNumber();
nsCString escapedExistingName;
nsCString escapedNewName;
CreateEscapedMailboxName(existingName, escapedExistingName);
CreateEscapedMailboxName(newName, escapedNewName);
nsCString command(GetServerCommandTag());
command += " rename \"";
command += escapedExistingName;
command += "\" \"";
command += escapedNewName;
command += "\"" CRLF;
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
nsCString nsImapProtocol::CreatePossibleTrashName(const char *prefix)
{
nsCString returnTrash(prefix);
returnTrash += m_trashFolderName;
return returnTrash;
}
bool nsImapProtocol::GetListSubscribedIsBrokenOnServer()
{
// This is a workaround for an issue with LIST(SUBSCRIBED) crashing older versions of Zimbra
if (GetServerStateParser().GetServerID().Find("\"NAME\" \"Zimbra\"", CaseInsensitiveCompare) != kNotFound) {
nsCString serverID(GetServerStateParser().GetServerID());
int start = serverID.Find("\"VERSION\" \"", CaseInsensitiveCompare) + 11;
int length = serverID.Find("\" ", start, CaseInsensitiveCompare);
const nsDependentCSubstring serverVersionSubstring = Substring(serverID, start, length);
nsCString serverVersionStr(serverVersionSubstring);
Version serverVersion(serverVersionStr.get());
Version sevenTwoThree("7.2.3_");
Version eightZeroZero("8.0.0_");
Version eightZeroThree("8.0.3_");
if ((serverVersion < sevenTwoThree) ||
((serverVersion >= eightZeroZero) && (serverVersion < eightZeroThree)))
return true;
}
return false;
}
// This identifies servers that require an extra imap SELECT to detect new
// email in a mailbox. Servers requiring this are found by comparing their
// ID string, returned with imap ID command, to strings entered in
// mail.imap.force_select_detect. Only openwave servers used by
// Charter/Spectrum ISP returning an ID containing the strings ""name" "Email Mx""
// and ""vendor" "Openwave Messaging"" are now known to have this issue. The
// compared strings can be modified with the config editor if necessary
// (e.g., a "version" substring could be added). Also, additional servers
// having a different set of strings can be added if ever needed.
// The mail.imap.force_select_detect uses semicolon delimiter between
// servers and within a server substrings to compare are comma delimited.
// This example force_select_detect value shows how two servers types
// could be detected:
// "name" "Email Mx","vendor" "Openwave Messaging";"vendor" "Yahoo! Inc.","name" "Y!IMAP";
bool nsImapProtocol::IsExtraSelectNeeded()
{
bool retVal;
for (uint32_t i = 0; i < gForceSelectServersArray.Length(); i++)
{
retVal = true;
nsTArray<nsCString> forceSelectStringsArray;
ParseString(gForceSelectServersArray[i], ',', forceSelectStringsArray);
for (uint32_t j = 0; j < forceSelectStringsArray.Length(); j++)
{
// Each substring within the server string must be contained in ID string.
// First un-escape any comma (%2c) or semicolon (%3b) within the substring.
nsAutoCString unescapedString;
MsgUnescapeString(forceSelectStringsArray[j], 0, unescapedString);
if (GetServerStateParser().GetServerID()
.Find(unescapedString, CaseInsensitiveCompare) == kNotFound)
{
retVal = false;
break;
}
}
// Matches found for all substrings for the server.
if (retVal)
return true;
}
// If reached, no substrings match for any server.
return false;
}
void nsImapProtocol::Lsub(const char *mailboxPattern, bool addDirectoryIfNecessary)
{
ProgressEventFunctionUsingName("imapStatusLookingForMailbox");
IncrementCommandTagNumber();
char *boxnameWithOnlineDirectory = nullptr;
if (addDirectoryIfNecessary)
m_runningUrl->AddOnlineDirectoryIfNecessary(mailboxPattern, &boxnameWithOnlineDirectory);
nsCString escapedPattern;
CreateEscapedMailboxName(boxnameWithOnlineDirectory ?
boxnameWithOnlineDirectory :
mailboxPattern, escapedPattern);
nsCString command (GetServerCommandTag());
eIMAPCapabilityFlags flag = GetServerStateParser().GetCapabilityFlag();
bool useListSubscribed = (flag & kHasListExtendedCapability) &&
!GetListSubscribedIsBrokenOnServer();
if (useListSubscribed)
command += " list (subscribed)";
else
command += " lsub";
command += " \"\" \"";
command += escapedPattern;
if (useListSubscribed && (flag & kHasSpecialUseCapability))
command += "\" return (special-use)" CRLF;
else
command += "\"" CRLF;
PR_Free(boxnameWithOnlineDirectory);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(command.get(), true);
}
void nsImapProtocol::List(const char *mailboxPattern, bool addDirectoryIfNecessary,
bool useXLIST)
{
ProgressEventFunctionUsingName("imapStatusLookingForMailbox");
IncrementCommandTagNumber();
char *boxnameWithOnlineDirectory = nullptr;
if (addDirectoryIfNecessary)
m_runningUrl->AddOnlineDirectoryIfNecessary(mailboxPattern, &boxnameWithOnlineDirectory);
nsCString escapedPattern;
CreateEscapedMailboxName(boxnameWithOnlineDirectory ?
boxnameWithOnlineDirectory :
mailboxPattern, escapedPattern);
nsCString command (GetServerCommandTag());
command += useXLIST ?
" xlist \"\" \"" : " list \"\" \"";
command += escapedPattern;
command += "\"" CRLF;
PR_Free(boxnameWithOnlineDirectory);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(command.get(), true);
}
void nsImapProtocol::Subscribe(const char *mailboxName)
{
ProgressEventFunctionUsingNameWithString("imapStatusSubscribeToMailbox", mailboxName);
IncrementCommandTagNumber();
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
nsCString command (GetServerCommandTag());
command += " subscribe \"";
command += escapedName;
command += "\"" CRLF;
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Unsubscribe(const char *mailboxName)
{
ProgressEventFunctionUsingNameWithString("imapStatusUnsubscribeMailbox", mailboxName);
IncrementCommandTagNumber();
nsCString escapedName;
CreateEscapedMailboxName(mailboxName, escapedName);
nsCString command (GetServerCommandTag());
command += " unsubscribe \"";
command += escapedName;
command += "\"" CRLF;
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Idle()
{
IncrementCommandTagNumber();
if (m_urlInProgress)
return;
nsAutoCString command (GetServerCommandTag());
command += " IDLE" CRLF;
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
{
m_idle = true;
// we'll just get back a continuation char at first.
// + idling...
ParseIMAPandCheckForNewMail();
// this will cause us to get notified of data or the socket getting closed.
// That notification will occur on the socket transport thread - we just
// need to poke a monitor so the imap thread will do a blocking read
// and parse the data.
nsCOMPtr <nsIAsyncInputStream> asyncInputStream = do_QueryInterface(m_inputStream);
if (asyncInputStream)
asyncInputStream->AsyncWait(this, 0, 0, nullptr);
}
}
// until we can fix the hang on shutdown waiting for server
// responses, we need to not wait for the server response
// on shutdown.
void nsImapProtocol::EndIdle(bool waitForResponse /* = true */)
{
// clear the async wait - otherwise, we seem to have trouble doing a blocking read
nsCOMPtr <nsIAsyncInputStream> asyncInputStream = do_QueryInterface(m_inputStream);
if (asyncInputStream)
asyncInputStream->AsyncWait(nullptr, 0, 0, nullptr);
nsresult rv = SendData("DONE" CRLF);
// set a short timeout if we don't want to wait for a response
if (m_transport && !waitForResponse)
m_transport->SetTimeout(nsISocketTransport::TIMEOUT_READ_WRITE, 5);
if (NS_SUCCEEDED(rv))
{
m_idle = false;
ParseIMAPandCheckForNewMail();
}
m_imapMailFolderSink = nullptr;
}
void nsImapProtocol::Search(const char * searchCriteria,
bool useUID,
bool notifyHit /* true */)
{
m_notifySearchHit = notifyHit;
ProgressEventFunctionUsingName("imapStatusSearchMailbox");
IncrementCommandTagNumber();
nsCString protocolString(GetServerCommandTag());
// the searchCriteria string contains the 'search ....' string
if (useUID)
protocolString.Append(" uid");
protocolString.Append(" ");
protocolString.Append(searchCriteria);
// the search criteria can contain string literals, which means we
// need to break up the protocol string by CRLF's, and after sending CRLF,
// wait for the server to respond OK before sending more data
nsresult rv;
int32_t crlfIndex;
while (crlfIndex = protocolString.Find(CRLF), crlfIndex != kNotFound && !DeathSignalReceived())
{
nsAutoCString tempProtocolString;
tempProtocolString = StringHead(protocolString, crlfIndex + 2);
rv = SendData(tempProtocolString.get());
if (NS_FAILED(rv))
return;
ParseIMAPandCheckForNewMail();
protocolString.Cut(0, crlfIndex + 2);
}
protocolString.Append(CRLF);
rv = SendData(protocolString.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Copy(const char * messageList,
const char *destinationMailbox,
bool idsAreUid)
{
IncrementCommandTagNumber();
nsCString escapedDestination;
CreateEscapedMailboxName(destinationMailbox, escapedDestination);
// turn messageList back into key array and then back into a message id list,
// but use the flag state to handle ranges correctly.
nsCString messageIdList;
nsTArray<nsMsgKey> msgKeys;
if (idsAreUid)
ParseUidString(messageList, msgKeys);
int32_t msgCountLeft = msgKeys.Length();
uint32_t msgsHandled = 0;
do
{
nsCString idString;
uint32_t msgsToHandle = msgCountLeft;
if (idsAreUid)
AllocateImapUidString(msgKeys.Elements() + msgsHandled, msgsToHandle, m_flagState, idString);
else
idString.Assign(messageList);
msgsHandled += msgsToHandle;
msgCountLeft -= msgsToHandle;
IncrementCommandTagNumber();
nsAutoCString protocolString(GetServerCommandTag());
if (idsAreUid)
protocolString.Append(" uid");
// If it's a MOVE operation on aol servers then use 'xaol-move' cmd.
if ((m_imapAction == nsIImapUrl::nsImapOnlineMove) &&
GetServerStateParser().ServerIsAOLServer())
protocolString.Append(" xaol-move ");
else if ((m_imapAction == nsIImapUrl::nsImapOnlineMove) &&
GetServerStateParser().GetCapabilityFlag() & kHasMoveCapability)
protocolString.Append(" move ");
else
protocolString.Append(" copy ");
protocolString.Append(idString);
protocolString.Append(" \"");
protocolString.Append(escapedDestination);
protocolString.Append("\"" CRLF);
nsresult rv = SendData(protocolString.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail(protocolString.get());
}
while (msgCountLeft > 0 && !DeathSignalReceived());
}
void nsImapProtocol::NthLevelChildList(const char* onlineMailboxPrefix,
int32_t depth)
{
NS_ASSERTION (depth >= 0,
"Oops ... depth must be equal or greater than 0");
if (depth < 0) return;
nsCString truncatedPrefix (onlineMailboxPrefix);
char16_t slash = '/';
if (truncatedPrefix.Last() == slash)
truncatedPrefix.SetLength(truncatedPrefix.Length()-1);
nsAutoCString pattern(truncatedPrefix);
nsAutoCString suffix;
int count = 0;
char separator = 0;
m_runningUrl->GetOnlineSubDirSeparator(&separator);
suffix.Assign(separator);
suffix += '%';
while (count < depth)
{
pattern += suffix;
count++;
List(pattern.get(), false);
}
}
void nsImapProtocol::ProcessAuthenticatedStateURL()
{
nsImapAction imapAction;
char * sourceMailbox = nullptr;
m_runningUrl->GetImapAction(&imapAction);
// switch off of the imap url action and take an appropriate action
switch (imapAction)
{
case nsIImapUrl::nsImapLsubFolders:
OnLSubFolders();
break;
case nsIImapUrl::nsImapAppendMsgFromFile:
OnAppendMsgFromFile();
break;
case nsIImapUrl::nsImapDiscoverAllBoxesUrl:
NS_ASSERTION (!GetSubscribingNow(),
"Oops ... should not get here from subscribe UI");
DiscoverMailboxList();
break;
case nsIImapUrl::nsImapDiscoverAllAndSubscribedBoxesUrl:
DiscoverAllAndSubscribedBoxes();
break;
case nsIImapUrl::nsImapCreateFolder:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnCreateFolder(sourceMailbox);
break;
case nsIImapUrl::nsImapEnsureExistsFolder:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnEnsureExistsFolder(sourceMailbox);
break;
case nsIImapUrl::nsImapDiscoverChildrenUrl:
{
char *canonicalParent = nullptr;
m_runningUrl->CreateServerSourceFolderPathString(&canonicalParent);
if (canonicalParent)
{
NthLevelChildList(canonicalParent, 2);
PR_Free(canonicalParent);
}
break;
}
case nsIImapUrl::nsImapSubscribe:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnSubscribe(sourceMailbox); // used to be called subscribe
if (GetServerStateParser().LastCommandSuccessful())
{
bool shouldList;
// if url is an external click url, then we should list the folder
// after subscribing to it, so we can select it.
m_runningUrl->GetExternalLinkUrl(&shouldList);
if (shouldList)
OnListFolder(sourceMailbox, true);
}
break;
case nsIImapUrl::nsImapUnsubscribe:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnUnsubscribe(sourceMailbox);
break;
case nsIImapUrl::nsImapRefreshACL:
sourceMailbox = OnCreateServerSourceFolderPathString();
RefreshACLForFolder(sourceMailbox);
break;
case nsIImapUrl::nsImapRefreshAllACLs:
OnRefreshAllACLs();
break;
case nsIImapUrl::nsImapListFolder:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnListFolder(sourceMailbox, false);
break;
case nsIImapUrl::nsImapFolderStatus:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnStatusForFolder(sourceMailbox);
break;
case nsIImapUrl::nsImapRefreshFolderUrls:
sourceMailbox = OnCreateServerSourceFolderPathString();
XMailboxInfo(sourceMailbox);
if (GetServerStateParser().LastCommandSuccessful())
SetFolderAdminUrl(sourceMailbox);
break;
case nsIImapUrl::nsImapDeleteFolder:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnDeleteFolder(sourceMailbox);
break;
case nsIImapUrl::nsImapRenameFolder:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnRenameFolder(sourceMailbox);
break;
case nsIImapUrl::nsImapMoveFolderHierarchy:
sourceMailbox = OnCreateServerSourceFolderPathString();
OnMoveFolderHierarchy(sourceMailbox);
break;
case nsIImapUrl::nsImapVerifylogon:
break;
default:
break;
}
PR_Free(sourceMailbox);
}
void nsImapProtocol::ProcessAfterAuthenticated()
{
// if we're a netscape server, and we haven't got the admin url, get it
bool hasAdminUrl = true;
if (NS_SUCCEEDED(m_hostSessionList->GetHostHasAdminURL(GetImapServerKey(), hasAdminUrl))
&& !hasAdminUrl)
{
if (GetServerStateParser().ServerHasServerInfo())
{
XServerInfo();
if (GetServerStateParser().LastCommandSuccessful() && m_imapServerSink)
{
m_imapServerSink->SetMailServerUrls(GetServerStateParser().GetMailAccountUrl(),
GetServerStateParser().GetManageListsUrl(),
GetServerStateParser().GetManageFiltersUrl());
// we've tried to ask for it, so don't try again this session.
m_hostSessionList->SetHostHasAdminURL(GetImapServerKey(), true);
}
}
else if (GetServerStateParser().ServerIsNetscape3xServer())
{
Netscape();
if (GetServerStateParser().LastCommandSuccessful() && m_imapServerSink)
m_imapServerSink->SetMailServerUrls(GetServerStateParser().GetMailAccountUrl(),
EmptyCString(), EmptyCString());
}
}
if (GetServerStateParser().ServerHasNamespaceCapability())
{
bool nameSpacesOverridable = false;
bool haveNameSpacesForHost = false;
m_hostSessionList->GetNamespacesOverridableForHost(GetImapServerKey(), nameSpacesOverridable);
m_hostSessionList->GetGotNamespacesForHost(GetImapServerKey(), haveNameSpacesForHost);
// mscott: VERIFY THIS CLAUSE!!!!!!!
if (nameSpacesOverridable && !haveNameSpacesForHost)
Namespace();
}
// If the server supports compression, turn it on now.
// Choosing this spot (after login has finished) because
// many proxies (e.g. perdition, nginx) talk IMAP to the
// client until login is finished, then hand off to the
// backend. If we enable compression early the proxy
// will be confused.
if (UseCompressDeflate())
StartCompressDeflate();
if ((GetServerStateParser().GetCapabilityFlag() & kHasEnableCapability) &&
UseCondStore())
EnableCondStore();
bool haveIdResponse = false;
if ((GetServerStateParser().GetCapabilityFlag() & kHasIDCapability) &&
m_sendID)
{
ID();
if (m_imapServerSink && !GetServerStateParser().GetServerID().IsEmpty())
{
haveIdResponse = true;
// Determine value for m_forceSelect based on config editor
// entries and comparison to imap ID string returned by the server.
m_imapServerSink->SetServerID(GetServerStateParser().GetServerID());
switch (m_forceSelectValue.get()[0])
{
// Yes: Set to always force even if imap server doesn't need it.
case 'y':
case 'Y':
m_forceSelect = true;
break;
// No: Set to never force a select for this imap server.
case 'n':
case 'N':
m_forceSelect = false;
break;
// Auto: Set to force only if imap server requires it.
default:
nsAutoCString statusString;
m_forceSelect = IsExtraSelectNeeded();
// Setting to "yes-auto" or "no-auto" avoids doing redundant calls to
// IsExtraSelectNeeded() on subsequent ID() occurrences. It also
// provides feedback to the user regarding the detection status.
if (m_forceSelect)
{
// Set preference value to "yes-auto".
statusString.Assign("yes-auto");
}
else
{
// Set preference value to "no-auto".
statusString.Assign("no-auto");
}
m_imapServerSink->SetServerForceSelect(statusString);
break;
}
}
}
// If no ID capability or empty ID response, user may still want to
// change "force select".
if (!haveIdResponse)
{
switch (m_forceSelectValue.get()[0])
{
case 'a':
{
// If default "auto", set to "no-auto" so visible in config editor
// and set/keep m_forceSelect false.
nsAutoCString statusString;
statusString.Assign("no-auto");
m_imapServerSink->SetServerForceSelect(statusString);
m_forceSelect = false;
}
break;
case 'y':
case 'Y':
m_forceSelect = true;
break;
default:
m_forceSelect = false;
}
}
}
void nsImapProtocol::SetupMessageFlagsString(nsCString& flagString,
imapMessageFlagsType flags,
uint16_t userFlags)
{
if (flags & kImapMsgSeenFlag)
flagString.Append("\\Seen ");
if (flags & kImapMsgAnsweredFlag)
flagString.Append("\\Answered ");
if (flags & kImapMsgFlaggedFlag)
flagString.Append("\\Flagged ");
if (flags & kImapMsgDeletedFlag)
flagString.Append("\\Deleted ");
if (flags & kImapMsgDraftFlag)
flagString.Append("\\Draft ");
if (flags & kImapMsgRecentFlag)
flagString.Append("\\Recent ");
if ((flags & kImapMsgForwardedFlag) &&
(userFlags & kImapMsgSupportForwardedFlag))
flagString.Append("$Forwarded "); // Not always available
if ((flags & kImapMsgMDNSentFlag) && (
userFlags & kImapMsgSupportMDNSentFlag))
flagString.Append("$MDNSent "); // Not always available
// eat the last space
if (!flagString.IsEmpty())
flagString.SetLength(flagString.Length()-1);
}
void nsImapProtocol::ProcessStoreFlags(const nsCString &messageIdsString,
bool idsAreUids,
imapMessageFlagsType flags,
bool addFlags)
{
nsCString flagString;
uint16_t userFlags = GetServerStateParser().SupportsUserFlags();
uint16_t settableFlags = GetServerStateParser().SettablePermanentFlags();
if (!addFlags && (flags & userFlags) && !(flags & settableFlags))
{
if (m_runningUrl)
m_runningUrl->SetExtraStatus(nsIImapUrl::ImapStatusFlagsNotSettable);
return; // if cannot set any of the flags bail out
}
if (addFlags)
flagString = "+Flags (";
else
flagString = "-Flags (";
if (flags & kImapMsgSeenFlag && kImapMsgSeenFlag & settableFlags)
flagString .Append("\\Seen ");
if (flags & kImapMsgAnsweredFlag && kImapMsgAnsweredFlag & settableFlags)
flagString .Append("\\Answered ");
if (flags & kImapMsgFlaggedFlag && kImapMsgFlaggedFlag & settableFlags)
flagString .Append("\\Flagged ");
if (flags & kImapMsgDeletedFlag && kImapMsgDeletedFlag & settableFlags)
flagString .Append("\\Deleted ");
if (flags & kImapMsgDraftFlag && kImapMsgDraftFlag & settableFlags)
flagString .Append("\\Draft ");
if (flags & kImapMsgForwardedFlag && kImapMsgSupportForwardedFlag & userFlags)
flagString .Append("$Forwarded "); // if supported
if (flags & kImapMsgMDNSentFlag && kImapMsgSupportMDNSentFlag & userFlags)
flagString .Append("$MDNSent "); // if supported
if (flagString.Length() > 8) // if more than "+Flags ("
{
// replace the final space with ')'
flagString.SetCharAt(')',flagString.Length() - 1);
Store(messageIdsString, flagString.get(), idsAreUids);
if (m_runningUrl && idsAreUids)
{
nsCString messageIdString;
m_runningUrl->GetListOfMessageIds(messageIdString);
nsTArray<nsMsgKey> msgKeys;
ParseUidString(messageIdString.get(), msgKeys);
int32_t msgCount = msgKeys.Length();
for (int32_t i = 0; i < msgCount; i++)
{
bool found;
imapMessageFlagsType resultFlags;
// check if the flags were added/removed, and if the uid really exists.
nsresult rv = GetFlagsForUID(msgKeys[i], &found, &resultFlags, nullptr);
if (NS_FAILED(rv) || !found ||
(addFlags && ((flags & resultFlags) != flags)) ||
(!addFlags && ((flags & resultFlags) != 0)))
{
m_runningUrl->SetExtraStatus(nsIImapUrl::ImapStatusFlagChangeFailed);
break;
}
}
}
}
}
void nsImapProtocol::Close(bool shuttingDown /* = false */,
bool waitForResponse /* = true */)
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" close" CRLF);
if (!shuttingDown)
ProgressEventFunctionUsingName("imapStatusCloseMailbox");
GetServerStateParser().ResetFlagInfo();
nsresult rv = SendData(command.get());
if (m_transport && shuttingDown)
m_transport->SetTimeout(nsISocketTransport::TIMEOUT_READ_WRITE, 5);
if (NS_SUCCEEDED(rv) && waitForResponse)
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::XAOL_Option(const char *option)
{
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" XAOL-OPTION ");
command.Append(option);
command.Append(CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
ParseIMAPandCheckForNewMail();
}
void nsImapProtocol::Check()
{
//ProgressUpdateEvent("Checking mailbox...");
IncrementCommandTagNumber();
nsCString command(GetServerCommandTag());
command.Append(" check" CRLF);
nsresult rv = SendData(command.get());
if (NS_SUCCEEDED(rv))
{
m_flagChangeCount = 0;
m_lastCheckTime = PR_Now();
ParseIMAPandCheckForNewMail();
}
}
nsresult nsImapProtocol::GetMsgWindow(nsIMsgWindow **aMsgWindow)
{
nsresult rv;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl =
do_QueryInterface(m_runningUrl, &rv);
NS_ENSURE_SUCCESS(rv, rv);
if (!m_imapProtocolSink)
return NS_ERROR_FAILURE;
return m_imapProtocolSink->GetUrlWindow(mailnewsUrl, aMsgWindow);
}
/**
* Get password from RAM, disk (password manager) or user (dialog)
* @return NS_MSG_PASSWORD_PROMPT_CANCELLED
* (which is NS_SUCCEEDED!) when user cancelled
* NS_FAILED(rv) for other errors
*/
nsresult nsImapProtocol::GetPassword(nsCString &password,
bool newPasswordRequested)
{
// we are in the imap thread so *NEVER* try to extract the password with UI
// if logon redirection has changed the password, use the cookie as the password
if (m_overRideUrlConnectionInfo)
{
password.Assign(m_logonCookie);
return NS_OK;
}
NS_ENSURE_TRUE(m_imapServerSink, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(m_server, NS_ERROR_NULL_POINTER);
nsresult rv;
// Get the password already stored in mem
rv = m_imapServerSink->GetServerPassword(password);
if (NS_FAILED(rv) || password.IsEmpty())
{
AutoProxyReleaseMsgWindow msgWindow;
GetMsgWindow(getter_AddRefs(msgWindow));
NS_ENSURE_TRUE(msgWindow, NS_ERROR_NOT_AVAILABLE); // biff case
// Get the password from pw manager (harddisk) or user (dialog)
m_passwordObtained = false;
rv = m_imapServerSink->AsyncGetPassword(this,
newPasswordRequested,
password);
if (password.IsEmpty())
{
PRIntervalTime sleepTime = kImapSleepTime;
m_passwordStatus = NS_OK;
ReentrantMonitorAutoEnter mon(m_passwordReadyMonitor);
while (!m_passwordObtained && !NS_FAILED(m_passwordStatus) &&
m_passwordStatus != NS_MSG_PASSWORD_PROMPT_CANCELLED &&
!DeathSignalReceived())
mon.Wait(sleepTime);
rv = m_passwordStatus;
password = m_password;
}
}
if (!password.IsEmpty())
m_lastPasswordSent = password;
return rv;
}
// This is called from the UI thread.
NS_IMETHODIMP
nsImapProtocol::OnPromptStart(bool *aResult)
{
nsresult rv;
nsCOMPtr<nsIImapIncomingServer> imapServer = do_QueryReferent(m_server, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgWindow> msgWindow;
*aResult = false;
GetMsgWindow(getter_AddRefs(msgWindow));
nsCString password = m_lastPasswordSent;
rv = imapServer->PromptPassword(msgWindow, password);
m_password = password;
m_passwordStatus = rv;
if (!m_password.IsEmpty())
*aResult = true;
// Notify the imap thread that we have a password.
m_passwordObtained = true;
ReentrantMonitorAutoEnter passwordMon(m_passwordReadyMonitor);
passwordMon.Notify();
return rv;
}
NS_IMETHODIMP
nsImapProtocol::OnPromptAuthAvailable()
{
nsresult rv;
nsCOMPtr<nsIMsgIncomingServer> imapServer = do_QueryReferent(m_server, &rv);
NS_ENSURE_SUCCESS(rv, rv);
m_passwordStatus = imapServer->GetPassword(m_password);
// Notify the imap thread that we have a password.
m_passwordObtained = true;
ReentrantMonitorAutoEnter passwordMon(m_passwordReadyMonitor);
passwordMon.Notify();
return m_passwordStatus;
}
NS_IMETHODIMP
nsImapProtocol::OnPromptCanceled()
{
// A prompt was cancelled, so notify the imap thread.
m_passwordStatus = NS_MSG_PASSWORD_PROMPT_CANCELLED;
ReentrantMonitorAutoEnter passwordMon(m_passwordReadyMonitor);
passwordMon.Notify();
return NS_OK;
}
bool nsImapProtocol::TryToLogon()
{
MOZ_LOG(IMAP, LogLevel::Debug, ("try to log in"));
NS_ENSURE_TRUE(m_imapServerSink, false);
bool loginSucceeded = false;
bool skipLoop = false;
nsAutoCString password;
nsAutoCString userName;
nsresult rv = ChooseAuthMethod();
if (NS_FAILED(rv)) // all methods failed
{
// are there any matching login schemes at all?
if (!(GetServerStateParser().GetCapabilityFlag() & m_prefAuthMethods))
{
// Pref doesn't match server. Now, find an appropriate error msg.
// pref has plaintext pw & server claims to support encrypted pw
if (m_prefAuthMethods == (kHasAuthOldLoginCapability |
kHasAuthLoginCapability | kHasAuthPlainCapability) &&
GetServerStateParser().GetCapabilityFlag() & kHasCRAMCapability)
// tell user to change to encrypted pw
AlertUserEventUsingName("imapAuthChangePlainToEncrypt");
// pref has encrypted pw & server claims to support plaintext pw
else if (m_prefAuthMethods == kHasCRAMCapability &&
GetServerStateParser().GetCapabilityFlag() &
(kHasAuthOldLoginCapability | kHasAuthLoginCapability |
kHasAuthPlainCapability))
{
// have SSL
if (m_socketType == nsMsgSocketType::SSL ||
m_socketType == nsMsgSocketType::alwaysSTARTTLS)
// tell user to change to plaintext pw
AlertUserEventUsingName("imapAuthChangeEncryptToPlainSSL");
else
// tell user to change to plaintext pw, with big warning
AlertUserEventUsingName("imapAuthChangeEncryptToPlainNoSSL");
}
else
// just "change auth method"
AlertUserEventUsingName("imapAuthMechNotSupported");
skipLoop = true;
}
else
{
// try to reset failed methods and try them again
ResetAuthMethods();
rv = ChooseAuthMethod();
if (NS_FAILED(rv)) // all methods failed
{
MOZ_LOG(IMAP, LogLevel::Error, ("huch? there are auth methods, and we resetted failed ones, but ChooseAuthMethod still fails."));
return false;
}
}
}
// Get username, either the stored one or from user
rv = m_imapServerSink->GetLoginUsername(userName);
if (NS_FAILED(rv) || userName.IsEmpty())
{
// The user hit "Cancel" on the dialog box
skipLoop = true;
}
/*
* Login can fail for various reasons:
* 1. Server claims to support GSSAPI, but it really doesn't.
* Or the client doesn't support GSSAPI, or is not logged in yet.
* (GSSAPI is a mechanism without password in apps).
* 2. Server claims to support CRAM-MD5, but it's broken and will fail despite correct password.
* 2.1. Some servers say they support CRAM but are so badly broken that trying it causes
* all subsequent login attempts to fail during this connection (bug 231303).
* So we use CRAM/NTLM/MSN only if enabled in prefs.
* Update: if it affects only some ISPs, we can maybe use the ISP DB
* and disable CRAM specifically for these.
* 3. Prefs are set to require auth methods which the server doesn't support
* (per CAPS or we tried and they failed).
* 4. User provided wrong password.
* 5. We tried too often and the server shut us down, so even a correct attempt
* will now (currently) fail.
* The above problems may overlap, e.g. 3. with 1. and 2., and we can't differentiate
* between 2. and 4., which is really unfortunate.
*/
bool newPasswordRequested = false;
// remember the msgWindow before we start trying to logon, because if the
// server drops the connection on errors, TellThreadToDie will null out the
// protocolsink and we won't be able to get the msgWindow.
AutoProxyReleaseMsgWindow msgWindow;
GetMsgWindow(getter_AddRefs(msgWindow));
// This loops over 1) auth methods (only one per loop) and 2) password tries (with UI)
while (!loginSucceeded && !skipLoop && !DeathSignalReceived())
{
// Get password
if (m_currentAuthMethod != kHasAuthGssApiCapability && // GSSAPI uses no pw in apps
m_currentAuthMethod != kHasAuthExternalCapability &&
#ifdef MOZ_MAILNEWS_OAUTH2
m_currentAuthMethod != kHasXOAuth2Capability &&
#endif
m_currentAuthMethod != kHasAuthNoneCapability)
{
rv = GetPassword(password, newPasswordRequested);
newPasswordRequested = false;
if (rv == NS_MSG_PASSWORD_PROMPT_CANCELLED || NS_FAILED(rv))
{
MOZ_LOG(IMAP, LogLevel::Error, ("IMAP: password prompt failed or user canceled it"));
break;
}
MOZ_LOG(IMAP, LogLevel::Debug, ("got new password"));
}
bool lastReportingErrors = GetServerStateParser().GetReportingErrors();
GetServerStateParser().SetReportingErrors(false); // turn off errors - we'll put up our own.
rv = AuthLogin(userName.get(), password, m_currentAuthMethod);
GetServerStateParser().SetReportingErrors(lastReportingErrors); // restore error reports
loginSucceeded = NS_SUCCEEDED(rv);
if (!loginSucceeded)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("authlogin failed"));
MarkAuthMethodAsFailed(m_currentAuthMethod);
rv = ChooseAuthMethod(); // change m_currentAuthMethod to try other one next round
if (NS_FAILED(rv)) // all methods failed
{
if (m_prefAuthMethods == kHasAuthGssApiCapability)
{
// GSSAPI failed, and it's the only available method,
// and it's password-less, so nothing left to do.
AlertUserEventUsingName("imapAuthGssapiFailed");
break;
}
#ifdef MOZ_MAILNEWS_OAUTH2
if (m_prefAuthMethods & kHasXOAuth2Capability)
{
// OAuth2 failed. We don't have an error message for this, and we
// in a string freeze, so use a generic error message. Entering
// a password does not help.
AlertUserEventUsingName("imapUnknownHostError");
break;
}
#endif
// The reason that we failed might be a wrong password, so
// ask user what to do
MOZ_LOG(IMAP, LogLevel::Warning, ("IMAP: ask user what to do (after login failed): new passwort, retry, cancel"));
if (!m_imapServerSink)
break;
// if there's no msg window, don't forget the password
if (!msgWindow)
break;
int32_t buttonPressed = 1;
rv = m_imapServerSink->PromptLoginFailed(msgWindow,
&buttonPressed);
if (NS_FAILED(rv))
break;
if (buttonPressed == 2) // 'New password' button
{
MOZ_LOG(IMAP, LogLevel::Warning, ("new password button pressed."));
// Forget the current password
password.Truncate();
m_hostSessionList->SetPasswordForHost(GetImapServerKey(), nullptr);
m_imapServerSink->ForgetPassword();
m_password.Truncate();
MOZ_LOG(IMAP, LogLevel::Warning, ("password resetted (nulled)"));
newPasswordRequested = true;
// Will call GetPassword() in beginning of next loop
// Try all possible auth methods again with the new password.
ResetAuthMethods();
}
else if (buttonPressed == 0) // Retry button
{
MOZ_LOG(IMAP, LogLevel::Warning, ("retry button pressed"));
// Try all possible auth methods again
ResetAuthMethods();
}
else if (buttonPressed == 1) // Cancel button
{
MOZ_LOG(IMAP, LogLevel::Warning, ("cancel button pressed"));
break; // Abort quickly
}
// TODO what is this for? When does it get set to != unknown again?
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_Unknown;
SendSetBiffIndicatorEvent(m_currentBiffState);
} // all methods failed
} // login failed
} // while
if (loginSucceeded)
{
MOZ_LOG(IMAP, LogLevel::Debug, ("login succeeded"));
bool passwordAlreadyVerified;
m_hostSessionList->SetPasswordForHost(GetImapServerKey(), password.get());
rv = m_hostSessionList->GetPasswordVerifiedOnline(GetImapServerKey(), passwordAlreadyVerified);
if (NS_SUCCEEDED(rv) && !passwordAlreadyVerified)
m_hostSessionList->SetPasswordVerifiedOnline(GetImapServerKey());
bool imapPasswordIsNew = !passwordAlreadyVerified;
if (imapPasswordIsNew)
{
if (m_currentBiffState == nsIMsgFolder::nsMsgBiffState_Unknown)
{
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_NoMail;
SendSetBiffIndicatorEvent(m_currentBiffState);
}
m_imapServerSink->SetUserAuthenticated(true);
}
nsImapAction imapAction;
m_runningUrl->GetImapAction(&imapAction);
// We don't want to do any more processing if we're just
// verifying the ability to logon because it can leave us in
// a half-constructed state.
if (imapAction != nsIImapUrl::nsImapVerifylogon)
ProcessAfterAuthenticated();
}
else // login failed
{
MOZ_LOG(IMAP, LogLevel::Error, ("login failed entirely"));
m_currentBiffState = nsIMsgFolder::nsMsgBiffState_Unknown;
SendSetBiffIndicatorEvent(m_currentBiffState);
HandleCurrentUrlError();
SetConnectionStatus(NS_ERROR_FAILURE); // stop netlib
}
return loginSucceeded;
}
void nsImapProtocol::UpdateFolderQuotaData(nsCString& aQuotaRoot, uint32_t aUsed, uint32_t aMax)
{
NS_ASSERTION(m_imapMailFolderSink, "m_imapMailFolderSink is null!");
m_imapMailFolderSink->SetFolderQuotaData(aQuotaRoot, aUsed, aMax);
}
void nsImapProtocol::GetQuotaDataIfSupported(const char *aBoxName)
{
// If server doesn't have quota support, don't do anything
if (! (GetServerStateParser().GetCapabilityFlag() & kQuotaCapability))
return;
nsCString escapedName;
CreateEscapedMailboxName(aBoxName, escapedName);
IncrementCommandTagNumber();
nsAutoCString quotacommand(GetServerCommandTag());
quotacommand.Append(NS_LITERAL_CSTRING(" getquotaroot \""));
quotacommand.Append(escapedName);
quotacommand.Append(NS_LITERAL_CSTRING("\"" CRLF));
NS_ASSERTION(m_imapMailFolderSink, "m_imapMailFolderSink is null!");
if (m_imapMailFolderSink)
m_imapMailFolderSink->SetFolderQuotaCommandIssued(true);
nsresult quotarv = SendData(quotacommand.get());
if (NS_SUCCEEDED(quotarv))
ParseIMAPandCheckForNewMail(nullptr, true); // don't display errors.
}
bool
nsImapProtocol::GetDeleteIsMoveToTrash()
{
bool rv = false;
NS_ASSERTION (m_hostSessionList, "fatal... null host session list");
if (m_hostSessionList)
m_hostSessionList->GetDeleteIsMoveToTrashForHost(GetImapServerKey(), rv);
return rv;
}
bool
nsImapProtocol::GetShowDeletedMessages()
{
bool rv = false;
if (m_hostSessionList)
m_hostSessionList->GetShowDeletedMessagesForHost(GetImapServerKey(), rv);
return rv;
}
NS_IMETHODIMP nsImapProtocol::OverrideConnectionInfo(const char16_t *pHost, uint16_t pPort, const char *pCookieData)
{
m_logonHost = NS_LossyConvertUTF16toASCII(pHost);
m_logonPort = pPort;
m_logonCookie = pCookieData;
m_overRideUrlConnectionInfo = true;
return NS_OK;
}
bool nsImapProtocol::CheckNeeded()
{
if (m_flagChangeCount >= kFlagChangesBeforeCheck)
return true;
int32_t deltaInSeconds;
PRTime2Seconds(PR_Now() - m_lastCheckTime, &deltaInSeconds);
return (deltaInSeconds >= kMaxSecondsBeforeCheck);
}
bool nsImapProtocol::UseCondStore()
{
// Check that the server is capable of cond store, and the user
// hasn't disabled the use of constore for this server.
return m_useCondStore &&
GetServerStateParser().GetCapabilityFlag() & kHasCondStoreCapability &&
GetServerStateParser().fUseModSeq;
}
bool nsImapProtocol::UseCompressDeflate()
{
// Check that the server is capable of compression, and the user
// hasn't disabled the use of compression for this server.
return m_useCompressDeflate &&
GetServerStateParser().GetCapabilityFlag() & kHasCompressDeflateCapability;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// The following is the implementation of nsImapMockChannel and an intermediary
// imap steam listener. The stream listener is used to make a clean binding between the
// imap mock channel and the memory cache channel (if we are reading from the cache)
//////////////////////////////////////////////////////////////////////////////////////////////
// WARNING: the cache stream listener is intended to be accessed from the UI thread!
// it will NOT create another proxy for the stream listener that gets passed in...
class nsImapCacheStreamListener : public nsIStreamListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
nsImapCacheStreamListener ();
nsresult Init(nsIStreamListener * aStreamListener, nsIImapMockChannel * aMockChannelToUse);
protected:
virtual ~nsImapCacheStreamListener();
nsCOMPtr<nsIImapMockChannel> mChannelToUse;
nsCOMPtr<nsIStreamListener> mListener;
};
NS_IMPL_ADDREF(nsImapCacheStreamListener)
NS_IMPL_RELEASE(nsImapCacheStreamListener)
NS_INTERFACE_MAP_BEGIN(nsImapCacheStreamListener)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
NS_INTERFACE_MAP_END
nsImapCacheStreamListener::nsImapCacheStreamListener()
{
}
nsImapCacheStreamListener::~nsImapCacheStreamListener()
{}
nsresult nsImapCacheStreamListener::Init(nsIStreamListener * aStreamListener, nsIImapMockChannel * aMockChannelToUse)
{
NS_ENSURE_ARG(aStreamListener);
NS_ENSURE_ARG(aMockChannelToUse);
mChannelToUse = aMockChannelToUse;
mListener = aStreamListener;
return NS_OK;
}
NS_IMETHODIMP
nsImapCacheStreamListener::OnStartRequest(nsIRequest *request, nsISupports * aCtxt)
{
if (!mChannelToUse)
{
NS_ERROR("OnStartRequest called after OnStopRequest");
return NS_ERROR_NULL_POINTER;
}
nsCOMPtr<nsILoadGroup> loadGroup;
mChannelToUse->GetLoadGroup(getter_AddRefs(loadGroup));
nsCOMPtr<nsIRequest> ourRequest = do_QueryInterface(mChannelToUse);
if (loadGroup)
loadGroup->AddRequest(ourRequest, nullptr /* context isupports */);
return mListener->OnStartRequest(ourRequest, aCtxt);
}
NS_IMETHODIMP
nsImapCacheStreamListener::OnStopRequest(nsIRequest *request, nsISupports * aCtxt, nsresult aStatus)
{
if (!mListener)
{
NS_ERROR("OnStopRequest called twice");
return NS_ERROR_NULL_POINTER;
}
nsresult rv = mListener->OnStopRequest(mChannelToUse, aCtxt, aStatus);
nsCOMPtr <nsILoadGroup> loadGroup;
mChannelToUse->GetLoadGroup(getter_AddRefs(loadGroup));
if (loadGroup)
loadGroup->RemoveRequest(mChannelToUse, nullptr, aStatus);
mListener = nullptr;
mChannelToUse->Close();
mChannelToUse = nullptr;
return rv;
}
NS_IMETHODIMP
nsImapCacheStreamListener::OnDataAvailable(nsIRequest *request, nsISupports * aCtxt, nsIInputStream * aInStream, uint64_t aSourceOffset, uint32_t aCount)
{
return mListener->OnDataAvailable(mChannelToUse, aCtxt, aInStream, aSourceOffset, aCount);
}
NS_IMPL_ISUPPORTS(nsImapMockChannel, nsIImapMockChannel, nsIChannel,
nsIRequest, nsICacheEntryOpenCallback, nsITransportEventSink, nsISupportsWeakReference)
nsImapMockChannel::nsImapMockChannel()
{
m_channelContext = nullptr;
m_cancelStatus = NS_OK;
mLoadFlags = 0;
mChannelClosed = false;
mReadingFromCache = false;
mTryingToReadPart = false;
}
nsImapMockChannel::~nsImapMockChannel()
{
// if we're offline, we may not get to close the channel correctly.
// we need to do this to send the url state change notification in
// the case of mem and disk cache reads.
NS_WARNING_ASSERTION(NS_IsMainThread(), "should only access mock channel on ui thread");
if (!mChannelClosed)
Close();
}
nsresult nsImapMockChannel::NotifyStartEndReadFromCache(bool start)
{
nsresult rv = NS_OK;
mReadingFromCache = start;
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(m_url, &rv);
nsCOMPtr<nsIImapProtocol> imapProtocol = do_QueryReferent(mProtocol);
if (imapUrl)
{
nsCOMPtr<nsIImapMailFolderSink> folderSink;
rv = imapUrl->GetImapMailFolderSink(getter_AddRefs(folderSink));
if (folderSink)
{
nsCOMPtr<nsIMsgMailNewsUrl> mailUrl = do_QueryInterface(m_url);
rv = folderSink->SetUrlState(nullptr /* we don't know the protocol */,
mailUrl, start, false, m_cancelStatus);
// Required for killing ImapProtocol thread
if (NS_FAILED(m_cancelStatus) && imapProtocol)
imapProtocol->TellThreadToDie(false);
}
}
return rv;
}
NS_IMETHODIMP nsImapMockChannel::Close()
{
if (mReadingFromCache)
NotifyStartEndReadFromCache(false);
else
{
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url);
if (mailnewsUrl)
{
nsCOMPtr<nsICacheEntry> cacheEntry;
mailnewsUrl->GetMemCacheEntry(getter_AddRefs(cacheEntry));
if (cacheEntry)
cacheEntry->MarkValid();
// remove the channel from the load group
nsCOMPtr <nsILoadGroup> loadGroup;
GetLoadGroup(getter_AddRefs(loadGroup));
// if the mock channel wasn't initialized with a load group then
// use our load group (they may differ)
if (!loadGroup)
mailnewsUrl->GetLoadGroup(getter_AddRefs(loadGroup));
if (loadGroup)
loadGroup->RemoveRequest((nsIRequest *) this, nullptr, NS_OK);
}
}
m_channelListener = nullptr;
mCacheRequest = nullptr;
if (mTryingToReadPart)
{
// clear mem cache entry on imap part url in case it's holding
// onto last reference in mem cache. Need to do this on ui thread
nsresult rv;
nsCOMPtr <nsIImapUrl> imapUrl = do_QueryInterface(m_url, &rv);
if (imapUrl)
{
nsCOMPtr <nsIImapMailFolderSink> folderSink;
rv = imapUrl->GetImapMailFolderSink(getter_AddRefs(folderSink));
if (folderSink)
{
nsCOMPtr <nsIMsgMailNewsUrl> mailUrl = do_QueryInterface(m_url);
rv = folderSink->ReleaseUrlCacheEntry(mailUrl);
}
}
}
mChannelClosed = true;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetProgressEventSink(nsIProgressEventSink ** aProgressEventSink)
{
*aProgressEventSink = mProgressEventSink;
NS_IF_ADDREF(*aProgressEventSink);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetProgressEventSink(nsIProgressEventSink * aProgressEventSink)
{
mProgressEventSink = aProgressEventSink;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetChannelListener(nsIStreamListener **aChannelListener)
{
*aChannelListener = m_channelListener;
NS_IF_ADDREF(*aChannelListener);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetChannelContext(nsISupports **aChannelContext)
{
*aChannelContext = m_channelContext;
NS_IF_ADDREF(*aChannelContext);
return NS_OK;
}
// now implement our mock implementation of the channel interface...we forward all calls to the real
// channel if we have one...otherwise we return something bogus...
NS_IMETHODIMP nsImapMockChannel::SetLoadGroup(nsILoadGroup * aLoadGroup)
{
m_loadGroup = aLoadGroup;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetLoadGroup(nsILoadGroup * *aLoadGroup)
{
*aLoadGroup = m_loadGroup;
NS_IF_ADDREF(*aLoadGroup);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetLoadInfo(nsILoadInfo * *aLoadInfo)
{
*aLoadInfo = m_loadInfo;
NS_IF_ADDREF(*aLoadInfo);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetLoadInfo(nsILoadInfo * aLoadInfo)
{
m_loadInfo = aLoadInfo;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetOriginalURI(nsIURI* *aURI)
{
// IMap does not seem to have the notion of an original URI :-(
// *aURI = m_originalUrl ? m_originalUrl : m_url;
*aURI = m_url;
NS_IF_ADDREF(*aURI);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetOriginalURI(nsIURI* aURI)
{
// IMap does not seem to have the notion of an original URI :-(
// NS_NOTREACHED("nsImapMockChannel::SetOriginalURI");
// return NS_ERROR_NOT_IMPLEMENTED;
return NS_OK; // ignore
}
NS_IMETHODIMP nsImapMockChannel::GetURI(nsIURI* *aURI)
{
*aURI = m_url;
NS_IF_ADDREF(*aURI);
return NS_OK ;
}
NS_IMETHODIMP nsImapMockChannel::SetURI(nsIURI* aURI)
{
m_url = aURI;
#ifdef DEBUG_bienvenu
if (!aURI)
printf("Clearing URI\n");
#endif
if (m_url)
{
// if we don't have a progress event sink yet, get it from the url for now...
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url);
if (mailnewsUrl && !mProgressEventSink)
{
nsCOMPtr<nsIMsgStatusFeedback> statusFeedback;
mailnewsUrl->GetStatusFeedback(getter_AddRefs(statusFeedback));
mProgressEventSink = do_QueryInterface(statusFeedback);
}
// If this is a fetch URL and we can, get the message size from the message
// header and set it to be the content length.
// Note that for an attachment URL, this will set the content length to be
// equal to the size of the entire message.
nsCOMPtr<nsIImapUrl> imapUrl(do_QueryInterface(m_url));
nsImapAction imapAction;
imapUrl->GetImapAction(&imapAction);
if (imapAction == nsIImapUrl::nsImapMsgFetch)
{
nsCOMPtr<nsIMsgMessageUrl> msgUrl(do_QueryInterface(m_url));
if (msgUrl)
{
nsCOMPtr<nsIMsgDBHdr> msgHdr;
// A failure to get a message header isn't an error
msgUrl->GetMessageHeader(getter_AddRefs(msgHdr));
if (msgHdr)
{
uint32_t messageSize;
if (NS_SUCCEEDED(msgHdr->GetMessageSize(&messageSize)))
SetContentLength(messageSize);
}
}
}
}
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::Open(nsIInputStream **_retval)
{
return NS_ImplementChannelOpen(this, _retval);
}
NS_IMETHODIMP nsImapMockChannel::Open2(nsIInputStream **_retval)
{
nsCOMPtr<nsIStreamListener> listener;
nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
NS_ENSURE_SUCCESS(rv, rv);
return Open(_retval);
}
NS_IMETHODIMP
nsImapMockChannel::OnCacheEntryAvailable(nsICacheEntry *entry, bool aNew, nsIApplicationCache* aAppCache, nsresult status)
{
nsresult rv = NS_OK;
// make sure we didn't close the channel before the async call back came in...
// hmmm....if we had write access and we canceled this mock channel then I wonder if we should
// be invalidating the cache entry before kicking out...
if (mChannelClosed)
{
entry->AsyncDoom(nullptr);
return NS_OK;
}
if (!m_url) {
// Something has gone terribly wrong.
NS_WARNING("m_url is null in OnCacheEntryAvailable");
return Cancel(NS_ERROR_UNEXPECTED);
}
do {
// For "normal" read/write access we always receive NS_OK here. aNew
// indicates whether the cache entry is new and needs to be written, or not
// new and can be read. If AsyncOpenURI() was called with access read-only,
// status==NS_ERROR_CACHE_KEY_NOT_FOUND can be received here and we just read
// the data directly.
if (NS_FAILED(status))
break;
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url, &rv);
mailnewsUrl->SetMemCacheEntry(entry);
// For URLs not related to parts, the processing is easy:
// aNew==true means that we need to write to the entry,
// aNew==false means that we can read it.
//
// Parts processing is a little complicated, we distinguish two cases:
// 1) The caller a) knows that the part is there or
// b) it is not there and can also not be read from the
// entire message.
// In this case, the URL used as cache key addresses the part and
// mTryingToReadPart==false.
// The caller has already set up part extraction.
// This case is no different to non-part processing.
// 2) The caller wants to try to extract the part from the cache entry
// of the entire message.
// In this case, the URL used as cache key addresses the message and
// mTryingToReadPart==true.
if (mTryingToReadPart)
{
// We are here with the URI of the entire message which we know exists.
MOZ_ASSERT(!aNew,
"Logic error: Trying to read part from entire message which doesn't exist");
if (!aNew)
{
// Check the meta data.
nsCString annotation;
rv = entry->GetMetaDataElement("ContentModified", getter_Copies(annotation));
if (NS_FAILED(rv) || !annotation.EqualsLiteral("Not Modified"))
{
// The cache entry is not marked "Not Modified", that means it doesn't
// contain the entire message, so we can't use it.
// Call OpenCacheEntry() a second time to get the part.
rv = OpenCacheEntry();
if (NS_SUCCEEDED(rv))
return rv;
// Something has gone wrong, fall back to reading from the imap
// connection so the protocol doesn't hang.
break;
}
}
}
if (aNew)
{
// If we are writing, then insert a "stream listener Tee" into the flow
// to force data into the cache and to our current channel listener.
nsCOMPtr<nsIStreamListenerTee> tee = do_CreateInstance(NS_STREAMLISTENERTEE_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
nsCOMPtr<nsIOutputStream> out;
// This will fail with the cache turned off, so we need to fall through
// to ReadFromImapConnection instead of aborting with NS_ENSURE_SUCCESS(rv,rv).
rv = entry->OpenOutputStream(0, getter_AddRefs(out));
if (NS_SUCCEEDED(rv))
{
rv = tee->Init(m_channelListener, out, nullptr);
m_channelListener = do_QueryInterface(tee);
}
else
NS_WARNING("IMAP Protocol failed to open output stream to Necko cache");
}
}
else
{
rv = ReadFromMemCache(entry);
if (NS_SUCCEEDED(rv))
{
NotifyStartEndReadFromCache(true);
entry->MarkValid();
return NS_OK; // Kick out if reading from the cache succeeded.
}
entry->AsyncDoom(nullptr); // Doom entry if we failed to read from cache.
mailnewsUrl->SetMemCacheEntry(nullptr); // We aren't going to be reading from the cache.
}
} while (false);
// If reading from the cache failed or if we are writing into the cache, default to ReadFromImapConnection.
return ReadFromImapConnection();
}
NS_IMETHODIMP
nsImapMockChannel::OnCacheEntryCheck(nsICacheEntry* entry, nsIApplicationCache* appCache,
uint32_t* aResult)
{
*aResult = nsICacheEntryOpenCallback::ENTRY_WANTED;
// Check concurrent read: We can't read concurrently since we don't know
// that the entry will ever be written successfully. It may be aborted
// due to a size limitation. If reading concurrently, the following function
// will return NS_ERROR_IN_PROGRESS. Then we tell the cache to wait until
// the write is finished.
int64_t size = 0;
nsresult rv = entry->GetDataSize(&size);
if (rv == NS_ERROR_IN_PROGRESS)
*aResult = nsICacheEntryOpenCallback::RECHECK_AFTER_WRITE_FINISHED;
return NS_OK;
}
nsresult nsImapMockChannel::OpenCacheEntry()
{
nsresult rv;
// get the cache session from our imap service...
nsCOMPtr<nsIImapService> imapService = do_GetService(NS_IMAPSERVICE_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsICacheStorage> cacheStorage;
rv = imapService->GetCacheStorage(getter_AddRefs(cacheStorage));
NS_ENSURE_SUCCESS(rv, rv);
int32_t uidValidity = -1;
nsCacheAccessMode cacheAccess = nsICacheStorage::OPEN_NORMALLY;
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(m_url, &rv);
NS_ENSURE_SUCCESS(rv, rv);
bool storeResultsOffline;
nsCOMPtr<nsIImapMailFolderSink> folderSink;
rv = imapUrl->GetImapMailFolderSink(getter_AddRefs(folderSink));
if (folderSink)
folderSink->GetUidValidity(&uidValidity);
imapUrl->GetStoreResultsOffline(&storeResultsOffline);
// If we're storing the message in the offline store, don't
// write to the memory cache.
if (storeResultsOffline)
cacheAccess = nsICacheStorage::OPEN_READONLY;
// Use the uid validity as part of the cache key, so that if the uid validity
// changes, we won't re-use the wrong cache entries.
nsAutoCString extension;
extension.AppendInt(uidValidity, 16);
// Open a cache entry where the key is the potentially modified URL.
nsCOMPtr<nsIURI> newUri;
m_url->Clone(getter_AddRefs(newUri));
nsAutoCString path;
newUri->GetPath(path);
// First we need to "normalise" the URL by extracting ?part= and &filename.
// The path should only contain: ?part=x.y&filename=file.ext
// These are seen in the wild:
// /;section=2?part=1.2&filename=A01.JPG
// ?section=2?part=1.2&filename=A01.JPG&type=image/jpeg&filename=A01.JPG
// ?part=1.2&type=image/jpeg&filename=IMG_C0030.jpg
// ?header=quotebody&part=1.2&filename=lijbmghmkilicioj.png
nsAutoCString partQuery = MsgExtractQueryPart(path, "?part=");
if (partQuery.IsEmpty()) {
partQuery = MsgExtractQueryPart(path, "&part=");
if (!partQuery.IsEmpty()) {
// ? indicates a part query, so set the first character to that.
partQuery.SetCharAt('?', 0);
}
}
nsAutoCString filenameQuery = MsgExtractQueryPart(path, "&filename=");
// Truncate path at either /; or ?
int32_t ind = path.FindChar('?');
if (ind != kNotFound)
path.SetLength(ind);
ind = path.Find("/;");
if (ind != kNotFound)
path.SetLength(ind);
if (partQuery.IsEmpty())
{
// Not looking for a part. That's the easy part.
newUri->SetPath(path);
return cacheStorage->AsyncOpenURI(newUri, extension, cacheAccess, this);
}
/**
* Part processing (rest of this function).
*/
if (mTryingToReadPart)
{
// If mTryingToReadPart is set, we are here for the second time.
// We tried to read a part from the entire message but the meta data didn't
// allow it. So we come back here.
// Now request the part with its full URL.
mTryingToReadPart = false;
// Note that part extraction was already set the first time.
newUri->SetPath(path + partQuery + filenameQuery);
return cacheStorage->AsyncOpenURI(newUri, extension, cacheAccess, this);
}
// First time processing. Set up part extraction.
SetupPartExtractorListener(imapUrl, m_channelListener);
// Check whether part is in the cache.
bool exists = false;
newUri->SetPath(path + partQuery + filenameQuery);
rv = cacheStorage->Exists(newUri, extension, &exists);
NS_ENSURE_SUCCESS(rv, rv);
if (exists) {
return cacheStorage->AsyncOpenURI(newUri, extension, cacheAccess, this);
}
// Let's see whether we have the entire message instead.
newUri->SetPath(path);
rv = cacheStorage->Exists(newUri, extension, &exists);
NS_ENSURE_SUCCESS(rv, rv);
if (!exists) {
// The entire message is not in the cache. Request the part.
newUri->SetPath(path + partQuery + filenameQuery);
return cacheStorage->AsyncOpenURI(newUri, extension, cacheAccess, this);
}
// This is where is gets complicated. The entire message is in the cache,
// but we don't know whether it's suitable for use. Its meta data
// might indicate that the message is incomplete.
mTryingToReadPart = true;
return cacheStorage->AsyncOpenURI(newUri, extension, cacheAccess, this);
}
nsresult nsImapMockChannel::ReadFromMemCache(nsICacheEntry *entry)
{
NS_ENSURE_ARG(entry);
nsCString annotation;
nsAutoCString entryKey;
nsAutoCString contentType;
nsresult rv = NS_OK;
bool shouldUseCacheEntry = false;
entry->GetKey(entryKey);
if (entryKey.FindChar('?') != kNotFound)
{
// Part processing: If we have a part, then we should use the cache entry.
entry->GetMetaDataElement("contentType", getter_Copies(contentType));
if (!contentType.IsEmpty())
SetContentType(contentType);
shouldUseCacheEntry = true;
}
else
{
// Whole message processing: We should make sure the content isn't modified.
rv = entry->GetMetaDataElement("ContentModified", getter_Copies(annotation));
if (NS_SUCCEEDED(rv) && !annotation.IsEmpty())
shouldUseCacheEntry = annotation.EqualsLiteral("Not Modified");
// Compare cache entry size with message size.
if (shouldUseCacheEntry)
{
int64_t entrySize;
rv = entry->GetDataSize(&entrySize);
// We don't expect concurrent read here, so this call should always work.
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgMessageUrl> msgUrl(do_QueryInterface(m_url));
if (msgUrl)
{
nsCOMPtr<nsIMsgDBHdr> msgHdr;
// A failure to get a message header isn't an error
msgUrl->GetMessageHeader(getter_AddRefs(msgHdr));
if (msgHdr)
{
uint32_t messageSize;
if (NS_SUCCEEDED(msgHdr->GetMessageSize(&messageSize)) &&
messageSize != entrySize)
{
MOZ_LOG(IMAP, LogLevel::Warning,
("ReadFromMemCache size mismatch for %s: message %d, cache %ld\n",
entryKey.get(), messageSize, entrySize));
shouldUseCacheEntry = false;
}
}
}
}
}
/**
* Common processing for full messages and message parts.
*/
// Check header of full message or part.
if (shouldUseCacheEntry)
{
nsCOMPtr<nsIInputStream> in;
uint32_t readCount;
rv = entry->OpenInputStream(0, getter_AddRefs(in));
NS_ENSURE_SUCCESS(rv, rv);
const int kFirstBlockSize = 100;
char firstBlock[kFirstBlockSize + 1];
// Note: This will not work for a cache2 disk cache.
// (see bug 1302422 comment #4)
rv = in->Read(firstBlock, sizeof(firstBlock), &readCount);
NS_ENSURE_SUCCESS(rv, rv);
firstBlock[kFirstBlockSize] = '\0';
int32_t findPos = MsgFindCharInSet(nsDependentCString(firstBlock),
":\n\r", 0);
// Check that the first line is a header line, i.e., with a ':' in it
// Or that it begins with "From " because some IMAP servers allow that,
// even though it's technically invalid.
shouldUseCacheEntry = ((findPos != -1 && firstBlock[findPos] == ':') ||
!(strncmp(firstBlock, "From ", 5)));
in->Close();
}
if (shouldUseCacheEntry)
{
nsCOMPtr<nsIInputStream> in;
rv = entry->OpenInputStream(0, getter_AddRefs(in));
NS_ENSURE_SUCCESS(rv, rv);
// if mem cache entry is broken or empty, return error.
uint64_t bytesAvailable;
rv = in->Available(&bytesAvailable);
NS_ENSURE_SUCCESS(rv, rv);
if (!bytesAvailable)
return NS_ERROR_FAILURE;
nsCOMPtr<nsIInputStreamPump> pump;
rv = NS_NewInputStreamPump(getter_AddRefs(pump), in);
NS_ENSURE_SUCCESS(rv, rv);
// if we are going to read from the cache, then create a mock stream listener class and use it
nsImapCacheStreamListener * cacheListener = new nsImapCacheStreamListener();
NS_ADDREF(cacheListener);
cacheListener->Init(m_channelListener, this);
rv = pump->AsyncRead(cacheListener, m_channelContext);
NS_RELEASE(cacheListener);
if (NS_SUCCEEDED(rv)) // ONLY if we succeeded in actually starting the read should we return
{
mCacheRequest = pump;
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(m_url);
// if the msg is unread, we should mark it read on the server. This lets
// the code running this url we're loading from the cache, if it cares.
imapUrl->SetMsgLoadingFromCache(true);
// be sure to set the cache entry's security info status as our security info status...
nsCOMPtr<nsISupports> securityInfo;
entry->GetSecurityInfo(getter_AddRefs(securityInfo));
SetSecurityInfo(securityInfo);
return NS_OK;
} // if AsyncRead succeeded.
} // if content is not modified
else
{
// Content is modified so return an error so we try to open it the
// old fashioned way.
rv = NS_ERROR_FAILURE;
}
return rv;
}
class nsReadFromImapConnectionFailure : public mozilla::Runnable
{
public:
explicit nsReadFromImapConnectionFailure(nsImapMockChannel *aChannel)
: mImapMockChannel(aChannel)
{}
NS_IMETHOD Run()
{
if (mImapMockChannel) {
mImapMockChannel->RunOnStopRequestFailure();
}
return NS_OK;
}
private:
RefPtr<nsImapMockChannel> mImapMockChannel;
};
nsresult nsImapMockChannel::RunOnStopRequestFailure()
{
if (m_channelListener) {
m_channelListener->OnStopRequest(this, m_channelContext,
NS_MSG_ERROR_MSG_NOT_OFFLINE);
}
return NS_OK;
}
// the requested url isn't in any of our caches so create an imap connection
// to process it.
nsresult nsImapMockChannel::ReadFromImapConnection()
{
nsresult rv = NS_OK;
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(m_url);
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url);
bool localOnly = false;
imapUrl->GetLocalFetchOnly(&localOnly);
if (localOnly)
{
// This will cause an OnStartRunningUrl, and the subsequent close
// will then cause an OnStopRunningUrl with the cancel status.
NotifyStartEndReadFromCache(true);
Cancel(NS_MSG_ERROR_MSG_NOT_OFFLINE);
// Dispatch error notification, so ReadFromImapConnection() returns *before*
// the error is sent to the listener's OnStopRequest(). This avoids
// endless recursion where the caller relies on async execution.
nsCOMPtr<nsIRunnable> event = new nsReadFromImapConnectionFailure(this);
NS_DispatchToCurrentThread(event);
return NS_MSG_ERROR_MSG_NOT_OFFLINE;
}
nsCOMPtr <nsILoadGroup> loadGroup;
GetLoadGroup(getter_AddRefs(loadGroup));
if (!loadGroup) // if we don't have one, the url will snag one from the msg window...
mailnewsUrl->GetLoadGroup(getter_AddRefs(loadGroup));
// okay, add the mock channel to the load group..
if (loadGroup)
loadGroup->AddRequest((nsIRequest *) this, nullptr /* context isupports */);
// loading the url consists of asking the server to add the url to it's imap event queue....
nsCOMPtr<nsIMsgIncomingServer> server;
rv = mailnewsUrl->GetServer(getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIImapIncomingServer> imapServer (do_QueryInterface(server, &rv));
NS_ENSURE_SUCCESS(rv, rv);
// Assume AsyncRead is always called from the UI thread.....
return imapServer->GetImapConnectionAndLoadUrl(imapUrl, m_channelListener);
}
// for messages stored in our offline cache, we have special code to handle that...
// If it's in the local cache, we return true and we can abort the download because
// this method does the rest of the work.
bool nsImapMockChannel::ReadFromLocalCache()
{
nsresult rv = NS_OK;
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(m_url);
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url, &rv);
bool useLocalCache = false;
mailnewsUrl->GetMsgIsInLocalCache(&useLocalCache);
if (useLocalCache)
{
nsAutoCString messageIdString;
SetupPartExtractorListener(imapUrl, m_channelListener);
imapUrl->GetListOfMessageIds(messageIdString);
nsCOMPtr <nsIMsgFolder> folder;
rv = mailnewsUrl->GetFolder(getter_AddRefs(folder));
if (folder && NS_SUCCEEDED(rv))
{
// we want to create a file channel and read the msg from there.
nsCOMPtr<nsIInputStream> fileStream;
nsMsgKey msgKey = strtoul(messageIdString.get(), nullptr, 10);
uint32_t size;
int64_t offset;
rv = folder->GetOfflineFileStream(msgKey, &offset, &size, getter_AddRefs(fileStream));
// get the file channel from the folder, somehow (through the message or
// folder sink?) We also need to set the transfer offset to the message offset
if (fileStream && NS_SUCCEEDED(rv))
{
// dougt - This may break the ablity to "cancel" a read from offline mail reading.
// fileChannel->SetLoadGroup(m_loadGroup);
nsImapCacheStreamListener * cacheListener = new nsImapCacheStreamListener();
NS_ADDREF(cacheListener);
cacheListener->Init(m_channelListener, this);
// create a stream pump that will async read the specified amount of data.
// XXX make offset/size 64-bit ints
nsCOMPtr<nsIInputStreamPump> pump;
rv = NS_NewInputStreamPump(getter_AddRefs(pump), fileStream,
offset, (int64_t) size);
if (NS_SUCCEEDED(rv))
rv = pump->AsyncRead(cacheListener, m_channelContext);
NS_RELEASE(cacheListener);
if (NS_SUCCEEDED(rv)) // ONLY if we succeeded in actually starting the read should we return
{
// if the msg is unread, we should mark it read on the server. This lets
// the code running this url we're loading from the cache, if it cares.
imapUrl->SetMsgLoadingFromCache(true);
return true;
}
} // if we got an offline file transport
} // if we got the folder for this url
} // if use local cache
return false;
}
NS_IMETHODIMP nsImapMockChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *ctxt)
{
nsresult rv = NS_OK;
int32_t port;
if (!m_url)
return NS_ERROR_NULL_POINTER;
rv = m_url->GetPort(&port);
if (NS_FAILED(rv))
return rv;
rv = NS_CheckPortSafety(port, "imap");
if (NS_FAILED(rv))
return rv;
// set the stream listener and then load the url
m_channelContext = ctxt;
NS_ASSERTION(!m_channelListener, "shouldn't already have a listener");
m_channelListener = listener;
nsCOMPtr<nsIImapUrl> imapUrl (do_QueryInterface(m_url));
nsImapAction imapAction;
imapUrl->GetImapAction(&imapAction);
bool externalLink = true;
imapUrl->GetExternalLinkUrl(&externalLink);
if (externalLink)
{
// for security purposes, only allow imap urls originating from external sources
// perform a limited set of actions.
// Currently the allowed set includes:
// 1) folder selection
// 2) message fetch
// 3) message part fetch
if (! (imapAction == nsIImapUrl::nsImapSelectFolder || imapAction == nsIImapUrl::nsImapMsgFetch || imapAction == nsIImapUrl::nsImapOpenMimePart
|| imapAction == nsIImapUrl::nsImapMsgFetchPeek))
return NS_ERROR_FAILURE; // abort the running of this url....it failed a security check
}
if (ReadFromLocalCache())
{
(void) NotifyStartEndReadFromCache(true);
return NS_OK;
}
// okay, it's not in the local cache, now check the memory cache...
// but we can't download for offline use from the memory cache
if (imapAction != nsIImapUrl::nsImapMsgDownloadForOffline)
{
rv = OpenCacheEntry();
if (NS_SUCCEEDED(rv))
return rv;
}
SetupPartExtractorListener(imapUrl, m_channelListener);
// if for some reason open cache entry failed then just default to opening an imap connection for the url
return ReadFromImapConnection();
}
NS_IMETHODIMP nsImapMockChannel::AsyncOpen2(nsIStreamListener *aListener)
{
nsCOMPtr<nsIStreamListener> listener = aListener;
nsresult rv = nsContentSecurityManager::doContentSecurityCheck(this, listener);
NS_ENSURE_SUCCESS(rv, rv);
return AsyncOpen(listener, nullptr);
}
nsresult nsImapMockChannel::SetupPartExtractorListener(nsIImapUrl * aUrl, nsIStreamListener * aConsumer)
{
// if the url we are loading refers to a specific part then we need
// libmime to extract that part from the message for us.
bool refersToPart = false;
aUrl->GetMimePartSelectorDetected(&refersToPart);
if (refersToPart)
{
nsCOMPtr<nsIStreamConverterService> converter = do_GetService("@mozilla.org/streamConverters;1");
if (converter && aConsumer)
{
nsCOMPtr<nsIStreamListener> newConsumer;
converter->AsyncConvertData("message/rfc822", "*/*",
aConsumer, static_cast<nsIChannel *>(this), getter_AddRefs(newConsumer));
if (newConsumer)
m_channelListener = newConsumer;
}
}
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetLoadFlags(nsLoadFlags *aLoadFlags)
{
//*aLoadFlags = nsIRequest::LOAD_NORMAL;
*aLoadFlags = mLoadFlags;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetLoadFlags(nsLoadFlags aLoadFlags)
{
mLoadFlags = aLoadFlags;
return NS_OK; // don't fail when trying to set this
}
NS_IMETHODIMP nsImapMockChannel::GetContentType(nsACString &aContentType)
{
if (mContentType.IsEmpty())
{
nsImapAction imapAction = 0;
if (m_url)
{
nsCOMPtr<nsIImapUrl> imapUrl = do_QueryInterface(m_url);
if (imapUrl)
{
imapUrl->GetImapAction(&imapAction);
}
}
if (imapAction == nsIImapUrl::nsImapSelectFolder)
aContentType.AssignLiteral("x-application-imapfolder");
else
aContentType.AssignLiteral("message/rfc822");
}
else
aContentType = mContentType;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetContentType(const nsACString &aContentType)
{
nsAutoCString charset;
nsresult rv = NS_ParseResponseContentType(aContentType, mContentType, charset);
if (NS_FAILED(rv) || mContentType.IsEmpty())
mContentType.AssignLiteral(UNKNOWN_CONTENT_TYPE);
return rv;
}
NS_IMETHODIMP nsImapMockChannel::GetContentCharset(nsACString &aContentCharset)
{
aContentCharset.Assign(mCharset);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetContentCharset(const nsACString &aContentCharset)
{
mCharset.Assign(aContentCharset);
return NS_OK;
}
NS_IMETHODIMP
nsImapMockChannel::GetContentDisposition(uint32_t *aContentDisposition)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsImapMockChannel::SetContentDisposition(uint32_t aContentDisposition)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsImapMockChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsImapMockChannel::SetContentDispositionFilename(const nsAString &aContentDispositionFilename)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsImapMockChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader)
{
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP nsImapMockChannel::GetContentLength(int64_t * aContentLength)
{
*aContentLength = mContentLength;
return NS_OK;
}
NS_IMETHODIMP
nsImapMockChannel::SetContentLength(int64_t aContentLength)
{
mContentLength = aContentLength;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetOwner(nsISupports * *aPrincipal)
{
*aPrincipal = mOwner;
NS_IF_ADDREF(*aPrincipal);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetOwner(nsISupports * aPrincipal)
{
mOwner = aPrincipal;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
{
NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetSecurityInfo(nsISupports *aSecurityInfo)
{
mSecurityInfo = aSecurityInfo;
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// From nsIRequest
////////////////////////////////////////////////////////////////////////////////
NS_IMETHODIMP nsImapMockChannel::GetName(nsACString &result)
{
if (m_url)
return m_url->GetSpec(result);
result.Truncate();
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::IsPending(bool *result)
{
*result = m_channelListener != nullptr;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::GetStatus(nsresult *status)
{
*status = m_cancelStatus;
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::SetImapProtocol(nsIImapProtocol *aProtocol)
{
mProtocol = do_GetWeakReference(aProtocol);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::Cancel(nsresult status)
{
NS_WARNING_ASSERTION(NS_IsMainThread(),
"nsImapMockChannel::Cancel should only be called from UI thread");
m_cancelStatus = status;
nsCOMPtr<nsIImapProtocol> imapProtocol = do_QueryReferent(mProtocol);
// if we aren't reading from the cache and we get canceled...doom our cache entry...
if (m_url)
{
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url);
DoomCacheEntry(mailnewsUrl);
}
// Required for killing ImapProtocol thread
if (imapProtocol)
imapProtocol->TellThreadToDie(false);
return NS_OK;
}
NS_IMETHODIMP nsImapMockChannel::Suspend()
{
NS_NOTREACHED("nsImapMockChannel::Suspend");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsImapMockChannel::Resume()
{
NS_NOTREACHED("nsImapMockChannel::Resume");
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsImapMockChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
{
*aNotificationCallbacks = mCallbacks.get();
NS_IF_ADDREF(*aNotificationCallbacks);
return NS_OK;
}
NS_IMETHODIMP
nsImapMockChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
{
mCallbacks = aNotificationCallbacks;
return NS_OK;
}
NS_IMETHODIMP
nsImapMockChannel::OnTransportStatus(nsITransport *transport, nsresult status,
int64_t progress, int64_t progressMax)
{
if (NS_FAILED(m_cancelStatus) || (mLoadFlags & LOAD_BACKGROUND) || !m_url)
return NS_OK;
// these transport events should not generate any status messages
if (status == NS_NET_STATUS_RECEIVING_FROM ||
status == NS_NET_STATUS_SENDING_TO)
return NS_OK;
if (!mProgressEventSink)
{
NS_QueryNotificationCallbacks(mCallbacks, m_loadGroup, mProgressEventSink);
if (!mProgressEventSink)
return NS_OK;
}
nsAutoCString host;
m_url->GetHost(host);
nsCOMPtr<nsIMsgMailNewsUrl> mailnewsUrl = do_QueryInterface(m_url);
if (mailnewsUrl)
{
nsCOMPtr<nsIMsgIncomingServer> server;
mailnewsUrl->GetServer(getter_AddRefs(server));
if (server)
server->GetRealHostName(host);
}
mProgressEventSink->OnStatus(this, nullptr, status,
NS_ConvertUTF8toUTF16(host).get());
return NS_OK;
}
nsIMAPMailboxInfo::nsIMAPMailboxInfo(const nsACString &aName, char aDelimiter)
{
mMailboxName.Assign(aName);
mDelimiter = aDelimiter;
mChildrenListed = false;
}
nsIMAPMailboxInfo::~nsIMAPMailboxInfo()
{
}
void nsIMAPMailboxInfo::SetChildrenListed(bool childrenListed)
{
mChildrenListed = childrenListed;
}
bool nsIMAPMailboxInfo::GetChildrenListed()
{
return mChildrenListed;
}
const nsACString& nsIMAPMailboxInfo::GetMailboxName()
{
return mMailboxName;
}
char nsIMAPMailboxInfo::GetDelimiter()
{
return mDelimiter;
}
|