summaryrefslogtreecommitdiff
path: root/imap/src/osdep/unix/mix.c
blob: 20d2b0126faaad9c29d14e12cbd80bc36f738fa7 (plain)
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
/* ========================================================================
 * Copyright 2008-2012 Mark Crispin
 * ========================================================================
 */

/*
 * Program:	MIX mail routines
 *
 * Author(s):	Mark Crispin
 *
 * Date:	1 March 2006
 * Last Edited:	15 February 2012
 *
 * Previous versions of this file were
 *
 * Copyright 1988-2008 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 */


#include <stdio.h>
#include <ctype.h>
#include <errno.h>
extern int errno;		/* just in case */
#include "mail.h"
#include "osdep.h"
#include <pwd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "misc.h"
#include "dummy.h"
#include "fdstring.h"

/* MIX definitions */

#define MEGABYTE (1024*1024)

#define MIXDATAROLL MEGABYTE	/* size at which we roll to a new file */


/* MIX files */

#define MIXNAME ".mix"		/* prefix for all MIX file names */
#define MIXMETA "meta"		/* suffix for metadata */
#define MIXINDEX "index"	/* suffix for index */
#define MIXSTATUS "status"	/* suffix for status */
#define MIXSORTCACHE "sortcache"/* suffix for sortcache */
#define METAMAX (MEGABYTE-1)	/* maximum metadata file size (sanity check) */


/* MIX file formats */

				/* sequence format (all but msg files) */
#define SEQFMT "S%08lx\015\012"
				/* metadata file format */
#define MTAFMT "V%08lx\015\012L%08lx\015\012N%08lx\015\012"
				/* index file record format */
#define IXRFMT ":%08lx:%04d%02d%02d%02d%02d%02d%c%02d%02d:%08lx:%08lx:%08lx:%08lx:%08lx:\015\012"
				/* status file record format */
#define STRFMT ":%08lx:%08lx:%04x:%08lx:\015\012"
				/* message file header format */
#define MSRFMT "%s%08lx:%04d%02d%02d%02d%02d%02d%c%02d%02d:%08lx:\015\012"
#define MSGTOK ":msg:"
#define MSGTSZ (sizeof(MSGTOK)-1)
				/* sortcache file record format */
#define SCRFMT ":%08lx:%08lx:%08lx:%08lx:%08lx:%c%08lx:%08lx:%08lx:\015\012"

/* MIX I/O stream local data */
	
typedef struct mix_local {
  unsigned long curmsg;		/* current message file number */
  unsigned long newmsg;		/* current new message file number */
  time_t lastsnarf;		/* last snarf time */
  int msgfd;			/* file description of current msg file */
  int mfd;			/* file descriptor of open metadata */
  unsigned long metaseq;	/* metadata sequence */
  char *index;			/* mailbox index name */
  unsigned long indexseq;	/* index sequence */
  char *status;			/* mailbox status name */
  unsigned long statusseq;	/* status sequence */
  char *sortcache;		/* mailbox sortcache name */
  unsigned long sortcacheseq;	/* sortcache sequence */
  unsigned char *buf;		/* temporary buffer */
  unsigned long buflen;		/* current size of temporary buffer */
  unsigned int expok : 1;	/* non-zero if expunge reports OK */
  unsigned int internal : 1;	/* internally opened, do not validate */
} MIXLOCAL;


#define MIXBURP struct mix_burp

MIXBURP {
  unsigned long fileno;		/* message file number */
  char *name;			/* message file name */
  SEARCHSET *tail;		/* tail of ranges */
  SEARCHSET set;		/* set of retained ranges */
  MIXBURP *next;		/* next file to burp */
};


/* Convenient access to local data */

#define LOCAL ((MIXLOCAL *) stream->local)

/* Function prototypes */

DRIVER *mix_valid (char *name);
long mix_isvalid (char *name,char *meta);
void *mix_parameters (long function,void *value);
long mix_dirfmttest (char *name);
void mix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
long mix_scan_contents (char *name,char *contents,unsigned long csiz,
			unsigned long fsiz);
void mix_list (MAILSTREAM *stream,char *ref,char *pat);
void mix_lsub (MAILSTREAM *stream,char *ref,char *pat);
long mix_subscribe (MAILSTREAM *stream,char *mailbox);
long mix_unsubscribe (MAILSTREAM *stream,char *mailbox);
long mix_create (MAILSTREAM *stream,char *mailbox);
long mix_delete (MAILSTREAM *stream,char *mailbox);
long mix_rename (MAILSTREAM *stream,char *old,char *newname);
int mix_rselect (struct direct *name);
MAILSTREAM *mix_open (MAILSTREAM *stream);
void mix_close (MAILSTREAM *stream,long options);
void mix_abort (MAILSTREAM *stream);
char *mix_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
		  long flags);
long mix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
void mix_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags);
unsigned long *mix_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
			 SORTPGM *pgm,long flags);
THREADNODE *mix_thread (MAILSTREAM *stream,char *type,char *charset,
			SEARCHPGM *spg,long flags);
long mix_ping (MAILSTREAM *stream);
void mix_check (MAILSTREAM *stream);
long mix_expunge (MAILSTREAM *stream,char *sequence,long options);
int mix_select (struct direct *name);
int mix_msgfsort (const void *d1,const void *d2);
long mix_addset (SEARCHSET **set,unsigned long start,unsigned long size);
long mix_burp (MAILSTREAM *stream,MIXBURP *burp,unsigned long *reclaimed);
long mix_burp_check (SEARCHSET *set,size_t size,char *file);
long mix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,
	       long options);
long mix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
long mix_append_msg (MAILSTREAM *stream,FILE *f,char *flags,MESSAGECACHE *delt,
		     STRING *msg,SEARCHSET *set,unsigned long seq);

FILE *mix_parse (MAILSTREAM *stream,FILE **idxf,long iflags,long sflags);
char *mix_meta_slurp (MAILSTREAM *stream,unsigned long *seq);
long mix_meta_update (MAILSTREAM *stream);
long mix_index_update (MAILSTREAM *stream,FILE *idxf,long flag);
long mix_status_update (MAILSTREAM *stream,FILE *statf,long flag);
FILE *mix_data_open (MAILSTREAM *stream,int *fd,long *size,
		     unsigned long newsize);
FILE *mix_sortcache_open (MAILSTREAM *stream);
long mix_sortcache_update (MAILSTREAM *stream,FILE **sortcache);
char *mix_read_record (FILE *f,char *buf,unsigned long buflen,char *type);
unsigned long mix_read_sequence (FILE *f);
char *mix_dir (char *dst,char *name);
char *mix_file (char *dst,char *dir,char *name);
char *mix_file_data (char *dst,char *dir,unsigned long data);
unsigned long mix_modseq (unsigned long oldseq);

/* MIX mail routines */


/* Driver dispatch used by MAIL */

DRIVER mixdriver = {
  "mix",			/* driver name */
				/* driver flags */
  DR_MAIL|DR_LOCAL|DR_NOFAST|DR_CRLF|DR_LOCKING|DR_DIRFMT|DR_MODSEQ,
  (DRIVER *) NIL,		/* next driver */
  mix_valid,			/* mailbox is valid for us */
  mix_parameters,		/* manipulate parameters */
  mix_scan,			/* scan mailboxes */
  mix_list,			/* find mailboxes */
  mix_lsub,			/* find subscribed mailboxes */
  mix_subscribe,		/* subscribe to mailbox */
  mix_unsubscribe,		/* unsubscribe from mailbox */
  mix_create,			/* create mailbox */
  mix_delete,			/* delete mailbox */
  mix_rename,			/* rename mailbox */
  mail_status_default,		/* status of mailbox */
  mix_open,			/* open mailbox */
  mix_close,			/* close mailbox */
  NIL,				/* fetch message "fast" attributes */
  NIL,				/* fetch message flags */
  NIL,				/* fetch overview */
  NIL,				/* fetch message envelopes */
  mix_header,			/* fetch message header only */
  mix_text,			/* fetch message body only */
  NIL,				/* fetch partial message test */
  NIL,				/* unique identifier */
  NIL,				/* message number */
  mix_flag,			/* modify flags */
  NIL,				/* per-message modify flags */
  NIL,				/* search for message based on criteria */
  mix_sort,			/* sort messages */
  mix_thread,			/* thread messages */
  mix_ping,			/* ping mailbox to see if still alive */
  mix_check,			/* check for new messages */
  mix_expunge,			/* expunge deleted messages */
  mix_copy,			/* copy messages to another mailbox */
  mix_append,			/* append string message to mailbox */
  NIL				/* garbage collect stream */
};

				/* prototype stream */
MAILSTREAM mixproto = {&mixdriver};

/* MIX mail validate mailbox
 * Accepts: mailbox name
 * Returns: our driver if name is valid, NIL otherwise
 */

DRIVER *mix_valid (char *name)
{
  char tmp[MAILTMPLEN];
  return mix_isvalid (name,tmp) ? &mixdriver : NIL;
}


/* MIX mail test for valid mailbox
 * Accepts: mailbox name
 *	    buffer to return meta name
 * Returns: T if valid, NIL otherwise, metadata name written in both cases
 */

long mix_isvalid (char *name,char *meta)
{
  char dir[MAILTMPLEN];
  struct stat sbuf;
				/* validate name as directory */
  if (!(errno = ((strlen (name) > NETMAXMBX) ? ENAMETOOLONG : NIL)) &&
      *mix_dir (dir,name) && mix_file (meta,dir,MIXMETA) &&
      !stat (dir,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFDIR)) {
				/* name is directory; is it mix? */
    if (!stat (meta,&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFREG))
      return LONGT;
    else errno = NIL;		/* directory but not mix */
  }
  return NIL;
}

/* MIX manipulate driver parameters
 * Accepts: function code
 *	    function-dependent value
 * Returns: function-dependent return value
 */

void *mix_parameters (long function,void *value)
{
  void *ret = NIL;
  switch ((int) function) {
  case GET_INBOXPATH:
    if (value) ret = mailboxfile ((char *) value,"~/INBOX");
    break;
  case GET_DIRFMTTEST:
    ret = (void *) mix_dirfmttest;
    break;
  case GET_SCANCONTENTS:
    ret = (void *) mix_scan_contents;
    break;
  case SET_ONETIMEEXPUNGEATPING:
    if (value) ((MIXLOCAL *) ((MAILSTREAM *) value)->local)->expok = T;
  case GET_ONETIMEEXPUNGEATPING:
    if (value) ret = (void *)
      (((MIXLOCAL *) ((MAILSTREAM *) value)->local)->expok ? VOIDT : NIL);
    break;
  }
  return ret;
}


/* MIX test for directory format internal node
 * Accepts: candidate node name
 * Returns: T if internal name, NIL otherwise
 */

long mix_dirfmttest (char *name)
{
				/* belongs to MIX if starts with .mix */
  return strncmp (name,MIXNAME,sizeof (MIXNAME) - 1) ? NIL : LONGT;
}

/* MIX mail scan mailboxes
 * Accepts: mail stream
 *	    reference
 *	    pattern to search
 *	    string to scan
 */

void mix_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
{
  if (stream) dummy_scan (NIL,ref,pat,contents);
}


/* MIX scan mailbox for contents
 * Accepts: mailbox name
 *	    desired contents
 *	    contents size
 *	    file size (ignored)
 * Returns: NIL if contents not found, T if found
 */

long mix_scan_contents (char *name,char *contents,unsigned long csiz,
			unsigned long fsiz)
{
  long i,nfiles;
  void *a;
  char *s;
  long ret = NIL;
  size_t namelen = strlen (name);
  struct stat sbuf;
  struct direct **names = NIL;
  if ((nfiles = scandir (name,&names,mix_select,mix_msgfsort)) > 0)
    for (i = 0; i < nfiles; ++i) {
      if (!ret) {
	sprintf (s = (char *) fs_get (namelen + strlen (names[i]->d_name) + 2),
		 "%s/%s",name,names[i]->d_name);
	if (!stat (s,&sbuf) && (csiz <= sbuf.st_size))
	  ret = dummy_scan_contents (s,contents,csiz,sbuf.st_size);
	fs_give ((void **) &s);
      }
      fs_give ((void **) &names[i]);
    }
				/* free directory list */
  if ((a = (void *) names) != NULL) fs_give ((void **) &a);
  return ret;
}

/* MIX list mailboxes
 * Accepts: mail stream
 *	    reference
 *	    pattern to search
 */

void mix_list (MAILSTREAM *stream,char *ref,char *pat)
{
  if (stream) dummy_list (NIL,ref,pat);
}


/* MIX list subscribed mailboxes
 * Accepts: mail stream
 *	    reference
 *	    pattern to search
 */

void mix_lsub (MAILSTREAM *stream,char *ref,char *pat)
{
  if (stream) dummy_lsub (NIL,ref,pat);
}

/* MIX mail subscribe to mailbox
 * Accepts: mail stream
 *	    mailbox to add to subscription list
 * Returns: T on success, NIL on failure
 */

long mix_subscribe (MAILSTREAM *stream,char *mailbox)
{
  return sm_subscribe (mailbox);
}


/* MIX mail unsubscribe to mailbox
 * Accepts: mail stream
 *	    mailbox to delete from subscription list
 * Returns: T on success, NIL on failure
 */

long mix_unsubscribe (MAILSTREAM *stream,char *mailbox)
{
  return sm_unsubscribe (mailbox);
}

/* MIX mail create mailbox
 * Accepts: mail stream
 *	    mailbox name to create
 * Returns: T on success, NIL on failure
 */

