~vm/vm/message

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
;;; vm-imap.el ---  Simple IMAP4 (RFC 2060) client for VM
;;
;; This file is part of VM
;;
;; Copyright (C) 1998, 2001, 2003 Kyle E. Jones
;; Copyright (C) 2003-2006 Robert Widhopf-Fenk
;; Copyright (C) 2006 Robert P. Goldman
;; Copyright (C) 2008-2010 Uday S. Reddy
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License along
;; with this program; if not, write to the Free Software Foundation, Inc.,
;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

;;; Code:

(provide 'vm-imap)

(eval-when-compile 
  (require 'sendmail)
  (require 'vm-misc))

;; For function declarations
(eval-when-compile
  (require 'vm-folder)
  (require 'vm-summary)
  (require 'vm-window)
  (require 'vm-motion)
  (require 'vm-undo)
  (require 'vm-delete)
  (require 'vm-crypto)
  (require 'vm-mime)
)

(declare-function vm-session-initialization 
		  "vm.el" ())
(declare-function vm-submit-bug-report 
		  "vm.el" (&optional pre-hooks post-hooks))

(defvar selectable-only)		; used with dynamic binding

;; To-Do  (USR)
;; - Need to ensure that new imap sessions get created as and when needed.

;; ------------------------------------------------------------------------
;; The IMAP session protocol
;; ------------------------------------------------------------------------

;; Folder-specific IMAP sessions are created and destroyed for each
;; get-new-mail.  (Same as in VM 7.19)

;; check-for-new-mail also creates and destroys sessions.

;; Rob F's save-composition creates and destroys its own sessions.

;; They are also created and destroyed at a global level for
;; operations like create-mailbox.  (VM 7.19 didn't destroy them in the
;; end, but we do.)

;; synchronize-folder creates an IMAP session but leaves it active.
;; Since session is linked to the folder buffer, the folder can use it
;; for other operations like fetch-imap-message and copy-message.  The
;; next time a synchronize-folder is done, this session is killed and
;; a fresh session is created.

;; ------------------------------------------------------------------------
;; Utilities
;; ------------------------------------------------------------------------

;; the maildrop spec of the imap folder
(defsubst vm-folder-imap-maildrop-spec ()
  (aref vm-folder-access-data 0))
;; current imap process of the folder - each folder has a separate one
(defsubst vm-folder-imap-process ()
  (aref vm-folder-access-data 1))
;; the uid validity value of the imap folder
(defsubst vm-folder-imap-uid-validity ()
  (aref vm-folder-access-data 2))
;; the list of uid's and flags of the messages in the imap folder
;; (msg-num . uid . size . flags list)
(defsubst vm-folder-imap-uid-list ()
  (aref vm-folder-access-data 3))	
;; the number of messages in the imap folder
(defsubst vm-folder-imap-mailbox-count ()
  (aref vm-folder-access-data 4))
;; flag indicating whether the imap folder allows writing
(defsubst vm-folder-imap-read-write ()
  (aref vm-folder-access-data 5))
;; flag indicating whether the imap folder allows deleting
(defsubst vm-folder-imap-can-delete ()
  (aref vm-folder-access-data 6))
;; flag indicating whether the imap server has body-peek functionality
(defsubst vm-folder-imap-body-peek ()
  (aref vm-folder-access-data 7))
;; list of permanent flags storable on the imap server
(defsubst vm-folder-imap-permanent-flags ()
  (aref vm-folder-access-data 8))
;; obarray of uid's with message numbers as their values
(defsubst vm-folder-imap-uid-obarray ()
  (aref vm-folder-access-data 9))	; obarray(uid, msg-num)
;; obarray of uid's with flags lists as their values
(defsubst vm-folder-imap-flags-obarray ()
  (aref vm-folder-access-data 10))	; obarray(uid, (size . flags list))
					; cons-pair shared with imap-uid-list

(defsubst vm-set-folder-imap-maildrop-spec (val)
  (aset vm-folder-access-data 0 val))
(defsubst vm-set-folder-imap-process (val)
  (aset vm-folder-access-data 1 val))
(defsubst vm-set-folder-imap-uid-validity (val)
  (aset vm-folder-access-data 2 val))
(defsubst vm-set-folder-imap-uid-list (val)
  (aset vm-folder-access-data 3 val))
(defsubst vm-set-folder-imap-mailbox-count (val)
  (aset vm-folder-access-data 4 val))
(defsubst vm-set-folder-imap-read-write (val)
  (aset vm-folder-access-data 5 val))
(defsubst vm-set-folder-imap-can-delete (val)
  (aset vm-folder-access-data 6 val))
(defsubst vm-set-folder-imap-body-peek (val)
  (aset vm-folder-access-data 7 val))
(defsubst vm-set-folder-imap-permanent-flags (val)
  (aset vm-folder-access-data 8 val))
(defsubst vm-set-folder-imap-uid-obarray (val)
  (aset vm-folder-access-data 9 val))
(defsubst vm-set-folder-imap-flags-obarray (val)
  (aset vm-folder-access-data 10 val))

;; For logging IMAP sessions

(defvar vm-imap-log-sessions nil
  "* Boolean flag to turn on or off logging of IMAP sessions.  Meant
  for debugging IMAP server interactions.")

(defvar vm-imap-tokens nil)

(defsubst vm-imap-init-log ()
  (setq vm-imap-tokens nil))

(defsubst vm-imap-log-token (token)
  (if vm-imap-log-sessions
      (setq vm-imap-tokens (cons token vm-imap-tokens))))
  
(defsubst vm-imap-log-tokens (tokens)
  (if vm-imap-log-sessions
      (setq vm-imap-tokens (append (nreverse tokens) vm-imap-tokens))))

;; For verification of session protocol
;; Possible values are 
;; 'active - active session present
;; 'valid - message sequence numbers are valid 
;;	validity is preserved by FETCH, STORE and SEARCH operations
;; 'inactive - session is inactive

;; (defvar vm-imap-session-type nil)  ; moved to vm-vars.el

(defun vm-imap-session-type:set (type)
  (setq vm-imap-session-type type))

(defun vm-imap-session-type:make-active ()
  (if (eq vm-imap-session-type 'inactive)
      (setq vm-imap-session-type 'active)))

(defsubst vm-imap-session-type:assert (type)
  (vm-assert (eq vm-imap-session-type type)))

(defsubst vm-imap-folder-session-type:assert (type)
  (with-current-buffer (process-buffer (vm-folder-imap-process))
    (vm-assert (eq vm-imap-session-type type))))

(defsubst vm-imap-session-type:assert-active ()
  (vm-assert (or (eq vm-imap-session-type 'active) 
		 (eq vm-imap-session-type 'valid))))

;; Simple macros

(defsubst vm-imap-delete-message (process n)
  (vm-imap-delete-messages process n n))

(if (fboundp 'define-error)
    (progn
      (define-error 'vm-imap-protocol-error "IMAP protocol error")
      (define-error 'vm-imap-normal-error "IMAP error" 'vm-imap-protocol-error)
      )
  (put 'vm-imap-protocol-error 'error-conditions
       '(vm-imap-protocol-error error))
  (put 'vm-imap-protocol-error 'error-message "IMAP protocol error")
  (put 'vm-imap-normal-error 'error-conditions
       '(vm-imap-protocol-error vm-imap-normal-error error))
  (put 'vm-imap-normal-error 'error-message "IMAP error")
  )

(defun vm-imap-protocol-error (&rest args)
  (set (make-local-variable 'vm-imap-keep-trace-buffer) t)
  (signal 'vm-imap-protocol-error (list (apply 'format args))))

(defun vm-imap-normal-error (&rest args)
  (set (make-local-variable 'vm-imap-keep-trace-buffer) t)
  (signal 'vm-imap-normal-error (list (apply 'format args))))

(defun vm-imap-capability (cap &optional process)
  (if process
      (with-current-buffer (process-buffer process)
	(memq cap vm-imap-capabilities))
    (memq cap vm-imap-capabilities)))

(defun vm-imap-auth-method (auth)
  (memq auth vm-imap-auth-methods))

(defsubst vm-accept-process-output (process)
  "Accept output from PROCESS.  The variable `vm-imap-server-timeout'
specifies how many seconds to wait before timing out.  If a timeout
occurs, typically VM cannot proceed."
  ;; protect against possible buffer change due to bug in Emacs
  (let ((buf (current-buffer))
	(got-output (accept-process-output process vm-imap-server-timeout)))
    (if got-output
	(when (not (equal (current-buffer) buf))
	  (if (string-lessp "23" emacs-version)
	      ;; the Emacs bug should have been fixed
	      (message 
	       "Emacs process output error: Buffer changed to %s" 
	       (current-buffer)))
	  ;; recover from the bug
	  (set-buffer buf))
      (vm-imap-protocol-error "No response from the IMAP server"))))


;; Mollify the pesky compiler
(defvar selectable-only)

(defvar vm-imap-connection-mode 'online
  "* The mode of connection to the IMAP server.  Possible values
are: 'online, 'offline and 'autoconnect.  In the 'online mode,
synchronization works normally and message bodies of headers-only
messages are fetched when needed.  In 'offline mode, no
connection is established to the IMAP server and message bodies
are not fetched.  In the 'autoconnect mode, a connection is
established whenever a synchronization operation is performed and the
connection mode is then turned into 'online.")

(defun delete-common-elements (list1 list2 pred)
  ;; Takes two lists of unique values with dummy headers and
  ;; destructively deletes all their common elements
  (rplacd list1 (sort (cdr list1) pred))
  (rplacd list2 (sort (cdr list2) pred))
  (while (and (cdr list1) (cdr list2))
    (cond ((equal (car (cdr list1)) (car (cdr list2)))
	   (rplacd list1 (cdr (cdr list1)))
	   (rplacd list2 (cdr (cdr list2))))
	  ((apply pred (car (cdr list1)) (car (cdr list2)) nil)
	   (setq list1 (cdr list1)))
	  (t
	   (setq list2 (cdr list2)))
	  )))


;; -----------------------------------------------------------------------
;; IMAP Spool
;; 
;; -- Functions that treat IMAP mailboxes as spools to get mail
;; -- into local buffers and subsequently expunge on the server.
;; -- USR thinks this is obsolete functionality that should not be
;; -- used. Use 'IMAP folders' instead.
;;
;; handler methods:
;; vm-imap-move-mail: (string & string) -> bool
;; vm-imap-check-mail: string -> void
;;
;; interactive commands:
;; vm-expunge-imap-messages: () -> void
;;
;; vm-imap-clear-invalid-retrieval-entries: ... 
;; ------------------------------------------------------------------------


(defsubst vm-imap-fetch-message (process n use-body-peek 
					   &optional headers-only)
  (vm-imap-fetch-messages process n n use-body-peek headers-only))

(defun vm-imap-fetch-messages (process beg end use-body-peek 
				       &optional headers-only) 
  (let ((fetchcmd
         (if headers-only
             (if use-body-peek "(BODY.PEEK[HEADER])" "(RFC822.HEADER)")
           (if use-body-peek "(BODY.PEEK[])" "(RFC822.PEEK)"))))
    (vm-imap-send-command process (format "FETCH %d:%d %s" beg end fetchcmd))))

(defsubst vm-imap-fetch-uid-message (process uid use-body-peek 
					   &optional headers-only)
  (let ((fetchcmd
         (if headers-only
             (if use-body-peek "(BODY.PEEK[HEADER])" "(RFC822.HEADER)")
           (if use-body-peek "(BODY.PEEK[])" "(RFC822.PEEK)"))))
    (vm-imap-send-command 
     process (format "UID FETCH %s:%s %s" uid uid fetchcmd))))

;; Our goal is to drag the mail from the IMAP maildrop to the crash box.
;; just as if we were using movemail on a spool file.
;; We remember which messages we have retrieved so that we can
;; leave the message in the mailbox, and yet not retrieve the
;; same messages again and again.

;;;###autoload
(defun vm-imap-move-mail (source destination)
  "move-mail function for IMAP folders.  SOURCE is the IMAP mail box
from which mail is to be moved and DESTINATION is the VM folder."
  ;;--------------------------
  (vm-buffer-type:set 'folder)
  ;;--------------------------
  (let ((process nil)
	(m-per-session vm-imap-messages-per-session)
	(b-per-session vm-imap-bytes-per-session)
	(handler (vm-find-file-name-handler source 'vm-imap-move-mail))
	(folder (or (vm-imap-folder-for-spec source)
		    (vm-safe-imapdrop-string source)))
	(statblob nil)
	(msgid (list nil nil (vm-imapdrop-sans-password source) 'uid))
	(imap-retrieved-messages vm-imap-retrieved-messages)
	(did-delete nil)
	(source-nopwd (vm-imapdrop-sans-password source))
	use-body-peek auto-expunge x select source-list uid
	can-delete read-write uid-validity
	mailbox mailbox-count message-size response
	n (retrieved 0) retrieved-bytes process-buffer)
    (setq auto-expunge 
	  (cond ((setq x (assoc source
				vm-imap-auto-expunge-alist))
		 (cdr x))
		((setq x (assoc (vm-imapdrop-sans-password source)
				vm-imap-auto-expunge-alist))
		 (cdr x))
		(t (if vm-imap-expunge-after-retrieving
		       t
		     (message 
		      (concat "Leaving messages on IMAP server; "
			      "See info under \"IMAP Spool Files\""))
		     (sit-for 4)
		     nil))))

    (unwind-protect
	(catch 'end-of-session
	  (if handler
	      (throw 'end-of-session
		     (funcall handler 'vm-imap-move-mail source destination)))
	  (setq process 
		(vm-imap-make-session source vm-imap-ok-to-ask "movemail"))
	  (or process (throw 'end-of-session nil))
	  (setq process-buffer (process-buffer process))
	  (save-excursion		; = save-current-buffer?
	    (set-buffer process-buffer)
	    ;;--------------------------------
	    (vm-buffer-type:enter 'process)
	    ;;--------------------------------
	    ;; find out how many messages are in the box.
	    (setq source-list (vm-parse source "\\([^:]+\\):?")
		  mailbox (nth 3 source-list))
	    (setq select (vm-imap-select-mailbox process mailbox))
	    (setq mailbox-count (nth 0 select)
		  uid-validity (nth 1 select)
		  read-write (nth 2 select)
		  can-delete (nth 3 select)
		  use-body-peek (vm-imap-capability 'IMAP4REV1))
	    ;;--------------------------------
	    (vm-imap-session-type:set 'valid)
	    ;;--------------------------------
	    ;; The session type is not really "valid" because the uid
	    ;; and flags data has not been obtained.  But since
	    ;; move-mail uses a short, bursty session, the effect is
	    ;; that of a valid session throughout.

	    ;; sweep through the retrieval list, removing entries
	    ;; that have been invalidated by the new UIDVALIDITY
	    ;; value.
	    (setq imap-retrieved-messages
		  (vm-imap-clear-invalid-retrieval-entries
		   source-nopwd
		   imap-retrieved-messages
		   uid-validity))
	    ;; loop through the maildrop retrieving and deleting
	    ;; messages as we go.
	    (setq n 1 retrieved-bytes 0)
	    (setq statblob (vm-imap-start-status-timer))
	    (vm-set-imap-stat-x-mailbox statblob folder)
	    (vm-set-imap-stat-x-maxmsg statblob mailbox-count)
	    (while (and (<= n mailbox-count)
			(or (not (natnump m-per-session))
			    (< retrieved m-per-session))
			(or (not (natnump b-per-session))
			    (< retrieved-bytes b-per-session)))
	      (catch 'skip
		(vm-set-imap-stat-x-currmsg statblob n)
		(let (list)
		  (setq list (vm-imap-get-uid-list process n n))
		  (setq uid (cdr (car list)))
		  (setcar msgid uid)
		  (setcar (cdr msgid) uid-validity)
		  (if (member msgid imap-retrieved-messages)
		      (progn
			(if vm-imap-ok-to-ask
			    (message
			     "Skipping message %d (of %d) from %s (retrieved already)..."
			     n mailbox-count folder))
			(throw 'skip t))))
		(setq message-size (vm-imap-get-message-size process n))
		(vm-set-imap-stat-x-need statblob message-size)
		(if (and (integerp vm-imap-max-message-size)
			 (> message-size vm-imap-max-message-size)
			 (progn
			   (setq response
				 (if vm-imap-ok-to-ask
				     (vm-imap-ask-about-large-message
				      process message-size n)
				   'skip))
			   (not (eq response 'retrieve))))
		    (progn
		      (if (and read-write can-delete (eq response 'delete))
			  (progn
			    (message "Deleting message %d..." n)
			    (vm-imap-delete-message process n)
			    (setq did-delete t))
			(if vm-imap-ok-to-ask
			    (message "Skipping message %d..." n)
			  (message
			   "Skipping message %d in %s, too large (%d > %d)..."
			   n folder message-size vm-imap-max-message-size)))
		      (throw 'skip t)))
		(message "Retrieving message %d (of %d) from %s..."
			 n mailbox-count folder)
                (vm-imap-fetch-message process n
				       use-body-peek nil) ; no headers-only
                (vm-imap-retrieve-to-target process destination
					    statblob use-body-peek) 
		(vm-imap-read-ok-response process)
                (message "Retrieving message %d (of %d) from %s...done"
                         n mailbox-count folder)
		(vm-increment retrieved)
		(and b-per-session
		     (setq retrieved-bytes (+ retrieved-bytes message-size)))
		(setq imap-retrieved-messages
		      (cons (copy-sequence msgid)
			    imap-retrieved-messages))
		(if auto-expunge
		  ;; The user doesn't want the messages
		  ;; kept in the mailbox.
		  ;; Delete the message now.
		  (if (and read-write can-delete)
		      (progn
			(vm-imap-delete-message process n)
			(setq did-delete t)))))
	      (vm-increment n))
	    (if did-delete
		(progn
		  ;; CLOSE forces an expunge and avoids the EXPUNGE
		  ;; responses.
		  (vm-imap-send-command process "CLOSE")
		  (vm-imap-read-ok-response process)
		  ;;----------------------------------
		  (vm-imap-session-type:set 'inactive)
		  ;;----------------------------------
		  ))
	    ;;-------------------
	    (vm-buffer-type:exit)
	    ;;-------------------
	    (not (equal retrieved 0))	; return result
	    ))
      ;; unwind-protections
      (setq vm-imap-retrieved-messages imap-retrieved-messages)
      (if (and (eq vm-flush-interval t) (not (equal retrieved 0)))
	  (vm-stuff-imap-retrieved))
      (when statblob 
	(vm-imap-stop-status-timer statblob))
      (when process
	(vm-imap-end-session process))
      )))

(defun vm-imap-check-mail (source)
  ;;--------------------------
  (vm-buffer-type:set 'folder)
  ;;--------------------------
  (let ((process nil)
	(handler (vm-find-file-name-handler source 'vm-imap-check-mail))
	(retrieved vm-imap-retrieved-messages)
	(imapdrop (vm-imapdrop-sans-password source))
	(count 0)
	msg-count uid-validity x response select mailbox source-list
	result)
    (unwind-protect
	(prog1
	    (save-excursion		; = save-current-buffer?
	      ;;----------------------------
	      (vm-buffer-type:enter 'process)
	      ;;----------------------------
	      (catch 'end-of-session
		(if handler
		    (throw 'end-of-session
			   (funcall handler 'vm-imap-check-mail source)))
		(setq process 
		      (vm-imap-make-session source nil "checkmail"))
		(or process (throw 'end-of-session nil))
		(set-buffer (process-buffer process))
		(setq source-list (vm-parse source "\\([^:]+\\):?")
		      mailbox (nth 3 source-list))
		(setq select (vm-imap-select-mailbox process mailbox)
		      msg-count (car select)
		      uid-validity (nth 1 select))
		(if (zerop msg-count)
		    (progn
		      (vm-store-folder-totals source '(0 0 0 0))
		      (throw 'end-of-session nil)))
		;; sweep through the retrieval list, removing entries
		;; that have been invalidated by the new UIDVALIDITY
		;; value.
		(setq retrieved
		  (vm-imap-clear-invalid-retrieval-entries imapdrop
							   retrieved
							   uid-validity))
		(setq response (vm-imap-get-uid-list process 1 msg-count))
		(if (null response)
		    nil
		  (if (null (car response))
		      ;; (nil . nil) is returned if there are no
		      ;; messages in the mailbox.
		      (progn
			(vm-store-folder-totals source '(0 0 0 0))
			(throw 'end-of-session nil))
		    (while response
		      (if (not (and (setq x (assoc (cdr (car response))
						   retrieved))
				    (equal (nth 1 x) imapdrop)
				    (eq (nth 2 x) 'uid)))
			  (vm-increment count))
		      (setq response (cdr response))))
		  (vm-store-folder-totals source (list count 0 0 0))
		  (throw 'end-of-session (not (eq count 0))))
		(not (equal 0 (car select)))))

	  (setq vm-imap-retrieved-messages retrieved))

      ;; unwind-protections
      (when process 
	(vm-imap-end-session process)
	;; (vm-imap-dump-uid-and-flags-data)
	;;-------------------
	(vm-buffer-type:exit)
	;;-------------------
	))))

(defun vm-expunge-imap-messages ()
  "Deletes all messages from IMAP mailbox that have already been retrieved
into the current folder.  VM sets the \\Deleted flag on all such messages
on all the relevant IMAP servers and then immediately expunges."
  (interactive)
  (vm-follow-summary-cursor)
  (vm-select-folder-buffer-and-validate 0 (interactive-p))
  (vm-error-if-virtual-folder)
  (let ((process nil)
	(source nil)
	(trouble nil)
	(delete-count 0)
	(vm-global-block-new-mail t)
	(vm-imap-ok-to-ask t)
	(did-delete nil)
	msg-count can-delete read-write uid-validity
	select-response source-list folder uid-alist mailbox data mp match)
    (unwind-protect
	(save-excursion
	  ;;------------------------
	  (vm-buffer-type:duplicate)
	  ;;------------------------
	  (setq vm-imap-retrieved-messages
		(sort vm-imap-retrieved-messages
		      (function (lambda (a b)
				  (cond ((string-lessp (nth 2 a) (nth 2 b)) t)
					((string-lessp (nth 2 b)
						       (nth 2 a))
					 nil)
					((string-lessp (nth 1 a) (nth 1 b)) t)
					((string-lessp (nth 1 b) (nth 1 a))
					 nil)
					((string-lessp (nth 0 a) (nth 0 b)) t)
					(t nil))))))
	  (setq mp vm-imap-retrieved-messages)
	  (while mp
	    (catch 'replay
	      (condition-case error-data
		  (progn
		    (setq data (car mp))
		    (when (not (equal source (nth 2 data)))
		      (when process
			(when did-delete
			  (vm-imap-send-command process "CLOSE")
			  (vm-imap-read-ok-response process)
			  ;;----------------------------------
			  (vm-imap-session-type:set 'inactive)
			  ;; (vm-imap-dump-uid-and-flags-data)
			  ;;----------------------------------
			  )
			(vm-imap-end-session process)
				
			(setq process nil
			      did-delete nil))
		      (setq source (nth 2 data))
		      (setq folder (or (vm-imap-folder-for-spec source)
				       (vm-safe-imapdrop-string source)))
		      (condition-case error-data
			  (progn
			    (message "Opening IMAP session to %s..."
				     folder)
			    (setq process 
				  (vm-imap-make-session 
				   source vm-imap-ok-to-ask "expunge"))
			    (if (null process)
				(signal 'vm-imap-protocol-error nil))
			    ;;--------------------------
			    (vm-buffer-type:set 'process)
			    ;;--------------------------
			    (set-buffer (process-buffer process))
			    (setq source-list (vm-parse source
							"\\([^:]+\\):?")
				  mailbox (nth 3 source-list)
				  select-response (vm-imap-select-mailbox
						   process mailbox)
				  msg-count (car select-response)
				  uid-validity (nth 1 select-response)
				  read-write (nth 2 select-response)
				  can-delete (nth 3 select-response))
			    (setq mp
				  (vm-imap-clear-invalid-retrieval-entries
				   source
				   mp
				   uid-validity))
			    (unless (eq data (car mp))
				;; this entry must have been
				;; discarded as invalid, so
				;; skip it and process the
				;; entry that is now at the
				;; head of the list.
				(throw 'replay t))
			    (unless can-delete
			      (error "Can't delete messages in mailbox %s, skipping..." mailbox))
			    (unless read-write
			      (error "Mailbox %s is read-only, skipping..." mailbox))
			    (message "Expunging messages in %s..." folder))
			(error
			 (if (cdr error-data)
			     (apply 'message (cdr error-data))
			   (message
			    "Couldn't open IMAP session to %s, skipping..."
			    folder))
			 (setq trouble (cons folder trouble))
			 (sleep-for 2)
			 (while (equal (nth 1 (car mp)) source)
			   (setq mp (cdr mp)))
			 (throw 'replay t)))
		      (when (zerop msg-count)
			(while (equal (nth 1 (car mp)) source)
			  (setq mp (cdr mp)))
			(throw 'replay t))
		      (setq uid-alist
			    (vm-imap-get-uid-list
			     process 1 msg-count)))
		    (when (setq match (rassoc (car data) uid-alist))
		      (vm-imap-delete-message process (car match))
		      (setq did-delete t)
		      (vm-increment delete-count)))
		(error
		 (setq trouble (cons folder trouble))
		 (message "Something signaled: %s"
			  (prin1-to-string error-data))
		 (sleep-for 2)
		 (message "Skipping rest of mailbox %s..." folder)
		 (sleep-for 2)
		 (while (equal (nth 2 (car mp)) source)
		   (setq mp (cdr mp)))
		 (throw 'replay t)))
	      (setq mp (cdr mp))))
	  (when did-delete
	    (vm-imap-send-command process "CLOSE")
	    (vm-imap-read-ok-response process)
	    ;;----------------------------------
	    (vm-imap-session-type:set 'inactive)
	    ;; (vm-imap-dump-uid-and-flags-data)
	    ;;----------------------------------
	    )
	  (if trouble
	      (progn
		;;--------------------------
		(vm-buffer-type:set 'scratch)
		;;--------------------------
		(set-buffer (get-buffer-create "*IMAP Expunge Trouble*"))
		(setq buffer-read-only nil)
		(erase-buffer)
		(insert (format "%s IMAP message%s expunged.\n\n"
				(if (zerop delete-count) "No" delete-count)
				(if (= delete-count 1) "" "s")))
		(insert "VM had problems expunging messages from:\n")
		(nreverse trouble)
		(setq mp trouble)
		(while mp
		  (insert "   " (car mp) "\n")
		  (setq mp (cdr mp)))
		(setq buffer-read-only t)
		(display-buffer (current-buffer)))
	    (message "%s IMAP message%s expunged."
		     (if (zerop delete-count) "No" delete-count)
		     (if (= delete-count 1) "" "s")))
	  ;;-------------------
	  (vm-buffer-type:exit)
	  ;;-------------------
	  )
      (and process (vm-imap-end-session process)))
    (or trouble (setq vm-imap-retrieved-messages nil))))

(defun vm-imap-clear-invalid-retrieval-entries (source retrieved uid-validity)
  "Remove from RETRIEVED (a copy of vm-imap-retrieved-messages)
all the entries for the password-free maildrop spec SOURCE which
do not match the given UID-VALIDITY."
  (let ((ret-list retrieved)
	(prev nil))
    (while ret-list
      (if (and (equal source (nth 2 (car ret-list)))
	       (not (equal (nth 1 (car ret-list)) uid-validity)))
	  (if prev
	      (setcdr prev (cdr ret-list))
	    (setq retrieved (cdr retrieved))))
      (setq prev ret-list)
      (setq ret-list (cdr ret-list)))
    retrieved ))

(defun vm-imap-recorded-uid-validity ()
  "Return the UID-VALIDITY value recorded in the X-IMAP-Retrieved header
of the current folder."
  (let ((pos (vm-find vm-imap-retrieved-messages
			 (lambda (record) (nth 1 record)))))
    (nth 1 (nth pos vm-imap-retrieved-messages))))



;; --------------------------------------------------------------------
;; Server-side
;;
;; vm-establish-new-folder-imap-session: 
;;	(&optional interactive string) -> process
;; vm-re-establish-folder-imap-session: 
;;	(&optional interactive string) -> process
;; vm-establish-writable-imap-session: 
;;	(maildrop &optional interactive string) -> process
;;
;; -- Functions to handle the interaction with the IMAP server
;;
;; vm-imap-make-session: folder -> process
;; vm-imap-end-session: (process &optional buffer) -> void
;; vm-imap-check-connection: process -> void
;;
;; -- mailbox operations
;; vm-imap-mailbox-list: (process & bool) -> string list
;; vm-imap-create-mailbox: (process & string &optional bool) -> void
;; vm-imap-delete-mailbox: (process & string) -> void
;; vm-imap-rename-mailbox: (process & string & string) -> void
;; 
;; -- lower level I/O
;; vm-imap-send-command: (process command &optional tag no-tag) ->
;; 				void
;; vm-imap-select-mailbox: (process & mailbox &optional bool) -> 
;;				(int uid-validity bool bool (flag list))
;; vm-imap-read-capability-response: process -> ?
;; vm-imap-read-greeting: process -> ?
;; vm-imap-read-ok-response: process -> ?
;; vm-imap-read-response: process -> server-resonse
;; vm-imap-read-response-and-verify: process -> server-resopnse
;; vm-imap-read-boolean-response: process -> ?
;; vm-imap-read-object: (process &optinal bool) -> ?
;; vm-imap-response-matches: (string &rest symbol) -> bool
;; vm-imap-response-bail-if-server-says-farewell: 
;;			response -> void + 'end-of-session exception
;; vm-imap-protocol-error: *&rest
;;
;; -- message opeations
;; vm-imap-retrieve-uid-and-flags-data: () -> void
;; vm-imap-dump-uid-and-flags-data: () -> void
;; vm-imap-dump-uid-seq-num-data: () -> void
;; vm-imap-get-uid-list: (process & int & int) -> (int . uid) list
;; vm-imap-get-message-data-list: (process & int & int) ->
;;					(int . uid . string list) list
;; vm-imap-get-message-data: (process & vm-message) -> 
;;					(int . uid . string list)
;; vm-imap-save-message-flags: (process & int &optional bool) -> void
;; vm-imap-get-message-size: (process & int) -> int
;; vm-imap-get-uid-message-size: (process & uid) -> int
;; vm-imap-save-message: (process & int & string?) -> void
;; vm-imap-delete-message: (process & int) -> void
;;
;; vm-imap-ask-about-large-message: (process int int) -> ?
;; vm-imap-retrieve-to-target: (process target statblob bodypeek) -> bool
;; 
;; -- to be phased out
;; vm-imap-get-message-flags: 
;;	(process & vm-message &optional norecord:bool) -> 
;; --------------------------------------------------------------------


;; The IMAP sessions work as follows:

;; Generally, sessions are created for get-new-mail, save-folder and
;; vm-imap-synchronize operations.  All these operations read the
;; uid-and-flags-data and cache it internally.  At this stage, the
;; IMAP session is said to be "valid", i.e., message numbers stored in
;; the cache are valid.  As long as FETCH and STORE operations are
;; performed, the session remains valid.

;; When other IMAP operations are performed, the server can send
;; EXPUNGE responses and invalidate the cached message sequence
;; numbers.  In this state, the IMAP session is "active", but not
;; "valid".  Only UID-based commands can be issued in this state.

;; Create a process for a new IMAP session to the account SOURCE and
;; return it.

;;;###autoload
(defun vm-imap-make-session (source &optional interactive purpose)
  "Create a new IMAP session for the IMAP mail box SOURCE.
Optional argument INTERACTIVE says the operation has been invoked
interactively, and the optional argument PURPOSE is inserted in
the process buffer for tracing purposes.  Returns the process or
nil if the session could not be created."
  (let ((shutdown nil)			; whether process is to be shutdown
	(folder-type vm-folder-type)
	process ooo
	(folder (or (vm-imap-folder-for-spec source)
		    (vm-safe-imapdrop-string source)))
	(coding-system-for-read (vm-binary-coding-system))
	(coding-system-for-write (vm-binary-coding-system))
	(use-ssl nil)
	(use-ssh nil)
	(session-name "IMAP")
	(process-connection-type nil)
	greeting
	host port mailbox auth user pass authinfo
	source-list imap-buffer
	source-nopwd-nombox)
    (vm-imap-log-token 'make)
    (unwind-protect
	(catch 'end-of-session
	  ;; parse the maildrop
	  (setq source-list (vm-parse source "\\([^:]*\\):?" 1 7)
		host (nth 1 source-list)
		port (nth 2 source-list)
;;		mailbox (nth 3 source-list)
		auth (nth 4 source-list)
		user (nth 5 source-list)
		pass (nth 6 source-list)
		source-nopwd-nombox
		(vm-imapdrop-sans-personal-info source))
	  (cond ((equal auth "preauth") t)
		((equal "imap-ssl" (car source-list))
		 (setq use-ssl t
		       session-name "IMAP over SSL"))
		((equal "imap-ssh" (car source-list))
		 (setq use-ssh t
		       session-name "IMAP over SSH")))
	  (vm-imap-check-for-server-spec source host port auth user pass
					 use-ssl use-ssh)
	  (setq port (string-to-number port))
	  (when (and (equal pass "*") (not (equal auth "preauth")))
	    (setq pass
		  (car (cdr (assoc source-nopwd-nombox vm-imap-passwords))))
	    (when (and (null pass)
		       (boundp 'auth-sources)
		       (fboundp 'auth-source-user-or-password))
	      (cond ((and (setq authinfo
				(auth-source-user-or-password
				 '("login" "password")
				 (vm-imap-account-name-for-spec source)
				 port))
			  (equal user (car authinfo)))
		     (setq pass (cadr authinfo)))
		    ((and (setq authinfo
				(auth-source-user-or-password
				 '("login" "password")
				 host port))
			  (equal user (car authinfo)))
		     (setq pass (cadr authinfo)))))
	    (when (and (null pass) interactive)
	      (setq pass
		    (read-passwd (format "IMAP password for %s: " folder))))
	    (when (null pass)
	      (message "Need password for %s" folder)
	      (throw 'end-of-session nil)))
	  ;; save the password for the sake of
	  ;; vm-expunge-imap-messages, which passes password-less
	  ;; imapdrop specifications to vm-make-imap-session.
	  (if (null (assoc source-nopwd-nombox vm-imap-passwords))
	      (setq vm-imap-passwords (cons (list source-nopwd-nombox pass)
					    vm-imap-passwords)))
	  ;; get the trace buffer
	  (setq imap-buffer
		(vm-make-work-buffer 
		 (vm-make-trace-buffer-name session-name host)))
	  (vm-imap-log-token imap-buffer)
	  (save-excursion		; = save-current-buffer?
	    (set-buffer imap-buffer)
	    ;;----------------------------
	    (vm-buffer-type:enter 'process)
	    ;;----------------------------
	    (setq vm-folder-type (or folder-type vm-default-folder-type))
	    (buffer-disable-undo imap-buffer)
	    (make-local-variable 'vm-imap-read-point)
	    ;; clear the trace buffer of old output
	    (erase-buffer)
	    ;; Tell MULE not to mess with the text.
	    (if (fboundp 'set-buffer-file-coding-system)
		(set-buffer-file-coding-system (vm-binary-coding-system) t))
	    (if (equal auth "preauth")
		(setq process
		      (run-hook-with-args-until-success 
		       'vm-imap-session-preauth-hook
		       host port mailbox user pass)))
	    (if (processp process)
		(set-process-buffer process (current-buffer))
	      (insert "Starting " session-name
		      " session " (current-time-string) "\n")
	      (insert (format "-- connecting to %s:%s\n" host port))
	      ;; open the connection to the server
	      (condition-case err
		  (cond 
		   (use-ssl
		    (vm-setup-stunnel-random-data-if-needed)
		    (setq process
			  (apply 'start-process session-name imap-buffer
				 vm-stunnel-program
				 (nconc (vm-stunnel-configuration-args host
								       port)
					vm-stunnel-program-switches))))
		   (use-ssh
		    (setq process (open-network-stream
				   session-name imap-buffer
				   "127.0.0.1"
				   (vm-setup-ssh-tunnel host port))))
		   (t
		    (setq process (open-network-stream session-name
						       imap-buffer
						       host port))))
		(error
		 (message "%s" (error-message-string err))
		 (setq shutdown t)
		 ;;-------------------
		 (vm-buffer-type:exit)
		 ;;-------------------
		 (throw 'end-of-session nil))))
	    (setq vm-imap-read-point (point))
	    (vm-process-kill-without-query process)
	    (if (setq greeting (vm-imap-read-greeting process))
		(insert-before-markers (format "-- connected for %s\n" purpose))
	      (delete-process process) ; why here?  USR
	      (setq shutdown t)
	      (throw 'end-of-session nil))
	    (setq shutdown t)
	    (set (make-local-variable 'vm-imap-session-done) nil)
	    ;; record server capabilities
	    (vm-imap-send-command process "CAPABILITY")
	    (if (null (setq ooo (vm-imap-read-capability-response process)))
		(throw 'end-of-session nil))
	    (set (make-local-variable 'vm-imap-capabilities) (car ooo))
	    (set (make-local-variable 'vm-imap-auth-methods) (nth 1 ooo))
	    ;; authentication
	    (cond 
	     ((equal auth "login")
	      ;; LOGIN must be supported by all imap servers,
	      ;; no need to check for it in CAPABILITIES.
	      (vm-imap-send-command 
	       process
	       (format "LOGIN %s %s" 
		       (vm-imap-quote-string user) (vm-imap-quote-string pass)))
	      (if (null (vm-imap-read-ok-response process))
		  (progn
		    (setq vm-imap-passwords
			  (delete (list source-nopwd-nombox pass)
				  vm-imap-passwords))
		    (message "IMAP password for %s incorrect" folder)
		    ;; don't sleep unless we're running synchronously.
		    (if vm-imap-ok-to-ask
			(sleep-for 2))
		    (throw 'end-of-session nil))
		;;--------------------------------
		(vm-imap-session-type:set 'active)
		;;--------------------------------
		))
	     ((equal auth "cram-md5")
	      (if (not (vm-imap-auth-method 'CRAM-MD5))
		  (error "CRAM-MD5 authentication unsupported by this server"))
	      (let ((ipad (make-string 64 54))
		    (opad (make-string 64 92))
		    (command "AUTHENTICATE CRAM-MD5")
		    (secret (concat
			     pass
			     (make-string (max 0 (- 64 (length pass))) 0)))
		    response p challenge answer)
		(vm-imap-send-command process command)
		(setq response 
		      (vm-imap-read-response-and-verify process command))
		(cond ((vm-imap-response-matches response '+ 'atom)
		       (setq p (cdr (nth 1 response))
			     challenge (buffer-substring (nth 0 p) (nth 1 p))
			     challenge (vm-mime-base64-decode-string
					challenge)))
		      (t
		       (vm-imap-protocol-error
			"Don't understand AUTHENTICATE response")))
		(setq answer
		      (concat
		       user " "
		       (vm-md5-string
			(concat
			 (vm-xor-string secret opad)
			 (vm-md5-raw-string 
			  (concat
			   (vm-xor-string secret ipad) challenge)))))
		      answer (vm-mime-base64-encode-string answer))
		(vm-imap-send-command process answer nil t)
		(if (null (vm-imap-read-ok-response process))
		    (progn
		      (setq vm-imap-passwords
			    (delete (list source-nopwd-nombox pass)
				    vm-imap-passwords))
		      (message "IMAP password for %s incorrect" folder)
		      ;; don't sleep unless we're running synchronously.
		      (if vm-imap-ok-to-ask
			  (sleep-for 2))
		      (throw 'end-of-session nil))
		  ;;-------------------------------
		  (vm-imap-session-type:set 'active)
		  ;;-------------------------------
		  )))
	     ((equal auth "preauth")
	      (if (not (eq greeting 'preauth))
		  (progn
		    (message "IMAP session was not pre-authenticated")
		    ;; don't sleep unless we're running synchronously.
		    (if vm-imap-ok-to-ask
			(sleep-for 2))
		    (throw 'end-of-session nil))
		;;-------------------------------
		(vm-imap-session-type:set 'active)
		;;-------------------------------
		))
	     (t (error "Don't know how to authenticate using %s" auth)))
	    (setq shutdown nil)
	    ;;-------------------
	    (vm-buffer-type:exit)
	    ;;-------------------
	    process ))
      ;; unwind-protection
      (if shutdown
	  (vm-imap-end-session process imap-buffer))
      (vm-tear-down-stunnel-random-data))))

(defun vm-imap-check-for-server-spec (source host port auth user pass 
					     use-ssl use-ssh)
  (when (null host)
    (error "No host in IMAP maildrop specification, \"%s\"" source))
  (when (or (null port) (not (string-match "^[0-9]+$" port)))
    (error "No port in IMAP maildrop specification, \"%s\"" source))
  (when (null auth)
    (error "No authentication method in IMAP maildrop specification, \"%s\"" 
	   source))
  (when (null user)
    (error "No user in IMAP maildrop specification, \"%s\"" source))
  (when (null pass)
    (error "No password in IMAP maildrop specification, \"%s\"" source))
  (when use-ssl
    (if (null vm-stunnel-program)
	(error "vm-stunnel-program must be non-nil to use IMAP over SSL.")))
  (when use-ssh
    (if (null vm-ssh-program)
	(error "vm-ssh-program must be non-nil to use IMAP over SSH.")))
  )



;;;###autoload
(defun vm-imap-end-session (process &optional imap-buffer keep-buffer)
  "Kill the IMAP session represented by PROCESS.  PROCESS could
be nil or be already closed. Optional argument IMAP-BUFFER specifies
the process-buffer. If the optional argument KEEP-BUFFER is
non-nil, the process buffer is retained, otherwise it is killed
as well."
  (vm-imap-log-token 'end-session)
  (when (and process (null imap-buffer))
    (setq imap-buffer (process-buffer process)))
  (when (and process (memq (process-status process) '(open run))
	   (buffer-live-p (process-buffer process)))
      (save-excursion			; = save-current-buffer?
	(set-buffer imap-buffer)
	;;----------------------------
	(vm-buffer-type:enter 'process)
	;;----------------------------
	;; vm-imap-end-session might have already been called on
	;; this process, so don't logout and schedule the killing
	;; the process again if it's already been done.
	(unwind-protect
	    (condition-case nil
		(if vm-imap-session-done
		    ;;-------------------------------------
		    (vm-imap-session-type:assert 'inactive)
		  ;;-------------------------------------
		  (vm-imap-send-command process "LOGOUT")
		  ;; we don't care about the response.
		  ;; try reading it anyway and trap any errors.
		  (vm-imap-read-ok-response process))
	      (vm-imap-protocol-error ; handler
	       nil)		      ; ignore errors 
	      (error nil))	      ; handler
	  (setq vm-imap-session-done t)
	  ;;----------------------------------
	  (vm-imap-session-type:set 'inactive)
	  ;;----------------------------------
	  ;; This is just for tracing purposes
	  (goto-char (point-max))
	  (insert "ending IMAP session " (current-time-string) "\n")
	  ;; Schedule killing of the process after a delay to allow
	  ;; any output to be received first
	  (if (fboundp 'add-async-timeout)
	      (add-async-timeout 2 'delete-process process)
	    (run-at-time 2 nil 'delete-process process)))
	;;----------------------------------
	(vm-buffer-type:exit)
	;;----------------------------------
	))
  (when (and imap-buffer (buffer-live-p imap-buffer))
      (if (and (not vm-imap-keep-trace-buffer) (not keep-buffer))
	  (kill-buffer imap-buffer)
	(with-current-buffer imap-buffer
	  ;;----------------------------
	  (vm-buffer-type:enter 'process)
	  ;;----------------------------
	  (rename-buffer (concat "saved " (buffer-name)) t)
	  (vm-keep-some-buffers (current-buffer) 'vm-kept-imap-buffers
				vm-imap-keep-failed-trace-buffers)
	  ;;-------------------
	  (vm-buffer-type:exit)
	  ;;-------------------
	  )))     
  )

;; Status indicator vector
;; timer
(defun vm-imap-stat-timer (o) (aref o 0))
;; whether the current status has been reported already
(defun vm-imap-stat-did-report (o) (aref o 1))
;; mailbox specification
(defun vm-imap-stat-x-mailbox (o) (aref o 2))
;; message number (count) of the message currently being retrieved
(defun vm-imap-stat-x-currmsg (o) (aref o 3))
;; total number of mesasges that need to be retrieved in this round
(defun vm-imap-stat-x-maxmsg (o) (aref o 4))
;; amount of the current message that has been retrieved
(defun vm-imap-stat-x-got (o) (aref o 5))
;; size of the current message
(defun vm-imap-stat-x-need (o) (aref o 6))
;; Data for the message last reported
(defun vm-imap-stat-y-mailbox (o) (aref o 7))
(defun vm-imap-stat-y-currmsg (o) (aref o 8))
(defun vm-imap-stat-y-maxmsg (o) (aref o 9))
(defun vm-imap-stat-y-got (o) (aref o 10))
(defun vm-imap-stat-y-need (o) (aref o 11))

(defun vm-set-imap-stat-timer (o val) (aset o 0 val))
(defun vm-set-imap-stat-did-report (o val) (aset o 1 val))
(defun vm-set-imap-stat-x-mailbox (o val) (aset o 2 val))
(defun vm-set-imap-stat-x-currmsg (o val) (aset o 3 val))
(defun vm-set-imap-stat-x-maxmsg (o val) (aset o 4 val))
(defun vm-set-imap-stat-x-got (o val) (aset o 5 val))
(defun vm-set-imap-stat-x-need (o val) (aset o 6 val))
(defun vm-set-imap-stat-y-mailbox (o val) (aset o 7 val))
(defun vm-set-imap-stat-y-currmsg (o val) (aset o 8 val))
(defun vm-set-imap-stat-y-maxmsg (o val) (aset o 9 val))
(defun vm-set-imap-stat-y-got (o val) (aset o 10 val))
(defun vm-set-imap-stat-y-need (o val) (aset o 11 val))

(defun vm-imap-start-status-timer ()
  (let ((blob (make-vector 12 nil))
	timer)
    (setq timer (add-timeout 2 'vm-imap-report-retrieval-status blob 2))
    (vm-set-imap-stat-timer blob timer)
    blob ))

(defun vm-imap-stop-status-timer (status-blob)
  (if (vm-imap-stat-did-report status-blob)
      (message ""))
  (if (fboundp 'disable-timeout)
      (disable-timeout (vm-imap-stat-timer status-blob))
    (cancel-timer (vm-imap-stat-timer status-blob))))

(defun vm-imap-report-retrieval-status (o)
  (vm-set-imap-stat-did-report o t)
  (cond ((null (vm-imap-stat-x-got o)) t)
	;; should not be possible, but better safe...
	((not (eq (vm-imap-stat-x-mailbox o) (vm-imap-stat-y-mailbox o))) t)
	((not (eq (vm-imap-stat-x-currmsg o) (vm-imap-stat-y-currmsg o))) t)
	(t 
	 (message "Retrieving message %d (of %d) from %s, %s..."
		  (vm-imap-stat-x-currmsg o)
		  (vm-imap-stat-x-maxmsg o)
		  (vm-imap-stat-x-mailbox o)
		  (if (vm-imap-stat-x-need o)
		      (format "%d%%%s"
			      (/ (* 100 (vm-imap-stat-x-got o))
				 (vm-imap-stat-x-need o))
			      (if (eq (vm-imap-stat-x-got o)
				      (vm-imap-stat-y-got o))
				  " (stalled)"
				""))
		    "post processing"))))
  (vm-set-imap-stat-y-mailbox o (vm-imap-stat-x-mailbox o))
  (vm-set-imap-stat-y-currmsg o (vm-imap-stat-x-currmsg o))
  (vm-set-imap-stat-y-maxmsg o (vm-imap-stat-x-maxmsg o))
  (vm-set-imap-stat-y-got o (vm-imap-stat-x-got o))
  (vm-set-imap-stat-y-need o (vm-imap-stat-x-need o)))

(defun vm-imap-check-connection (process)
  (cond ((not (memq (process-status process) '(open run)))
	 (vm-imap-normal-error "not connected"))
	((not (buffer-live-p (process-buffer process)))
	 (vm-imap-protocol-error
	  "IMAP process %s's buffer has been killed" process))))

(defun vm-imap-send-command (process command &optional tag no-tag)
  (vm-imap-log-token 'send)
  ;;------------------------------
  (vm-buffer-type:assert 'process)
  ;;------------------------------
  (vm-imap-check-connection process)
  (if (not (= (point) (point-max)))
      (vm-imap-log-tokens (list 'send1 (point) (point-max))))
  (goto-char (point-max))
;;   try if it makes a difference to get pending output here, use timeout
;;   (accept-process-output process 0 0.01)
;;   (if (not (= (point) (point-max)))
;;       (vm-imap-log-tokens (list 'send2 (point) (point-max))))
;;   (goto-char (point-max))

  (or no-tag (insert-before-markers (or tag "VM") " "))
  (let ((case-fold-search t))
    (if (string-match "^LOGIN" command)
	(insert-before-markers "LOGIN <parameters omitted>\r\n")
      (insert-before-markers command "\r\n")))
  (setq vm-imap-read-point (point))
  ;; previously we had a process-send-string call for each string
  ;; to avoid extra consing but that caused a lot of packet overhead.
  (if no-tag
      (process-send-string process (format "%s\r\n" command))
    (process-send-string process (format "%s %s\r\n" (or tag "VM") command))))

(defun vm-imap-select-mailbox (process mailbox &optional just-examine)
  ;; I/O function to select an IMAP mailbox
  ;;   PROCESS - the IMAP process
  ;;   MAILBOX - the name fo the mailbox to be selected
  ;;   JUST-EXAMINE - select the mailbox in a read-only (examine) mode
  ;; Returns a list containing:
  ;;   int msg-count - number of messages in the mailbox
  ;;   string uid-validity - the UID validity value of the mailbox
  ;;   bool read-write - whether the mailbox is writable
  ;;   bool can-delete - whether the mailbox allows message deletion
  ;;   server-response permanent-flags - permanent flags used in the mailbox

  ;;------------------------------
  (vm-buffer-type:assert 'process)
  ;;------------------------------

  (let ((imap-buffer (current-buffer))
	(command (if just-examine "EXAMINE" "SELECT"))
	tok response p
	(flags nil)
	(permanent-flags nil)
	(msg-count nil)
	(uid-validity nil)
	(read-write (not just-examine))
	(can-delete t)
	(need-ok t))
    (vm-imap-log-token 'select-mailbox)
    (vm-imap-send-command 
     process (format "%s %s" command (vm-imap-quote-string mailbox)))
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process command))
      (cond ((vm-imap-response-matches response '* 'OK 'vector)
	     (setq p (cdr (nth 2 response)))
	     (cond ((vm-imap-response-matches p 'UIDVALIDITY 'atom)
		    (setq tok (nth 1 p))
		    (setq uid-validity (buffer-substring (nth 1 tok)
							 (nth 2 tok))))
		   ((vm-imap-response-matches p 'PERMANENTFLAGS 'list)
		    (setq permanent-flags (nth 1 p)))))
	    ((vm-imap-response-matches response '* 'FLAGS 'list)
	     (setq flags (nth 2 response)))
	    ((vm-imap-response-matches response '* 'atom 'EXISTS)
	     (setq tok (nth 1 response))
	     (goto-char (nth 1 tok))
	     (setq msg-count (read imap-buffer)))
	    ((vm-imap-response-matches response 'VM 'OK '(vector READ-WRITE))
	     (setq need-ok nil read-write t))
	    ((vm-imap-response-matches response 'VM 'OK '(vector READ-ONLY))
	     (setq need-ok nil read-write nil))
	    ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil))))
    (if (null flags)
	(vm-imap-protocol-error "FLAGS missing from SELECT responses"))
    (if (null msg-count)
	(vm-imap-protocol-error "EXISTS missing from SELECT responses"))
    (if (null uid-validity)
	(vm-imap-protocol-error "UIDVALIDITY missing from SELECT responses"))
    (setq can-delete (vm-imap-scan-list-for-flag flags "\\Deleted"))
    (if (vm-imap-scan-list-for-flag permanent-flags "\\*")
	(if (vm-imap-scan-list-for-flag flags "\\Seen")
	    nil
	  (message "Warning: No permanent changes permitted for the mailbox"))
      (message "Warning: Only basic message flags available for the mailbox")
      )
    ;;-------------------------------
    (vm-imap-session-type:set 'active)
    ;;-------------------------------
    (list msg-count uid-validity read-write can-delete permanent-flags)))

(defun vm-imap-read-expunge-response (process)
  (let ((list nil)
	(imap-buffer (current-buffer))
	(need-ok t)
	tok msg-num response
	)
    (vm-imap-log-token 'read-expunge)
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "EXPUNGE"))
      (cond ((vm-imap-response-matches response '* 'atom 'EXPUNGE)
	     (setq tok (nth 1 response))
	     (goto-char (nth 1 tok))
	     (setq msg-num (read imap-buffer))
	     (setq list (cons msg-num list)))
	    ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil))))
    ;;--------------------------------
    (vm-imap-session-type:set 'active)		; seq nums are now invalid
    ;;--------------------------------
    (nreverse list)))

(defun vm-imap-get-uid-list (process first last)
  ;; I/O function to read the uid's of a message range
  ;;   PROCESS - the IMAP process
  ;;   FIRST - message sequence number of the first message in the range
  ;;   LAST - message sequene number of the last message in the range
  ;; Returns an assoc list with pairs 
  ;;   int msg-num - message sequence number of a message
  ;;   string uid - uid of the message
  ;; or nil indicating failure
  ;; If there are no messages in the range then (nil) is returned

  (let ((list nil)
	(imap-buffer (current-buffer))
	tok msg-num uid response p
	(need-ok t))
    (vm-imap-log-token 'uid-list)
    ;;----------------------------------
    (vm-imap-session-type:assert-active)
    ;;----------------------------------
    (vm-imap-send-command process (format "FETCH %s:%s (UID)" first last))
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "UID FETCH"))
      (cond ((vm-imap-response-matches response '* 'atom 'FETCH 'list)
	     (setq p (cdr (nth 3 response)))
	     (if (not (vm-imap-response-matches p 'UID 'atom))
		 (vm-imap-protocol-error
		  "expected (UID number) in FETCH response"))
	     (setq tok (nth 1 response))
	     (goto-char (nth 1 tok))
	     (setq msg-num (read imap-buffer))
	     (setq tok (nth 1 p))
	     (setq uid (buffer-substring (nth 1 tok) (nth 2 tok))
		   list (cons (cons msg-num uid) list)))
	    ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil))))
      ;; returning nil means the uid fetch failed so return
      ;; something other than nil if there aren't any messages.
      (if (null list)
	  (cons nil nil)
	list )))

;; This function is not recommended, but is available to use when
;; caching uid-and-flags data might be too expensive.

(defun vm-imap-get-message-data (process m uid-validity)
  ;; I/O function to read the flags of a message
  ;;   PROCESS  - The IMAP process
  ;;   M - a vm-message
  ;;   uid-validity -  the folder's uid-validity
  ;; Returns (msg-num: int . uid: string . size: string . flags: string list)
  ;; Throws vm-imap-protocol-error for failure.
  (let ((imap-buffer (current-buffer))
	response tok need-ok msg-num list)
    (if (not (equal (vm-imap-uid-validity-of m) uid-validity))
	(vm-imap-normal-error "message has invalid uid"))
    (vm-imap-log-tokens (list 'message-data (current-buffer)))
    ;;----------------------------------
    (vm-imap-session-type:assert 'valid)
    ;;----------------------------------
    (vm-imap-send-command
     process (format "SEARCH UID %s" (vm-imap-uid-of m)))
    (setq need-ok t)
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "UID"))
      (cond ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil))
	    ((vm-imap-response-matches response '* 'SEARCH 'atom)
	     (if (null (setq tok (nth 2 response)))
		 (vm-imap-normal-error "message not found on server"))
	     (goto-char (nth 1 tok))
	     (setq msg-num (read imap-buffer))
	     )))
    (setq list (vm-imap-get-message-data-list process msg-num msg-num))
    (car list)))
	

(defun vm-imap-get-message-data-list (process first last)
  ;; I/O function to read the flags of a message range
  ;;   PROCESS - the IMAP process
  ;;   FIRST - message sequence number of the first message in the range
  ;;   LAST - message sequene number of the last message in the range
  ;; Returns an assoc list with entries
  ;;   int msg-num - message sequence number of a message
  ;;   string uid - uid of the message
  ;;   string size - message size
  ;;   (string list) flags - list of flags for the message
  ;; throws vm-imap-protocol-error for failure.

  (let ((list nil)
	(imap-buffer (current-buffer))
	tok msg-num uid size flag flags response p pl
	(need-ok t))
    (vm-imap-log-token (list 'message-data-list (current-buffer)))
    ;;----------------------------------
    (if vm-buffer-type-debug
	(setq vm-buffer-type-trail (cons 'message-data vm-buffer-type-trail)))
    (vm-buffer-type:assert 'process)
    (vm-imap-session-type:assert-active)
    ;;----------------------------------
    (vm-imap-send-command 
     process (format "FETCH %s:%s (UID RFC822.SIZE FLAGS)" first last))
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "FLAGS FETCH"))
      (cond 
       ((vm-imap-response-matches response '* 'atom 'FETCH 'list)
	(setq p (cdr (nth 3 response)))
	(setq tok (nth 1 response))
	(goto-char (nth 1 tok))
	(setq msg-num (read imap-buffer))
	(while p
	  (cond 
	   ((vm-imap-response-matches p 'UID 'atom)
	    (setq tok (nth 1 p))
	    (setq uid (buffer-substring (nth 1 tok) (nth 2 tok)))
	    (setq p (nthcdr 2 p)))
	   ((vm-imap-response-matches p 'RFC822\.SIZE 'atom)
	    (setq tok (nth 1 p))
	    (setq size (buffer-substring (nth 1 tok) (nth 2 tok)))
	    (setq p (nthcdr 2 p)))
	   ((vm-imap-response-matches p  'FLAGS 'list)
	    (setq pl (cdr (nth 1 p))
		  flags nil)
	    (while pl
	      (setq tok (car pl))
	      (if (not (vm-imap-response-matches (list tok) 'atom))
		  (vm-imap-protocol-error
		   "expected atom in FLAGS list in FETCH response"))
	      (setq flag (downcase
			  (buffer-substring (nth 1 tok) (nth 2 tok)))
		    flags (cons flag flags)
		    pl (cdr pl)))
	    (setq p (nthcdr 2 p)))
	   (t
	    (vm-imap-protocol-error
	     "expected UID, RFC822.SIZE and (FLAGS list) in FETCH response"))
	   ))
	(setq list 
	      (cons (cons msg-num (cons uid (cons size flags)))
		    list)))
       ((vm-imap-response-matches response 'VM 'OK)
	(setq need-ok nil))))
    list))

(defun vm-imap-ask-about-large-message (process size n)
  (let ((work-buffer nil)
	(imap-buffer (current-buffer))
	(need-ok t)
	(need-header t)
	response fetch-response
	list p
	start end)
    (unwind-protect
	(save-excursion
	  ;;------------------------
	  (vm-buffer-type:duplicate)
	  ;;------------------------
	  (save-window-excursion
	    ;;----------------------------------
	    (vm-imap-session-type:assert 'valid)
	    ;;----------------------------------
	    (vm-imap-send-command process
				  (format "FETCH %d (RFC822.HEADER)" n))
	    (while need-ok
	      (setq response 
		    (vm-imap-read-response-and-verify process "header FETCH"))
	      (cond ((vm-imap-response-matches response '* 'atom 'FETCH 'list)
		     (setq fetch-response response
			   need-header nil))
		    ((vm-imap-response-matches response 'VM 'OK)
		     (setq need-ok nil))))
	    (if need-header
		(vm-imap-protocol-error "FETCH OK sent before FETCH response"))
	    (setq vm-imap-read-point (point-marker))
	    (setq list (cdr (nth 3 fetch-response)))
	    (if (not (vm-imap-response-matches list 'RFC822\.HEADER 'string))
		(vm-imap-protocol-error
		 "expected (RFC822.HEADER string) in FETCH response"))
	    (setq p (nth 1 list)
		  start (nth 1 p)
		  end (nth 2 p))
	    (setq work-buffer (generate-new-buffer "*imap-glop*"))
	    ;;--------------------------
	    (vm-buffer-type:set 'scratch)
	    ;;--------------------------
	    (set-buffer work-buffer)
	    (insert-buffer-substring imap-buffer start end)
	    (vm-imap-cleanup-region (point-min) (point-max))
	    (vm-display-buffer work-buffer)
	    (setq minibuffer-scroll-window (selected-window))
	    (goto-char (point-min))
	    (if (re-search-forward "^Received:" nil t)
		(progn
		  (goto-char (match-beginning 0))
		  (vm-reorder-message-headers
		   nil vm-visible-headers
		   vm-invisible-header-regexp)))
	    (set-window-point (selected-window) (point))
	    ;;-------------------
	    (vm-buffer-type:exit)
	    ;;-------------------
	    (if (y-or-n-p (format "Retrieve message %d (size = %d)? " n size))
		'retrieve
	      (if (y-or-n-p (format "Delete message %d from maildrop? " n))
		  'delete
		'skip))))
      (and work-buffer (kill-buffer work-buffer)))))

(defun vm-imap-retrieve-to-target (process target statblob bodypeek)
  ;; Read a mail message from PROCESS and store it in TARGET, which is
  ;; either a file or a buffer.  Report status using STATBLOB.  The
  ;; boolean BODYPEEK tells if the bodypeek function is available for
  ;; the IMAP server.
  (vm-assert (not (null vm-imap-read-point)))
  (vm-imap-log-token 'retrieve)
  (let ((***start vm-imap-read-point)	; avoid dynamic binding of 'start'
	end fetch-response list p)
    (goto-char ***start)
    (vm-set-imap-stat-x-got statblob 0)
    (let* ((func
	    (function
	     (lambda (beg end len)
	       (if vm-imap-read-point
		   (progn
		     (vm-set-imap-stat-x-got statblob (- end ***start))
		     (if (zerop (% (random) 10))
			 (vm-imap-report-retrieval-status statblob)))))))
	   ;; this seems to slow things down.  USR, 2008-04-25
	   ;; reenabled.  USR, 2010-09-17
	   (after-change-functions (cons func after-change-functions))
	   
	   (need-ok t)
	   response)
      (setq response (vm-imap-read-response-and-verify process "message FETCH"))
      (cond ((vm-imap-response-matches response '* 'atom 'FETCH 'list)
	     (setq fetch-response response))
	    (t
	     (vm-imap-normal-error "cannot retrieve message from the server"))))
      
    ;; must make the read point a marker so that it stays fixed
    ;; relative to the text when we modify things below.
    (setq vm-imap-read-point (point-marker))
    (setq list (cdr (nth 3 fetch-response)))
    (cond
     (bodypeek
      (cond ((vm-imap-response-matches list 'BODY '(vector) 'string)
	     (setq p (nth 2 list) 
		   ***start (nth 1 p)))
	    ((vm-imap-response-matches list 'UID 'atom 'BODY '(vector) 'string)
	     (setq p (nth 4 list)
		   ***start (nth 1 p)))
	    (t
	     (vm-imap-protocol-error
	      "expected (BODY[] string) in FETCH response"))))
     (t
      (if (not (vm-imap-response-matches list 'RFC822 'string))
	  (vm-imap-protocol-error
	   "expected (RFC822 string) in FETCH response"))
      (setq p (nth 1 list)
	    ***start (nth 1 p))))
    (goto-char (nth 2 p))
    (setq end (point-marker))
    (vm-set-imap-stat-x-need statblob nil)
    (vm-imap-cleanup-region ***start end)
    (vm-munge-message-separators vm-folder-type ***start end)
    (goto-char ***start)
    (vm-set-imap-stat-x-got statblob nil)
    ;; avoid the consing and stat() call for all but babyl
    ;; files, since this will probably slow things down.
    ;; only babyl files have the folder header, and we
    ;; should only insert it if the crash box is empty.
    (if (and (eq vm-folder-type 'babyl)
	     (cond ((stringp target)
		    (let ((attrs (file-attributes target)))
		      (or (null attrs) (equal 0 (nth 7 attrs)))))
		   ((bufferp target)
		    (with-current-buffer target
		      (zerop (buffer-size))))))
	(let ((opoint (point)))
	  (vm-convert-folder-header nil vm-folder-type)
	  ;; if start is a marker, then it was moved
	  ;; forward by the insertion.  restore it.
	  (setq ***start opoint)
	  (goto-char ***start)
	  (vm-skip-past-folder-header)))
    (insert (vm-leading-message-separator))
    (save-restriction
      (narrow-to-region (point) end)
      (vm-convert-folder-type-headers 'baremessage vm-folder-type))
    (goto-char end)
    ;; Some IMAP servers don't understand Sun's stupid
    ;; From_-with-Content-Length style folder and assume the last
    ;; newline in the message is a separator.  And so the server
    ;; strips it, leaving us with a message that does not end
    ;; with a newline.  Add the newline if needed.
    ;;
    ;; Added From_ folders among the ones to be repaired.  USR, 2010-05-19
    (if (and (not (eq ?\n (char-after (1- (point)))))
	     (memq vm-folder-type 
		   '(From_-with-Content-Length BellFrom_ From_)))
	(insert-before-markers "\n"))
    (insert-before-markers (vm-trailing-message-separator))
    (if (stringp target)
	;; Set file type to binary for DOS/Windows.  I don't know if
	;; this is correct to do or not; it depends on whether the
	;; the CRLF or the LF newline convention is used on the inbox
	;; associated with this crashbox.  This setting assumes the LF
	;; newline convention is used.
	(let ((buffer-file-type t)
	      (selective-display nil))
	  (write-region ***start end target t 0))
      (let ((b (current-buffer)))
	(with-current-buffer target
	  ;;----------------------------
	  (vm-buffer-type:enter 'unknown)
	  ;;----------------------------
	  (let ((buffer-read-only nil))
	    (insert-buffer-substring b ***start end)
	    )
	  ;;-------------------
	  (vm-buffer-type:exit)
	  ;;-------------------
	  )))
    (delete-region ***start end)
    t ))

(defun vm-imap-delete-messages (process beg end)
  ;;----------------------------------
  (vm-buffer-type:assert 'process)
  (vm-imap-session-type:assert 'valid)
  ;;----------------------------------
  (vm-imap-send-command process (format "STORE %d:%d +FLAGS.SILENT (\\Deleted)"
					beg end))
  (if (null (vm-imap-read-ok-response process))
      (vm-imap-normal-error "deletion failed")))

(defun vm-imap-get-message-size (process n)
  (let ((imap-buffer (current-buffer))
	tok size response p
	(need-size t)
	(need-ok t))
    ;;----------------------------------
    (vm-buffer-type:assert 'process)
    (vm-imap-session-type:assert 'valid)
    (vm-imap-log-tokens (list 'message-size (current-buffer)))
    ;;----------------------------------
    (vm-imap-send-command process (format "FETCH %d:%d (RFC822.SIZE)" n n))
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "size FETCH"))
      (cond ((and need-size
		  (vm-imap-response-matches response '* 'atom 'FETCH 'list))
	     (setq need-size nil)
	     (setq p (cdr (nth 3 response)))
	     (if (not (vm-imap-response-matches p 'RFC822\.SIZE 'atom))
		 (vm-imap-protocol-error
		  "expected (RFC822.SIZE number) in FETCH response"))
	     (setq tok (nth 1 p))
	     (goto-char (nth 1 tok))
	     (setq size (read imap-buffer)))
	    ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil))))
    size ))

(defun vm-imap-get-uid-message-size (process uid)
  (let ((imap-buffer (current-buffer))
	tok size response p
	(need-size t)
	(need-ok t))
    ;;----------------------------------
    (vm-buffer-type:assert 'process)
    (vm-imap-session-type:assert-active)
    ;;----------------------------------
    (vm-imap-log-token 'uid-size)
    (vm-imap-send-command 
     process (format "UID FETCH %s:%s (RFC822.SIZE)" uid uid))
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "size FETCH"))
      (cond ((and need-size
		  (vm-imap-response-matches response '* 'atom 'FETCH 'list))
	     (setq need-size nil)
	     (setq p (cdr (nth 3 response)))
	     (while p
	       (cond 
		((vm-imap-response-matches p 'UID 'atom)
		 (setq tok (nth 1 p))
		 (if (not (equal uid (buffer-substring (nth 1 tok) (nth 2 tok))))
		     (vm-imap-protocol-error
		      "wrong UID number returned"))
		 (setq p (nthcdr 2 p)))
		((vm-imap-response-matches p 'RFC822\.SIZE 'atom)
		 (setq tok (nth 1 p))
		 (goto-char (nth 1 tok))
		 (setq size (buffer-substring (nth 1 tok) (nth 2 tok)))
		 (setq need-size nil)
		 (setq p (nthcdr 2 p)))
		(t
		 (vm-imap-protocol-error
		  "expected UID, RFC822.SIZE in FETCH response")))))
	    ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil))
	    ;; Otherwise, skip the response
	    ))
    (string-to-number size) ))

(defun vm-imap-read-capability-response (process)
  ;;----------------------------------
  (vm-buffer-type:assert 'process)
  ;;----------------------------------
  (vm-imap-log-token 'read-capability)
  (let (response r cap-list auth-list (need-ok t))
    (while need-ok
      (setq response (vm-imap-read-response-and-verify process "CAPABILITY"))
      (if (vm-imap-response-matches response 'VM 'OK)
	  (setq need-ok nil)
	(if (not (vm-imap-response-matches response '* 'CAPABILITY))
	    nil
	  ;; skip * CAPABILITY
	  (setq response (cdr (cdr response)))
	  (while response
	    (setq r (car response))
	    (if (not (eq (car r) 'atom))
		nil
	      (if (save-excursion
		    (goto-char (nth 1 r))
		    (let ((case-fold-search t))
		      (eq (re-search-forward "AUTH=." (nth 2 r) t)
			  (+ 6 (nth 1 r)))))
		  (progn
		    (setq auth-list (cons (intern
					   (upcase (buffer-substring
						    (+ 5 (nth 1 r))
						    (nth 2 r))))
					  auth-list)))
		(setq r (car response))
		(if (not (eq (car r) 'atom))
		    nil
		  (setq cap-list (cons (intern
					(upcase (buffer-substring
						 (nth 1 r) (nth 2 r))))
				       cap-list)))))
	    (setq response (cdr response))))))
    (if (or cap-list auth-list)
	(list (nreverse cap-list) (nreverse auth-list))
      nil)))

(defun vm-imap-read-greeting (process)
  ;;----------------------------------
  (vm-buffer-type:assert 'process)
  ;;----------------------------------
  (vm-imap-log-token 'read-greeting)
  (let (response)
    (setq response (vm-imap-read-response process))
    (cond ((vm-imap-response-matches response '* 'OK)
	   t )
	  ((vm-imap-response-matches response '* 'PREAUTH)
	   'preauth )
	  (t nil))))

(defun vm-imap-read-ok-response (process)
  ;;----------------------------------
  (vm-buffer-type:assert 'process)
  ;;----------------------------------
  (vm-imap-log-token 'read-ok)
  (let (response retval (done nil))
    (while (not done)
      (setq response (vm-imap-read-response process))
      (cond ((vm-imap-response-matches response '*)
	     nil )
	    ((vm-imap-response-matches response 'VM 'OK)
	     (setq retval t done t))
	    ((vm-imap-response-matches response 'VM 'NO)
	     (setq retval nil done t))
	    (t
	     (vm-imap-protocol-error "Did not receive OK response"))))
    retval ))

(defun vm-imap-cleanup-region (start end)
  (setq end (vm-marker end))
  (save-excursion
    (goto-char start)
    ;; CRLF -> LF
    (while (and (< (point) end) (search-forward "\r\n"  end t))
      (replace-match "\n" t t)))
  (set-marker end nil))

(defun vm-imap-read-response (process)
  ;; Reads a line of respose from the imap PROCESS
  ;;--------------------------------------------
  ;; This assertion often fails for some reason,
  ;; perhaps some asynchrony involved?
  ;; Assertion check being disabled unless debugging is on.
  (if vm-buffer-type-debug
      (vm-buffer-type:assert 'process))
  (if vm-buffer-type-debug
      (setq vm-buffer-type-trail (cons 'read vm-buffer-type-trail)))
  ;;--------------------------------------------
  (vm-imap-log-tokens (list 'response vm-imap-read-point))
  (let ((list nil) tail obj)
    (goto-char vm-imap-read-point)
    (while (not (eq (car (setq obj (vm-imap-read-object process)))
		    'end-of-line))
      (if (null list)
	  (setq list (cons obj nil)
		tail list)
	(setcdr tail (cons obj nil))
	(setq tail (cdr tail))))
    list ))

(defun vm-imap-read-response-and-verify (process &optional command-desc)
  ;; Reads a line of response from the imap PROCESS and checks for
  ;; standard errors like "BAD" and "BYE".  Optional COMMAND-DESC is a
  ;; command description that can be printed with the error message.
  ;;--------------------------------------------
  ;; This assertion often fails for some reason,
  ;; perhaps some asynchrony involved?
  ;; Assertion check being disabled unless debugging is on.
  (if vm-buffer-type-debug
      (vm-buffer-type:assert 'process))
  (if vm-buffer-type-debug
      (setq vm-buffer-type-trail (cons 'verify vm-buffer-type-trail)))
  ;;--------------------------------------------
  (let ((response (vm-imap-read-response process)))
    (if (vm-imap-response-matches response 'VM 'NO)
	(vm-imap-normal-error (format "server said NO")))
    (if (vm-imap-response-matches response 'VM 'BAD)
	(vm-imap-normal-error (format "server said BAD")))
    (if (vm-imap-response-matches response '* 'BYE)
	(vm-imap-normal-error (format "server disconnected")))
    response))


(defun vm-imap-read-object (process &optional skip-eol)
  ;;----------------------------------
  ;; Originally, this assertion failed often for some reason,
  ;; perhaps some asynchrony involved?
  ;; It has been mostly chased up by now. (Nov 2009)
  ;; Still assertion check being disabled unless debugging is on.
  (if vm-buffer-type-debug
      (vm-buffer-type:assert 'process))
  (vm-imap-log-tokens (list 'object (current-buffer)))
  ;;----------------------------------
  (let ((done nil)
	opoint
	(token nil))
    (unwind-protect
	(while (not done)
	  (skip-chars-forward " \t")
	  (cond ((< (- (point-max) (point)) 2)
		 (setq opoint (point))
		 (vm-imap-check-connection process)
		 (vm-accept-process-output process) 
		 (goto-char opoint))
		((looking-at "\r\n")
		 (forward-char 2)
		 (setq token '(end-of-line) done (not skip-eol)))
		((looking-at "\\[")
		 (forward-char 1)
		 (let* ((list (list 'vector))
			(tail list)
			obj)
		   (while (not (eq (car (setq obj (vm-imap-read-object process t)))
				   'close-bracket))
		     (if (eq (car obj) 'close-paren)
			 (vm-imap-protocol-error "unexpected )"))
		     (setcdr tail (cons obj nil))
		     (setq tail (cdr tail)))
		   (setq token list done t)))
		((looking-at "\\]")
		 (forward-char 1)
		 (setq token '(close-bracket) done t))
		((looking-at "(")
		 (forward-char 1)
		 (let* ((list (list 'list))
			(tail list)
			obj)
		   (while (not (eq (car (setq obj (vm-imap-read-object process t)))
				   'close-paren))
		     (if (eq (car obj) 'close-bracket)
			 (vm-imap-protocol-error "unexpected ]"))
		     (setcdr tail (cons obj nil))
		     (setq tail (cdr tail)))
		   (setq token list done t)))
		((looking-at ")")
		 (forward-char 1)
		 (setq token '(close-paren) done t))
		((looking-at "{")
		 (forward-char 1)
		 (let (start obj n-octets)
		   (setq obj (vm-imap-read-object process))
		   (if (not (eq (car obj) 'atom))
		       (vm-imap-protocol-error "number expected after {"))
		   (setq n-octets (string-to-number
				   (buffer-substring (nth 1 obj)
						     (nth 2 obj))))
		   (setq obj (vm-imap-read-object process))
		   (if (not (eq (car obj) 'close-brace))
		       (vm-imap-protocol-error "} expected"))
		   (setq obj (vm-imap-read-object process))
		   (if (not (eq (car obj) 'end-of-line))
		       (vm-imap-protocol-error "CRLF expected"))
		   (setq start (point))
		   (while (< (- (point-max) start) n-octets)
		     (vm-imap-check-connection process)
		     (vm-accept-process-output process))
		   (goto-char (+ start n-octets))
		   (setq token (list 'string start (point))
			 done t)))
		((looking-at "}")
		 (forward-char 1)
		 (setq token '(close-brace) done t))
		((looking-at "\042") ;; double quote
		 (forward-char 1)
		 (let ((start (point))
		       (curpoint (point)))
		   (while (not done)
		     (skip-chars-forward "^\042")
		     (setq curpoint (point))
		     (if (looking-at "\042")
			 (progn
			   (setq done t)
			   (forward-char 1))
		       (vm-imap-check-connection process)
		       (vm-accept-process-output process)
		       (goto-char curpoint))
		     (setq token (list 'string start curpoint)))))
		;; should be (looking-at "[\000-\040\177-\377]")
		;; but Microsoft Exchange emits 8-bit chars.
		((and (looking-at "[\000-\040\177]") 
		      (= vm-imap-tolerant-of-bad-imap 0))
		 (vm-imap-protocol-error "illegal char (%d)"
					 (char-after (point))))
		(t
		 (let ((start (point))
		       (curpoint (point))
		       ;; We should be considering 8-bit chars as
		       ;; non-word chars also but Microsoft Exchange
		       ;; uses them, despite the RFC 2060 prohibition.
		       ;; If we ever resume disallowing 8-bit chars,
		       ;; remember to write the range as \177-\376 ...
		       ;; \376 instead of \377 because Emacs 19.34 has
		       ;; a bug in the fastmap initialization code
		       ;; that causes it to infloop.
		       (not-word-chars "^\000-\040\177()[]{}")
		       (not-word-regexp "[][\000-\040\177(){}]"))
		   (while (not done)
		     (skip-chars-forward not-word-chars)
		     (setq curpoint (point))
		     (if (looking-at not-word-regexp)
			 (setq done t)
		       (vm-imap-check-connection process)
		       (vm-accept-process-output process)
		       (goto-char curpoint))
		     (vm-imap-log-token (buffer-substring start curpoint))
		     (setq token (list 'atom start curpoint)))))))
      (setq vm-imap-read-point (point))
      (vm-imap-log-token vm-imap-read-point)
      (vm-imap-log-token token))
    token ))

(defun vm-imap-response-matches (response &rest expr)
  (let ((case-fold-search t) e r)
    (catch 'done
      (while (and expr response)
	(setq e (car expr)
	      r (car response))
	(cond ((stringp e)
	       (if (or (not (eq (car r) 'string))
		       (save-excursion
			 (goto-char (nth 1 r))
			 (not (eq (search-forward e (nth 2 r) t) (nth 2 r)))))
		   (throw 'done nil)))
	      ((numberp e)
	       (if (or (not (eq (car r) 'atom))
		       (save-excursion
			 (goto-char (nth 1 r))
			 (not (eq (search-forward (int-to-string e)
						  (nth 2 r) t)
				  (nth 2 r)))))
		   (throw 'done nil)))
	      ((consp e)
	       (if (not (eq (car e) (car r)))
		   (throw 'done nil))
	       (apply 'vm-imap-response-matches (cdr r) (cdr e)))
	      ((eq e 'atom)
	       (if (not (eq (car r) 'atom))
		   (throw 'done nil)))
	      ((eq e 'vector)
	       (if (not (eq (car r) 'vector))
		   (throw 'done nil)))
	      ((eq e 'list)
	       (if (not (eq (car r) 'list))
		   (throw 'done nil)))
	      ((eq e 'string)
	       (if (not (eq (car r) 'string))
		   (throw 'done nil)))
	      ;; this must to come after all the comparisons for
	      ;; specific symbols.
	      ((symbolp e)
	       (if (or (not (eq (car r) 'atom))
		       (save-excursion
			 (goto-char (nth 1 r))
			 (not (eq (search-forward (symbol-name e) (nth 2 r) t)
				  (nth 2 r)))))
		   (throw 'done nil))))
	(setq response (cdr response)
	      expr (cdr expr)))
      t )))

(defun vm-imap-bail-if-server-says-farewell (response)
  (if (vm-imap-response-matches response '* 'BYE)
      (throw 'end-of-session t)))

(defun vm-imap-scan-list-for-flag (list flag)
  (setq list (cdr list))
  (let ((case-fold-search t) e)
    (catch 'done
      (while list
	(setq e (car list))
	(if (not (eq (car e) 'atom))
	    nil
	  (goto-char (nth 1 e))
	  (if (eq (search-forward flag (nth 2 e) t) (nth 2 e))
	      (throw 'done t)))
	(setq list (cdr list)))
      nil )))

;; like Lisp get but for IMAP property lists like those returned by FETCH.
(defun vm-imap-plist-get (list name)
  (setq list (cdr list))
  (let ((case-fold-search t) e)
    (catch 'done
      (while list
	(setq e (car list))
	(if (not (eq (car e) 'atom))
	    nil
	  (goto-char (nth 1 e))
	  (if (eq (search-forward name (nth 2 e) t) (nth 2 e))
	      (throw 'done (car (cdr list)))))
	(setq list (cdr (cdr list))))
      nil )))

(defun vm-imap-quote-string (string)
  (vm-with-string-as-temp-buffer string 'vm-imap-quote-buffer))

(defun vm-imap-quote-buffer ()
  (goto-char (point-min))
  (insert "\"")
  (while (re-search-forward "[\"\\]" nil t)
    (forward-char -1)
    (insert "\\")
    (forward-char 1))
  (goto-char (point-max))
  (insert "\""))

(defun vm-imap-poke-session (process)
  "Poke the IMAP session by sending a NOOP command, just to make sure
that the session is active.  Returns t or nil."
  (if (and process (memq (process-status process) '(open run))
	   (buffer-live-p (process-buffer process)))
      (if vm-imap-ensure-active-sessions
	  (let ((buffer (process-buffer process)))
	    (save-excursion		; = save-current-buffer?
	      (set-buffer buffer)
	      ;;----------------------------
	      (vm-buffer-type:enter 'process)
	      ;;----------------------------
	      (vm-imap-send-command process "NOOP")
	      (condition-case err
		  (let ((response nil)
			(need-ok t))
		    (while need-ok
		      (setq response
			    (vm-imap-read-response-and-verify process "NOOP"))
		      (cond ((vm-imap-response-matches response 'VM 'OK)
			     (setq need-ok nil))))
		    ;;----------------------------
		    (vm-buffer-type:exit)
		    ;;----------------------------
		    t)
		(vm-imap-protocol-error	; handler
		 ;;--------------------
		 (vm-buffer-type:exit)
		 ;;--------------------
		 nil))))		; ignore errors
	t)
    nil))

(defun vm-re-establish-folder-imap-session (&optional interactive purpose)
  "If the IMAP session for the current folder has died, re-establish a
new one.  Returns the IMAP process or nil if unsuccessful."
  (let ((process (vm-folder-imap-process)) temp)
    (if  (and (processp process)
	      (vm-imap-poke-session process))
	process
      (if process
	  (vm-imap-end-session process))
      (vm-establish-new-folder-imap-session interactive purpose))))

(defun vm-establish-new-folder-imap-session (&optional interactive purpose)
  "Kill and restart the IMAP session for the current folder.  Optional
argument PURPOSE is inserted into the process buffer for tracing
purposes. Returns the IMAP process or nil if unsuccessful."
;; This is necessary because we might get unexpected EXPUNGE responses
;; which we don't know how to deal with.

  (let (process 
	(vm-imap-ok-to-ask interactive)
	mailbox select mailbox-count uid-validity permanent-flags
	read-write can-delete body-peek)
    (if (vm-folder-imap-process)
	(vm-imap-end-session (vm-folder-imap-process)))
    (vm-imap-log-token 'new)
    (setq process 
	  (vm-imap-make-session (vm-folder-imap-maildrop-spec)
				interactive purpose))
    (when (processp process)
      (vm-set-folder-imap-process process)
      (setq mailbox (vm-imap-parse-spec-to-list (vm-folder-imap-maildrop-spec))
	    mailbox (nth 3 mailbox))
      (save-excursion			; = save-current-buffer?
	(set-buffer (process-buffer process))
	;;----------------------------
	(vm-buffer-type:enter 'process)
	;;----------------------------
	(setq select (vm-imap-select-mailbox process mailbox))
	(setq mailbox-count (nth 0 select)
	      uid-validity (nth 1 select)
	      read-write (nth 2 select)
	      can-delete (nth 3 select)
	      permanent-flags (nth 4 select)
	      body-peek (vm-imap-capability 'IMAP4REV1))
	;;---------------------------------
	(vm-imap-session-type:set 'active)
	(vm-buffer-type:exit)
	;;---------------------------------
	)
      (if (and (vm-folder-imap-uid-validity)
	       (not (equal (vm-folder-imap-uid-validity) uid-validity)))
	  (if (y-or-n-p 
	       (concat "Folder's UID VALIDITY value has changed "
		       "on the server.  Continue? "))
	      (progn
		(message (concat "VM will download new copies of messages"
				 " and mark the old ones for deletion"))
		(sit-for 4))
	    (error "Aborted")))
      (vm-set-folder-imap-uid-validity uid-validity) ; unique per session
      (vm-set-folder-imap-mailbox-count mailbox-count)
      (vm-set-folder-imap-read-write read-write)
      (vm-set-folder-imap-can-delete can-delete)
      (vm-set-folder-imap-body-peek body-peek)
      (vm-set-folder-imap-permanent-flags permanent-flags)
      ;;-------------------------------
      (vm-imap-dump-uid-and-flags-data)
      ;;-------------------------------
      process )))

(defun vm-establish-writable-imap-session (maildrop &optional 
						    interactive purpose)
  "Create a new writable IMAP session for MAILDROP and return the process.
Optional argument PURPOSE is inserted into the process buffer for
tracing purposes. Returns the IMAP process or nil if
unsuccessful."
  (let (process 
	(vm-imap-ok-to-ask interactive)
	mailbox select mailbox-count uid-validity permanent-flags
	read-write can-delete body-peek)
    (vm-imap-log-token 'new)
    (setq process 
	  (vm-imap-make-session maildrop interactive purpose))
    (if (processp process)
	(save-current-buffer
	  (setq mailbox (vm-imap-parse-spec-to-list maildrop)
		mailbox (nth 3 mailbox))
	  ;;----------------------------
	  (vm-buffer-type:enter 'process)
	  ;;----------------------------
	  (set-buffer (process-buffer process))
	  (setq select (vm-imap-select-mailbox process mailbox))
	  (setq mailbox-count (nth 0 select)
		uid-validity (nth 1 select)
		read-write (nth 2 select)
		can-delete (nth 3 select)
		permanent-flags (nth 4 select)
		body-peek (vm-imap-capability 'IMAP4REV1))
	  ;;---------------------------------
	  (vm-imap-session-type:set 'active)
	  (vm-buffer-type:exit)
	  ;;---------------------------------
	  (if read-write
	      process
	    (vm-imap-end-session process)
	    nil))
      nil)))


(defun vm-kill-folder-imap-session  (&optional interactive)
  (let ((process (vm-folder-imap-process)))
    (if (processp process)
	(vm-imap-end-session process))))

(defun vm-imap-retrieve-uid-and-flags-data ()
  "Retrieve the uid's and message flags for all the messages on the
IMAP server in the current mail box.
Throws vm-imap-protocol-error for failure."
  ;;------------------------------
  (if vm-buffer-type-debug
      (setq vm-buffer-type-trail 
	    (cons 'uid-and-flags-data vm-buffer-type-trail)))
  (vm-buffer-type:assert 'folder)
  ;;------------------------------
  (if (vm-folder-imap-uid-list)
      nil ; don't retrieve twice
    (let ((there (make-vector 67 0))
	  (flags (make-vector 67 0))
	  (process (vm-folder-imap-process))
	  (mailbox-count (vm-folder-imap-mailbox-count))
	  list tuples tuple uid)
      (save-excursion			; = save-current-buffer?
	(set-buffer (process-buffer process))
	;;----------------------------
	(vm-buffer-type:enter 'process)
	;;----------------------------
	(if (eq mailbox-count 0)
	    (setq list nil)
	  (setq list (vm-imap-get-message-data-list process 1 mailbox-count)))
	(setq tuples list)
	(while tuples
	  (setq tuple (car tuples))
	  (set (intern (cadr tuple) there) (car tuple))
	  (set (intern (cadr tuple) flags) (nthcdr 2 tuple))
	  (setq tuples (cdr tuples)))
	;;-------------------------------
	(vm-imap-session-type:set 'valid)
	(vm-buffer-type:exit)
	;;-------------------------------
	)
      (vm-set-folder-imap-uid-list list)
      (vm-set-folder-imap-uid-obarray there)
      (vm-set-folder-imap-flags-obarray flags))))

(defun vm-imap-dump-uid-and-flags-data ()
  (when (and vm-folder-access-data
             (eq (car vm-buffer-types) 'folder))
             
    ;;------------------------------
    (vm-buffer-type:assert 'folder)
    ;;------------------------------
    (vm-set-folder-imap-uid-list nil)
    (vm-set-folder-imap-uid-obarray nil)
    (vm-set-folder-imap-flags-obarray nil)
    (if (processp (vm-folder-imap-process))
	(with-current-buffer (process-buffer (vm-folder-imap-process))
	  ;;---------------------------------
	  (vm-imap-session-type:set 'active)
	  ;;---------------------------------
	  ))
    ))

(defun vm-imap-dump-uid-seq-num-data ()
  (when (and vm-folder-access-data
             (eq (car vm-buffer-types) 'folder))
             
    ;;------------------------------
    (vm-buffer-type:assert 'folder)
    ;;------------------------------
    (vm-set-folder-imap-uid-list nil)
    (vm-set-folder-imap-uid-obarray nil)
    (if (processp (vm-folder-imap-process))
	(with-current-buffer (process-buffer (vm-folder-imap-process))
	  ;;---------------------------------
	  (vm-imap-session-type:set 'active)
	  ;;---------------------------------
	  ))
    ))

;; This function is now obsolete.  It is faster to get flags of
;; several messages at once, using vm-imap-get-message-data-list

(defun vm-imap-get-message-flags (process m &optional norecord)
  ;; gives an error if the message has an invalid uid
  (let (need-ok p r flag response saw-Seen)
    (if (not (equal (vm-imap-uid-validity-of m)
		    (vm-folder-imap-uid-validity)))
	(vm-imap-normal-error "message UIDVALIDITY does not match the server"))
    (save-excursion		  ; = save-current-buffer?
      (set-buffer (process-buffer process))
      ;;----------------------------------
      (vm-buffer-type:enter 'process)
      (vm-imap-session-type:assert-active)
      ;;----------------------------------
      (vm-imap-send-command process
			    (format "UID FETCH %s (FLAGS)"
				    (vm-imap-uid-of m)))
      ;;--------------------------------
      (vm-imap-session-type:set 'active)
      ;;--------------------------------
      (setq need-ok t)
      (while need-ok
	(setq response (vm-imap-read-response-and-verify process "UID FETCH (FLAGS)"))
	(cond ((vm-imap-response-matches response 'VM 'OK)
	       (setq need-ok nil))
	      ((vm-imap-response-matches response '* 'atom 'FETCH 'list)
	       (setq r (nthcdr 3 response)
		     r (car r)
		     r (vm-imap-plist-get r "FLAGS")
		     r (cdr r))
	       (while r
		 (setq p (car r))
		 (if (not (eq (car p) 'atom))
		     nil
		   (setq flag (downcase (buffer-substring (nth 1 p) (nth 2 p))))
		   (cond ((string= flag "\\answered")
			  (vm-set-replied-flag m t norecord))
			 ((string= flag "\\deleted")
			  (vm-set-deleted-flag m t norecord))
			 ((string= flag "\\seen")
			  (vm-set-unread-flag m nil norecord)
			  (vm-set-new-flag m nil norecord)
			  (setq saw-Seen t))
			 ((string= flag "\\recent")
			  (vm-set-new-flag m t norecord))))
		 (setq r (cdr r)))
	       (if (not saw-Seen)
		   (vm-set-unread-flag m t norecord)))))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      )))

(defun vm-imap-update-message-flags (m flags &optional norecord)
  "Update the flags of the message M in the folder to imap flags FLAGS.
Optional argument NORECORD says whether this fact should not be
recorded in the undo stack."
  (let (flag saw-Seen saw-Deleted saw-Flagged seen-labels labels)
    (while flags
      (setq flag (car flags))
      (cond ((string= flag "\\answered")
	     (when (null (vm-replied-flag m))
	       (vm-set-replied-flag m t norecord)
	       (vm-set-stuff-flag-of m t)))

	    ((string= flag "\\deleted")
	     (when (null (vm-deleted-flag m))
	       (vm-set-deleted-flag m t norecord)
	       (vm-set-stuff-flag-of m t))
	     (setq saw-Deleted t))

	    ((string= flag "\\seen")
	     (when (vm-unread-flag m)
	       (vm-set-unread-flag m nil norecord)
	       (vm-set-stuff-flag-of m t))
	     (when (vm-new-flag m)
	       (vm-set-new-flag m nil norecord)
	       (vm-set-stuff-flag-of m t))
	     (setq saw-Seen t))

	    ((string= flag "\\recent")
	     (when (null (vm-new-flag m))
	       (vm-set-new-flag m t norecord)
	       (vm-set-stuff-flag-of m t)))

	    ((string= flag "forwarded")
	     (when (null (vm-forwarded-flag m))
	       (vm-set-forwarded-flag m t norecord)
	       (vm-set-stuff-flag-of m t)))

	    ((string= flag "redistributed")
	     (when (null (vm-redistributed-flag m))
	       (vm-set-redistributed-flag m t norecord)
	       (vm-set-stuff-flag-of m t)))

	    ((string= flag "filed")
	     (when (null (vm-filed-flag m))
	       (vm-set-filed-flag m t norecord)
	       (vm-set-stuff-flag-of m t)))

	    ((string= flag "written")
	     (when (null (vm-written-flag m))
	       (vm-set-written-flag m t norecord)
	       (vm-set-stuff-flag-of m t)))

	    (t			  ; all other flags including \flagged
	     (setq seen-labels (cons flag seen-labels)))
	    )
      (setq flags (cdr flags)))

    (if (not saw-Seen)			; unread if the server says so
	(if (null (vm-unread-flag m))
	    (vm-set-unread-flag m t norecord)))
    (if (not saw-Deleted)		; undelete if the server says so
	(if (vm-deleted-flag m)
	    (vm-set-deleted-flag m nil norecord)))
    (setq labels (sort (vm-labels-of m) 'string-lessp))
    (setq seen-labels (sort seen-labels 'string-lessp))
    (if (equal labels seen-labels)
	t
      (vm-set-labels-of m seen-labels)
      (vm-set-label-string-of m nil)
      (vm-mark-for-summary-update m)
      (vm-set-stuff-flag-of m t))
    ))

(defun vm-imap-save-message-flags (process m &optional by-uid)
  "Saves the message flags of a message on the IMAP server,
adding or deleting flags on the server as necessary.  Monotonic
flags, however, are not deleted.

Optional argument BY-UID says that the save commands to the
server should be issued by UID, not message sequence number."

  ;; Comment by USR
  ;; According to RFC 2060, it is not an error to store flags that
  ;; are not listed in PERMANENTFLAGS.  Removed unnecessary checks to
  ;; this effect.

  ;; This is a hairy routine! There are 
  ;; - monotonic flags that can only be set, and 
  ;; - reversible flags that can be set or unset.
  ;; For monotonic flags that are set in VM, we set them on the
  ;; server.
  ;; For reversible flags, we copy the state from VM to the server.
  ;; (We don't know which one has precedence, but we punt that issue.)
  ;; The cache needs to be maintained consistently.

  ;;-----------------------------------------------------
  (vm-buffer-type:assert 'folder)
  (or by-uid (vm-imap-folder-session-type:assert 'valid))
  ;;-----------------------------------------------------
  (if (not (equal (vm-imap-uid-validity-of m)
		  (vm-folder-imap-uid-validity)))
      (vm-imap-normal-error "message UIDVALIDITY does not match the server"))
  (let* ((uid (vm-imap-uid-of m))
	 (uid-key1 (intern uid (vm-folder-imap-uid-obarray)))
	 (uid-key2 (intern-soft uid (vm-folder-imap-flags-obarray)))
	 (message-num (and (boundp uid-key1) (symbol-value uid-key1)))
	 (cached-flags (and (boundp uid-key2) (symbol-value uid-key2)))
					; leave uid as the dummy header
	 (labels (vm-labels-of m))
	 copied-flags need-ok flags+ flags- response)
    (when message-num
      ;; Reversible flags are treated the same as labels
      (if (not (vm-unread-flag m))
	  (setq labels (cons "\\seen" labels)))
      (if (vm-deleted-flag m)
	  (setq labels (cons "\\deleted" labels)))
      ;; Irreversible flags
      (if (and (vm-replied-flag m) 
	       (not (member "\\answered" cached-flags)))
	  (setq flags+ (cons "\\Answered" flags+)))
      (if (and (vm-filed-flag m) (not (member "filed" cached-flags)))
	  (setq flags+ (cons "filed" flags+)))
      (if (and (vm-written-flag m) 
	       (not (member "written" cached-flags)))
	  (setq flags+ (cons "written" flags+)))
      (if (and (vm-forwarded-flag m)
	       (not (member "forwarded" cached-flags)))
	  (setq flags+ (cons "forwarded" flags+)))
      (if (and (vm-redistributed-flag m)
	       (not (member "redistributed" cached-flags)))
	  (setq flags+ (cons "redistributed" flags+)))
      (mapc (lambda (flag) (delete flag cached-flags))
	    '("\\answered" "filed" "written" "forwarded" "redistributed"))
      ;; make copies for side effects
      (setq copied-flags (copy-sequence cached-flags))
      (setq labels (cons nil (copy-sequence labels)))
      ;; Ignore labels that are both in vm and the server
      (delete-common-elements labels copied-flags 'string<)
      ;; Ignore reversible flags that we have locally reversed -- Why?
      ;; (mapc (lambda (flag) (delete flag copied-flags))
      ;;  '("\\seen" "\\deleted" "\\flagged"))
      ;; Flags to be added to the server
      (setq flags+ (append (cdr labels) flags+))
      ;; Flags to be deleted from the server
      (setq flags- (append (cdr copied-flags) flags-))

      (save-excursion			; = save-current-buffer?
	(set-buffer (process-buffer process))
	;;----------------------------------
	(vm-buffer-type:enter 'process)
	;;----------------------------------
	(when flags+
	  (vm-imap-send-command 
	   process
	   (format "%sSTORE %s +FLAGS.SILENT %s" 
		   (if by-uid "UID " "")
		   (if by-uid uid message-num)
		   (mapc 'intern flags+)))
	  (setq need-ok t)
	  (while need-ok
	    (setq response 
		  (vm-imap-read-response-and-verify 
		   process "STORE +FLAGS.SILENT"))
	    (cond ((vm-imap-response-matches response 'VM 'OK)
		   (setq need-ok nil))))
	  (nconc cached-flags flags+))

	(when flags-
	  (vm-imap-send-command 
	   process
	   (format "%sSTORE %s -FLAGS.SILENT %s"
		   (if by-uid "UID " "")
		   (if by-uid uid message-num)
		   (mapc 'intern flags-)))
	  (setq need-ok t)
	  (while need-ok
	    (setq response 
		  (vm-imap-read-response-and-verify 
		   process "STORE -FLAGS.SILENT"))
	    (cond ((vm-imap-response-matches response 'VM 'OK)
		   (setq need-ok nil))))
	  (while flags-
	    (delete (car flags-) cached-flags)
	    (setq flags- (cdr flags-))))

	(vm-set-attribute-modflag-of m nil)
	;;-------------------
	(vm-buffer-type:exit)
	;;-------------------
	))))

(defvar vm-imap-subst-char-in-string-buffer
  (get-buffer-create " *subst-char-in-string*"))

(defun vm-imap-subst-CRLF-for-LF (string)
  (with-current-buffer vm-imap-subst-char-in-string-buffer
    (erase-buffer)
    (insert string)
    (goto-char (point-min))
    (while (search-forward "\n" nil t)
      (replace-match "\r\n" nil t))
    (buffer-substring-no-properties (point-min) (point-max))))

;;;###autoload
(defun vm-imap-save-message (process m mailbox)
  "Using the IMAP process PROCESS, save the message M to IMAP mailbox
MAILBOX." 
  (let (need-ok need-plus flags response string)
    ;; save the message's flag along with it.
    ;; don't save the deleted flag.
    (if (vm-replied-flag m)
	(setq flags (cons (intern "\\Answered") flags)))
    (if (not (vm-unread-flag m))
	(setq flags (cons (intern "\\Seen") flags)))
    (with-current-buffer (vm-buffer-of m)
      ;;----------------------------
      (vm-buffer-type:enter 'folder)
      ;;----------------------------
      (save-restriction
	(widen)
	(setq string (buffer-substring (vm-headers-of m) (vm-text-end-of m))
              string (vm-imap-subst-CRLF-for-LF string)))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      )
    (save-excursion			; = save-current-buffer?
      (set-buffer (process-buffer process))
      ;;----------------------------
      (vm-buffer-type:enter 'process)
      ;;----------------------------
      (condition-case nil
	  (vm-imap-create-mailbox process mailbox)
	(vm-imap-protocol-error (vm-buffer-type:set 'process))); ignore errors
      ;;----------------------------------
      (vm-imap-session-type:assert-active)
      ;;----------------------------------
      (vm-imap-send-command process
			    (format "APPEND %s %s {%d}"
				    (vm-imap-quote-string mailbox)
				    (if flags flags "()")
				    (length string)))
      ;;--------------------------------
      (vm-imap-session-type:set 'active)
      ;;--------------------------------
      (setq need-plus t)
      (while need-plus
	(setq response (vm-imap-read-response-and-verify process "APPEND"))
	(cond ((vm-imap-response-matches response '+)
	       (setq need-plus nil))))
      (vm-imap-send-command process string nil t)
      (setq need-ok t)
      (while need-ok
	(setq response (vm-imap-read-response-and-verify process "APPEND data"))
	(cond ((vm-imap-response-matches response 'VM 'OK)
	       (setq need-ok nil))))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      )))

;; Incomplete -- Yet to be finished.  USR
;; creation of new mailboxes has to be straightened out

(defun vm-imap-copy-message (process m mailbox)
  "Use IMAP session PROCESS to copy message M to MAILBOX.  The PROCESS
is expected to have logged in and selected the current folder.

This is similar to vm-imap-save-message but uses the internal copy
operation of the server to minimize I/O."
  ;;-----------------------------
  (vm-buffer-type:set 'folder)
  ;;-----------------------------
  (let ((uid (vm-imap-uid-of m))
	(uid-validity (vm-imap-uid-validity-of m))
	need-ok response string)
    (if (not (equal uid-validity (vm-folder-imap-uid-validity)))
	(error "Message does not have a valid UID"))
    (save-excursion
      ;;------------------------
      (vm-buffer-type:duplicate)
      ;;------------------------
      (if (vm-attribute-modflag-of m)
	  (condition-case nil
	      (progn
		(if (null (vm-folder-imap-flags-obarray))
		    (vm-imap-retrieve-uid-and-flags-data))
		(vm-imap-save-message-flags process m 'by-uid))
	    (vm-imap-protocol-error nil))) ; is this right?
;;       (condition-case nil
;; 	    (vm-imap-create-mailbox process mailbox)
;; 	  (vm-imap-protocol-error nil))

      (set-buffer (process-buffer process))
      ;;-----------------------------------------
      (vm-buffer-type:set 'process)
      (vm-imap-session-type:assert-active)
      ;;-----------------------------------------
      (vm-imap-send-command 
       process
       (format "UID COPY %s %s"
	       (vm-imap-uid-of m)
	       (vm-imap-quote-string mailbox)))
      ;;--------------------------------
      (vm-imap-session-type:set 'active)
      ;;--------------------------------
      (setq need-ok t)
      (while need-ok
	(setq response 
	      (vm-imap-read-response-and-verify process "UID COPY"))
	(cond ((vm-imap-response-matches response 'VM 'OK)
	       (setq need-ok nil))))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      )))

;; ------------------------------------------------------------------------
;; 
;; interactive commands:
;; vm-create-imap-folder: string -> void
;; vm-delete-imap-folder: string -> void
;; vm-rename-imap-folder: string & string -> void
;; 
;; top-level operations
;; vm-fetch-imap-message: (vm-message) -> void
;; vm-imap-synchronize-folder:
;;	(&optional interactive & bool & bool & bool & bool) -> void
;; vm-imap-save-attributes: (&optional interactive) -> void
;; vm-imap-folder-check-for-mail: (&optional interactive) -> ?
;;
;; vm-imap-get-synchronization-data: (&optional bool) -> 
;;		(retrieve-list: (uid . int) list &
;;		 expunge-list: vm-message list & 
;;		 stale-list: vm-message list)
;;
;; ------------------------------------------------------------------------



(defun vm-imap-get-synchronization-data (&optional do-retrieves)
  ;; Compares the UID's of messages in the local cache and the IMAP
  ;; server.  Returns a list containing:
  ;; RETRIEVE-LIST: A list of pairs consisting of UID's and message
  ;; sequence numbers of the messages that are not present in the
  ;; local cache and, hence, need to be retrieved.
  ;; EXPUNGE-LIST: A list of message descriptors for messages in the
  ;; local cache which are not present on the server and, hence, need
  ;; to expunged locally.
  ;; STALE-LIST: A list of message descriptors for messages in the
  ;; local cache whose uidvalidity values are stale.
  ;; If the argument DO-RETRIEVES is 'full, then all the messages that
  ;; are not presently in cache are retrieved.  Otherwise, the
  ;; messages previously retrieved are ignored.

  ;; Comments by USR
  ;; - Originally, messages with stale UIDVALIDITY values were
  ;; ignored.  So, they would never get expunged from the cache.  The
  ;; STALE-LIST component was added to fix this.
  
  ;;-----------------------------
  (if vm-buffer-type-debug
      (setq vm-buffer-type-trail (cons 'synchronization-data
				       vm-buffer-type-trail)))
  (vm-buffer-type:assert 'folder)
  ;;-----------------------------
  (let ((here (make-vector 67 0))	; OBARRAY(uid, vm-message)
	there flags
	(uid-validity (vm-folder-imap-uid-validity))
	(do-full-retrieve (eq do-retrieves 'full))
	retrieve-list expunge-list stale-list uid
	mp)
    (vm-imap-retrieve-uid-and-flags-data)
    (setq there (vm-folder-imap-uid-obarray))
    ;; Figure out stale uidvalidity values and messages to be expunged
    ;; in the cache.
    (setq mp vm-message-list)
    (while mp
      (cond ((not (equal (vm-imap-uid-validity-of (car mp)) uid-validity))
	     (setq stale-list (cons (car mp) stale-list)))
	    ((member "stale" (vm-labels-of (car mp)))
	     nil)
	    (t
	     (setq uid (vm-imap-uid-of (car mp)))
	     (set (intern uid here) (car mp))
	     (if (not (boundp (intern uid there)))
		 (setq expunge-list (cons (car mp) expunge-list)))))
      (setq mp (cdr mp)))
    ;; Figure out messages that need to be retrieved
    (mapatoms (function
	       (lambda (sym)
		 (if (and (not (boundp (intern (symbol-name sym) here)))
			  (or do-full-retrieve
			      (not (assoc (symbol-name sym)
					  vm-imap-retrieved-messages))))
		     ;; don't retrieve messages that have been
		     ;; retrieved previously
		     ;; This is bad because if a message got lost
		     ;; somehow, it won't be retrieved!  USR
		     (setq retrieve-list (cons
					  (cons (symbol-name sym)
						(symbol-value sym))
					  retrieve-list)))))
	      there)
    (setq retrieve-list 
	  (sort retrieve-list 
		(lambda (**pair1 **pair2)
		  (< (cdr **pair1) (cdr **pair2)))))	  
    (list retrieve-list expunge-list stale-list)))

(defun vm-imap-server-error (msg &rest args)
  (if (eq vm-imap-connection-mode 'online)
      (apply (function error) msg args)
    (message "VM working in offline mode")))

;;;###autoload
(defun vm-imap-synchronize-folder (&optional interactive
					     do-remote-expunges
					     do-local-expunges
					     do-retrieves
					     save-attributes
					     retrieve-attributes)
  "* Synchronize IMAP folder with the server.
   INTERACTIVE, true if the function was invoked interactively, e.g., as
   vm-get-spooled-mail.
   DO-REMOTE-EXPUNGES indicates whether the server mail box should be
   expunged.
   DO-LOCAL-EXPUNGES indicates whether the cache buffer should be
   expunged.
   DO-RETRIEVES indicates if new messages that are not already in the
   cache should be retrieved from the server.  If this flag is 'full
   then messages previously retrieved but not in cache are retrieved
   as well.
   SAVE-ATTRIBUTES indicates if the message attributes should be updated on
   the server.  If it is 'all, then the attributes of all messages are
   updated irrespective of whether they were modified or not.
   RETRIEVE-ATTRIBTUES indicates if the message attributes on the server
   should be retrieved, updating the cache.
"
  ;; -- Comments by USR
  ;; Not clear why do-local-expunges and do-remote-expunges should be
  ;; separate.  It doesn't make sense to do one but not the other!

  ;;--------------------------
  (if vm-buffer-type-debug
      (setq vm-buffer-type-trail (cons 'synchronize vm-buffer-type-trail)))
  (vm-buffer-type:set 'folder)
  (vm-imap-init-log)
  (vm-imap-log-tokens (list 'synchronize (current-buffer)
			    (vm-folder-imap-process)))
  (setq vm-buffer-type-trail nil)
  ;;--------------------------
  (if (and do-retrieves vm-block-new-mail)
      (error "Can't get new mail until you save this folder"))
  (if (or vm-global-block-new-mail
	  (eq vm-imap-connection-mode 'offline)
	  (null (vm-establish-new-folder-imap-session 
		 interactive "general operation")))
      (vm-imap-server-error "Could not connect to the IMAP server")
    (if do-retrieves
	(vm-assimilate-new-messages))	; Funny that this should be
					; necessary.  Indicates bugs?
    (message "Logging into the IMAP server...")
    (let* ((folder-buffer (current-buffer))
	   (process (vm-folder-imap-process))
	   (imap-buffer (process-buffer process))
	   (n 1) (pos nil)
	   (statblob nil) (m nil) (mflags nil)
	   (uid nil)
	   (uid-validity (vm-folder-imap-uid-validity))
	   (imapdrop (vm-folder-imap-maildrop-spec))
	   (folder (or (vm-imap-folder-for-spec imapdrop)
		       (vm-safe-imapdrop-string imapdrop)))
	   (use-body-peek (vm-folder-imap-body-peek))
	   r-list range k mp got-some message-size old-eob
	   (sync-data (vm-imap-get-synchronization-data do-retrieves))
	   (retrieve-list (nth 0 sync-data))
	   (expunge-list (nth 1 sync-data))
	   (stale-list (nth 2 sync-data))
	   (flags (vm-folder-imap-flags-obarray)))
      (when save-attributes
	(let ((mp vm-message-list)
	      (errors 0))
	  ;;  (perm-flags (vm-folder-imap-permanent-flags))
	  (message "Updating attributes on the IMAP server... ")
	  (while mp
	    (if (or (eq save-attributes 'all)
		    (vm-attribute-modflag-of (car mp)))
		(condition-case nil
		    (vm-imap-save-message-flags process (car mp))
		  (vm-imap-protocol-error ; handler
		   (setq errors (1+ errors))
		   (vm-buffer-type:set 'folder))))
	    (setq mp (cdr mp)))
	  (if (> errors 0)
	      (message 
	       "Updating attributes on the IMAP server... %d errors" errors)
	    (message "Updating attributes on the IMAP server... done"))))
      (when retrieve-attributes
	(let ((mp vm-message-list)
	      (len (length vm-message-list))
	      (n 0))
	  (message "Retrieving message attributes and labels... ")
	  (while mp
	    (setq m (car mp))
	    (setq uid (vm-imap-uid-of m))
	    (when (and (equal (vm-imap-uid-validity-of m) uid-validity)
		       (boundp (intern uid flags)))
	      (setq mflags (cdr (symbol-value (intern uid flags))))
	      (vm-imap-update-message-flags m mflags t))
	    (setq mp (cdr mp)
		  n (1+ n)))
	  (message "Retrieving message atrributes and labels... done")
	  ))
      (when (and do-retrieves retrieve-list)
	(save-excursion
	  (message "Retrieving new messages... ")
	  (vm-save-restriction
	   (widen)
	   (setq old-eob (point-max))
	   (goto-char (point-max))
	   (unwind-protect
	       (condition-case error-data
		   (save-excursion	; = save-current-buffer?
		     (set-buffer (process-buffer process))
		     ;;----------------------------
		     (vm-buffer-type:enter 'process)
		     ;;----------------------------
		     (setq statblob (vm-imap-start-status-timer))
		     (vm-set-imap-stat-x-mailbox statblob folder)
		     (vm-set-imap-stat-x-maxmsg statblob
						(length retrieve-list))
		     (setq r-list (vm-imap-bunch-messages 
				   (mapcar (function cdr) retrieve-list)))
		     (while r-list
		       (setq range (car r-list))
		       (vm-set-imap-stat-x-currmsg statblob n)
		       (setq message-size 
			     (vm-imap-get-message-size
			      process (car range))) ; sloppy, one size fits all
		       (vm-set-imap-stat-x-need statblob message-size)
		       ;;----------------------------------
		       (vm-imap-session-type:assert 'valid)
		       ;;----------------------------------
		       (vm-imap-fetch-messages 
			process (car range) (cdr range)
			use-body-peek vm-load-headers-only)
		       (setq k (1+ (- (cdr range) (car range))))
		       (setq pos (with-current-buffer folder-buffer (point)))
		       (while (> k 0)
			 (vm-imap-retrieve-to-target process folder-buffer
						     statblob use-body-peek)
			 (with-current-buffer folder-buffer
			   (if (= (point) pos)
			       (debug "IMAP internal error #2012: the point hasn't moved")))
			 (setq k (1- k)))
		       (vm-imap-read-ok-response process)
		       (setq r-list (cdr r-list)
			     n (+ n (1+ (- (cdr range) (car range)))))))
		 (vm-imap-normal-error	; handler
		  (message "IMAP error: %s" (cadr error-data)))
		 (vm-imap-protocol-error ; handler
		  (message "Retrieval from %s signaled: %s" folder
			   error-data))
		 ;; Continue with whatever messages have been read
		 (quit
		  (delete-region old-eob (point-max))
		  (error (format "Quit received during retrieval from %s"
				 folder))))
	     ;; cleanup
	     (when statblob 
	       (vm-imap-stop-status-timer statblob))	   
	     )
	   ;;-------------------
	   (vm-buffer-type:exit)
	   ;;-------------------
	   ;; to make the "Mail" indicator go away
	   (setq vm-spooled-mail-waiting nil)
	   (intern (buffer-name) vm-buffers-needing-display-update)
	   (message "Updating summary... ")
	   (vm-update-summary-and-mode-line)
	   (setq mp (vm-assimilate-new-messages t))
	   (setq got-some mp)
           (if got-some
               (vm-increment vm-modification-counter))
           (setq r-list retrieve-list)
	   (while mp
	     ;; headers-only loading is still experimental. USR, 2010-01-12
	     (when vm-load-headers-only 
	       (vm-set-body-to-be-retrieved-of (car mp) t)
	       (vm-set-body-to-be-discarded-of (car mp) nil))
	     (setq uid (car (car r-list)))
	     (vm-set-imap-uid-of (car mp) uid)
	     (vm-set-imap-uid-validity-of (car mp) uid-validity)
	     (vm-set-byte-count-of 
	      (car mp) (car (symbol-value (intern uid flags))))
	     (vm-imap-update-message-flags 
	      (car mp) (cdr (symbol-value (intern uid flags))) t)
	     (setq mp (cdr mp)
		   r-list (cdr r-list)))
	   )))

      (when do-local-expunges
	(message "Expunging messages in cache... ")
	(vm-expunge-folder t t expunge-list)
	(if (and interactive stale-list)
	    (if (y-or-n-p 
		 (format 
		  "Found %s messages with invalid UIDs.  Expunge them? "
		  (length stale-list)))
		(vm-expunge-folder t t stale-list)
	      (message "They will be labelled 'stale'")
	      (mapc 
	       (lambda (m)
		 (vm-set-labels m (cons "stale" (vm-labels-of m)))
		 (vm-set-attribute-modflag-of m t)
		 (vm-set-stuff-flag-of m t))
	       stale-list)
	      ))
	(message "Expunging messages in cache... done"))
      (when (and do-remote-expunges
		 vm-imap-messages-to-expunge)
	;; New code.  Kyle's version was piggybacking on IMAP spool
	;; file code and wasn't ideal.
	(message "Expunging messages on the server... ")
	(let ((mailbox-count (vm-folder-imap-mailbox-count))
	      (expunge-count (length vm-imap-messages-to-expunge))
	      (uid-obarray (vm-folder-imap-uid-obarray))
	      uids-to-delete m-list message e-list count)
	  ;; uids-to-delete to have UID's of all UID-valid messages in
	  ;; vm-imap-messages-to-expunge 
	  (condition-case error-data
	      (progn
		(while vm-imap-messages-to-expunge
		  (setq message (car vm-imap-messages-to-expunge))
		  (if (equal (cdr message) uid-validity)
		      (setq uids-to-delete (cons (car message) uids-to-delete)))
		  (setq vm-imap-messages-to-expunge 
			(cdr vm-imap-messages-to-expunge)))
		(if (not (equal expunge-count (length uids-to-delete)))
		    (progn
		      (message "%s stale deleted messages are ignored"
			       (- expunge-count (length uids-to-delete)))
		      (sit-for 2)))
		(setq expunge-count 0)	; number of messages expunged
		(save-excursion		; = save-current-buffer?
		  (set-buffer (process-buffer process))
		  ;;---------------------------
		  (vm-buffer-type:set 'process)
		  ;;---------------------------
		  ;; (setq uid-alist (vm-imap-get-uid-list 
		  ;;		 process 1 mailbox-count))
		  ;; m-list to have the message sequence numbers of
		  ;; messages to be expunged, in descending order.
		  ;; the message sequence numbers don't change in the
		  ;; process, according to the IMAP4 protocol
		  (setq m-list 
			(delete nil
				(mapcar 
				 (lambda (uid)
				   (let* ((key (intern uid uid-obarray)))
				     (and (boundp key)
					  (progn
					    (vm-imap-delete-message 
					     process (symbol-value key))
					    (symbol-value key)))))
				 uids-to-delete)))
		  (setq m-list (cons nil (sort m-list '>)))
					; dummy header added
		  (setq count 0)
		  (while (and (cdr m-list) (<= count vm-imap-expunge-retries))
		    ;;----------------------------------
		    (vm-imap-session-type:assert-active)
		    ;;----------------------------------
		    (vm-imap-send-command process "EXPUNGE")
		    ;;--------------------------------
		    (vm-imap-session-type:set 'active)
		    ;;--------------------------------
		    ;; e-list to have the message sequence numbers of
		    ;; messages that got expunged
		    (setq e-list (sort 
				  (vm-imap-read-expunge-response
				   process)
				  '>))
		    (setq expunge-count (+ expunge-count (length e-list)))
		    (while e-list	; for each message expunged
		      (let ((e (car e-list))
			    (pair m-list)
			    (done nil))
			(while (not done) ; remove it from m-list
			  (cond ((null (cdr pair))
				 (setq done t))
				((> (car (cdr pair)) e) 
					; decrement the message sequence
					; numbers following e in m-list
				 (rplaca (cdr pair) 
					 (- (car (cdr pair)) 1)))
				((= (car (cdr pair)) e)
				 (rplacd pair (cdr (cdr pair)))
				 (setq done t))
				((< (car (cdr pair)) e)
					; oops. somebody expunged e!?!
				 (setq done t)))
			  (setq pair (cdr pair)))
			(setq e-list (cdr e-list))))
		    ;; m-list has message sequence numbers of messages
		    ;; that haven't yet been expunged
		    (if (cdr m-list)
			(message "%s messages yet to be expunged"
				 (length (cdr m-list))))
					; try again, if the user wants us to
		    (setq count (1+ count)))
		  (message "Expunging messages on the server... done")))
	    (vm-imap-normal-error	; handler
	     (message "IMAP error: %s" (cadr error-data)))

	    (vm-imap-protocol-error	; handler
	     (message "Expunge from %s signalled: %s"
		      folder error-data))
	    (quit 			; handler
	     (error "Quit received during expunge from %s"
		    folder)))
	  ;;-----------------------------
	  (vm-buffer-type:exit)
	  (vm-imap-dump-uid-seq-num-data)
	  ;;-----------------------------
	  (vm-set-folder-imap-mailbox-count (- mailbox-count expunge-count))
	  ))
      ;; Not clear that one should end the session right away.  We
      ;; will keep it around for use with headers-only messages.
      ;; (vm-imap-end-session process)
      (setq vm-imap-connection-mode 'online)
      got-some)))

(defvar vm-imap-message-bunch-size 10
  "* Number of messages in a bunch to be used for IMAP server
operations")

(defun vm-imap-bunch-messages (seq-nums)
  ;; Given a sorted list of message sequence numbers, creates a list
  ;; of bunched message sequences, each of the form 
  ;; (begin-num . end-num)
  (let ((seqs nil)
	beg last next diff)
    (when seq-nums
      (setq beg (car seq-nums))
      (setq last beg)
      (setq seq-nums (cdr seq-nums)))
    (while seq-nums
      (setq next (car seq-nums))
      (if (and (= (- next last) 1)
	       (< (- next beg) vm-imap-message-bunch-size))
	  (setq last next)
	(setq seqs (cons (cons beg last) seqs))
	(setq beg next)
	(setq last next))
      (setq seq-nums (cdr seq-nums)))
    (setq seqs (cons (cons beg last) seqs))
    (nreverse seqs)))


(defun vm-fetch-imap-message (m)
  "Insert the message body of M in the current buffer, which must be
either the folder buffer or the presentation buffer.

 (This is a special case of vm-fetch-message, not to be confused with
 vm-imap-fetch-message.)"
  (let ((body-buffer (current-buffer))
	(statblob nil))
    (unwind-protect
	(save-excursion
	  ;;----------------------------------
	  (vm-buffer-type:enter 'folder)
	  ;;----------------------------------
	  (set-buffer (vm-buffer-of (vm-real-message-of m)))
	  (let* ((statblob nil)
		 (uid (vm-imap-uid-of m))
		 (imapdrop (vm-folder-imap-maildrop-spec))
		 (folder (or (vm-imap-folder-for-spec imapdrop)
			     (vm-safe-imapdrop-string imapdrop)))
		 (process (and (eq vm-imap-connection-mode 'online)
			       (vm-re-establish-folder-imap-session 
				imapdrop "fetch")))
		 (imap-buffer (and process (process-buffer process)))
		 (use-body-peek (vm-folder-imap-body-peek))
		 (server-uid-validity (vm-folder-imap-uid-validity))
		 (old-eob (point-max))
		 message-size
		 )

	    (when (null process)
		(if (eq vm-imap-connection-mode 'offline)
		    (error "Working in offline mode")
		  (setq vm-imap-connection-mode 'autoconnect)
		  (error (concat "Could not connect to IMAP server; "
				 "Type g to reconnect"))))
	      (setq imap-buffer (process-buffer process))
	      (unwind-protect
		  (save-excursion	; = save-current-buffer?
		    (set-buffer imap-buffer)
		    ;;----------------------------------
		    (vm-buffer-type:enter 'process)
		    (vm-imap-session-type:assert-active)
		    ;;----------------------------------
		    (condition-case error-data
			(progn
			  (setq message-size 
				(vm-imap-get-uid-message-size process uid))
			  (setq statblob (vm-imap-start-status-timer))
			  (vm-set-imap-stat-x-mailbox statblob folder)
			  (vm-set-imap-stat-x-maxmsg statblob 1)
			  (vm-set-imap-stat-x-currmsg statblob 1)
			  (vm-set-imap-stat-x-need statblob message-size)
			  (vm-imap-fetch-uid-message 
			   process uid use-body-peek nil)
			  (vm-imap-retrieve-to-target 
			   process body-buffer statblob use-body-peek)
			  (vm-imap-read-ok-response process)
			  )
		      (vm-imap-normal-error ; handler
		       (message "IMAP error: %s" (cadr error-data)))
		      (vm-imap-protocol-error ; handler
		       (message "Retrieval from %s signaled: %s" folder
				error-data)
		       ;; Continue with whatever messages have been read
		       )
		      (quit
		       (delete-region old-eob (point-max))
		       (error (format "Quit received during retrieval from %s"
				      folder)))))
		;; unwind-protections
		(when statblob
		  (vm-imap-stop-status-timer statblob))
		;;-------------------
		(vm-buffer-type:exit)
		;;-------------------
		;;-----------------------------
		(vm-imap-dump-uid-seq-num-data)
		;;-----------------------------
		)))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      )))
	 

;; The following three functions should go into vm-folder.el or vm.el
;; or some such place.

;;;###autoload
(defun vm-load-message (&optional count)
  "Load the message by retrieving its body from its
permanent location.  Currently this facility is only available for IMAP
folders.

With a prefix argument COUNT, the current message and the next 
COUNT - 1 messages are loaded.  A negative argument means
the current message and the previous |COUNT| - 1 messages are
loaded.

When invoked on marked messages (via `vm-next-command-uses-marks'),
only marked messages are loaded, other messages are ignored.  If
applied to collapsed threads in summary and thread operations are
enabled via `vm-enable-thread-operations' then all messages in the
thread are loaded."
  (interactive "p")
  (if (interactive-p)
      (vm-follow-summary-cursor))
  (vm-select-folder-buffer-and-validate 1 (interactive-p))
  (vm-error-if-folder-read-only)
  (when (null count) (setq count 1))
  (let ((mlist (vm-select-operable-messages count "Load"))
	(errors 0)
	(n 0)
	fetch-method
	m mm)
    (save-excursion
      ;; (message "Retrieving message body...")
      (while mlist
	(setq m (car mlist))
	(setq mm (vm-real-message-of m))
	(set-buffer (vm-buffer-of mm))
	(if (vm-body-retrieved-of mm)
	    (if (vm-body-to-be-discarded-of mm)
		(vm-unregister-fetched-message mm))
	  ;; else retrieve the body
	  (setq n (1+ n))
	  (message "Retrieving message body... %s" n)
	  (vm-retrieve-real-message-body mm)
	  )
	(setq mlist (cdr mlist)))
      (when (> n 0)
	(message "Retrieving message body... done")))
    (intern (buffer-name) vm-buffers-needing-display-update)
    ;; FIXME - is this needed?  Is it correct?
    (vm-display nil nil '(vm-load-message vm-refresh-message)
		(list this-command))	
    (vm-update-summary-and-mode-line)
    ))

;;;###autoload
(defun vm-retrieve-operable-messages (&optional count mlist)
  "Retrieve the message from from its permanent location for
temporary use.  Currently this facility is only available for
IMAP folders.

With a prefix argument COUNT, the current message and the next 
COUNT - 1 messages are retrieved.  A negative argument means
the current message and the previous |COUNT| - 1 messages are
retrieved.

When invoked on marked messages (via `vm-next-command-uses-marks'),
only marked messages are retrieved, other messages are ignored.  If
applied to collapsed threads in summary and thread operations are
enabled via `vm-enable-thread-operations' then all messages in the
thread are retrieved."
  (vm-select-folder-buffer-and-validate 1 (interactive-p))
  (when (null count) (setq count 1))
  (let ((used-marks (eq last-command 'vm-next-command-uses-marks))
	(vm-fetched-message-limit nil)
	(errors 0)
	(n 0)
	fetch-method
	m mm)
;;     (if (not used-marks) 
;; 	(setq mlist (list (car vm-message-pointer))))
    (unless mlist
      (setq mlist (vm-select-operable-messages count "Retrieve")))
    (save-excursion
      (while mlist
	(setq m (car mlist))
	(setq mm (vm-real-message-of m))
	(set-buffer (vm-buffer-of mm))
	(when (vm-body-to-be-retrieved-of mm)
	  (setq n (1+ n))
	  (message "Retrieving message body... %s" n)
	  (vm-retrieve-real-message-body mm)
	  (vm-register-fetched-message mm))
	(setq mlist (cdr mlist)))
      (when (> n 0)
	(message "Retrieving message body... done")))
    (intern (buffer-name) vm-buffers-needing-display-update)
    (vm-update-summary-and-mode-line)
    ))

(defun vm-retrieve-real-message-body (mm &optional fetch)
  "Retrieve the body of a real message MM from its external
source and insert it into the Folder buffer.  If the optional argument
FETCH is t, then the retrieval is for a temporary message fetch."
  (when (not (eq (vm-message-access-method-of mm) 'imap))
    (error "This is currently available only for imap folders."))
  (save-excursion
    (set-buffer (vm-buffer-of mm))
    (vm-save-restriction
     (widen)
     (narrow-to-region (marker-position (vm-headers-of mm)) 
		       (marker-position (vm-text-end-of mm)))
     (let ((fetch-method (vm-message-access-method-of mm))
	   (vm-folder-read-only (and vm-folder-read-only (not fetch)))
	   (inhibit-read-only t)
	   ;; (buffer-read-only nil)    ; seems redundant
	   (buffer-undo-list t)		; why this?  USR, 2010-06-11
	   (modified (buffer-modified-p))
	   (testing 0))
       (goto-char (vm-text-of mm))
       ;; Check to see that we are at the right place
       (vm-assert (save-excursion (forward-line -1) (looking-at "\n")))
       (vm-increment testing)

       (delete-region (point) (point-max))
       ;; Remember that this does I/O and accept-process-output,
       ;; allowing concurrent threads to run!!!  USR, 2010-07-11
       (condition-case err
	   (apply (intern (format "vm-fetch-%s-message" fetch-method))
		  mm nil)
	 (error 
	  (error "Unable to load message; %s" (error-message-string err))))
       (vm-assert (eq (point) (marker-position (vm-text-of mm))))
       (vm-increment testing)
       ;; delete the new headers
       (delete-region (vm-text-of mm)
		      (or (re-search-forward "\n\n" (point-max) t) (point-max)))
       (vm-assert (eq (point) (marker-position (vm-text-of mm))))
       (vm-increment testing)
       ;; fix markers now
       (set-marker (vm-text-end-of mm) (point-max))
       (vm-assert (eq (point) (marker-position (vm-text-of mm))))
       (vm-assert (save-excursion (forward-line -1) (looking-at "\n")))
       (vm-increment testing)
       ;; now care for the layout of the message
       (vm-set-mime-layout-of mm (vm-mime-parse-entity-safe mm))
       ;; update the message data
       (vm-set-body-to-be-retrieved-flag mm nil)
       (vm-set-body-to-be-discarded-flag mm nil)
       (vm-set-line-count-of mm nil)
       (vm-set-byte-count-of mm nil)
       ;; update the virtual messages
       (vm-update-virtual-messages mm)
       (set-buffer-modified-p modified)

       (vm-assert (eq (point) (marker-position (vm-text-of mm))))
       (vm-assert (save-excursion (forward-line -1) (looking-at "\n")))
       (vm-increment testing)))))

;;;###autoload
(defun vm-refresh-message ()
  "Reload the message body from its permanent location.  Currently
this facilty is only available for IMAP folders."
  (interactive)
  (vm-unload-message 1 t)
  (vm-load-message))

;;;###autoload
(defun vm-unload-message (&optional count physical)
  "Unload the message body, i.e., delete it from the folder
buffer.  It can be retrieved again in future from its permanent
external location.  Currently this facility is only available for
IMAP folders.

With a prefix argument COUNT, the current message and the next 
COUNT - 1 messages are unloaded.  A negative argument means
the current message and the previous |COUNT| - 1 messages are
unloaded.

When invoked on marked messages (via `vm-next-command-uses-marks'), only 
marked messages are unloaded, other messages are ignored.  If
applied to collapsed threads in summary and thread operations are
enabled via `vm-enable-thread-operations' then all messages in
the thread are unloaded.

If the optional argument PHYSICAL is non-nil, then the message is
physically discarded.  Otherwise, the discarding may be delayed until
the folder is saved."
  (interactive "p")
  (if (interactive-p)
      (vm-follow-summary-cursor))
  (vm-select-folder-buffer-and-validate 1 (interactive-p))
  (vm-error-if-folder-read-only)
  (when (null count) 
    (setq count 1))
  (let ((mlist (vm-select-operable-messages count "Unload"))
	(buffer-undo-list t)
	(errors 0)
	m mm)
    (save-excursion
      (setq count 0)
      (while mlist
	(setq m (car mlist))
	(setq mm (vm-real-message-of m))
	(set-buffer (vm-buffer-of mm))
	(when (and (vm-body-retrieved-of mm)
		   (null (vm-body-to-be-discarded-of mm)))
	  (if (and (= count 0) (not physical))
	      ;; Register the message as fetched instead of actually
	      ;; discarding the message
	      (vm-register-fetched-message mm)
	    (vm-discard-real-message-body mm)))
	(setq mlist (cdr mlist))
	(setq count (1+ count))))
    (if (= count 1) 
	(message "Message body discarded")
      (message "%d message bodies discarded" count))
    (vm-update-summary-and-mode-line)
    ))

(defun vm-discard-real-message-body (mm)
  "Discard the real message body of MM from its Folder buffer."
  (when (not (eq (vm-message-access-method-of mm) 'imap))
    (error "This is currently available only for imap folders."))
  (save-excursion
    (set-buffer (vm-buffer-of mm))
    (vm-save-restriction
     (widen)
     (let ((inhibit-read-only t)
	   ;; (buffer-read-only nil)     ; seems redundant
	   (modified (buffer-modified-p)))
       (goto-char (vm-text-of mm))
       ;; Check to see that we are at the right place
       (if (or (bobp)
	       (save-excursion (forward-line -1) (looking-at "\n")))
	   (progn
	     (delete-region (point) (vm-text-end-of mm))
	     (vm-set-buffer-modified-p t)
	     (vm-set-mime-layout-of mm nil)
	     (vm-set-body-to-be-retrieved-flag mm t)
	     (vm-set-body-to-be-discarded-flag mm nil)
	     (vm-set-line-count-of mm nil)
	     (vm-update-virtual-messages mm)
	     (set-buffer-modified-p modified))
	 (if (y-or-n-p
	      (concat "VM internal error: "
		       "headers of a message have been corrupted. "
		       "Continue? "))
	     (progn
	       (message (concat "The damaged message, with UID %s, "
				"is left in the folder")
			(vm-imap-uid-of mm))
	       (sit-for 5)
	       (vm-set-body-to-be-discarded-flag mm nil))
	   (error "Aborted operation")))
       ))))


(defun vm-imap-save-attributes (&optional interactive all-flags)
  "* Save the attributes of changed messages to the IMAP folder.
   INTERACTIVE, true if the function was invoked interactively, e.g., as
   vm-get-spooled-mail.
   ALL-FLAGS, if true says that the attributes of all messages should
   be saved to the IMAP folder, not only those of changed messages.
"
  ;;--------------------------
  (vm-buffer-type:set 'folder)
  ;;--------------------------
  (let* ((process (vm-folder-imap-process))
	 (uid-validity (vm-folder-imap-uid-validity))
	 (mp vm-message-list)
	 (errors 0))
      ;;  (perm-flags (vm-folder-imap-permanent-flags))
      (message "Updating attributes on the IMAP server... ")
      ;;-----------------------------------------
      (vm-imap-folder-session-type:assert 'valid)
      ;;-----------------------------------------
      (while mp
	(if (or all-flags (vm-attribute-modflag-of (car mp)))
	    (condition-case nil
		(vm-imap-save-message-flags process (car mp))
	      (vm-imap-protocol-error 	; handler
	       (setq errors (1+ errors))
	       (vm-buffer-type:set 'folder))))
	(setq mp (cdr mp)))
      (if (> errors 0)
	  (message "Updating attributes on the IMAP server... %d errors" errors)
	(message "Updating attributes on the IMAP server... done"))))


(defun vm-imap-synchronize (&optional all-flags)
  "Synchronize the current folder with the IMAP mailbox.
Deleted messages are not expunged.
Changes made to the buffer are uploaded to the server first before
downloading the server data.
Prefix argument ALL-FLAGS says that all the messages' flags should be
written to the server irrespective of whether they were changed in the
VM session.  This is useful for saving offline work."
  (interactive "P")
  (vm-select-folder-buffer-and-validate 0 (interactive-p))
  (vm-display nil nil '(vm-imap-synchronize) '(vm-imap-synchronize))
  (if (not (eq vm-folder-access-method 'imap))
      (message "This is not an IMAP folder")
    (if (null (vm-establish-new-folder-imap-session t "general operation"))
	nil

      (vm-imap-retrieve-uid-and-flags-data)
      (vm-imap-save-attributes all-flags)
      ;; (vm-imap-synchronize-folder t nil nil nil 
      ;; 			(if all-flags 'all t) nil)
					; save-attributes
      (vm-imap-synchronize-folder t t t t nil t)
					; interactive
					; do-remote-expunges, 
					; do-local-expunges,
					; do-retrieves and
					; retrieve-attributes 
      ;; stuff the attributes of messages that need it.
      ;; (message "Stuffing cached data...")
      ;; (vm-stuff-folder-data nil)
      ;; (message "Stuffing cached data... done")
      ;; stuff bookmark and header variable values
      (if vm-message-list
	  (progn
	    ;; get summary cache up-to-date
	    (message "Updating summary... ")
	    (vm-update-summary-and-mode-line)
	    (message "Updating summary... done")
	    ;; 	  (vm-stuff-bookmark)
	    ;; 	  (vm-stuff-pop-retrieved)
	    ;; 	  (vm-stuff-imap-retrieved)
	    ;; 	  (vm-stuff-last-modified)
	    ;; 	  (vm-stuff-header-variables)
	    ;; 	  (vm-stuff-labels)
	    ;; 	  (vm-stuff-summary)
	    ;; 	  (and vm-message-order-changed
	    ;; 	       (vm-stuff-message-order))
	    )))))
  

;;;###autoload
(defun vm-imap-folder-check-for-mail (&optional interactive)
  "Check if there is new mail in th current IMAP folder.  The optional
argument INTERACTIVE says if the function is being invoked
interactively."
  ;;--------------------------
  (vm-buffer-type:set 'folder)
  ;;--------------------------
  (if (or vm-global-block-new-mail
	  (null (vm-establish-new-folder-imap-session interactive "checkmail")))
      nil
    (let ((result (car (vm-imap-get-synchronization-data))))
      (vm-imap-end-session (vm-folder-imap-process))
      result )))


;; ---------------------------------------------------------------------------
;; Utilities for maildrop specs  (this should be moved up top)
;;
;; A maildrop spec is of the form
;;      protocol:hostname:port:mailbox:auth:loginid:password 
;;             0        1    2       3    4       5        6
;; vm-imap-find-spec-for-buffer: (buffer) -> maildrop-spec
;; vm-imap-make-filename-for-spec: (maildrop-spec) -> string
;; vm-imap-normalize-spec: (maildrop-spec) -> maildrop-spec
;; vm-imap-account-name-for-spec: (maildrop-spec) -> string
;; vm-imap-spec-for-account: (string) -> maildrop-spec
;; vm-imap-parse-spec-to-list: (maildrop-spec) -> string list
;; vm-imap-spec-list-to-host-alist: 
;;	(maildrop-spec list) -> (string, maildrop-spec) alist
;; ---------------------------------------------------------------------------

;; ----------- missing functions-----------
;;;###autoload
(defun vm-imap-find-name-for-spec (spec)
  "This is a stub for a function that has not been defined."
  (error "vm-imap-find-name-for-spec has not been defined.  Please report it."
	 ))
;;-----------------------------------------

;;;###autoload
(defun vm-imap-find-spec-for-buffer (buffer)
  "Find the IMAP maildrop spec for the folder BUFFER."
  (with-current-buffer buffer
    (vm-folder-imap-maildrop-spec)))
;;   (let ((list (mapcar 'car vm-imap-account-alist))
;; 	(done nil)
;; 	(spec-items nil))
;;     (while (and (not done) list)
;;       (setq spec-items (vm-imap-parse-spec-to-list (car list)))
;;       (setcar (nthcdr 3 spec-items) folder)
;;       (if (eq buffer (vm-get-file-buffer 
;; 		      (vm-imap-make-filename-for-spec
;; 		       (mapconcat 'identity spec-items ":"))))
;; 	  (setq done t)
;; 	(setq list (cdr list))))
;;     (and list (car list)))

;;;###autoload
(defun vm-imap-make-filename-for-spec (spec)
  "Returns a cache file name appropriate for the IMAP maildrop
specification SPEC."
  (let (md5)
    (setq spec (vm-imap-normalize-spec spec))
    (setq md5 (vm-md5-string spec))
    (expand-file-name (concat "imap-cache-" md5)
		      (or vm-imap-folder-cache-directory
			  vm-folder-directory
			  (getenv "HOME")))))

;;;###autoload
(defun vm-imap-normalize-spec (spec)
  (let (comps)
    (setq comps (vm-imap-parse-spec-to-list spec))
    (setcar (vm-last comps) "*")		; scrub password
    (setcar comps "imap")		; standardise protocol name
    (setcar (nthcdr 2 comps) "*")	; scrub portnumber
    (setcar (nthcdr 4 comps) "*")	; scrub authentication method
    (setq spec (mapconcat (function identity) comps ":"))
    spec ))

;;;###autoload
(defun vm-imap-account-name-for-spec (spec)
  "Returns the IMAP account name for maildrop specification SPEC, by
looking up `vm-imap-account-alist' or nil if there is no such account."
  (let (comps account-comps (alist vm-imap-account-alist))
    (setq comps (vm-imap-parse-spec-to-list spec))
    (catch 'return
    (while alist
      (setq account-comps (vm-imap-parse-spec-to-list (car (car alist))))
      (if (and (equal (nth 1 comps) (nth 1 account-comps)) ; host
	       (equal (nth 4 comps) (nth 4 account-comps))) ; login
	  (throw 'return (cadr (car alist)))
	(setq alist (cdr alist))))
    nil)))

;;;###autoload
(defun vm-imap-folder-for-spec (spec)
  "Returns the IMAP folder for maildrop specification SPEC in the
format account:mailbox."
  (let (comps account-comps (alist vm-imap-account-alist))
    (setq comps (vm-imap-parse-spec-to-list spec))
    (catch 'return
    (while alist
      (setq account-comps (vm-imap-parse-spec-to-list (car (car alist))))
      (if (and (equal (nth 1 comps) (nth 1 account-comps)) ; host
	       (equal (nth 4 comps) (nth 4 account-comps))) ; login
	  (throw 'return (concat (cadr (car alist)) ":" (nth 3 comps)))
	(setq alist (cdr alist))))
    nil)))

;;;###autoload
(defun vm-imap-spec-for-account (account)
  "Returns the IMAP maildrop spec for ACCOUNT, by looking up
`vm-imap-account-alist' or nil if there is no such account."
  (car (rassoc (list account) vm-imap-account-alist)))

;;;###autoload
(defun vm-imap-parse-spec-to-list (spec)
  "Parses the IMAP maildrop specification SPEC and returns a list of
its components."
  (vm-parse spec "\\([^:]+\\):?" 1 6))

(defun vm-imap-spec-list-to-host-alist (spec-list)
  (let (host-alist spec host)
    (while spec-list
      (setq spec (vm-imapdrop-sans-password-and-mailbox (car spec-list)))
      (setq host-alist (cons
			(list
			 (nth 1 (vm-imap-parse-spec-to-list spec))
			 spec)
			host-alist)
	    spec-list (cdr spec-list)))
    host-alist ))

(defvar vm-imap-account-folder-cache nil
  "Caches the list of all folders on an IMAP account.")

(defun vm-imap-folder-completion-list (string predicate flag)
  ;; selectable-only is used via dynamic binding
  (let ((completion-list (mapcar (lambda (a) (list (concat (cadr a) ":")))
				 vm-imap-account-alist))
	folder account spec process mailbox-list)
    
    ;; check for account 
    (setq folder (try-completion (or string "") completion-list predicate))
    
    ;; get folders of this account
    (if (stringp folder)
	(setq account (car (vm-parse folder "\\([^:]+\\):?" 1)))
      (setq account (car (vm-parse string "\\([^:]+\\):?" 1))))
    
    (when account
      (setq mailbox-list (cdr (assoc account vm-imap-account-folder-cache)))
      (setq spec (vm-imap-spec-for-account account))
      (when (and (null mailbox-list) spec)
	(unwind-protect
	    (progn
	      (setq process (vm-imap-make-session spec t "folders"))
	      (when process
		(setq mailbox-list 
		      (vm-imap-mailbox-list process selectable-only))
		(when mailbox-list
		  (add-to-list 'vm-imap-account-folder-cache 
			       (cons account mailbox-list)))))
	  ;; unwind-protection
	  (when process (vm-imap-end-session process))))
      (setq completion-list 
	    (mapcar '(lambda (m) (list (format "%s:%s" account m))) mailbox-list))
      (setq folder (try-completion (or string "") completion-list predicate)))
    
    (setq folder (or folder ""))
    (if (eq folder t)
	(setq folder string))
    (cond ((null flag)
	   folder)
	  ((or (eq t flag) (string= " " folder))
	   (mapcar 'car completion-list))
	  ((eq 'lambda flag)
	   (try-completion folder completion-list predicate)))))

;;;###autoload
(defun vm-read-imap-folder-name (prompt &optional selectable-only
					newone default) 
  "Read an IMAP folder name in the format account:mailbox, return an
IMAP mailbox spec." 
  (let* (folder-input completion-list spec process list 
	 default-account default-folder
	 (vm-imap-ok-to-ask t)
	 (account-list (mapcar 'cadr vm-imap-account-alist))
	 account-and-folder account folder mailbox-list)
    (if (null account-list)
	(error "No known IMAP accounts.  Please set vm-imap-account-alist."))
    (if default 
	(setq list (vm-imap-parse-spec-to-list default)
	      default-account 
	      (cadr (assoc (vm-imapdrop-sans-password-and-mailbox default)
			   vm-imap-account-alist))
	      default-folder (nth 3 list))
      (setq default-account vm-last-visit-imap-account))
    (setq folder-input
	  (completing-read
	   (format			; prompt
;;	    "IMAP folder:%s " 
	    "%s%s" prompt
	    (if (and default-account default-folder)
		(format "(default %s:%s) " default-account default-folder)
	      ""))
	   'vm-imap-folder-completion-list
	   nil				; predicate
	   nil				; require-match
	   (if default-account		; initial-input
	       (format "%s:" default-account)
	     "")))
    (if (or (equal folder-input "")  
	    (equal folder-input (format "%s:" default-account)))
	(if (and default-account default-folder)
	    (setq folder-input (format "%s:%s" default-account default-folder))
	  (error 
	   "IMAP folder required in the format account-name:folder-name"))) 
    (setq account-and-folder (vm-parse folder-input "\\([^:]+\\):?" 1 2)
	  account (car account-and-folder)
	  folder (cadr account-and-folder)
	  spec (vm-imap-spec-for-account account))
    (if (null folder)
	(error 
	 "IMAP folder required in the format account-name:folder-name"))
    (if (null spec)
	(error "Unknown IMAP account %s" account))
    (setq list (vm-imap-parse-spec-to-list spec))
    (setcar (nthcdr 3 list) folder)
    (setq vm-last-visit-imap-account account)
    (mapconcat 'identity list ":")))

(defun vm-imap-directory-separator (process ref)
  (let ((c-list nil)
	sep p r response need-ok)
    (vm-imap-check-connection process)
    (save-excursion			; = save-current-buffer?
      (set-buffer (process-buffer process))
      ;;----------------------------------
      (vm-buffer-type:enter 'process)
      (vm-imap-session-type:assert-active)
      ;;----------------------------------
      (vm-imap-send-command process (format "LIST %s \"\""
					    (vm-imap-quote-string ref)))
      ;;--------------------------------
      (vm-imap-dump-uid-seq-num-data)
      ;;--------------------------------
      (setq need-ok t)
      (while need-ok
	(setq response (vm-imap-read-response-and-verify process "LIST"))
	(cond ((vm-imap-response-matches response 'VM 'OK)
	       (setq need-ok nil))
	      ((vm-imap-response-matches response '* 'LIST 'list 'string)
	       (setq r (nthcdr 3 response)
		     p (car r)
		     sep (buffer-substring (nth 1 p) (nth 2 p))))
	      ((vm-imap-response-matches response '* 'LIST 'list)
	       (vm-imap-protocol-error "unexpedcted LIST response"))))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      sep )))

(defun vm-imap-mailbox-list (process selectable-only)
  "Query the IMAP PROCESS to get a list of the mailboxes (folders)
available in the IMAP account.  SELECTABLE-ONLY flag asks only
selectable mailboxes to be listed.  Returns a list of mailbox names."
  (let ((c-list nil)
	p r response need-ok)
    (vm-imap-check-connection process)
    (save-excursion			; = save-current-buffer?
      (set-buffer (process-buffer process))
      ;;----------------------------------
      (vm-buffer-type:enter 'process)
      (vm-imap-session-type:assert-active)
      (vm-imap-dump-uid-seq-num-data)
      ;;----------------------------------
      (vm-imap-send-command process "LIST \"\" \"*\"")
      (setq need-ok t)
      (while need-ok
	(setq response (vm-imap-read-response-and-verify process "LIST"))
	(cond ((vm-imap-response-matches response 'VM 'OK)
	       (setq need-ok nil))
	      ((vm-imap-response-matches response '* 'LIST 'list)
	       (setq r (nthcdr 2 response)
		     p (car r))
	       (if (and selectable-only
			(vm-imap-scan-list-for-flag p "\\Noselect"))
		   nil
		 (setq r (nthcdr 4 response)
		       p (car r))
		 (if (memq (car p) '(atom string))
		     (setq c-list (cons (buffer-substring (nth 1 p) (nth 2 p))
					c-list)))))))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      c-list )))

;; This is unfinished
(defun vm-imap-mailbox-p (process mailbox selectable-only)
  "Query the IMAP PROCESS to check if MAILBOX exists as a folder.
SELECTABLE-ONLY flag asks whether the mailbox is selectable as
well. Returns a boolean value."
  (let ((c-list nil)
	p r response need-ok)
    (vm-imap-check-connection process)
    (save-excursion			; = save-current-buffer?
      (set-buffer (process-buffer process))
      ;;----------------------------------
      (vm-buffer-type:enter 'process)
      (vm-imap-session-type:assert-active)
      (vm-imap-dump-uid-seq-num-data)
      ;;----------------------------------
      (vm-imap-send-command process (concat "LIST \"\" \"" mailbox "\""))
      (setq need-ok t)
      (while need-ok
	(setq response (vm-imap-read-response-and-verify process "LIST"))
	(cond ((vm-imap-response-matches response 'VM 'OK)
	       (setq need-ok nil))
	      ((vm-imap-response-matches response '* 'LIST 'list)
	       (setq r (nthcdr 2 response)
		     p (car r))
	       (if (and selectable-only
			(vm-imap-scan-list-for-flag p "\\Noselect"))
		   nil
		 (setq r (nthcdr 4 response)
		       p (car r))
		 (if (memq (car p) '(atom string))
		     (setq c-list (cons (buffer-substring (nth 1 p) (nth 2 p))
					c-list)))))))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      c-list )))

(defun vm-imap-read-boolean-response (process)
  (let ((need-ok t) retval response)
    (while need-ok
      (vm-imap-check-connection process)
      (setq response (vm-imap-read-response process))
      (cond ((vm-imap-response-matches response 'VM 'OK)
	     (setq need-ok nil retval t))
	    ((vm-imap-response-matches response 'VM 'NO)
	     (setq need-ok nil retval nil))
	    ((vm-imap-response-matches response '* 'BYE)
	     (vm-imap-normal-error "server disconnected"))
	    ((vm-imap-response-matches response 'VM 'BAD)
	     (vm-imap-normal-error "server said BAD"))))
    retval ))

(defun vm-imap-create-mailbox (process mailbox
			       &optional dont-create-parent-directories)
  (if (not dont-create-parent-directories)
      (let (dir sep sep-regexp i)
	(setq sep (vm-imap-directory-separator process "")
	      sep-regexp (regexp-quote sep)
	      i 0)
	(while (string-match sep-regexp mailbox i)
	  (setq dir (substring mailbox i (match-end 0)))
	  (vm-imap-create-mailbox process dir t)
	  ;; ignore command result since creating a directory will
	  ;; routinely fail with "File exists".  We'll generate a
	  ;; real error if the final mailbox creation fails.
	  (vm-imap-read-boolean-response process)
	  (setq i (match-end 0)))))
  (vm-imap-send-command process (format "CREATE %s"
					(vm-imap-quote-string mailbox)))
  (if (null (vm-imap-read-boolean-response process))
      (vm-imap-normal-error "creation of %s failed" mailbox)))

(defun vm-imap-delete-mailbox (process mailbox)
  (vm-imap-send-command process (format "DELETE %s"
					(vm-imap-quote-string mailbox)))
  (if (null (vm-imap-read-boolean-response process))
      (vm-imap-normal-error "deletion of %s failed" mailbox)))

(defun vm-imap-rename-mailbox (process source dest)
  (vm-imap-send-command process (format "RENAME %s %s"
					(vm-imap-quote-string source)
					(vm-imap-quote-string dest)))
  (if (null (vm-imap-read-boolean-response process))
      (vm-imap-normal-error "renaming of %s to %s failed" source dest)))

;;;###autoload
(defun vm-create-imap-folder (folder)
  "Create a folder on an IMAP server.
First argument FOLDER is read from the minibuffer if called
interactively.  Non-interactive callers must provide an IMAP
maildrop specification for the folder as described in the
documentation for `vm-spool-files'."
;; Creates a self-contained IMAP session and destroys it at the end.
  (interactive
   (save-excursion
     ;;------------------------
     (vm-buffer-type:duplicate)
     ;;------------------------
     (vm-session-initialization)
     ;; (vm-check-for-killed-folder) 	; seems no need for this
     ;; (vm-select-folder-buffer-if-possible)
     (let ((this-command this-command)
	   (last-command last-command)
	   (folder (vm-read-imap-folder-name "Create IMAP folder: " nil t)))
       ;;-------------------
       (vm-buffer-type:exit)
       ;;-------------------
       (list folder))
     ))
  (let ((vm-imap-ok-to-ask t)
	process mailbox folder-display)
    (setq process (vm-imap-make-session folder t "create"))
    (if (null process)
	(error "Couldn't open IMAP session for %s"
	       (vm-safe-imapdrop-string folder)))
    (save-current-buffer
      ;;-----------------------------
      (vm-buffer-type:enter 'process)
      ;;-----------------------------
      (set-buffer (process-buffer process))
      (setq mailbox (nth 3 (vm-imap-parse-spec-to-list folder)))
      (setq folder-display (or (vm-imap-folder-for-spec folder)
			       (vm-safe-imapdrop-string folder)))
      (vm-imap-create-mailbox process mailbox t)
      (message "Folder %s created" folder-display)
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      (when (and (processp process)
		 (memq (process-status process) '(open run)))
	(vm-imap-end-session process)))
    ))

;;;###autoload
(defun vm-delete-imap-folder (folder)
  "Delete a folder on an IMAP server.
First argument FOLDER is read from the minibuffer if called
interactively.  Non-interactive callers must provide an IMAP
maildrop specification for the folder as described in the
documentation for `vm-spool-files'."
;; Creates a self-contained IMAP session and destroys it at the end.
  (interactive
   (save-excursion
     ;;------------------------
     (vm-buffer-type:duplicate)
     ;;------------------------
     (vm-session-initialization)
     ;; (vm-check-for-killed-folder)	; seems no need for this
     ;; (vm-select-folder-buffer-if-possible)
     (let ((this-command this-command)
	   (last-command last-command))
       (list (vm-read-imap-folder-name "Delete IMAP folder: " nil nil)))))
  (let ((vm-imap-ok-to-ask t)
	process mailbox folder-display)
    (setq process (vm-imap-make-session folder t "delete folder"))
    (if (null process)
	(error "Couldn't open IMAP session for %s"
	       (vm-safe-imapdrop-string folder)))
    (save-current-buffer
      ;;-----------------------------
      (vm-buffer-type:enter 'process)
      ;;-----------------------------
      (set-buffer (process-buffer process))
      (setq mailbox (nth 3 (vm-imap-parse-spec-to-list folder)))
      (setq folder-display (or (vm-imap-folder-for-spec folder)
			       (vm-safe-imapdrop-string folder)))
      (vm-imap-delete-mailbox process mailbox)
      (message "Folder %s deleted" folder-display)
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      (when (and (processp process)
		 (memq (process-status process) '(open run)))
	(vm-imap-end-session process))
      )))

;;;###autoload
(defun vm-rename-imap-folder (source dest)
  "Rename a folder on an IMAP server.
Argument SOURCE and DEST are read from the minibuffer if called
interactively.  Non-interactive callers must provide full IMAP
maildrop specifications for SOURCE and DEST as described in the
documentation for `vm-spool-files'."
;; Creates a self-contained IMAP session and destroys it at the end.
  (interactive
   (save-excursion
     ;;------------------------
     (vm-buffer-type:duplicate)
     ;;------------------------
     (vm-session-initialization)
     ;; (vm-check-for-killed-folder)	; seems no need for this
     ;; (vm-select-folder-buffer-if-possible)
     (let ((this-command this-command)
	   (last-command last-command)
	   source dest)
       (setq source (vm-read-imap-folder-name "Rename IMAP folder: " t nil))
       (setq dest (vm-read-imap-folder-name
		   (format "Rename %s to: " (vm-safe-imapdrop-string source))
		    nil t))
       (list source dest))))
  (let ((vm-imap-ok-to-ask t)
	process mailbox-source mailbox-dest)
    (setq process (vm-imap-make-session source t "rename folder"))
    (if (null process)
	(error "Couldn't open IMAP session for %s"
	       (vm-safe-imapdrop-string source)))
    (save-current-buffer
      ;;-----------------------------
      (vm-buffer-type:enter 'process)
      ;;-----------------------------
      (set-buffer (process-buffer process))
      (setq mailbox-source (nth 3 (vm-imap-parse-spec-to-list source)))
      (setq mailbox-dest (nth 3 (vm-imap-parse-spec-to-list dest)))
      (vm-imap-rename-mailbox process mailbox-source mailbox-dest)
      (message "Folder %s renamed to %s" 
	       (or (vm-imap-folder-for-spec source)
		   (vm-safe-imapdrop-string source))
	       (or (vm-imap-folder-for-spec dest)
		   (vm-safe-imapdrop-string dest)))
      ;;-------------------
      (vm-buffer-type:exit)
      ;;-------------------
      (when (and (processp process)
		 (memq (process-status process) '(open run)))
	(vm-imap-end-session process))
      )))

;;;###autoload
(defun vm-list-imap-folders (account)
  "List all folders on an IMAP account ACCOUNT.  The account must be
one declared in `vm-imap-account-alist'."
;; Creates a self-contained IMAP session and destroys it at the end.
  (interactive
   (save-excursion
     ;;------------------------
     (vm-buffer-type:duplicate)
     ;;------------------------
     (vm-session-initialization)
     (let ((this-command this-command)
	   (last-command last-command)
	   (completion-list (mapcar (function cadr) vm-imap-account-alist)))
       (list (completing-read 
	      "IMAP account: " completion-list nil t
	      (if vm-last-visit-imap-account		; initial-input
		  (format "%s" vm-last-visit-imap-account)
		"")
	      )))))
  (setq vm-last-visit-imap-account account)
  (let ((vm-imap-ok-to-ask t)
	folder spec process mailbox-list buffer)
    (setq spec (vm-imap-spec-for-account account))
    (setq process (and spec (vm-imap-make-session spec t "folders")))
    (if (null process)
	(error "Couldn't open IMAP session for %s"
	       (vm-safe-imapdrop-string account)))
    (unwind-protect
	(progn
	  (setq mailbox-list 
		(vm-imap-mailbox-list process nil))
	  (when mailbox-list
	    (add-to-list 'vm-imap-account-folder-cache 
			 (cons account mailbox-list))))
      ;; unwind-protection
      (when process (vm-imap-end-session process)))
    (setq mailbox-list (sort mailbox-list (function string-lessp)))
    (setq buffer (get-buffer-create (format "%s folders" account)))
    (save-current-buffer
      (set-buffer buffer)
      (erase-buffer)
      (while mailbox-list
	(insert (format "%s\n" (car mailbox-list)))
	(setq mailbox-list (cdr mailbox-list))))
    (switch-to-buffer-other-window buffer)
    ))

;;; Robert Fenk's draft function for saving messages to IMAP folders.

;;;###autoload
(defun vm-imap-save-composition ()
  "Saves the current composition in the IMAP folder given by the
IMAP-FCC header. 
Add this to your `mail-send-hook' and start composing from an IMAP
folder." 
;; Creates a self-contained IMAP session and destroys it at the end.
  (let ((mailbox (vm-mail-get-header-contents "IMAP-FCC:"))
	(mailboxes nil)
	(fcc-string (vm-mail-get-header-contents "FCC:" ","))
	fcc-list fcc maildrop spec-list 
	process flags response string m
	(vm-imap-ok-to-ask t))
    (if (null mailbox)
	(setq mailboxes nil)
      ;; IMAP-FCC header present
      (when vm-mail-buffer		; has parent folder
	(save-current-buffer
	  ;;----------------------------
	  (vm-buffer-type:enter 'folder)
	  ;;----------------------------
	  (vm-select-folder-buffer)
	  (setq m (car vm-message-pointer))
	  (when m 
	    (set-buffer (vm-buffer-of (vm-real-message-of m))))
	  (if (eq vm-folder-access-method 'imap)
	      (setq maildrop (vm-folder-imap-maildrop-spec)))
	  ;;-------------------
	  (vm-buffer-type:exit)
	  ;;-------------------
	  ))
      (when (and (null maildrop)  vm-imap-default-account)
	(setq maildrop 
	      (vm-imap-spec-for-account vm-imap-default-account)))
      (when (null maildrop)
	(error "Set `vm-imap-default-account' to use IMAP-FCC"))
      (setq process 
	    (vm-imap-make-session maildrop t "IMAP-FCC"))
      (setq mailboxes (list (cons mailbox process)))
      (vm-mail-mode-remove-header "IMAP-FCC:"))

    (when fcc-string
      (setq fcc-list (vm-parse fcc-string "\\([^,]+\\),?"))
      (while fcc-list
	(setq fcc (car fcc-list))
	(setq spec-list (vm-parse fcc "\\([^:]+\\):?"))
	(when (member (car spec-list) '("imap" "imap-ssl" "imap-ssh"))
	  (setq process (vm-imap-make-session fcc nil "IMAP-FCC"))
	  (if (null process)
	      (error "Could not connect to the IMAP server"))
	  (setq mailboxes (cons (cons (nth 3 spec-list) process) 
				mailboxes)))
	(setq fcc-list (cdr fcc-list))))
    
    (goto-char (point-min))
    (re-search-forward (concat "^" (regexp-quote mail-header-separator) "$"))
    (setq string (concat (buffer-substring (point-min) (match-beginning 0))
			 (buffer-substring
			  (match-end 0) (point-max))))
    
    (while mailboxes
      (setq mailbox (car (car mailboxes)))
      (setq process (cdr (car mailboxes)))
      (unwind-protect
	  (save-excursion	       ; = save-current-buffer?
	    ;;-----------------------------
	    (vm-buffer-type:enter 'process)
	    ;;-----------------------------
	    ;; this can go awry if the process has died...
	    (set-buffer (process-buffer process))
	    (condition-case nil
		(vm-imap-create-mailbox process mailbox)
	      (vm-imap-protocol-error 	; handler
	       (vm-buffer-type:set 'process))) ; ignore errors
	    ;;----------------------------------
	    ;; (vm-imap-session-type:assert-active)
	    ;;----------------------------------

	    (vm-imap-send-command process
				  (format "APPEND %s %s {%d}"
					  (vm-imap-quote-string mailbox)
					  (if flags flags "()")
					  (length string)))
	    ;; could these be done with vm-imap-read-boolean-response?
	    (let ((need-plus t) response)
	      (while need-plus
		(setq response (vm-imap-read-response process))
		(cond ((vm-imap-response-matches response 'VM 'NO)
		       (vm-imap-normal-error "server said NO"))
		      ((vm-imap-response-matches response 'VM 'BAD)
		       (vm-imap-normal-error "server said BAD"))
		      ((vm-imap-response-matches response '* 'BYE)
		       (vm-imap-normal-error "server disconnected"))
		      ((vm-imap-response-matches response '+)
		       (setq need-plus nil)))))

	    (vm-imap-send-command process string nil t)
	    (let ((need-ok t) response)
	      (while need-ok

		(setq response (vm-imap-read-response process))
		(cond
		 ((vm-imap-response-matches response 'VM 'NO)
		  (vm-imap-protocol-error "server said NO to APPEND data"))
		 ((vm-imap-response-matches response 'VM 'BAD)
		  (vm-imap-protocol-error "server said BAD to APPEND data"))
		 ((vm-imap-response-matches response '* 'BYE)
		  (vm-imap-protocol-error "server said BYE to APPEND data"))
		 ((vm-imap-response-matches response 'VM 'OK)
		  (setq need-ok nil)))))
	    ;;-------------------
	    (vm-buffer-type:exit)
	    ;;-------------------
	    )
	(when (and (processp process)
		   (memq (process-status process) '(open run)))
	  (vm-imap-end-session process)))
      (setq mailboxes (cdr mailboxes)))
    ))

(defun vm-imap-start-bug-report ()
  "Begin to compose a bug report for IMAP support functionality."
  (interactive)
  (vm-follow-summary-cursor)
  (vm-select-folder-buffer-and-validate 0 (interactive-p))
  (setq vm-kept-imap-buffers nil)
  (setq vm-imap-keep-trace-buffer t)
  (setq vm-imap-keep-failed-trace-buffers 20))

(defun vm-imap-submit-bug-report ()
  "Submit a bug report for VM's IMAP support functionality.  
It is necessary to run vm-imap-start-bug-report before the problem
occurrence and this command after the problem occurrence, in
order to capture the trace of IMAP sessions during the occurrence."
  (interactive)
  (vm-follow-summary-cursor)
  (vm-select-folder-buffer-and-validate 0 (interactive-p))
  (if (or vm-imap-keep-trace-buffer
	  (y-or-n-p "Did you run vm-imap-start-bug-report earlier? "))
      (message "Thank you. Preparing the bug report... ")
    (message "Consider running vm-imap-start-bug-report before the problem occurrence"))
  (let ((process (vm-folder-imap-process)))
    (if process
	(vm-imap-end-session (vm-folder-imap-process))))
  (let ((trace-buffer-hook
	 '(lambda ()
	    (let ((bufs vm-kept-imap-buffers) 
		  buf)
	      (insert "\n\n")
	      (insert "IMAP Trace buffers - most recent first\n\n")
	      (while bufs
		(setq buf (car bufs))
		(insert "----") 
		(insert (format "%s" buf))
		(insert "----------\n")
		(insert (with-current-buffer buf
			  (buffer-string)))
		(setq bufs (cdr bufs)))
	      (insert "--------------------------------------------------\n"))
	    )))
    (vm-submit-bug-report nil (list trace-buffer-hook))
  ))


(defun vm-imap-set-default-attributes (m)
  (vm-set-headers-to-be-retrieved-of m nil)
  (vm-set-body-to-be-retrieved-of m vm-load-headers-only)
  (vm-set-body-to-be-discarded-of m nil))

(defun vm-imap-unset-body-retrieve ()
  "Unset the body-to-be-retrieved flag of all the messages.  May
  be needed if the folder has become corrupted somehow."
  (interactive)
  (save-current-buffer
   (vm-select-folder-buffer-and-validate 0 (interactive-p))
   (let ((mp vm-message-list))
     (while mp
       (vm-set-body-to-be-retrieved-of (car mp) nil)
       (vm-set-body-to-be-discarded-of (car mp) nil)
       (setq mp (cdr mp))))
   (message "Marked %s messages as having retrieved bodies" 
	    (length vm-message-list))
   ))

(defun vm-imap-unset-byte-counts ()
  "Unset the byte counts of all the messages, so that the size of the
downloaded bodies will be displayed."
  (interactive)
  (save-current-buffer
   (vm-select-folder-buffer-and-validate 0 (interactive-p))
   (let ((mp vm-message-list))
     (while mp
       (vm-set-byte-count-of (car mp) nil)
       (setq mp (cdr mp))))
   (message "Unset the byte counts of %s messages" 
	    (length vm-message-list))
   ))


;;; vm-imap.el ends here