summarylogtreecommitdiffstats
path: root/nntp.patch
blob: 278691a834dd74924cafcda4a1928707220f2972 (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
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
From: Vsevolod Volkov <vvv@mutt.org.ua>
Date: Sun, 16 Mar 2014 15:54:12 +0100
Subject: nntp

http://mutt.org.ua/download/mutt-1.5.23/patch-1.5.23.vvv.nntp.gz

Signed-off-by: Matteo F. Vescovi <mfvescovi@gmail.com>

Gbp-Pq: Topic mutt-patched
---
 ChangeLog.nntp      |  399 +++++++++
 Makefile.am         |    2 +
 OPS                 |   23 +-
 account.c           |   27 +
 account.h           |    3 +-
 attach.h            |    2 +-
 browser.c           |  470 +++++++++-
 browser.h           |    7 +
 buffy.c             |   12 +
 complete.c          |   64 ++
 compose.c           |  177 +++-
 configure.ac        |   11 +-
 curs_main.c         |  312 ++++++-
 doc/Muttrc          |  300 +++++++
 doc/manual.xml.head |   20 +
 doc/mutt.man        |    8 +-
 functions.h         |   59 +-
 globals.h           |   16 +
 hash.c              |   27 +
 hash.h              |    3 +-
 hcache.c            |   12 +
 hdrline.c           |   25 +
 headers.c           |    3 +
 init.c              |   22 +
 init.h              |  217 +++++
 keymap.c            |    1 -
 mailbox.h           |    3 +
 main.c              |   54 ++
 mutt.h              |   39 +-
 mutt_sasl.c         |    5 +
 muttlib.c           |   17 +-
 mx.c                |   75 ++
 mx.h                |    3 +
 newsrc.c            | 1260 +++++++++++++++++++++++++++
 nntp.c              | 2404 +++++++++++++++++++++++++++++++++++++++++++++++++++
 nntp.h              |  167 ++++
 pager.c             |   81 +-
 parse.c             |   48 +-
 pattern.c           |    8 +
 po/POTFILES.in      |    2 +
 postpone.c          |   11 +
 protos.h            |    1 +
 recvattach.c        |   34 +-
 recvcmd.c           |   61 +-
 send.c              |  163 +++-
 sendlib.c           |   78 +-
 sort.c              |   16 +
 url.c               |    4 +-
 url.h               |    2 +
 49 files changed, 6674 insertions(+), 84 deletions(-)
 create mode 100644 ChangeLog.nntp
 create mode 100644 newsrc.c
 create mode 100644 nntp.c
 create mode 100644 nntp.h

diff --git a/ChangeLog.nntp b/ChangeLog.nntp
new file mode 100644
index 0000000..70d1126
--- /dev/null
+++ b/ChangeLog.nntp
@@ -0,0 +1,399 @@
+* Thu Mar 13 2014 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.23
+
+* Tue Oct 29 2013 Vsevolod Volkov <vvv@mutt.org.ua>
+- minor bug fixed while removing new articles
+
+* Fri Oct 18 2013 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.22
+
+* Tue Nov 27 2012 Vsevolod Volkov <vvv@mutt.org.ua>
+- SASL authentication
+- new option nntp_authenticators
+
+* Fri Nov 16 2012 Vsevolod Volkov <vvv@mutt.org.ua>
+- support of NNTP commands: CAPABILITIES, STARTTLS, LIST NEWSGROUPS,
+  LIST OVERVIEW.FMT, OVER, DATE
+- added bcache support
+- newss URI scheme renamed to snews
+- removed option nntp_reconnect
+
+* Sun Sep 16 2012 Vsevolod Volkov <vvv@mutt.org.ua>
+- internal header caching replaced with hcache
+- new option newsgroups_charset
+
+* Wed Sep 16 2010 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.21
+
+* Thu Aug 13 2009 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed writting references in nntp_save_cache_group()
+
+* Tue Jun 15 2009 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.20
+
+* Tue Mar 20 2009 Vsevolod Volkov <vvv@mutt.org.ua>
+- save Date: header of recorded outgoing articles
+
+* Tue Jan  6 2009 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.19
+
+* Mon May 19 2008 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.18
+- fixed SIGSEGV when followup or forward to newsgroup
+
+* Sun Nov  4 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.17
+
+* Tue Jul  3 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed arguments of nntp_format_str()
+
+* Fri Jun 15 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed error selecting news group
+
+* Tue Jun 12 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.16
+
+* Wed Apr 11 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed posting error if $smtp_url is set
+- added support of print-style sequence %R (x-comment-to)
+
+* Sun Apr  8 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.15
+- nntp://... url changed to news://...
+- added indicator of fetching descriptions progress
+
+* Tue Feb 28 2007 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.14
+
+* Tue Aug 15 2006 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.13
+
+* Mon Jul 17 2006 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.12
+- fixed reading empty .newsrc
+
+* Sat Sep 17 2005 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.11
+
+* Sat Aug 13 2005 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.10
+
+* Sun Mar 13 2005 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.9
+
+* Sun Feb 13 2005 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.8
+
+* Sat Feb  5 2005 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.7
+- function mutt_update_list_file() moved to newsrc.c and changed algorithm
+
+* Thu Jul  8 2004 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed error in nntp_logout_all()
+
+* Sat Apr  3 2004 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed debug output in mutt_newsrc_update()
+- added optional support of LISTGROUP command
+- fixed typo in nntp_parse_xref()
+
+* Tue Feb  3 2004 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.6
+
+* Thu Dec 18 2003 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed compose menu
+
+* Thu Nov  6 2003 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.5.1
+
+* Wed Nov  5 2003 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.5
+- added space after newsgroup name in .newsrc file
+
+* Sun May 18 2003 Vsevolod Volkov <vvv@mutt.org.ua>
+- nntp patch: fixed SIGSEGV when posting article
+
+* Sat Mar 22 2003 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.4
+
+* Sat Dec 21 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.3
+- replace safe_free calls by the FREE macro
+
+* Fri Dec  6 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.2
+- nntp authentication can be passed after any command
+
+* Sat May  4 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.5.1
+
+* Thu May  2 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.99
+
+* Wed Mar 13 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.28
+- fixed SIGSEGV in <get-message>, <get-parent>, <get-children>,
+  <reconstruct-thread> functions
+- fixed message about nntp reconnect
+- fixed <attach-news-message> function using browser
+- added support of Followup-To: poster
+- added %n (new articles) in group_index_format
+- posting articles without inews by default
+
+* Wed Jan 23 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.27
+
+* Fri Jan 18 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.26
+
+* Thu Jan  3 2002 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.25
+- accelerated speed of access to news->newsgroups hash (by <gul@gul.kiev.ua>)
+- added default content disposition
+
+* Mon Dec  3 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.24
+
+* Fri Nov  9 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.23.2
+- fixed segfault if mutt_conn_find() returns null
+
+* Wed Oct 31 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.23.1
+- added support of LISTGROUP command
+- added support for servers with broken overview
+- disabled <flag-message> function on news server
+- fixed error storing bad authentication information
+
+* Wed Oct 10 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.23
+- fixed typo in buffy.c
+- added substitution of %s parameter in $inews variable
+
+* Fri Aug 31 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.22.1
+- update to 1.3.22
+
+* Thu Aug 23 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.21
+
+* Wed Jul 25 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.20
+- removed 'server-hook', use 'account-hook' instead
+- fixed error opening NNTP server without newsgroup using -f option
+
+* Fri Jun  8 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.19
+
+* Sat May  5 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.18
+- fixed typo in nntp_attempt_features()
+- changed algorithm of XGTITLE command testing
+- disabled writing of NNTP password in debug file
+- fixed reading and writing of long newsrc lines
+- changed checking of last line while reading lines from server
+- fixed possible buffer overrun in nntp_parse_newsrc_line()
+- removed checking of XHDR command
+- compare NNTP return codes without trailing space
+
+* Thu Mar 29 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.17
+- support for 'LIST NEWSGROUPS' command to read descriptions
+
+* Fri Mar  2 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.16
+
+* Wed Feb 14 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.15
+
+* Sun Jan 28 2001 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.14
+- show number of tagged messages patch from Felix von Leitner <leitner@fefe.de>
+
+* Sun Dec 31 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.13
+
+* Sat Dec 30 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- Fixed problem if last article in group is deleted
+
+* Fri Dec 22 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- Fixed checking of XGTITLE command on some servers
+
+* Mon Dec 18 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- Added \r in AUTHINFO commands
+
+* Mon Nov 27 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.12
+
+* Wed Nov  1 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.11
+- fixed error opening newsgroup from mutt started with -g or -G
+
+* Thu Oct 12 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.10
+- hotkey 'G' (get-message) replaced with '^G'
+
+* Thu Sep 21 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.9
+- changed delay displaying error messages from 1 to 2 seconds
+- fixed error compiling with nntp and without imap
+
+* Wed Sep  6 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- fixed catchup in index
+- fixed nntp_open_mailbox()
+
+* Sat Sep  2 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- functions <edit> and <delete-entry> disabled
+- format of news mailbox names changed to url form
+- option nntp_attempts removed
+- option reconnect_news renamed to nntp_reconnect
+- default value of nntp_poll changed from 30 to 60
+- error handling improved
+
+* Wed Aug 30 2000 Vsevolod Volkov <vvv@mutt.org.ua>
+- update to 1.3.8
+- new option show_only_unread
+- add newsgroup completion
+
+* Fri Aug  4 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.7
+
+* Sat Jul 29 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.6
+
+* Sun Jul  9 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.5
+- authentication code update
+- fix for changing to newsgroup from mailbox with read messages
+- socket code optimization
+
+* Wed Jun 21 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.4
+
+* Wed Jun 14 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- don't substitute current newsgroup with deleted new messages
+
+* Mon Jun 12 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.3
+- fix for substitution of newsgroup after reconnection
+- fix for loading newsgroups with very long names
+- fix for loading more than 32768 newsgroups
+
+* Wed May 24 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.2
+
+* Sat May 20 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3.1
+
+* Fri May 12 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.3
+
+* Thu May 11 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.2
+
+* Thu May  4 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.14
+
+* Sun Apr 23 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.12
+
+* Fri Apr  7 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- add substitution of newsgroup with new messages by default
+
+* Wed Apr  5 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- add attach message from newsgroup
+- add one-line help in newsreader mode
+- disable 'change-dir' command in newsgroups browser
+- add -G option
+
+* Tue Apr  4 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- get default news server name from file /etc/nntpserver
+- use case insensitive server names
+- add print-style sequence %s to $newsrc
+- add -g option
+
+* Sat Apr  1 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- remove 'X-FTN-Origin' header processing
+
+* Thu Mar 30 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.11
+- update to 1.1.10
+
+* Thu Mar 23 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix mutt_select_newsserver()
+- remove 'toggle-mode' function
+- add 'change-newsgroup' function
+
+* Wed Mar 22 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix server-hook
+
+* Tue Mar 21 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix error 'bounce' function after 'post'
+- add 'forward to newsgroup' function
+
+* Mon Mar 20 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- 'forward' function works in newsreader mode
+- add 'post' and 'followup' functions to pager and attachment menu
+- fix active descriptions and allowed flag reload
+
+* Tue Mar 14 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.9
+- remove deleted newsgroups from list
+
+* Mon Mar 13 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update .newsrc in browser
+
+* Sun Mar 12 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- reload .newsrc if externally modified
+- fix active cache update
+
+* Sun Mar  5 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.8
+
+* Sat Mar  4 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- patch *.update_list_file is not required
+- count lines when loading descriptions
+- remove cache of unsubscribed newsgroups
+
+* Thu Mar  2 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- load list of newsgroups from cache faster
+
+* Wed Mar  1 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.7
+
+* Tue Feb 29 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix unread messages in browser
+- fix newsrc_gen_entries()
+
+* Mon Feb 28 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix mutt_newsgroup_stat()
+- fix nntp_delete_cache()
+- fix nntp_get_status()
+- fix check_children()
+- fix nntp_fetch_headers()
+
+* Fri Feb 25 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.5
+
+* Thu Feb 24 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix updating new messages in cache
+
+* Mon Feb 21 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- change default cache filenames
+- fix updating new messages in cache
+
+* Fri Feb 18 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- fix segmentation fault in news groups browser
+
+* Tue Feb 15 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.4
+
+* Thu Feb 10 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.3
+
+* Sun Jan 30 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- add X-Comment-To editing
+- add my_hdr support for Newsgroups:, Followup-To: and X-Comment-To: headers
+- add variables $ask_followup_to and $ask_x_comment_to
+
+* Fri Jan 28 2000 Vsevolod Volkov <vvv@mutt.kiev.ua>
+- update to 1.1.2
diff --git a/Makefile.am b/Makefile.am
index cf1ac98..20b4fad 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -57,6 +57,7 @@ EXTRA_mutt_SOURCES = account.c bcache.c crypt-gpgme.c crypt-mod-pgp-classic.c \
 	mutt_idna.c mutt_sasl.c mutt_socket.c mutt_ssl.c mutt_ssl_gnutls.c \
 	mutt_tunnel.c pgp.c pgpinvoke.c pgpkey.c pgplib.c pgpmicalg.c \
 	pgppacket.c pop.c pop_auth.c pop_lib.c remailer.c resize.c sha1.c \
+	nntp.c newsrc.c \
 	smime.c smtp.c utf8.c wcwidth.c \
 	bcache.h browser.h hcache.h mbyte.h mutt_idna.h remailer.h url.h
 
@@ -68,6 +69,7 @@ EXTRA_DIST = COPYRIGHT GPL OPS OPS.PGP OPS.CRYPT OPS.SMIME TODO UPDATING \
 	mutt_regex.h mutt_sasl.h mutt_socket.h mutt_ssl.h mutt_tunnel.h \
 	mx.h pager.h pgp.h pop.h protos.h rfc1524.h rfc2047.h \
 	rfc2231.h rfc822.h rfc3676.h sha1.h sort.h mime.types VERSION prepare \
+	nntp.h ChangeLog.nntp \
 	_regex.h OPS.MIX README.SECURITY remailer.c remailer.h browser.h \
 	mbyte.h lib.h extlib.c pgpewrap.c smime_keys.pl pgplib.h \
 	README.SSL smime.h group.h \
diff --git a/OPS b/OPS
index 3ffb82a..62db4b4 100644
--- a/OPS
+++ b/OPS
@@ -8,14 +8,16 @@ OP_BOUNCE_MESSAGE "remail a message to another user"
 OP_BROWSER_NEW_FILE "select a new file in this directory"
 OP_BROWSER_VIEW_FILE "view file"
 OP_BROWSER_TELL "display the currently selected file's name"
-OP_BROWSER_SUBSCRIBE "subscribe to current mailbox (IMAP only)"
-OP_BROWSER_UNSUBSCRIBE "unsubscribe from current mailbox (IMAP only)"
+OP_BROWSER_SUBSCRIBE "subscribe to current mbox (IMAP/NNTP only)"
+OP_BROWSER_UNSUBSCRIBE "unsubscribe from current mbox (IMAP/NNTP only)"
 OP_BROWSER_TOGGLE_LSUB "toggle view all/subscribed mailboxes (IMAP only)"
 OP_BUFFY_LIST "list mailboxes with new mail"
+OP_CATCHUP "mark all articles in newsgroup as read"
 OP_CHANGE_DIRECTORY "change directories"
 OP_CHECK_NEW "check mailboxes for new mail"
 OP_COMPOSE_ATTACH_FILE "attach file(s) to this message"
 OP_COMPOSE_ATTACH_MESSAGE "attach message(s) to this message"
+OP_COMPOSE_ATTACH_NEWS_MESSAGE "attach news article(s) to this message"
 OP_COMPOSE_EDIT_BCC "edit the BCC list"
 OP_COMPOSE_EDIT_CC "edit the CC list"
 OP_COMPOSE_EDIT_DESCRIPTION "edit attachment description"
@@ -26,7 +28,10 @@ OP_COMPOSE_EDIT_FROM "edit the from field"
 OP_COMPOSE_EDIT_HEADERS "edit the message with headers"
 OP_COMPOSE_EDIT_MESSAGE "edit the message"
 OP_COMPOSE_EDIT_MIME "edit attachment using mailcap entry"
+OP_COMPOSE_EDIT_NEWSGROUPS "edit the newsgroups list"
 OP_COMPOSE_EDIT_REPLY_TO "edit the Reply-To field"
+OP_COMPOSE_EDIT_FOLLOWUP_TO "edit the Followup-To field"
+OP_COMPOSE_EDIT_X_COMMENT_TO "edit the X-Comment-To field"
 OP_COMPOSE_EDIT_SUBJECT "edit the subject of this message"
 OP_COMPOSE_EDIT_TO "edit the TO list"
 OP_CREATE_MAILBOX "create a new mailbox (IMAP only)"
@@ -85,8 +90,13 @@ OP_EXIT "exit this menu"
 OP_FILTER "filter attachment through a shell command"
 OP_FIRST_ENTRY "move to the first entry"
 OP_FLAG_MESSAGE "toggle a message's 'important' flag"
+OP_FOLLOWUP "followup to newsgroup"
+OP_FORWARD_TO_GROUP "forward to newsgroup"
 OP_FORWARD_MESSAGE "forward a message with comments"
 OP_GENERIC_SELECT_ENTRY "select the current entry"
+OP_GET_CHILDREN "get all children of the current message"
+OP_GET_MESSAGE "get message with Message-Id"
+OP_GET_PARENT "get parent of the current message"
 OP_GROUP_REPLY "reply to all recipients"
 OP_HALF_DOWN "scroll down 1/2 page"
 OP_HALF_UP "scroll up 1/2 page"
@@ -94,11 +104,14 @@ OP_HELP "this screen"
 OP_JUMP "jump to an index number"
 OP_LAST_ENTRY "move to the last entry"
 OP_LIST_REPLY "reply to specified mailing list"
+OP_LOAD_ACTIVE "load list of all newsgroups from NNTP server"
 OP_MACRO "execute a macro"
 OP_MAIL "compose a new mail message"
 OP_MAIN_BREAK_THREAD "break the thread in two"
 OP_MAIN_CHANGE_FOLDER "open a different folder"
 OP_MAIN_CHANGE_FOLDER_READONLY "open a different folder in read only mode"
+OP_MAIN_CHANGE_GROUP "open a different newsgroup"
+OP_MAIN_CHANGE_GROUP_READONLY "open a different newsgroup in read only mode"
 OP_MAIN_CLEAR_FLAG "clear a status flag from a message"
 OP_MAIN_DELETE_PATTERN "delete messages matching a pattern"
 OP_MAIN_IMAP_FETCH "force retrieval of mail from IMAP server"
@@ -138,6 +151,7 @@ OP_PAGER_HIDE_QUOTED "toggle display of quoted text"
 OP_PAGER_SKIP_QUOTED "skip beyond quoted text"
 OP_PAGER_TOP "jump to the top of the message"
 OP_PIPE "pipe message/attachment to a shell command"
+OP_POST "post message to newsgroup"
 OP_PREV_ENTRY "move to the previous entry"
 OP_PREV_LINE "scroll up one line"
 OP_PREV_PAGE "move to the previous page"
@@ -147,6 +161,7 @@ OP_QUERY "query external program for addresses"
 OP_QUERY_APPEND "append new query results to current results"
 OP_QUIT "save changes to mailbox and quit"
 OP_RECALL_MESSAGE "recall a postponed message"
+OP_RECONSTRUCT_THREAD "reconstruct thread containing current message"
 OP_REDRAW "clear and redraw the screen"
 OP_REFORMAT_WINCH "{internal}"
 OP_RENAME_MAILBOX "rename the current mailbox (IMAP only)"
@@ -161,18 +176,22 @@ OP_SEARCH_TOGGLE "toggle search pattern coloring"
 OP_SHELL_ESCAPE "invoke a command in a subshell"
 OP_SORT "sort messages"
 OP_SORT_REVERSE "sort messages in reverse order"
+OP_SUBSCRIBE_PATTERN "subscribe to newsgroups matching a pattern"
 OP_TAG "tag the current entry"
 OP_TAG_PREFIX "apply next function to tagged messages"
 OP_TAG_PREFIX_COND "apply next function ONLY to tagged messages"
 OP_TAG_SUBTHREAD "tag the current subthread"
 OP_TAG_THREAD "tag the current thread"
 OP_TOGGLE_NEW "toggle a message's 'new' flag"
+OP_TOGGLE_READ "toggle view of read messages"
 OP_TOGGLE_WRITE "toggle whether the mailbox will be rewritten"
 OP_TOGGLE_MAILBOXES "toggle whether to browse mailboxes or all files"
 OP_TOP_PAGE "move to the top of the page"
+OP_UNCATCHUP "mark all articles in newsgroup as unread"
 OP_UNDELETE "undelete the current entry"
 OP_UNDELETE_THREAD "undelete all messages in thread"
 OP_UNDELETE_SUBTHREAD "undelete all messages in subthread"
+OP_UNSUBSCRIBE_PATTERN "unsubscribe from newsgroups matching a pattern"
 OP_VERSION "show the Mutt version number and date"
 OP_VIEW_ATTACH "view attachment using mailcap entry if necessary"
 OP_VIEW_ATTACHMENTS "show MIME attachments"
diff --git a/account.c b/account.c
index bf59995..624f931 100644
--- a/account.c
+++ b/account.c
@@ -51,8 +51,17 @@ int mutt_account_match (const ACCOUNT* a1, const ACCOUNT* a2)
     user = PopUser;
 #endif
   
+#ifdef USE_NNTP
+  if (a1->type == M_ACCT_TYPE_NNTP && NntpUser)
+    user = NntpUser;
+#endif
+
   if (a1->flags & a2->flags & M_ACCT_USER)
     return (!strcmp (a1->user, a2->user));
+#ifdef USE_NNTP
+  if (a1->type == M_ACCT_TYPE_NNTP)
+    return a1->flags & M_ACCT_USER && a1->user[0] ? 0 : 1;
+#endif
   if (a1->flags & M_ACCT_USER)
     return (!strcmp (a1->user, user));
   if (a2->flags & M_ACCT_USER)
@@ -130,6 +139,16 @@ void mutt_account_tourl (ACCOUNT* account, ciss_url_t* url)
   }
 #endif
 
+#ifdef USE_NNTP
+  if (account->type == M_ACCT_TYPE_NNTP)
+  {
+    if (account->flags & M_ACCT_SSL)
+      url->scheme = U_NNTPS;
+    else
+      url->scheme = U_NNTP;
+  }
+#endif
+
   url->host = account->host;
   if (account->flags & M_ACCT_PORT)
     url->port = account->port;
@@ -155,6 +174,10 @@ int mutt_account_getuser (ACCOUNT* account)
   else if ((account->type == M_ACCT_TYPE_POP) && PopUser)
     strfcpy (account->user, PopUser, sizeof (account->user));
 #endif
+#ifdef USE_NNTP
+  else if ((account->type == M_ACCT_TYPE_NNTP) && NntpUser)
+    strfcpy (account->user, NntpUser, sizeof (account->user));
+#endif
   else if (option (OPTNOCURSES))
     return -1;
   /* prompt (defaults to unix username), copy into account->user */
@@ -217,6 +240,10 @@ int mutt_account_getpass (ACCOUNT* account)
   else if ((account->type == M_ACCT_TYPE_SMTP) && SmtpPass)
     strfcpy (account->pass, SmtpPass, sizeof (account->pass));
 #endif
+#ifdef USE_NNTP
+  else if ((account->type == M_ACCT_TYPE_NNTP) && NntpPass)
+    strfcpy (account->pass, NntpPass, sizeof (account->pass));
+#endif
   else if (option (OPTNOCURSES))
     return -1;
   else
diff --git a/account.h b/account.h
index 662fb32..9febee6 100644
--- a/account.h
+++ b/account.h
@@ -29,7 +29,8 @@ enum
   M_ACCT_TYPE_NONE = 0,
   M_ACCT_TYPE_IMAP,
   M_ACCT_TYPE_POP,
-  M_ACCT_TYPE_SMTP
+  M_ACCT_TYPE_SMTP,
+  M_ACCT_TYPE_NNTP
 };
 
 /* account flags */
diff --git a/attach.h b/attach.h
index 928408a..071f22c 100644
--- a/attach.h
+++ b/attach.h
@@ -50,7 +50,7 @@ void mutt_print_attachment_list (FILE *fp, int tag, BODY *top);
 
 void mutt_attach_bounce (FILE *, HEADER *, ATTACHPTR **, short, BODY *);
 void mutt_attach_resend (FILE *, HEADER *, ATTACHPTR **, short, BODY *);
-void mutt_attach_forward (FILE *, HEADER *, ATTACHPTR **, short, BODY *);
+void mutt_attach_forward (FILE *, HEADER *, ATTACHPTR **, short, BODY *, int);
 void mutt_attach_reply (FILE *, HEADER *, ATTACHPTR **, short, BODY *, int);
 
 #endif /* _ATTACH_H_ */
diff --git a/browser.c b/browser.c
index dbb31c8..3c84044 100644
--- a/browser.c
+++ b/browser.c
@@ -32,6 +32,9 @@
 #ifdef USE_IMAP
 #include "imap.h"
 #endif
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
 
 #include <stdlib.h>
 #include <dirent.h>
@@ -50,6 +53,19 @@ static const struct mapping_t FolderHelp[] = {
   { NULL,	 0 }
 };
 
+#ifdef USE_NNTP
+static struct mapping_t FolderNewsHelp[] = {
+  { N_("Exit"),        OP_EXIT },
+  { N_("List"),        OP_TOGGLE_MAILBOXES },
+  { N_("Subscribe"),   OP_BROWSER_SUBSCRIBE },
+  { N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE },
+  { N_("Catchup"),     OP_CATCHUP },
+  { N_("Mask"),        OP_ENTER_MASK },
+  { N_("Help"),        OP_HELP },
+  { NULL,              0 }
+};
+#endif
+
 typedef struct folder_t
 {
   struct folder_file *ff;
@@ -116,9 +132,17 @@ static void browser_sort (struct browser_state *state)
     case SORT_ORDER:
       return;
     case SORT_DATE:
+#ifdef USE_NNTP
+      if (option (OPTNEWS))
+	return;
+#endif
       f = browser_compare_date;
       break;
     case SORT_SIZE:
+#ifdef USE_NNTP
+      if (option (OPTNEWS))
+	return;
+#endif
       f = browser_compare_size;
       break;
     case SORT_SUBJECT:
@@ -325,8 +349,111 @@ folder_format_str (char *dest, size_t destlen, size_t col, char op, const char *
   return (src);
 }
 
+#ifdef USE_NNTP
+static const char *
+newsgroup_format_str (char *dest, size_t destlen, size_t col, char op, const char *src,
+		      const char *fmt, const char *ifstring, const char *elsestring,
+		      unsigned long data, format_flag flags)
+{
+  char fn[SHORT_STRING], tmp[SHORT_STRING];
+  FOLDER *folder = (FOLDER *) data;
+
+  switch (op)
+  {
+    case 'C':
+      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+      snprintf (dest, destlen, tmp, folder->num + 1);
+      break;
+
+    case 'f':
+      strncpy (fn, folder->ff->name, sizeof(fn) - 1);
+      snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+      snprintf (dest, destlen, tmp, fn);
+      break;
+
+    case 'N':
+      snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
+      if (folder->ff->nd->subscribed)
+	snprintf (dest, destlen, tmp, ' ');
+      else
+	snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
+      break;
+
+    case 'M':
+      snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
+      if (folder->ff->nd->deleted)
+	snprintf (dest, destlen, tmp, 'D');
+      else
+	snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
+      break;
+
+    case 's':
+      if (flags & M_FORMAT_OPTIONAL)
+      {
+	if (folder->ff->nd->unread != 0)
+	  mutt_FormatString (dest, destlen, col, ifstring, newsgroup_format_str,
+		data, flags);
+	else
+	  mutt_FormatString (dest, destlen, col, elsestring, newsgroup_format_str,
+		data, flags);
+      }
+      else if (Context && Context->data == folder->ff->nd)
+      {
+	snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+	snprintf (dest, destlen, tmp, Context->unread);
+      }
+      else
+      {
+	snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+	snprintf (dest, destlen, tmp, folder->ff->nd->unread);
+      }
+      break;
+
+    case 'n':
+      if (Context && Context->data == folder->ff->nd)
+      {
+	snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+	snprintf (dest, destlen, tmp, Context->new);
+      }
+      else if (option (OPTMARKOLD) &&
+		folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
+		folder->ff->nd->lastCached <= folder->ff->nd->lastMessage)
+      {
+	snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+	snprintf (dest, destlen, tmp, folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
+      }
+      else
+      {
+	snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+	snprintf (dest, destlen, tmp, folder->ff->nd->unread);
+      }
+      break;
+
+    case 'd':
+      if (folder->ff->nd->desc != NULL)
+      {
+	char *buf = safe_strdup (folder->ff->nd->desc);
+	if (NewsgroupsCharset && *NewsgroupsCharset)
+	  mutt_convert_string (&buf, NewsgroupsCharset, Charset, M_ICONV_HOOK_FROM);
+	mutt_filter_unprintable (&buf);
+
+	snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+	snprintf (dest, destlen, tmp, buf);
+      }
+      else
+      {
+	snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+	snprintf (dest, destlen, tmp, "");
+      }
+      break;
+  }
+  return (src);
+}
+#endif /* USE_NNTP */
+
 static void add_folder (MUTTMENU *m, struct browser_state *state,
-			const char *name, const struct stat *s, unsigned int new)
+			const char *name, const struct stat *s,
+			void *data, unsigned int new)
 {
   if (state->entrylen == state->entrymax)
   {
@@ -355,6 +482,10 @@ static void add_folder (MUTTMENU *m, struct browser_state *state,
 #ifdef USE_IMAP
   (state->entry)[state->entrylen].imap = 0;
 #endif
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+    (state->entry)[state->entrylen].nd = (NNTP_DATA *)data;
+#endif
   (state->entrylen)++;
 }
 
@@ -370,9 +501,36 @@ static void init_state (struct browser_state *state, MUTTMENU *menu)
     menu->data = state->entry;
 }
 
+/* get list of all files/newsgroups with mask */
 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
 			      char *d, const char *prefix)
 {
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+  {
+    NNTP_SERVER *nserv = CurrentNewsSrv;
+    unsigned int i;
+
+/*  mutt_buffy_check (0); */
+    init_state (state, menu);
+
+    for (i = 0; i < nserv->groups_num; i++)
+    {
+      NNTP_DATA *nntp_data = nserv->groups_list[i];
+      if (!nntp_data)
+	continue;
+      if (prefix && *prefix &&
+	  strncmp (prefix, nntp_data->group, strlen (prefix)))
+	continue;
+      if (!((regexec (Mask.rx, nntp_data->group, 0, NULL, 0) == 0) ^ Mask.not))
+	continue;
+      add_folder (menu, state, nntp_data->group, NULL,
+		  nntp_data, nntp_data->new);
+    }
+  }
+  else
+#endif /* USE_NNTP */
+  {
   struct stat s;
   DIR *dp;
   struct dirent *de;
@@ -433,17 +591,41 @@ static int examine_directory (MUTTMENU *menu, struct browser_state *state,
     tmp = Incoming;
     while (tmp && mutt_strcmp (buffer, tmp->path))
       tmp = tmp->next;
-    add_folder (menu, state, de->d_name, &s, (tmp) ? tmp->new : 0);
+    add_folder (menu, state, de->d_name, &s, NULL, (tmp) ? tmp->new : 0);
   }
   closedir (dp);  
+  }
   browser_sort (state);
   return 0;
 }
 
+/* get list of mailboxes/subscribed newsgroups */
 static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
 {
   struct stat s;
   char buffer[LONG_STRING];
+
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+  {
+    NNTP_SERVER *nserv = CurrentNewsSrv;
+    unsigned int i;
+
+/*  mutt_buffy_check (0); */
+    init_state (state, menu);
+
+    for (i = 0; i < nserv->groups_num; i++)
+    {
+      NNTP_DATA *nntp_data = nserv->groups_list[i];
+      if (nntp_data && (nntp_data->new || (nntp_data->subscribed &&
+	 (nntp_data->unread || !option (OPTSHOWONLYUNREAD)))))
+	add_folder (menu, state, nntp_data->group, NULL,
+		    nntp_data, nntp_data->new);
+    }
+  }
+  else
+#endif
+  {
   BUFFY *tmp = Incoming;
 #ifdef USE_IMAP
   struct mailbox_state mbox;
@@ -461,14 +643,21 @@ static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
     if (mx_is_imap (tmp->path))
     {
       imap_mailbox_state (tmp->path, &mbox);
-      add_folder (menu, state, tmp->path, NULL, mbox.new);
+      add_folder (menu, state, tmp->path, NULL, NULL, mbox.new);
       continue;
     }
 #endif
 #ifdef USE_POP
     if (mx_is_pop (tmp->path))
     {
-      add_folder (menu, state, tmp->path, NULL, tmp->new);
+      add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
+      continue;
+    }
+#endif
+#ifdef USE_NNTP
+    if (mx_is_nntp (tmp->path))
+    {
+      add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
       continue;
     }
 #endif
@@ -497,15 +686,20 @@ static int examine_mailboxes (MUTTMENU *menu, struct browser_state *state)
     strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
     mutt_pretty_mailbox (buffer, sizeof (buffer));
 
-    add_folder (menu, state, buffer, &s, tmp->new);
+    add_folder (menu, state, buffer, &s, NULL, tmp->new);
   }
   while ((tmp = tmp->next));
+  }
   browser_sort (state);
   return 0;
 }
 
 static int select_file_search (MUTTMENU *menu, regex_t *re, int n)
 {
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+    return (regexec (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
+#endif
   return (regexec (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
 }
 
@@ -516,6 +710,12 @@ static void folder_entry (char *s, size_t slen, MUTTMENU *menu, int num)
   folder.ff = &((struct folder_file *) menu->data)[num];
   folder.num = num;
   
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+    mutt_FormatString (s, slen, 0, NONULL(GroupFormat), newsgroup_format_str, 
+      (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
+  else
+#endif
   mutt_FormatString (s, slen, 0, NONULL(FolderFormat), folder_format_str, 
       (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
 }
@@ -536,6 +736,17 @@ static void init_menu (struct browser_state *state, MUTTMENU *menu, char *title,
 
   menu->tagged = 0;
   
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+  {
+    if (buffy)
+      snprintf (title, titlelen, _("Subscribed newsgroups"));
+    else
+      snprintf (title, titlelen, _("Newsgroups on server [%s]"),
+		CurrentNewsSrv->conn->account.host);
+  }
+  else
+#endif
   if (buffy)
   {
     menu->is_mailbox_list = 1;
@@ -609,6 +820,31 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *num
   if (!folder)
     strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
 
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+  {
+    if (*f)
+      strfcpy (prefix, f, sizeof (prefix));
+    else
+    {
+      NNTP_SERVER *nserv = CurrentNewsSrv;
+      unsigned int i;
+
+      /* default state for news reader mode is browse subscribed newsgroups */
+      buffy = 0;
+      for (i = 0; i < nserv->groups_num; i++)
+      {
+	NNTP_DATA *nntp_data = nserv->groups_list[i];
+	if (nntp_data && nntp_data->subscribed)
+	{
+	  buffy = 1;
+	  break;
+	}
+      }
+    }
+  }
+  else
+#endif
   if (*f)
   {
     mutt_expand_path (f, flen);
@@ -705,6 +941,9 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *num
     menu->tag = file_tag;
 
   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
+#ifdef USE_NNTP
+    option (OPTNEWS) ? FolderNewsHelp :
+#endif
     FolderHelp);
 
   init_menu (&state, menu, title, sizeof (title), buffy);
@@ -842,7 +1081,11 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *num
 	  }
 	}
 
+#ifdef USE_NNTP
+	if (buffy || option (OPTNEWS))
+#else
 	if (buffy)
+#endif
 	{
 	  strfcpy (f, state.entry[menu->current].name, flen);
 	  mutt_expand_path (f, flen);
@@ -900,14 +1143,6 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *num
         break;
 
 #ifdef USE_IMAP
-      case OP_BROWSER_SUBSCRIBE:
-	imap_subscribe (state.entry[menu->current].name, 1);
-	break;
-
-      case OP_BROWSER_UNSUBSCRIBE:
-	imap_subscribe (state.entry[menu->current].name, 0);
-	break;
-
       case OP_BROWSER_TOGGLE_LSUB:
 	if (option (OPTIMAPLSUB))
 	  unset_option (OPTIMAPLSUB);
@@ -1008,6 +1243,11 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *num
       
       case OP_CHANGE_DIRECTORY:
 
+#ifdef USE_NNTP
+	if (option (OPTNEWS))
+	  break;
+#endif
+
 	strfcpy (buf, LastDir, sizeof (buf));
 #ifdef USE_IMAP
 	if (!state.imap_browse)
@@ -1273,6 +1513,210 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, int *num
 	  else
 	    mutt_error _("Error trying to view file");
 	}
+	break;
+
+#ifdef USE_NNTP
+      case OP_CATCHUP:
+      case OP_UNCATCHUP:
+	if (option (OPTNEWS))
+	{
+	  struct folder_file *f = &state.entry[menu->current];
+	  int rc;
+	  NNTP_DATA *nntp_data;
+
+	  rc = nntp_newsrc_parse (CurrentNewsSrv);
+	  if (rc < 0)
+	    break;
+
+	  if (i == OP_CATCHUP)
+	    nntp_data = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
+	  else
+	    nntp_data = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
+
+	  if (nntp_data)
+	  {
+/*	    FOLDER folder;
+	    struct folder_file ff;
+	    char buffer[_POSIX_PATH_MAX + SHORT_STRING];
+
+	    folder.ff = &ff;
+	    folder.ff->name = f->name;
+	    folder.ff->st = NULL;
+	    folder.ff->is_new = nntp_data->new;
+	    folder.ff->nntp_data = nntp_data;
+	    FREE (&f->desc);
+	    mutt_FormatString (buffer, sizeof (buffer), 0, NONULL(GroupFormat),
+		  newsgroup_format_str, (unsigned long) &folder,
+		  M_FORMAT_ARROWCURSOR);
+	    f->desc = safe_strdup (buffer); */
+	    nntp_newsrc_update (CurrentNewsSrv);
+	    if (menu->current + 1 < menu->max)
+	      menu->current++;
+	    menu->redraw = REDRAW_MOTION_RESYNCH;
+	  }
+	  if (rc)
+	    menu->redraw = REDRAW_INDEX;
+	  nntp_newsrc_close (CurrentNewsSrv);
+	}
+	break;
+
+      case OP_LOAD_ACTIVE:
+	if (option (OPTNEWS))
+	{
+	  NNTP_SERVER *nserv = CurrentNewsSrv;
+	  unsigned int i;
+
+	  if (nntp_newsrc_parse (nserv) < 0)
+	    break;
+
+	  for (i = 0; i < nserv->groups_num; i++)
+	  {
+	    NNTP_DATA *nntp_data = nserv->groups_list[i];
+	    if (nntp_data)
+	      nntp_data->deleted = 1;
+	  }
+	  nntp_active_fetch (nserv);
+	  nntp_newsrc_update (nserv);
+	  nntp_newsrc_close (nserv);
+
+	  destroy_state (&state);
+	  if (buffy)
+	    examine_mailboxes (menu, &state);
+	  else
+	    examine_directory (menu, &state, NULL, NULL);
+	  init_menu (&state, menu, title, sizeof (title), buffy);
+	}
+	break;
+#endif /* USE_NNTP */
+
+#if defined USE_IMAP || defined USE_NNTP
+      case OP_BROWSER_SUBSCRIBE:
+      case OP_BROWSER_UNSUBSCRIBE:
+#endif
+#ifdef USE_NNTP
+      case OP_SUBSCRIBE_PATTERN:
+      case OP_UNSUBSCRIBE_PATTERN:
+	if (option (OPTNEWS))
+	{
+	  NNTP_SERVER *nserv = CurrentNewsSrv;
+	  NNTP_DATA *nntp_data;
+	  regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
+	  char *s = buf;
+	  int rc, j = menu->current;
+
+	  if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN)
+	  {
+	    char tmp[STRING];
+	    int err;
+
+	    buf[0] = 0;
+	    if (i == OP_SUBSCRIBE_PATTERN)
+	      snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
+	    else
+	      snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
+	    if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0])
+	    {
+	      FREE (&rx);
+	      break;
+	    }
+
+	    err = REGCOMP (rx, s, REG_NOSUB);
+	    if (err)
+	    {
+	      regerror (err, rx, buf, sizeof (buf));
+	      regfree (rx);
+	      FREE (&rx);
+	      mutt_error ("%s", buf);
+	      break;
+	    }
+	    menu->redraw = REDRAW_FULL;
+	    j = 0;
+	  }
+	  else if (!state.entrylen)
+	  {
+	    mutt_error _("No newsgroups match the mask");
+	    break;
+	  }
+
+	  rc = nntp_newsrc_parse (nserv);
+	  if (rc < 0)
+	    break;
+
+	  for ( ; j < state.entrylen; j++)
+	  {
+	    struct folder_file *f = &state.entry[j];
+
+	    if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
+		  regexec (rx, f->name, 0, NULL, 0) == 0)
+	    {
+	      if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
+		nntp_data = mutt_newsgroup_subscribe (nserv, f->name);
+	      else
+		nntp_data = mutt_newsgroup_unsubscribe (nserv, f->name);
+/*	      if (nntp_data)
+	      {
+		FOLDER folder;
+		char buffer[_POSIX_PATH_MAX + SHORT_STRING];
+
+		folder.name = f->name;
+		folder.f = NULL;
+		folder.new = nntp_data->new;
+		folder.nd = nntp_data;
+		FREE (&f->desc);
+		mutt_FormatString (buffer, sizeof (buffer), 0, NONULL(GroupFormat),
+			newsgroup_format_str, (unsigned long) &folder,
+			M_FORMAT_ARROWCURSOR);
+		f->desc = safe_strdup (buffer);
+	      } */
+	    }
+	    if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE)
+	    {
+	      if (menu->current + 1 < menu->max)
+		menu->current++;
+	      menu->redraw = REDRAW_MOTION_RESYNCH;
+	      break;
+	    }
+	  }
+	  if (i == OP_SUBSCRIBE_PATTERN)
+	  {
+	    unsigned int i;
+
+	    for (i = 0; nserv && i < nserv->groups_num; i++)
+	    {
+	      nntp_data = nserv->groups_list[i];
+	      if (nntp_data && nntp_data->group && !nntp_data->subscribed)
+	      {
+		if (regexec (rx, nntp_data->group, 0, NULL, 0) == 0)
+		{
+		  mutt_newsgroup_subscribe (nserv, nntp_data->group);
+		  add_folder (menu, &state, nntp_data->group, NULL,
+			      nntp_data, nntp_data->new);
+		}
+	      }
+	    }
+	    init_menu (&state, menu, title, sizeof (title), buffy);
+	  }
+	  if (rc > 0)
+	    menu->redraw = REDRAW_FULL;
+	  nntp_newsrc_update (nserv);
+	  nntp_clear_cache (nserv);
+	  nntp_newsrc_close (nserv);
+	  if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
+	    regfree (rx);
+	  FREE (&rx);
+	}
+#ifdef USE_IMAP
+	else
+#endif /* USE_IMAP && USE_NNTP */
+#endif /* USE_NNTP */
+#ifdef USE_IMAP
+	{
+	  if (i == OP_BROWSER_SUBSCRIBE)
+	    imap_subscribe (state.entry[menu->current].name, 1);
+	  else
+	    imap_subscribe (state.entry[menu->current].name, 0);
+	}
+#endif /* USE_IMAP */
     }
   }
   