long mix_create (MAILSTREAM *stream,char *mailbox)
{
  DRIVER *test;
  FILE *f;
  int c,i;
  char *t,tmp[MAILTMPLEN],file[MAILTMPLEN];
  char *s = strrchr (mailbox,'/');
  unsigned long now = time (NIL);
  long ret = NIL;
				/* always create \NoSelect if trailing /  */
  if (s && !s[1]) return dummy_create (stream,mailbox);
				/* validate name */
  if (mix_dirfmttest (s ? s + 1 : mailbox))
    sprintf(tmp,"Can't create mailbox %.80s: invalid MIX-format name",mailbox);
				/* must not already exist */
  else if ((test = mail_valid (NIL,mailbox,NIL)) &&
	   strcmp (test->name,"dummy"))
    sprintf (tmp,"Can't create mailbox %.80s: mailbox already exists",mailbox);
				/* create directory and metadata */
  else if (!dummy_create_path (stream,
			       mix_file (file,mix_dir (tmp,mailbox),MIXMETA),
			       get_dir_protection (mailbox)))
    sprintf (tmp,"Can't create mailbox %.80s: %.80s",mailbox,strerror (errno));
  else if (!(f = fopen (file,"w")))
    sprintf (tmp,"Can't re-open metadata %.80s: %.80s",mailbox,
	     strerror (errno));
  else {			/* success, write initial metadata */
    fprintf (f,SEQFMT,now);
    fprintf (f,MTAFMT,now,(unsigned long) 0,now);
    for (i = 0, c = 'K'; (i < NUSERFLAGS) &&
	   (t = (stream && stream->user_flags[i]) ? stream->user_flags[i] :
	    default_user_flag (i)) && *t; ++i) {
      putc (c,f);		/* write another keyword */
      fputs (t,f);
      c = ' ';			/* delimiter is now space */
    }
    fclose (f);
    set_mbx_protections (mailbox,file);
				/* point to suffix */
    s = file + strlen (file) - (sizeof (MIXMETA) - 1);
    strcpy (s,MIXINDEX);	/* create index */
    if (!dummy_create_path (stream,file,get_dir_protection (mailbox)))
      sprintf (tmp,"Can't create mix mailbox index: %.80s",strerror (errno));
    else {
      set_mbx_protections (mailbox,file);
      strcpy (s,MIXSTATUS);	/* create status */
      if (!dummy_create_path (stream,file,get_dir_protection (mailbox)))
	sprintf (tmp,"Can't create mix mailbox status: %.80s",
		 strerror (errno));
      else {
	set_mbx_protections (mailbox,file);
	sprintf (s,"%08lx",now);/* message file */
	if (!dummy_create_path (stream,file,get_dir_protection (mailbox)))
	  sprintf (tmp,"Can't create mix mailbox data: %.80s",
		   strerror (errno));
	else {
	  set_mbx_protections (mailbox,file);
	  ret = LONGT;	/* declare success at this point */
	}
      }
    }
  }
  if (!ret) MM_LOG (tmp,ERROR);	/* some error */
  return ret;
}

/* MIX mail delete mailbox
 *	    mailbox name to delete
 * Returns: T on success, NIL on failure
 */

long mix_delete (MAILSTREAM *stream,char *mailbox)
{
  DIR *dirp;
  struct direct *d;
  int fd = -1;
  char *s,tmp[MAILTMPLEN];
  if (!mix_isvalid (mailbox,tmp))
    sprintf (tmp,"Can't delete mailbox %.80s: no such mailbox",mailbox);
  else if (((fd = open (tmp,O_RDWR,NIL)) < 0) || flock (fd,LOCK_EX|LOCK_NB))
    sprintf (tmp,"Can't lock mailbox for delete: %.80s",mailbox);
				/* delete metadata */
  else if (unlink (tmp)) sprintf (tmp,"Can't delete mailbox %.80s index: %80s",
				  mailbox,strerror (errno));
  else {
    close (fd);			/* close descriptor on deleted metadata */
				/* get directory name */
    *(s = strrchr (tmp,'/')) = '\0';
    if ((dirp = opendir (tmp)) != NULL) {	/* open directory */
      *s++ = '/';		/* restore delimiter */
				/* massacre messages */
      while ((d = readdir (dirp)) != NULL) if (mix_dirfmttest (d->d_name)) {
	strcpy (s,d->d_name);	/* make path */
	unlink (tmp);		/* sayonara */
      }
      closedir (dirp);		/* flush directory */
      *(s = strrchr (tmp,'/')) = '\0';
      if (rmdir (tmp)) {	/* try to remove the directory */
	sprintf (tmp,"Can't delete name %.80s: %.80s",
		 mailbox,strerror (errno));
	MM_LOG (tmp,WARN);
      }
    }
    return T;			/* always success */
  }
  if (fd >= 0) close (fd);	/* close any descriptor on metadata */
  MM_LOG (tmp,ERROR);		/* something failed */
  return NIL;
}

/* MIX mail rename mailbox
 * Accepts: MIX mail stream
 *	    old mailbox name
 *	    new mailbox name
 * Returns: T on success, NIL on failure
 */

long mix_rename (MAILSTREAM *stream,char *old,char *newname)
{
  char c,*s,tmp[MAILTMPLEN],tmp1[MAILTMPLEN];
  struct stat sbuf;
  int fd = -1;
  if (!mix_isvalid (old,tmp))
    sprintf (tmp,"Can't rename mailbox %.80s: no such mailbox",old);
  else if (((fd = open (tmp,O_RDWR,NIL)) < 0) || flock (fd,LOCK_EX|LOCK_NB))
    sprintf (tmp,"Can't lock mailbox for rename: %.80s",old);
  else if (mix_dirfmttest ((s = strrchr (newname,'/')) ? s + 1 : newname))
    sprintf (tmp,"Can't rename to mailbox %.80s: invalid MIX-format name",
	     newname);
				/* new mailbox name must not be valid */
  else if (mix_isvalid (newname,tmp))
    sprintf (tmp,"Can't rename to mailbox %.80s: destination already exists",
	     newname);
  else {
    mix_dir (tmp,old);		/* build old directory name */
    mix_dir (tmp1,newname);	/* and new directory name */
				/* easy if not INBOX */
    if (compare_cstring (old,"INBOX")) {
				/* found superior to destination name? */
      if ((s = strrchr (tmp1,'/')) != NULL) {
	c = *++s;		/* remember first character of inferior */
	*s = '\0';		/* tie off to get just superior */
				/* name doesn't exist, create it */
	if ((stat (tmp1,&sbuf) || ((sbuf.st_mode & S_IFMT) != S_IFDIR)) &&
	    !dummy_create_path (stream,tmp1,get_dir_protection (newname)))
	  return NIL;
	*s = c;			/* restore full name */
      }
      if (!rename (tmp,tmp1)) {
	close (fd);		/* close descriptor on metadata */
	return LONGT;
      }
    }

				/* RFC 3501 requires this */
    else if (dummy_create_path (stream,strcat (tmp1,"/"),
				get_dir_protection (newname))) {
      void *a;
      int i,n,lasterror;
      char *src,*dst;
      struct direct **names = NIL;
      size_t srcl = strlen (tmp);
      size_t dstl = strlen (tmp1);
				/* rename each mix file to new directory */
      for (i = lasterror = 0,n = scandir (tmp,&names,mix_rselect,alphasort);
	   i < n; ++i) {
	size_t len = strlen (names[i]->d_name);
	sprintf (src = (char *) fs_get (srcl + len + 2),"%s/%s",
		 tmp,names[i]->d_name);
	sprintf (dst = (char *) fs_get (dstl + len + 1),"%s%s",
		 tmp1,names[i]->d_name);
	if (rename (src,dst)) lasterror = errno;
	fs_give ((void **) &src);
	fs_give ((void **) &dst);
	fs_give ((void **) &names[i]);
      }
				/* free directory list */
      if ((a = (void *) names) != NULL) fs_give ((void **) &a);
      if (lasterror) errno = lasterror;
      else {
	close (fd);		/* close descriptor on metadata */
	return mix_create (NIL,"INBOX");
      }
    }
    sprintf (tmp,"Can't rename mailbox %.80s to %.80s: %.80s",
	     old,newname,strerror (errno));
  }
  if (fd >= 0) close (fd);	/* close any descriptor on metadata */
  MM_LOG (tmp,ERROR);		/* something failed */
  return NIL;
}


/* MIX test for mix name
 * Accepts: candidate directory name
 * Returns: T if mix file name, NIL otherwise
 */

int mix_rselect (struct direct *name)
{
  return mix_dirfmttest (name->d_name);
}

/* MIX mail open
 * Accepts: stream to open
 * Returns: stream on success, NIL on failure
 */

MAILSTREAM *mix_open (MAILSTREAM *stream)
{
  short silent;
				/* return prototype for OP_PROTOTYPE call */
  if (!stream) return user_flags (&mixproto);
  if (stream->local) fatal ("mix recycle stream");
  stream->local = memset (fs_get (sizeof (MIXLOCAL)),0,sizeof (MIXLOCAL));
				/* note if an INBOX or not */
  stream->inbox = !compare_cstring (stream->mailbox,"INBOX");
				/* make temporary buffer */
  LOCAL->buf = (char *) fs_get (CHUNKSIZE);
  LOCAL->buflen = CHUNKSIZE - 1;
				/* set stream->mailbox to be directory name */
  mix_dir (LOCAL->buf,stream->mailbox);
  fs_give ((void **) &stream->mailbox);
  stream->mailbox = cpystr (LOCAL->buf);
  LOCAL->msgfd = -1;		/* currently no file open */
  if (!(((!stream->rdonly &&	/* open metadata file */
	  ((LOCAL->mfd = open (mix_file (LOCAL->buf,stream->mailbox,MIXMETA),
			       O_RDWR,NIL)) >= 0)) ||
	 ((stream->rdonly = T) &&
	  ((LOCAL->mfd = open (mix_file (LOCAL->buf,stream->mailbox,MIXMETA),
			       O_RDONLY,NIL)) >= 0))) &&
	!flock (LOCAL->mfd,LOCK_SH))) {
    MM_LOG ("Error opening mix metadata file",ERROR);
    mix_abort (stream);
    stream = NIL;		/* open fails */
  }
  else {			/* metadata open, complete open */
    LOCAL->index = cpystr (mix_file (LOCAL->buf,stream->mailbox,MIXINDEX));
    LOCAL->status = cpystr (mix_file (LOCAL->buf,stream->mailbox,MIXSTATUS));
    LOCAL->sortcache = cpystr (mix_file (LOCAL->buf,stream->mailbox,
					 MIXSORTCACHE));
    stream->sequence++;		/* bump sequence number */
				/* parse mailbox */
    stream->nmsgs = stream->recent = 0;
    if ((silent = stream->silent) != 0) LOCAL->internal = T;
    stream->silent = T;
    if (mix_ping (stream)) {	/* do initial ping */
				/* try burping in case we are exclusive */
      if (!stream->rdonly) mix_expunge (stream,"",NIL);
      if (!(stream->nmsgs || stream->silent))
	MM_LOG ("Mailbox is empty",(long) NIL);
      stream->silent = silent;	/* now notify upper level */
      mail_exists (stream,stream->nmsgs);
      stream->perm_seen = stream->perm_deleted = stream->perm_flagged =
	stream->perm_answered = stream->perm_draft = stream->rdonly ? NIL : T;
      stream->perm_user_flags = stream->rdonly ? NIL : 0xffffffff;
      stream->kwd_create =	/* can we create new user flags? */
	(stream->user_flags[NUSERFLAGS-1] || stream->rdonly) ? NIL : T;
    }
    else {			/* got murdelyzed in ping */
      mix_abort (stream);
      stream = NIL;
    }
  }
  return stream;		/* return stream to caller */
}

/* MIX mail close
 * Accepts: MAIL stream
 *	    close options
 */

void mix_close (MAILSTREAM *stream,long options)
{
  if (LOCAL) {			/* only if a file is open */
    int silent = stream->silent;
    stream->silent = T;		/* note this stream is dying */
				/* burp-only or expunge */
    mix_expunge (stream,(options & CL_EXPUNGE) ? NIL : "",NIL);
    mix_abort (stream);
    stream->silent = silent;	/* reset silent state */
  }
}


/* MIX mail abort stream
 * Accepts: MAIL stream
 */

void mix_abort (MAILSTREAM *stream)
{
  if (LOCAL) {			/* only if a file is open */
				/* close current message file if open */
    if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
				/* close current metadata file if open */
    if (LOCAL->mfd >= 0) close (LOCAL->mfd);
    if (LOCAL->index) fs_give ((void **) &LOCAL->index);
    if (LOCAL->status) fs_give ((void **) &LOCAL->status);
    if (LOCAL->sortcache) fs_give ((void **) &LOCAL->sortcache);
				/* free local scratch buffer */
    if (LOCAL->buf) fs_give ((void **) &LOCAL->buf);
				/* nuke the local data */
    fs_give ((void **) &stream->local);
    stream->dtb = NIL;		/* log out the DTB */
  }
}

/* MIX mail fetch message header
 * Accepts: MAIL stream
 *	    message # to fetch
 *	    pointer to returned header text length
 *	    option flags
 * Returns: message header in RFC822 format
 */

char *mix_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length,
		  long flags)
{
  unsigned long i,j,k;
  int fd;
  char *s,tmp[MAILTMPLEN];
  MESSAGECACHE *elt;
  if (length) *length = 0;	/* default return */
  if (flags & FT_UID) return "";/* UID call "impossible" */
  elt = mail_elt (stream,msgno);/* get elt */
				/* is message in current message file? */
  if ((LOCAL->msgfd < 0) || (elt->private.spare.data != LOCAL->curmsg)) {
    if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
    if ((LOCAL->msgfd = open (mix_file_data (LOCAL->buf,stream->mailbox,
					     elt->private.spare.data),
					     O_RDONLY,NIL)) < 0) return "";
				/* got file */
    LOCAL->curmsg = elt->private.spare.data;
  }    
  lseek (LOCAL->msgfd,elt->private.special.offset,L_SET);
				/* size of special data and header */
  j = elt->private.msg.header.offset + elt->private.msg.header.text.size;
  if (j > LOCAL->buflen) {	/* is buffer big enough? */
				/* no, make one that is */
    fs_give ((void **) &LOCAL->buf);
    LOCAL->buf = (char *) fs_get ((LOCAL->buflen = j) + 1);
  }
  /* Maybe someday validate internaldate too */
				/* slurp special data + header, validate */
  if ((read (LOCAL->msgfd,LOCAL->buf,j) == j) &&
      !strncmp (LOCAL->buf,MSGTOK,MSGTSZ) &&
      (elt->private.uid == strtoul ((char *) LOCAL->buf + MSGTSZ,&s,16)) &&
      (*s++ == ':') && (s = strchr (s,':')) &&
      (k = strtoul (s+1,&s,16)) && (*s++ == ':') &&
      (s < (char *) (LOCAL->buf + elt->private.msg.header.offset))) {
				/* won, set offset and size of message */
    i = elt->private.msg.header.offset;
    *length = elt->private.msg.header.text.size;
    if (k != elt->rfc822_size) {
      sprintf (tmp,"Inconsistency in mix message size, uid=%lx (%lu != %lu)",
	       elt->private.uid,elt->rfc822_size,k);
      MM_LOG (tmp,WARN);
    }
  }
  else {			/* document the problem */
    LOCAL->buf[100] = '\0';	/* tie off buffer at no more than 100 octets */
				/* or at newline, whichever is first */
    if ((s = strpbrk (LOCAL->buf,"\015\012")) != NULL) *s = '\0';
    sprintf (tmp,"Error reading mix message header, uid=%lx, s=%.0lx, h=%s",
	     elt->private.uid,elt->rfc822_size,LOCAL->buf);
    MM_LOG (tmp,ERROR);
    *length = i = j = 0;	/* default to empty */
  }
  LOCAL->buf[j] = '\0';		/* tie off buffer at the end */
  return (char *) LOCAL->buf + i;
}

/* MIX mail fetch message text (body only)
 * Accepts: MAIL stream
 *	    message # to fetch
 *	    pointer to returned stringstruct
 *	    option flags
 * Returns: T on success, NIL on failure
 */

long mix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
{
  unsigned long i;
  FDDATA d;
  MESSAGECACHE *elt;
				/* UID call "impossible" */
  if (flags & FT_UID) return NIL;
  elt = mail_elt (stream,msgno);
				/* is message in current message file? */
  if ((LOCAL->msgfd < 0) || (elt->private.spare.data != LOCAL->curmsg)) {
    if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
    if ((LOCAL->msgfd = open (mix_file_data (LOCAL->buf,stream->mailbox,
					     elt->private.spare.data),
					     O_RDONLY,NIL)) < 0) return NIL;
				/* got file */
    LOCAL->curmsg = elt->private.spare.data;
  }    
				/* doing non-peek fetch? */
  if (!(flags & FT_PEEK) && !elt->seen) {
    FILE *idxf;			/* yes, process metadata/index/status */
    FILE *statf = mix_parse (stream,&idxf,NIL,LONGT);
    elt->seen = T;		/* mark as seen */
    MM_FLAGS (stream,elt->msgno);
				/* update status file if possible */
    if (statf && !stream->rdonly) {
      elt->private.mod = LOCAL->statusseq = mix_modseq (LOCAL->statusseq);
      mix_status_update (stream,statf,NIL);
    }
    if (idxf) fclose (idxf);	/* release index and status file */
    if (statf) fclose (statf);
  } 
  d.fd = LOCAL->msgfd;		/* set up file descriptor */
				/* offset of message text */
  d.pos = elt->private.special.offset + elt->private.msg.header.offset +
    elt->private.msg.header.text.size;
  d.chunk = LOCAL->buf;		/* initial buffer chunk */
  d.chunksize = CHUNKSIZE;	/* chunk size */
  INIT (bs,fd_string,&d,elt->rfc822_size - elt->private.msg.header.text.size);
  return T;
}

/* MIX mail modify flags
 * Accepts: MAIL stream
 *	    sequence
 *	    flag(s)
 *	    option flags
 */

void mix_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags)
{
  MESSAGECACHE *elt;
  unsigned long i,uf,ffkey;
  long f;
  short nf;
  FILE *idxf;
  FILE *statf = mix_parse (stream,&idxf,NIL,LONGT);
  unsigned long seq = mix_modseq (LOCAL->statusseq);
				/* find first free key */
  for (ffkey = 0; (ffkey < NUSERFLAGS) && stream->user_flags[ffkey]; ++ffkey);
				/* parse sequence and flags */
  if (((flags & ST_UID) ? mail_uid_sequence (stream,sequence) :
       mail_sequence (stream,sequence)) &&
      ((f = mail_parse_flags (stream,flag,&uf)) || uf)) {
				/* alter flags */
    for (i = 1,nf = (flags & ST_SET) ? T : NIL; i <= stream->nmsgs; i++)
      if ((elt = mail_elt (stream,i))->sequence) {
	struct {		/* old flags */
	  unsigned int seen : 1;
	  unsigned int deleted : 1;
	  unsigned int flagged : 1;
	  unsigned int answered : 1;
	  unsigned int draft : 1;
	  unsigned long user_flags;
	} old;
	old.seen = elt->seen; old.deleted = elt->deleted;
	old.flagged = elt->flagged; old.answered = elt->answered;
	old.draft = elt->draft; old.user_flags = elt->user_flags;
	if (f&fSEEN) elt->seen = nf;
	if (f&fDELETED) elt->deleted = nf;
	if (f&fFLAGGED) elt->flagged = nf;
	if (f&fANSWERED) elt->answered = nf;
	if (f&fDRAFT) elt->draft = nf;
				/* user flags */
	if (flags & ST_SET) elt->user_flags |= uf;
	else elt->user_flags &= ~uf;
	if ((old.seen != elt->seen) || (old.deleted != elt->deleted) ||
	    (old.flagged != elt->flagged) ||
	    (old.answered != elt->answered) || (old.draft != elt->draft) ||
	    (old.user_flags != elt->user_flags)) {
	  if (!stream->rdonly) elt->private.mod = LOCAL->statusseq = seq;
	  MM_FLAGS (stream,elt->msgno);
	}
      }
				/* update status file after change */
    if (statf && (seq == LOCAL->statusseq))
      mix_status_update (stream,statf,NIL);
				/* update metadata if created a keyword */
    if ((ffkey < NUSERFLAGS) && stream->user_flags[ffkey] &&
	!mix_meta_update (stream))
      MM_LOG ("Error updating mix metadata after keyword creation",ERROR);
  }
  if (statf) fclose (statf);	/* release status file if still open */
  if (idxf) fclose (idxf);	/* release index file */
}

/* MIX mail sort messages
 * Accepts: mail stream
 *	    character set
 *	    search program
 *	    sort program
 *	    option flags
 * Returns: vector of sorted message sequences or NIL if error
 */

unsigned long *mix_sort (MAILSTREAM *stream,char *charset,SEARCHPGM *spg,
			 SORTPGM *pgm,long flags)
{
  unsigned long *ret;
  FILE *sortcache = mix_sortcache_open (stream);
  ret = mail_sort_msgs (stream,charset,spg,pgm,flags);
  mix_sortcache_update (stream,&sortcache);
  return ret;
}


/* MIX mail thread messages
 * Accepts: mail stream
 *	    thread type
 *	    character set
 *	    search program
 *	    option flags
 * Returns: thread node tree or NIL if error
 */

THREADNODE *mix_thread (MAILSTREAM *stream,char *type,char *charset,
			SEARCHPGM *spg,long flags)
{
  THREADNODE *ret;
  FILE *sortcache = mix_sortcache_open (stream);
  ret = mail_thread_msgs (stream,type,charset,spg,flags,mail_sort_msgs);
  mix_sortcache_update (stream,&sortcache);
  return ret;
}

/* MIX mail ping mailbox
 * Accepts: MAIL stream
 * Returns: T if stream alive, else NIL
 */

static int snarfing = 0;	/* lock against recursive snarfing */

long mix_ping (MAILSTREAM *stream)
{
  FILE *idxf,*statf;
  struct stat sbuf;
  STRING msg;
  MESSAGECACHE *elt;
  int mfd,ifd,sfd;
  unsigned long i,msglen;
  char *message,date[MAILTMPLEN],flags[MAILTMPLEN];
  MAILSTREAM *sysibx = NIL;
  long ret = NIL;
  long snarfok = LONGT;
				/* time to snarf? */
  if (stream->inbox && !stream->rdonly && !snarfing &&
      (time (0) >= (LOCAL->lastsnarf +
		    (time_t) mail_parameters (NIL,GET_SNARFINTERVAL,NIL)))) {
    appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
    copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
    MM_CRITICAL (stream);	/* go critical */
    snarfing = T;		/* don't recursively snarf */
				/* disable APPENDUID/COPYUID callbacks */
    mail_parameters (NIL,SET_APPENDUID,NIL);
    mail_parameters (NIL,SET_COPYUID,NIL);
				/* sizes match and anything in sysinbox? */
    if (!stat (sysinbox (),&sbuf) && ((sbuf.st_mode & S_IFMT) == S_IFREG) &&
	sbuf.st_size &&	(sysibx = mail_open (sysibx,sysinbox (),OP_SILENT)) &&
	!sysibx->rdonly && sysibx->nmsgs) {
				/* for each message in sysibx mailbox */
      for (i = 1; snarfok && (i <= sysibx->nmsgs); ++i)
	if (!(elt = mail_elt (sysibx,i))->deleted &&
	    (message = mail_fetch_message (sysibx,i,&msglen,FT_PEEK)) &&
	    msglen) {
	  mail_date (date,elt);	/* make internal date string */
				/* make flag string */
	  flags[0] = flags[1] = '\0';
	  if (elt->seen) strcat (flags," \\Seen");
	  if (elt->flagged) strcat (flags," \\Flagged");
	  if (elt->answered) strcat (flags," \\Answered");
	  if (elt->draft) strcat (flags," \\Draft");
	  flags[0] = '(';
	  strcat (flags,")");
	  INIT (&msg,mail_string,message,msglen);
	  if ((snarfok = mail_append_full (stream,"INBOX",flags,date,&msg)) != 0L) {
	    char sequence[15];
	    sprintf (sequence,"%lu",i);
	    mail_flag (sysibx,sequence,"\\Deleted",ST_SET);
	  }
	}

				/* now expunge all those messages */
      if (snarfok) mail_expunge (sysibx);
      else {
	sprintf (LOCAL->buf,"Can't copy new mail at message: %lu",i - 1);
	MM_LOG (LOCAL->buf,WARN);
      }
    }
    if (sysibx) mail_close (sysibx);
				/* re-enable APPENDUID/COPYUID */
    mail_parameters (NIL,SET_APPENDUID,(void *) au);
    mail_parameters (NIL,SET_COPYUID,(void *) cu);
    snarfing = NIL;		/* no longer snarfing */
    MM_NOCRITICAL (stream);	/* release critical */
    LOCAL->lastsnarf = time (0);/* note time of last snarf */
  }
				/* expunging OK if global flag set */
  if (mail_parameters (NIL,GET_EXPUNGEATPING,NIL)) LOCAL->expok = T;
				/* process metadata/index/status */
  if ((statf = mix_parse (stream,&idxf,LONGT,
			 (LOCAL->internal ? NIL : LONGT))) != NULL) {
    fclose (statf);		/* just close the status file */
    ret = LONGT;		/* declare success */
  }
  if (idxf) fclose (idxf);	/* release index file */
  LOCAL->expok = NIL;		/* expunge no longer OK */
  if (!ret) mix_abort (stream);	/* murdelyze stream if ping fails */
  return ret;
}


/* MIX mail checkpoint mailbox (burp only)
 * Accepts: MAIL stream
 */

void mix_check (MAILSTREAM *stream)
{
  if (stream->rdonly)		/* won't do on readonly files! */
    MM_LOG ("Checkpoint ignored on readonly mailbox",NIL);
				/* do burp-only expunge action */
  if (mix_expunge (stream,"",NIL)) MM_LOG ("Check completed",(long) NIL);
}

/* MIX mail expunge mailbox
 * Accepts: MAIL stream
 *	    sequence to expunge if non-NIL, empty string for burp only
 *	    expunge options
 * Returns: T on success, NIL if failure
 */

long mix_expunge (MAILSTREAM *stream,char *sequence,long options)
{
  FILE *idxf = NIL;
  FILE *statf = NIL;
  MESSAGECACHE *elt;
  int ifd,sfd;
  long ret;
  unsigned long i;
  unsigned long nexp = 0;
  unsigned long reclaimed = 0;
  int burponly = (sequence && !*sequence);
  LOCAL->expok = T;		/* expunge during ping is OK */
  if (!(ret = burponly || !sequence ||
	((options & EX_UID) ?
	 mail_uid_sequence (stream,sequence) :
	 mail_sequence (stream,sequence))) || stream->rdonly);
				/* read index and open status exclusive */
  else if ((statf = mix_parse (stream,&idxf,LONGT,
			      LOCAL->internal ? NIL : LONGT)) != NULL) {
				/* expunge unless just burping */
    if (!burponly) for (i = 1; i <= stream->nmsgs;) {
      elt = mail_elt (stream,i);/* need to expunge this message? */
      if (elt->deleted && (sequence ? elt->sequence : T)) {
	++nexp;			/* yes, make it so */
	mail_expunged (stream,i);
      }
      else ++i;		       /* otherwise advance to next message */
    }

				/* burp if can get exclusive access */
    if (!flock (LOCAL->mfd,LOCK_EX|LOCK_NB)) {
      void *a;
      struct direct **names = NIL;
      long nfiles = scandir (stream->mailbox,&names,mix_select,mix_msgfsort);
      if (nfiles > 0) {		/* if have message files */
	MIXBURP *burp,*cur;
				/* initialize burp list */
	for (i = 0, burp = cur = NIL; i < nfiles; ++i) {
	  MIXBURP *nxt = (MIXBURP *) memset (fs_get (sizeof (MIXBURP)),0,
					     sizeof (MIXBURP));
				/* another file found */
	  if (cur) cur = cur->next = nxt;
	  else cur = burp = nxt;
	  cur->name = cpystr (names[i]->d_name);
	  cur->fileno = strtoul (cur->name + sizeof (MIXNAME) - 1,NIL,16);
	  cur->tail = &cur->set;
	  fs_give ((void **) &names[i]);
	}
				/* now load ranges */
	for (i = 1, cur = burp; ret && (i <= stream->nmsgs); i++) {
				/* is this message in current set? */
	  elt = mail_elt (stream,i);
	  if (cur && (elt->private.spare.data != cur->fileno)) {
				/* restart if necessary */
	    if (elt->private.spare.data < cur->fileno) cur = burp;
				/* hunt for appropriate mailbox */
	    while (cur && (elt->private.spare.data > cur->fileno))
	      cur = cur->next;
				/* ought to have found it now... */
	    if (cur && (elt->private.spare.data != cur->fileno)) cur = NIL;
	  }
				/* if found, add to set */
	  if (cur) ret = mix_addset (&cur->tail,elt->private.special.offset,
				     elt->private.msg.header.offset +
				     elt->rfc822_size);
	  else {		/* uh-oh */
	    sprintf (LOCAL->buf,"Can't locate mix message file %.08lx",
		     elt->private.spare.data);
	    MM_LOG (LOCAL->buf,ERROR);
	    ret = NIL;
	  }
	}
	if (ret) 		/* if no errors, burp all files */
	  for (cur = burp; ret && cur; cur = cur->next) {
				/* if non-empty, burp it */
	    if (cur->set.last) ret = mix_burp (stream,cur,&reclaimed);
				/* empty, delete it unless new msg file */
	    else if (mix_file_data (LOCAL->buf,stream->mailbox,cur->fileno) &&
		     ((cur->fileno == LOCAL->newmsg) ?
		      truncate (LOCAL->buf,0) : unlink (LOCAL->buf))) {
	      sprintf (LOCAL->buf,
		       "Can't delete empty message file %.80s: %.80s",
		       cur->name,strerror (errno));
	      MM_LOG (LOCAL->buf,WARN);
	    }
	  }
	while (burp) {		/* flush the burp list */
	  cur = burp->next;
	  if (burp->name) fs_give ((void **) &burp->name);
	  fs_give ((void **) &burp);
	  burp = cur;
	}
      }
      else MM_LOG ("No mix message files found during expunge",WARN);
				/* free directory list */
      if ((a = (void *) names) != NULL) fs_give ((void **) &a);
    }

				/* either way, re-acquire shared lock */
    if (flock (LOCAL->mfd,LOCK_SH|LOCK_NB))
      fatal ("Unable to re-acquire metadata shared lock!");
    /* Do this step even if ret is NIL (meaning some burp problem)! */
    if (nexp || reclaimed) {	/* rewrite index and status if changed */
      LOCAL->indexseq = mix_modseq (LOCAL->indexseq);
      if ((ret = mix_index_update (stream,idxf,NIL)) != 0L){
	LOCAL->statusseq = mix_modseq (LOCAL->statusseq);
				/* set failure if update fails */
	ret = mix_status_update (stream,statf,NIL);
      }
    }
  }
  if (statf) fclose (statf);	/* close status if still open */
  if (idxf) fclose (idxf);	/* close index if still open */
  LOCAL->expok = NIL;		/* cancel expok */
  if (ret) {			/* only if success */
    char *s = NIL;
    if (nexp) sprintf (s = LOCAL->buf,"Expunged %lu messages",nexp);
    else if (reclaimed)
      sprintf (s=LOCAL->buf,"Reclaimed %lu bytes of expunged space",reclaimed);
    else if (!burponly)
      s = stream->rdonly ? "Expunge ignored on readonly mailbox" :
	   "No messages deleted, so no update needed";
    if (s) MM_LOG (s,(long) NIL);
  }
  return ret;
}