diff --git a/browser.h b/browser.h
index 515d69f..ad89ab2 100644
--- a/browser.h
+++ b/browser.h
@@ -19,6 +19,10 @@
 #ifndef _BROWSER_H
 #define _BROWSER_H 1
 
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
+
 struct folder_file
 {
   mode_t mode;
@@ -37,6 +41,9 @@ struct folder_file
   unsigned selectable : 1;
   unsigned inferiors : 1;
 #endif
+#ifdef USE_NNTP
+  NNTP_DATA *nd;
+#endif
   unsigned tagged : 1;
 };
 
diff --git a/buffy.c b/buffy.c
index 90ca6db..bd026df 100644
--- a/buffy.c
+++ b/buffy.c
@@ -543,6 +543,9 @@ int mutt_buffy_check (int force)
 
   /* check device ID and serial number instead of comparing paths */
   if (!Context || Context->magic == M_IMAP || Context->magic == M_POP
+#ifdef USE_NNTP
+      || Context->magic == M_NNTP
+#endif
       || stat (Context->path, &contex_sb) != 0)
   {
     contex_sb.st_dev=0;
@@ -559,6 +562,11 @@ int mutt_buffy_check (int force)
 	tmp->magic = M_POP;
       else
 #endif
+#ifdef USE_NNTP
+      if ((tmp->magic == M_NNTP) || mx_is_nntp (tmp->path))
+	tmp->magic = M_NNTP;
+      else
+#endif
       if (stat (tmp->path, &sb) != 0 || (S_ISREG(sb.st_mode) && sb.st_size == 0) ||
 	  (!tmp->magic && (tmp->magic = mx_get_magic (tmp->path)) <= 0))
       {
@@ -574,7 +582,11 @@ int mutt_buffy_check (int force)
     /* check to see if the folder is the currently selected folder
      * before polling */
     if (!Context || !Context->path ||
+#ifdef USE_NNTP
+	(( tmp->magic == M_IMAP || tmp->magic == M_POP || tmp->magic == M_NNTP )
+#else
 	(( tmp->magic == M_IMAP || tmp->magic == M_POP )
+#endif
 	    ? mutt_strcmp (tmp->path, Context->path) :
 	      (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)))
     {
diff --git a/complete.c b/complete.c
index d0ee4af..8dc48cd 100644
--- a/complete.c
+++ b/complete.c
@@ -25,6 +25,9 @@
 #include "mailbox.h"
 #include "imap.h"
 #endif
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
 
 #include <dirent.h>
 #include <string.h>
@@ -48,9 +51,70 @@ int mutt_complete (char *s, size_t slen)
   char filepart[_POSIX_PATH_MAX];
 #ifdef USE_IMAP
   char imap_path[LONG_STRING];
+#endif
 
   dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
 
+#ifdef USE_NNTP
+  if (option (OPTNEWS))
+  {
+    NNTP_SERVER *nserv = CurrentNewsSrv;
+    unsigned int n = 0;
+
+    strfcpy (filepart, s, sizeof (filepart));
+
+    /* special case to handle when there is no filepart yet
+     * find the first subscribed newsgroup */
+    len = mutt_strlen (filepart);
+    if (len == 0)
+    {
+      for (; n < nserv->groups_num; n++)
+      {
+	NNTP_DATA *nntp_data = nserv->groups_list[n];
+
+	if (nntp_data && nntp_data->subscribed)
+	{
+	  strfcpy (filepart, nntp_data->group, sizeof (filepart));
+	  init = 1;
+	  n++;
+	  break;
+	}
+      }
+    }
+
+    for (; n < nserv->groups_num; n++)
+    {
+      NNTP_DATA *nntp_data = nserv->groups_list[n];
+
+      if (nntp_data && nntp_data->subscribed &&
+	  mutt_strncmp (nntp_data->group, filepart, len) == 0)
+      {
+	if (init)
+	{
+	  for (i = 0; filepart[i] && nntp_data->group[i]; i++)
+	  {
+	    if (filepart[i] != nntp_data->group[i])
+	    {
+	      filepart[i] = 0;
+	      break;
+	    }
+	  }
+	  filepart[i] = 0;
+	}
+	else
+	{
+	  strfcpy (filepart, nntp_data->group, sizeof (filepart));
+	  init = 1;
+	}
+      }
+    }
+
+    strcpy (s, filepart);
+    return (init ? 0 : -1);
+  }
+#endif
+
+#ifdef USE_IMAP
   /* we can use '/' as a delimiter, imap_complete rewrites it */
   if (*s == '=' || *s == '+' || *s == '!')
   {
diff --git a/compose.c b/compose.c
index 0fa6df2..901ffc0 100644
--- a/compose.c
+++ b/compose.c
@@ -33,11 +33,16 @@
 #include "sort.h"
 #include "charset.h"
 #include "sidebar.h"
+#include "mx.h"
 
 #ifdef MIXMASTER
 #include "remailer.h"
 #endif
 
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
+
 #include <errno.h>
 #include <string.h>
 #include <sys/stat.h>
@@ -68,11 +73,17 @@ enum
   HDR_CRYPT,
   HDR_CRYPTINFO,
 
+#ifdef USE_NNTP
+  HDR_NEWSGROUPS,
+  HDR_FOLLOWUPTO,
+  HDR_XCOMMENTTO,
+#endif
+
   HDR_ATTACH  = (HDR_FCC + 5) /* where to start printing the attachments */
 };
 
-#define HDR_XOFFSET 10
-#define TITLE_FMT "%10s" /* Used for Prompts, which are ASCII */
+#define HDR_XOFFSET 14
+#define TITLE_FMT "%14s" /* Used for Prompts, which are ASCII */
 #define W (COLS - HDR_XOFFSET - SidebarWidth)
 
 static const char * const Prompts[] =
@@ -84,6 +95,16 @@ static const char * const Prompts[] =
   "Subject: ",
   "Reply-To: ",
   "Fcc: "
+#ifdef USE_NNTP
+#ifdef MIXMASTER
+  ,""
+#endif
+  ,""
+  ,""
+  ,"Newsgroups: "
+  ,"Followup-To: "
+  ,"X-Comment-To: "
+#endif
 };
 
 static const struct mapping_t ComposeHelp[] = {
@@ -98,6 +119,19 @@ static const struct mapping_t ComposeHelp[] = {
   { NULL,	0 }
 };
 
+#ifdef USE_NNTP
+static struct mapping_t ComposeNewsHelp[] = {
+  { N_("Send"),		OP_COMPOSE_SEND_MESSAGE },
+  { N_("Abort"),	OP_EXIT },
+  { "Newsgroups",	OP_COMPOSE_EDIT_NEWSGROUPS },
+  { "Subj",		OP_COMPOSE_EDIT_SUBJECT },
+  { N_("Attach file"),	OP_COMPOSE_ATTACH_FILE },
+  { N_("Descrip"),	OP_COMPOSE_EDIT_DESCRIPTION },
+  { N_("Help"),		OP_HELP },
+  { NULL,		0 }
+};
+#endif
+
 static void snd_entry (char *b, size_t blen, MUTTMENU *menu, int num)
 {
     mutt_FormatString (b, blen, 0, NONULL (AttachFormat), mutt_attach_fmt,
@@ -111,7 +145,7 @@ static void snd_entry (char *b, size_t blen, MUTTMENU *menu, int num)
 
 static void redraw_crypt_lines (HEADER *msg)
 {
-  mvaddstr (HDR_CRYPT, SidebarWidth, "Security: ");
+  mvprintw (HDR_CRYPT, SidebarWidth, TITLE_FMT, "Security: ");
 
   if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME)) == 0)
   {
@@ -152,10 +186,11 @@ static void redraw_crypt_lines (HEADER *msg)
   if ((WithCrypto & APPLICATION_PGP)
       && (msg->security & APPLICATION_PGP) && (msg->security & SIGN))
     printw ("%s%s", _(" sign as: "), PgpSignAs ? PgpSignAs : _("<default>"));
+    printw (TITLE_FMT "%s", _(" sign as: "), PgpSignAs ? PgpSignAs : _("<default>"));
 
   if ((WithCrypto & APPLICATION_SMIME)
       && (msg->security & APPLICATION_SMIME) && (msg->security & SIGN)) {
-      printw ("%s%s", _(" sign as: "), SmimeDefaultKey ? SmimeDefaultKey : _("<default>"));
+    printw (TITLE_FMT "%s", _(" sign as: "), SmimeDefaultKey ? SmimeDefaultKey : _("<default>"));
   }
 
   if ((WithCrypto & APPLICATION_SMIME)
@@ -176,7 +211,7 @@ static void redraw_mix_line (LIST *chain)
   int c;
   char *t;
 
-  mvaddstr (HDR_MIX, SidebarWidth,     "     Mix: ");
+  mvprintw (HDR_MIX, SidebarWidth, TITLE_FMT, "Mix: ");
 
   if (!chain)
   {
@@ -251,9 +286,28 @@ static void draw_envelope (HEADER *msg, char *fcc)
 {
   draw_sidebar (MENU_COMPOSE);
   draw_envelope_addr (HDR_FROM, msg->env->from);
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+  {
+#endif
   draw_envelope_addr (HDR_TO, msg->env->to);
   draw_envelope_addr (HDR_CC, msg->env->cc);
   draw_envelope_addr (HDR_BCC, msg->env->bcc);
+#ifdef USE_NNTP
+  }
+  else
+  {
+    mvprintw (HDR_TO, 0, TITLE_FMT , Prompts[HDR_NEWSGROUPS - 1]);
+    mutt_paddstr (W, NONULL (msg->env->newsgroups));
+    mvprintw (HDR_CC, 0, TITLE_FMT , Prompts[HDR_FOLLOWUPTO - 1]);
+    mutt_paddstr (W, NONULL (msg->env->followup_to));
+    if (option (OPTXCOMMENTTO))
+    {
+      mvprintw (HDR_BCC, 0, TITLE_FMT , Prompts[HDR_XCOMMENTTO - 1]);
+      mutt_paddstr (W, NONULL (msg->env->x_comment_to));
+    }
+  }
+#endif
   mvprintw (HDR_SUBJECT, SidebarWidth, TITLE_FMT, Prompts[HDR_SUBJECT - 1]);
   mutt_paddstr (W, NONULL (msg->env->subject));
   draw_envelope_addr (HDR_REPLYTO, msg->env->reply_to);
@@ -504,6 +558,12 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
   /* Sort, SortAux could be changed in mutt_index_menu() */
   int oldSort, oldSortAux;
   struct stat st;
+#ifdef USE_NNTP
+  int news = 0;		/* is it a news article ? */
+
+  if (option (OPTNEWSSEND))
+    news++;
+#endif
 
   mutt_attach_init (msg->content);
   idx = mutt_gen_attach_list (msg->content, -1, idx, &idxlen, &idxmax, 0, 1);
@@ -514,10 +574,18 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
   menu->make_entry = snd_entry;
   menu->tag = mutt_tag_attach;
   menu->data = idx;
+#ifdef USE_NNTP
+  if (news)
+    menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_COMPOSE, ComposeNewsHelp);
+  else
+#endif
   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_COMPOSE, ComposeHelp);
   
   while (loop)
   {
+#ifdef USE_NNTP
+    unset_option (OPTNEWS);	/* for any case */
+#endif
     switch (op = mutt_menuLoop (menu))
     {
       case OP_REDRAW:
@@ -530,6 +598,10 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
         mutt_message_hook (NULL, msg, M_SEND2HOOK);
 	break;
       case OP_COMPOSE_EDIT_TO:
+#ifdef USE_NNTP
+	if (news)
+	  break;
+#endif
 	menu->redraw = edit_address_list (HDR_TO, &msg->env->to);
 	if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
 	{
@@ -539,6 +611,10 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
         mutt_message_hook (NULL, msg, M_SEND2HOOK);
         break;
       case OP_COMPOSE_EDIT_BCC:
+#ifdef USE_NNTP
+	if (news)
+	  break;
+#endif
 	menu->redraw = edit_address_list (HDR_BCC, &msg->env->bcc);
 	if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
 	{
@@ -548,6 +624,10 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
         mutt_message_hook (NULL, msg, M_SEND2HOOK);
 	break;
       case OP_COMPOSE_EDIT_CC:
+#ifdef USE_NNTP
+	if (news)
+	  break;
+#endif
 	menu->redraw = edit_address_list (HDR_CC, &msg->env->cc);
 	if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
 	{
@@ -556,6 +636,67 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
 	}
         mutt_message_hook (NULL, msg, M_SEND2HOOK);	
         break;
+#ifdef USE_NNTP
+      case OP_COMPOSE_EDIT_NEWSGROUPS:
+	if (news)
+	{
+	  if (msg->env->newsgroups)
+	    strfcpy (buf, msg->env->newsgroups, sizeof (buf));
+	  else
+	    buf[0] = 0;
+	  if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) == 0 &&
+	      buf[0])
+	  {
+	    FREE (&msg->env->newsgroups);
+	    mutt_remove_trailing_ws (buf);
+	    msg->env->newsgroups = safe_strdup (mutt_skip_whitespace (buf));
+	    move (HDR_TO, HDR_XOFFSET);
+	    clrtoeol ();
+	    if (msg->env->newsgroups)
+	      printw ("%-*.*s", W, W, msg->env->newsgroups);
+	  }
+	}
+	break;
+
+      case OP_COMPOSE_EDIT_FOLLOWUP_TO:
+	if (news)
+	{
+	  buf[0] = 0;
+	  if (msg->env->followup_to)
+	    strfcpy (buf, msg->env->followup_to, sizeof (buf));
+	  if (mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) == 0 &&
+	      buf[0])
+	  {
+	    FREE (&msg->env->followup_to);
+	    mutt_remove_trailing_ws (buf);
+	    msg->env->followup_to = safe_strdup (mutt_skip_whitespace (buf));
+	    move (HDR_CC, HDR_XOFFSET);
+	    clrtoeol ();
+	    if (msg->env->followup_to)
+	      printw ("%-*.*s", W, W, msg->env->followup_to);
+	  }
+	}
+	break;
+
+      case OP_COMPOSE_EDIT_X_COMMENT_TO:
+	if (news && option (OPTXCOMMENTTO))
+	{
+	  buf[0] = 0;
+	  if (msg->env->x_comment_to)
+	    strfcpy (buf, msg->env->x_comment_to, sizeof (buf));
+	  if (mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) == 0 &&
+	      buf[0])
+	  {
+	    FREE (&msg->env->x_comment_to);
+	    msg->env->x_comment_to = safe_strdup (buf);
+	    move (HDR_BCC, HDR_XOFFSET);
+	    clrtoeol ();
+	    if (msg->env->x_comment_to)
+	      printw ("%-*.*s", W, W, msg->env->x_comment_to);
+	  }
+	}
+	break;
+#endif
       case OP_COMPOSE_EDIT_SUBJECT:
 	if (msg->env->subject)
 	  strfcpy (buf, msg->env->subject, sizeof (buf));
@@ -721,6 +862,9 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
         break;
 
       case OP_COMPOSE_ATTACH_MESSAGE:
+#ifdef USE_NNTP
+      case OP_COMPOSE_ATTACH_NEWS_MESSAGE:
+#endif
 	{
 	  char *prompt;
 	  HEADER *h;
@@ -728,7 +872,22 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
 	  fname[0] = 0;
 	  prompt = _("Open mailbox to attach message from");
 
+#ifdef USE_NNTP
+	  unset_option (OPTNEWS);
+	  if (op == OP_COMPOSE_ATTACH_NEWS_MESSAGE)
+	  {
+	    if (!(CurrentNewsSrv = nntp_select_server (NewsServer, 0)))
+	      break;
+
+	    prompt = _("Open newsgroup to attach message from");
+	    set_option (OPTNEWS);
+	  }
+#endif
+
 	  if (Context)
+#ifdef USE_NNTP
+	  if ((op == OP_COMPOSE_ATTACH_MESSAGE) ^ (Context->magic == M_NNTP))
+#endif
 	  {
 	    strfcpy (fname, NONULL (Context->path), sizeof (fname));
 	    mutt_pretty_mailbox (fname, sizeof (fname));
@@ -737,6 +896,11 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
 	  if (mutt_enter_fname (prompt, fname, sizeof (fname), &menu->redraw, 1) == -1 || !fname[0])
 	    break;
 
+#ifdef USE_NNTP
+	  if (option (OPTNEWS))
+	    nntp_expand_path (fname, sizeof (fname), &CurrentNewsSrv->conn->account);
+	  else
+#endif
 	  mutt_expand_path (fname, sizeof (fname));
 #ifdef USE_IMAP
           if (!mx_is_imap (fname))
@@ -744,6 +908,9 @@ int mutt_compose_menu (HEADER *msg,   /* structure for new message */
 #ifdef USE_POP
           if (!mx_is_pop (fname))
 #endif
+#ifdef USE_NNTP
+	  if (!mx_is_nntp (fname) && !option (OPTNEWS))
+#endif
 	  /* check to make sure the file exists and is readable */
 	  if (access (fname, R_OK) == -1)
 	  {
diff --git a/configure.ac b/configure.ac
index 1d260aa..d17abaa 100644
--- a/configure.ac
+++ b/configure.ac
@@ -600,6 +600,15 @@ AC_ARG_ENABLE(imap, AS_HELP_STRING([--enable-imap],[Enable IMAP support]),
 ])
 AM_CONDITIONAL(BUILD_IMAP, test x$need_imap = xyes)
 
+AC_ARG_ENABLE(nntp, AC_HELP_STRING([--enable-nntp],[Enable NNTP support]),
+[	if test x$enableval = xyes ; then
+		AC_DEFINE(USE_NNTP,1,[ Define if you want support for the NNTP protocol. ])
+		MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS nntp.o newsrc.o"
+		need_nntp="yes"
+		need_socket="yes"
+	fi
+])
+
 AC_ARG_ENABLE(smtp, AS_HELP_STRING([--enable-smtp],[include internal SMTP relay support]),
 	[if test $enableval = yes; then
 		AC_DEFINE(USE_SMTP, 1, [Include internal SMTP relay support])
@@ -607,7 +616,7 @@ AC_ARG_ENABLE(smtp, AS_HELP_STRING([--enable-smtp],[include internal SMTP relay
 		need_socket="yes"
 	fi])
 
-if test x"$need_imap" = xyes -o x"$need_pop" = xyes ; then
+if test x"$need_imap" = xyes -o x"$need_pop" = xyes -o x"$need_nntp" = xyes ; then
   MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS bcache.o"
 fi
 
diff --git a/curs_main.c b/curs_main.c
index 2e35f90..d052c38 100644
--- a/curs_main.c
+++ b/curs_main.c
@@ -22,6 +22,7 @@
 
 #include "mutt.h"
 #include "mutt_curses.h"
+#include "mx.h"
 #include "mutt_menu.h"
 #include "mailbox.h"
 #include "mapping.h"
@@ -40,6 +41,10 @@
 
 #include "mutt_crypt.h"
 
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
+
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -492,12 +497,27 @@ static const struct mapping_t IndexHelp[] = {
   { NULL,	 0 }
 };
 
+#ifdef USE_NNTP
+struct mapping_t IndexNewsHelp[] = {
+  { N_("Quit"),     OP_QUIT },
+  { N_("Del"),      OP_DELETE },
+  { N_("Undel"),    OP_UNDELETE },
+  { N_("Save"),     OP_SAVE },
+  { N_("Post"),     OP_POST },
+  { N_("Followup"), OP_FOLLOWUP },
+  { N_("Catchup"),  OP_CATCHUP },
+  { N_("Help"),     OP_HELP },
+  { NULL,           0 }
+};
+#endif
+
 /* This function handles the message index window as well as commands returned
  * from the pager (MENU_PAGER).
  */
 int mutt_index_menu (void)
 {
   char buf[LONG_STRING], helpstr[LONG_STRING];
+  int flags;
   int op = OP_NULL;
   int done = 0;                /* controls when to exit the "event" loop */
   int i = 0, j;
@@ -518,7 +538,11 @@ int mutt_index_menu (void)
   menu->make_entry = index_make_entry;
   menu->color = index_color;
   menu->current = ci_first_message ();
-  menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_MAIN, IndexHelp);
+  menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_MAIN,
+#ifdef USE_NNTP
+	(Context && (Context->magic == M_NNTP)) ? IndexNewsHelp :
+#endif
+	IndexHelp);
 
   if (!attach_msg)
     mutt_buffy_check(1); /* force the buffy check after we enter the folder */
@@ -774,6 +798,9 @@ int mutt_index_menu (void)
       mutt_curs_set (1);	/* fallback from the pager */
     }
 
+#ifdef USE_NNTP
+    unset_option (OPTNEWS);	/* for any case */
+#endif
     switch (op)
     {
 
@@ -824,6 +851,161 @@ int mutt_index_menu (void)
 	menu_current_bottom (menu);
 	break;
 
+#ifdef USE_NNTP
+      case OP_GET_PARENT:
+	CHECK_MSGCOUNT;
+	CHECK_VISIBLE;
+
+      case OP_GET_MESSAGE:
+	CHECK_IN_MAILBOX;
+	CHECK_READONLY;
+	CHECK_ATTACH;
+	if (Context->magic == M_NNTP)
+	{
+	  HEADER *hdr;
+
+	  if (op == OP_GET_MESSAGE)
+	  {
+	    buf[0] = 0;
+	    if (mutt_get_field (_("Enter Message-Id: "),
+		buf, sizeof (buf), 0) != 0 || !buf[0])
+	      break;
+	  }
+	  else
+	  {
+	    LIST *ref = CURHDR->env->references;
+	    if (!ref)
+	    {
+	      mutt_error _("Article has no parent reference.");
+	      break;
+	    }
+	    strfcpy (buf, ref->data, sizeof (buf));
+	  }
+	  if (!Context->id_hash)
+	    Context->id_hash = mutt_make_id_hash (Context);
+	  hdr = hash_find (Context->id_hash, buf);
+	  if (hdr)
+	  {
+	    if (hdr->virtual != -1)
+	    {
+	      menu->current = hdr->virtual;
+	      menu->redraw = REDRAW_MOTION_RESYNCH;
+	    }
+	    else if (hdr->collapsed)
+	    {
+	      mutt_uncollapse_thread (Context, hdr);
+	      mutt_set_virtual (Context);
+	      menu->current = hdr->virtual;
+	      menu->redraw = REDRAW_MOTION_RESYNCH;
+	    }
+	    else
+	      mutt_error _("Message is not visible in limited view.");
+	  }
+	  else
+	  {
+	    int rc;
+
+	    mutt_message (_("Fetching %s from server..."), buf);
+	    rc = nntp_check_msgid (Context, buf);
+	    if (rc == 0)
+	    {
+	      hdr = Context->hdrs[Context->msgcount - 1];
+	      mutt_sort_headers (Context, 0);
+	      menu->current = hdr->virtual;
+	      menu->redraw = REDRAW_FULL;
+	    }
+	    else if (rc > 0)
+	      mutt_error (_("Article %s not found on the server."), buf);
+	  }
+	}
+	break;
+
+      case OP_GET_CHILDREN:
+      case OP_RECONSTRUCT_THREAD:
+	CHECK_MSGCOUNT;
+	CHECK_VISIBLE;
+	CHECK_READONLY;
+	CHECK_ATTACH;
+	if (Context->magic == M_NNTP)
+	{
+	  int oldmsgcount = Context->msgcount;
+	  int oldindex = CURHDR->index;
+	  int rc = 0;
+
+	  if (!CURHDR->env->message_id)
+	  {
+	    mutt_error _("No Message-Id. Unable to perform operation.");
+	    break;
+	  }
+
+	  mutt_message _("Fetching message headers...");
+	  if (!Context->id_hash)
+	    Context->id_hash = mutt_make_id_hash (Context);
+	  strfcpy (buf, CURHDR->env->message_id, sizeof (buf));
+
+	  /* trying to find msgid of the root message */
+	  if (op == OP_RECONSTRUCT_THREAD)
+	  {
+	    LIST *ref = CURHDR->env->references;
+	    while (ref)
+	    {
+	      if (hash_find (Context->id_hash, ref->data) == NULL)
+	      {
+		rc = nntp_check_msgid (Context, ref->data);
+		if (rc < 0)
+		  break;
+	      }
+
+	      /* the last msgid in References is the root message */
+	      if (!ref->next)
+		strfcpy (buf, ref->data, sizeof (buf));
+	      ref = ref->next;
+	    }
+	  }
+
+	  /* fetching all child messages */
+	  if (rc >= 0)
+	    rc = nntp_check_children (Context, buf);
+
+	  /* at least one message has been loaded */
+	  if (Context->msgcount > oldmsgcount)
+	  {
+	    HEADER *hdr;
+	    int i, quiet = Context->quiet;
+
+	    if (rc < 0)
+	      Context->quiet = 1;
+	    mutt_sort_headers (Context, (op == OP_RECONSTRUCT_THREAD));
+	    Context->quiet = quiet;
+
+	    /* if the root message was retrieved, move to it */
+	    hdr = hash_find (Context->id_hash, buf);
+	    if (hdr)
+	      menu->current = hdr->virtual;
+
+	    /* try to restore old position */
+	    else
+	    {
+	      for (i = 0; i < Context->msgcount; i++)
+	      {
+		if (Context->hdrs[i]->index == oldindex)
+		{
+		  menu->current = Context->hdrs[i]->virtual;
+		  /* as an added courtesy, recenter the menu
+		   * with the current entry at the middle of the screen */
+		  menu_check_recenter (menu);
+		  menu_current_middle (menu);
+		}
+	      }
+	    }
+	    menu->redraw = REDRAW_FULL;
+	  }
+	  else if (rc >= 0)
+	    mutt_error _("No deleted messages found in the thread.");
+	}
+	break;
+#endif
+
       case OP_JUMP:
 
 	CHECK_MSGCOUNT;
@@ -920,11 +1102,33 @@ int mutt_index_menu (void)
         break;
 
       case OP_MAIN_LIMIT:
+      case OP_TOGGLE_READ:
 
 	CHECK_IN_MAILBOX;
 	menu->oldcurrent = (Context->vcount && menu->current >= 0 && menu->current < Context->vcount) ?
 		CURHDR->index : -1;
-	if (mutt_pattern_func (M_LIMIT, _("Limit to messages matching: ")) == 0)
+	if (op == OP_TOGGLE_READ)
+	{
+	  char buf[LONG_STRING];
+
+	  if (!Context->pattern || strncmp (Context->pattern, "!~R!~D~s", 8) != 0)
+	  {
+	    snprintf (buf, sizeof (buf), "!~R!~D~s%s",
+		      Context->pattern ? Context->pattern : ".*");
+	    set_option (OPTHIDEREAD);
+	  }
+	  else
+	  {
+	    strfcpy (buf, Context->pattern + 8, sizeof(buf));
+	    if (!*buf || strncmp (buf, ".*", 2) == 0)
+	      snprintf (buf, sizeof(buf), "~A");
+	    unset_option (OPTHIDEREAD);
+	  }
+	  FREE (&Context->pattern);
+	  Context->pattern = safe_strdup (buf);
+	}
+	if ((op == OP_TOGGLE_READ && mutt_pattern_func (M_LIMIT, NULL) == 0) ||
+	    mutt_pattern_func (M_LIMIT, _("Limit to messages matching: ")) == 0)
 	{
 	  if (menu->oldcurrent >= 0)
 	  {
@@ -1167,15 +1371,22 @@ int mutt_index_menu (void)
       case OP_SIDEBAR_OPEN:
       case OP_MAIN_CHANGE_FOLDER:
       case OP_MAIN_NEXT_UNREAD_MAILBOX:
-
-	if (attach_msg)
-	  op = OP_MAIN_CHANGE_FOLDER_READONLY;
-
-	/* fallback to the readonly case */
-
       case OP_MAIN_CHANGE_FOLDER_READONLY:
+#ifdef USE_NNTP
+      case OP_MAIN_CHANGE_GROUP:
+      case OP_MAIN_CHANGE_GROUP_READONLY:
+	unset_option (OPTNEWS);
+#endif
+	if (attach_msg || option (OPTREADONLY) ||
+#ifdef USE_NNTP
+	    op == OP_MAIN_CHANGE_GROUP_READONLY ||
+#endif
+	    op == OP_MAIN_CHANGE_FOLDER_READONLY)
+	  flags = M_READONLY;
+	else
+	  flags = 0;
 
-        if ((op == OP_MAIN_CHANGE_FOLDER_READONLY) || option (OPTREADONLY))
+	if (flags)
           cp = _("Open mailbox in read-only mode");
         else
           cp = _("Open mailbox");
@@ -1194,6 +1405,22 @@ int mutt_index_menu (void)
 	}
 	else
 	{
+#ifdef USE_NNTP
+	  if (op == OP_MAIN_CHANGE_GROUP ||
+	      op == OP_MAIN_CHANGE_GROUP_READONLY)
+	  {
+	    set_option (OPTNEWS);
+	    CurrentNewsSrv = nntp_select_server (NewsServer, 0);
+	    if (!CurrentNewsSrv)
+	      break;
+	    if (flags)
+	      cp = _("Open newsgroup in read-only mode");
+	    else
+	      cp = _("Open newsgroup");
+	    nntp_buffy (buf, sizeof (buf));
+	  }
+	  else
+#endif
 	  mutt_buffy (buf, sizeof (buf));
 
           if ( op == OP_SIDEBAR_OPEN ) {
@@ -1217,6 +1444,14 @@ int mutt_index_menu (void)
 	  }
 	}
 
+#ifdef USE_NNTP
+	if (option (OPTNEWS))
+	{
+	  unset_option (OPTNEWS);
+	  nntp_expand_path (buf, sizeof (buf), &CurrentNewsSrv->conn->account);
+	}
+	else
+#endif
 	mutt_expand_path (buf, sizeof (buf));
         set_curbuffy(buf);
 	if (mx_get_magic (buf) <= 0)
@@ -1264,15 +1499,18 @@ int mutt_index_menu (void)
 	CurrentMenu = MENU_MAIN;
 	mutt_folder_hook (buf);
 
-	if ((Context = mx_open_mailbox (buf,
-					(option (OPTREADONLY) || op == OP_MAIN_CHANGE_FOLDER_READONLY) ?
-					M_READONLY : 0, NULL)) != NULL)
+	if ((Context = mx_open_mailbox (buf, flags, NULL)) != NULL)
 	{
 	  menu->current = ci_first_message ();
 	}
 	else
 	  menu->current = 0;
 
+#ifdef USE_NNTP
+	/* mutt_buffy_check() must be done with mail-reader mode! */
+	menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_MAIN,
+	  (Context && (Context->magic == M_NNTP)) ? IndexNewsHelp : IndexHelp);
+#endif
 	mutt_clear_error ();
 	mutt_buffy_check(1); /* force the buffy check after we have changed
 			      the folder */
@@ -1341,6 +1579,7 @@ int mutt_index_menu (void)
 	CHECK_MSGCOUNT;
         CHECK_VISIBLE;
 	CHECK_READONLY;
+	CHECK_ACL(M_ACL_WRITE, _("break thread"));
 
         if ((Sort & SORT_MASK) != SORT_THREADS)
 	  mutt_error _("Threading is not enabled.");
@@ -1375,7 +1614,7 @@ int mutt_index_menu (void)
 	CHECK_MSGCOUNT;
         CHECK_VISIBLE;
 	CHECK_READONLY;
-	CHECK_ACL(M_ACL_DELETE, _("link threads"));
+	CHECK_ACL(M_ACL_WRITE, _("link threads"));
 
         if ((Sort & SORT_MASK) != SORT_THREADS)
 	  mutt_error _("Threading is not enabled.");
@@ -1996,6 +2235,20 @@ int mutt_index_menu (void)
 	}
 	break;
 
+#ifdef USE_NNTP
+      case OP_CATCHUP:
+	CHECK_MSGCOUNT;
+	CHECK_READONLY;
+	CHECK_ATTACH
+	if (Context && Context->magic == M_NNTP)
+	{
+	  NNTP_DATA *nntp_data = Context->data;
+	  if (mutt_newsgroup_catchup (nntp_data->nserv, nntp_data->group))
+	    menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
+	}
+	break;
+#endif
+
       case OP_DISPLAY_ADDRESS:
 
 	CHECK_MSGCOUNT;
@@ -2200,6 +2453,39 @@ int mutt_index_menu (void)
         menu->redraw = REDRAW_FULL;
         break;
 
+#ifdef USE_NNTP
+      case OP_FOLLOWUP:
+      case OP_FORWARD_TO_GROUP:
+
+	CHECK_MSGCOUNT;
+	CHECK_VISIBLE;
+
+      case OP_POST:
+
+	CHECK_ATTACH;
+	if (op != OP_FOLLOWUP || !CURHDR->env->followup_to ||
+	    mutt_strcasecmp (CURHDR->env->followup_to, "poster") ||
+	    query_quadoption (OPT_FOLLOWUPTOPOSTER,
+	    _("Reply by mail as poster prefers?")) != M_YES)
+	{
+	  if (Context && Context->magic == M_NNTP &&
+	      !((NNTP_DATA *)Context->data)->allowed &&
+	      query_quadoption (OPT_TOMODERATED,
+	      _("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
+	    break;
+	  if (op == OP_POST)
+	    ci_send_message (SENDNEWS, NULL, NULL, Context, NULL);
+	  else
+	  {
+	    CHECK_MSGCOUNT;
+	    ci_send_message ((op == OP_FOLLOWUP ? SENDREPLY : SENDFORWARD) |
+			SENDNEWS, NULL, NULL, Context, tag ? NULL : CURHDR);
+	  }
+	  menu->redraw = REDRAW_FULL;
+	  break;
+	}
+#endif
+
       case OP_REPLY:
 
 	CHECK_ATTACH;
diff --git a/doc/Muttrc b/doc/Muttrc
index d1a96a2..7fdc12f 100644
--- a/doc/Muttrc
+++ b/doc/Muttrc
@@ -240,6 +240,28 @@ attachments   -I message/external-body
 # editing the body of an outgoing message.
 # 
 # 
+# set ask_follow_up=no
+#
+# Name: ask_follow_up
+# Type: boolean
+# Default: no
+# 
+# 
+# If set, Mutt will prompt you for follow-up groups before editing
+# the body of an outgoing message.
+# 
+# 
+# set ask_x_comment_to=no
+#
+# Name: ask_x_comment_to
+# Type: boolean
+# Default: no
+# 
+# 
+# If set, Mutt will prompt you for x-comment-to field before editing
+# the body of an outgoing message.
+# 
+# 
 # set assumed_charset=""
 #
 # Name: assumed_charset
@@ -445,6 +467,17 @@ attachments   -I message/external-body
 # visual terminals don't permit making the cursor invisible.
 # 
 # 
+# set catchup_newsgroup=ask-yes
+#
+# Name: catchup_newsgroup
+# Type: quadoption
+# Default: ask-yes
+# 
+# 
+# If this variable is set, Mutt will mark all articles in newsgroup
+# as read when you quit the newsgroup (catchup newsgroup).
+# 
+# 
 # set certificate_file="~/.mutt_certificates"
 #
 # Name: certificate_file
@@ -1170,6 +1203,19 @@ attachments   -I message/external-body
 # of the same email for you.
 # 
 # 
+# set followup_to_poster=ask-yes
+#
+# Name: followup_to_poster
+# Type: quadoption
+# Default: ask-yes
+# 
+# 
+# If this variable is set and the keyword "poster" is present in
+# Followup-To header, follow-up to newsgroup function is not
+# permitted.  The message will be mailed to the submitter of the
+# message via mail.
+# 
+# 
 # set force_name=no
 #
 # Name: force_name
@@ -1280,6 +1326,27 @@ attachments   -I message/external-body
 # ``Franklin'' to ``Franklin, Steve''.
 # 
 # 
+# set group_index_format="%4C %M%N %5s  %-45.45f %d"
+#
+# Name: group_index_format
+# Type: string
+# Default: "%4C %M%N %5s  %-45.45f %d"
+# 
+# 
+# This variable allows you to customize the newsgroup browser display to
+# your personal taste.  This string is similar to ``index_format'', but
+# has its own set of printf()-like sequences:
+# %C      current newsgroup number
+# %d      description of newsgroup (becomes from server)
+# %f      newsgroup name
+# %M      - if newsgroup not allowed for direct post (moderated for example)
+# %N      N if newsgroup is new, u if unsubscribed, blank otherwise
+# %n      number of new articles in newsgroup
+# %s      number of unread articles in newsgroup
+# %>X     right justify the rest of the string and pad with character "X"
+# %|X     pad to the end of the line with character "X"
+# 
+# 
 # set hdrs=yes
 #
 # Name: hdrs
@@ -1828,6 +1895,7 @@ attachments   -I message/external-body
 # %E      number of messages in current thread
 # %f      sender (address + real name), either From: or Return-Path:
 # %F      author name, or recipient name if the message is from you
+# %g      newsgroup name (if compiled with NNTP support)
 # %H      spam attribute(s) of this message
 # %i      message-id of the current message
 # %l      number of lines in the message (does not work with maildir,
@@ -1843,12 +1911,14 @@ attachments   -I message/external-body
 #         stashed the message: list name or recipient name
 #         if not sent to a list
 # %P      progress indicator for the built-in pager (how much of the file has been displayed)
+# %R      ``X-Comment-To:'' field (if present and compiled with NNTP support)
 # %s      subject of the message
 # %S      status of the message (``N''/``D''/``d''/``!''/``r''/*)
 # %t      ``To:'' field (recipients)
 # %T      the appropriate character from the $to_chars string
 # %u      user (login) name of the author
 # %v      first name of the author, or the recipient if the message is from you
+# %W      name of organization of author (``Organization:'' field)
 # %X      number of attachments
 #         (please see the ``attachments'' section for possible speed effects)
 # %y      ``X-Label:'' field, if present
@@ -1884,6 +1954,27 @@ attachments   -I message/external-body
 # ``save-hook'', ``fcc-hook'' and ``fcc-save-hook'', too.
 # 
 # 
+# set inews=""
+#
+# Name: inews
+# Type: path
+# Default: ""
+# 
+# 
+# If set, specifies the program and arguments used to deliver news posted
+# by Mutt.  Otherwise, mutt posts article using current connection to
+# news server.  The following printf-style sequence is understood:
+# %a      account url
+# %p      port
+# %P      port if specified
+# %s      news server name
+# %S      url schema
+# %u      username
+# 
+# 
+# Example: set inews="/usr/local/bin/inews -hS"
+# 
+# 
 # set ispell="ispell"
 #
 # Name: ispell
@@ -2268,6 +2359,18 @@ attachments   -I message/external-body
 # be attached to the newly composed message if this option is set.
 # 
 # 
+# set mime_subject=yes
+#
+# Name: mime_subject
+# Type: boolean
+# Default: yes
+# 
+# 
+# If unset, 8-bit ``subject:'' line in article header will not be
+# encoded according to RFC2047 to base64.  This is useful when message
+# is Usenet article, because MIME for news is nonstandard feature.
+# 
+# 
 # set mix_entry_format="%4n %c %-16s %a"
 #
 # Name: mix_entry_format
@@ -2334,6 +2437,144 @@ attachments   -I message/external-body
 # See also $read_inc, $write_inc and $net_inc.
 # 
 # 
+# set news_cache_dir="~/.mutt"
+#
+# Name: news_cache_dir
+# Type: path
+# Default: "~/.mutt"
+# 
+# 
+# This variable pointing to directory where Mutt will save cached news
+# articles and headers in. If unset, articles and headers will not be
+# saved at all and will be reloaded from the server each time.
+# 
+# 
+# set news_server=""
+#
+# Name: news_server
+# Type: string
+# Default: ""
+# 
+# 
+# This variable specifies domain name or address of NNTP server. It
+# defaults to the news server specified in the environment variable
+# $NNTPSERVER or contained in the file /etc/nntpserver.  You can also
+# specify username and an alternative port for each news server, ie:
+# 
+# [[s]news://][username[:password]@]server[:port]
+# 
+# 
+# set newsgroups_charset="utf-8"
+#
+# Name: newsgroups_charset
+# Type: string
+# Default: "utf-8"
+# 
+# 
+# Character set of newsgroups descriptions.
+# 
+# 
+# set newsrc="~/.newsrc"
+#
+# Name: newsrc
+# Type: path
+# Default: "~/.newsrc"
+# 
+# 
+# The file, containing info about subscribed newsgroups - names and
+# indexes of read articles.  The following printf-style sequence
+# is understood:
+# %a      account url
+# %p      port
+# %P      port if specified
+# %s      news server name
+# %S      url schema
+# %u      username
+# 
+# 
+# set nntp_authenticators=""
+#
+# Name: nntp_authenticators
+# Type: string
+# Default: ""
+# 
+# 
+# This is a colon-delimited list of authentication methods mutt may
+# attempt to use to log in to a news server, in the order mutt should
+# try them.  Authentication methods are either ``user'' or any
+# SASL mechanism, e.g. ``digest-md5'', ``gssapi'' or ``cram-md5''.
+# This option is case-insensitive.  If it's unset (the default)
+# mutt will try all available methods, in order from most-secure to
+# least-secure.
+# 
+# Example:
+# set nntp_authenticators="digest-md5:user"
+# 
+# Note: Mutt will only fall back to other authentication methods if
+# the previous methods are unavailable. If a method is available but
+# authentication fails, mutt will not connect to the IMAP server.
+# 
+# 
+# set nntp_context=1000
+#
+# Name: nntp_context
+# Type: number
+# Default: 1000
+# 
+# 
+# This variable defines number of articles which will be in index when
+# newsgroup entered.  If active newsgroup have more articles than this
+# number, oldest articles will be ignored.  Also controls how many
+# articles headers will be saved in cache when you quit newsgroup.
+# 
+# 
+# set nntp_load_description=yes
+#
+# Name: nntp_load_description
+# Type: boolean
+# Default: yes
+# 
+# 
+# This variable controls whether or not descriptions for each newsgroup
+# must be loaded when newsgroup is added to list (first time list
+# loading or new newsgroup adding).
+# 
+# 
+# set nntp_user=""
+#
+# Name: nntp_user
+# Type: string
+# Default: ""
+# 
+# 
+# Your login name on the NNTP server.  If unset and NNTP server requires
+# authentification, Mutt will prompt you for your account name when you
+# connect to news server.
+# 
+# 
+# set nntp_pass=""
+#
+# Name: nntp_pass
+# Type: string
+# Default: ""
+# 
+# 
+# Your password for NNTP account.
+# 
+# 
+# set nntp_poll=60
+#
+# Name: nntp_poll
+# Type: number
+# Default: 60
+# 
+# 
+# The time in seconds until any operations on newsgroup except post new
+# article will cause recheck for new news.  If set to 0, Mutt will
+# recheck newsgroup on each operation in index (stepping, read article,
+# etc.).
+# 
+# 
 # set pager="builtin"
 #
 # Name: pager
@@ -3064,6 +3305,19 @@ attachments   -I message/external-body
 # string after the inclusion of a message which is being replied to.
 # 
 # 
+# set post_moderated=ask-yes
+#
+# Name: post_moderated
+# Type: quadoption
+# Default: ask-yes
+# 
+# 
+# If set to yes, Mutt will post article to newsgroup that have
+# not permissions to posting (e.g. moderated).  Note: if news server
+# does not support posting to that newsgroup or totally read-only, that
+# posting will not have an effect.
+# 
+# 
 # set postpone=ask-yes
 #
 # Name: postpone
@@ -3697,6 +3951,41 @@ attachments   -I message/external-body
 # shell from /etc/passwd is used.
 # 
 # 
+# set save_unsubscribed=no
+#
+# Name: save_unsubscribed
+# Type: boolean
+# Default: no
+# 
+# 
+# When set, info about unsubscribed newsgroups will be saved into
+# ``newsrc'' file and into cache.
+# 
+# 
+# set show_new_news=yes
+#
+# Name: show_new_news
+# Type: boolean
+# Default: yes
+# 
+# 
+# If set, news server will be asked for new newsgroups on entering
+# the browser.  Otherwise, it will be done only once for a news server.
+# Also controls whether or not number of new articles of subscribed
+# newsgroups will be then checked.
+# 
+# 
+# set show_only_unread=no
+#
+# Name: show_only_unread
+# Type: boolean
+# Default: no
+# 
+# 
+# If set, only subscribed newsgroups that contain unread articles
+# will be displayed in browser.
+# 
+# 
 # set sig_dashes=yes
 #
 # Name: sig_dashes
@@ -4995,3 +5284,14 @@ attachments   -I message/external-body
 # ``tuning'' section of the manual for performance considerations.
 # 
 # 
+# set x_comment_to=no
+#
+# Name: x_comment_to
+# Type: boolean
+# Default: no
+# 
+# 
+# If set, Mutt will add ``X-Comment-To:'' field (that contains full
+# name of original article author) to article that followuped to newsgroup.
+# 
+# 
diff --git a/doc/manual.xml.head b/doc/manual.xml.head
index f7a9387..aeefa5c 100644
--- a/doc/manual.xml.head
+++ b/doc/manual.xml.head
@@ -1696,6 +1696,26 @@ See also the <link linkend="postpone">$postpone</link> quad-option.
 
 </sect1>
 
+<sect1 id="nntp">
+<title>Reading news via NNTP</title>
+
+<para>
+If compiled with <emphasis>--enable-nntp</emphasis> option, Mutt can
+read news from news server via NNTP.  You can open a newsgroup with
+function ``change-newsgroup'' (default: ``i'').  Default news server
+can be obtained from <literal>$NNTPSERVER</literal> environment
+variable or from <literal>/etc/nntpserver</literal> file.  Like other
+news readers, info about subscribed newsgroups is saved in file by
+<link linkend="newsrc">$newsrc</link> variable.  The variable <link
+linkend="news-cache-dir">$news_cache_dir</link> can be used to point
+to a directory.  Mutt will create a hierarchy of subdirectories named
+like the account and newsgroup the cache is for.  Also the hierarchy
+is used to store header cache if Mutt was compiled with <link
+linkend="header-caching">header cache</link> support.
+</para>
+
+</sect1>
+
 </chapter>
 
 <chapter id="configuration">
diff --git a/doc/mutt.man b/doc/mutt.man
index 753953e..0e61ae2 100644
--- a/doc/mutt.man
+++ b/doc/mutt.man
@@ -23,8 +23,8 @@ mutt \- The Mutt Mail User Agent
 .SH SYNOPSIS
 .PP
 .B mutt
-[\-nRyzZ]
-[\-e \fIcmd\fP] [\-F \fIfile\fP] [\-m \fItype\fP] [\-f \fIfile\fP]
+[\-GnRyzZ]
+[\-e \fIcmd\fP] [\-F \fIfile\fP] [\-g \fIserver\fP] [\-m \fItype\fP] [\-f \fIfile\fP]
 .PP
 .B mutt 
 [\-nx] 
@@ -101,6 +101,10 @@ files.
 Specify which mailbox to load.
 .IP "-F \fImuttrc\fP"
 Specify an initialization file to read instead of ~/.muttrc
+.IP "-g \fIserver\fP"
+Start Mutt with a listing of subscribed newsgroups at specified news server.
+.IP "-G"
+Start Mutt with a listing of subscribed newsgroups.
 .IP "-h"
 Display help.
 .IP "-H \fIdraft\fP"
diff --git a/functions.h b/functions.h
index 1485080..8ca5411 100644
--- a/functions.h
+++ b/functions.h
@@ -88,6 +88,10 @@ const struct binding_t OpMain[] = { /* map: index */
   { "break-thread",		OP_MAIN_BREAK_THREAD,		"#" },
   { "change-folder",		OP_MAIN_CHANGE_FOLDER,		"c" },
   { "change-folder-readonly",	OP_MAIN_CHANGE_FOLDER_READONLY,	"\033c" },
+#ifdef USE_NNTP
+  { "change-newsgroup",		OP_MAIN_CHANGE_GROUP,		"i" },
+  { "change-newsgroup-readonly",OP_MAIN_CHANGE_GROUP_READONLY,	"\033i" },
+#endif
   { "next-unread-mailbox",	OP_MAIN_NEXT_UNREAD_MAILBOX,    NULL },
   { "collapse-thread",		OP_MAIN_COLLAPSE_THREAD,	"\033v" },
   { "collapse-all",		OP_MAIN_COLLAPSE_ALL,		"\033V" },
@@ -101,7 +105,15 @@ const struct binding_t OpMain[] = { /* map: index */
   { "edit",			OP_EDIT_MESSAGE,		"e" },
   { "edit-type",		OP_EDIT_TYPE,			"\005" },
   { "forward-message",		OP_FORWARD_MESSAGE,		"f" },
-  { "flag-message",		OP_FLAG_MESSAGE,		"F" },
+#ifdef USE_NNTP
+  { "forward-to-group",		OP_FORWARD_TO_GROUP,		"\033F" },
+  { "followup-message",		OP_FOLLOWUP,			"F" },
+  { "get-children",		OP_GET_CHILDREN,		NULL },
+  { "get-message",		OP_GET_MESSAGE,			"\007" },
+  { "get-parent",		OP_GET_PARENT,			"\033G" },
+  { "reconstruct-thread",	OP_RECONSTRUCT_THREAD,		NULL },
+#endif
+  { "flag-message",		OP_FLAG_MESSAGE,		"\033f" },
   { "group-reply",		OP_GROUP_REPLY,			"g" },
 #ifdef USE_POP
   { "fetch-mail",		OP_MAIN_FETCH_MAIL,		"G" },
@@ -129,6 +141,9 @@ const struct binding_t OpMain[] = { /* map: index */
   { "sort-mailbox",		OP_SORT,			"o" },
   { "sort-reverse",		OP_SORT_REVERSE,		"O" },
   { "print-message",		OP_PRINT,			"p" },
+#ifdef USE_NNTP
+  { "post-message",		OP_POST,			"P" },
+#endif
   { "previous-thread",		OP_MAIN_PREV_THREAD,		"\020" },
   { "previous-subthread",	OP_MAIN_PREV_SUBTHREAD,		"\033p" },
   { "recall-message",		OP_RECALL_MESSAGE,		"R" },
@@ -148,6 +163,10 @@ const struct binding_t OpMain[] = { /* map: index */
   { "show-version",		OP_VERSION,			"V" },
   { "set-flag",			OP_MAIN_SET_FLAG,		"w" },
   { "clear-flag",		OP_MAIN_CLEAR_FLAG,		"W" },
+  { "toggle-read",		OP_TOGGLE_READ,			"X" },
+#ifdef USE_NNTP
+  { "catchup",			OP_CATCHUP,			"y" },
+#endif
   { "display-message",		OP_DISPLAY_MESSAGE,		M_ENTER_S },
   { "buffy-list",		OP_BUFFY_LIST,			"." },
   { "sync-mailbox",		OP_MAIN_SYNC_FOLDER,		"$" },
@@ -159,7 +178,7 @@ const struct binding_t OpMain[] = { /* map: index */
   { "previous-new-then-unread",	OP_MAIN_PREV_NEW_THEN_UNREAD,	"\033\t" },
   { "next-unread",		OP_MAIN_NEXT_UNREAD,		NULL },
   { "previous-unread",		OP_MAIN_PREV_UNREAD,		NULL },
-  { "parent-message",		OP_MAIN_PARENT_MESSAGE,		"P" },
+  { "parent-message",		OP_MAIN_PARENT_MESSAGE,		NULL },
 
 
   { "extract-keys",		OP_EXTRACT_KEYS,		"\013" },
@@ -187,6 +206,10 @@ const struct binding_t OpPager[] = { /* map: pager */
   { "bounce-message",	OP_BOUNCE_MESSAGE,		"b" },
   { "change-folder",	OP_MAIN_CHANGE_FOLDER,		"c" },
   { "change-folder-readonly",	OP_MAIN_CHANGE_FOLDER_READONLY,	"\033c" },
+#ifdef USE_NNTP
+  { "change-newsgroup",		OP_MAIN_CHANGE_GROUP,		"i" },
+  { "change-newsgroup-readonly",OP_MAIN_CHANGE_GROUP_READONLY,	"\033i" },
+#endif
   { "next-unread-mailbox",	OP_MAIN_NEXT_UNREAD_MAILBOX, NULL },
   { "copy-message",	OP_COPY_MESSAGE,		"C" },
   { "decode-copy",	OP_DECODE_COPY,			"\033C" },
@@ -197,8 +220,12 @@ const struct binding_t OpPager[] = { /* map: pager */
   { "clear-flag",       OP_MAIN_CLEAR_FLAG,		"W" },
   { "edit",		OP_EDIT_MESSAGE,		"e" },
   { "edit-type",	OP_EDIT_TYPE,			"\005" },
+#ifdef USE_NNTP
+  { "followup-message",	OP_FOLLOWUP,			"F" },
+  { "forward-to-group",	OP_FORWARD_TO_GROUP,		"\033F" },
+#endif
   { "forward-message",	OP_FORWARD_MESSAGE,		"f" },
-  { "flag-message",	OP_FLAG_MESSAGE,		"F" },
+  { "flag-message",	OP_FLAG_MESSAGE,		"\033f" },
   { "group-reply",	OP_GROUP_REPLY,			"g" },
 #ifdef USE_IMAP
   { "imap-fetch-mail",  OP_MAIN_IMAP_FETCH,		NULL },
@@ -220,6 +247,9 @@ const struct binding_t OpPager[] = { /* map: pager */
   { "sort-mailbox",	OP_SORT,			"o" },
   { "sort-reverse",	OP_SORT_REVERSE,		"O" },
   { "print-message",	OP_PRINT,			"p" },
+#ifdef USE_NNTP
+  { "post-message",	OP_POST,			"P" },
+#endif
   { "previous-thread",	OP_MAIN_PREV_THREAD,		"\020" },
   { "previous-subthread",OP_MAIN_PREV_SUBTHREAD,	"\033p" },
   { "purge-message",	OP_PURGE_MESSAGE,		NULL },
@@ -268,7 +298,7 @@ const struct binding_t OpPager[] = { /* map: pager */
   { "half-down",	OP_HALF_DOWN,			NULL },
   { "previous-line",	OP_PREV_LINE,			NULL },
   { "bottom",		OP_PAGER_BOTTOM,		NULL },
-  { "parent-message",	OP_MAIN_PARENT_MESSAGE,		"P" },
+  { "parent-message",	OP_MAIN_PARENT_MESSAGE,		NULL },
 
 
 
@@ -297,6 +327,10 @@ const struct binding_t OpAttach[] = { /* map: attachment */
   { "bounce-message",	OP_BOUNCE_MESSAGE,		"b" },
   { "display-toggle-weed",	OP_DISPLAY_HEADERS,	"h" },
   { "edit-type",	OP_EDIT_TYPE,			"\005" },
+#ifdef USE_NNTP
+  { "followup-message",	OP_FOLLOWUP,			"F" },
+  { "forward-to-group",	OP_FORWARD_TO_GROUP,		"\033F" },
+#endif
   { "print-entry",	OP_PRINT,			"p" },
   { "save-entry",	OP_SAVE,			"s" },
   { "pipe-entry",	OP_PIPE,			"|" },
@@ -322,6 +356,7 @@ const struct binding_t OpAttach[] = { /* map: attachment */
 const struct binding_t OpCompose[] = { /* map: compose */
   { "attach-file",	OP_COMPOSE_ATTACH_FILE,		"a" },
   { "attach-message",	OP_COMPOSE_ATTACH_MESSAGE,	"A" },
+  { "attach-news-message",OP_COMPOSE_ATTACH_NEWS_MESSAGE,"\033a" },
   { "edit-bcc",		OP_COMPOSE_EDIT_BCC,		"b" },
   { "edit-cc",		OP_COMPOSE_EDIT_CC,		"c" },
   { "copy-file",	OP_SAVE,			"C" },
@@ -341,6 +376,11 @@ const struct binding_t OpCompose[] = { /* map: compose */
   { "print-entry",	OP_PRINT,			"l" },
   { "edit-mime",	OP_COMPOSE_EDIT_MIME,		"m" },
   { "new-mime",		OP_COMPOSE_NEW_MIME,		"n" },
+#ifdef USE_NNTP
+  { "edit-newsgroups",	OP_COMPOSE_EDIT_NEWSGROUPS,	"N" },
+  { "edit-followup-to",	OP_COMPOSE_EDIT_FOLLOWUP_TO,	"o" },
+  { "edit-x-comment-to",OP_COMPOSE_EDIT_X_COMMENT_TO,	"x" },
+#endif
   { "postpone-message",	OP_COMPOSE_POSTPONE_MESSAGE,	"P" },
   { "edit-reply-to",	OP_COMPOSE_EDIT_REPLY_TO,	"r" },
   { "rename-file",	OP_COMPOSE_RENAME_FILE,		"R" },
@@ -392,14 +432,25 @@ const struct binding_t OpBrowser[] = { /* map: browser */
   { "select-new",	OP_BROWSER_NEW_FILE,	"N" },
   { "check-new",	OP_CHECK_NEW,		NULL },
   { "toggle-mailboxes", OP_TOGGLE_MAILBOXES, 	"\t" },
+#ifdef USE_NNTP
+  { "reload-active",	OP_LOAD_ACTIVE,		"g" },
+  { "subscribe-pattern", OP_SUBSCRIBE_PATTERN,	"S" },
+  { "unsubscribe-pattern", OP_UNSUBSCRIBE_PATTERN, "U" },
+  { "catchup",		OP_CATCHUP,		"y" },
+  { "uncatchup",	OP_UNCATCHUP,		"Y" },
+#endif
   { "view-file",	OP_BROWSER_VIEW_FILE,	" " },
   { "buffy-list",	OP_BUFFY_LIST,		"." },
 #ifdef USE_IMAP
   { "create-mailbox",   OP_CREATE_MAILBOX,      "C" },
   { "delete-mailbox",   OP_DELETE_MAILBOX,      "d" },
   { "rename-mailbox",   OP_RENAME_MAILBOX,      "r" },
+#endif
+#if defined USE_IMAP || defined USE_NNTP
   { "subscribe",	OP_BROWSER_SUBSCRIBE,	"s" },
   { "unsubscribe",	OP_BROWSER_UNSUBSCRIBE,	"u" },
+#endif
+#ifdef USE_IMAP
   { "toggle-subscribed", OP_BROWSER_TOGGLE_LSUB, "T" },
 #endif
   { NULL,		0,			NULL }
diff --git a/globals.h b/globals.h
index 602f932..814eb05 100644
--- a/globals.h
+++ b/globals.h
@@ -95,6 +95,17 @@ WHERE char *MixEntryFormat;
 #endif
 
 WHERE char *Muttrc INITVAL (NULL);
+#ifdef USE_NNTP
+WHERE char *GroupFormat;
+WHERE char *Inews;
+WHERE char *NewsCacheDir;
+WHERE char *NewsServer;
+WHERE char *NewsgroupsCharset;
+WHERE char *NewsRc;
+WHERE char *NntpAuthenticators;
+WHERE char *NntpUser;
+WHERE char *NntpPass;
+#endif
 WHERE char *Outbox;
 WHERE char *Pager;
 WHERE char *PagerFmt;
@@ -196,6 +207,11 @@ extern unsigned char QuadOptions[];
 
 WHERE unsigned short Counter INITVAL (0);
 
+#ifdef USE_NNTP
+WHERE short NewsPollTimeout;
+WHERE short NntpContext;
+#endif
+
 WHERE short ConnectTimeout;
 WHERE short HistSize;
 WHERE short MenuContext;
diff --git a/hash.c b/hash.c
index 08f7171..983a7fd 100644
--- a/hash.c
+++ b/hash.c
@@ -57,6 +57,7 @@ HASH *hash_create (int nelem, int lower)
   if (nelem == 0)
     nelem = 2;
   table->nelem = nelem;
+  table->curnelem = 0;
   table->table = safe_calloc (nelem, sizeof (struct hash_elem *));
   if (lower)
   {
@@ -71,6 +72,29 @@ HASH *hash_create (int nelem, int lower)
   return table;
 }
 
+HASH *hash_resize (HASH *ptr, int nelem, int lower)
+{
+  HASH *table;
+  struct hash_elem *elem, *tmp;
+  int i;
+
+  table = hash_create (nelem, lower);
+
+  for (i = 0; i < ptr->nelem; i++)
+  {
+    for (elem = ptr->table[i]; elem; )
+    {
+      tmp = elem;
+      elem = elem->next;
+      hash_insert (table, tmp->key, tmp->data, 1);
+      FREE (&tmp);
+    }
+  }
+  FREE (&ptr->table);
+  FREE (&ptr);
+  return table;
+}
+
 /* table        hash table to update
  * key          key to hash on
  * data         data to associate with `key'
@@ -90,6 +114,7 @@ int hash_insert (HASH * table, const char *key, void *data, int allow_dup)
   {
     ptr->next = table->table[h];
     table->table[h] = ptr;
+    table->curnelem++;
   }
   else
   {
@@ -112,6 +137,7 @@ int hash_insert (HASH * table, const char *key, void *data, int allow_dup)
     else
       table->table[h] = ptr;
     ptr->next = tmp;
+    table->curnelem++;
   }
   return h;
 }
@@ -142,6 +168,7 @@ void hash_delete_hash (HASH * table, int hash, const char *key, const void *data
       if (destroy)
 	destroy (ptr->data);
       FREE (&ptr);
+      table->curnelem--;
       
       ptr = *last;
     }
diff --git a/hash.h b/hash.h
index fb77d0c..9e7df82 100644
--- a/hash.h
+++ b/hash.h
@@ -28,7 +28,7 @@ struct hash_elem
 
 typedef struct
 {
-  int nelem;
+  int nelem, curnelem;
   struct hash_elem **table;
   unsigned int (*hash_string)(const unsigned char *, unsigned int);
   int (*cmp_string)(const char *, const char *);
@@ -41,6 +41,7 @@ HASH;
 
 HASH *hash_create (int nelem, int lower);
 int hash_insert (HASH * table, const char *key, void *data, int allow_dup);
+HASH *hash_resize (HASH * table, int nelem, int lower);
 void *hash_find_hash (const HASH * table, int hash, const char *key);
 void hash_delete_hash (HASH * table, int hash, const char *key, const void *data,
 		       void (*destroy) (void *));
diff --git a/hcache.c b/hcache.c
index 561dce3..d4f33b5 100644
--- a/hcache.c
+++ b/hcache.c
@@ -447,6 +447,12 @@ dump_envelope(ENVELOPE * e, unsigned char *d, int *off, int convert)
   d = dump_list(e->in_reply_to, d, off, 0);
   d = dump_list(e->userhdrs, d, off, convert);
 
+#ifdef USE_NNTP
+  d = dump_char(e->xref, d, off, 0);
+  d = dump_char(e->followup_to, d, off, 0);
+  d = dump_char(e->x_comment_to, d, off, convert);
+#endif
+
   return d;
 }
 
@@ -483,6 +489,12 @@ restore_envelope(ENVELOPE * e, const unsigned char *d, int *off, int convert)
   restore_list(&e->references, d, off, 0);
   restore_list(&e->in_reply_to, d, off, 0);
   restore_list(&e->userhdrs, d, off, convert);
+
+#ifdef USE_NNTP
+  restore_char(&e->xref, d, off, 0);
+  restore_char(&e->followup_to, d, off, 0);
+  restore_char(&e->x_comment_to, d, off, convert);
+#endif
 }
 
 static int
diff --git a/hdrline.c b/hdrline.c
index 21adc28..c05d28a 100644
--- a/hdrline.c
+++ b/hdrline.c
@@ -211,6 +211,7 @@ int mutt_user_is_recipient (HEADER *h)
  * %E = number of messages in current thread
  * %f = entire from line
  * %F = like %n, unless from self
+ * %g = newsgroup name (if compiled with NNTP support)
  * %i = message-id
  * %l = number of lines in the message
  * %L = like %F, except `lists' are displayed first
@@ -219,12 +220,14 @@ int mutt_user_is_recipient (HEADER *h)
  * %N = score
  * %O = like %L, except using address instead of name
  * %P = progress indicator for builtin pager
+ * %R = `x-comment-to:' field (if present and compiled with NNTP support)
  * %s = subject
  * %S = short message status (e.g., N/O/D/!/r/-)
  * %t = `to:' field (recipients)
  * %T = $to_chars
  * %u = user (login) name of author
  * %v = first name of author, unless from self
+ * %W = where user is (organization)
  * %X = number of MIME attachments
  * %y = `x-label:' field (if present)
  * %Y = `x-label:' field (if present, tree unfolded, and != parent's x-label)
@@ -457,6 +460,12 @@ hdr_format_str (char *dest,
 
       break;
 
+#ifdef USE_NNTP
+    case 'g':
+      mutt_format_s (dest, destlen, prefix, hdr->env->newsgroups ? hdr->env->newsgroups : "");
+      break;
+#endif
+
     case 'i':
       mutt_format_s (dest, destlen, prefix, hdr->env->message_id ? hdr->env->message_id : "<no.id>");
       break;
@@ -548,6 +557,15 @@ hdr_format_str (char *dest,
       strfcpy(dest, NONULL(hfi->pager_progress), destlen);
       break;
 
+#ifdef USE_NNTP
+    case 'R':
+      if (!optional)
+	mutt_format_s (dest, destlen, prefix, hdr->env->x_comment_to ? hdr->env->x_comment_to : "");
+      else if (!hdr->env->x_comment_to)
+	optional = 0;
+      break;
+#endif
+
     case 's':
       
       if (flags & M_FORMAT_TREE && !hdr->collapsed)
@@ -637,6 +655,13 @@ hdr_format_str (char *dest,
       mutt_format_s (dest, destlen, prefix, buf2);
       break;
 
+    case 'W':
+      if (!optional)
+	mutt_format_s (dest, destlen, prefix, hdr->env->organization ? hdr->env->organization : "");
+      else if (!hdr->env->organization)
+	optional = 0;
+      break;
+
     case 'Z':
     
       ch = ' ';
diff --git a/headers.c b/headers.c
index f701c8e..e817dad 100644
--- a/headers.c
+++ b/headers.c
@@ -114,6 +114,9 @@ void mutt_edit_headers (const char *editor,
      $edit_headers set, we remove References: as they're likely invalid;
      we can simply compare strings as we don't generate References for
      multiple Message-Ids in IRT anyways */
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+#endif
   if (msg->env->in_reply_to &&
       (!n->in_reply_to || mutt_strcmp (n->in_reply_to->data,
 				       msg->env->in_reply_to->data) != 0))
diff --git a/init.c b/init.c
index d82772c..3de1f1f 100644
--- a/init.c
+++ b/init.c
@@ -3082,6 +3082,28 @@ void mutt_init (int skip_sys_rc, LIST *commands)
   else
     Fqdn = safe_strdup(NONULL(Hostname));
 
+#ifdef USE_NNTP
+  {
+    FILE *f;
+    char *i;
+
+    if ((f = safe_fopen (SYSCONFDIR "/nntpserver", "r")))
+    {
+      buffer[0] = '\0';
+      fgets (buffer, sizeof (buffer), f);
+      p = &buffer;
+      SKIPWS (p);
+      i = p;
+      while (*i && (*i != ' ') && (*i != '\t') && (*i != '\r') && (*i != '\n')) i++;
+      *i = '\0';
+      NewsServer = safe_strdup (p);
+      fclose (f);
+    }
+  }
+  if ((p = getenv ("NNTPSERVER")))
+    NewsServer = safe_strdup (p);
+#endif
+
   if ((p = getenv ("MAIL")))
     Spoolfile = safe_strdup (p);
   else if ((p = getenv ("MAILDIR")))
diff --git a/init.h b/init.h
index a5d4238..b9cd406 100644
--- a/init.h
+++ b/init.h
@@ -176,6 +176,20 @@ struct option_t MuttVars[] = {
   ** If \fIset\fP, Mutt will prompt you for carbon-copy (Cc) recipients before
   ** editing the body of an outgoing message.
   */
+#ifdef USE_NNTP
+  { "ask_follow_up",	DT_BOOL, R_NONE, OPTASKFOLLOWUP, 0 },
+  /*
+  ** .pp
+  ** If set, Mutt will prompt you for follow-up groups before editing
+  ** the body of an outgoing message.
+  */
+  { "ask_x_comment_to",	DT_BOOL, R_NONE, OPTASKXCOMMENTTO, 0 },
+  /*
+  ** .pp
+  ** If set, Mutt will prompt you for x-comment-to field before editing
+  ** the body of an outgoing message.
+  */
+#endif
   { "assumed_charset", DT_STR, R_NONE, UL &AssumedCharset, UL 0},
   /*
   ** .pp
@@ -328,6 +342,14 @@ struct option_t MuttVars[] = {
   ** follow these menus.  The option is \fIunset\fP by default because many
   ** visual terminals don't permit making the cursor invisible.
   */
+#ifdef USE_NNTP
+  { "catchup_newsgroup", DT_QUAD, R_NONE, OPT_CATCHUP, M_ASKYES },
+  /*
+  ** .pp
+  ** If this variable is \fIset\fP, Mutt will mark all articles in newsgroup
+  ** as read when you quit the newsgroup (catchup newsgroup).
+  */
+#endif
 #if defined(USE_SSL)
   { "certificate_file",	DT_PATH, R_NONE, UL &SslCertFile, UL "~/.mutt_certificates" },
   /*
@@ -844,6 +866,16 @@ struct option_t MuttVars[] = {
   ** sent to both the list and your address, resulting in two copies
   ** of the same email for you.
   */
+#ifdef USE_NNTP
+  { "followup_to_poster", DT_QUAD, R_NONE, OPT_FOLLOWUPTOPOSTER, M_ASKYES },
+  /*
+  ** .pp
+  ** If this variable is \fIset\fP and the keyword "poster" is present in
+  ** \fIFollowup-To\fP header, follow-up to newsgroup function is not
+  ** permitted.  The message will be mailed to the submitter of the
+  ** message via mail.
+  */
+#endif
   { "force_name",	DT_BOOL, R_NONE, OPTFORCENAME, 0 },
   /*
   ** .pp
@@ -926,6 +958,26 @@ struct option_t MuttVars[] = {
   ** a regular expression that will match the whole name so mutt will expand
   ** ``Franklin'' to ``Franklin, Steve''.
   */
+#ifdef USE_NNTP
+  { "group_index_format", DT_STR, R_BOTH, UL &GroupFormat, UL "%4C %M%N %5s  %-45.45f %d" },
+  /*
+  ** .pp
+  ** This variable allows you to customize the newsgroup browser display to
+  ** your personal taste.  This string is similar to ``$index_format'', but
+  ** has its own set of printf()-like sequences:
+  ** .dl
+  ** .dt %C  .dd current newsgroup number
+  ** .dt %d  .dd description of newsgroup (becomes from server)
+  ** .dt %f  .dd newsgroup name
+  ** .dt %M  .dd - if newsgroup not allowed for direct post (moderated for example)
+  ** .dt %N  .dd N if newsgroup is new, u if unsubscribed, blank otherwise
+  ** .dt %n  .dd number of new articles in newsgroup
+  ** .dt %s  .dd number of unread articles in newsgroup
+  ** .dt %>X .dd right justify the rest of the string and pad with character "X"
+  ** .dt %|X .dd pad to the end of the line with character "X"
+  ** .de
+  */
+#endif
   { "hdr_format",	DT_SYN,  R_NONE, UL "index_format", 0 },
   /*
   */
@@ -1307,6 +1359,7 @@ struct option_t MuttVars[] = {
   ** .dt %E .dd number of messages in current thread
   ** .dt %f .dd sender (address + real name), either From: or Return-Path:
   ** .dt %F .dd author name, or recipient name if the message is from you
+  ** .dt %g .dd newsgroup name (if compiled with NNTP support)
   ** .dt %H .dd spam attribute(s) of this message
   ** .dt %i .dd message-id of the current message
   ** .dt %l .dd number of lines in the message (does not work with maildir,
@@ -1322,12 +1375,14 @@ struct option_t MuttVars[] = {
   **            stashed the message: list name or recipient name
   **            if not sent to a list
   ** .dt %P .dd progress indicator for the built-in pager (how much of the file has been displayed)
+  ** .dt %R .dd ``X-Comment-To:'' field (if present and compiled with NNTP support)
   ** .dt %s .dd subject of the message
   ** .dt %S .dd status of the message (``N''/``D''/``d''/``!''/``r''/\(as)
   ** .dt %t .dd ``To:'' field (recipients)
   ** .dt %T .dd the appropriate character from the $$to_chars string
   ** .dt %u .dd user (login) name of the author
   ** .dt %v .dd first name of the author, or the recipient if the message is from you
+  ** .dt %W .dd name of organization of author (``Organization:'' field)
   ** .dt %X .dd number of attachments
   **            (please see the ``$attachments'' section for possible speed effects)
   ** .dt %y .dd ``X-Label:'' field, if present
@@ -1362,6 +1417,25 @@ struct option_t MuttVars[] = {
   ** Note that these expandos are supported in
   ** ``$save-hook'', ``$fcc-hook'' and ``$fcc-save-hook'', too.
   */
+#ifdef USE_NNTP
+  { "inews",		DT_PATH, R_NONE, UL &Inews, UL "" },
+  /*
+  ** .pp
+  ** If set, specifies the program and arguments used to deliver news posted
+  ** by Mutt.  Otherwise, mutt posts article using current connection to
+  ** news server.  The following printf-style sequence is understood:
+  ** .dl
+  ** .dt %a .dd account url
+  ** .dt %p .dd port
+  ** .dt %P .dd port if specified
+  ** .dt %s .dd news server name
+  ** .dt %S .dd url schema
+  ** .dt %u .dd username
+  ** .de
+  ** .pp
+  ** Example: set inews="/usr/local/bin/inews -hS"
+  */
+#endif
   { "ispell",		DT_PATH, R_NONE, UL &Ispell, UL ISPELL },
   /*
   ** .pp
@@ -1606,6 +1680,15 @@ struct option_t MuttVars[] = {
   ** menu, attachments which cannot be decoded in a reasonable manner will
   ** be attached to the newly composed message if this option is \fIset\fP.
   */
+#ifdef USE_NNTP
+  { "mime_subject",	DT_BOOL, R_NONE, OPTMIMESUBJECT, 1 },
+  /*
+  ** .pp
+  ** If \fIunset\fP, 8-bit ``subject:'' line in article header will not be
+  ** encoded according to RFC2047 to base64.  This is useful when message
+  ** is Usenet article, because MIME for news is nonstandard feature.
+  */
+#endif
 #ifdef MIXMASTER
   { "mix_entry_format", DT_STR,  R_NONE, UL &MixEntryFormat, UL "%4n %c %-16s %a" },
   /*
@@ -1656,6 +1739,100 @@ struct option_t MuttVars[] = {
    ** See also $$read_inc, $$write_inc and $$net_inc.
    */
 #endif
+#ifdef USE_NNTP
+  { "news_cache_dir",	DT_PATH, R_NONE, UL &NewsCacheDir, UL "~/.mutt" },
+  /*
+  ** .pp
+  ** This variable pointing to directory where Mutt will save cached news
+  ** articles and headers in. If \fIunset\fP, articles and headers will not be
+  ** saved at all and will be reloaded from the server each time.
+  */
+  { "news_server",	DT_STR, R_NONE, UL &NewsServer, 0 },
+  /*
+  ** .pp
+  ** This variable specifies domain name or address of NNTP server. It
+  ** defaults to the news server specified in the environment variable
+  ** $$$NNTPSERVER or contained in the file /etc/nntpserver.  You can also
+  ** specify username and an alternative port for each news server, ie:
+  ** .pp
+  ** [[s]news://][username[:password]@]server[:port]
+  */
+  { "newsgroups_charset", DT_STR, R_NONE, UL &NewsgroupsCharset, UL "utf-8" },
+  /*
+  ** .pp
+  ** Character set of newsgroups descriptions.
+  */
+  { "newsrc",		DT_PATH, R_NONE, UL &NewsRc, UL "~/.newsrc" },
+  /*
+  ** .pp
+  ** The file, containing info about subscribed newsgroups - names and
+  ** indexes of read articles.  The following printf-style sequence
+  ** is understood:
+  ** .dl
+  ** .dt %a .dd account url
+  ** .dt %p .dd port
+  ** .dt %P .dd port if specified
+  ** .dt %s .dd news server name
+  ** .dt %S .dd url schema
+  ** .dt %u .dd username
+  ** .de
+  */
+  { "nntp_authenticators", DT_STR, R_NONE, UL &NntpAuthenticators, UL 0 },
+  /*
+  ** .pp
+  ** This is a colon-delimited list of authentication methods mutt may
+  ** attempt to use to log in to a news server, in the order mutt should
+  ** try them.  Authentication methods are either ``user'' or any
+  ** SASL mechanism, e.g. ``digest-md5'', ``gssapi'' or ``cram-md5''.
+  ** This option is case-insensitive.  If it's \fIunset\fP (the default)
+  ** mutt will try all available methods, in order from most-secure to
+  ** least-secure.
+  ** .pp
+  ** Example:
+  ** .ts
+  ** set nntp_authenticators="digest-md5:user"
+  ** .te
+  ** .pp
+  ** \fBNote:\fP Mutt will only fall back to other authentication methods if
+  ** the previous methods are unavailable. If a method is available but
+  ** authentication fails, mutt will not connect to the IMAP server.
+  */
+  { "nntp_context",	DT_NUM, R_NONE, UL &NntpContext, 1000 },
+  /*
+  ** .pp
+  ** This variable defines number of articles which will be in index when
+  ** newsgroup entered.  If active newsgroup have more articles than this
+  ** number, oldest articles will be ignored.  Also controls how many
+  ** articles headers will be saved in cache when you quit newsgroup.
+  */
+  { "nntp_load_description", DT_BOOL, R_NONE, OPTLOADDESC, 1 },
+  /*
+  ** .pp
+  ** This variable controls whether or not descriptions for each newsgroup
+  ** must be loaded when newsgroup is added to list (first time list
+  ** loading or new newsgroup adding).
+  */
+  { "nntp_user",	DT_STR, R_NONE, UL &NntpUser, UL "" },
+  /*
+  ** .pp
+  ** Your login name on the NNTP server.  If \fIunset\fP and NNTP server requires
+  ** authentification, Mutt will prompt you for your account name when you
+  ** connect to news server.
+  */
+  { "nntp_pass",	DT_STR, R_NONE, UL &NntpPass, UL "" },
+  /*
+  ** .pp
+  ** Your password for NNTP account.
+  */
+  { "nntp_poll",	DT_NUM, R_NONE, UL &NewsPollTimeout, 60 },
+  /*
+  ** .pp
+  ** The time in seconds until any operations on newsgroup except post new
+  ** article will cause recheck for new news.  If set to 0, Mutt will
+  ** recheck newsgroup on each operation in index (stepping, read article,
+  ** etc.).
+  */
+#endif
   { "pager",		DT_PATH, R_NONE, UL &Pager, UL "builtin" },
   /*
   ** .pp
@@ -2204,6 +2381,16 @@ struct option_t MuttVars[] = {
   { "post_indent_str",  DT_SYN,  R_NONE, UL "post_indent_string", 0 },
   /*
   */
+#ifdef USE_NNTP
+  { "post_moderated",	DT_QUAD, R_NONE, OPT_TOMODERATED, M_ASKYES },
+  /*
+  ** .pp
+  ** If set to \fIyes\fP, Mutt will post article to newsgroup that have
+  ** not permissions to posting (e.g. moderated).  \fBNote:\fP if news server
+  ** does not support posting to that newsgroup or totally read-only, that
+  ** posting will not have an effect.
+  */
+#endif
   { "postpone",		DT_QUAD, R_NONE, OPT_POSTPONE, M_ASKYES },
   /*
   ** .pp
@@ -2643,6 +2830,28 @@ struct option_t MuttVars[] = {
   ** Command to use when spawning a subshell.  By default, the user's login
   ** shell from \fC/etc/passwd\fP is used.
   */
+#ifdef USE_NNTP
+  { "save_unsubscribed", DT_BOOL, R_NONE, OPTSAVEUNSUB, 0 },
+  /*
+  ** .pp
+  ** When \fIset\fP, info about unsubscribed newsgroups will be saved into
+  ** ``newsrc'' file and into cache.
+  */
+  { "show_new_news",	DT_BOOL, R_NONE, OPTSHOWNEWNEWS, 1 },
+  /*
+  ** .pp
+  ** If \fIset\fP, news server will be asked for new newsgroups on entering
+  ** the browser.  Otherwise, it will be done only once for a news server.
+  ** Also controls whether or not number of new articles of subscribed
+  ** newsgroups will be then checked.
+  */
+  { "show_only_unread",	DT_BOOL, R_NONE, OPTSHOWONLYUNREAD, 0 },
+  /*
+  ** .pp
+  ** If \fIset\fP, only subscribed newsgroups that contain unread articles
+  ** will be displayed in browser.
+  */
+#endif
   { "sig_dashes",	DT_BOOL, R_NONE, OPTSIGDASHES, 1 },
   /*
   ** .pp
@@ -3598,6 +3807,14 @@ struct option_t MuttVars[] = {
   {"xterm_set_titles",	DT_SYN,  R_NONE, UL "ts_enabled", 0 },
   /*
   */
+#ifdef USE_NNTP
+  { "x_comment_to",	DT_BOOL, R_NONE, OPTXCOMMENTTO, 0 },
+  /*
+  ** .pp
+  ** If \fIset\fP, Mutt will add ``X-Comment-To:'' field (that contains full
+  ** name of original article author) to article that followuped to newsgroup.
+  */
+#endif
   /*--*/
   { NULL, 0, 0, 0, 0 }
 };
diff --git a/keymap.c b/keymap.c
index 1710b17..9d90807 100644
--- a/keymap.c
+++ b/keymap.c
@@ -786,7 +786,6 @@ void km_init (void)
   km_bindkey ("<enter>", MENU_MAIN, OP_DISPLAY_MESSAGE);
 
   km_bindkey ("x", MENU_PAGER, OP_EXIT);
-  km_bindkey ("i", MENU_PAGER, OP_EXIT);
   km_bindkey ("<backspace>", MENU_PAGER, OP_PREV_LINE);
   km_bindkey ("<pagedown>", MENU_PAGER, OP_NEXT_PAGE);
   km_bindkey ("<pageup>", MENU_PAGER, OP_PREV_PAGE);
diff --git a/mailbox.h b/mailbox.h
index 000503d..9052e46 100644
--- a/mailbox.h
+++ b/mailbox.h
@@ -77,6 +77,9 @@ int mx_is_imap (const char *);
 #ifdef USE_POP
 int mx_is_pop (const char *);
 #endif
+#ifdef USE_NNTP
+int mx_is_nntp (const char *);
+#endif
 
 int mx_access (const char*, int);
 int mx_check_empty (const char *);
diff --git a/main.c b/main.c
index e71bc9b..01a8121 100644
--- a/main.c
+++ b/main.c
@@ -62,6 +62,10 @@
 #include <idn/stringprep.h>
 #endif
 
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
+
 static const char *ReachingUs = N_("\
 To contact the developers, please mail to <mutt-dev@mutt.org>.\n\
 To report a bug, please visit http://bugs.mutt.org/.\n");
@@ -136,6 +140,8 @@ options:\n\
 "  -e <command>\tspecify a command to be executed after initialization\n\
   -f <file>\tspecify which mailbox to read\n\
   -F <file>\tspecify an alternate muttrc file\n\
+  -g <server>\tspecify a news server (if compiled with NNTP)\n\
+  -G\t\tselect a newsgroup (if compiled with NNTP)\n\
   -H <file>\tspecify a draft file to read header and body from\n\
   -i <file>\tspecify a file which Mutt should include in the body\n\
   -m <type>\tspecify a default mailbox type\n\
@@ -284,6 +290,12 @@ static void show_version (void)
 	"-USE_POP  "
 #endif
 
+#ifdef USE_NNTP
+	"+USE_NNTP  "
+#else
+	"-USE_NNTP  "
+#endif
+
 #ifdef USE_IMAP
         "+USE_IMAP  "
 #else
@@ -558,6 +570,9 @@ init_extended_keys();
 #define M_NOSYSRC (1<<2)	/* -n */
 #define M_RO      (1<<3)	/* -R */
 #define M_SELECT  (1<<4)	/* -y */
+#ifdef USE_NNTP
+#define M_NEWS    (1<<5)	/* -g and -G */
+#endif
 
 int main (int argc, char **argv)
 {
@@ -630,7 +645,11 @@ int main (int argc, char **argv)
         argv[nargc++] = argv[optind];
     }
 
+#ifdef USE_NNTP
+    if ((i = getopt (argc, argv, "+A:a:b:F:f:c:Dd:e:g:GH:s:i:hm:npQ:RvxyzZ")) != EOF)
+#else
     if ((i = getopt (argc, argv, "+A:a:b:F:f:c:Dd:e:H:s:i:hm:npQ:RvxyzZ")) != EOF)
+#endif
       switch (i)
       {
       case 'A':
@@ -727,6 +746,20 @@ int main (int argc, char **argv)
 	flags |= M_SELECT;
 	break;
 
+#ifdef USE_NNTP
+      case 'g': /* Specify a news server */
+	{
+	  char buf[LONG_STRING];
+
+	  snprintf (buf, sizeof (buf), "set news_server=%s", optarg);
+	  commands = mutt_add_list (commands, buf);
+	}
+
+      case 'G': /* List of newsgroups */
+	flags |= M_SELECT | M_NEWS;
+	break;
+#endif
+
       case 'z':
 	flags |= M_IGNORE;
 	break;
@@ -1025,6 +1058,18 @@ int main (int argc, char **argv)
     }
     else if (flags & M_SELECT)
     {
+#ifdef USE_NNTP
+      if (flags & M_NEWS)
+      {
+	set_option (OPTNEWS);
+	if(!(CurrentNewsSrv = nntp_select_server (NewsServer, 0)))
+	{
+	  mutt_endwin (Errorbuf);
+	  exit (1);
+	}
+      }
+      else
+#endif
       if (!Incoming) {
 	mutt_endwin _("No incoming mailboxes defined.");
 	exit (1);
@@ -1040,6 +1085,15 @@ int main (int argc, char **argv)
 
     if (!folder[0])
       strfcpy (folder, NONULL(Spoolfile), sizeof (folder));
+
+#ifdef USE_NNTP
+    if (option (OPTNEWS))
+    {
+      unset_option (OPTNEWS);
+      nntp_expand_path (folder, sizeof (folder), &CurrentNewsSrv->conn->account);
+    }
+    else
+#endif
     mutt_expand_path (folder, sizeof (folder));
 
     mutt_str_replace (&CurrentFolder, folder);
diff --git a/mutt.h b/mutt.h
index d73e514..1ee1583 100644
--- a/mutt.h
+++ b/mutt.h
@@ -239,6 +239,9 @@ enum
   M_PGP_KEY,
   M_XLABEL,
   M_MIMEATTACH,
+#ifdef USE_NNTP
+  M_NEWSGROUPS,
+#endif
   
   /* Options for Mailcap lookup */
   M_EDIT,
@@ -295,6 +298,11 @@ enum
 #endif
   OPT_SUBJECT,
   OPT_VERIFYSIG,      /* verify PGP signatures */
+#ifdef USE_NNTP
+  OPT_TOMODERATED,
+  OPT_CATCHUP,
+  OPT_FOLLOWUPTOPOSTER,
+#endif
     
   /* THIS MUST BE THE LAST VALUE. */
   OPT_MAX
@@ -311,6 +319,7 @@ enum
 #define SENDKEY		(1<<7)
 #define SENDRESEND	(1<<8)
 #define SENDPOSTPONEDFCC	(1<<9) /* used by mutt_get_postponed() to signal that the x-mutt-fcc header field was present */
+#define SENDNEWS	(1<<10)
 
 /* flags to _mutt_select_file() */
 #define M_SEL_BUFFY	(1<<0)
@@ -330,6 +339,8 @@ enum
   OPTASCIICHARS,
   OPTASKBCC,
   OPTASKCC,
+  OPTASKFOLLOWUP,
+  OPTASKXCOMMENTTO,
   OPTATTACHSPLIT,
   OPTAUTOEDIT,
   OPTAUTOTAG,
@@ -411,6 +422,9 @@ enum
   OPTMETOO,
   OPTMHPURGE,
   OPTMIMEFORWDECODE,
+#ifdef USE_NNTP
+  OPTMIMESUBJECT,	/* encode subject line with RFC2047 */
+#endif
   OPTNARROWTREE,
   OPTPAGERSTOP,
   OPTPIPEDECODE,
@@ -499,6 +513,16 @@ enum
   OPTPGPAUTOINLINE,
   OPTPGPREPLYINLINE,
 
+  /* news options */
+
+#ifdef USE_NNTP
+  OPTSHOWNEWNEWS,
+  OPTSHOWONLYUNREAD,
+  OPTSAVEUNSUB,
+  OPTLOADDESC,
+  OPTXCOMMENTTO,
+#endif
+
   /* pseudo options */
 
   OPTAUXSORT,		/* (pseudo) using auxiliary sort function */
@@ -519,6 +543,7 @@ enum
   OPTSORTSUBTHREADS,	/* (pseudo) used when $sort_aux changes */
   OPTNEEDRESCORE,	/* (pseudo) set when the `score' command is used */
   OPTATTACHMSG,		/* (pseudo) used by attach-message */
+  OPTHIDEREAD,		/* (pseudo) whether or not hide read messages */
   OPTKEEPQUIET,		/* (pseudo) shut up the message and refresh
 			 * 	    functions while we are executing an
 			 * 	    external program.
@@ -531,6 +556,11 @@ enum
 
   OPTSIDEBARNEWMAILONLY,
 
+#ifdef USE_NNTP
+  OPTNEWS,		/* (pseudo) used to change reader mode */
+  OPTNEWSSEND,		/* (pseudo) used to change behavior when posting */
+#endif
+
   OPTMAX
 };
 
@@ -610,6 +640,13 @@ typedef struct envelope
   char *supersedes;
   char *date;
   char *x_label;
+  char *organization;
+#ifdef USE_NNTP
+  char *newsgroups;
+  char *xref;
+  char *followup_to;
+  char *x_comment_to;
+#endif
   BUFFER *spam;
   LIST *references;		/* message references (in reverse order) */
   LIST *in_reply_to;		/* in-reply-to header content */
@@ -796,7 +833,7 @@ typedef struct header
   int refno;			/* message number on server */
 #endif
 
-#if defined USE_POP || defined USE_IMAP
+#if defined USE_POP || defined USE_IMAP || defined USE_NNTP
   void *data;            	/* driver-specific data */
 #endif
   
diff --git a/mutt_sasl.c b/mutt_sasl.c
index 7d7388c..0ba4f4e 100644
--- a/mutt_sasl.c
+++ b/mutt_sasl.c
@@ -190,6 +190,11 @@ int mutt_sasl_client_new (CONNECTION* conn, sasl_conn_t** saslconn)
     case M_ACCT_TYPE_SMTP:
       service = "smtp";
       break;
+#ifdef USE_NNTP
+    case M_ACCT_TYPE_NNTP:
+      service = "nntp";
+      break;
+#endif
     default:
       mutt_error (_("Unknown SASL profile"));
       return -1;
diff --git a/muttlib.c b/muttlib.c
index 039e7c3..cc3a681 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -329,7 +329,7 @@ void mutt_free_header (HEADER **h)
 #ifdef MIXMASTER
   mutt_free_list (&(*h)->chain);
 #endif
-#if defined USE_POP || defined USE_IMAP
+#if defined USE_POP || defined USE_IMAP || defined USE_NNTP
   FREE (&(*h)->data);
 #endif
   FREE (h);		/* __FREE_CHECKED__ */
@@ -717,6 +717,13 @@ void mutt_free_envelope (ENVELOPE **p)
   FREE (&(*p)->supersedes);
   FREE (&(*p)->date);
   FREE (&(*p)->x_label);
+  FREE (&(*p)->organization);
+#ifdef USE_NNTP
+  FREE (&(*p)->newsgroups);
+  FREE (&(*p)->xref);
+  FREE (&(*p)->followup_to);
+  FREE (&(*p)->x_comment_to);
+#endif
 
   mutt_buffer_free (&(*p)->spam);
 
@@ -1568,6 +1575,14 @@ int mutt_save_confirm (const char *s, struct stat *st)
     }
   }
 
+#ifdef USE_NNTP
+  if (magic == M_NNTP)
+  {
+    mutt_error _("Can't save message to news server.");
+    return 0;
+  }
+#endif
+
   if (stat (s, st) != -1)
   {
     if (magic == -1)
diff --git a/mx.c b/mx.c
index e80b8ff..b2ac0b7 100644
--- a/mx.c
+++ b/mx.c
@@ -347,6 +347,22 @@ int mx_is_pop (const char *p)
 }
 #endif
 
+#ifdef USE_NNTP
+int mx_is_nntp (const char *p)
+{
+  url_scheme_t scheme;
+
+  if (!p)
+    return 0;
+
+  scheme = url_check_scheme (p);
+  if (scheme == U_NNTP || scheme == U_NNTPS)
+    return 1;
+
+  return 0;
+}
+#endif
+
 int mx_get_magic (const char *path)
 {
   struct stat st;
@@ -364,6 +380,11 @@ int mx_get_magic (const char *path)
     return M_POP;
 #endif /* USE_POP */
 
+#ifdef USE_NNTP
+  if (mx_is_nntp (path))
+    return M_NNTP;
+#endif /* USE_NNTP */
+
   if (stat (path, &st) == -1)
   {
     dprint (1, (debugfile, "mx_get_magic(): unable to stat %s: %s (errno %d).\n",
@@ -691,6 +712,12 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT *pctx)
       break;
 #endif /* USE_POP */
 
+#ifdef USE_NNTP
+    case M_NNTP:
+      rc = nntp_open_mailbox (ctx);
+      break;
+#endif /* USE_NNTP */
+
     default:
       rc = -1;
       break;
@@ -803,6 +830,12 @@ static int sync_mailbox (CONTEXT *ctx, int *index_hint)
       rc = pop_sync_mailbox (ctx, index_hint);
       break;
 #endif /* USE_POP */
+
+#ifdef USE_NNTP
+    case M_NNTP:
+      rc = nntp_sync_mailbox (ctx);
+      break;
+#endif /* USE_NNTP */
   }
 
 #if 0
@@ -905,6 +938,25 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
     return 0;
   }
 
+#ifdef USE_NNTP
+  if (ctx->unread && ctx->magic == M_NNTP)
+  {
+    NNTP_DATA *nntp_data = ctx->data;
+
+    if (nntp_data && nntp_data->nserv && nntp_data->group)
+    {
+      int rc = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
+      if (rc < 0)
+      {
+	ctx->closing = 0;
+	return -1;
+      }
+      else if (rc == M_YES)
+	mutt_newsgroup_catchup (nntp_data->nserv, nntp_data->group);
+    }
+  }
+#endif
+
   for (i = 0; i < ctx->msgcount; i++)
   {
     if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->read 
@@ -912,6 +964,12 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
       read_msgs++;
   }
 
+#ifdef USE_NNTP
+  /* don't need to move articles from newsgroup */
+  if (ctx->magic == M_NNTP)
+    read_msgs = 0;
+#endif
+
   if (read_msgs && quadoption (OPT_MOVE) != M_NO)
   {
     char *p;
@@ -1465,6 +1523,11 @@ int mx_check_mailbox (CONTEXT *ctx, int *index_hint, int lock)
       case M_POP:
 	return (pop_check_mailbox (ctx, index_hint));
 #endif /* USE_POP */
+
+#ifdef USE_NNTP
+      case M_NNTP:
+	return (nntp_check_mailbox (ctx, 0));
+#endif /* USE_NNTP */
     }
   }
 
@@ -1525,6 +1588,15 @@ MESSAGE *mx_open_message (CONTEXT *ctx, int msgno)
     }
 #endif /* USE_POP */
 
+#ifdef USE_NNTP
+    case M_NNTP:
+    {
+      if (nntp_fetch_message (msg, ctx, msgno) != 0)
+	FREE (&msg);
+      break;
+    }
+#endif /* USE_NNTP */
+
     default:
       dprint (1, (debugfile, "mx_open_message(): function not implemented for mailbox type %d.\n", ctx->magic));
       FREE (&msg);
@@ -1600,6 +1672,9 @@ int mx_close_message (MESSAGE **msg)
   int r = 0;
 
   if ((*msg)->magic == M_MH || (*msg)->magic == M_MAILDIR
+#ifdef USE_NNTP
+      || (*msg)->magic == M_NNTP
+#endif
       || (*msg)->magic == M_IMAP || (*msg)->magic == M_POP)
   {
     r = safe_fclose (&(*msg)->fp);
diff --git a/mx.h b/mx.h
index 4aabadf..9980c4e 100644
--- a/mx.h
+++ b/mx.h
@@ -34,6 +34,9 @@ enum
   M_MMDF,
   M_MH,
   M_MAILDIR,
+#ifdef USE_NNTP
+  M_NNTP,
+#endif
   M_IMAP,
   M_POP
 #ifdef USE_COMPRESSED
diff --git a/newsrc.c b/newsrc.c
new file mode 100644
index 0000000..482214c
--- /dev/null
+++ b/newsrc.c
@@ -0,0 +1,1260 @@
+/*
+ * Copyright (C) 1998 Brandon Long <blong@fiction.net>
+ * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
+ * Copyright (C) 2000-2012 Vsevolod Volkov <vvv@mutt.org.ua>
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "mutt.h"
+#include "mutt_curses.h"
+#include "sort.h"
+#include "mx.h"
+#include "mime.h"
+#include "mailbox.h"
+#include "nntp.h"
+#include "rfc822.h"
+#include "rfc1524.h"
+#include "rfc2047.h"
+#include "bcache.h"
+
+#if USE_HCACHE
+#include "hcache.h"
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+
+/* Find NNTP_DATA for given newsgroup or add it */
+static NNTP_DATA *nntp_data_find (NNTP_SERVER *nserv, const char *group)
+{
+  NNTP_DATA *nntp_data = hash_find (nserv->groups_hash, group);
+
+  if (!nntp_data)
+  {
+    /* create NNTP_DATA structure and add it to hash */
+    nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
+    nntp_data->group = (char *)nntp_data + sizeof (NNTP_DATA);
+    strcpy (nntp_data->group, group);
+    nntp_data->nserv = nserv;
+    nntp_data->deleted = 1;
+    if (nserv->groups_hash->nelem < nserv->groups_hash->curnelem * 2)
+      nserv->groups_hash = hash_resize (nserv->groups_hash,
+			   nserv->groups_hash->nelem * 2, 0);
+    hash_insert (nserv->groups_hash, nntp_data->group, nntp_data, 0);
+
+    /* add NNTP_DATA to list */
+    if (nserv->groups_num >= nserv->groups_max)
+    {
+      nserv->groups_max *= 2;
+      safe_realloc (&nserv->groups_list,
+		    nserv->groups_max * sizeof (nntp_data));
+    }
+    nserv->groups_list[nserv->groups_num++] = nntp_data;
+  }
+  return nntp_data;
+}
+
+/* Remove all temporarily cache files */
+void nntp_acache_free (NNTP_DATA *nntp_data)
+{
+  int i;
+
+  for (i = 0; i < NNTP_ACACHE_LEN; i++)
+  {
+    if (nntp_data->acache[i].path)
+    {
+      unlink (nntp_data->acache[i].path);
+      FREE (&nntp_data->acache[i].path);
+    }
+  }
+}
+
+/* Free NNTP_DATA, used to destroy hash elements */
+void nntp_data_free (void *data)
+{
+  NNTP_DATA *nntp_data = data;
+
+  if (!nntp_data)
+    return;
+  nntp_acache_free (nntp_data);
+  mutt_bcache_close (&nntp_data->bcache);
+  FREE (&nntp_data->newsrc_ent);
+  FREE (&nntp_data->desc);
+  FREE (&data);
+}
+
+/* Unlock and close .newsrc file */
+void nntp_newsrc_close (NNTP_SERVER *nserv)
+{
+  if (!nserv->newsrc_fp)
+    return;
+
+  dprint (1, (debugfile, "Unlocking %s\n", nserv->newsrc_file));
+  mx_unlock_file (nserv->newsrc_file, fileno (nserv->newsrc_fp), 0);
+  safe_fclose (&nserv->newsrc_fp);
+}
+
+/* Parse .newsrc file:
+ *  0 - not changed
+ *  1 - parsed
+ * -1 - error */
+int nntp_newsrc_parse (NNTP_SERVER *nserv)
+{
+  unsigned int i;
+  char *line;
+  struct stat sb;
+
+  /* if file doesn't exist, create it */
+  nserv->newsrc_fp = safe_fopen (nserv->newsrc_file, "a");
+  safe_fclose (&nserv->newsrc_fp);
+
+  /* open .newsrc */
+  nserv->newsrc_fp = safe_fopen (nserv->newsrc_file, "r");
+  if (!nserv->newsrc_fp)
+  {
+    mutt_perror (nserv->newsrc_file);
+    mutt_sleep (2);
+    return -1;
+  }
+
+  /* lock it */
+  dprint (1, (debugfile, "Locking %s\n", nserv->newsrc_file));
+  if (mx_lock_file (nserv->newsrc_file, fileno (nserv->newsrc_fp), 0, 0, 1))
+  {
+    safe_fclose (&nserv->newsrc_fp);
+    return -1;
+  }
+
+  if (stat (nserv->newsrc_file, &sb))
+  {
+    mutt_perror (nserv->newsrc_file);
+    nntp_newsrc_close (nserv);
+    mutt_sleep (2);
+    return -1;
+  }
+
+  if (nserv->size == sb.st_size && nserv->mtime == sb.st_mtime)
+    return 0;
+
+  nserv->size = sb.st_size;
+  nserv->mtime = sb.st_mtime;
+  nserv->newsrc_modified = 1;
+  dprint (1, (debugfile, "Parsing %s\n", nserv->newsrc_file));
+
+  /* .newsrc has been externally modified or hasn't been loaded yet */
+  for (i = 0; i < nserv->groups_num; i++)
+  {
+    NNTP_DATA *nntp_data = nserv->groups_list[i];
+
+    if (!nntp_data)
+      continue;
+
+    nntp_data->subscribed = 0;
+    nntp_data->newsrc_len = 0;
+    FREE (&nntp_data->newsrc_ent);
+  }
+
+  line = safe_malloc (sb.st_size + 1);
+  while (sb.st_size && fgets (line, sb.st_size + 1, nserv->newsrc_fp))
+  {
+    char *b, *h, *p;
+    unsigned int subs = 0, i = 1;
+    NNTP_DATA *nntp_data;
+
+    /* find end of newsgroup name */
+    p = strpbrk (line, ":!");
+    if (!p)
+      continue;
+
+    /* ":" - subscribed, "!" - unsubscribed */
+    if (*p == ':')
+      subs++;
+    *p++ = '\0';
+
+    /* get newsgroup data */
+    nntp_data = nntp_data_find (nserv, line);
+    FREE (&nntp_data->newsrc_ent);
+
+    /* count number of entries */
+    b = p;
+    while (*b)
+      if (*b++ == ',')
+	i++;
+    nntp_data->newsrc_ent = safe_calloc (i, sizeof (NEWSRC_ENTRY));
+    nntp_data->subscribed = subs;
+
+    /* parse entries */
+    i = 0;
+    while (p)
+    {
+      b = p;
+
+      /* find end of entry */
+      p = strchr (p, ',');
+      if (p)
+	*p++ = '\0';
+
+      /* first-last or single number */
+      h = strchr (b, '-');
+      if (h)
+	*h++ = '\0';
+      else
+	h = b;
+
+      if (sscanf (b, ANUM, &nntp_data->newsrc_ent[i].first) == 1 &&
+	  sscanf (h, ANUM, &nntp_data->newsrc_ent[i].last) == 1)
+	i++;
+    }
+    if (i == 0)
+    {
+	nntp_data->newsrc_ent[i].first = 1;
+	nntp_data->newsrc_ent[i].last = 0;
+	i++;
+    }
+    if (nntp_data->lastMessage == 0)
+      nntp_data->lastMessage = nntp_data->newsrc_ent[i - 1].last;
+    nntp_data->newsrc_len = i;
+    safe_realloc (&nntp_data->newsrc_ent, i * sizeof (NEWSRC_ENTRY));
+    nntp_group_unread_stat (nntp_data);
+    dprint (2, (debugfile, "nntp_newsrc_parse: %s\n", nntp_data->group));
+  }
+  FREE (&line);
+  return 1;
+}
+
+/* Generate array of .newsrc entries */
+void nntp_newsrc_gen_entries (CONTEXT *ctx)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  anum_t last = 0, first = 1;
+  int series, i;
+  int save_sort = SORT_ORDER;
+  unsigned int entries;
+
+  if (Sort != SORT_ORDER)
+  {
+    save_sort = Sort;
+    Sort = SORT_ORDER;
+    mutt_sort_headers (ctx, 0);
+  }
+
+  entries = nntp_data->newsrc_len;
+  if (!entries)
+  {
+    entries = 5;
+    nntp_data->newsrc_ent = safe_calloc (entries, sizeof (NEWSRC_ENTRY));
+  }
+
+  /* Set up to fake initial sequence from 1 to the article before the
+   * first article in our list */
+  nntp_data->newsrc_len = 0;
+  series = 1;
+  for (i = 0; i < ctx->msgcount; i++)
+  {
+    /* search for first unread */
+    if (series)
+    {
+      /* We don't actually check sequential order, since we mark
+       * "missing" entries as read/deleted */
+      last = NHDR (ctx->hdrs[i])->article_num;
+      if (last >= nntp_data->firstMessage && !ctx->hdrs[i]->deleted &&
+	  !ctx->hdrs[i]->read)
+      {
+	if (nntp_data->newsrc_len >= entries)
+	{
+	  entries *= 2;
+	  safe_realloc (&nntp_data->newsrc_ent, entries * sizeof (NEWSRC_ENTRY));
+	}
+	nntp_data->newsrc_ent[nntp_data->newsrc_len].first = first;
+	nntp_data->newsrc_ent[nntp_data->newsrc_len].last = last - 1;
+	nntp_data->newsrc_len++;
+	series = 0;
+      }
+    }
+
+    /* search for first read */
+    else
+    {
+      if (ctx->hdrs[i]->deleted || ctx->hdrs[i]->read)
+      {
+	first = last + 1;
+	series = 1;
+      }
+      last = NHDR (ctx->hdrs[i])->article_num;
+    }
+  }
+
+  if (series && first <= nntp_data->lastLoaded)
+  {
+    if (nntp_data->newsrc_len >= entries)
+    {
+      entries++;
+      safe_realloc (&nntp_data->newsrc_ent, entries * sizeof (NEWSRC_ENTRY));
+    }
+    nntp_data->newsrc_ent[nntp_data->newsrc_len].first = first;
+    nntp_data->newsrc_ent[nntp_data->newsrc_len].last = nntp_data->lastLoaded;
+    nntp_data->newsrc_len++;
+  }
+  safe_realloc (&nntp_data->newsrc_ent,
+		nntp_data->newsrc_len * sizeof (NEWSRC_ENTRY));
+
+  if (save_sort != Sort)
+  {
+    Sort = save_sort;
+    mutt_sort_headers (ctx, 0);
+  }
+}
+
+/* Update file with new contents */
+static int update_file (char *filename, char *buf)
+{
+  FILE *fp;
+  char tmpfile[_POSIX_PATH_MAX];
+  int rc = -1;
+
+  while (1)
+  {
+    snprintf (tmpfile, sizeof (tmpfile), "%s.tmp", filename);
+    fp = fopen (tmpfile, "w");
+    if (!fp)
+    {
+      mutt_perror (tmpfile);
+      *tmpfile = '\0';
+      break;
+    }
+    if (fputs (buf, fp) == EOF)
+    {
+      mutt_perror (tmpfile);
+      break;
+    }
+    if (fclose (fp) == EOF)
+    {
+      mutt_perror (tmpfile);
+      fp = NULL;
+      break;
+    }
+    fp = NULL;
+    if (rename (tmpfile, filename) < 0)
+    {
+      mutt_perror (filename);
+      break;
+    }
+    *tmpfile = '\0';
+    rc = 0;
+    break;
+  }
+  if (fp)
+    fclose (fp);
+  if (*tmpfile)
+    unlink (tmpfile);
+  if (rc)
+    mutt_sleep (2);
+  return rc;
+}
+
+/* Update .newsrc file */
+int nntp_newsrc_update (NNTP_SERVER *nserv)
+{
+  char *buf;
+  size_t buflen, off;
+  unsigned int i;
+  int rc = -1;
+
+  if (!nserv)
+    return -1;
+
+  buflen = 10 * LONG_STRING;
+  buf = safe_calloc (1, buflen);
+  off = 0;
+
+  /* we will generate full newsrc here */
+  for (i = 0; i < nserv->groups_num; i++)
+  {
+    NNTP_DATA *nntp_data = nserv->groups_list[i];
+    unsigned int n;
+
+    if (!nntp_data || !nntp_data->newsrc_ent)
+      continue;
+
+    /* write newsgroup name */
+    if (off + strlen (nntp_data->group) + 3 > buflen)
+    {
+      buflen *= 2;
+      safe_realloc (&buf, buflen);
+    }
+    snprintf (buf + off, buflen - off, "%s%c ", nntp_data->group,
+	      nntp_data->subscribed ? ':' : '!');
+    off += strlen (buf + off);
+
+    /* write entries */
+    for (n = 0; n < nntp_data->newsrc_len; n++)
+    {
+      if (off + LONG_STRING > buflen)
+      {
+	buflen *= 2;
+	safe_realloc (&buf, buflen);
+      }
+      if (n)
+	buf[off++] = ',';
+      if (nntp_data->newsrc_ent[n].first == nntp_data->newsrc_ent[n].last)
+	snprintf (buf + off, buflen - off, "%d", nntp_data->newsrc_ent[n].first);
+      else if (nntp_data->newsrc_ent[n].first < nntp_data->newsrc_ent[n].last)
+	snprintf (buf + off, buflen - off, "%d-%d",
+		  nntp_data->newsrc_ent[n].first, nntp_data->newsrc_ent[n].last);
+      off += strlen (buf + off);
+    }
+    buf[off++] = '\n';
+  }
+  buf[off] = '\0';
+
+  /* newrc being fully rewritten */
+  dprint (1, (debugfile, "Updating %s\n", nserv->newsrc_file));
+  if (nserv->newsrc_file && update_file (nserv->newsrc_file, buf) == 0)
+  {
+    struct stat sb;
+
+    rc = stat (nserv->newsrc_file, &sb);
+    if (rc == 0)
+    {
+      nserv->size = sb.st_size;
+      nserv->mtime = sb.st_mtime;
+    }
+    else
+    {
+      mutt_perror (nserv->newsrc_file);
+      mutt_sleep (2);
+    }
+  }
+  FREE (&buf);
+  return rc;
+}
+
+/* Make fully qualified cache file name */
+static void cache_expand (char *dst, size_t dstlen, ACCOUNT *acct, char *src)
+{
+  char *c;
+  char file[_POSIX_PATH_MAX];
+
+  /* server subdirectory */
+  if (acct)
+  {
+    ciss_url_t url;
+
+    mutt_account_tourl (acct, &url);
+    url.path = src;
+    url_ciss_tostring (&url, file, sizeof (file), U_PATH);
+  }
+  else
+    strfcpy (file, src ? src : "", sizeof (file));
+
+  snprintf (dst, dstlen, "%s/%s", NewsCacheDir, file);
+
+  /* remove trailing slash */
+  c = dst + strlen (dst) - 1;
+  if (*c == '/')
+    *c = '\0';
+  mutt_expand_path (dst, dstlen);
+}
+
+/* Make fully qualified url from newsgroup name */
+void nntp_expand_path (char *line, size_t len, ACCOUNT *acct)
+{
+  ciss_url_t url;
+
+  url.path = safe_strdup (line);
+  mutt_account_tourl (acct, &url);
+  url_ciss_tostring (&url, line, len, 0);
+  FREE (&url.path);
+}
+
+/* Parse newsgroup */
+int nntp_add_group (char *line, void *data)
+{
+  NNTP_SERVER *nserv = data;
+  NNTP_DATA *nntp_data;
+  char group[LONG_STRING];
+  char desc[HUGE_STRING] = "";
+  char mod;
+  anum_t first, last;
+
+  if (!nserv || !line)
+    return 0;
+
+  if (sscanf (line, "%s " ANUM " " ANUM " %c %[^\n]", group,
+	      &last, &first, &mod, desc) < 4)
+    return 0;
+
+  nntp_data = nntp_data_find (nserv, group);
+  nntp_data->deleted = 0;
+  nntp_data->firstMessage = first;
+  nntp_data->lastMessage = last;
+  nntp_data->allowed = mod == 'y' || mod == 'm' ? 1 : 0;
+  mutt_str_replace (&nntp_data->desc, desc);
+  if (nntp_data->newsrc_ent || nntp_data->lastCached)
+    nntp_group_unread_stat (nntp_data);
+  else if (nntp_data->lastMessage &&
+	   nntp_data->firstMessage <= nntp_data->lastMessage)
+    nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
+  else
+    nntp_data->unread = 0;
+  return 0;
+}
+
+/* Load list of all newsgroups from cache */
+static int active_get_cache (NNTP_SERVER *nserv)
+{
+  char buf[HUGE_STRING];
+  char file[_POSIX_PATH_MAX];
+  time_t t;
+  FILE *fp;
+
+  cache_expand (file, sizeof (file), &nserv->conn->account, ".active");
+  dprint (1, (debugfile, "Parsing %s\n", file));
+  fp = safe_fopen (file, "r");
+  if (!fp)
+    return -1;
+
+  if (fgets (buf, sizeof (buf), fp) == NULL ||
+      sscanf (buf, "%ld%s", &t, file) != 1 || t == 0)
+  {
+    fclose (fp);
+    return -1;
+  }
+  nserv->newgroups_time = t;
+
+  mutt_message _("Loading list of groups from cache...");
+  while (fgets (buf, sizeof (buf), fp))
+    nntp_add_group (buf, nserv);
+  nntp_add_group (NULL, NULL);
+  fclose (fp);
+  mutt_clear_error ();
+  return 0;
+}
+
+/* Save list of all newsgroups to cache */
+int nntp_active_save_cache (NNTP_SERVER *nserv)
+{
+  char file[_POSIX_PATH_MAX];
+  char *buf;
+  size_t buflen, off;
+  unsigned int i;
+  int rc;
+
+  if (!nserv->cacheable)
+    return 0;
+
+  buflen = 10 * LONG_STRING;
+  buf = safe_calloc (1, buflen);
+  snprintf (buf, buflen, "%lu\n", (unsigned long)nserv->newgroups_time);
+  off = strlen (buf);
+
+  for (i = 0; i < nserv->groups_num; i++)
+  {
+    NNTP_DATA *nntp_data = nserv->groups_list[i];
+
+    if (!nntp_data || nntp_data->deleted)
+      continue;
+
+    if (off + strlen (nntp_data->group) +
+	(nntp_data->desc ? strlen (nntp_data->desc) : 0) + 50 > buflen)
+    {
+      buflen *= 2;
+      safe_realloc (&buf, buflen);
+    }
+    snprintf (buf + off, buflen - off, "%s %d %d %c%s%s\n", nntp_data->group,
+	      nntp_data->lastMessage, nntp_data->firstMessage,
+	      nntp_data->allowed ? 'y' : 'n', nntp_data->desc ? " " : "",
+	      nntp_data->desc ? nntp_data->desc : "");
+    off += strlen (buf + off);
+  }
+
+  cache_expand (file, sizeof (file), &nserv->conn->account, ".active");
+  dprint (1, (debugfile, "Updating %s\n", file));
+  rc = update_file (file, buf);
+  FREE (&buf);
+  return rc;
+}
+
+#ifdef USE_HCACHE
+/* Used by mutt_hcache_open() to compose hcache file name */
+static int nntp_hcache_namer (const char *path, char *dest, size_t destlen)
+{
+  return snprintf (dest, destlen, "%s.hcache", path);
+}
+
+/* Open newsgroup hcache */
+header_cache_t *nntp_hcache_open (NNTP_DATA *nntp_data)
+{
+  ciss_url_t url;
+  char file[_POSIX_PATH_MAX];
+
+  if (!nntp_data->nserv || !nntp_data->nserv->cacheable ||
+      !nntp_data->nserv->conn || !nntp_data->group ||
+      !(nntp_data->newsrc_ent || nntp_data->subscribed ||
+      option (OPTSAVEUNSUB)))
+    return NULL;
+
+  mutt_account_tourl (&nntp_data->nserv->conn->account, &url);
+  url.path = nntp_data->group;
+  url_ciss_tostring (&url, file, sizeof (file), U_PATH);
+  return mutt_hcache_open (NewsCacheDir, file, nntp_hcache_namer);
+}
+
+/* Remove stale cached headers */
+void nntp_hcache_update (NNTP_DATA *nntp_data, header_cache_t *hc)
+{
+  char buf[16];
+  int old = 0;
+  void *hdata;
+  anum_t first, last, current;
+
+  if (!hc)
+    return;
+
+  /* fetch previous values of first and last */
+  hdata = mutt_hcache_fetch_raw (hc, "index", strlen);
+  if (hdata)
+  {
+    dprint (2, (debugfile,
+		"nntp_hcache_update: mutt_hcache_fetch index: %s\n", hdata));
+    if (sscanf (hdata, ANUM " " ANUM, &first, &last) == 2)
+    {
+      old = 1;
+      nntp_data->lastCached = last;
+
+      /* clean removed headers from cache */
+      for (current = first; current <= last; current++)
+      {
+	if (current >= nntp_data->firstMessage &&
+	    current <= nntp_data->lastMessage)
+	  continue;
+
+	snprintf (buf, sizeof (buf), "%d", current);
+	dprint (2, (debugfile,
+		    "nntp_hcache_update: mutt_hcache_delete %s\n", buf));
+	mutt_hcache_delete (hc, buf, strlen);
+      }
+    }
+  }
+
+  /* store current values of first and last */
+  if (!old || nntp_data->firstMessage != first ||
+	      nntp_data->lastMessage != last)
+  {
+    snprintf (buf, sizeof (buf), "%u %u", nntp_data->firstMessage,
+					  nntp_data->lastMessage);
+    dprint (2, (debugfile,
+		"nntp_hcache_update: mutt_hcache_store index: %s\n", buf));
+    mutt_hcache_store_raw (hc, "index", buf, strlen (buf) + 1, strlen);
+  }
+}
+#endif
+
+/* Remove bcache file */
+static int nntp_bcache_delete (const char *id, body_cache_t *bcache, void *data)
+{
+  NNTP_DATA *nntp_data = data;
+  anum_t anum;
+  char c;
+
+  if (!nntp_data || sscanf (id, ANUM "%c", &anum, &c) != 1 ||
+      anum < nntp_data->firstMessage || anum > nntp_data->lastMessage)
+  {
+    if (nntp_data)
+      dprint (2, (debugfile, "nntp_bcache_delete: mutt_bcache_del %s\n", id));
+    mutt_bcache_del (bcache, id);
+  }
+  return 0;
+}
+
+/* Remove stale cached messages */
+void nntp_bcache_update (NNTP_DATA *nntp_data)
+{
+  mutt_bcache_list (nntp_data->bcache, nntp_bcache_delete, nntp_data);
+}
+
+/* Remove hcache and bcache of newsgroup */
+void nntp_delete_group_cache (NNTP_DATA *nntp_data)
+{
+  char file[_POSIX_PATH_MAX];
+
+  if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->cacheable)
+    return;
+
+#ifdef USE_HCACHE
+  nntp_hcache_namer (nntp_data->group, file, sizeof (file));
+  cache_expand (file, sizeof (file), &nntp_data->nserv->conn->account, file);
+  unlink (file);
+  nntp_data->lastCached = 0;
+  dprint (2, (debugfile, "nntp_delete_group_cache: %s\n", file));
+#endif
+
+  if (!nntp_data->bcache)
+    nntp_data->bcache = mutt_bcache_open (&nntp_data->nserv->conn->account,
+			nntp_data->group);
+  if (nntp_data->bcache)
+  {
+    dprint (2, (debugfile, "nntp_delete_group_cache: %s/*\n", nntp_data->group));
+    mutt_bcache_list (nntp_data->bcache, nntp_bcache_delete, NULL);
+    mutt_bcache_close (&nntp_data->bcache);
+  }
+}
+
+/* Remove hcache and bcache of all unexistent and unsubscribed newsgroups */
+void nntp_clear_cache (NNTP_SERVER *nserv)
+{
+  char file[_POSIX_PATH_MAX];
+  char *fp;
+  struct dirent *entry;
+  DIR *dp;
+
+  if (!nserv || !nserv->cacheable)
+    return;
+
+  cache_expand (file, sizeof (file), &nserv->conn->account, NULL);
+  dp = opendir (file);
+  if (dp)
+  {
+    safe_strncat (file, sizeof (file), "/", 1);
+    fp = file + strlen (file);
+    while ((entry = readdir (dp)))
+    {
+      char *group = entry->d_name;
+      struct stat sb;
+      NNTP_DATA *nntp_data;
+      NNTP_DATA nntp_tmp;
+
+      if (mutt_strcmp (group, ".") == 0 ||
+	  mutt_strcmp (group, "..") == 0)
+	continue;
+      *fp = '\0';
+      safe_strncat (file, sizeof (file), group, strlen (group));
+      if (stat (file, &sb))
+	continue;
+
+#ifdef USE_HCACHE
+      if (S_ISREG (sb.st_mode))
+      {
+	char *ext = group + strlen (group) - 7;
+	if (strlen (group) < 8 || mutt_strcmp (ext, ".hcache"))
+	  continue;
+	*ext = '\0';
+      }
+      else
+#endif
+      if (!S_ISDIR (sb.st_mode))
+	continue;
+
+      nntp_data = hash_find (nserv->groups_hash, group);
+      if (!nntp_data)
+      {
+	nntp_data = &nntp_tmp;
+	nntp_data->nserv = nserv;
+	nntp_data->group = group;
+	nntp_data->bcache = NULL;
+      }
+      else if (nntp_data->newsrc_ent || nntp_data->subscribed ||
+	       option (OPTSAVEUNSUB))
+	continue;
+
+      nntp_delete_group_cache (nntp_data);
+      if (S_ISDIR (sb.st_mode))
+      {
+	rmdir (file);
+	dprint (2, (debugfile, "nntp_clear_cache: %s\n", file));
+      }
+    }
+    closedir (dp);
+  }
+  return;
+}
+
+/* %a = account url
+ * %p = port
+ * %P = port if specified
+ * %s = news server name
+ * %S = url schema
+ * %u = username */
+const char *
+nntp_format_str (char *dest, size_t destlen, size_t col, char op, const char *src,
+		const char *fmt, const char *ifstring, const char *elsestring,
+		unsigned long data, format_flag flags)
+{
+  NNTP_SERVER *nserv = (NNTP_SERVER *)data;
+  ACCOUNT *acct = &nserv->conn->account;
+  ciss_url_t url;
+  char fn[SHORT_STRING], tmp[SHORT_STRING], *p;
+
+  switch (op)
+  {
+    case 'a':
+      mutt_account_tourl (acct, &url);
+      url_ciss_tostring (&url, fn, sizeof (fn), U_PATH);
+      p = strchr (fn, '/');
+      if (p)
+	*p = '\0';
+      snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+      snprintf (dest, destlen, tmp, fn);
+      break;
+    case 'p':
+      snprintf (tmp, sizeof (tmp), "%%%su", fmt);
+      snprintf (dest, destlen, tmp, acct->port);
+      break;
+    case 'P':
+      *dest = '\0';
+      if (acct->flags & M_ACCT_PORT)
+      {
+	snprintf (tmp, sizeof (tmp), "%%%su", fmt);
+	snprintf (dest, destlen, tmp, acct->port);
+      }
+      break;
+    case 's':
+      strncpy (fn, acct->host, sizeof (fn) - 1);
+      mutt_strlower (fn);
+      snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+      snprintf (dest, destlen, tmp, fn);
+      break;
+    case 'S':
+      mutt_account_tourl (acct, &url);
+      url_ciss_tostring (&url, fn, sizeof (fn), U_PATH);
+      p = strchr (fn, ':');
+      if (p)
+	*p = '\0';
+      snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+      snprintf (dest, destlen, tmp, fn);
+      break;
+    case 'u':
+      snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
+      snprintf (dest, destlen, tmp, acct->user);
+      break;
+  }
+  return (src);
+}
+
+/* Automatically loads a newsrc into memory, if necessary.
+ * Checks the size/mtime of a newsrc file, if it doesn't match, load
+ * again.  Hmm, if a system has broken mtimes, this might mean the file
+ * is reloaded every time, which we'd have to fix. */
+NNTP_SERVER *nntp_select_server (char *server, int leave_lock)
+{
+  char file[_POSIX_PATH_MAX];
+  char *p;
+  int rc;
+  struct stat sb;
+  ACCOUNT acct;
+  NNTP_SERVER *nserv;
+  NNTP_DATA *nntp_data;
+  CONNECTION *conn;
+  ciss_url_t url;
+
+  if (!server || !*server)
+  {
+    mutt_error _("No news server defined!");
+    mutt_sleep (2);
+    return NULL;
+  }
+
+  /* create account from news server url */
+  acct.flags = 0;
+  acct.port = NNTP_PORT;
+  acct.type = M_ACCT_TYPE_NNTP;
+  snprintf (file, sizeof (file), "%s%s",
+	    strstr (server, "://") ? "" : "news://", server);
+  if (url_parse_ciss (&url, file) < 0 ||
+      (url.path && *url.path) ||
+      !(url.scheme == U_NNTP || url.scheme == U_NNTPS) ||
+      mutt_account_fromurl (&acct, &url) < 0)
+  {
+    mutt_error (_("%s is an invalid news server specification!"), server);
+    mutt_sleep (2);
+    return NULL;
+  }
+  if (url.scheme == U_NNTPS)
+  {
+    acct.flags |= M_ACCT_SSL;
+    acct.port = NNTP_SSL_PORT;
+  }
+
+  /* find connection by account */
+  conn = mutt_conn_find (NULL, &acct);
+  if (!conn)
+    return NULL;
+  if (!(conn->account.flags & M_ACCT_USER) && acct.flags & M_ACCT_USER)
+  {
+    conn->account.flags |= M_ACCT_USER;
+    conn->account.user[0] = '\0';
+  }
+
+  /* news server already exists */
+  nserv = conn->data;
+  if (nserv)
+  {
+    if (nserv->status == NNTP_BYE)
+      nserv->status = NNTP_NONE;
+    if (nntp_open_connection (nserv) < 0)
+      return NULL;
+
+    rc = nntp_newsrc_parse (nserv);
+    if (rc < 0)
+      return NULL;
+
+    /* check for new newsgroups */
+    if (!leave_lock && nntp_check_new_groups (nserv) < 0)
+      rc = -1;
+
+    /* .newsrc has been externally modified */
+    if (rc > 0)
+      nntp_clear_cache (nserv);
+    if (rc < 0 || !leave_lock)
+      nntp_newsrc_close (nserv);
+    return rc < 0 ? NULL : nserv;
+  }
+
+  /* new news server */
+  nserv = safe_calloc (1, sizeof (NNTP_SERVER));
+  nserv->conn = conn;
+  nserv->groups_hash = hash_create (1009, 0);
+  nserv->groups_max = 16;
+  nserv->groups_list = safe_malloc (nserv->groups_max * sizeof (nntp_data));
+
+  rc = nntp_open_connection (nserv);
+
+  /* try to create cache directory and enable caching */
+  nserv->cacheable = 0;
+  if (rc >= 0 && NewsCacheDir && *NewsCacheDir)
+  {
+    cache_expand (file, sizeof (file), &conn->account, NULL);
+    p = *file == '/' ? file + 1 : file;
+    while (1)
+    {
+      p = strchr (p, '/');
+      if (p)
+	*p = '\0';
+      if ((stat (file, &sb) || (sb.st_mode & S_IFDIR) == 0) &&
+	  mkdir (file, 0700))
+      {
+	mutt_error (_("Can't create %s: %s."), file, strerror (errno));
+	mutt_sleep (2);
+	break;
+      }
+      if (!p)
+      {
+	nserv->cacheable = 1;
+	break;
+      }
+      *p++ = '/';
+    }
+  }
+
+  /* load .newsrc */
+  if (rc >= 0)
+  {
+    mutt_FormatString (file, sizeof (file), 0, NONULL (NewsRc),
+		       nntp_format_str, (unsigned long)nserv, 0);
+    mutt_expand_path (file, sizeof (file));
+    nserv->newsrc_file = safe_strdup (file);
+    rc = nntp_newsrc_parse (nserv);
+  }
+  if (rc >= 0)
+  {
+    /* try to load list of newsgroups from cache */
+    if (nserv->cacheable && active_get_cache (nserv) == 0)
+      rc = nntp_check_new_groups (nserv);
+
+    /* load list of newsgroups from server */
+    else
+      rc = nntp_active_fetch (nserv);
+  }
+
+  if (rc >= 0)
+    nntp_clear_cache (nserv);
+
+#ifdef USE_HCACHE
+  /* check cache files */
+  if (rc >= 0 && nserv->cacheable)
+  {
+    struct dirent *entry;
+    DIR *dp = opendir (file);
+
+    if (dp)
+    {
+      while ((entry = readdir (dp)))
+      {
+	header_cache_t *hc;
+	void *hdata;
+	char *group = entry->d_name;
+
+	p = group + strlen (group) - 7;
+	if (strlen (group) < 8 || strcmp (p, ".hcache"))
+	  continue;
+	*p = '\0';
+	nntp_data = hash_find (nserv->groups_hash, group);
+	if (!nntp_data)
+	  continue;
+
+	hc = nntp_hcache_open (nntp_data);
+	if (!hc)
+	  continue;
+
+	/* fetch previous values of first and last */
+	hdata = mutt_hcache_fetch_raw (hc, "index", strlen);
+	if (hdata)
+	{
+	  anum_t first, last;
+
+	  if (sscanf (hdata, ANUM " " ANUM, &first, &last) == 2)
+	  {
+	    if (nntp_data->deleted)
+	    {
+	      nntp_data->firstMessage = first;
+	      nntp_data->lastMessage = last;
+	    }
+	    if (last >= nntp_data->firstMessage &&
+		last <= nntp_data->lastMessage)
+	    {
+	      nntp_data->lastCached = last;
+	      dprint (2, (debugfile, "nntp_select_server: %s lastCached=%u\n",
+			  nntp_data->group, last));
+	    }
+	  }
+	}
+	mutt_hcache_close (hc);
+      }
+      closedir (dp);
+    }
+  }
+#endif
+
+  if (rc < 0 || !leave_lock)
+    nntp_newsrc_close (nserv);
+
+  if (rc < 0)
+  {
+    hash_destroy (&nserv->groups_hash, nntp_data_free);
+    FREE (&nserv->groups_list);
+    FREE (&nserv->newsrc_file);
+    FREE (&nserv->authenticators);
+    FREE (&nserv);
+    mutt_socket_close (conn);
+    mutt_socket_free (conn);
+    return NULL;
+  }
+
+  conn->data = nserv;
+  return nserv;
+}
+
+/* Full status flags are not supported by nntp, but we can fake some of them:
+ * Read = a read message number is in the .newsrc
+ * New = not read and not cached
+ * Old = not read but cached */
+void nntp_article_status (CONTEXT *ctx, HEADER *hdr, char *group, anum_t anum)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  unsigned int i;
+
+  if (group)
+    nntp_data = hash_find (nntp_data->nserv->groups_hash, group);
+
+  if (!nntp_data)
+    return;
+
+  for (i = 0; i < nntp_data->newsrc_len; i++)
+  {
+    if ((anum >= nntp_data->newsrc_ent[i].first) &&
+	(anum <= nntp_data->newsrc_ent[i].last))
+    {
+      /* can't use mutt_set_flag() because mx_update_context()
+	 didn't called yet */
+      hdr->read = 1;
+      return;
+    }
+  }
+
+  /* article was not cached yet, it's new */
+  if (anum > nntp_data->lastCached)
+    return;
+
+  /* article isn't read but cached, it's old */
+  if (option (OPTMARKOLD))
+    hdr->old = 1;
+}
+
+/* calculate number of unread articles using .newsrc data */
+void nntp_group_unread_stat (NNTP_DATA *nntp_data)
+{
+  unsigned int i;
+  anum_t first, last;
+
+  nntp_data->unread = 0;
+  if (nntp_data->lastMessage == 0 ||
+      nntp_data->firstMessage > nntp_data->lastMessage)
+    return;
+
+  nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
+  for (i = 0; i < nntp_data->newsrc_len; i++)
+  {
+    first = nntp_data->newsrc_ent[i].first;
+    if (first < nntp_data->firstMessage)
+      first = nntp_data->firstMessage;
+    last = nntp_data->newsrc_ent[i].last;
+    if (last > nntp_data->lastMessage)
+      last = nntp_data->lastMessage;
+    if (first <= last)
+      nntp_data->unread -= last - first + 1;
+  }
+}
+
+/* Subscribe newsgroup */
+NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER *nserv, char *group)
+{
+  NNTP_DATA *nntp_data;
+
+  if (!nserv || !nserv->groups_hash || !group || !*group)
+    return NULL;
+
+  nntp_data = nntp_data_find (nserv, group);
+  nntp_data->subscribed = 1;
+  if (!nntp_data->newsrc_ent)
+  {
+    nntp_data->newsrc_ent = safe_calloc (1, sizeof (NEWSRC_ENTRY));
+    nntp_data->newsrc_len = 1;
+    nntp_data->newsrc_ent[0].first = 1;
+    nntp_data->newsrc_ent[0].last = 0;
+  }
+  return nntp_data;
+}
+
+/* Unsubscribe newsgroup */
+NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER *nserv, char *group)
+{
+  NNTP_DATA *nntp_data;
+
+  if (!nserv || !nserv->groups_hash || !group || !*group)
+    return NULL;
+
+  nntp_data = hash_find (nserv->groups_hash, group);
+  if (!nntp_data)
+    return NULL;
+
+  nntp_data->subscribed = 0;
+  if (!option (OPTSAVEUNSUB))
+  {
+    nntp_data->newsrc_len = 0;
+    FREE (&nntp_data->newsrc_ent);
+  }
+  return nntp_data;
+}
+
+/* Catchup newsgroup */
+NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER *nserv, char *group)
+{
+  NNTP_DATA *nntp_data;
+
+  if (!nserv || !nserv->groups_hash || !group || !*group)
+    return NULL;
+
+  nntp_data = hash_find (nserv->groups_hash, group);
+  if (!nntp_data)
+    return NULL;
+
+  if (nntp_data->newsrc_ent)
+  {
+    safe_realloc (&nntp_data->newsrc_ent, sizeof (NEWSRC_ENTRY));
+    nntp_data->newsrc_len = 1;
+    nntp_data->newsrc_ent[0].first = 1;
+    nntp_data->newsrc_ent[0].last = nntp_data->lastMessage;
+  }
+  nntp_data->unread = 0;
+  if (Context && Context->data == nntp_data)
+  {
+    unsigned int i;
+
+    for (i = 0; i < Context->msgcount; i++)
+      mutt_set_flag (Context, Context->hdrs[i], M_READ, 1);
+  }
+  return nntp_data;
+}
+
+/* Uncatchup newsgroup */
+NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER *nserv, char *group)
+{
+  NNTP_DATA *nntp_data;
+
+  if (!nserv || !nserv->groups_hash || !group || !*group)
+    return NULL;
+
+  nntp_data = hash_find (nserv->groups_hash, group);
+  if (!nntp_data)
+    return NULL;
+
+  if (nntp_data->newsrc_ent)
+  {
+    safe_realloc (&nntp_data->newsrc_ent, sizeof (NEWSRC_ENTRY));
+    nntp_data->newsrc_len = 1;
+    nntp_data->newsrc_ent[0].first = 1;
+    nntp_data->newsrc_ent[0].last = nntp_data->firstMessage - 1;
+  }
+  if (Context && Context->data == nntp_data)
+  {
+    unsigned int i;
+
+    nntp_data->unread = Context->msgcount;
+    for (i = 0; i < Context->msgcount; i++)
+      mutt_set_flag (Context, Context->hdrs[i], M_READ, 0);
+  }
+  else
+    nntp_data->unread = nntp_data->lastMessage - nntp_data->newsrc_ent[0].last;
+  return nntp_data;
+}
+
+/* Get first newsgroup with new messages */
+void nntp_buffy (char *buf, size_t len)
+{
+  unsigned int i;
+
+  for (i = 0; i < CurrentNewsSrv->groups_num; i++)
+  {
+    NNTP_DATA *nntp_data = CurrentNewsSrv->groups_list[i];
+
+    if (!nntp_data || !nntp_data->subscribed || !nntp_data->unread)
+      continue;
+
+    if (Context && Context->magic == M_NNTP &&
+	!mutt_strcmp (nntp_data->group, ((NNTP_DATA *)Context->data)->group))
+    {
+      unsigned int i, unread = 0;
+
+      for (i = 0; i < Context->msgcount; i++)
+	if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
+	  unread++;
+      if (!unread)
+	continue;
+    }
+    strfcpy (buf, nntp_data->group, len);
+    break;
+  }
+}
diff --git a/nntp.c b/nntp.c
new file mode 100644
index 0000000..c78d3fa
--- /dev/null
+++ b/nntp.c
@@ -0,0 +1,2404 @@
+/*
+ * Copyright (C) 1998 Brandon Long <blong@fiction.net>
+ * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
+ * Copyright (C) 2000-2012 Vsevolod Volkov <vvv@mutt.org.ua>
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "mutt.h"
+#include "mutt_curses.h"
+#include "sort.h"
+#include "mx.h"
+#include "mime.h"
+#include "rfc1524.h"
+#include "rfc2047.h"
+#include "mailbox.h"
+#include "mutt_crypt.h"
+#include "nntp.h"
+
+#if defined(USE_SSL)
+#include "mutt_ssl.h"
+#endif
+
+#ifdef HAVE_PGP
+#include "pgp.h"
+#endif
+
+#ifdef HAVE_SMIME
+#include "smime.h"
+#endif
+
+#if USE_HCACHE
+#include "hcache.h"
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include <stdlib.h>
+
+#ifdef USE_SASL
+#include <sasl/sasl.h>
+#include <sasl/saslutil.h>
+
+#include "mutt_sasl.h"
+#endif
+
+static int nntp_connect_error (NNTP_SERVER *nserv)
+{
+  nserv->status = NNTP_NONE;
+  mutt_error _("Server closed connection!");
+  mutt_sleep (2);
+  return -1;
+}
+
+/* Get capabilities:
+ * -1 - error, connection is closed
+ *  0 - mode is reader, capabilities setted up
+ *  1 - need to switch to reader mode */
+static int nntp_capabilities (NNTP_SERVER *nserv)
+{
+  CONNECTION *conn = nserv->conn;
+  unsigned int mode_reader = 0;
+  char buf[LONG_STRING];
+  char authinfo[LONG_STRING] = "";
+
+  nserv->hasCAPABILITIES = 0;
+  nserv->hasSTARTTLS = 0;
+  nserv->hasDATE = 0;
+  nserv->hasLIST_NEWSGROUPS = 0;
+  nserv->hasLISTGROUP = 0;
+  nserv->hasOVER = 0;
+  FREE (&nserv->authenticators);
+
+  if (mutt_socket_write (conn, "CAPABILITIES\r\n") < 0 ||
+      mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+    return nntp_connect_error (nserv);
+
+  /* no capabilities */
+  if (mutt_strncmp ("101", buf, 3))
+    return 1;
+  nserv->hasCAPABILITIES = 1;
+
+  /* parse capabilities */
+  do
+  {
+    if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (!mutt_strcmp ("STARTTLS", buf))
+      nserv->hasSTARTTLS = 1;
+    else if (!mutt_strcmp ("MODE-READER", buf))
+      mode_reader = 1;
+    else if (!mutt_strcmp ("READER", buf))
+    {
+      nserv->hasDATE = 1;
+      nserv->hasLISTGROUP = 1;
+    }
+    else if (!mutt_strncmp ("AUTHINFO ", buf, 9))
+    {
+      safe_strcat (buf, sizeof (buf), " ");
+      strfcpy (authinfo, buf + 8, sizeof (authinfo));
+    }
+#ifdef USE_SASL
+    else if (!mutt_strncmp ("SASL ", buf, 5))
+    {
+      char *p = buf + 5;
+      while (*p == ' ')
+	p++;
+      nserv->authenticators = safe_strdup (p);
+    }
+#endif
+    else if (!mutt_strcmp ("OVER", buf))
+      nserv->hasOVER = 1;
+    else if (!mutt_strncmp ("LIST ", buf, 5))
+    {
+      char *p = strstr (buf, " NEWSGROUPS");
+      if (p)
+      {
+	p += 11;
+	if (*p == '\0' || *p == ' ')
+	  nserv->hasLIST_NEWSGROUPS = 1;
+      }
+    }
+  } while (mutt_strcmp (".", buf));
+  *buf = '\0';
+#ifdef USE_SASL
+  if (nserv->authenticators && strcasestr (authinfo, " SASL "))
+    strfcpy (buf, nserv->authenticators, sizeof (buf));
+#endif
+  if (strcasestr (authinfo, " USER "))
+  {
+    if (*buf)
+      safe_strcat (buf, sizeof (buf), " ");
+    safe_strcat (buf, sizeof (buf), "USER");
+  }
+  mutt_str_replace (&nserv->authenticators, buf);
+
+  /* current mode is reader */
+  if (nserv->hasLISTGROUP)
+    return 0;
+
+  /* server is mode-switching, need to switch to reader mode */
+  if (mode_reader)
+    return 1;
+
+  mutt_socket_close (conn);
+  nserv->status = NNTP_BYE;
+  mutt_error _("Server doesn't support reader mode.");
+  mutt_sleep (2);
+  return -1;
+}
+
+char *OverviewFmt =
+	"Subject:\0"
+	"From:\0"
+	"Date:\0"
+	"Message-ID:\0"
+	"References:\0"
+	"Content-Length:\0"
+	"Lines:\0"
+	"\0";
+
+/* Detect supported commands */
+static int nntp_attempt_features (NNTP_SERVER *nserv)
+{
+  CONNECTION *conn = nserv->conn;
+  char buf[LONG_STRING];
+
+  /* no CAPABILITIES, trying DATE, LISTGROUP, LIST NEWSGROUPS */
+  if (!nserv->hasCAPABILITIES)
+  {
+    if (mutt_socket_write (conn, "DATE\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("500", buf, 3))
+      nserv->hasDATE = 1;
+
+    if (mutt_socket_write (conn, "LISTGROUP\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("500", buf, 3))
+      nserv->hasLISTGROUP = 1;
+
+    if (mutt_socket_write (conn, "LIST NEWSGROUPS +\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("500", buf, 3))
+      nserv->hasLIST_NEWSGROUPS = 1;
+    if (!mutt_strncmp ("215", buf, 3))
+    {
+      do
+      {
+	if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+	  return nntp_connect_error (nserv);
+      } while (mutt_strcmp (".", buf));
+    }
+  }
+
+  /* no LIST NEWSGROUPS, trying XGTITLE */
+  if (!nserv->hasLIST_NEWSGROUPS)
+  {
+    if (mutt_socket_write (conn, "XGTITLE\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("500", buf, 3))
+      nserv->hasXGTITLE = 1;
+  }
+
+  /* no OVER, trying XOVER */
+  if (!nserv->hasOVER)
+  {
+    if (mutt_socket_write (conn, "XOVER\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("500", buf, 3))
+      nserv->hasXOVER = 1;
+  }
+
+  /* trying LIST OVERVIEW.FMT */
+  if (nserv->hasOVER || nserv->hasXOVER)
+  {
+    if (mutt_socket_write (conn, "LIST OVERVIEW.FMT\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("215", buf, 3))
+      nserv->overview_fmt = OverviewFmt;
+    else
+    {
+      int chunk, cont = 0;
+      size_t buflen = 2 * LONG_STRING, off = 0, b = 0;
+
+      if (nserv->overview_fmt)
+	FREE (&nserv->overview_fmt);
+      nserv->overview_fmt = safe_malloc (buflen);
+
+      while (1)
+      {
+	if (buflen - off < LONG_STRING)
+	{
+	  buflen *= 2;
+	  safe_realloc (&nserv->overview_fmt, buflen);
+	}
+
+	chunk = mutt_socket_readln (nserv->overview_fmt + off,
+				    buflen - off, conn);
+	if (chunk < 0)
+	{
+	  FREE (&nserv->overview_fmt);
+	  return nntp_connect_error (nserv);
+	}
+
+	if (!cont && !mutt_strcmp (".", nserv->overview_fmt + off))
+	  break;
+
+	cont = chunk >= buflen - off ? 1 : 0;
+	off += strlen (nserv->overview_fmt + off);
+	if (!cont)
+	{
+	  char *colon;
+
+	  if (nserv->overview_fmt[b] == ':')
+	  {
+	    memmove (nserv->overview_fmt + b,
+		     nserv->overview_fmt + b + 1, off - b - 1);
+	    nserv->overview_fmt[off - 1] = ':';
+	  }
+	  colon = strchr (nserv->overview_fmt + b, ':');
+	  if (!colon)
+	    nserv->overview_fmt[off++] = ':';
+	  else if (strcmp (colon + 1, "full"))
+	    off = colon + 1 - nserv->overview_fmt;
+	  if (!strcasecmp (nserv->overview_fmt + b, "Bytes:"))
+	  {
+	    strcpy (nserv->overview_fmt + b, "Content-Length:");
+	    off = b + strlen (nserv->overview_fmt + b);
+	  }
+	  nserv->overview_fmt[off++] = '\0';
+	  b = off;
+	}
+      }
+      nserv->overview_fmt[off++] = '\0';
+      safe_realloc (&nserv->overview_fmt, off);
+    }
+  }
+  return 0;
+}
+
+/* Get login, password and authenticate */
+static int nntp_auth (NNTP_SERVER *nserv)
+{
+  CONNECTION *conn = nserv->conn;
+  char buf[LONG_STRING];
+  char authenticators[LONG_STRING] = "USER";
+  char *method, *a, *p;
+  unsigned char flags = conn->account.flags;
+
+  while (1)
+  {
+    /* get login and password */
+    if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
+	mutt_account_getpass (&conn->account) || !conn->account.pass[0])
+      break;
+
+    /* get list of authenticators */
+    if (NntpAuthenticators && *NntpAuthenticators)
+      strfcpy (authenticators, NntpAuthenticators, sizeof (authenticators));
+    else if (nserv->hasCAPABILITIES)
+    {
+      strfcpy (authenticators, NONULL (nserv->authenticators),
+	       sizeof (authenticators));
+      p = authenticators;
+      while (*p)
+      {
+	if (*p == ' ')
+	  *p = ':';
+	p++;
+      }
+    }
+    p = authenticators;
+    while (*p)
+    {
+      *p = ascii_toupper (*p);
+      p++;
+    }
+
+    dprint (1, (debugfile,
+		"nntp_auth: available methods: %s\n", nserv->authenticators));
+    a = authenticators;
+    while (1)
+    {
+      if (!a)
+      {
+	mutt_error _("No authenticators available");
+	mutt_sleep (2);
+	break;
+      }
+
+      method = a;
+      a = strchr (a, ':');
+      if (a)
+	*a++ = '\0';
+
+      /* check authenticator */
+      if (nserv->hasCAPABILITIES)
+      {
+	char *m;
+
+	if (!nserv->authenticators)
+	  continue;
+	m = strcasestr (nserv->authenticators, method);
+	if (!m)
+	  continue;
+	if (m > nserv->authenticators && *(m - 1) != ' ')
+	  continue;
+	m += strlen (method);
+	if (*m != '\0' && *m != ' ')
+	  continue;
+      }
+      dprint (1, (debugfile, "nntp_auth: trying method %s\n", method));
+
+      /* AUTHINFO USER authentication */
+      if (!strcmp (method, "USER"))
+      {
+	mutt_message (_("Authenticating (%s)..."), method);
+	snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
+	if (mutt_socket_write (conn, buf) < 0 ||
+	    mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+	  break;
+
+	/* authenticated, password is not required */
+	if (!mutt_strncmp ("281", buf, 3))
+	  return 0;
+
+	/* username accepted, sending password */
+	if (!mutt_strncmp ("381", buf, 3))
+	{
+#ifdef DEBUG
+	  if (debuglevel < M_SOCK_LOG_FULL)
+	    dprint (M_SOCK_LOG_CMD, (debugfile,
+		    "%d> AUTHINFO PASS *\n", conn->fd));
+#endif
+	  snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n",
+		    conn->account.pass);
+	  if (mutt_socket_write_d (conn, buf, -1, M_SOCK_LOG_FULL) < 0 ||
+	      mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+	  break;
+
+	  /* authenticated */
+	  if (!mutt_strncmp ("281", buf, 3))
+	    return 0;
+	}
+
+	/* server doesn't support AUTHINFO USER, trying next method */
+	if (*buf == '5')
+	  continue;
+      }
+
+      else
+      {
+#ifdef USE_SASL
+	sasl_conn_t *saslconn;
+	sasl_interact_t *interaction = NULL;
+	int rc;
+	char inbuf[LONG_STRING] = "";
+	const char *mech;
+	const char *client_out = NULL;
+	unsigned int client_len, len;
+
+	if (mutt_sasl_client_new (conn, &saslconn) < 0)
+	{
+	  dprint (1, (debugfile,
+		  "nntp_auth: error allocating SASL connection.\n"));
+	  continue;
+	}
+
+	while (1)
+	{
+	  rc = sasl_client_start (saslconn, method, &interaction,
+				  &client_out, &client_len, &mech);
+	  if (rc != SASL_INTERACT)
+	    break;
+	  mutt_sasl_interact (interaction);
+	}
+	if (rc != SASL_OK && rc != SASL_CONTINUE)
+	{
+	  sasl_dispose (&saslconn);
+	  dprint (1, (debugfile,
+		  "nntp_auth: error starting SASL authentication exchange.\n"));
+	  continue;
+	}
+
+	mutt_message (_("Authenticating (%s)..."), method);
+	snprintf (buf, sizeof (buf), "AUTHINFO SASL %s", method);
+
+	/* looping protocol */
+	while (rc == SASL_CONTINUE || (rc == SASL_OK && client_len))
+	{
+	  /* send out client response */
+	  if (client_len)
+	  {
+#ifdef DEBUG
+	    if (debuglevel >= M_SOCK_LOG_FULL)
+	    {
+	      char tmp[LONG_STRING];
+	      memcpy (tmp, client_out, client_len);
+	      for (p = tmp; p < tmp + client_len; p++)
+	      {
+		if (*p == '\0')
+		  *p = '.';
+	      }
+	      *p = '\0';
+	      dprint (1, (debugfile, "SASL> %s\n", tmp));
+	    }
+#endif
+
+	    if (*buf)
+	      safe_strcat (buf, sizeof (buf), " ");
+	    len = strlen (buf);
+	    if (sasl_encode64 (client_out, client_len,
+		buf + len, sizeof (buf) - len, &len) != SASL_OK)
+	    {
+	      dprint (1, (debugfile,
+		      "nntp_auth: error base64-encoding client response.\n"));
+	      break;
+	    }
+	  }
+
+	  safe_strcat (buf, sizeof (buf), "\r\n");
+#ifdef DEBUG
+	  if (debuglevel < M_SOCK_LOG_FULL)
+	  {
+	    if (strchr (buf, ' '))
+	      dprint (M_SOCK_LOG_CMD, (debugfile, "%d> AUTHINFO SASL %s%s\n",
+		      conn->fd, method, client_len ? " sasl_data" : ""));
+	    else
+	      dprint (M_SOCK_LOG_CMD, (debugfile, "%d> sasl_data\n", conn->fd));
+	  }
+#endif
+	  client_len = 0;
+	  if (mutt_socket_write_d (conn, buf, -1, M_SOCK_LOG_FULL) < 0 ||
+	      mutt_socket_readln_d (inbuf, sizeof (inbuf), conn, M_SOCK_LOG_FULL) < 0)
+	    break;
+	  if (mutt_strncmp (inbuf, "283 ", 4) &&
+	      mutt_strncmp (inbuf, "383 ", 4))
+	  {
+#ifdef DEBUG
+	    if (debuglevel < M_SOCK_LOG_FULL)
+	      dprint (M_SOCK_LOG_CMD, (debugfile, "%d< %s\n", conn->fd, inbuf));
+#endif
+	    break;
+	  }
+#ifdef DEBUG
+	  if (debuglevel < M_SOCK_LOG_FULL)
+	  {
+	    inbuf[3] = '\0';
+	    dprint (M_SOCK_LOG_CMD, (debugfile,
+		    "%d< %s sasl_data\n", conn->fd, inbuf));
+	  }
+#endif
+
+	  if (!strcmp ("=", inbuf + 4))
+	    len = 0;
+	  else if (sasl_decode64 (inbuf + 4, strlen (inbuf + 4),
+		   buf, sizeof (buf) - 1, &len) != SASL_OK)
+	  {
+	    dprint (1, (debugfile,
+		    "nntp_auth: error base64-decoding server response.\n"));
+	    break;
+	  }
+#ifdef DEBUG
+	  else if (debuglevel >= M_SOCK_LOG_FULL)
+	  {
+	    char tmp[LONG_STRING];
+	    memcpy (tmp, buf, len);
+	    for (p = tmp; p < tmp + len; p++)
+	    {
+	      if (*p == '\0')
+		*p = '.';
+	    }
+	    *p = '\0';
+	    dprint (1, (debugfile, "SASL< %s\n", tmp));
+	  }
+#endif
+
+	  while (1)
+	  {
+	    rc = sasl_client_step (saslconn, buf, len,
+				   &interaction, &client_out, &client_len);
+	    if (rc != SASL_INTERACT)
+	      break;
+	    mutt_sasl_interact (interaction);
+	  }
+	  if (*inbuf != '3')
+	    break;
+
+	  *buf = '\0';
+	} /* looping protocol */
+
+	if (rc == SASL_OK && client_len == 0 && *inbuf == '2')
+	{
+	  mutt_sasl_setup_conn (conn, saslconn);
+	  return 0;
+	}
+
+	/* terminate SASL sessoin */
+	sasl_dispose (&saslconn);
+	if (conn->fd < 0)
+	  break;
+	if (!mutt_strncmp (inbuf, "383 ", 4))
+	{
+	  if (mutt_socket_write (conn, "*\r\n") < 0 ||
+	      mutt_socket_readln (inbuf, sizeof (inbuf), conn) < 0)
+	    break;
+	}
+
+	/* server doesn't support AUTHINFO SASL, trying next method */
+	if (*inbuf == '5')
+	  continue;
+#else
+	continue;
+#endif /* USE_SASL */
+      }
+
+      mutt_error (_("%s authentication failed."), method);
+      mutt_sleep (2);
+      break;
+    }
+    break;
+  }
+
+  /* error */
+  nserv->status = NNTP_BYE;
+  conn->account.flags = flags;
+  if (conn->fd < 0)
+  {
+    mutt_error _("Server closed connection!");
+    mutt_sleep (2);
+  }
+  else
+    mutt_socket_close (conn);
+  return -1;
+}
+
+/* Connect to server, authenticate and get capabilities */
+int nntp_open_connection (NNTP_SERVER *nserv)
+{
+  CONNECTION *conn = nserv->conn;
+  char buf[STRING];
+  int cap;
+  unsigned int posting = 0, auth = 1;
+
+  if (nserv->status == NNTP_OK)
+    return 0;
+  if (nserv->status == NNTP_BYE)
+    return -1;
+  nserv->status = NNTP_NONE;
+
+  if (mutt_socket_open (conn) < 0)
+    return -1;
+
+  if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+    return nntp_connect_error (nserv);
+
+  if (!mutt_strncmp ("200", buf, 3))
+    posting = 1;
+  else if (mutt_strncmp ("201", buf, 3))
+  {
+    mutt_socket_close (conn);
+    mutt_remove_trailing_ws (buf);
+    mutt_error ("%s", buf);
+    mutt_sleep (2);
+    return -1;
+  }
+
+  /* get initial capabilities */
+  cap = nntp_capabilities (nserv);
+  if (cap < 0)
+    return -1;
+
+  /* tell news server to switch to mode reader if it isn't so */
+  if (cap > 0)
+  {
+    if (mutt_socket_write (conn, "MODE READER\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+
+    if (!mutt_strncmp ("200", buf, 3))
+      posting = 1;
+    else if (!mutt_strncmp ("201", buf, 3))
+      posting = 0;
+    /* error if has capabilities, ignore result if no capabilities */
+    else if (nserv->hasCAPABILITIES)
+    {
+      mutt_socket_close (conn);
+      mutt_error _("Could not switch to reader mode.");
+      mutt_sleep (2);
+      return -1;
+    }
+
+    /* recheck capabilities after MODE READER */
+    if (nserv->hasCAPABILITIES)
+    {
+      cap = nntp_capabilities (nserv);
+      if (cap < 0)
+	return -1;
+    }
+  }
+
+  mutt_message (_("Connected to %s. %s"), conn->account.host,
+		posting ? _("Posting is ok.") : _("Posting is NOT ok."));
+  mutt_sleep (1);
+
+#if defined(USE_SSL)
+  /* Attempt STARTTLS if available and desired. */
+  if (nserv->use_tls != 1 && (nserv->hasSTARTTLS || option (OPTSSLFORCETLS)))
+  {
+    if (nserv->use_tls == 0)
+      nserv->use_tls = option (OPTSSLFORCETLS) ||
+			query_quadoption (OPT_SSLSTARTTLS,
+			_("Secure connection with TLS?")) == M_YES ? 2 : 1;
+    if (nserv->use_tls == 2)
+    {
+      if (mutt_socket_write (conn, "STARTTLS\r\n") < 0 ||
+	  mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+	return nntp_connect_error (nserv);
+      if (mutt_strncmp ("382", buf, 3))
+      {
+	nserv->use_tls = 0;
+	mutt_error ("STARTTLS: %s", buf);
+	mutt_sleep (2);
+      }
+      else if (mutt_ssl_starttls (conn))
+      {
+	nserv->use_tls = 0;
+	nserv->status = NNTP_NONE;
+	mutt_socket_close (nserv->conn);
+	mutt_error _("Could not negotiate TLS connection");
+	mutt_sleep (2);
+	return -1;
+      }
+      else
+      {
+	/* recheck capabilities after STARTTLS */
+	cap = nntp_capabilities (nserv);
+	if (cap < 0)
+	  return -1;
+      }
+    }
+  }
+#endif
+
+  /* authentication required? */
+  if (conn->account.flags & M_ACCT_USER)
+  {
+    if (!conn->account.user[0])
+      auth = 0;
+  }
+  else
+  {
+    if (mutt_socket_write (conn, "STAT\r\n") < 0 ||
+	mutt_socket_readln (buf, sizeof (buf), conn) < 0)
+      return nntp_connect_error (nserv);
+    if (mutt_strncmp ("480", buf, 3))
+      auth = 0;
+  }
+
+  /* authenticate */
+  if (auth && nntp_auth (nserv) < 0)
+      return -1;
+
+  /* get final capabilities after authentication */
+  if (nserv->hasCAPABILITIES && (auth || cap > 0))
+  {
+    cap = nntp_capabilities (nserv);
+    if (cap < 0)
+      return -1;
+    if (cap > 0)
+    {
+      mutt_socket_close (conn);
+      mutt_error _("Could not switch to reader mode.");
+      mutt_sleep (2);
+      return -1;
+    }
+  }
+
+  /* attempt features */
+  if (nntp_attempt_features (nserv) < 0)
+    return -1;
+
+  nserv->status = NNTP_OK;
+  return 0;
+}
+
+/* Send data from buffer and receive answer to same buffer */
+static int nntp_query (NNTP_DATA *nntp_data, char *line, size_t linelen)
+{
+  NNTP_SERVER *nserv = nntp_data->nserv;
+  char buf[LONG_STRING];
+
+  if (nserv->status == NNTP_BYE)
+    return -1;
+
+  while (1)
+  {
+    if (nserv->status == NNTP_OK)
+    {
+      int rc = 0;
+
+      if (*line)
+	rc = mutt_socket_write (nserv->conn, line);
+      else if (nntp_data->group)
+      {
+	snprintf (buf, sizeof (buf), "GROUP %s\r\n", nntp_data->group);
+	rc = mutt_socket_write (nserv->conn, buf);
+      }
+      if (rc >= 0)
+	rc = mutt_socket_readln (buf, sizeof (buf), nserv->conn);
+      if (rc >= 0)
+	break;
+    }
+
+    /* reconnect */
+    while (1)
+    {
+      nserv->status = NNTP_NONE;
+      if (nntp_open_connection (nserv) == 0)
+	break;
+
+      snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
+		nserv->conn->account.host);
+      if (mutt_yesorno (buf, M_YES) != M_YES)
+      {
+	nserv->status = NNTP_BYE;
+	return -1;
+      }
+    }
+
+    /* select newsgroup after reconnection */
+    if (nntp_data->group)
+    {
+      snprintf (buf, sizeof (buf), "GROUP %s\r\n", nntp_data->group);
+      if (mutt_socket_write (nserv->conn, buf) < 0 ||
+	  mutt_socket_readln (buf, sizeof (buf), nserv->conn) < 0)
+	return nntp_connect_error (nserv);
+    }
+    if (!*line)
+      break;
+  }
+
+  strfcpy (line, buf, linelen);
+  return 0;
+}
+
+/* This function calls funct(*line, *data) for each received line,
+ * funct(NULL, *data) if rewind(*data) needs, exits when fail or done:
+ *  0 - success
+ *  1 - bad response (answer in query buffer)
+ * -1 - conection lost
+ * -2 - error in funct(*line, *data) */
+static int nntp_fetch_lines (NNTP_DATA *nntp_data, char *query, size_t qlen,
+			char *msg, int (*funct) (char *, void *), void *data)
+{
+  int done = FALSE;
+  int rc;
+
+  while (!done)
+  {
+    char buf[LONG_STRING];
+    char *line;
+    unsigned int lines = 0;
+    size_t off = 0;
+    progress_t progress;
+
+    if (msg)
+      mutt_progress_init (&progress, msg, M_PROGRESS_MSG, ReadInc, -1);
+
+    strfcpy (buf, query, sizeof (buf));
+    if (nntp_query (nntp_data, buf, sizeof (buf)) < 0)
+      return -1;
+    if (buf[0] != '2')
+    {
+      strfcpy (query, buf, qlen);
+      return 1;
+    }
+
+    line = safe_malloc (sizeof (buf));
+    rc = 0;
+
+    while (1)
+    {
+      char *p;
+      int chunk = mutt_socket_readln_d (buf, sizeof (buf),
+		  nntp_data->nserv->conn, M_SOCK_LOG_HDR);
+      if (chunk < 0)
+      {
+	nntp_data->nserv->status = NNTP_NONE;
+	break;
+      }
+
+      p = buf;
+      if (!off && buf[0] == '.')
+      {
+	if (buf[1] == '\0')
+	{
+	  done = TRUE;
+	  break;
+	}
+	if (buf[1] == '.')
+	  p++;
+      }
+
+      strfcpy (line + off, p, sizeof (buf));
+
+      if (chunk >= sizeof (buf))
+	off += strlen (p);
+      else
+      {
+	if (msg)
+	  mutt_progress_update (&progress, ++lines, -1);
+
+	if (rc == 0 && funct (line, data) < 0)
+	  rc = -2;
+	off = 0;
+      }
+
+      safe_realloc (&line, off + sizeof (buf));
+    }
+    FREE (&line);
+    funct (NULL, data);
+  }
+  return rc;
+}
+
+/* Parse newsgroup description */
+static int fetch_description (char *line, void *data)
+{
+  NNTP_SERVER *nserv = data;
+  NNTP_DATA *nntp_data;
+  char *desc;
+
+  if (!line)
+    return 0;
+
+  desc = strpbrk (line, " \t");
+  if (desc)
+  {
+    *desc++ = '\0';
+    desc += strspn (desc, " \t");
+  }
+  else
+    desc = strchr (line, '\0');
+
+  nntp_data = hash_find (nserv->groups_hash, line);
+  if (nntp_data && mutt_strcmp (desc, nntp_data->desc))
+  {
+    mutt_str_replace (&nntp_data->desc, desc);
+    dprint (2, (debugfile, "group: %s, desc: %s\n", line, desc));
+  }
+  return 0;
+}
+
+/* Fetch newsgroups descriptions.
+ * Returns the same code as nntp_fetch_lines() */
+static int get_description (NNTP_DATA *nntp_data, char *wildmat, char *msg)
+{
+  NNTP_SERVER *nserv;
+  char buf[STRING];
+  char *cmd;
+  int rc;
+
+  /* get newsgroup description, if possible */
+  nserv = nntp_data->nserv;
+  if (!wildmat)
+    wildmat = nntp_data->group;
+  if (nserv->hasLIST_NEWSGROUPS)
+    cmd = "LIST NEWSGROUPS";
+  else if (nserv->hasXGTITLE)
+    cmd = "XGTITLE";
+  else
+    return 0;
+
+  snprintf (buf, sizeof (buf), "%s %s\r\n", cmd, wildmat);
+  rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), msg,
+			 fetch_description, nserv);
+  if (rc > 0)
+  {
+    mutt_error ("%s: %s", cmd, buf);
+    mutt_sleep (2);
+  }
+  return rc;
+}
+
+/* Update read flag and set article number if empty */
+static void nntp_parse_xref (CONTEXT *ctx, HEADER *hdr)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  char *buf, *p;
+
+  buf = p = safe_strdup (hdr->env->xref);
+  while (p)
+  {
+    char *grp, *colon;
+    anum_t anum;
+
+    /* skip to next word */
+    p += strspn (p, " \t");
+    grp = p;
+
+    /* skip to end of word */
+    p = strpbrk (p, " \t");
+    if (p)
+      *p++ = '\0';
+
+    /* find colon */
+    colon = strchr (grp, ':');
+    if (!colon)
+      continue;
+    *colon++ = '\0';
+    if (sscanf (colon, ANUM, &anum) != 1)
+      continue;
+
+    nntp_article_status (ctx, hdr, grp, anum);
+    if (hdr && !NHDR (hdr)->article_num && !mutt_strcmp (nntp_data->group, grp))
+      NHDR (hdr)->article_num = anum;
+  }
+  FREE (&buf);
+}
+
+/* Write line to temporarily file */
+static int fetch_tempfile (char *line, void *data)
+{
+  FILE *fp = data;
+
+  if (!line)
+    rewind (fp);
+  else if (fputs (line, fp) == EOF || fputc ('\n', fp) == EOF)
+    return -1;
+  return 0;
+}
+
+typedef struct
+{
+  CONTEXT *ctx;
+  anum_t first;
+  anum_t last;
+  int restore;
+  unsigned char *messages;
+  progress_t progress;
+#ifdef USE_HCACHE
+  header_cache_t *hc;
+#endif
+} FETCH_CTX;
+
+/* Parse article number */
+static int fetch_numbers (char *line, void *data)
+{
+  FETCH_CTX *fc = data;
+  anum_t anum;
+
+  if (!line)
+    return 0;
+  if (sscanf (line, ANUM, &anum) != 1)
+    return 0;
+  if (anum < fc->first || anum > fc->last)
+    return 0;
+  fc->messages[anum - fc->first] = 1;
+  return 0;
+}
+
+/* Parse overview line */
+static int parse_overview_line (char *line, void *data)
+{
+  FETCH_CTX *fc = data;
+  CONTEXT *ctx = fc->ctx;
+  NNTP_DATA *nntp_data = ctx->data;
+  HEADER *hdr;
+  FILE *fp;
+  char tempfile[_POSIX_PATH_MAX];
+  char *header, *field;
+  int save = 1;
+  anum_t anum;
+
+  if (!line)
+    return 0;
+
+  /* parse article number */
+  field = strchr (line, '\t');
+  if (field)
+    *field++ = '\0';
+  if (sscanf (line, ANUM, &anum) != 1)
+    return 0;
+  dprint (2, (debugfile, "parse_overview_line: " ANUM "\n", anum));
+
+  /* out of bounds */
+  if (anum < fc->first || anum > fc->last)
+    return 0;
+
+  /* not in LISTGROUP */
+  if (!fc->messages[anum - fc->first])
+  {
+    /* progress */
+    if (!ctx->quiet)
+      mutt_progress_update (&fc->progress, anum - fc->first + 1, -1);
+    return 0;
+  }
+
+  /* convert overview line to header */
+  mutt_mktemp (tempfile, sizeof (tempfile));
+  fp = safe_fopen (tempfile, "w+");
+  if (!fp)
+    return -1;
+
+  header = nntp_data->nserv->overview_fmt;
+  while (field)
+  {
+    char *b = field;
+
+    if (*header)
+    {
+      if (strstr (header, ":full") == NULL && fputs (header, fp) == EOF)
+      {
+	fclose (fp);
+	unlink (tempfile);
+	return -1;
+      }
+      header = strchr (header, '\0') + 1;
+    }
+
+    field = strchr (field, '\t');
+    if (field)
+      *field++ = '\0';
+    if (fputs (b, fp) == EOF || fputc ('\n', fp) == EOF)
+    {
+      fclose (fp);
+      unlink (tempfile);
+      return -1;
+    }
+  }
+  rewind (fp);
+
+  /* allocate memory for headers */
+  if (ctx->msgcount >= ctx->hdrmax)
+    mx_alloc_memory (ctx);
+
+  /* parse header */
+  hdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
+  hdr->env = mutt_read_rfc822_header (fp, hdr, 0, 0);
+  hdr->env->newsgroups = safe_strdup (nntp_data->group);
+  hdr->received = hdr->date_sent;
+  fclose (fp);
+  unlink (tempfile);
+
+#ifdef USE_HCACHE
+  if (fc->hc)
+  {
+    void *hdata;
+    char buf[16];
+
+    /* try to replace with header from cache */
+    snprintf (buf, sizeof (buf), "%d", anum);
+    hdata = mutt_hcache_fetch (fc->hc, buf, strlen);
+    if (hdata)
+    {
+      dprint (2, (debugfile,
+		  "parse_overview_line: mutt_hcache_fetch %s\n", buf));
+      mutt_free_header (&hdr);
+      ctx->hdrs[ctx->msgcount] =
+      hdr = mutt_hcache_restore (hdata, NULL);
+      hdr->read = 0;
+      hdr->old = 0;
+
+      /* skip header marked as deleted in cache */
+      if (hdr->deleted && !fc->restore)
+      {
+	if (nntp_data->bcache)
+	{
+	  dprint (2, (debugfile,
+		      "parse_overview_line: mutt_bcache_del %s\n", buf));
+	  mutt_bcache_del (nntp_data->bcache, buf);
+	}
+	save = 0;
+      }
+    }
+
+    /* not chached yet, store header */
+    else
+    {
+      dprint (2, (debugfile,
+		  "parse_overview_line: mutt_hcache_store %s\n", buf));
+      mutt_hcache_store (fc->hc, buf, hdr, 0, strlen, M_GENERATE_UIDVALIDITY);
+    }
+  }
+#endif
+
+  if (save)
+  {
+    hdr->index = ctx->msgcount++;
+    hdr->read = 0;
+    hdr->old = 0;
+    hdr->deleted = 0;
+    hdr->data = safe_calloc (1, sizeof (NNTP_HEADER_DATA));
+    NHDR (hdr)->article_num = anum;
+    if (fc->restore)
+      hdr->changed = 1;
+    else
+    {
+      nntp_article_status (ctx, hdr, NULL, anum);
+      if (!hdr->read)
+	nntp_parse_xref (ctx, hdr);
+    }
+    if (anum > nntp_data->lastLoaded)
+      nntp_data->lastLoaded = anum;
+  }
+  else
+    mutt_free_header (&hdr);
+
+  /* progress */
+  if (!ctx->quiet)
+    mutt_progress_update (&fc->progress, anum - fc->first + 1, -1);
+  return 0;
+}
+
+/* Fetch headers */
+static int nntp_fetch_headers (CONTEXT *ctx, void *hc,
+			       anum_t first, anum_t last, int restore)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  FETCH_CTX fc;
+  HEADER *hdr;
+  char buf[HUGE_STRING];
+  int rc = 0;
+  int oldmsgcount = ctx->msgcount;
+  anum_t current;
+#ifdef USE_HCACHE
+  void *hdata;
+#endif
+
+  /* if empty group or nothing to do */
+  if (!last || first > last)
+    return 0;
+
+  /* init fetch context */
+  fc.ctx = ctx;
+  fc.first = first;
+  fc.last = last;
+  fc.restore = restore;
+  fc.messages = safe_calloc (last - first + 1, sizeof (unsigned char));
+#ifdef USE_HCACHE
+  fc.hc = hc;
+#endif
+
+  /* fetch list of articles */
+  if (nntp_data->nserv->hasLISTGROUP && !nntp_data->deleted)
+  {
+    if (!ctx->quiet)
+      mutt_message _("Fetching list of articles...");
+    snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
+    rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), NULL,
+			   fetch_numbers, &fc);
+    if (rc > 0)
+    {
+      mutt_error ("LISTGROUP: %s", buf);
+      mutt_sleep (2);
+    }
+    if (rc == 0)
+    {
+      for (current = first; current <= last && rc == 0; current++)
+      {
+	if (fc.messages[current - first])
+	  continue;
+
+	snprintf (buf, sizeof (buf), "%d", current);
+	if (nntp_data->bcache)
+	{
+	  dprint (2, (debugfile,
+		      "nntp_fetch_headers: mutt_bcache_del %s\n", buf));
+	  mutt_bcache_del (nntp_data->bcache, buf);
+	}
+
+#ifdef USE_HCACHE
+	if (fc.hc)
+	{
+	  dprint (2, (debugfile,
+		      "nntp_fetch_headers: mutt_hcache_delete %s\n", buf));
+	  mutt_hcache_delete (fc.hc, buf, strlen);
+	}
+#endif
+      }
+    }
+  }
+  else
+    for (current = first; current <= last; current++)
+      fc.messages[current - first] = 1;
+
+  /* fetching header from cache or server, or fallback to fetch overview */
+  if (!ctx->quiet)
+    mutt_progress_init (&fc.progress, _("Fetching message headers..."),
+			M_PROGRESS_MSG, ReadInc, last - first + 1);
+  for (current = first; current <= last && rc == 0; current++)
+  {
+    if (!ctx->quiet)
+      mutt_progress_update (&fc.progress, current - first + 1, -1);
+
+#ifdef USE_HCACHE
+    snprintf (buf, sizeof (buf), "%d", current);
+#endif
+
+    /* delete header from cache that does not exist on server */
+    if (!fc.messages[current - first])
+      continue;
+
+    /* allocate memory for headers */
+    if (ctx->msgcount >= ctx->hdrmax)
+      mx_alloc_memory (ctx);
+
+#ifdef USE_HCACHE
+    /* try to fetch header from cache */
+    hdata = mutt_hcache_fetch (fc.hc, buf, strlen);
+    if (hdata)
+    {
+      dprint (2, (debugfile,
+		  "nntp_fetch_headers: mutt_hcache_fetch %s\n", buf));
+      ctx->hdrs[ctx->msgcount] =
+      hdr = mutt_hcache_restore (hdata, NULL);
+
+      /* skip header marked as deleted in cache */
+      if (hdr->deleted && !restore)
+      {
+	mutt_free_header (&hdr);
+	if (nntp_data->bcache)
+	{
+	  dprint (2, (debugfile,
+		      "nntp_fetch_headers: mutt_bcache_del %s\n", buf));
+	  mutt_bcache_del (nntp_data->bcache, buf);
+	}
+	continue;
+      }
+
+      hdr->read = 0;
+      hdr->old = 0;
+    }
+    else
+#endif
+
+    /* don't try to fetch header from removed newsgroup */
+    if (nntp_data->deleted)
+      continue;
+
+    /* fallback to fetch overview */
+    else if (nntp_data->nserv->hasOVER || nntp_data->nserv->hasXOVER)
+      break;
+
+    /* fetch header from server */
+    else
+    {
+      FILE *fp;
+      char tempfile[_POSIX_PATH_MAX];
+
+      mutt_mktemp (tempfile, sizeof (tempfile));
+      fp = safe_fopen (tempfile, "w+");
+      if (!fp)
+      {
+	mutt_perror (tempfile);
+	mutt_sleep (2);
+	unlink (tempfile);
+	rc = -1;
+	break;
+      }
+
+      snprintf (buf, sizeof (buf), "HEAD %d\r\n", current);
+      rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), NULL,
+			     fetch_tempfile, fp);
+      if (rc)
+      {
+	fclose (fp);
+	unlink (tempfile);
+	if (rc < 0)
+	  break;
+
+	/* invalid response */
+	if (mutt_strncmp ("423", buf, 3))
+	{
+	  mutt_error ("HEAD: %s", buf);
+	  mutt_sleep (2);
+	  break;
+	}
+
+	/* no such article */
+	if (nntp_data->bcache)
+	{
+	  snprintf (buf, sizeof (buf), "%d", current);
+	  dprint (2, (debugfile,
+		      "nntp_fetch_headers: mutt_bcache_del %s\n", buf));
+	  mutt_bcache_del (nntp_data->bcache, buf);
+	}
+	rc = 0;
+	continue;
+      }
+
+      /* parse header */
+      hdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
+      hdr->env = mutt_read_rfc822_header (fp, hdr, 0, 0);
+      hdr->received = hdr->date_sent;
+      fclose (fp);
+      unlink (tempfile);
+    }
+
+    /* save header in context */
+    hdr->index = ctx->msgcount++;
+    hdr->read = 0;
+    hdr->old = 0;
+    hdr->deleted = 0;
+    hdr->data = safe_calloc (1, sizeof (NNTP_HEADER_DATA));
+    NHDR (hdr)->article_num = current;
+    if (restore)
+      hdr->changed = 1;
+    else
+    {
+      nntp_article_status (ctx, hdr, NULL, NHDR (hdr)->article_num);
+      if (!hdr->read)
+	nntp_parse_xref (ctx, hdr);
+    }
+    if (current > nntp_data->lastLoaded)
+      nntp_data->lastLoaded = current;
+  }
+
+  /* fetch overview information */
+  if (current <= last && rc == 0) {
+    char *cmd = nntp_data->nserv->hasOVER ? "OVER" : "XOVER";
+    snprintf (buf, sizeof (buf), "%s %d-%d\r\n", cmd, current, last);
+    rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), NULL,
+	 parse_overview_line, &fc);
+    if (rc > 0)
+    {
+      mutt_error ("%s: %s", cmd, buf);
+      mutt_sleep (2);
+    }
+  }
+
+  if (ctx->msgcount > oldmsgcount)
+    mx_update_context (ctx, ctx->msgcount - oldmsgcount);
+
+  FREE (&fc.messages);
+  if (rc != 0)
+    return -1;
+  mutt_clear_error ();
+  return 0;
+}
+
+/* Open newsgroup */
+int nntp_open_mailbox (CONTEXT *ctx)
+{
+  NNTP_SERVER *nserv;
+  NNTP_DATA *nntp_data;
+  char buf[HUGE_STRING];
+  char server[LONG_STRING];
+  char *group;
+  int rc;
+  void *hc = NULL;
+  anum_t first, last, count = 0;
+  ciss_url_t url;
+
+  strfcpy (buf, ctx->path, sizeof (buf));
+  if (url_parse_ciss (&url, buf) < 0 || !url.path ||
+     !(url.scheme == U_NNTP || url.scheme == U_NNTPS))
+  {
+    mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
+    mutt_sleep (2);
+    return -1;
+  }
+
+  group = url.path;
+  url.path = strchr (url.path, '\0');
+  url_ciss_tostring (&url, server, sizeof (server), 0);
+  nserv = nntp_select_server (server, 1);
+  if (!nserv)
+    return -1;
+  CurrentNewsSrv = nserv;
+
+  /* find news group data structure */
+  nntp_data = hash_find (nserv->groups_hash, group);
+  if (!nntp_data)
+  {
+    nntp_newsrc_close (nserv);
+    mutt_error (_("Newsgroup %s not found on the server."), group);
+    mutt_sleep (2);
+    return -1;
+  }
+
+  mutt_bit_unset (ctx->rights, M_ACL_INSERT);
+  if (!nntp_data->newsrc_ent && !nntp_data->subscribed &&
+      !option (OPTSAVEUNSUB))
+    ctx->readonly = 1;
+
+  /* select newsgroup */
+  mutt_message (_("Selecting %s..."), group);
+  buf[0] = '\0';
+  if (nntp_query (nntp_data, buf, sizeof (buf)) < 0)
+  {
+    nntp_newsrc_close (nserv);
+    return -1;
+  }
+
+  /* newsgroup not found, remove it */
+  if (!mutt_strncmp ("411", buf, 3))
+  {
+    mutt_error (_("Newsgroup %s has been removed from the server."),
+		nntp_data->group);
+    if (!nntp_data->deleted)
+    {
+      nntp_data->deleted = 1;
+      nntp_active_save_cache (nserv);
+    }
+    if (nntp_data->newsrc_ent && !nntp_data->subscribed &&
+	!option (OPTSAVEUNSUB))
+    {
+      FREE (&nntp_data->newsrc_ent);
+      nntp_data->newsrc_len = 0;
+      nntp_delete_group_cache (nntp_data);
+      nntp_newsrc_update (nserv);
+    }
+    mutt_sleep (2);
+  }
+
+  /* parse newsgroup info */
+  else {
+    if (sscanf (buf, "211 " ANUM " " ANUM " " ANUM, &count, &first, &last) != 3)
+    {
+      nntp_newsrc_close (nserv);
+      mutt_error ("GROUP: %s", buf);
+      mutt_sleep (2);
+      return -1;
+    }
+    nntp_data->firstMessage = first;
+    nntp_data->lastMessage = last;
+    nntp_data->deleted = 0;
+
+    /* get description if empty */
+    if (option (OPTLOADDESC) && !nntp_data->desc)
+    {
+      if (get_description (nntp_data, NULL, NULL) < 0)
+      {
+	nntp_newsrc_close (nserv);
+	return -1;
+      }
+      if (nntp_data->desc)
+	nntp_active_save_cache (nserv);
+    }
+  }
+
+  time (&nserv->check_time);
+  ctx->data = nntp_data;
+  ctx->mx_close = nntp_fastclose_mailbox;
+  if (!nntp_data->bcache && (nntp_data->newsrc_ent ||
+      nntp_data->subscribed || option (OPTSAVEUNSUB)))
+    nntp_data->bcache = mutt_bcache_open (&nserv->conn->account,
+			nntp_data->group);
+
+  /* strip off extra articles if adding context is greater than $nntp_context */
+  first = nntp_data->firstMessage;
+  if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
+    first = nntp_data->lastMessage - NntpContext + 1;
+  nntp_data->lastLoaded = first ? first - 1 : 0;
+  count = nntp_data->firstMessage;
+  nntp_data->firstMessage = first;
+  nntp_bcache_update (nntp_data);
+  nntp_data->firstMessage = count;
+#ifdef USE_HCACHE
+  hc = nntp_hcache_open (nntp_data);
+  nntp_hcache_update (nntp_data, hc);
+#endif
+  if (!hc)
+  {
+    mutt_bit_unset (ctx->rights, M_ACL_WRITE);
+    mutt_bit_unset (ctx->rights, M_ACL_DELETE);
+  }
+  nntp_newsrc_close (nserv);
+  rc = nntp_fetch_headers (ctx, hc, first, nntp_data->lastMessage, 0);
+#ifdef USE_HCACHE
+  mutt_hcache_close (hc);
+#endif
+  if (rc < 0)
+    return -1;
+  nntp_data->lastLoaded = nntp_data->lastMessage;
+  nserv->newsrc_modified = 0;
+  return 0;
+}
+
+/* Fetch message */
+int nntp_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  NNTP_ACACHE *acache;
+  HEADER *hdr = ctx->hdrs[msgno];
+  char buf[_POSIX_PATH_MAX];
+  char article[16];
+  char *fetch_msg = _("Fetching message...");
+  int rc;
+
+  /* try to get article from cache */
+  acache = &nntp_data->acache[hdr->index % NNTP_ACACHE_LEN];
+  if (acache->path)
+  {
+    if (acache->index == hdr->index)
+    {
+      msg->fp = fopen (acache->path, "r");
+      if (msg->fp)
+	return 0;
+    }
+    /* clear previous entry */
+    else
+    {
+      unlink (acache->path);
+      FREE (&acache->path);
+    }
+  }
+  snprintf (article, sizeof (article), "%d", NHDR (hdr)->article_num);
+  msg->fp = mutt_bcache_get (nntp_data->bcache, article);
+  if (msg->fp)
+  {
+    if (NHDR (hdr)->parsed)
+      return 0;
+  }
+  else
+  {
+    /* don't try to fetch article from removed newsgroup */
+    if (nntp_data->deleted)
+      return -1;
+
+    /* create new cache file */
+    mutt_message (fetch_msg);
+    msg->fp = mutt_bcache_put (nntp_data->bcache, article, 1);
+    if (!msg->fp)
+    {
+      mutt_mktemp (buf, sizeof (buf));
+      acache->path = safe_strdup (buf);
+      acache->index = hdr->index;
+      msg->fp = safe_fopen (acache->path, "w+");
+      if (!msg->fp)
+      {
+	mutt_perror (acache->path);
+	unlink (acache->path);
+	FREE (&acache->path);
+	return -1;
+      }
+    }
+
+    /* fetch message to cache file */
+    snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
+	      NHDR (hdr)->article_num ? article : hdr->env->message_id);
+    rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), fetch_msg,
+			   fetch_tempfile, msg->fp);
+    if (rc)
+    {
+      safe_fclose (&msg->fp);
+      if (acache->path)
+      {
+	unlink (acache->path);
+	FREE (&acache->path);
+      }
+      if (rc > 0)
+      {
+	if (!mutt_strncmp (NHDR (hdr)->article_num ? "423" : "430", buf, 3))
+	  mutt_error (_("Article %d not found on the server."),
+		      NHDR (hdr)->article_num ? article : hdr->env->message_id);
+	else
+	  mutt_error ("ARTICLE: %s", buf);
+      }
+      return -1;
+    }
+
+    if (!acache->path)
+      mutt_bcache_commit (nntp_data->bcache, article);
+  }
+
+  /* replace envelope with new one
+   * hash elements must be updated because pointers will be changed */
+  if (ctx->id_hash && hdr->env->message_id)
+    hash_delete (ctx->id_hash, hdr->env->message_id, hdr, NULL);
+  if (ctx->subj_hash && hdr->env->real_subj)
+    hash_delete (ctx->subj_hash, hdr->env->real_subj, hdr, NULL);
+
+  mutt_free_envelope (&hdr->env);
+  hdr->env = mutt_read_rfc822_header (msg->fp, hdr, 0, 0);
+
+  if (ctx->id_hash && hdr->env->message_id)
+    hash_insert (ctx->id_hash, hdr->env->message_id, hdr, 0);
+  if (ctx->subj_hash && hdr->env->real_subj)
+    hash_insert (ctx->subj_hash, hdr->env->real_subj, hdr, 1);
+
+  /* fix content length */
+  fseek (msg->fp, 0, SEEK_END);
+  hdr->content->length = ftell (msg->fp) - hdr->content->offset;
+
+  /* this is called in mutt before the open which fetches the message,
+   * which is probably wrong, but we just call it again here to handle
+   * the problem instead of fixing it */
+  NHDR (hdr)->parsed = 1;
+  mutt_parse_mime_message (ctx, hdr);
+
+  /* these would normally be updated in mx_update_context(), but the
+   * full headers aren't parsed with overview, so the information wasn't
+   * available then */
+  if (WithCrypto)
+    hdr->security = crypt_query (hdr->content);
+
+  rewind (msg->fp);
+  mutt_clear_error();
+  return 0;
+}
+
+/* Post article */
+int nntp_post (const char *msg) {
+  NNTP_DATA *nntp_data, nntp_tmp;
+  FILE *fp;
+  char buf[LONG_STRING];
+  size_t len;
+
+  if (Context && Context->magic == M_NNTP)
+    nntp_data = Context->data;
+  else
+  {
+    CurrentNewsSrv = nntp_select_server (NewsServer, 0);
+    if (!CurrentNewsSrv)
+      return -1;
+
+    nntp_data = &nntp_tmp;
+    nntp_data->nserv = CurrentNewsSrv;
+    nntp_data->group = NULL;
+  }
+
+  fp = safe_fopen (msg, "r");
+  if (!fp)
+  {
+    mutt_perror (msg);
+    return -1;
+  }
+
+  strfcpy (buf, "POST\r\n", sizeof (buf));
+  if (nntp_query (nntp_data, buf, sizeof (buf)) < 0)
+    return -1;
+  if (buf[0] != '3')
+  {
+    mutt_error (_("Can't post article: %s"), buf);
+    return -1;
+  }
+
+  buf[0] = '.';
+  buf[1] = '\0';
+  while (fgets (buf + 1, sizeof (buf) - 2, fp))
+  {
+    len = strlen (buf);
+    if (buf[len - 1] == '\n')
+    {
+      buf[len - 1] = '\r';
+      buf[len] = '\n';
+      len++;
+      buf[len] = '\0';
+    }
+    if (mutt_socket_write_d (nntp_data->nserv->conn,
+	buf[1] == '.' ? buf : buf + 1, -1, M_SOCK_LOG_HDR) < 0)
+      return nntp_connect_error (nntp_data->nserv);
+  }
+  fclose (fp);
+
+  if ((buf[strlen (buf) - 1] != '\n' &&
+      mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", -1, M_SOCK_LOG_HDR) < 0) ||
+      mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", -1, M_SOCK_LOG_HDR) < 0 ||
+      mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0)
+    return nntp_connect_error (nntp_data->nserv);
+  if (buf[0] != '2')
+  {
+    mutt_error (_("Can't post article: %s"), buf);
+    return -1;
+  }
+  return 0;
+}
+
+/* Save changes to .newsrc and cache */
+int nntp_sync_mailbox (CONTEXT *ctx)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  int rc, i;
+#ifdef USE_HCACHE
+  header_cache_t *hc;
+#endif
+
+  /* check for new articles */
+  nntp_data->nserv->check_time = 0;
+  rc = nntp_check_mailbox (ctx, 1);
+  if (rc)
+    return rc;
+
+#ifdef USE_HCACHE
+  nntp_data->lastCached = 0;
+  hc = nntp_hcache_open (nntp_data);
+#endif
+
+  nntp_data->unread = ctx->unread;
+  for (i = 0; i < ctx->msgcount; i++)
+  {
+    HEADER *hdr = ctx->hdrs[i];
+    char buf[16];
+
+    snprintf (buf, sizeof (buf), "%d", NHDR (hdr)->article_num);
+    if (nntp_data->bcache && hdr->deleted)
+    {
+      dprint (2, (debugfile, "nntp_sync_mailbox: mutt_bcache_del %s\n", buf));
+      mutt_bcache_del (nntp_data->bcache, buf);
+    }
+
+#ifdef USE_HCACHE
+    if (hc && (hdr->changed || hdr->deleted))
+    {
+      if (hdr->deleted && !hdr->read)
+	nntp_data->unread--;
+      dprint (2, (debugfile, "nntp_sync_mailbox: mutt_hcache_store %s\n", buf));
+      mutt_hcache_store (hc, buf, hdr, 0, strlen, M_GENERATE_UIDVALIDITY);
+    }
+#endif
+  }
+
+#ifdef USE_HCACHE
+  if (hc)
+  {
+    mutt_hcache_close (hc);
+    nntp_data->lastCached = nntp_data->lastLoaded;
+  }
+#endif
+
+  /* save .newsrc entries */
+  nntp_newsrc_gen_entries (ctx);
+  nntp_newsrc_update (nntp_data->nserv);
+  nntp_newsrc_close (nntp_data->nserv);
+  return 0;
+}
+
+/* Free up memory associated with the newsgroup context */
+int nntp_fastclose_mailbox (CONTEXT *ctx)
+{
+  NNTP_DATA *nntp_data = ctx->data, *nntp_tmp;
+
+  if (!nntp_data)
+    return 0;
+
+  nntp_acache_free (nntp_data);
+  if (!nntp_data->nserv || !nntp_data->nserv->groups_hash || !nntp_data->group)
+    return 0;
+
+  nntp_tmp = hash_find (nntp_data->nserv->groups_hash, nntp_data->group);
+  if (nntp_tmp == NULL || nntp_tmp != nntp_data)
+    nntp_data_free (nntp_data);
+  return 0;
+}
+
+/* Get date and time from server */
+int nntp_date (NNTP_SERVER *nserv, time_t *now)
+{
+  if (nserv->hasDATE)
+  {
+    NNTP_DATA nntp_data;
+    char buf[LONG_STRING];
+    struct tm tm;
+
+    nntp_data.nserv = nserv;
+    nntp_data.group = NULL;
+    strfcpy (buf, "DATE\r\n", sizeof (buf));
+    if (nntp_query (&nntp_data, buf, sizeof (buf)) < 0)
+      return -1;
+
+    if (sscanf (buf, "111 %4d%2d%2d%2d%2d%2d%*s", &tm.tm_year, &tm.tm_mon,
+		&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6)
+    {
+      tm.tm_year -= 1900;
+      tm.tm_mon--;
+      *now = timegm (&tm);
+      if (*now >= 0)
+      {
+	dprint (1, (debugfile, "nntp_date: server time is %d\n", *now));
+	return 0;
+      }
+    }
+  }
+  time (now);
+  return 0;
+}
+
+/* Fetch list of all newsgroups from server */
+int nntp_active_fetch (NNTP_SERVER *nserv)
+{
+  NNTP_DATA nntp_data;
+  char msg[SHORT_STRING];
+  char buf[LONG_STRING];
+  unsigned int i;
+  int rc;
+
+  snprintf (msg, sizeof (msg), _("Loading list of groups from server %s..."),
+	    nserv->conn->account.host);
+  mutt_message (msg);
+  if (nntp_date (nserv, &nserv->newgroups_time) < 0)
+    return -1;
+
+  nntp_data.nserv = nserv;
+  nntp_data.group = NULL;
+  strfcpy (buf, "LIST\r\n", sizeof (buf));
+  rc = nntp_fetch_lines (&nntp_data, buf, sizeof (buf), msg,
+			 nntp_add_group, nserv);
+  if (rc)
+  {
+    if (rc > 0)
+    {
+      mutt_error ("LIST: %s", buf);
+      mutt_sleep (2);
+    }
+    return -1;
+  }
+
+  if (option (OPTLOADDESC) &&
+      get_description (&nntp_data, "*", _("Loading descriptions...")) < 0)
+    return -1;
+
+  for (i = 0; i < nserv->groups_num; i++)
+  {
+    NNTP_DATA *nntp_data = nserv->groups_list[i];
+
+    if (nntp_data && nntp_data->deleted && !nntp_data->newsrc_ent)
+    {
+      nntp_delete_group_cache (nntp_data);
+      hash_delete (nserv->groups_hash, nntp_data->group, NULL, nntp_data_free);
+      nserv->groups_list[i] = NULL;
+    }
+  }
+  nntp_active_save_cache (nserv);
+  mutt_clear_error ();
+  return 0;
+}
+
+/* Check newsgroup for new articles:
+ *  1 - new articles found
+ *  0 - no change
+ * -1 - lost connection */
+static int nntp_group_poll (NNTP_DATA *nntp_data, int update_stat)
+{
+  char buf[LONG_STRING] = "";
+  anum_t count, first, last;
+
+  /* use GROUP command to poll newsgroup */
+  if (nntp_query (nntp_data, buf, sizeof (buf)) < 0)
+    return -1;
+  if (sscanf (buf, "211 " ANUM " " ANUM " " ANUM, &count, &first, &last) != 3)
+    return 0;
+  if (first == nntp_data->firstMessage && last == nntp_data->lastMessage)
+    return 0;
+
+  /* articles have been renumbered */
+  if (last < nntp_data->lastMessage)
+  {
+    nntp_data->lastCached = 0;
+    if (nntp_data->newsrc_len)
+    {
+      safe_realloc (&nntp_data->newsrc_ent, sizeof (NEWSRC_ENTRY));
+      nntp_data->newsrc_len = 1;
+      nntp_data->newsrc_ent[0].first = 1;
+      nntp_data->newsrc_ent[0].last = 0;
+    }
+  }
+  nntp_data->firstMessage = first;
+  nntp_data->lastMessage = last;
+  if (!update_stat)
+    return 1;
+
+  /* update counters */
+  else if (!last || (!nntp_data->newsrc_ent && !nntp_data->lastCached))
+    nntp_data->unread = count;
+  else
+    nntp_group_unread_stat (nntp_data);
+  return 1;
+}
+
+/* Check current newsgroup for new articles:
+ *  M_REOPENED	- articles have been renumbered or removed from server
+ *  M_NEW_MAIL	- new articles found
+ *  0		- no change
+ * -1		- lost connection */
+int nntp_check_mailbox (CONTEXT *ctx, int leave_lock)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  NNTP_SERVER *nserv = nntp_data->nserv;
+  time_t now = time (NULL);
+  int i, j;
+  int rc, ret = 0;
+  void *hc = NULL;
+
+  if (nserv->check_time + NewsPollTimeout > now)
+    return 0;
+
+  mutt_message _("Checking for new messages...");
+  if (nntp_newsrc_parse (nserv) < 0)
+    return -1;
+
+  nserv->check_time = now;
+  rc = nntp_group_poll (nntp_data, 0);
+  if (rc < 0)
+  {
+    nntp_newsrc_close (nserv);
+    return -1;
+  }
+  if (rc)
+    nntp_active_save_cache (nserv);
+
+  /* articles have been renumbered, remove all headers */
+  if (nntp_data->lastMessage < nntp_data->lastLoaded)
+  {
+    for (i = 0; i < ctx->msgcount; i++)
+      mutt_free_header (&ctx->hdrs[i]);
+    ctx->msgcount = 0;
+    ctx->tagged = 0;
+
+    if (nntp_data->lastMessage < nntp_data->lastLoaded)
+    {
+      nntp_data->lastLoaded = nntp_data->firstMessage - 1;
+      if (NntpContext && nntp_data->lastMessage - nntp_data->lastLoaded >
+	  NntpContext)
+	nntp_data->lastLoaded = nntp_data->lastMessage - NntpContext;
+    }
+    ret = M_REOPENED;
+  }
+
+  /* .newsrc has been externally modified */
+  if (nserv->newsrc_modified)
+  {
+    anum_t anum;
+#ifdef USE_HCACHE
+    unsigned char *messages;
+    char buf[16];
+    void *hdata;
+    HEADER *hdr;
+    anum_t first = nntp_data->firstMessage;
+
+    if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
+      first = nntp_data->lastMessage - NntpContext + 1;
+    messages = safe_calloc (nntp_data->lastLoaded - first + 1,
+			    sizeof (unsigned char));
+    hc = nntp_hcache_open (nntp_data);
+    nntp_hcache_update (nntp_data, hc);
+#endif
+
+    /* update flags according to .newsrc */
+    for (i = j = 0; i < ctx->msgcount; i++)
+    {
+      int flagged = 0;
+      anum = NHDR (ctx->hdrs[i])->article_num;
+
+#ifdef USE_HCACHE
+      /* check hcache for flagged and deleted flags */
+      if (hc)
+      {
+	if (anum >= first && anum <= nntp_data->lastLoaded)
+	  messages[anum - first] = 1;
+
+	snprintf (buf, sizeof (buf), "%d", anum);
+	hdata = mutt_hcache_fetch (hc, buf, strlen);
+	if (hdata)
+	{
+	  int deleted;
+
+	  dprint (2, (debugfile,
+		      "nntp_check_mailbox: mutt_hcache_fetch %s\n", buf));
+	  hdr = mutt_hcache_restore (hdata, NULL);
+	  deleted = hdr->deleted;
+	  flagged = hdr->flagged;
+	  mutt_free_header (&hdr);
+
+	  /* header marked as deleted, removing from context */
+	  if (deleted)
+	  {
+	    mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, 0);
+	    mutt_free_header (&ctx->hdrs[i]);
+	    continue;
+	  }
+	}
+      }
+#endif
+
+      if (!ctx->hdrs[i]->changed)
+      {
+	ctx->hdrs[i]->flagged = flagged;
+	ctx->hdrs[i]->read = 0;
+	ctx->hdrs[i]->old = 0;
+	nntp_article_status (ctx, ctx->hdrs[i], NULL, anum);
+	if (!ctx->hdrs[i]->read)
+	  nntp_parse_xref (ctx, ctx->hdrs[i]);
+      }
+      ctx->hdrs[j++] = ctx->hdrs[i];
+    }
+
+#ifdef USE_HCACHE
+    ctx->msgcount = j;
+
+    /* restore headers without "deleted" flag */
+    for (anum = first; anum <= nntp_data->lastLoaded; anum++)
+    {
+      if (messages[anum - first])
+	continue;
+
+      snprintf (buf, sizeof (buf), "%d", anum);
+      hdata = mutt_hcache_fetch (hc, buf, strlen);
+      if (hdata)
+      {
+	dprint (2, (debugfile,
+		    "nntp_check_mailbox: mutt_hcache_fetch %s\n", buf));
+	if (ctx->msgcount >= ctx->hdrmax)
+	  mx_alloc_memory (ctx);
+
+	ctx->hdrs[ctx->msgcount] =
+	hdr = mutt_hcache_restore (hdata, NULL);
+	if (hdr->deleted)
+	{
+	  mutt_free_header (&hdr);
+	  if (nntp_data->bcache)
+	  {
+	    dprint (2, (debugfile,
+			"nntp_check_mailbox: mutt_bcache_del %s\n", buf));
+	    mutt_bcache_del (nntp_data->bcache, buf);
+	  }
+	  continue;
+	}
+
+	ctx->msgcount++;
+	hdr->read = 0;
+	hdr->old = 0;
+	hdr->data = safe_calloc (1, sizeof (NNTP_HEADER_DATA));
+	NHDR (hdr)->article_num = anum;
+	nntp_article_status (ctx, hdr, NULL, anum);
+	if (!hdr->read)
+	  nntp_parse_xref (ctx, hdr);
+      }
+    }
+    FREE (&messages);
+#endif
+
+    nserv->newsrc_modified = 0;
+    ret = M_REOPENED;
+  }
+
+  /* some headers were removed, context must be updated */
+  if (ret == M_REOPENED)
+  {
+    if (ctx->subj_hash)
+      hash_destroy (&ctx->subj_hash, NULL);
+    if (ctx->id_hash)
+      hash_destroy (&ctx->id_hash, NULL);
+    mutt_clear_threads (ctx);
+
+    ctx->vcount = 0;
+    ctx->deleted = 0;
+    ctx->new = 0;
+    ctx->unread = 0;
+    ctx->flagged = 0;
+    ctx->changed = 0;
+    ctx->id_hash = NULL;
+    ctx->subj_hash = NULL;
+    mx_update_context (ctx, ctx->msgcount);
+  }
+
+  /* fetch headers of new articles */
+  if (nntp_data->lastMessage > nntp_data->lastLoaded)
+  {
+    int oldmsgcount = ctx->msgcount;
+    int quiet = ctx->quiet;
+    ctx->quiet = 1;
+#ifdef USE_HCACHE
+    if (!hc)
+    {
+      hc = nntp_hcache_open (nntp_data);
+      nntp_hcache_update (nntp_data, hc);
+    }
+#endif
+    rc = nntp_fetch_headers (ctx, hc, nntp_data->lastLoaded + 1,
+			     nntp_data->lastMessage, 0);
+    ctx->quiet = quiet;
+    if (rc >= 0)
+      nntp_data->lastLoaded = nntp_data->lastMessage;
+    if (ret == 0 && ctx->msgcount > oldmsgcount)
+      ret = M_NEW_MAIL;
+  }
+
+#ifdef USE_HCACHE
+  mutt_hcache_close (hc);
+#endif
+  if (ret || !leave_lock)
+    nntp_newsrc_close (nserv);
+  mutt_clear_error ();
+  return ret;
+}
+
+/* Check for new groups and new articles in subscribed groups:
+ *  1 - new groups found
+ *  0 - no new groups
+ * -1 - error */
+int nntp_check_new_groups (NNTP_SERVER *nserv)
+{
+  NNTP_DATA nntp_data;
+  time_t now;
+  struct tm *tm;
+  char buf[LONG_STRING];
+  char *msg = _("Checking for new newsgroups...");
+  unsigned int i;
+  int rc, update_active = FALSE;
+
+  if (!nserv || !nserv->newgroups_time)
+    return -1;
+
+  /* check subscribed newsgroups for new articles */
+  if (option (OPTSHOWNEWNEWS))
+  {
+    mutt_message _("Checking for new messages...");
+    for (i = 0; i < nserv->groups_num; i++)
+    {
+      NNTP_DATA *nntp_data = nserv->groups_list[i];
+
+      if (nntp_data && nntp_data->subscribed)
+      {
+	rc = nntp_group_poll (nntp_data, 1);
+	if (rc < 0)
+	  return -1;
+	if (rc > 0)
+	  update_active = TRUE;
+      }
+    }
+  }
+  else if (nserv->newgroups_time)
+    return 0;
+
+  /* get list of new groups */
+  mutt_message (msg);
+  if (nntp_date (nserv, &now) < 0)
+    return -1;
+  nntp_data.nserv = nserv;
+  if (Context && Context->magic == M_NNTP)
+    nntp_data.group = ((NNTP_DATA *)Context->data)->group;
+  else
+    nntp_data.group = NULL;
+  i = nserv->groups_num;
+  tm = gmtime (&nserv->newgroups_time);
+  snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
+	    tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
+	    tm->tm_hour, tm->tm_min, tm->tm_sec);
+  rc = nntp_fetch_lines (&nntp_data, buf, sizeof (buf), msg,
+			 nntp_add_group, nserv);
+  if (rc)
+  {
+    if (rc > 0)
+    {
+      mutt_error ("NEWGROUPS: %s", buf);
+      mutt_sleep (2);
+    }
+    return -1;
+  }
+
+  /* new groups found */
+  rc = 0;
+  if (nserv->groups_num != i)
+  {
+    nserv->newgroups_time = now;
+
+    /* loading descriptions */
+    if (option (OPTLOADDESC))
+    {
+      unsigned int count = 0;
+      progress_t progress;
+
+      mutt_progress_init (&progress, _("Loading descriptions..."),
+			  M_PROGRESS_MSG, ReadInc, nserv->groups_num - i);
+      for (; i < nserv->groups_num; i++)
+      {
+	NNTP_DATA *nntp_data = nserv->groups_list[i];
+
+	if (get_description (nntp_data, NULL, NULL) < 0)
+	  return -1;
+	mutt_progress_update (&progress, ++count, -1);
+      }
+    }
+    update_active = TRUE;
+    rc = 1;
+  }
+  if (update_active)
+    nntp_active_save_cache (nserv);
+  mutt_clear_error ();
+  return rc;
+}
+
+/* Fetch article by Message-ID:
+ *  0 - success
+ *  1 - no such article
+ * -1 - error */
+int nntp_check_msgid (CONTEXT *ctx, const char *msgid)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  HEADER *hdr;
+  FILE *fp;
+  char tempfile[_POSIX_PATH_MAX];
+  char buf[LONG_STRING];
+  int rc;
+
+  mutt_mktemp (tempfile, sizeof (tempfile));
+  fp = safe_fopen (tempfile, "w+");
+  if (!fp)
+  {
+    mutt_perror (tempfile);
+    unlink (tempfile);
+    return -1;
+  }
+
+  snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
+  rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), NULL,
+			 fetch_tempfile, fp);
+  if (rc)
+  {
+    fclose (fp);
+    unlink (tempfile);
+    if (rc < 0)
+      return -1;
+    if (!mutt_strncmp ("430", buf, 3))
+      return 1;
+    mutt_error ("HEAD: %s", buf);
+    return -1;
+  }
+
+  /* parse header */
+  if (ctx->msgcount == ctx->hdrmax)
+    mx_alloc_memory (ctx);
+  hdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
+  hdr->data = safe_calloc (1, sizeof (NNTP_HEADER_DATA));
+  hdr->env = mutt_read_rfc822_header (fp, hdr, 0, 0);
+  fclose (fp);
+  unlink (tempfile);
+
+  /* get article number */
+  if (hdr->env->xref)
+    nntp_parse_xref (ctx, hdr);
+  else
+  {
+    snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
+    if (nntp_query (nntp_data, buf, sizeof (buf)) < 0)
+    {
+      mutt_free_header (&hdr);
+      return -1;
+    }
+    sscanf (buf + 4, ANUM, &NHDR (hdr)->article_num);
+  }
+
+  /* reset flags */
+  hdr->read = 0;
+  hdr->old = 0;
+  hdr->deleted = 0;
+  hdr->changed = 1;
+  hdr->received = hdr->date_sent;
+  hdr->index = ctx->msgcount++;
+  mx_update_context (ctx, 1);
+  return 0;
+}
+
+typedef struct
+{
+  CONTEXT *ctx;
+  unsigned int num;
+  unsigned int max;
+  anum_t *child;
+} CHILD_CTX;
+
+/* Parse XPAT line */
+static int fetch_children (char *line, void *data)
+{
+  CHILD_CTX *cc = data;
+  anum_t anum;
+  unsigned int i;
+
+  if (!line || sscanf (line, ANUM, &anum) != 1)
+    return 0;
+  for (i = 0; i < cc->ctx->msgcount; i++)
+    if (NHDR (cc->ctx->hdrs[i])->article_num == anum)
+      return 0;
+  if (cc->num >= cc->max)
+  {
+    cc->max *= 2;
+    safe_realloc (&cc->child, sizeof (anum_t) * cc->max);
+  }
+  cc->child[cc->num++] = anum;
+  return 0;
+}
+
+/* Fetch children of article with the Message-ID */
+int nntp_check_children (CONTEXT *ctx, const char *msgid)
+{
+  NNTP_DATA *nntp_data = ctx->data;
+  CHILD_CTX cc;
+  char buf[STRING];
+  int i, rc, quiet;
+  void *hc = NULL;
+
+  if (!nntp_data || !nntp_data->nserv)
+    return -1;
+  if (nntp_data->firstMessage > nntp_data->lastLoaded)
+    return 0;
+
+  /* init context */
+  cc.ctx = ctx;
+  cc.num = 0;
+  cc.max = 10;
+  cc.child = safe_malloc (sizeof (anum_t) * cc.max);
+
+  /* fetch numbers of child messages */
+  snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
+	    nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
+  rc = nntp_fetch_lines (nntp_data, buf, sizeof (buf), NULL,
+			 fetch_children, &cc);
+  if (rc)
+  {
+    FREE (&cc.child);
+    if (rc > 0) {
+      if (mutt_strncmp ("500", buf, 3))
+	mutt_error ("XPAT: %s", buf);
+      else
+	mutt_error _("Unable to find child articles because server does not support XPAT command.");
+    }
+    return -1;
+  }
+
+  /* fetch all found messages */
+  quiet = ctx->quiet;
+  ctx->quiet = 1;
+#ifdef USE_HCACHE
+  hc = nntp_hcache_open (nntp_data);
+#endif
+  for (i = 0; i < cc.num; i++)
+  {
+    rc = nntp_fetch_headers (ctx, hc, cc.child[i], cc.child[i], 1);
+    if (rc < 0)
+      break;
+  }
+#ifdef USE_HCACHE
+  mutt_hcache_close (hc);
+#endif
+  ctx->quiet = quiet;
+  FREE (&cc.child);
+  return rc < 0 ? -1 : 0;
+}
diff --git a/nntp.h b/nntp.h
new file mode 100644
index 0000000..c937034
--- /dev/null
+++ b/nntp.h
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 1998 Brandon Long <blong@fiction.net>
+ * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
+ * Copyright (C) 2000-2012 Vsevolod Volkov <vvv@mutt.org.ua>
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ *
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef _NNTP_H_
+#define _NNTP_H_ 1
+
+#include "mutt_socket.h"
+#include "mailbox.h"
+#include "bcache.h"
+
+#if USE_HCACHE
+#include "hcache.h"
+#endif
+
+#include <time.h>
+#include <sys/types.h>
+#include <stdint.h>
+
+#define NNTP_PORT 119
+#define NNTP_SSL_PORT 563
+
+/* number of entries in article cache */
+#define NNTP_ACACHE_LEN 10
+
+/* article number type and format */
+#define anum_t uint32_t
+#define ANUM "%u"
+
+enum
+{
+  NNTP_NONE = 0,
+  NNTP_OK,
+  NNTP_BYE
+};
+
+typedef struct
+{
+  unsigned int hasCAPABILITIES : 1;
+  unsigned int hasSTARTTLS : 1;
+  unsigned int hasDATE : 1;
+  unsigned int hasLIST_NEWSGROUPS : 1;
+  unsigned int hasXGTITLE : 1;
+  unsigned int hasLISTGROUP : 1;
+  unsigned int hasOVER : 1;
+  unsigned int hasXOVER : 1;
+  unsigned int use_tls : 3;
+  unsigned int status : 3;
+  unsigned int cacheable : 1;
+  unsigned int newsrc_modified : 1;
+  FILE *newsrc_fp;
+  char *newsrc_file;
+  char *authenticators;
+  char *overview_fmt;
+  off_t size;
+  time_t mtime;
+  time_t newgroups_time;
+  time_t check_time;
+  unsigned int groups_num;
+  unsigned int groups_max;
+  void **groups_list;
+  HASH *groups_hash;
+  CONNECTION *conn;
+} NNTP_SERVER;
+
+typedef struct
+{
+  anum_t first;
+  anum_t last;
+} NEWSRC_ENTRY;
+
+typedef struct
+{
+  unsigned int index;
+  char *path;
+} NNTP_ACACHE;
+
+typedef struct
+{
+  char *group;
+  char *desc;
+  anum_t firstMessage;
+  anum_t lastMessage;
+  anum_t lastLoaded;
+  anum_t lastCached;
+  anum_t unread;
+  unsigned int subscribed : 1;
+  unsigned int new : 1;
+  unsigned int allowed : 1;
+  unsigned int deleted : 1;
+  unsigned int newsrc_len;
+  NEWSRC_ENTRY *newsrc_ent;
+  NNTP_SERVER *nserv;
+  NNTP_ACACHE acache[NNTP_ACACHE_LEN];
+  body_cache_t *bcache;
+} NNTP_DATA;
+
+typedef struct
+{
+  anum_t article_num;
+  unsigned int parsed : 1;
+} NNTP_HEADER_DATA;
+
+#define NHDR(hdr) ((NNTP_HEADER_DATA*)((hdr)->data))
+
+/* internal functions */
+int nntp_add_group (char *, void *);
+int nntp_active_save_cache (NNTP_SERVER *);
+int nntp_check_new_groups (NNTP_SERVER *);
+int nntp_fastclose_mailbox (CONTEXT *);
+int nntp_open_connection (NNTP_SERVER *);
+void nntp_newsrc_gen_entries (CONTEXT *);
+void nntp_bcache_update (NNTP_DATA *);
+void nntp_article_status (CONTEXT *, HEADER *, char *, anum_t);
+void nntp_group_unread_stat (NNTP_DATA *);
+void nntp_data_free (void *);
+void nntp_acache_free (NNTP_DATA *);
+void nntp_delete_group_cache (NNTP_DATA *);
+
+/* exposed interface */
+NNTP_SERVER *nntp_select_server (char *, int);
+NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER *, char *);
+NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER *, char *);
+NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER *, char *);
+NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER *, char *);
+int nntp_active_fetch (NNTP_SERVER *);
+int nntp_newsrc_update (NNTP_SERVER *);
+int nntp_open_mailbox (CONTEXT *);
+int nntp_sync_mailbox (CONTEXT *);
+int nntp_check_mailbox (CONTEXT *, int);
+int nntp_fetch_message (MESSAGE *, CONTEXT *, int);
+int nntp_post (const char *);
+int nntp_check_msgid (CONTEXT *, const char *);
+int nntp_check_children (CONTEXT *, const char *);
+int nntp_newsrc_parse (NNTP_SERVER *);
+void nntp_newsrc_close (NNTP_SERVER *);
+void nntp_buffy (char *, size_t);
+void nntp_expand_path (char *, size_t, ACCOUNT *);
+void nntp_clear_cache (NNTP_SERVER *);
+const char *nntp_format_str (char *, size_t, size_t, char, const char *,
+			     const char *, const char *, const char *,
+			     unsigned long, format_flag);
+
+NNTP_SERVER *CurrentNewsSrv INITVAL (NULL);
+
+#ifdef USE_HCACHE
+header_cache_t *nntp_hcache_open (NNTP_DATA *);
+void nntp_hcache_update (NNTP_DATA *, header_cache_t *);
+#endif
+
+#endif /* _NNTP_H_ */
diff --git a/pager.c b/pager.c
index 696e55c..8e38e38 100644
--- a/pager.c
+++ b/pager.c
@@ -1085,6 +1085,11 @@ fill_buffer (FILE *f, LOFF_T *last_pos, LOFF_T offset, unsigned char **buf,
   return b_read;
 }
 
+#ifdef USE_NNTP
+#include "mx.h"
+#include "nntp.h"
+#endif
+
 
 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
 			int flags, ansi_attr *pa, int cnt,
@@ -1543,6 +1548,16 @@ static const struct mapping_t PagerHelpExtra[] = {
   { NULL,	0 }
 };
 
+#ifdef USE_NNTP
+static struct mapping_t PagerNewsHelpExtra[] = {
+  { N_("Post"),     OP_POST },
+  { N_("Followup"), OP_FOLLOWUP },
+  { N_("Del"),      OP_DELETE },
+  { N_("Next"),     OP_MAIN_NEXT_UNDELETED },
+  { NULL,           0 }
+};
+#endif
+
 
 
 /* This pager is actually not so simple as it once was.  It now operates in
@@ -1584,6 +1599,10 @@ mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
   int old_PagerIndexLines;		/* some people want to resize it
   					 * while inside the pager... */
 
+#ifdef USE_NNTP
+  char *followup_to;
+#endif
+
   if (!(flags & M_SHOWCOLOR))
     flags |= M_SHOWFLAT;
 
@@ -1623,7 +1642,11 @@ mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
   if (IsHeader (extra))
   {
     strfcpy (tmphelp, helpstr, sizeof (tmphelp));
-    mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER, PagerHelpExtra);
+    mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
+#ifdef USE_NNTP
+	(Context && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
+#endif
+	PagerHelpExtra);
     snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
   }
   if (!InHelp)
@@ -2551,6 +2574,60 @@ search_next:
 	redraw = REDRAW_FULL;
 	break;
 
+#ifdef USE_NNTP
+      case OP_POST:
+	CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
+	CHECK_ATTACH;
+	if (extra->ctx && extra->ctx->magic == M_NNTP &&
+	    !((NNTP_DATA *)extra->ctx->data)->allowed &&
+	    query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
+	  break;
+	ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
+	redraw = REDRAW_FULL;
+	break;
+
+      case OP_FORWARD_TO_GROUP:
+	CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
+	CHECK_ATTACH;
+	if (extra->ctx && extra->ctx->magic == M_NNTP &&
+	    !((NNTP_DATA *)extra->ctx->data)->allowed &&
+	    query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
+	  break;
+	if (IsMsgAttach (extra))
+	  mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
+			       extra->idxlen, extra->bdy, SENDNEWS);
+	else
+	  ci_send_message (SENDNEWS|SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
+	redraw = REDRAW_FULL;
+	break;
+
+      case OP_FOLLOWUP:
+	CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
+	CHECK_ATTACH;
+
+	if (IsMsgAttach (extra))
+	  followup_to = extra->bdy->hdr->env->followup_to;
+	else
+	  followup_to = extra->hdr->env->followup_to;
+
+	if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
+	    query_quadoption (OPT_FOLLOWUPTOPOSTER,_("Reply by mail as poster prefers?")) != M_YES)
+	{
+	  if (extra->ctx && extra->ctx->magic == M_NNTP &&
+	      !((NNTP_DATA *)extra->ctx->data)->allowed &&
+	      query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
+	    break;
+	  if (IsMsgAttach (extra))
+	    mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
+			       extra->idxlen, extra->bdy, SENDNEWS|SENDREPLY);
+	  else
+	    ci_send_message (SENDNEWS|SENDREPLY, NULL, NULL,
+			     extra->ctx, extra->hdr);
+	  redraw = REDRAW_FULL;
+	  break;
+	}
+#endif
+
       case OP_REPLY:
 	CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
         CHECK_ATTACH;      
@@ -2597,7 +2674,7 @@ search_next:
         CHECK_ATTACH;
         if (IsMsgAttach (extra))
 	  mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
-			       extra->idxlen, extra->bdy);
+			       extra->idxlen, extra->bdy, 0);
         else
 	  ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
 	redraw = REDRAW_FULL;
diff --git a/parse.c b/parse.c
index 58c7774..36046e9 100644
--- a/parse.c
+++ b/parse.c
@@ -89,7 +89,7 @@ char *mutt_read_rfc822_line (FILE *f, char *line, size_t *linelen)
   /* not reached */
 }
 
-static LIST *mutt_parse_references (char *s, int in_reply_to)
+LIST *mutt_parse_references (char *s, int in_reply_to)
 {
   LIST *t, *lst = NULL;
   char *m;
@@ -1072,6 +1072,17 @@ int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short
       e->from = rfc822_parse_adrlist (e->from, p);
       matched = 1;
     }
+#ifdef USE_NNTP
+    else if (!mutt_strcasecmp (line+1, "ollowup-to"))
+    {
+      if (!e->followup_to)
+      {
+	mutt_remove_trailing_ws (p);
+	e->followup_to = safe_strdup (mutt_skip_whitespace (p));
+      }
+      matched = 1;
+    }
+#endif
     break;
     
     case 'i':
@@ -1154,6 +1165,27 @@ int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short
     }
     break;
     
+#ifdef USE_NNTP
+    case 'n':
+    if (!mutt_strcasecmp (line + 1, "ewsgroups"))
+    {
+      FREE (&e->newsgroups);
+      mutt_remove_trailing_ws (p);
+      e->newsgroups = safe_strdup (mutt_skip_whitespace (p));
+      matched = 1;
+    }
+    break;
+#endif
+
+    case 'o':
+    /* field `Organization:' saves only for pager! */
+    if (!mutt_strcasecmp (line + 1, "rganization"))
+    {
+      if (!e->organization && mutt_strcasecmp (p, "unknown"))
+	e->organization = safe_strdup (p);
+    }
+    break;
+
     case 'r':
     if (!ascii_strcasecmp (line + 1, "eferences"))
     {
@@ -1266,6 +1298,20 @@ int mutt_parse_rfc822_line (ENVELOPE *e, HEADER *hdr, char *line, char *p, short
       e->x_label = safe_strdup(p);
       matched = 1;
     }
+#ifdef USE_NNTP
+    else if (!mutt_strcasecmp (line + 1, "-comment-to"))
+    {
+      if (!e->x_comment_to)
+	e->x_comment_to = safe_strdup (p);
+      matched = 1;
+    }
+    else if (!mutt_strcasecmp (line + 1, "ref"))
+    {
+      if (!e->xref)
+	e->xref = safe_strdup (p);
+      matched = 1;
+    }
+#endif
     
     default:
     break;
diff --git a/pattern.c b/pattern.c
index 4cdbd05..9375a4e 100644
--- a/pattern.c
+++ b/pattern.c
@@ -92,6 +92,9 @@ Flags[] =
   { 'U', M_UNREAD,		0,		NULL },
   { 'v', M_COLLAPSED,		0,		NULL },
   { 'V', M_CRYPT_VERIFIED,	0,		NULL },
+#ifdef USE_NNTP
+  { 'w', M_NEWSGROUPS,		0,		eat_regexp },
+#endif
   { 'x', M_REFERENCE,		0,		eat_regexp },
   { 'X', M_MIMEATTACH,		0,		eat_range },
   { 'y', M_XLABEL,		0,		eat_regexp },
@@ -1213,6 +1216,10 @@ mutt_pattern_exec (struct pattern_t *pat, pattern_exec_flag flags, CONTEXT *ctx,
       }
     case M_UNREFERENCED:
       return (pat->not ^ (h->thread && !h->thread->child));
+#ifdef USE_NNTP
+    case M_NEWSGROUPS:
+      return (pat->not ^ (h->env->newsgroups && patmatch (pat, h->env->newsgroups) == 0));
+#endif
   }
   mutt_error (_("error: unknown op %d (report this error)."), pat->op);
   return (-1);
@@ -1294,6 +1301,7 @@ int mutt_pattern_func (int op, char *prompt)
   progress_t progress;
 
   strfcpy (buf, NONULL (Context->pattern), sizeof (buf));
+  if (prompt || op != M_LIMIT)
   if (mutt_get_field (prompt, buf, sizeof (buf), M_PATTERN | M_CLEAR) != 0 || !buf[0])
     return (-1);
 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 3654ad1..1e499ec 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -47,6 +47,8 @@ mutt_ssl_gnutls.c
 mutt_tunnel.c
 muttlib.c
 mx.c
+newsrc.c
+nntp.c
 pager.c
 parse.c
 pattern.c
diff --git a/postpone.c b/postpone.c
index 7a4cbb1..569edb6 100644
--- a/postpone.c
+++ b/postpone.c
@@ -125,15 +125,26 @@ int mutt_num_postponed (int force)
 
   if (LastModify < st.st_mtime)
   {
+#ifdef USE_NNTP
+    int optnews = option (OPTNEWS);
+#endif
     LastModify = st.st_mtime;
 
     if (access (Postponed, R_OK | F_OK) != 0)
       return (PostCount = 0);
+#ifdef USE_NNTP
+    if (optnews)
+	unset_option (OPTNEWS);
+#endif
     if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
       PostCount = 0;
     else
       PostCount = ctx.msgcount;
     mx_fastclose_mailbox (&ctx);
+#ifdef USE_NNTP
+    if (optnews)
+	set_option (OPTNEWS);
+#endif
   }
 
   return (PostCount);
diff --git a/protos.h b/protos.h
index 5d2fafb..81d09db 100644
--- a/protos.h
+++ b/protos.h
@@ -111,6 +111,7 @@ HASH *mutt_make_id_hash (CONTEXT *);
 HASH *mutt_make_subj_hash (CONTEXT *);
 
 LIST *mutt_make_references(ENVELOPE *e);
+LIST *mutt_parse_references (char *, int);
 
 char *mutt_read_rfc822_line (FILE *, char *, size_t *);
 ENVELOPE *mutt_read_rfc822_header (FILE *, HEADER *, short, short);
diff --git a/recvattach.c b/recvattach.c
index fddbc2f..b76a4ea 100644
--- a/recvattach.c
+++ b/recvattach.c
@@ -1120,6 +1120,15 @@ void mutt_view_attachments (HEADER *hdr)
 	}
 #endif
 
+#ifdef USE_NNTP
+	if (Context->magic == M_NNTP)
+	{
+	  mutt_flushinp ();
+	  mutt_error _("Can't delete attachment from news server.");
+	  break;
+	}
+#endif
+
         if (WithCrypto && (hdr->security & ENCRYPT))
         {
           mutt_message _(
@@ -1214,10 +1223,33 @@ void mutt_view_attachments (HEADER *hdr)
       case OP_FORWARD_MESSAGE:
         CHECK_ATTACH;
         mutt_attach_forward (fp, hdr, idx, idxlen,
-			     menu->tagprefix ? NULL : idx[menu->current]->content);
+			     menu->tagprefix ? NULL : idx[menu->current]->content, 0);
         menu->redraw = REDRAW_FULL;
         break;
       
+#ifdef USE_NNTP
+      case OP_FORWARD_TO_GROUP:
+	CHECK_ATTACH;
+	mutt_attach_forward (fp, hdr, idx, idxlen,
+		menu->tagprefix ? NULL : idx[menu->current]->content, SENDNEWS);
+	menu->redraw = REDRAW_FULL;
+	break;
+
+      case OP_FOLLOWUP:
+	CHECK_ATTACH;
+
+	if (!idx[menu->current]->content->hdr->env->followup_to ||
+	    mutt_strcasecmp (idx[menu->current]->content->hdr->env->followup_to, "poster") ||
+	    query_quadoption (OPT_FOLLOWUPTOPOSTER,_("Reply by mail as poster prefers?")) != M_YES)
+	{
+	  mutt_attach_reply (fp, hdr, idx, idxlen,
+		menu->tagprefix ? NULL : idx[menu->current]->content,
+		SENDNEWS|SENDREPLY);
+	  menu->redraw = REDRAW_FULL;
+	  break;
+	}
+#endif
+
       case OP_REPLY:
       case OP_GROUP_REPLY:
       case OP_LIST_REPLY:
diff --git a/recvcmd.c b/recvcmd.c
index a6a3a91..e633119 100644
--- a/recvcmd.c
+++ b/recvcmd.c
@@ -401,7 +401,7 @@ static BODY ** copy_problematic_attachments (FILE *fp,
 static void attach_forward_bodies (FILE * fp, HEADER * hdr,
 				   ATTACHPTR ** idx, short idxlen,
 				   BODY * cur,
-				   short nattach)
+				   short nattach, int flags)
 {
   short i;
   short mime_fwd_all = 0;
@@ -547,7 +547,7 @@ _("Can't decode all tagged attachments.  MIME-forward the others?"))) == -1)
   tmpfp = NULL;
 
   /* now that we have the template, send it. */
-  ci_send_message (0, tmphdr, tmpbody, NULL, parent);
+  ci_send_message (flags, tmphdr, tmpbody, NULL, parent);
   return;
   
   bail:
@@ -574,7 +574,7 @@ _("Can't decode all tagged attachments.  MIME-forward the others?"))) == -1)
  */
 
 static void attach_forward_msgs (FILE * fp, HEADER * hdr, 
-	       ATTACHPTR ** idx, short idxlen, BODY * cur)
+	       ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
 {
   HEADER *curhdr = NULL;
   HEADER *tmphdr;
@@ -679,23 +679,23 @@ static void attach_forward_msgs (FILE * fp, HEADER * hdr,
   else
     mutt_free_header (&tmphdr);
 
-  ci_send_message (0, tmphdr, *tmpbody ? tmpbody : NULL, 
+  ci_send_message (flags, tmphdr, *tmpbody ? tmpbody : NULL, 
 		   NULL, curhdr);
 
 }
 
 void mutt_attach_forward (FILE * fp, HEADER * hdr, 
-			  ATTACHPTR ** idx, short idxlen, BODY * cur)
+			  ATTACHPTR ** idx, short idxlen, BODY * cur, int flags)
 {
   short nattach;
   
 
   if (check_all_msg (idx, idxlen, cur, 0) == 0)
-    attach_forward_msgs (fp, hdr, idx, idxlen, cur);
+    attach_forward_msgs (fp, hdr, idx, idxlen, cur, flags);
   else
   {
     nattach = count_tagged (idx, idxlen);
-    attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach);
+    attach_forward_bodies (fp, hdr, idx, idxlen, cur, nattach, flags);
   }
 }
 
@@ -753,28 +753,40 @@ attach_reply_envelope_defaults (ENVELOPE *env, ATTACHPTR **idx, short idxlen,
     return -1;
   }
 
-  if (parent)
+#ifdef USE_NNTP
+  if ((flags & SENDNEWS))
   {
-    if (mutt_fetch_recips (env, curenv, flags) == -1)
-      return -1;
+    /* in case followup set Newsgroups: with Followup-To: if it present */
+    if (!env->newsgroups && curenv &&
+	mutt_strcasecmp (curenv->followup_to, "poster"))
+      env->newsgroups = safe_strdup (curenv->followup_to);
   }
   else
+#endif
   {
-    for (i = 0; i < idxlen; i++)
+    if (parent)
     {
-      if (idx[i]->content->tagged
-	  && mutt_fetch_recips (env, idx[i]->content->hdr->env, flags) == -1)
+      if (mutt_fetch_recips (env, curenv, flags) == -1)
 	return -1;
     }
+    else
+    {
+      for (i = 0; i < idxlen; i++)
+      {
+	if (idx[i]->content->tagged
+	    && mutt_fetch_recips (env, idx[i]->content->hdr->env, flags) == -1)
+	  return -1;
+      }
+    }
+
+    if ((flags & SENDLISTREPLY) && !env->to)
+    {
+      mutt_error _("No mailing lists found!");
+      return (-1);
+    }
+
+    mutt_fix_reply_recipients (env);
   }
-  
-  if ((flags & SENDLISTREPLY) && !env->to)
-  {
-    mutt_error _("No mailing lists found!");
-    return (-1);
-  }
-  
-  mutt_fix_reply_recipients (env);
   mutt_make_misc_reply_headers (env, Context, curhdr, curenv);
 
   if (parent)
@@ -835,6 +847,13 @@ void mutt_attach_reply (FILE * fp, HEADER * hdr,
   char prefix[SHORT_STRING];
   int rc;
   
+#ifdef USE_NNTP
+  if (flags & SENDNEWS)
+    set_option (OPTNEWSSEND);
+  else
+    unset_option (OPTNEWSSEND);
+#endif
+
   if (check_all_msg (idx, idxlen, cur, 0) == -1)
   {
     nattach = count_tagged (idx, idxlen);
diff --git a/send.c b/send.c
index 0b45171..f4a3611 100644
--- a/send.c
+++ b/send.c
@@ -44,6 +44,11 @@
 #include <sys/types.h>
 #include <utime.h>
 
+#ifdef USE_NNTP
+#include "nntp.h"
+#include "mx.h"
+#endif
+
 #ifdef MIXMASTER
 #include "remailer.h"
 #endif
@@ -213,17 +218,51 @@ static int edit_address (ADDRESS **a, /* const */ char *field)
   return 0;
 }
 
-static int edit_envelope (ENVELOPE *en)
+static int edit_envelope (ENVELOPE *en, int flags)
 {
   char buf[HUGE_STRING];
   LIST *uh = UserHeader;
 
-  if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
-    return (-1);
-  if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
-    return (-1);
-  if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
-    return (-1);
+#ifdef USE_NNTP
+  if (option (OPTNEWSSEND))
+  {
+    if (en->newsgroups)
+      strfcpy (buf, en->newsgroups, sizeof (buf));
+    else
+      buf[0] = 0;
+    if (mutt_get_field ("Newsgroups: ", buf, sizeof (buf), 0) != 0)
+      return (-1);
+    FREE (&en->newsgroups);
+    en->newsgroups = safe_strdup (buf);
+
+    if (en->followup_to)
+      strfcpy (buf, en->followup_to, sizeof (buf));
+    else
+      buf[0] = 0;
+    if (option (OPTASKFOLLOWUP) && mutt_get_field ("Followup-To: ", buf, sizeof (buf), 0) != 0)
+      return (-1);
+    FREE (&en->followup_to);
+    en->followup_to = safe_strdup (buf);
+
+    if (en->x_comment_to)
+      strfcpy (buf, en->x_comment_to, sizeof (buf));
+    else
+      buf[0] = 0;
+    if (option (OPTXCOMMENTTO) && option (OPTASKXCOMMENTTO) && mutt_get_field ("X-Comment-To: ", buf, sizeof (buf), 0) != 0)
+      return (-1);
+    FREE (&en->x_comment_to);
+    en->x_comment_to = safe_strdup (buf);
+  }
+  else
+#endif
+  {
+    if (edit_address (&en->to, "To: ") == -1 || en->to == NULL)
+      return (-1);
+    if (option (OPTASKCC) && edit_address (&en->cc, "Cc: ") == -1)
+      return (-1);
+    if (option (OPTASKBCC) && edit_address (&en->bcc, "Bcc: ") == -1)
+      return (-1);
+  }
 
   if (en->subject)
   {
@@ -258,6 +297,14 @@ static int edit_envelope (ENVELOPE *en)
   return 0;
 }
 
+#ifdef USE_NNTP
+char *nntp_get_header (const char *s)
+{
+  SKIPWS (s);
+  return safe_strdup (s);
+}
+#endif
+
 static void process_user_recips (ENVELOPE *env)
 {
   LIST *uh = UserHeader;
@@ -270,6 +317,14 @@ static void process_user_recips (ENVELOPE *env)
       env->cc = rfc822_parse_adrlist (env->cc, uh->data + 3);
     else if (ascii_strncasecmp ("bcc:", uh->data, 4) == 0)
       env->bcc = rfc822_parse_adrlist (env->bcc, uh->data + 4);
+#ifdef USE_NNTP
+    else if (ascii_strncasecmp ("newsgroups:", uh->data, 11) == 0)
+      env->newsgroups = nntp_get_header (uh->data + 11);
+    else if (ascii_strncasecmp ("followup-to:", uh->data, 12) == 0)
+      env->followup_to = nntp_get_header (uh->data + 12);
+    else if (ascii_strncasecmp ("x-comment-to:", uh->data, 13) == 0)
+      env->x_comment_to = nntp_get_header (uh->data + 13);
+#endif
   }
 }
 
@@ -308,6 +363,12 @@ static void process_user_header (ENVELOPE *env)
     else if (ascii_strncasecmp ("to:", uh->data, 3) != 0 &&
 	     ascii_strncasecmp ("cc:", uh->data, 3) != 0 &&
 	     ascii_strncasecmp ("bcc:", uh->data, 4) != 0 &&
+#ifdef USE_NNTP
+	     ascii_strncasecmp ("newsgroups:", uh->data, 11) != 0 &&
+	     ascii_strncasecmp ("followup-to:", uh->data, 12) != 0 &&
+	     ascii_strncasecmp ("x-comment-to:", uh->data, 13) != 0 &&
+#endif
+	     ascii_strncasecmp ("supersedes:", uh->data, 11) != 0 &&
 	     ascii_strncasecmp ("subject:", uh->data, 8) != 0 &&
 	     ascii_strncasecmp ("return-path:", uh->data, 12) != 0)
     {
@@ -656,6 +717,10 @@ void mutt_add_to_reference_headers (ENVELOPE *env, ENVELOPE *curenv, LIST ***pp,
   if (pp) *pp = p;
   if (qq) *qq = q;
   
+#ifdef USE_NNTP
+  if (option (OPTNEWSSEND) && option (OPTXCOMMENTTO) && curenv->from)
+    env->x_comment_to = safe_strdup (mutt_get_name (curenv->from));
+#endif
 }
 
 static void 
@@ -718,6 +783,16 @@ envelope_defaults (ENVELOPE *env, CONTEXT *ctx, HEADER *cur, int flags)
 
   if (flags & SENDREPLY)
   {
+#ifdef USE_NNTP
+    if ((flags & SENDNEWS))
+    {
+      /* in case followup set Newsgroups: with Followup-To: if it present */
+      if (!env->newsgroups && curenv &&
+	  mutt_strcasecmp (curenv->followup_to, "poster"))
+	env->newsgroups = safe_strdup (curenv->followup_to);
+    }
+    else
+#endif
     if (tag)
     {
       HEADER *h;
@@ -864,7 +939,18 @@ void mutt_set_followup_to (ENVELOPE *e)
    * it hasn't already been set
    */
 
-  if (option (OPTFOLLOWUPTO) && !e->mail_followup_to)
+  if (!option (OPTFOLLOWUPTO))
+    return;
+#ifdef USE_NNTP
+  if (option (OPTNEWSSEND))
+  {
+    if (!e->followup_to && e->newsgroups && (strrchr (e->newsgroups, ',')))
+      e->followup_to = safe_strdup (e->newsgroups);
+    return;
+  }
+#endif
+
+  if (!e->mail_followup_to)
   {
     if (mutt_is_list_cc (0, e->to, e->cc))
     {
@@ -1026,6 +1112,9 @@ static int send_message (HEADER *msg)
 #endif
 
 #if USE_SMTP
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+#endif
   if (SmtpUrl)
       return mutt_smtp_send (msg->env->from, msg->env->to, msg->env->cc,
                              msg->env->bcc, tempfile,
@@ -1159,6 +1248,13 @@ ci_send_message (int flags,		/* send mode */
 
   int rv = -1;
   
+#ifdef USE_NNTP
+  if (flags & SENDNEWS)
+    set_option (OPTNEWSSEND);
+  else
+    unset_option (OPTNEWSSEND);
+#endif
+
   if (!flags && !msg && quadoption (OPT_RECALL) != M_NO &&
       mutt_num_postponed (1))
   {
@@ -1194,6 +1290,22 @@ ci_send_message (int flags,		/* send mode */
     {
       if ((flags = mutt_get_postponed (ctx, msg, &cur, fcc, sizeof (fcc))) < 0)
 	goto cleanup;
+#ifdef USE_NNTP
+      /*
+       * If postponed message is a news article, it have
+       * a "Newsgroups:" header line, then set appropriate flag.
+       */
+      if (msg->env->newsgroups)
+      {
+	flags |= SENDNEWS;
+	set_option (OPTNEWSSEND);
+      }
+      else
+      {
+	flags &= ~SENDNEWS;
+	unset_option (OPTNEWSSEND);
+      }
+#endif
     }
 
     if (flags & (SENDPOSTPONED|SENDRESEND))
@@ -1286,11 +1398,16 @@ ci_send_message (int flags,		/* send mode */
     if (flags & SENDREPLY)
       mutt_fix_reply_recipients (msg->env);
 
+#ifdef USE_NNTP
+    if ((flags & SENDNEWS) && ctx && ctx->magic == M_NNTP && !msg->env->newsgroups)
+      msg->env->newsgroups = safe_strdup (((NNTP_DATA *)ctx->data)->group);
+#endif
+
     if (! (flags & (SENDMAILX|SENDBATCH)) &&
 	! (option (OPTAUTOEDIT) && option (OPTEDITHDRS)) &&
 	! ((flags & SENDREPLY) && option (OPTFASTREPLY)))
     {
-      if (edit_envelope (msg->env) == -1)
+      if (edit_envelope (msg->env, flags) == -1)
 	goto cleanup;
     }
 
@@ -1587,6 +1704,11 @@ main_loop:
     if (i == -1)
     {
       /* abort */
+#ifdef USE_NNTP
+      if (flags & SENDNEWS)
+	mutt_message _("Article not posted.");
+      else
+#endif
       mutt_message _("Mail not sent.");
       goto cleanup;
     }
@@ -1641,6 +1763,9 @@ main_loop:
     }
   }
 
+#ifdef USE_NNTP
+  if (!(flags & SENDNEWS))
+#endif
   if (!has_recips (msg->env->to) && !has_recips (msg->env->cc) &&
       !has_recips (msg->env->bcc))
   {
@@ -1674,6 +1799,19 @@ main_loop:
       mutt_error _("No subject specified.");
     goto main_loop;
   }
+#ifdef USE_NNTP
+  if ((flags & SENDNEWS) && !msg->env->subject)
+  {
+    mutt_error _("No subject specified.");
+    goto main_loop;
+  }
+
+  if ((flags & SENDNEWS) && !msg->env->newsgroups)
+  {
+    mutt_error _("No newsgroup specified.");
+    goto main_loop;
+  }
+#endif
 
   if (msg->content->next)
     msg->content = mutt_make_multipart (msg->content);
@@ -1880,7 +2018,12 @@ full_fcc:
     }
   }
   else if (!option (OPTNOCURSES) && ! (flags & SENDMAILX))
-    mutt_message (i == 0 ? _("Mail sent.") : _("Sending in background."));
+    mutt_message (i != 0 ? _("Sending in background.") :
+#ifdef USE_NNTP
+		  (flags & SENDNEWS) ? _("Article posted.") : _("Mail sent."));
+#else
+		  _("Mail sent."));
+#endif
 
   if (WithCrypto && (msg->security & ENCRYPT))
     FREE (&pgpkeylist);
diff --git a/sendlib.c b/sendlib.c
index 2144156..cddbe32 100644
--- a/sendlib.c
+++ b/sendlib.c
@@ -46,6 +46,10 @@
 #include <sys/wait.h>
 #include <fcntl.h>
 
+#ifdef USE_NNTP
+#include "nntp.h"
+#endif
+
 #ifdef HAVE_SYSEXITS_H
 #include <sysexits.h>
 #else /* Make sure EX_OK is defined <philiph@pobox.com> */
@@ -1543,6 +1547,14 @@ void mutt_write_references (LIST *r, FILE *f, int trim)
 {
   LIST **ref = NULL;
   int refcnt = 0, refmax = 0;
+  int multiline = 1;
+  int space = 0;
+
+  if (trim < 0)
+  {
+    trim = -trim;
+    multiline = 0;
+  }
 
   for ( ; (trim == 0 || refcnt < trim) && r ; r = r->next)
   {
@@ -1553,9 +1565,11 @@ void mutt_write_references (LIST *r, FILE *f, int trim)
 
   while (refcnt-- > 0)
   {
-    fputc (' ', f);
+    if (multiline || space)
+      fputc (' ', f);
+    space = 1;
     fputs (ref[refcnt]->data, f);
-    if (refcnt >= 1)
+    if (multiline && refcnt >= 1)
       fputc ('\n', f);
   }
 
@@ -1969,6 +1983,9 @@ int mutt_write_rfc822_header (FILE *fp, ENVELOPE *env, BODY *attach,
     mutt_write_address_list (env->to, fp, 4, 0);
   }
   else if (mode > 0)
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+#endif
     fputs ("To: \n", fp);
 
   if (env->cc)
@@ -1977,6 +1994,9 @@ int mutt_write_rfc822_header (FILE *fp, ENVELOPE *env, BODY *attach,
     mutt_write_address_list (env->cc, fp, 4, 0);
   }
   else if (mode > 0)
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+#endif
     fputs ("Cc: \n", fp);
 
   if (env->bcc && should_write_bcc)
@@ -1988,8 +2008,28 @@ int mutt_write_rfc822_header (FILE *fp, ENVELOPE *env, BODY *attach,
     }
   }
   else if (mode > 0)
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+#endif
     fputs ("Bcc: \n", fp);
 
+#ifdef USE_NNTP
+  if (env->newsgroups)
+    fprintf (fp, "Newsgroups: %s\n", env->newsgroups);
+  else if (mode == 1 && option (OPTNEWSSEND))
+    fputs ("Newsgroups: \n", fp);
+
+  if (env->followup_to)
+    fprintf (fp, "Followup-To: %s\n", env->followup_to);
+  else if (mode == 1 && option (OPTNEWSSEND))
+    fputs ("Followup-To: \n", fp);
+
+  if (env->x_comment_to)
+    fprintf (fp, "X-Comment-To: %s\n", env->x_comment_to);
+  else if (mode == 1 && option (OPTNEWSSEND) && option (OPTXCOMMENTTO))
+    fputs ("X-Comment-To: \n", fp);
+#endif
+
   if (env->subject)
     mutt_write_one_header (fp, "Subject", env->subject, NULL, 0, 0);
   else if (mode == 1)
@@ -2008,6 +2048,9 @@ int mutt_write_rfc822_header (FILE *fp, ENVELOPE *env, BODY *attach,
     fputs ("Reply-To: \n", fp);
 
   if (env->mail_followup_to)
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+#endif
   {
     fputs ("Mail-Followup-To: ", fp);
     mutt_write_address_list (env->mail_followup_to, fp, 18, 0);
@@ -2351,6 +2394,23 @@ mutt_invoke_sendmail (ADDRESS *from,	/* the sender */
   size_t argslen = 0, argsmax = 0;
   int i;
 
+#ifdef USE_NNTP
+  if (option (OPTNEWSSEND))
+  {
+    char cmd[LONG_STRING];
+
+    mutt_FormatString (cmd, sizeof (cmd), 0, NONULL (Inews), nntp_format_str, 0, 0);
+    if (!*cmd)
+    {
+      i = nntp_post (msg);
+      unlink (msg);
+      return i;
+    }
+
+    s = safe_strdup (cmd);
+  }
+#endif
+
   /* ensure that $sendmail is set to avoid a crash. http://dev.mutt.org/trac/ticket/3548 */
   if (!s)
   {
@@ -2381,6 +2441,10 @@ mutt_invoke_sendmail (ADDRESS *from,	/* the sender */
     i++;
   }
 
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND))
+  {
+#endif
   if (eightbit && option (OPTUSE8BITMIME))
     args = add_option (args, &argslen, &argsmax, "-B8BITMIME");
 
@@ -2412,6 +2476,9 @@ mutt_invoke_sendmail (ADDRESS *from,	/* the sender */
   args = add_args (args, &argslen, &argsmax, to);
   args = add_args (args, &argslen, &argsmax, cc);
   args = add_args (args, &argslen, &argsmax, bcc);
+#ifdef USE_NNTP
+  }
+#endif
 
   if (argslen == argsmax)
     safe_realloc (&args, sizeof (char *) * (++argsmax));
@@ -2492,6 +2559,9 @@ void mutt_prepare_envelope (ENVELOPE *env, int final)
   rfc2047_encode_string (&env->x_label);
 
   if (env->subject)
+#ifdef USE_NNTP
+  if (!option (OPTNEWSSEND) || option (OPTMIMESUBJECT))
+#endif
   {
     rfc2047_encode_string (&env->subject);
   }
@@ -2612,6 +2682,10 @@ int mutt_bounce_message (FILE *fp, HEADER *h, ADDRESS *to)
   }
   rfc822_write_address (resent_from, sizeof (resent_from), from, 0);
 
+#ifdef USE_NNTP
+  unset_option (OPTNEWSSEND);
+#endif
+
   /*
    * prepare recipient list. idna conversion appears to happen before this
    * function is called, since the user receives confirmation of the address
diff --git a/sort.c b/sort.c
index 76e9e79..9aa259c 100644
--- a/sort.c
+++ b/sort.c
@@ -24,6 +24,11 @@
 #include "sort.h"
 #include "mutt_idna.h"
 
+#ifdef USE_NNTP
+#include "mx.h"
+#include "nntp.h"
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
@@ -151,6 +156,17 @@ static int compare_order (const void *a, const void *b)
   HEADER **ha = (HEADER **) a;
   HEADER **hb = (HEADER **) b;
 
+#ifdef USE_NNTP
+  if (Context && Context->magic == M_NNTP)
+  {
+    anum_t na = NHDR (*ha)->article_num;
+    anum_t nb = NHDR (*hb)->article_num;
+    int result = na == nb ? 0 : na > nb ? 1 : -1;
+    AUXSORT (result, a, b);
+    return (SORTCODE (result));
+  }
+  else
+#endif
   /* no need to auxsort because you will never have equality here */
   return (SORTCODE ((*ha)->index - (*hb)->index));
 }
diff --git a/url.c b/url.c
index 988cf59..3fd4079 100644
--- a/url.c
+++ b/url.c
@@ -39,6 +39,8 @@ static const struct mapping_t UrlMap[] =
   { "imaps", 	U_IMAPS },
   { "pop",  	U_POP },
   { "pops", 	U_POPS },
+  { "news",	U_NNTP },
+  { "snews",	U_NNTPS },
   { "mailto",	U_MAILTO },
   { "smtp",     U_SMTP },
   { "smtps",    U_SMTPS },
@@ -214,7 +216,7 @@ int url_ciss_tostring (ciss_url_t* ciss, char* dest, size_t len, int flags)
       safe_strcat (dest, len, "//");
     len -= (l = strlen (dest)); dest += l;
 
-    if (ciss->user)
+    if (ciss->user && (ciss->user[0] || !(flags & U_PATH)))
     {
       char u[STRING];
       url_pct_encode (u, sizeof (u), ciss->user);
diff --git a/url.h b/url.h
index 926416e..15ec9ce 100644
--- a/url.h
+++ b/url.h
@@ -8,6 +8,8 @@ typedef enum url_scheme
   U_POPS,
   U_IMAP,
   U_IMAPS,
+  U_NNTP,
+  U_NNTPS,
   U_SMTP,
   U_SMTPS,
   U_MAILTO,