/* MIX test for message file name
 * Accepts: candidate directory name
 * Returns: T if message file name, NIL otherwise
 *
 * ".mix" with no suffix was used by experimental versions
 */

int mix_select (struct direct *name)
{
  char c,*s;
				/* make sure name has prefix */
  if (mix_dirfmttest (name->d_name)) {
    for (c = *(s = name->d_name + sizeof (MIXNAME) - 1); c && isxdigit (c);
	 c = *s++);
    if (!c) return T;		/* all-hex or no suffix */
  }
  return NIL;			/* not suffix or non-hex */
}


/* MIX msg file name comparison
 * Accepts: first candidate directory entry
 *	    second candidate directory entry
 * Returns: -1 if d1 < d2, 0 if d1 == d2, 1 d1 > d2
 */

int mix_msgfsort (const void *d1,const void *d2)
{
  char *n1 = (*(struct direct **) d1)->d_name + sizeof (MIXNAME) - 1;
  char *n2 = (*(struct direct **) d2)->d_name + sizeof (MIXNAME) - 1;
  return compare_ulong (*n1 ? strtoul (n1,NIL,16) : 0,
			*n2 ? strtoul (n2,NIL,16) : 0);
}


/* MIX add a range to a set
 * Accepts: pointer to set to add
 *	    start of set
 *	    size of set
 * Returns: T if success, set updated, NIL otherwise
 */

long mix_addset (SEARCHSET **set,unsigned long start,unsigned long size)
{
  SEARCHSET *s = *set;
  if (start < s->last) {	/* sanity check */
    char tmp[MAILTMPLEN];
    sprintf (tmp,"Backwards-running mix index %lu < %lu",start,s->last);
    MM_LOG (tmp,ERROR);
    return NIL;
  }
				/* range initially empty? */
  if (!s->last) s->first = start;
  else if (start > s->last)	/* no, start new range if can't append */
    (*set = s = s->next = mail_newsearchset ())->first = start;
  s->last = start + size;	/* end of current range */
  return LONGT;
}

/* MIX burp message file
 * Accepts: MAIL stream
 *	    current burp block for this message
 * Returns: T if successful, NIL if failed
 */

static char *staterr = "Error in stat of mix message file %.80s: %.80s";
static char *truncerr = "Error truncating mix message file %.80s: %.80s";

long mix_burp (MAILSTREAM *stream,MIXBURP *burp,unsigned long *reclaimed)
{
  MESSAGECACHE *elt;
  SEARCHSET *set;
  struct stat sbuf;
  off_t rpos,wpos;
  size_t size,wsize,wpending,written;
  int fd;
  FILE *f;
  void *s;
  unsigned long i;
  long ret = NIL;
				/* build file name */
  mix_file_data (LOCAL->buf,stream->mailbox,burp->fileno);
				/* need to burp at start or multiple ranges? */
  if (!burp->set.first && !burp->set.next) {
				/* easy case, single range at start of file */
    if (stat (LOCAL->buf,&sbuf)) {
      sprintf (LOCAL->buf,staterr,burp->name,strerror (errno));
      MM_LOG (LOCAL->buf,ERROR);
    }
				/* is this range sane? */
    else if (mix_burp_check (&burp->set,sbuf.st_size,LOCAL->buf)) {
				/* if matches range then no burp needed! */
      if (burp->set.last == sbuf.st_size) ret = LONGT;
				/* just need to remove cruft at end */
      else if ((ret = !truncate (LOCAL->buf,burp->set.last)) != 0L)
	*reclaimed += sbuf.st_size - burp->set.last;
      else {
	sprintf (LOCAL->buf,truncerr,burp->name,strerror (errno));
	MM_LOG (LOCAL->buf,ERROR);
      }
    }
  }
				/* have to do more work, get the file */
  else if (((fd = open (LOCAL->buf,O_RDWR,NIL)) < 0) ||
	   !(f = fdopen (fd,"r+b"))) {
    sprintf (LOCAL->buf,"Error opening mix message file %.80s: %.80s",
	     burp->name,strerror (errno));
    MM_LOG (LOCAL->buf,ERROR);
    if (fd >= 0) close (fd);	/* in case fdopen() failure */
  }
  else if (fstat (fd,&sbuf)) {	/* get file size */
    sprintf (LOCAL->buf,staterr,burp->name,strerror (errno));
    MM_LOG (LOCAL->buf,ERROR);
    fclose (f);
  }

				/* only if sane */
  else if (mix_burp_check (&burp->set,sbuf.st_size,LOCAL->buf)) {
				/* make sure each range starts with token */
    for (set = &burp->set; set; set = set->next)
      if (fseek (f,set->first,SEEK_SET) ||
	  (fread (LOCAL->buf,1,MSGTSZ,f) != MSGTSZ) ||
	  strncmp (LOCAL->buf,MSGTOK,MSGTSZ)) {
	sprintf (LOCAL->buf,"Bad message token in mix message file at %lu",
		 set->first);
	MM_LOG (LOCAL->buf,ERROR);
	fclose (f);
	return NIL;		/* burp fails for this file */
      }
				/* burp out each old message */
    for (set = &burp->set, rpos = wpos = 0; set; set = set->next) {
				/* move down this range */
      for (rpos = set->first, size = set->last - set->first;
	   size; size -= wsize) {
	if (rpos != wpos) {	/* data to skip at start? */
				/* no, slide this buffer down */
	  wsize = min (size,LOCAL->buflen);
				/* failure is not an option here */
	  while (fseek (f,rpos,SEEK_SET) ||
		 (fread (LOCAL->buf,1,wsize,f) != wsize)) {
	    MM_NOTIFY (stream,strerror (errno),WARN);
	    MM_DISKERROR (stream,errno,T);
	  }
				/* nor here */
	  while (fseek (f,wpos,SEEK_SET)) {
	    MM_NOTIFY (stream,strerror (errno),WARN);
	    MM_DISKERROR (stream,errno,T);
	  }
				/* and especially not here */
	  for (s = LOCAL->buf, wpending = wsize; wpending; s += written, wpending -= written)
	    if (!(written = fwrite (s,1,wpending,f))) {
	      MM_NOTIFY (stream,strerror (errno),WARN);
	      MM_DISKERROR (stream,errno,T);
	    }
	}
	else wsize = size;	/* nothing to skip, say we wrote it all */
	rpos += wsize; wpos += wsize;
      }
    }

    while (fflush (f)) {	/* failure also not an option here... */
      MM_NOTIFY (stream,strerror (errno),WARN);
      MM_DISKERROR (stream,errno,T);
    }
    if (ftruncate (fd,wpos)) {	/* flush cruft at end of file */
      sprintf (LOCAL->buf,truncerr,burp->name,strerror (errno));
      MM_LOG (LOCAL->buf,WARN);
    }
    else *reclaimed += rpos - wpos;
    ret = !fclose (f);		/* close file */
				/* slide down message positions in index */
    for (i = 1,rpos = 0; i <= stream->nmsgs; ++i)
      if ((elt = mail_elt (stream,i))->private.spare.data == burp->fileno) {
	elt->private.special.offset = rpos;
	rpos += elt->private.msg.header.offset + elt->rfc822_size;
      }
				/* debugging */
    if (rpos != wpos) fatal ("burp size consistency check!");
  }
  return ret;
}


/* MIX burp sanity check to make sure not burping off end of file
 * Accepts: burp set
 *	    file size
 *	    file name
 * Returns: T if sane, NIL if insane
 */

long mix_burp_check (SEARCHSET *set,size_t size,char *file)
{
  do if (set->last > size) {	/* sanity check */
    char tmp[MAILTMPLEN];
    sprintf (tmp,"Unexpected short mix message file %.80s %lu < %lu",
	     file,size,set->last);
    MM_LOG (tmp,ERROR);
    return NIL;			/* don't burp this file at all */
  } while ((set = set->next) != NULL);
  return LONGT;
}

/* MIX mail copy message(s)
 * Accepts: MAIL stream
 *	    sequence
 *	    destination mailbox
 *	    copy options
 * Returns: T if copy successful, else NIL
 */

long mix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
{
  FDDATA d;
  STRING st;
  char tmp[2*MAILTMPLEN];
  long ret = mix_isvalid (mailbox,LOCAL->buf);
  mailproxycopy_t pc =
    (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
  MAILSTREAM *astream = NIL;
  FILE *idxf = NIL;
  FILE *msgf = NIL;
  FILE *statf = NIL;
  if (!ret) switch (errno) {	/* make sure valid mailbox */
  case NIL:			/* no error in stat() */
    if (pc) return (*pc) (stream,sequence,mailbox,options);
    sprintf (tmp,"Not a MIX-format mailbox: %.80s",mailbox);
    MM_LOG (tmp,ERROR);
    break;
  default:			/* some stat() error */
    MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before copy",NIL);
    break;
  }
				/* get sequence to copy */
  else if (!(ret = ((options & CP_UID) ? mail_uid_sequence (stream,sequence) :
		    mail_sequence (stream,sequence))));
				/* acquire stream to append */
  else if ((ret = ((astream = mail_open (NIL,mailbox,OP_SILENT)) &&
		  !astream->rdonly &&
		  (((MIXLOCAL *) astream->local)->expok = T) &&
		  (statf = mix_parse (astream,&idxf,LONGT,NIL))) ?
	   LONGT : NIL) != 0L) {
    int fd;
    unsigned long i;
    MESSAGECACHE *elt;
    unsigned long newsize,hdrsize,size;
    MIXLOCAL *local = (MIXLOCAL *) astream->local;
    unsigned long seq = mix_modseq (local->metaseq);
				/* make sure new modseq fits */
    if (local->indexseq > seq) seq = local->indexseq + 1;
    if (local->statusseq > seq) seq = local->statusseq + 1;
				/* calculate size of per-message header */
    sprintf (local->buf,MSRFMT,MSGTOK,(unsigned long) 0,0,0,0,0,0,0,'+',0,0,
	     (unsigned long) 0);
    hdrsize = strlen (local->buf);

    MM_CRITICAL (stream);	/* go critical */
    astream->silent = T;	/* no events here */
				/* calculate size that will be added */
    for (i = 1, newsize = 0; i <= stream->nmsgs; ++i)
      if ((elt = mail_elt (stream,i))->sequence)
	newsize += hdrsize + elt->rfc822_size;
				/* open data file */
    if ((msgf = mix_data_open (astream,&fd,&size,newsize)) != NULL) {
      char *t;
      unsigned long j,uid,uidv;
      copyuid_t cu = (copyuid_t) mail_parameters (NIL,GET_COPYUID,NIL);
      SEARCHSET *source = cu ? mail_newsearchset () : NIL;
      SEARCHSET *dest = cu ? mail_newsearchset () : NIL;
      for (i = 1,uid = uidv = 0; ret && (i <= stream->nmsgs); ++i) 
	if (((elt = mail_elt (stream,i))->sequence) && elt->rfc822_size) {
				/* is message in current message file? */
	  if ((LOCAL->msgfd < 0) ||
	      (elt->private.spare.data != LOCAL->curmsg)) {
	    if (LOCAL->msgfd >= 0) close (LOCAL->msgfd);
	    if ((LOCAL->msgfd = open (mix_file_data (LOCAL->buf,
						     stream->mailbox,
						     elt->private.spare.data),
				      O_RDONLY,NIL)) >= 0)
	      LOCAL->curmsg = elt->private.spare.data;
	  }
	  if (LOCAL->msgfd < 0) ret = NIL;
	  else {		/* got file */
	    d.fd = LOCAL->msgfd;/* set up file descriptor */
				/* start of message */
	    d.pos = elt->private.special.offset +
	      elt->private.msg.header.offset;
	    d.chunk = LOCAL->buf;
	    d.chunksize = CHUNKSIZE;
	    INIT (&st,fd_string,&d,elt->rfc822_size);
				/* init flag string */
	    tmp[0] = tmp[1] = '\0';
	    if ((j = elt->user_flags) != 0L) do
	      if ((t = stream->user_flags[find_rightmost_bit (&j)]) && *t)
		strcat (strcat (tmp," "),t);
	    while (j);
	    if (elt->seen) strcat (tmp," \\Seen");
	    if (elt->deleted) strcat (tmp," \\Deleted");
	    if (elt->flagged) strcat (tmp," \\Flagged");
	    if (elt->answered) strcat (tmp," \\Answered");
	    if (elt->draft) strcat (tmp," \\Draft");
	    tmp[0] = '(';	/* wrap list */
	    strcat (tmp,")");
				/* if append OK, add to source set */
	    if ((ret = mix_append_msg (astream,msgf,tmp,elt,&st,dest,
				       seq)) &&	source)
	      mail_append_set (source,mail_uid (stream,i));
	  }
	}

				/* finish write if success */
      if (ret && (ret = !fflush (msgf))) {
	fclose (msgf);		/* all good, close the msg file now */
				/* write new metadata, index, and status */
	local->metaseq = local->indexseq = local->statusseq = seq;
	if ((ret = (mix_meta_update (astream) &&
		   mix_index_update (astream,idxf,LONGT))) != 0L){
				/* success, delete if doing a move */
	  if (options & CP_MOVE)
	    for (i = 1; i <= stream->nmsgs; i++)
	      if ((elt = mail_elt (stream,i))->sequence) {
		elt->deleted = T;
		if (!stream->rdonly) elt->private.mod = LOCAL->statusseq = seq;
		MM_FLAGS (stream,elt->msgno);
	      }
				/* done with status file now */
	  mix_status_update (astream,statf,LONGT);
				/* return sets if doing COPYUID */
	  if (cu) (*cu) (stream,mailbox,astream->uid_validity,source,dest);
	  source = dest = NIL;	/* don't free these sets now */
	}
      }
      else {			/* error */
	if (errno) {		/* output error message if system call error */
	  sprintf (tmp,"Message copy failed: %.80s",strerror (errno));
	  MM_LOG (tmp,ERROR);
	}
	ftruncate (fd,size);	/* revert file */
	close (fd);		/* make sure that fclose doesn't corrupt us */
	fclose (msgf);		/* free the stdio resources */
      }
				/* flush any sets remaining */
      mail_free_searchset (&source);
      mail_free_searchset (&dest);
    }
    else {			/* message file open failed */
      sprintf (tmp,"Error opening copy message file: %.80s",
	       strerror (errno));
      MM_LOG (tmp,ERROR);
      ret = NIL;
    }
    MM_NOCRITICAL (stream);
  }
  else MM_LOG ("Can't open copy mailbox",ERROR);
  if (statf) fclose (statf);	/* close status if still open */
  if (idxf) fclose (idxf);	/* close index if still open */
				/* finished with append stream */
  if (astream) mail_close (astream);
  return ret;			/* return state */
}

/* MIX mail append message from stringstruct
 * Accepts: MAIL stream
 *	    destination mailbox
 *	    append callback
 *	    data for callback
 * Returns: T if append successful, else NIL
 */

long mix_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
{
  STRING *message;
  char *flags,*date,tmp[MAILTMPLEN];
				/* N.B.: can't use LOCAL->buf for tmp */
  long ret = mix_isvalid (mailbox,tmp);
				/* default stream to prototype */
  if (!stream) stream = user_flags (&mixproto);
  if (!ret) switch (errno) {	/* if not valid mailbox */
  case ENOENT:			/* no such file? */
    if ((ret = compare_cstring (mailbox,"INBOX") ?
	NIL : mix_create (NIL,"INBOX")) != 0L)
      break;
    MM_NOTIFY (stream,"[TRYCREATE] Must create mailbox before append",NIL);
    break;
  default:
    sprintf (tmp,"Not a MIX-format mailbox: %.80s",mailbox);
    MM_LOG (tmp,ERROR);
    break;
  }

				/* get first message */
  if (ret && MM_APPEND (af) (stream,data,&flags,&date,&message)) {
    MAILSTREAM *astream;
    FILE *idxf = NIL;
    FILE *msgf = NIL;
    FILE *statf = NIL;
    if ((ret = ((astream = mail_open (NIL,mailbox,OP_SILENT)) &&
	       !astream->rdonly &&
	       (((MIXLOCAL *) astream->local)->expok = T) &&
	       (statf = mix_parse (astream,&idxf,LONGT,NIL))) ?
	LONGT : NIL) != 0l) {
      int fd;
      unsigned long size,hdrsize;
      MESSAGECACHE elt;
      MIXLOCAL *local = (MIXLOCAL *) astream->local;
      unsigned long seq = mix_modseq (local->metaseq);
				/* make sure new modseq fits */
      if (local->indexseq > seq) seq = local->indexseq + 1;
      if (local->statusseq > seq) seq = local->statusseq + 1;
				/* calculate size of per-message header */
      sprintf (local->buf,MSRFMT,MSGTOK,(unsigned long) 0,0,0,0,0,0,0,'+',0,0,
	       (unsigned long) 0);
      hdrsize = strlen (local->buf);
      MM_CRITICAL (astream);	/* go critical */
      astream->silent = T;	/* no events here */
				/* open data file */
      if ((msgf = mix_data_open (astream,&fd,&size,hdrsize + SIZE (message))) != NULL){
	appenduid_t au = (appenduid_t) mail_parameters (NIL,GET_APPENDUID,NIL);
	SEARCHSET *dst = au ? mail_newsearchset () : NIL;
	while (ret && message) {/* while good to go and have messages */
	  errno = NIL;		/* in case one of these causes failure */
				/* guard against zero-length */
	  if (!(ret = SIZE (message)))
	    MM_LOG ("Append of zero-length message",ERROR);
	  else if (date && !(ret = mail_parse_date (&elt,date))) {
	    sprintf (tmp,"Bad date in append: %.80s",date);
	    MM_LOG (tmp,ERROR);
	  }
	  else {
	    if (!date) {	/* if date not specified, use now */
	      internal_date (tmp);
	      mail_parse_date (&elt,tmp);
	    }
	    ret = mix_append_msg (astream,msgf,flags,&elt,message,dst,seq) &&
	      MM_APPEND (af) (stream,data,&flags,&date,&message);
	  }
	}

				/* finish write if success */
	if (ret && (ret = !fflush (msgf))) {
	  fclose (msgf);	/* all good, close the msg file now */
				/* write new metadata, index, and status */
	  local->metaseq = local->indexseq = local->statusseq = seq;
	  if ((ret = (mix_meta_update (astream) &&
		      mix_index_update (astream,idxf,LONGT) &&
		      mix_status_update (astream,statf,LONGT))) && au) {
	      (*au) (mailbox,astream->uid_validity,dst);
	      dst = NIL;	/* don't free this set now */
	  }
	}
	else {			/* failure */
	  if (errno) {		/* output error message if system call error */
	    sprintf (tmp,"Message append failed: %.80s",strerror (errno));
	    MM_LOG (tmp,ERROR);
	  }
	  ftruncate (fd,size);	/* revert all writes to file*/
	  close (fd);		/* make sure that fclose doesn't corrupt us */
	  fclose (msgf);	/* free the stdio resources */
	}
				/* flush any set remaining */
	mail_free_searchset (&dst);
      }
      else {			/* message file open failed */
	sprintf (tmp,"Error opening append message file: %.80s",
		 strerror (errno));
	MM_LOG (tmp,ERROR);
	ret = NIL;
      }
      MM_NOCRITICAL (astream);	/* release critical */
    }
    else MM_LOG ("Can't open append mailbox",ERROR);
    if (statf) fclose (statf);	/* close status if still open */
    if (idxf) fclose (idxf);	/* close index if still open */
    if (astream) mail_close (astream);
  }
  return ret;
}

/* MIX mail append single message
 * Accepts: MAIL stream
 *	    flags for new message if non-NIL
 *	    elt with source date if non-NIL
 *	    stringstruct of message text
 *	    searchset to place UID
 *	    modseq of message
 * Returns: T if success, NIL if failure
 */

long mix_append_msg (MAILSTREAM *stream,FILE *f,char *flags,MESSAGECACHE *delt,
		     STRING *msg,SEARCHSET *set,unsigned long seq)
{
  MESSAGECACHE *elt;
  int c,cs;
  unsigned long i,j,k,uf,hoff;
  long sf;
  void *s;
  stream->kwd_create = NIL;	/* don't copy unknown keywords */
  sf = mail_parse_flags (stream,flags,&uf);
				/* swell the cache */
  mail_exists (stream,++stream->nmsgs);
				/* assign new UID from metadata */
  (elt = mail_elt (stream,stream->nmsgs))->private.uid = ++stream->uid_last;
  elt->private.mod = seq;	/* set requested modseq in status */
  elt->rfc822_size = SIZE (msg);/* copy message size and date to index */
  elt->year = delt->year; elt->month = delt->month; elt->day = delt->day;
  elt->hours = delt->hours; elt->minutes = delt->minutes;
  elt->seconds = delt->seconds; elt->zoccident = delt->zoccident;
  elt->zhours = delt->zhours; elt->zminutes = delt->zminutes;
  /*
   * Do NOT set elt->valid here!  mix_status_update() uses it to determine
   * whether a message should be marked as old.
   */
  if (sf&fSEEN) elt->seen = T;	/* copy flags to status */
  if (sf&fDELETED) elt->deleted = T;
  if (sf&fFLAGGED) elt->flagged = T;
  if (sf&fANSWERED) elt->answered = T;
  if (sf&fDRAFT) elt->draft = T;
  elt->user_flags |= uf;
				/* message is in new message file */
  elt->private.spare.data = LOCAL->newmsg;

				/* offset to message internal header */
  elt->private.special.offset = ftell (f);
				/* build header for message */
  fprintf (f,MSRFMT,MSGTOK,elt->private.uid,
	   elt->year + BASEYEAR,elt->month,elt->day,
	   elt->hours,elt->minutes,elt->seconds,
	   elt->zoccident ? '-' : '+',elt->zhours,elt->zminutes,
	   elt->rfc822_size);
				/* offset to header from  internal header */
  elt->private.msg.header.offset = ftell (f) - elt->private.special.offset;
  for (cs = 0; SIZE (msg); ) {	/* copy message */
    if (elt->private.msg.header.text.size) {
      if (msg->cursize)		/* blat entire chunk if have it */
	for (s = msg->curpos,j = msg->cursize; j; s += k, j -= k)
	  if (!(k = fwrite (s,1,j,f))) return NIL;
      SETPOS (msg,GETPOS (msg) + msg->cursize);
    }
    else {			/* still searching for delimiter */
      c = 0xff & SNX (msg);	/* get source character */
      if (putc (c,f) == EOF) return NIL;
      switch (cs) {		/* decide what to do based on state */
      case 0:			/* previous char ordinary */
	if (c == '\015') cs = 1;/* advance if CR */
	break;
      case 1:			/* previous CR, advance if LF */
	cs = (c == '\012') ? 2 : 0;
	break;
      case 2:			/* previous CRLF, advance if CR */
	cs = (c == '\015') ? 3 : 0;
	break;
      case 3:			/* previous CRLFCR, done if LF */
	if (c == '\012') elt->private.msg.header.text.size =
			   elt->rfc822_size - SIZE (msg);
	cs = 0;			/* reset mechanism */
	break;
      }
    }
  }
				/* if no delimiter, header is entire msg */
  if (!elt->private.msg.header.text.size)
    elt->private.msg.header.text.size = elt->rfc822_size;
				/* add this message to set */
  mail_append_set (set,elt->private.uid);
  return LONGT;			/* success */
}

/* MIX mail read metadata, index, and status
 * Accepts: MAIL stream
 *	    returned index file
 *	    index file flags (non-NIL if want to add/remove messages)
 *	    status file flags (non-NIL if want to update elt->valid and old)
 * Returns: open status file, or NIL if failure
 *
 * Note that this routine can return an open index file even if it fails!
 */

static char *shortmsg =
  "message %lu (UID=%.08lx) truncated by %lu byte(s) (%lu < %lu)";

FILE *mix_parse (MAILSTREAM *stream,FILE **idxf,long iflags,long sflags)
{
  int fd;
  unsigned long i;
  char *s,*t;
  struct stat sbuf;
  FILE *statf = NIL;
  short metarepairneeded = 0;
  short indexrepairneeded = 0;
  short silent = stream->silent;
  *idxf = NIL;			/* in case error */
				/* readonly means no updates */
  if (stream->rdonly) iflags = sflags = NIL;
				/* open index file */
  if ((fd = open (LOCAL->index,iflags ? O_RDWR : O_RDONLY,NIL)) < 0)
    MM_LOG ("Error opening mix index file",ERROR);
				/* acquire exclusive access and FILE */
  else if (!flock (fd,iflags ? LOCK_EX : LOCK_SH) &&
	   !(*idxf = fdopen (fd,iflags ? "r+b" : "rb"))) {
    MM_LOG ("Error obtaining stream on mix index file",ERROR);
    flock (fd,LOCK_UN);		/* relinquish lock */
    close (fd);
  }

				/* slurp metadata */
  else if ((s = mix_meta_slurp (stream,&i)) != NULL) {
    unsigned long j = 0;	/* non-zero if UIDVALIDITY/UIDLAST changed */
    if (i != LOCAL->metaseq) {	/* metadata changed? */
      char *t,*k;
      LOCAL->metaseq = i;	/* note new metadata sequence */
      while (s && *s) {		/* parse entire metadata file */
				/* locate end of line */
	if ((s = strstr (t = s,"\015\012")) != NULL) {
	  *s = '\0';		/* tie off line */
	  s += 2;		/* skip past CRLF */
	  switch (*t++) {	/* parse line */
	  case 'V':		/* UIDVALIDITY */
	    if (!isxdigit (*t) || !(i = strtoul (t,&t,16))) {
	      MM_LOG ("Error in mix metadata file UIDVALIDITY record",ERROR);
	      return NIL;	/* give up */
	    }
	    if (i != stream->uid_validity) j = stream->uid_validity = i;
	    break;
	  case 'L':		/* new UIDLAST */
	    if (!isxdigit (*t)) {
	      MM_LOG ("Error in mix metadata file UIDLAST record",ERROR);
	      return NIL;	/* give up */
	    }
	    if ((i = strtoul (t,&t,16)) != stream->uid_last)
	      j = stream->uid_last = i;
	    break;
	  case 'N':		/* new message file */
	    if (!isxdigit (*t)) {
	      MM_LOG ("Error in mix metadata file new msg record",ERROR);
	      return NIL;	/* give up */
	    }
	    if ((i = strtoul (t,&t,16)) != stream->uid_last)
	      LOCAL->newmsg = i;
	    break;
	  case 'K':		/* new keyword list */
	    for (i = 0; t && *t && (i < NUSERFLAGS); ++i) {
	      if ((t = strchr (k = t,' ')) != NULL) *t++ = '\0';
				/* make sure keyword non-empty */
	      if (*k && (strlen (k) <= MAXUSERFLAG)) {
				/* in case value changes (shouldn't happen) */
		if (stream->user_flags[i] && strcmp (stream->user_flags[i],k)){
		  char tmp[MAILTMPLEN];
		  sprintf (tmp,"flag rename old=%.80s new=%.80s",
			   stream->user_flags[i],k);
		  MM_LOG (tmp,WARN);
		  fs_give ((void **) &stream->user_flags[i]);
		}
		if (!stream->user_flags[i]) stream->user_flags[i] = cpystr (k);
	      }
	      else break;	/* empty keyword */
	    }
	    if ((i < NUSERFLAGS) && stream->user_flags[i]) {
	      MM_LOG ("Error in mix metadata file keyword record",ERROR);
	      return NIL;	/* give up */
	    }
	    else if (i == NUSERFLAGS) stream->kwd_create = NIL;
	    break;
	  }
	}
	if (t && *t) {		/* junk in line */
	  MM_LOG ("Error in mix metadata record",ERROR);
	  return NIL;		/* give up */
	}
      }
    }

				/* get sequence */
    if (!(i = mix_read_sequence (*idxf)) || (i < LOCAL->indexseq)) {
      MM_LOG ("Error in mix index file sequence record",ERROR);
      return NIL;		/* give up */
    }
				/* sequence changed from last time? */
    else if (j || (i > LOCAL->indexseq)) {
      unsigned long prevuid = 0;
      unsigned long uid,nmsgs,curfile,curfilesize,curpos;
      char *t,*msg,tmp[MAILTMPLEN];
				/* start with no messages */
      curfile = curfilesize = curpos = nmsgs = 0;
				/* update sequence iff expunging OK */
      if (LOCAL->expok) LOCAL->indexseq = i;
				/* get first elt */
      while ((s = mix_read_record (*idxf,LOCAL->buf,LOCAL->buflen,"index")) &&
	     *s)
	switch (*s) {
	case ':':		/* message record */
	  if (!(isxdigit (*++s) && (uid = strtoul (s,&t,16)))) msg = "UID";
	  else if (!((*t++ == ':') && isdigit (*t) && isdigit (t[1]) &&
		     isdigit (t[2]) && isdigit (t[3]) && isdigit (t[4]) &&
		     isdigit (t[5]) && isdigit (t[6]) && isdigit (t[7]) &&
		     isdigit (t[8]) && isdigit (t[9]) && isdigit (t[10]) &&
		     isdigit (t[11]) && isdigit (t[12]) && isdigit (t[13]) &&
		     ((t[14] == '+') || (t[14] == '-')) && 
		     isdigit (t[15]) && isdigit (t[16]) && isdigit (t[17]) &&
		     isdigit (t[18]))) msg = "internaldate";
	  else if ((*(s = t+19) != ':') || !isxdigit (*++s)) msg = "size";
	  else {
	    unsigned int y = (((*t - '0') * 1000) + ((t[1] - '0') * 100) +
			      ((t[2] - '0') * 10) + t[3] - '0') - BASEYEAR;
	    unsigned int m = ((t[4] - '0') * 10) + t[5] - '0';
	    unsigned int d = ((t[6] - '0') * 10) + t[7] - '0';
	    unsigned int hh = ((t[8] - '0') * 10) + t[9] - '0';
	    unsigned int mm = ((t[10] - '0') * 10) + t[11] - '0';
	    unsigned int ss = ((t[12] - '0') * 10) + t[13] - '0';
	    unsigned int z = (t[14] == '-') ? 1 : 0;
	    unsigned int zh = ((t[15] - '0') * 10) + t[16] - '0';
	    unsigned int zm = ((t[17] - '0') * 10) + t[18] - '0';
	    unsigned long size = strtoul (s,&s,16);
	    if ((*s++ == ':') && isxdigit (*s)) {
	      unsigned long file = strtoul (s,&s,16);
	      if ((*s++ == ':') && isxdigit (*s)) {
		unsigned long pos = strtoul (s,&s,16);
		if ((*s++ == ':') && isxdigit (*s)) {
		  unsigned long hpos = strtoul (s,&s,16);
		  if ((*s++ == ':') && isxdigit (*s)) {
		    unsigned long hsiz = strtoul (s,&s,16);
		    if (uid > stream->uid_last) {
		      sprintf (tmp,"mix index invalid UID (%08lx < %08lx)",
			       uid,stream->uid_last);
		      if (stream->rdonly) {
			MM_LOG (tmp,ERROR);
			return NIL;
		      }
		      strcat (tmp,", repaired");
		      MM_LOG (tmp,WARN);
		      stream->uid_last = uid;
		      metarepairneeded = T;
		    }

				/* ignore expansion values */
		    if (*s++ == ':') {
		      MESSAGECACHE *elt;
		      if(prevuid > uid) {
			sprintf (tmp,"mix index backwards UID: %lx",uid);
			MM_LOG (tmp,ERROR);
			return NIL;
		      }
		      prevuid = uid;
		      ++nmsgs;	/* this is another message */
				/* within current known range of messages? */
		      while (nmsgs <= stream->nmsgs) {
				/* yes, get corresponding elt */
			elt = mail_elt (stream,nmsgs);
				/* existing message with matching data? */
			if (uid == elt->private.uid) {
				/* beware of Dracula's resurrection */
			  if (elt->private.ghost) {
			    sprintf (tmp,"mix index data unexpunged UID: %lx",
				     uid);
			    MM_LOG (tmp,ERROR);
			    return NIL;
			  }
				/* also of static data changing */
			  if ((size != elt->rfc822_size) ||
			      (file != elt->private.spare.data) ||
			      (pos != elt->private.special.offset) ||
			      (hpos != elt->private.msg.header.offset) ||
			      (hsiz != elt->private.msg.header.text.size) ||
			      (y != elt->year) || (m != elt->month) ||
			      (d != elt->day) || (hh != elt->hours) ||
			      (mm != elt->minutes) || (ss != elt->seconds) ||
			      (z != elt->zoccident) || (zh != elt->zhours) ||
			      (zm != elt->zminutes)) {
			    sprintf (tmp,"mix index data mismatch: %lx",uid);
			    MM_LOG (tmp,ERROR);
			    return NIL;
			  }
			  break;
			}
				/* existing msg with lower UID is expunged */
			else if (uid > elt->private.uid) {
			  if (LOCAL->expok) mail_expunged (stream,nmsgs);
			  else {/* message expunged, but not yet for us */
			    ++nmsgs;
			    elt->private.ghost = T;
			  }
			}
			else {	/* unexpected message record */
			  sprintf (tmp,"mix index UID mismatch (%lx < %lx)",
				   uid,elt->private.uid);
			  MM_LOG (tmp,ERROR);
			  return NIL;
			}
		      }

				/* time to create a new message? */
		      if (nmsgs > stream->nmsgs) {
				/* defer announcing until later */
			stream->silent = T;
			mail_exists (stream,nmsgs);
			stream->silent = silent;
			(elt = mail_elt (stream,nmsgs))->recent = T;
			elt->private.uid = uid; elt->rfc822_size = size;
			elt->private.spare.data = file;
			elt->private.special.offset = pos;
			elt->private.msg.header.offset = hpos;
			elt->private.msg.header.text.size = hsiz;
			elt->year = y; elt->month = m; elt->day = d;
			elt->hours = hh; elt->minutes = mm;
			elt->seconds = ss; elt->zoccident = z;
			elt->zhours = zh; elt->zminutes = zm;
				/* message in same file? */
			if (curfile == file) {
			  if (pos < curpos) {
			    MESSAGECACHE *plt = mail_elt (stream,elt->msgno-1);
				/* uh-oh, calculate delta? */
			    i = curpos - pos;
			    sprintf (tmp,shortmsg,plt->msgno,plt->private.uid,
				     i,pos,curpos);
				/* possible to fix? */
			    if (!stream->rdonly && LOCAL->expok &&
				(i < plt->rfc822_size)) {
			      plt->rfc822_size -= i;
			      if (plt->rfc822_size <
				  plt->private.msg.header.text.size)
				plt->private.msg.header.text.size =
				  plt->rfc822_size;
			      strcat (tmp,", repaired");
			      indexrepairneeded = T;
			    }
			    MM_LOG (tmp,WARN);
			  }
			}
			else {	/* new file, restart */
			  if (stat (mix_file_data (LOCAL->buf,stream->mailbox,
						   curfile = file),&sbuf)) {
			    sprintf (tmp,"Missing mix data file: %.500s",
				     LOCAL->buf);
			    MM_LOG (tmp,ERROR);
			    return NIL;
			  }
			  curfile = file;
			  curfilesize = sbuf.st_size;
			}

				/* position of message in file */
			curpos = pos + elt->private.msg.header.offset +
			  elt->rfc822_size;
				/* short file? */
			if (curfilesize < curpos) {
				/* uh-oh, calculate delta? */
			    i = curpos - curfilesize;
			    sprintf (tmp,shortmsg,elt->msgno,elt->private.uid,
				     i,curfilesize,curpos);
				/* possible to fix? */
			    if (!stream->rdonly && LOCAL->expok &&
				(i < elt->rfc822_size)) {
			      elt->rfc822_size -= i;
			      if (elt->rfc822_size <
				  elt->private.msg.header.text.size)
				elt->private.msg.header.text.size =
				  elt->rfc822_size;
			      strcat (tmp,", repaired");
			      indexrepairneeded = T;
			    }
			    MM_LOG (tmp,WARN);
			}
		      }
		      break;
		    }
		    else msg = "expansion";
		  }
		  else msg = "header size";
		}
		else msg = "header position";
	      }
	      else msg = "message position";
	    }
	    else msg = "file#";
	  }
	  sprintf (tmp,"Error in %s in mix index file: %.500s",msg,s);
	  MM_LOG (tmp,ERROR);
	  return NIL;
	default:
	  sprintf (tmp,"Unknown record in mix index file: %.500s",s);
	  MM_LOG (tmp,ERROR);
	  return NIL;
	}
      if (!s) return NIL;	/* barfage from mix_read_record() */
				/* expunge trailing messages not in index */
      if (LOCAL->expok) while (nmsgs < stream->nmsgs)
	mail_expunged (stream,stream->nmsgs);
    }

				/* repair metadata and index if needed */
    if ((metarepairneeded ? mix_meta_update (stream) : T) &&
	(indexrepairneeded ? mix_index_update (stream,*idxf,NIL) : T)) {
      MESSAGECACHE *elt;
      int fd;
      unsigned long uid,uf,sf,mod;
      char *s;
      int updatep = NIL;
				/* open status file */
      if ((fd = open (LOCAL->status,
		      stream->rdonly ? O_RDONLY : O_RDWR,NIL)) < 0)
	MM_LOG ("Error opening mix status file",ERROR);
				/* acquire exclusive access and FILE */
      else if (!flock (fd,stream->rdonly ? LOCK_SH : LOCK_EX) &&
	       !(statf = fdopen (fd,stream->rdonly ? "rb" : "r+b"))) {
	MM_LOG ("Error obtaining stream on mix status file",ERROR);
	flock (fd,LOCK_UN);	/* relinquish lock */
	close (fd);
      }
				/* get sequence */
      else if (!(i = mix_read_sequence (statf)) ||
	       ((i < LOCAL->statusseq) && stream->nmsgs && (i != 1))) {
	sprintf (LOCAL->buf,
		 "Error in mix status sequence record, i=%lx, seq=%lx",
		 i,LOCAL->statusseq);
	MM_LOG (LOCAL->buf,ERROR);
      }
				/* sequence changed from last time? */
      else if (i != LOCAL->statusseq) {
				/* update sequence, get first elt */
	if (i > LOCAL->statusseq) LOCAL->statusseq = i;
	if (stream->nmsgs) {
	  elt = mail_elt (stream,i = 1);

				/* read message records */
	  while ((t = s = mix_read_record (statf,LOCAL->buf,LOCAL->buflen,
					   "status")) && *s && (*s++ == ':') &&
		 isxdigit (*s)) {
	    uid = strtoul (s,&s,16);
	    if ((*s++ == ':') && isxdigit (*s)) {
	      uf = strtoul (s,&s,16);
	      if ((*s++ == ':') && isxdigit (*s)) {
		sf = strtoul (s,&s,16);
		if ((*s++ == ':') && isxdigit (*s)) {
		  mod = strtoul (s,&s,16);
				/* ignore expansion values */
		  if (*s++ == ':') {
				/* need to move ahead to next elt? */
		    while ((uid > elt->private.uid) && (i < stream->nmsgs))
		      elt = mail_elt (stream,++i);
				/* update elt if altered */
		    if ((uid == elt->private.uid) &&
			(!elt->valid || (mod != elt->private.mod))) {
		      elt->user_flags = uf;
		      elt->private.mod = mod;
		      elt->seen = (sf & fSEEN) ? T : NIL;
		      elt->deleted = (sf & fDELETED) ? T : NIL;
		      elt->flagged = (sf & fFLAGGED) ? T : NIL;
		      elt->answered = (sf & fANSWERED) ? T : NIL;
		      elt->draft = (sf & fDRAFT) ? T : NIL;
				/* announce if altered existing message */
		      if (elt->valid) MM_FLAGS (stream,elt->msgno);
				/* first time, is old message? */
		      else if (sf & fOLD) {
				/* yes, clear recent and set valid */
			elt->recent = NIL;
			elt->valid = T;
		      }
				/* recent, allowed to update its status? */
		      else if (sflags) {
				/* yes, set valid and check in status */
			elt->valid = T;
			elt->private.mod = mix_modseq (elt->private.mod);
			updatep = T;
		      }
		      /* leave valid unset and recent if sflags not set */
		    }
		    continue;	/* everything looks good */
		  }
		}
	      }
	    }
	    break;		/* error somewhere */
	  }

	  if (t && *t) {	/* non-null means bogus record */
	    char msg[MAILTMPLEN];
	    sprintf (msg,"Error in mix status file message record%s: %.80s",
		     stream->rdonly ? "" : ", fixing",t);
	    MM_LOG (msg,WARN);
				/* update it if not readonly */
	    if (!stream->rdonly) updatep = T;
	  }
	  if (updatep) {		/* need to update? */
	    LOCAL->statusseq = mix_modseq (LOCAL->statusseq);
	    mix_status_update (stream,statf,LONGT);
	  }
	}
      }
    }
  }
  if (statf) {			/* still happy? */
    unsigned long j;
    stream->silent = silent;	/* now notify upper level */
    mail_exists (stream,stream->nmsgs);
    for (i = 1, j = 0; i <= stream->nmsgs; ++i)
      if (mail_elt (stream,i)->recent) ++j;
    mail_recent (stream,j);
  }
  return statf;
}

/* MIX metadata file routines */

/* MIX read metadata
 * Accepts: MAIL stream
 *	    return pointer for modseq
 * Returns: pointer to metadata after modseq or NIL if failure
 */

char *mix_meta_slurp (MAILSTREAM *stream,unsigned long *seq)
{
  struct stat sbuf;
  char *s;
  char *ret = NIL;
  if (fstat (LOCAL->mfd,&sbuf))
    MM_LOG ("Error obtaining size of mix metadata file",ERROR);
  if (sbuf.st_size > LOCAL->buflen) {
				/* should be just a few dozen bytes */
    if (sbuf.st_size > METAMAX) fatal ("absurd mix metadata file size");
    fs_give ((void **) &LOCAL->buf);
    LOCAL->buf = (char *) fs_get ((LOCAL->buflen = sbuf.st_size) + 1);
  }
				/* read current metadata file */
  LOCAL->buf[sbuf.st_size] = '\0';
  if (lseek (LOCAL->mfd,0,L_SET) ||
      (read (LOCAL->mfd,s = LOCAL->buf,sbuf.st_size) != sbuf.st_size))
    MM_LOG ("Error reading mix metadata file",ERROR);
  else if ((*s != 'S') || !isxdigit (s[1]) ||
	   ((*seq = strtoul (s+1,&s,16)) < LOCAL->metaseq) ||
	   (*s++ != '\015') || (*s++ != '\012'))
    MM_LOG ("Error in mix metadata file sequence record",ERROR);
  else ret = s;
  return ret;
}

/* MIX update metadata
 * Accepts: MAIL stream
 * Returns: T on success, NIL if error
 *
 * Index MUST be locked!!
 */

long mix_meta_update (MAILSTREAM *stream)
{
  long ret;
				/* do nothing if stream readonly */
  if (stream->rdonly) ret = LONGT;
  else {
    unsigned char c,*s,*ss,*t;
    unsigned long i;
    /* The worst-case metadata is limited to:
     *    4 * (1 + 8 + 2) + (NUSERFLAGS * (MAXUSERFLAG + 1))
     * which comes out to 1994 octets.  This is much smaller than the normal
     * CHUNKSIZE definition of 64K, and CHUNKSIZE is the smallest size of
     * LOCAL->buf.
     *
     * If more stuff gets added to the metadata, or if you change the value
     * of NUSERFLAGS, MAXUSERFLAG or CHUNKSIZE, be sure to recalculate the
     * above assertion.
     */
    sprintf (LOCAL->buf,SEQFMT,LOCAL->metaseq = mix_modseq (LOCAL->metaseq));
    sprintf (LOCAL->buf + strlen (LOCAL->buf),MTAFMT,
	     stream->uid_validity,stream->uid_last,LOCAL->newmsg);
    for (i = 0, c = 'K', s = ss = LOCAL->buf + strlen (LOCAL->buf);
	 (i < NUSERFLAGS) && (t = stream->user_flags[i]); ++i) {
      if (!*t) fatal ("impossible empty keyword");
      *s++ = c;			/* write delimiter */
      while (*t) *s++ = *t++;	/* write keyword */
      c = ' ';			/* delimiter is now space */
    }
    if (s != ss) {		/* tie off keywords line */
      *s++ = '\015'; *s++ = '\012';
    }
				/* calculate length of metadata */
    if ((i = s - LOCAL->buf) > LOCAL->buflen)
      fatal ("impossible buffer overflow");
    lseek (LOCAL->mfd,0,L_SET);	/* rewind file */
				/* write new metadata */
    ret = (write (LOCAL->mfd,LOCAL->buf,i) == i) ? LONGT : NIL;
    ftruncate (LOCAL->mfd,i);	/* and tie off at that point */
  }
  return ret;
}

/* MIX index file routines */


/* MIX update index
 * Accepts: MAIL stream
 *	    open FILE
 *	    expansion check flag
 * Returns: T on success, NIL if error
 */

long mix_index_update (MAILSTREAM *stream,FILE *idxf,long flag)
{
  unsigned long i;
  long ret = LONGT;
  if (!stream->rdonly) {	/* do nothing if stream readonly */
    if (flag) {			/* need to do expansion check? */
      char tmp[MAILTMPLEN];
      size_t size;
      struct stat sbuf;
				/* calculate file size we need */
      for (i = 1, size = 0; i <= stream->nmsgs; ++i)
	if (!mail_elt (stream,i)->private.ghost) ++size;
      if (size) {		/* Winston Smith's first dairy entry */
	sprintf (tmp,IXRFMT,(unsigned long) 0,14,4,4,13,0,0,'+',0,0,
		 (unsigned long) 0,(unsigned long) 0,(unsigned long) 0,
		 (unsigned long) 0,(unsigned long) 0);
	size *= strlen (tmp);
      }
				/* calculate file size we need */
      sprintf (tmp,SEQFMT,LOCAL->indexseq);
      size += strlen (tmp);
				/* get current file size */
      if (fstat (fileno (idxf),&sbuf)) {
	MM_LOG ("Error getting size of mix index file",ERROR);
	ret = NIL;
      }
				/* need to write additional space? */
      else if (sbuf.st_size < size) {
	void *buf = fs_get (size -= sbuf.st_size);
	memset (buf,0,size);
	if (fseek (idxf,0,SEEK_END) || (fwrite (buf,1,size,idxf) != size) ||
	    fflush (idxf)) {
	  fseek (idxf,sbuf.st_size,SEEK_SET);
	  ftruncate (fileno (idxf),sbuf.st_size);
	  MM_LOG ("Error extending mix index file",ERROR);
	  ret = NIL;
	}
	fs_give ((void **) &buf);
      }
    }

    if (ret) {			/* if still good to go */
      rewind (idxf);		/* let's start at the very beginning */
				/* write modseq first */
      fprintf (idxf,SEQFMT,LOCAL->indexseq);
				/* then write all messages */
      for (i = 1; ret && (i <= stream->nmsgs); i++) {
	MESSAGECACHE *elt = mail_elt (stream,i);
	if (!elt->private.ghost)/* only write living messages */
	  fprintf (idxf,IXRFMT,elt->private.uid,
		   elt->year + BASEYEAR,elt->month,elt->day,
		   elt->hours,elt->minutes,elt->seconds,
		   elt->zoccident ? '-' : '+',elt->zhours,elt->zminutes,
		   elt->rfc822_size,elt->private.spare.data,
		   elt->private.special.offset,
		   elt->private.msg.header.offset,
		   elt->private.msg.header.text.size);
	if (ferror (idxf)) {
	  MM_LOG ("Error updating mix index file",ERROR);
	  ret = NIL;
	}
      }
      if (fflush (idxf)) {
	MM_LOG ("Error flushing mix index file",ERROR);
	ret = NIL;
      }
      if (ret) ftruncate (fileno (idxf),ftell (idxf));
    }
  }
  return ret;
}

/* MIX status file routines */


/* MIX update status
 * Accepts: MAIL stream
 *	    pointer to open FILE
 *	    expansion check flag
 * Returns: T on success, NIL if error
 */

long mix_status_update (MAILSTREAM *stream,FILE *statf,long flag)
{
  unsigned long i;
  char tmp[MAILTMPLEN];
  long ret = LONGT;
  if (!stream->rdonly) {	/* do nothing if stream readonly */
    if (flag) {			/* need to do expansion check? */
      char tmp[MAILTMPLEN];
      size_t size;
      struct stat sbuf;
				/* calculate file size we need */
      for (i = 1, size = 0; i <= stream->nmsgs; ++i)
	if (!mail_elt (stream,i)->private.ghost) ++size;
      if (size) {		/* number of living messages */
	sprintf (tmp,STRFMT,(unsigned long) 0,(unsigned long) 0,0,
		 (unsigned long) 0);
	size *= strlen (tmp);
      }
      sprintf (tmp,SEQFMT,LOCAL->statusseq);
      size += strlen (tmp);
				/* get current file size */
      if (fstat (fileno (statf),&sbuf)) {
	MM_LOG ("Error getting size of mix status file",ERROR);
	ret = NIL;
      }
				/* need to write additional space? */
      else if (sbuf.st_size < size) {
	void *buf = fs_get (size -= sbuf.st_size);
	memset (buf,0,size);
	if (fseek (statf,0,SEEK_END) || (fwrite (buf,1,size,statf) != size) ||
	    fflush (statf)) {
	  fseek (statf,sbuf.st_size,SEEK_SET);
	  ftruncate (fileno (statf),sbuf.st_size);
	  MM_LOG ("Error extending mix status file",ERROR);
	  ret = NIL;
	}
	fs_give ((void **) &buf);
      }
    }

    if (ret) {			/* if still good to go */
      rewind (statf);		/* let's start at the very beginning */
				/* write sequence */
      fprintf (statf,SEQFMT,LOCAL->statusseq);
				/* write message status records */
      for (i = 1; ret && (i <= stream->nmsgs); ++i) {
	MESSAGECACHE *elt = mail_elt (stream,i);
				/* make sure all messages have a modseq */
	if (!elt->private.mod) elt->private.mod = LOCAL->statusseq;
	if (!elt->private.ghost)/* only write living messages */
	  fprintf (statf,STRFMT,elt->private.uid,elt->user_flags,
		   (fSEEN * elt->seen) + (fDELETED * elt->deleted) +
		   (fFLAGGED * elt->flagged) + (fANSWERED * elt->answered) +
		   (fDRAFT * elt->draft) + (elt->valid ? fOLD : NIL),
		   elt->private.mod);
	if (ferror (statf)) {
	  sprintf (tmp,"Error updating mix status file: %.80s",
		   strerror (errno));
	  MM_LOG (tmp,ERROR);
	  ret = NIL;
	}
      }
      if (ret && fflush (statf)) {
	MM_LOG ("Error flushing mix status file",ERROR);
	ret = NIL;
      }
      if (ret) ftruncate (fileno (statf),ftell (statf));
    }
  }
  return ret;
}

/* MIX data file routines */


/* MIX open data file
 * Accepts: MAIL stream
 *	    pointer to returned fd if success
 *	    pointer to returned size if success
 *	    size of new data to be added
 * Returns: open FILE, or NIL if failure
 *
 * The curend test assumes that the last message of the mailbox is the furthest
 * point that the current data file extends, and thus that is all that needs to
 * be tested for short file prevention.
 */

FILE *mix_data_open (MAILSTREAM *stream,int *fd,long *size,
		     unsigned long newsize)
{
  FILE *msgf = NIL;
  struct stat sbuf;
  MESSAGECACHE *elt = stream->nmsgs ? mail_elt (stream,stream->nmsgs) : NIL;
  unsigned long curend = (elt && (elt->private.spare.data == LOCAL->newmsg)) ?
    elt->private.special.offset + elt->private.msg.header.offset +
    elt->rfc822_size : 0;
				/* allow create if curend 0 */
  if ((*fd = open (mix_file_data (LOCAL->buf,stream->mailbox,LOCAL->newmsg),
		   O_RDWR | (curend ? NIL : O_CREAT),NIL)) >= 0) {
    fstat (*fd,&sbuf);		/* get current file size */
				/* can we use this file? */
    if ((curend <= sbuf.st_size) &&
	(!sbuf.st_size || ((sbuf.st_size + newsize) <= MIXDATAROLL)))
      *size = sbuf.st_size;	/* yes, return current size */
    else {			/* short file or becoming too long */
      if (curend > sbuf.st_size) {
	char tmp[MAILTMPLEN];
	sprintf (tmp,"short mix message file %.08lx (%ld > %ld), rolling",
		 LOCAL->newmsg,curend,(unsigned long) sbuf.st_size);
	MM_LOG (tmp,WARN);	/* shouldn't happen */
      }
      close (*fd);		/* roll to a new file */
      errno = NIL;
      while ((*fd = open (mix_file_data
			  (LOCAL->buf,stream->mailbox,
			   LOCAL->newmsg = mix_modseq (LOCAL->newmsg)),
			  O_RDWR | O_CREAT | O_EXCL,sbuf.st_mode)) < 0) {
	switch(errno) {
	case EEXIST:		/* always retry if path exists or interrupt */
	case EINTR:
	  errno = NIL;
	  break;
	default:		/* probably EDQUOT */
	  {
	    char tmp[MAILTMPLEN];
	    sprintf (tmp,"data file %.08lx creation failure: %.80s",
		     LOCAL->newmsg,strerror (errno));
	    MM_LOG (tmp,ERROR);	/* shouldn't happen */
	    return NIL;
	  }
	}
      }
      *size = 0;		/* brand new file */
      fchmod (*fd,sbuf.st_mode);/* with same mode as previous file */
    }
  }
  if (*fd >= 0) {		/* have a data file? */
				/* yes, get stdio and set position */
    if ((msgf = fdopen (*fd,"r+b")) != NULL)fseek (msgf,*size,SEEK_SET);
    else close (*fd);		/* fdopen() failed? */
  }
  return msgf;			/* return results */
}

/* MIX open sortcache
 * Accepts: MAIL stream
 * Returns: open FILE, or NIL if failure or could only get readonly sortcache
 */

FILE *mix_sortcache_open (MAILSTREAM *stream)
{
  int fd,refwd;
  unsigned long i,uid,sentdate,fromlen,tolen,cclen,subjlen,msgidlen,reflen;
  char *s,*t,*msg,tmp[MAILTMPLEN];
  MESSAGECACHE *elt;
  SORTCACHE *sc;
  STRINGLIST *sl;
  struct stat sbuf;
  int rdonly = NIL;
  FILE *srtcf = NIL;
  mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
  fstat (LOCAL->mfd,&sbuf);
  if (!stream->nmsgs);		/* do nothing if mailbox empty */
				/* open sortcache file */
  else if (((fd = open (LOCAL->sortcache,O_RDWR|O_CREAT,sbuf.st_mode)) < 0) &&
	   !(rdonly = ((fd = open (LOCAL->sortcache,O_RDONLY,NIL)) >= 0)))
    MM_LOG ("Error opening mix sortcache file",WARN);
				/* acquire lock and FILE */
  else if (!flock (fd,rdonly ? LOCK_SH : LOCK_EX) &&
	   !(srtcf = fdopen (fd,rdonly ? "rb" : "r+b"))) {
    MM_LOG ("Error obtaining stream on mix sortcache file",WARN);
    flock (fd,LOCK_UN);		/* relinquish lock */
    close (fd);
  }
  else if (!(i = mix_read_sequence (srtcf)) || (i < LOCAL->sortcacheseq))
    MM_LOG ("Error in mix sortcache file sequence record",WARN);
				/* sequence changed from last time? */
  else if (i > LOCAL->sortcacheseq) {
    LOCAL->sortcacheseq = i;	/* update sequence */
    while ((s = t = mix_read_record (srtcf,LOCAL->buf,LOCAL->buflen,
				     "sortcache")) && *s &&
	   (msg = "uid") && (*s++ == ':') && isxdigit (*s)) {
      uid = strtoul (s,&s,16);
      if ((*s++ == ':') && isxdigit (*s)) {
	sentdate = strtoul (s,&s,16);
	if ((*s++ == ':') && isxdigit (*s)) {
	  fromlen = strtoul (s,&s,16);
	  if ((*s++ == ':') && isxdigit (*s)) {
	    tolen = strtoul (s,&s,16);
	    if ((*s++ == ':') && isxdigit (*s)) {
	      cclen = strtoul (s,&s,16);
	      if ((*s++ == ':') && ((*s == 'R') || (*s == ' ')) &&
		  isxdigit (s[1])) {
		refwd = (*s++ == 'R') ? T : NIL;
		subjlen = strtoul (s,&s,16);
		if ((*s++ == ':') && isxdigit (*s)) {
		  msgidlen = strtoul (s,&s,16);
		  if ((*s++ == ':') && isxdigit (*s)) {
		    reflen = strtoul (s,&s,16);
				/* ignore expansion values */
		    if (*s++ == ':') {

		      if ((i = mail_msgno (stream,uid)) != 0L) {
			sc = (SORTCACHE *) (*mc) (stream,i,CH_SORTCACHE);
			sc->size = (elt = mail_elt (stream,i))->rfc822_size;
			sc->date = sentdate;
			sc->arrival = elt->day ? mail_longdate (elt) : 1;
			if (refwd) sc->refwd = T;
			if (fromlen) {
			  if (sc->from) fseek (srtcf,fromlen + 2,SEEK_CUR);
			  else if ((getc (srtcf) != 'F') ||
				   (fread (sc->from = (char *) fs_get(fromlen),
					   1,fromlen-1,srtcf) != (fromlen-1))||
				   (sc->from[fromlen-1] = '\0') ||
				   (getc (srtcf) != '\015') ||
				   (getc (srtcf) != '\012')) {
			    msg = "from data";
			    break;
			  }
			}
			if (tolen) {
			  if (sc->to) fseek (srtcf,tolen + 2,SEEK_CUR);
			  else if ((getc (srtcf) != 'T') ||
				   (fread (sc->to = (char *) fs_get (tolen),
					   1,tolen-1,srtcf) != (tolen - 1)) ||
				   (sc->to[tolen-1] = '\0') ||
				   (getc (srtcf) != '\015') ||
				   (getc (srtcf) != '\012')) {
			    msg = "to data";
			    break;
			  }
			}
			if (cclen) {
			  if (sc->cc) fseek (srtcf,cclen + 2,SEEK_CUR);
			  else if ((getc (srtcf) != 'C') ||
				   (fread (sc->cc = (char *) fs_get (cclen),
					   1,cclen-1,srtcf) != (cclen - 1)) ||
				   (sc->cc[cclen-1] = '\0') ||
				   (getc (srtcf) != '\015') ||
				   (getc (srtcf) != '\012')) {
			    msg = "cc data";
			    break;
			  }
			}
			if (subjlen) {
			  if (sc->subject) fseek (srtcf,subjlen + 2,SEEK_CUR);
			  else if ((getc (srtcf) != 'S') ||
				     (fread (sc->subject =
					     (char *) fs_get (subjlen),1,
					     subjlen-1,srtcf) != (subjlen-1))||
				   (sc->subject[subjlen-1] = '\0') ||
				   (getc (srtcf) != '\015') ||
				   (getc (srtcf) != '\012')) {
			    msg = "subject data";
			    break;
			  }
			}

			if (msgidlen) {
			  if (sc->message_id)
			    fseek (srtcf,msgidlen + 2,SEEK_CUR);
			  else if ((getc (srtcf) != 'M') ||
				   (fread (sc->message_id = 
					   (char *) fs_get (msgidlen),1,
					   msgidlen-1,srtcf) != (msgidlen-1))||
				   (sc->message_id[msgidlen-1] = '\0') ||
				   (getc (srtcf) != '\015') ||
				   (getc (srtcf) != '\012')) {
			    msg = "message-id data";
			    break;
			  }
			}
			if (reflen) {
			  if (sc->references) fseek(srtcf,reflen + 2,SEEK_CUR);
				/* make sure it fits */
			  else {
			    if (reflen >= LOCAL->buflen) {
			      fs_give ((void **) &LOCAL->buf);
			      LOCAL->buf = (char *)
				fs_get ((LOCAL->buflen = reflen) + 1);
			      }
			    if ((getc (srtcf) != 'R') ||
				(fread (LOCAL->buf,1,reflen-1,srtcf) !=
				 (reflen - 1)) ||
				(LOCAL->buf[reflen-1] = '\0') ||
				(getc (srtcf) != '\015') ||
				(getc (srtcf) != '\012')) {
			      msg = "references data";
			      break;
			    }
			    for (s = LOCAL->buf,sl = NIL,
				   sc->references = mail_newstringlist ();
				 s && *s; s += i + 1) {
			      if ((i = strtoul (s,&s,16)) && (*s++ == ':') &&
				  (s[i] == ':')) {
				if (sl) sl = sl->next = mail_newstringlist();
				else sl = sc->references;
				s[i] = '\0';
				sl->text.data = cpystr (s);
				sl->text.size = i;
			      }
			      else s = NIL;
			    }
			    if (!s || *s ||
				(s != ((char *) LOCAL->buf + reflen - 1))) {
			      msg = "references length consistency check";
			      break;
			    }
			  }
			}
		      }

				/* UID not found, ignore this message */
		      else fseek (srtcf,((fromlen ? fromlen + 2 : 0) +
					 (tolen ? tolen + 2 : 0) +
					 (cclen ? cclen + 2 : 0) +
					 (subjlen ? subjlen + 2 : 0) +
					 (msgidlen ? msgidlen + 2 : 0) +
					 (reflen ? reflen + 2 : 0)),
				  SEEK_CUR);
		      continue;
		    }
		    else msg = "expansion";
		  }
		  else msg = "references";
		}
		else msg = "message-id";
	      }
	      else msg = "subject";
	    }
	    else msg = "cc";
	  }
	  else msg = "to";
	}
	else msg = "from";
      }
      else msg = "sentdate";
      break;			/* error somewhere */
    }
    if (!t || *t) {		/* error detected? */
      if (t) {			/* non-null means bogus record */
	sprintf (tmp,"Error in %s in mix sortcache record: %.500s",msg,t);
	MM_LOG (tmp,WARN);
      }
      fclose (srtcf);		/* either way, must punt */
      srtcf = NIL;
    }
  }
  if (rdonly && srtcf) {	/* can't update if readonly */
    unlink (LOCAL->sortcache);	/* try deleting it */
    fclose (srtcf);		/* so close it and return as if error */
    srtcf = NIL;
  }
  else fchmod (fd,sbuf.st_mode);
  return srtcf;
}

/* MIX update and close sortcache
 * Accepts: MAIL stream
 *	    pointer to open FILE (if FILE is NIL, do nothing)
 * Returns: T on success, NIL on error
 */

long mix_sortcache_update (MAILSTREAM *stream,FILE **sortcache)
{
  FILE *f = *sortcache;
  long ret = LONGT;
  if (f) {			/* ignore if no file */
    unsigned long i,j;
    mailcache_t mc = (mailcache_t) mail_parameters (NIL,GET_CACHE,NIL);
    for (i = 1; (i <= stream->nmsgs) &&
	   !((SORTCACHE *) (*mc) (stream,i,CH_SORTCACHE))->dirty; ++i);
    if (i <= stream->nmsgs) {	/* only update if some entry is dirty */
      rewind (f);		/* let's start at the very beginning */
				/* write sequence */
      fprintf (f,SEQFMT,LOCAL->sortcacheseq = mix_modseq(LOCAL->sortcacheseq));
      for (i = 1; ret && (i <= stream->nmsgs); ++i) {
	MESSAGECACHE *elt = mail_elt (stream,i);
	SORTCACHE *s = (SORTCACHE *) (*mc) (stream,i,CH_SORTCACHE);
	STRINGLIST *sl;
	s->dirty = NIL;		/* no longer dirty */
	if ((sl = s->references) != NULL)	/* count length of references */
	  for (j = 1; sl && sl->text.data; sl = sl->next)
	    j += 10 + sl->text.size;
	else j = 0;		/* no references yet */
	fprintf (f,SCRFMT,elt->private.uid,s->date,
		 s->from ? strlen (s->from) + 1 : 0,
		 s->to ? strlen (s->to) + 1 : 0,s->cc ? strlen (s->cc) + 1 : 0,
		 s->refwd ? 'R' : ' ',s->subject ? strlen (s->subject) + 1: 0,
		 s->message_id ? strlen (s->message_id) + 1 : 0,j);
	if (s->from) fprintf (f,"F%s\015\012",s->from);
	if (s->to) fprintf (f,"T%s\015\012",s->to);
	if (s->cc) fprintf (f,"C%s\015\012",s->cc);
	if (s->subject) fprintf (f,"S%s\015\012",s->subject);
	if (s->message_id) fprintf (f,"M%s\015\012",s->message_id);
	if (j) {		/* any references to write? */
	  fputc ('R',f);	/* yes, do so */
	  for (sl = s->references; sl && sl->text.data; sl = sl->next)
	    fprintf (f,"%08lx:%s:",sl->text.size,sl->text.data);
	  fputs ("\015\012",f);
	}
	if (ferror (f)) {
	  MM_LOG ("Error updating mix sortcache file",WARN);
	  ret = NIL;
	}
      }
      if (ret && fflush (f)) {
	MM_LOG ("Error flushing mix sortcache file",WARN);
	ret = NIL;
      }
      if (ret) ftruncate (fileno (f),ftell (f));
    }
    if (fclose (f)) {
      MM_LOG ("Error closing mix sortcache file",WARN);
      ret = NIL;
    }
  }
  return ret;
}

/* MIX generic file routines */

/* MIX read record
 * Accepts: open FILE
 *	    buffer
 *	    buffer length
 *	    record type
 * Returns: buffer if success, else NIL (zero-length buffer means EOF)
 */

char *mix_read_record (FILE *f,char *buf,unsigned long buflen,char *type)
{
  char *s,tmp[MAILTMPLEN];
				/* ensure string tied off */
  buf[buflen-2] = buf[buflen-1] = '\0';
  while (fgets (buf,buflen-1,f)) {
    if ((s = strchr (buf,'\012')) != NULL) {
      if ((s != buf) && (s[-1] == '\015')) --s;
      *s = '\0';		/* tie off buffer */
      if (s != buf) return buf;	/* return if non-empty buffer */
      sprintf (tmp,"Empty mix %s record",type);
      MM_LOG (tmp,WARN);
    }
    else if (buf[buflen-2]) {	/* overlong record is bad news */
      sprintf (tmp,"Oversize mix %s record: %.512s",type,buf);
      MM_LOG (tmp,ERROR);
      return NIL;
    }
    else {
      sprintf (tmp,"Truncated mix %s record: %.512s",type,buf);
      MM_LOG (tmp,WARN);
      return buf;		/* pass to caller anyway */
    }
  }
  buf[0] = '\0';		/* return empty buffer on EOF */
  return buf;
}

/* MIX read sequence record
 * Accepts: open FILE
 * Returns: sequence value, or NIL if failure
 */

unsigned long mix_read_sequence (FILE *f)
{
  unsigned long ret;
  char *s,tmp[MAILTMPLEN];
  if (!mix_read_record (f,tmp,MAILTMPLEN-1,"sequence")) return NIL;
  switch (tmp[0]) {		/* examine record */
  case '\0':			/* end of file */
    ret = 1;			/* start a new sequence regime */
    break;
  case 'S':			/* sequence record */
    if (isxdigit (tmp[1])) {	/* must be followed by hex value */
      ret = strtoul (tmp+1,&s,16);
      if (!*s) break;		/* and nothing more */
    }
				/* drop into default case */
  default:			/* anything else is an error */
    return NIL;			/* return error */
  }
  return ret;
}

/* MIX internal routines */


/* MIX mail build directory name
 * Accepts: destination string
 *          source
 * Returns: destination or empty string if error
 */

char *mix_dir (char *dst,char *name)
{
  char *s;
				/* empty string if mailboxfile fails */
  if (!mailboxfile (dst,name)) *dst = '\0';
				/* driver-selected INBOX  */
  else if (!*dst) mailboxfile (dst,"~/INBOX");
				/* tie off unnecessary trailing / */
  else if ((s = strrchr (dst,'/')) && !s[1]) *s = '\0';
  return dst;
}


/* MIX mail build file name
 * Accepts: destination string
 *	    directory name
 *	    file name
 * Returns: destination
 */

char *mix_file (char *dst,char *dir,char *name)
{
  sprintf (dst,"%.500s/%.80s%.80s",dir,MIXNAME,name);
  return dst;
}


/* MIX mail build file name from data file number
 * Accepts: destination string
 *	    directory name
 *	    data file number
 * Returns: destination
 */

char *mix_file_data (char *dst,char *dir,unsigned long data)
{
  char tmp[MAILTMPLEN];
  if (data) sprintf (tmp,"%08lx",data);
  else tmp[0] = '\0';		/* compatibility with experimental version */
  return mix_file (dst,dir,tmp);
}

/* MIX mail get new modseq
 * Accepts: old modseq
 * Returns: new modseq value
 */

unsigned long mix_modseq (unsigned long oldseq)
{
				/* normally time now */
  unsigned long ret = (unsigned long) time (NIL);
				/* ensure that modseq doesn't go backwards */
  if (ret <= oldseq) ret = oldseq + 1;
  return ret;
}