unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob c174ca4b30ec1ea81906fef2ab2318c148610114 409749 bytes (raw)
name: lisp/erc/erc.el 	 # note: path name is non-authoritative(*)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
 
;;; erc.el --- An Emacs Internet Relay Chat client  -*- lexical-binding:t -*-

;; Copyright (C) 1997-2024 Free Software Foundation, Inc.

;; Author: Alexander L. Belikoff <alexander@belikoff.net>
;; Maintainer: Amin Bandali <bandali@gnu.org>, F. Jason Park <jp@neverwas.me>
;; Contributors: Sergey Berezin (sergey.berezin@cs.cmu.edu),
;;               Mario Lang (mlang@delysid.org),
;;               Alex Schroeder (alex@gnu.org)
;;               Andreas Fuchs (afs@void.at)
;;               Gergely Nagy (algernon@midgard.debian.net)
;;               David Edmondson (dme@dme.org)
;;               Michael Olson (mwolson@gnu.org)
;;               Kelvin White (kwhite@gnu.org)
;; Version: 5.6-git
;; Package-Requires: ((emacs "27.1") (compat "29.1.4.4"))
;; Keywords: IRC, chat, client, Internet
;; URL: https://www.gnu.org/software/emacs/erc.html

;; This is a GNU ELPA :core package.  Avoid functionality that is not
;; compatible with the version of Emacs recorded above.

;; This file is part of GNU Emacs.

;; GNU Emacs 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 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; ERC is a powerful, modular, and extensible IRC client for Emacs.
;; For more information, visit the ERC page at
;; <https://www.gnu.org/software/emacs/erc.html>.

;; Configuration:

;; Use M-x customize-group RET erc RET to get an overview
;; of all the variables you can tweak.

;; Usage:

;; To connect to an IRC server, do
;;
;; M-x erc RET
;;
;; or
;;
;; M-x erc-tls RET
;;
;; to connect over TLS (encrypted).  Once you are connected to a
;; server, you can use C-h m or have a look at the ERC menu.

;;; Code:

(eval-and-compile (load "erc-loaddefs" 'noerror 'nomessage))

(require 'erc-networks)
(require 'erc-backend)
(require 'cl-lib)
(require 'format-spec)
(require 'auth-source)
(eval-when-compile (require 'subr-x))

(defconst erc-version "5.6-git"
  "This version of ERC.")

(defvar erc-official-location
  "https://www.gnu.org/software/emacs/erc.html (mailing list: emacs-erc@gnu.org)"
  "Location of the ERC client on the Internet.")

;; Map each :package-version to the associated Emacs version.
;; (This eliminates the need for explicit :version keywords on the
;; custom definitions.)
(add-to-list
 'customize-package-emacs-version-alist
 '(ERC ("5.2" . "22.1")
       ("5.3" . "23.1")
       ("5.4" . "28.1")
       ("5.4.1" . "29.1")
       ("5.5" . "29.1")
       ("5.6" . "30.1")))

(defgroup erc nil
  "Emacs Internet Relay Chat client."
  :link '(url-link "https://www.gnu.org/software/emacs/erc.html")
  :link '(custom-manual "(erc) Top")
  :prefix "erc-"
  :group 'applications)

(defgroup erc-buffers nil
  "Creating new ERC buffers."
  :group 'erc)

(defgroup erc-display nil
  "Settings controlling how various things are displayed.
See the customization group `erc-buffers' for display options
concerning buffers."
  :group 'erc)

(defgroup erc-mode-line-and-header nil
  "Displaying information in the mode-line and header."
  :group 'erc-display)

(defgroup erc-ignore nil
  "Ignoring certain messages."
  :group 'erc)

(defgroup erc-lurker nil
  "Hide specified message types sent by lurkers."
  :version "24.3"
  :group 'erc-ignore)

(defgroup erc-query nil
  "Using separate buffers for private discussions."
  :group 'erc)

(defgroup erc-quit-and-part nil
  "Quitting and parting channels."
  :group 'erc)

(defgroup erc-paranoia nil
  "Know what is sent and received; control the display of sensitive data."
  :group 'erc)

(defgroup erc-scripts nil
  "Running scripts at startup and with /LOAD."
  :group 'erc)

;; Add `custom-loads' features for group symbols missing from a
;; supported Emacs version, possibly because they belong to a new ERC
;; library.  These groups all share their library's feature name.
;;;###autoload(dolist (symbol '( erc-sasl erc-spelling ; 29
;;;###autoload                   erc-imenu erc-nicks)) ; 30
;;;###autoload  (custom-add-load symbol symbol))

(defvar erc-message-parsed) ; only known to this file

(defvar erc--msg-props nil
  "Hash table containing metadata properties for current message.
Provided by the insertion functions `erc-display-message' and
`erc-display-msg' while running their modification hooks.
Initialized when null for each visitation round from function
parameters and environmental factors, as well as the alist
`erc--msg-prop-overrides'.  Keys are symbols.  Values are opaque
objects, unless otherwise specified.  Items present after running
`erc-insert-post-hook' or `erc-send-post-hook' become text
properties added to the first character of an inserted message.
A given message therefore spans the interval extending from one
set of such properties to the newline before the next (or
`erc-insert-marker').  As of ERC 5.6, this forms the basis for
visiting and editing inserted messages.  Modules should align
their markers accordingly.  The following properties have meaning
as of ERC 5.6:

 - `erc--msg': a symbol, guaranteed present; possible values
    include `unknown', a fallback used by `erc-display-message'; a
    catalog key, such as `s401' or `finished'; an
   `erc-display-message' TYPE parameter, like `notice'

 - `erc--cmd': a message's associated IRC command, as read by
   `erc--get-eq-comparable-cmd'; currently either a symbol, like
   `PRIVMSG', or a number, like 5, which represents the numeric
    \"005\"; absent on \"local\" messages, such as simple warnings
    and help text, and on outgoing messages unless echoed back by
    the server (assuming future support)

 - `erc--spkr': a string, the nick of the person speaking

 - `erc--ctcp': a CTCP command, like `ACTION'

 - `erc--ts': a timestamp, possibly provided by the server; as of
    5.6, a ticks/hertz pair on Emacs 29 and above, and a \"list\"
    type otherwise; managed by the `stamp' module

 - `erc--skip': list of symbols known to modules that indicate an
    intent to skip or simplify module-specific processing

 - `erc--ephemeral': a symbol prefixed by or matching a module
    name; indicates to other modules and members of modification
    hooks that the current message should not affect stateful
    operations, such as recording a channel's most recent speaker

 - `erc--hide': a symbol or list of symbols added as an `invisible'
    prop value to the entire message, starting *before* the preceding
    newline and ending before the trailing newline

This is an internal API, and the selection of related helper
utilities is fluid and provisional.  As of ERC 5.6, see the
functions `erc--check-msg-prop' and `erc--get-inserted-msg-prop'.")

(defvar erc--msg-prop-overrides nil
  "Alist of \"message properties\" for populating `erc--msg-props'.
These override any defaults normally shown to modification hooks
by `erc-display-msg' and `erc-display-message'.  Modules should
accommodate existing overrides when applicable.  Items toward the
front shadow any that follow.  Ignored when `erc--msg-props' is
already non-nil.")

;; Forward declarations
(declare-function decoded-time-period "time-date" (time))
(declare-function iso8601-parse-duration "iso8601" (string))
(declare-function word-at-point "thingatpt" (&optional no-properties))
(autoload 'word-at-point "thingatpt") ; for hl-nicks

(declare-function gnutls-negotiate "gnutls" (&rest rest))
(declare-function socks-open-network-stream "socks" (name buffer host service))
(declare-function url-host "url-parse" (cl-x))
(declare-function url-password "url-parse" (cl-x))
(declare-function url-portspec "url-parse" (cl-x))
(declare-function url-type "url-parse" (cl-x))
(declare-function url-user "url-parse" (cl-x))

;; tunable connection and authentication parameters

(defcustom erc-server nil
  "IRC server to use if one is not provided.
See function `erc-compute-server' for more details on connection
parameters and authentication."
  :group 'erc
  :type '(choice (const :tag "None" nil)
                 (string :tag "Server")))

(defcustom erc-port nil
  "IRC port to use if not specified.

This can be either a string or a number."
  :group 'erc
  :type '(choice (const :tag "None" nil)
                 (integer :tag "Port number")
                 (string :tag "Port string")))

(defcustom erc-nick nil
  "Nickname to use if one is not provided.

This can be either a string, or a list of strings.
In the latter case, if the first nick in the list is already in use,
other nicks are tried in the list order.

See function `erc-compute-nick' for more details on connection
parameters and authentication."
  :group 'erc
  :type '(choice (const :tag "None" nil)
                 (string :tag "Nickname")
                 (repeat (string :tag "Nickname"))))

(defcustom erc-nick-uniquifier "`"
  "The string to append to the nick if it is already in use."
  :group 'erc
  :type 'string)

(defcustom erc-try-new-nick-p t
  "Non-nil means attempt to connect with another nickname if nickname unavailable.
You can manually set another nickname with the /NICK command."
  :group 'erc
  :type 'boolean)

(defcustom erc-user-full-name nil
  "User full name.

This can be either a string or a function to call.

See function `erc-compute-full-name' for more details on connection
parameters and authentication."
  :group 'erc
  :type '(choice (const :tag "No name" nil)
                 (string :tag "Name")
                 (function :tag "Get from function"))
  :set (lambda (sym val)
         (set sym (if (functionp val) (funcall val) val))))

(defcustom erc-rename-buffers t
  "Non-nil means rename buffers with network name, if available."
  :version "24.5"
  :group 'erc
  :type 'boolean)

;; For the sake of compatibility, an ID will be created on the user's
;; behalf when `erc-rename-buffers' is nil and one wasn't provided.
;; The name will simply be that of the buffer, usually SERVER:PORT.
;; This violates the policy of treating provided IDs as gospel, but
;; it'll have to do for now.

(make-obsolete-variable 'erc-rename-buffers
                        "old behavior when t now permanent" "29.1")

(defvar erc-password nil
  "Password to use when authenticating to an IRC server interactively.

This variable only exists for legacy reasons.  It's not customizable and
is limited to a single server password.  Users looking for similar
functionality should consider auth-source instead.  See Info
node `(auth) Top' and Info node `(erc) auth-source'.")

(make-obsolete-variable 'erc-password "use auth-source instead" "29.1")

(defcustom erc-user-mode "+i"
  ;; +i "Invisible".  Hides user from global /who and /names.
  "Initial user modes to be set after a connection is established."
  :group 'erc
  :type '(choice (const nil) string function)
  :version "28.1")


(defcustom erc-prompt-for-password t
  "Ask for a server password when invoking `erc-tls' interactively."
  :group 'erc
  :type 'boolean)

(defcustom erc-warn-about-blank-lines t
  "Warn the user if they attempt to send a blank line.
When non-nil, ERC signals a `user-error' upon encountering prompt
input containing empty or whitespace-only lines.  When nil, ERC
still inhibits sending but does so silently.  With the companion
option `erc-send-whitespace-lines' enabled, ERC sends pending
input and prints a message in the echo area indicating the amount
of padding and/or stripping applied, if any.  Setting this option
to nil suppresses such reporting."
  :group 'erc
  :type 'boolean)

(defcustom erc-send-whitespace-lines nil
  "If set to non-nil, send lines consisting of only whitespace."
  :group 'erc
  :type 'boolean)

(defcustom erc-inhibit-multiline-input nil
  "When non-nil, conditionally disallow input consisting of multiple lines.
Issue an error when the number of input lines submitted for
sending meets or exceeds this value.  The value t is synonymous
with a value of 2 and means disallow more than 1 line of input."
  :package-version '(ERC . "5.5")
  :group 'erc
  :type '(choice integer boolean))

(defcustom erc-ask-about-multiline-input nil
  "Whether to ask to ignore `erc-inhibit-multiline-input' when tripped."
  :package-version '(ERC . "5.5")
  :group 'erc
  :type 'boolean)

(defcustom erc-prompt-hidden ">"
  "Text to show in lieu of the prompt when hidden."
  :package-version '(ERC . "5.5")
  :group 'erc-display
  :type 'string)

(defcustom erc-hide-prompt t
  "If non-nil, hide input prompt upon disconnecting.
To unhide, type something in the input area.  Once revealed, a
prompt remains unhidden until the next disconnection.  Channel
prompts are unhidden upon rejoining.  For behavior concerning
query prompts, see `erc-unhide-query-prompt'.  Longtime ERC users
should note that this option was repurposed in ERC 5.5 because it
had lain dormant for years after being sidelined in 5.3 when its
only use in the interactive client was removed.  Before then, its
role was controlling whether `erc-command-indicator' would appear
alongside echoed slash-command lines."
  :package-version '(ERC . "5.5")
  :group 'erc-display
  :type '(choice (const :tag "Always hide prompt" t)
                 (set (const server)
                      (const query)
                      (const channel))))

(defcustom erc-unhide-query-prompt nil
  "When non-nil, always reveal query prompts upon reconnecting.
Otherwise, prompts in a connection's query buffers remain hidden
until the user types in the input area or a new message arrives
from the target."
  :package-version '(ERC . "5.5")
  :group 'erc-display
  ;; Extensions may one day offer a way to discover whether a target
  ;; is online.  When that happens, this can be expanded accordingly.
  :type 'boolean)

;; tunable GUI stuff

(defcustom erc-show-my-nick t
  "If non-nil, display one's own nickname when sending a message.

If non-nil, \"<nickname>\" will be shown.
If nil, only \"> \" will be shown."
  :group 'erc-display
  :type 'boolean)

(define-widget 'erc-message-type 'set
  "A set of standard IRC Message types."
  :args '((const "JOIN")
          (const "KICK")
          (const "NICK")
          (const "PART")
          (const "QUIT")
          (const "MODE")
          (const :tag "Away notices (RPL_AWAY 301)" "301")
          (const :tag "Self back notice (REP_UNAWAY 305)" "305")
          (const :tag "Self away notice (REP_NOWAWAY 306)" "306")
          (const :tag "Channel modes on join (RPL_CHANNELMODEIS 324)" "324")
          (const :tag "Channel creation time (RPL_CREATIONTIME 329)" "329")
          (const :tag "Channel no-topic on join (RPL_NOTOPIC 331)" "331")
          (const :tag "Channel topic on join (RPL_TOPIC 332)" "332")
          (const :tag "Topic author and time on join (RPL_TOPICWHOTIME 333)" "333")
          (const :tag "Invitation success notice (RPL_INVITING 341)" "341")
          (const :tag "Channel member names (353 RPL_NAMEREPLY)" "353")
          (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))

(defcustom erc-hide-list nil
  "A global list of IRC message types to hide.
A typical value would be \(\"JOIN\" \"PART\" \"QUIT\")."
  :group 'erc-ignore
  :type 'erc-message-type)

(defcustom erc-network-hide-list nil
  "A list of IRC networks to hide message types from.
A typical value would be \((\"Libera.Chat\" \"MODE\")
  \(\"OFTC\" \"JOIN\" \"QUIT\"))."
  :version "25.1"
  :group 'erc-ignore
  :type '(alist :key-type string :value-type erc-message-type
                :options ("Libera.Chat")))

(defcustom erc-channel-hide-list nil
  "A list of IRC channels to hide message types from.
A typical value would be \((\"#emacs\" \"QUIT\" \"JOIN\")
  \(\"#erc\" \"NICK\")."
  :version "25.1"
  :group 'erc-ignore
  :type '(alist :key-type string :value-type erc-message-type
                :options ("#emacs")))

(defcustom erc-disconnected-hook nil
  "Run this hook with arguments (NICK IP REASON) when disconnected.
This happens before automatic reconnection.  Note, that
`erc-server-QUIT-functions' might not be run when we disconnect,
simply because we do not necessarily receive the QUIT event."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-complete-functions nil
  "These functions get called when the user hits \\`TAB' in ERC.
Each function in turn is called until one returns non-nil to
indicate it has handled the input."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-join-hook nil
  "Hook run when we join a channel.
Hook functions are called without arguments, with the current
buffer set to the buffer of the new channel.

See also `erc-server-JOIN-functions', `erc-part-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-quit-hook nil
  "Hook run when processing a quit command directed at our nick.

The hook receives one argument, the current PROCESS.
See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-part-hook nil
  "Hook run when processing a PART message directed at our nick.
Called in the server buffer with a single argument: the channel buffer
being parted.  For historical reasons, the buffer argument may be nil if
it's been killed or otherwise no longer exists.  This typically happens
when the \"PART\" response being handled comes by way of a channel
buffer being killed, which, by default, tells `erc-part-channel-on-kill'
to emit a \"PART\".  Users needing to operate on a parted channel buffer
before it's killed in this fashion should use `erc-kill-channel-hook'
and condition their code on `erc-killing-buffer-on-part-p' being nil."
  :group 'erc-hooks
  :type 'hook)

(defvar erc-killing-buffer-on-part-p nil
  "Non-nil when killing a target buffer while handling a \"PART\" response.
Useful for preventing the redundant execution of code designed to run
once when parting a channel.")

(defcustom erc-kick-hook nil
  "Hook run when processing a KICK message directed at our nick.

The hook receives one argument, the current BUFFER.
See also `erc-server-PART-functions' and `erc-part-hook'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-nick-changed-functions nil
  "List of functions run when your nick was successfully changed.

Each function should accept two arguments, NEW-NICK and OLD-NICK."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
  "Hook called just before `erc' calls `erc-connect'.
Functions are passed a buffer as the first argument."
  :group 'erc-hooks
  :type 'hook)

(defvaralias 'erc-channel-users 'erc-channel-members)
(defvar-local erc-channel-members nil
  "Hash table of members in the current channel.
It associates nicknames with cons cells of the form
\(SERVER-USER . MEMBER-DATA), where SERVER-USER is a
`erc-server-user' object and MEMBER-DATA is a `erc-channel-user'
object.  Convenient abbreviations for these two components are
`susr' and `cusr', along with `cmem' for the pair.")

(defvar-local erc-server-users nil
  "Hash table of users on the current server.
It associates nicknames with `erc-server-user' struct instances.")

(defconst erc--casemapping-rfc1459-strict
  (let ((tbl (copy-sequence ascii-case-table))
        (cup (copy-sequence (char-table-extra-slot ascii-case-table 0))))
    (set-char-table-extra-slot tbl 0 cup)
    (set-char-table-extra-slot tbl 1 nil)
    (set-char-table-extra-slot tbl 2 nil)
    (pcase-dolist (`(,uc . ,lc) '((?\[ . ?\{) (?\] . ?\}) (?\\ . ?\|)))
      (aset tbl uc lc)
      (aset tbl lc lc)
      (aset cup uc uc))
    tbl))

(defconst erc--casemapping-rfc1459
  (let ((tbl (copy-sequence erc--casemapping-rfc1459-strict))
        (cup (copy-sequence (char-table-extra-slot
                             erc--casemapping-rfc1459-strict 0))))
    (set-char-table-extra-slot tbl 0 cup)
    (aset tbl ?~ ?^)
    (aset tbl ?^ ?^)
    (aset cup ?~ ?~)
    tbl))

(defun erc-add-server-user (nick user)
  "This function is for internal use only.

Adds USER with nickname NICK to the `erc-server-users' hash table."
  (erc-with-server-buffer
    (puthash (erc-downcase nick) user erc-server-users)))

(defun erc-remove-server-user (nick)
  "This function is for internal use only.

Removes the user with nickname NICK from the `erc-server-users'
hash table.  This user is not removed from the
`erc-channel-users' lists of other buffers.

See also: `erc-remove-user'."
  (erc-with-server-buffer
    (remhash (erc-downcase nick) erc-server-users)))

(defun erc-change-user-nickname (user new-nick)
  "This function is for internal use only.

Changes the nickname of USER to NEW-NICK in the
`erc-server-users' hash table.  The `erc-channel-users' lists of
other buffers are also changed."
  (let ((nick (erc-server-user-nickname user)))
    (setf (erc-server-user-nickname user) new-nick)
    (erc-with-server-buffer
      (remhash (erc-downcase nick) erc-server-users)
      (puthash (erc-downcase new-nick) user erc-server-users))
    (dolist (buf (erc-server-user-buffers user))
      (if (buffer-live-p buf)
          (with-current-buffer buf
            (let ((cdata (erc-get-channel-user nick)))
              (remhash (erc-downcase nick) erc-channel-users)
              (puthash (erc-downcase new-nick) cdata
                       erc-channel-users)))))))

(defun erc-remove-channel-user (nick)
  "This function is for internal use only.

Removes the user with nickname NICK from the `erc-channel-users'
list for this channel.  If this user is not in the
`erc-channel-users' list of any other buffers, the user is also
removed from the server's `erc-server-users' list.

See also: `erc-remove-server-user' and `erc-remove-user'."
  (let ((channel-data (erc-get-channel-user nick)))
    (when channel-data
      (let ((user (car channel-data)))
        (setf (erc-server-user-buffers user)
              (delq (current-buffer)
                    (erc-server-user-buffers user)))
        (remhash (erc-downcase nick) erc-channel-users)
        (if (null (erc-server-user-buffers user))
            (erc-remove-server-user nick))))))

(defun erc-remove-user (nick)
  "This function is for internal use only.

Removes the user with nickname NICK from the `erc-server-users'
list as well as from all `erc-channel-users' lists.

See also: `erc-remove-server-user' and
`erc-remove-channel-user'."
  (let ((user (erc-get-server-user nick)))
    (when user
      (let ((buffers (erc-server-user-buffers user)))
        (dolist (buf buffers)
          (if (buffer-live-p buf)
              (with-current-buffer buf
                (remhash (erc-downcase nick) erc-channel-users)
                (run-hooks 'erc-channel-members-changed-hook)))))
      (erc-remove-server-user nick))))

(defun erc-remove-channel-users ()
  "This function is for internal use only.

Removes all users in the current channel.  This is called by
`erc-server-PART' and `erc-server-QUIT'."
  (when (erc--target-channel-p erc--target)
    (setf (erc--target-channel-joined-p erc--target) nil))
  (when (and erc-server-connected
             (erc-server-process-alive)
             (hash-table-p erc-channel-users))
    (maphash (lambda (nick _cdata)
               (erc-remove-channel-user nick))
             erc-channel-users)
    (clrhash erc-channel-users)))

(defmacro erc--define-channel-user-status-compat-getter (name c d)
  "Define a gv getter for historical `erc-channel-user' status slot NAME.
Expect NAME to be a string, C to be its traditionally associated
letter, and D to be its fallback power-of-2 integer for non-ERC
buffers."
  `(defun ,(intern (concat "erc-channel-user-" name)) (u)
     ,(format "Get equivalent of pre-5.6 `%s' slot for `erc-channel-user'."
              name)
     (declare (gv-setter (lambda (v)
                           (macroexp-let2 nil v v
                             (,'\`(let ((val (erc-channel-user-status ,',u))
                                        (n (or (erc--get-prefix-flag ,c) ,d)))
                                    (setf (erc-channel-user-status ,',u)
                                          (if ,',v
                                              (logior val n)
                                            (logand val (lognot n))))
                                    ,',v))))))
     (let ((n (or (erc--get-prefix-flag ,c) ,d)))
       (= n (logand n (erc-channel-user-status u))))))

(erc--define-channel-user-status-compat-getter "voice"  ?v 1)
(erc--define-channel-user-status-compat-getter "halfop" ?h 2)
(erc--define-channel-user-status-compat-getter "op"     ?o 4)
(erc--define-channel-user-status-compat-getter "admin"  ?a 8)
(erc--define-channel-user-status-compat-getter "owner"  ?q 16)

;; This is a generalized version of the compat-oriented getters above.
(defun erc--cusr-status-p (nick-or-cusr letter)
  "Return non-nil if NICK-OR-CUSR has channel membership status LETTER."
  (and-let* ((cusr (or (and (erc-channel-user-p nick-or-cusr) nick-or-cusr)
                       (cdr (erc-get-channel-member nick-or-cusr))))
             (n (erc--get-prefix-flag letter)))
    (= n (logand n (erc-channel-user-status cusr)))))

(defun erc--cusr-change-status (nick-or-cusr letter enablep &optional resetp)
  "Add or remove membership status associated with LETTER for NICK-OR-CUSR.
With RESETP, clear the user's status info completely.  If ENABLEP
is non-nil, add the status value associated with LETTER."
  (when-let ((cusr (or (and (erc-channel-user-p nick-or-cusr) nick-or-cusr)
                       (cdr (erc-get-channel-member nick-or-cusr))))
             (n (erc--get-prefix-flag letter)))
    (cl-callf (lambda (v)
                (if resetp
                    (if enablep n 0)
                  (if enablep (logior v n) (logand v (lognot n)))))
        (erc-channel-user-status cusr))))

(defun erc-channel-user-owner-p (nick)
  "Return non-nil if NICK is an owner of the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-owner (cdr cdata))))))

(defun erc-channel-user-admin-p (nick)
  "Return non-nil if NICK is an admin in the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-admin (cdr cdata))))))

(defun erc-channel-user-op-p (nick)
  "Return non-nil if NICK is an operator in the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-op (cdr cdata))))))

(defun erc-channel-user-halfop-p (nick)
  "Return non-nil if NICK is a half-operator in the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-halfop (cdr cdata))))))

(defun erc-channel-user-voice-p (nick)
  "Return non-nil if NICK has voice in the current channel."
  (and nick
       (hash-table-p erc-channel-users)
       (let ((cdata (erc-get-channel-user nick)))
         (and cdata (cdr cdata)
              (erc-channel-user-voice (cdr cdata))))))

(defun erc-get-channel-user-list ()
  "Return a list of users in the current channel.
Each element of the list is of the form (USER . CHANNEL-DATA),
where USER is an erc-server-user struct, and CHANNEL-DATA is
either nil or an erc-channel-user struct.

See also: `erc-sort-channel-users-by-activity'."
  (let (users)
    (if (hash-table-p erc-channel-users)
        (maphash (lambda (_nick cdata)
                   (setq users (cons cdata users)))
                 erc-channel-users))
    users))

(defun erc-get-server-nickname-list ()
  "Return a list of known nicknames on the current server."
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
        (maphash (lambda (_n user)
                   (setq nicks
                         (cons (erc-server-user-nickname user)
                               nicks)))
                 erc-server-users)
        nicks))))

(defun erc-get-channel-nickname-list ()
  "Return a list of known nicknames on the current channel."
  (let (nicks)
    (when (hash-table-p erc-channel-users)
      (maphash (lambda (_n cdata)
                 (setq nicks
                       (cons (erc-server-user-nickname (car cdata))
                             nicks)))
               erc-channel-users)
      nicks)))

(defun erc-get-server-nickname-alist ()
  "Return an alist of known nicknames on the current server."
  (erc-with-server-buffer
    (let (nicks)
      (when (hash-table-p erc-server-users)
        (maphash (lambda (_n user)
                   (setq nicks
                         (cons (cons (erc-server-user-nickname user) nil)
                               nicks)))
                 erc-server-users)
        nicks))))

(defun erc-get-channel-nickname-alist ()
  "Return an alist of known nicknames on the current channel."
  (let (nicks)
    (when (hash-table-p erc-channel-users)
      (maphash (lambda (_n cdata)
                 (setq nicks
                       (cons (cons (erc-server-user-nickname (car cdata)) nil)
                             nicks)))
               erc-channel-users)
      nicks)))

(defun erc-sort-channel-users-by-activity (list)
  "Sort LIST such that users which have spoken most recently are listed first.
LIST must be of the form (USER . CHANNEL-DATA).

See also: `erc-get-channel-user-list'."
  (sort list
        (lambda (x y)
          (when (and (cdr x) (cdr y))
            (let ((tx (erc-channel-user-last-message-time (cdr x)))
                  (ty (erc-channel-user-last-message-time (cdr y))))
              (and tx
                   (or (not ty)
                       (time-less-p ty tx))))))))

(defun erc-sort-channel-users-alphabetically (list)
  "Sort LIST so that users' nicknames are in alphabetical order.
LIST must be of the form (USER . CHANNEL-DATA).

See also: `erc-get-channel-user-list'."
  (sort list
        (lambda (x y)
          (when (and (cdr x) (cdr y))
            (let ((nickx (downcase (erc-server-user-nickname (car x))))
                  (nicky (downcase (erc-server-user-nickname (car y)))))
              (and nickx
                   (or (not nicky)
                       (string-lessp nickx nicky))))))))

(defvar-local erc-channel-topic nil
  "A topic string for the channel.  Should only be used in channel-buffers.")

(defvar-local erc-channel-modes nil
  "List of letters, as strings, representing channel modes.
For example, (\"i\" \"m\" \"s\").  Modes that take accompanying
parameters are not included.")

(defvar-local erc-insert-marker nil
  "The place where insertion of new text in erc buffers should happen.")

(defvar-local erc-input-marker nil
  "The marker where input should be inserted.")

(defun erc-string-no-properties (string)
  "Return a copy of STRING will all text-properties removed."
  (let ((newstring (copy-sequence string)))
    (set-text-properties 0 (length newstring) nil newstring)
    newstring))

(defcustom erc-prompt "ERC>"
  "Prompt used by ERC.  Trailing whitespace is not required."
  :group 'erc-display
  :type '(choice string
                 (function-item :tag "Interpret format specifiers"
                                erc-prompt-format)
                 function))

(defvar erc--prompt-format-face-example
  #("%p%m%a\u00b7%b>"
    0 2 (font-lock-face erc-my-nick-prefix-face)
    2 4 (font-lock-face font-lock-keyword-face)
    4 6 (font-lock-face erc-error-face)
    6 7 (font-lock-face shadow)
    7 9 (font-lock-face font-lock-constant-face)
    9 10 (font-lock-face shadow))
  "An example value for option `erc-prompt-format' with faces.")

(defcustom erc-prompt-format erc--prompt-format-face-example
  "Format string when `erc-prompt' is `erc-prompt-format'.
ERC recognizes these substitution specifiers:

 %a - away indicator
 %b - buffer name
 %t - channel or query target, server domain, or dialed address
 %S - target@network or buffer name
 %s - target@server or server
 %N - current network, like Libera.Chat
 %p - channel membership prefix, like @ or +
 %n - current nickname
 %c - channel modes with args for select modes
 %C - channel modes with all args
 %u - user modes
 %m - channel modes sans args in channels, user modes elsewhere
 %M - like %m but show nothing in query buffers

To pick your own colors, do something like:

  (setopt erc-prompt-format
          (concat
           (propertize \"%b\" \\='font-lock-face \\='erc-input-face)
           (propertize \"%a\" \\='font-lock-face \\='erc-error-face)))

Please remember that ERC ignores this option completely unless
the \"parent\" option `erc-prompt' is set to `erc-prompt-format'."
  :package-version '(ERC . "5.6")
  :group 'erc-display
  :type `(choice (const :tag "{Prefix}{Mode}{Away}{MIDDLE DOT}{Buffer}>"
                        ,erc--prompt-format-face-example)
                 string))

(defun erc-prompt-format ()
  "Make predefined `format-spec' substitutions.

See option `erc-prompt-format' and option `erc-prompt'."
  (format-spec erc-prompt-format
               (erc-compat--defer-format-spec-in-buffer
                (?C erc--channel-modes 3 ",")
                (?M erc--format-modes 'no-query-p)
                (?N erc-format-network)
                (?S erc-format-target-and/or-network)
                (?a erc--format-away-indicator)
                (?b buffer-name)
                (?c erc-format-channel-modes)
                (?m erc--format-modes)
                (?n erc-current-nick)
                (?p erc--format-channel-status-prefix)
                (?s erc-format-target-and/or-server)
                (?t erc-format-target)
                (?u erc--format-user-modes))
               'ignore-missing)) ; formerly `only-present'

(defun erc-prompt ()
  "Return the input prompt as a string.

See also the variable `erc-prompt'."
  (let ((prompt (if (functionp erc-prompt)
                    (funcall erc-prompt)
                  erc-prompt)))
    (if (> (length prompt) 0)
        (concat prompt " ")
      prompt)))

(defcustom erc-notice-prefix "*** "
  "Prefix for all notices."
  :group 'erc-display
  :type 'string)

(defcustom erc-notice-highlight-type 'all
  "Determines how to highlight notices.
See `erc-notice-prefix'.

The following values are allowed:

    `prefix' - highlight notice prefix only
    `all'    - highlight the entire notice

Any other value disables notice's highlighting altogether."
  :group 'erc-display
  :type '(choice (const :tag "highlight notice prefix only" prefix)
                 (const :tag "highlight the entire notice" all)
                 (const :tag "don't highlight notices at all" nil)))

(defcustom erc-echo-notice-hook nil
  "List of functions to call to echo a private notice.
Each function is called with four arguments, the string
to display, the parsed server message, the target buffer (or
nil), and the sender.  The functions are called in order, until a
function evaluates to non-nil.  These hooks are called after
those specified in `erc-echo-notice-always-hook'.

See also: `erc-echo-notice-always-hook',
`erc-echo-notice-in-default-buffer',
`erc-echo-notice-in-target-buffer',
`erc-echo-notice-in-minibuffer',
`erc-echo-notice-in-server-buffer',
`erc-echo-notice-in-active-non-server-buffer',
`erc-echo-notice-in-active-buffer',
`erc-echo-notice-in-user-buffers',
`erc-echo-notice-in-user-and-target-buffers',
`erc-echo-notice-in-first-user-buffer'."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-echo-notice-in-default-buffer
             erc-echo-notice-in-target-buffer
             erc-echo-notice-in-minibuffer
             erc-echo-notice-in-server-buffer
             erc-echo-notice-in-active-non-server-buffer
             erc-echo-notice-in-active-buffer
             erc-echo-notice-in-user-buffers
             erc-echo-notice-in-user-and-target-buffers
             erc-echo-notice-in-first-user-buffer))

(defcustom erc-echo-notice-always-hook
  '(erc-echo-notice-in-default-buffer)
  "List of functions to call to echo a private notice.
Each function is called with four arguments, the string
to display, the parsed server message, the target buffer (or
nil), and the sender.  The functions are called in order, and all
functions are called.  These hooks are called before those
specified in `erc-echo-notice-hook'.

See also: `erc-echo-notice-hook',
`erc-echo-notice-in-default-buffer',
`erc-echo-notice-in-target-buffer',
`erc-echo-notice-in-minibuffer',
`erc-echo-notice-in-server-buffer',
`erc-echo-notice-in-active-non-server-buffer',
`erc-echo-notice-in-active-buffer',
`erc-echo-notice-in-user-buffers',
`erc-echo-notice-in-user-and-target-buffers',
`erc-echo-notice-in-first-user-buffer'."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-echo-notice-in-default-buffer
             erc-echo-notice-in-target-buffer
             erc-echo-notice-in-minibuffer
             erc-echo-notice-in-server-buffer
             erc-echo-notice-in-active-non-server-buffer
             erc-echo-notice-in-active-buffer
             erc-echo-notice-in-user-buffers
             erc-echo-notice-in-user-and-target-buffers
             erc-echo-notice-in-first-user-buffer))

;; other tunable parameters

(defcustom erc-whowas-on-nosuchnick nil
  "If non-nil, do a whowas on a nick if no such nick."
  :group 'erc
  :type 'boolean)

(defcustom erc-verbose-server-ping nil
  "If non-nil, show every time you get a PING or PONG from the server."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-public-away-p nil
  "Let others know you are back when you are no longer marked away.
This happens in this form:
* <nick> is back (gone for <time>)

Many consider it impolite to do so automatically."
  :group 'erc
  :type 'boolean)

(defcustom erc-away-nickname nil
  "The nickname to take when you are marked as being away."
  :group 'erc
  :type '(choice (const nil)
                 string))

(defcustom erc-paranoid nil
  "If non-nil, then all incoming CTCP requests will be shown."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-disable-ctcp-replies nil
  "Disable replies to CTCP requests that require a reply.
If non-nil, then all incoming CTCP requests that normally require
an automatic reply (like VERSION or PING) will be ignored.  Good to
set if some hacker is trying to flood you away."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-anonymous-login t
  "Be paranoid, don't give away your machine name."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-prompt-for-channel-key nil
  "Prompt for channel key when using `erc-join-channel' interactively."
  :group 'erc
  :type 'boolean)

(defcustom erc-email-userid "user"
  "Use this as your email user ID."
  :group 'erc
  :type 'string)

(defcustom erc-system-name nil
  "Use this as the name of your system.
If nil, ERC will call function `system-name' to get this information."
  :group 'erc
  :type '(choice (const :tag "Default system name" nil)
                 string))

(defcustom erc-ignore-list nil
  "List of regexps matching user identifiers to ignore.

A user identifier has the form \"nick!login@host\".  If an
identifier matches, the message from the person will not be
processed."
  :group 'erc-ignore
  :type '(repeat regexp))
(make-variable-buffer-local 'erc-ignore-list)

(defcustom erc-ignore-reply-list nil
  "List of regexps matching user identifiers to ignore completely.

This differs from `erc-ignore-list' in that it also ignores any
messages directed at the user.

A user identifier has the form \"nick!login@host\".

If an identifier matches, or a message is addressed to a nick
whose identifier matches, the message will not be processed.

CAVEAT: ERC doesn't know about the user and host of anyone who
was already in the channel when you joined, but never said
anything, so it won't be able to match the user and host of those
people.  You can update the ERC internal info using /WHO *."
  :group 'erc-ignore
  :type '(repeat regexp))

(defvar erc-flood-protect t
  "If non-nil, flood protection is enabled.
Flooding is sending too much information to the server in too
short of an interval, which may cause the server to terminate the
connection.

Note that older code conflated rate limiting and line splitting.
Starting in ERC 5.6, this option no longer influences the latter.

See `erc-server-flood-margin' for other flood-related parameters.")

;; Script parameters

(defcustom erc-startup-file-list
  (list (locate-user-emacs-file ".ercrc.el")
        (locate-user-emacs-file ".ercrc")
        "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
  "List of files to try for a startup script.
The first existent and readable one will get executed.

If the filename ends with `.el' it is presumed to be an Emacs Lisp
script and it gets (load)ed.  Otherwise it is treated as a bunch of
regular IRC commands."
  :group 'erc-scripts
  :type '(repeat file))

(defcustom erc-script-path nil
  "List of directories to look for a script in /load command.
The script is first searched in the current directory, then in each
directory in the list."
  :group 'erc-scripts
  :type '(repeat directory))

(defcustom erc-script-echo t
  "If non-nil, echo the IRC script commands locally."
  :group 'erc-scripts
  :type 'boolean)

(defvar-local erc-last-saved-position nil
  "A marker containing the position the current buffer was last saved at.")

(defcustom erc-kill-buffer-on-part nil
  "Kill the channel buffer on PART.
Nil by default because ERC can reuse buffers later re-joined."
  :group 'erc-quit-and-part
  :type 'boolean)

(defcustom erc-kill-queries-on-quit nil
  "Kill all query (also channel) buffers of this server on QUIT.
See the variable `erc-kill-buffer-on-part' for details."
  :group 'erc-quit-and-part
  :type 'boolean)

(defcustom erc-kill-server-buffer-on-quit nil
  "Kill the server buffer of the process on QUIT."
  :group 'erc-quit-and-part
  :type 'boolean)

(defcustom erc-quit-reason-various-alist nil
  "Alist of possible arguments to the /quit command.

Each element has the form:
  (REGEXP RESULT)

If REGEXP matches the argument to /quit, then its relevant RESULT
will be used.  RESULT may be either a string, or a function.  If
a function, it should return the quit message as a string.

If no elements match, then the empty string is used.

As an example:
  (setq erc-quit-reason-various-alist
      \\='((\"xmms\" dme:now-playing)
        (\"version\" erc-quit-reason-normal)
        (\"home\" \"Gone home !\")
        (\"^$\" \"Default Reason\")))
If the user types \"/quit home\", then \"Gone home !\" will be used
as the quit message."
  :group 'erc-quit-and-part
  :type '(repeat (list regexp (choice (string) (function)))))

(defcustom erc-part-reason-various-alist nil
  "Alist of possible arguments to the /part command.

Each element has the form:
  (REGEXP RESULT)

If REGEXP matches the argument to /part, then its relevant RESULT
will be used.  RESULT may be either a string, or a function.  If
a function, it should return the part message as a string.

If no elements match, then the empty string is used.

As an example:
  (setq erc-part-reason-various-alist
      \\='((\"xmms\" dme:now-playing)
        (\"version\" erc-part-reason-normal)
        (\"home\" \"Gone home !\")
        (\"^$\" \"Default Reason\")))
If the user types \"/part home\", then \"Gone home !\" will be used
as the part message."
  :group 'erc-quit-and-part
  :type '(repeat (list regexp (choice (string) (function)))))

(defcustom erc-quit-reason 'erc-quit-reason-normal
  "A function which returns the reason for quitting.

The function is passed a single argument, the string typed by the
user after \"/quit\"."
  :group 'erc-quit-and-part
  :type '(choice (const erc-quit-reason-normal)
                 (const erc-quit-reason-various)
                 (symbol)))

(defcustom erc-part-reason 'erc-part-reason-normal
  "A function which returns the reason for parting a channel.

The function is passed a single argument, the string typed by the
user after \"/PART\"."
  :group 'erc-quit-and-part
  :type '(choice (const erc-part-reason-normal)
                 (const erc-part-reason-various)
                 (symbol)))

(defvar erc-grab-buffer-name "*erc-grab*"
  "The name of the buffer created by `erc-grab-region'.")

;; variables available for IRC scripts

(defvar erc-user-information "ERC User"
  "USER_INFORMATION IRC variable.")

;; Hooks

(defgroup erc-hooks nil
  "Hooks for ERC.
Users of the interactive client should be aware that many of
these hooks have names predating the modern convention of
conveying abnormality via the \"-function\" suffix.  Users should
likewise be aware that built-in and third-party modules use these
hooks as well, and some of their variables may be buffer-local in
particular sessions and/or `let'-bound for spells."
  :group 'erc)

(defcustom erc-mode-hook nil
  "Hook run after `erc-mode' setup is finished."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-add-scroll-to-bottom))

(defcustom erc-timer-hook nil
  "Abnormal hook run after each response handler.
Called with a float returned from `erc-current-time'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-insert-pre-hook nil
  "Hook called first when some text is inserted through `erc-display-line'.
It gets called with one argument, STRING.
To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
Filtering functions can set `erc-insert-this' to nil to avoid
display of that particular string at all."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-send-pre-hook nil
  "Hook called first when some text is sent through `erc-send-current-line'.
It gets called with one argument, STRING.

To change the text that will be sent, set the variable `str' which is
used in `erc-send-current-line'.

To change the text inserted into the buffer without changing the text
that will be sent, use `erc-send-modify-hook' instead.

Filtering functions can set `erc-send-this' to nil to avoid sending of
that particular string at all and `erc-insert-this' to prevent
inserting that particular string into the buffer.

Note that it's useless to set `erc-send-this' to nil and
`erc-insert-this' to t.  ERC is sane enough to not insert the text
anyway."
  :group 'erc-hooks
  :type 'hook)
(make-obsolete-variable 'erc-send-pre-hook 'erc-pre-send-functions "27.1")

(defcustom erc-pre-send-functions nil
  "Special hook to possibly alter the string to send and insert.
ERC calls the member functions with one argument, an `erc-input'
struct instance to modify as needed.

The struct has five slots:

  `string': String to send, originally from prompt input.
  `insertp': Whether a string should be inserted in the buffer.
  `sendp': Whether `string' should be sent to the IRC server.
  `substxt': String to display (but not send) instead of `string'.
  `refoldp': Whether to re-split `string' per protocol limits.

This hook runs after protocol line splitting has taken place, so
the value of `string' comes \"pre-split\" according to the option
`erc-split-line-length'.  If you need ERC to refill the entire
payload before sending it, set the `refoldp' slot to a non-nil
value.  Note that this refilling is only a convenience, and
modules with special needs, such as preserving \"preformatted\"
text or encoding for subprotocol \"tunneling\", should handle
splitting manually and possibly also specify replacement text to
display via the `substxt' slot."
  :package-version '(ERC . "5.3")
  :group 'erc-hooks
  :type 'hook)

(define-obsolete-variable-alias 'erc--pre-send-split-functions
  'erc--input-review-functions "30.1")
(defvar erc--input-review-functions '(erc--split-lines
                                      erc--run-input-validation-checks
                                      erc--discard-trailing-multiline-nulls
                                      erc--inhibit-slash-cmd-insertion)
  "Special hook for reviewing and modifying prompt input.
ERC runs this before clearing the prompt and before running any
send-related hooks, such as `erc-pre-send-functions'.  Thus, it's
quite \"safe\" to bail out of this hook with a `user-error', if
necessary.  The hook's members are called with one argument, an
`erc--input-split' struct, which they can optionally modify.

The struct has five slots:

  `string': the original input as a read-only reference
  `insertp': same as in `erc-pre-send-functions'
  `sendp': same as in `erc-pre-send-functions'
  `refoldp': same as in `erc-pre-send-functions'
  `lines': a list of lines to be sent, each one a `string'
  `cmdp': whether to interpret input as a command, like /ignore

When `cmdp' is non-nil, all but the first line will be discarded.")

(defvar erc-insert-this t
  "Insert the text into the target buffer or not.
Functions on `erc-insert-pre-hook' can set this variable to nil
if they wish to avoid insertion of a particular string.")

(defvar erc-send-this t
  "Send the text to the target or not.
Functions on `erc-send-pre-hook' can set this variable to nil
if they wish to avoid sending of a particular string.")
(make-obsolete-variable 'erc-send-this 'erc-pre-send-functions "27.1")

(defcustom erc-insert-modify-hook ()
  "Insertion hook for functions that will change the text's appearance.
This hook is called just after `erc-insert-pre-hook' when the value
of `erc-insert-this' is t.

ERC runs this hook with the buffer narrowed to the bounds of the
inserted message plus a trailing newline.  Built-in modules place
their hook members in two depth ranges: the first between -80 and
-20 and the second between 20 and 80.  Use the functions
`erc-find-parsed-property' and `erc-get-parsed-vector' to locate
and extract the `erc-response' object for the inserted message."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-insert-post-hook nil
  "This hook is called just after `erc-insert-modify-hook'.
At this point, all modifications from prior hook functions are done."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-truncate-buffer
             erc-make-read-only
             erc-save-buffer-in-logs))

(defcustom erc-insert-done-hook nil
  "This hook is called after inserting strings into the buffer.
This hook is not called from inside `save-excursion' and should
preserve point if needed."
  :group 'erc-hooks
  :version "27.1"
  :type 'hook)

(defcustom erc-send-modify-hook nil
  "Sending hook for functions that will change the text's appearance.
ERC runs this just after `erc-pre-send-functions' if its shared
`erc-input' object's `sendp' and `insertp' slots remain non-nil.
While this hook is run, narrowing is in effect and `current-buffer' is
the buffer where the text got inserted.

Note that no function in this hook can change the appearance of the
text that is sent.  Only changing the sent text's appearance on the
sending user's screen is possible.  One possible value to add here
is `erc-fill'."
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-send-post-hook nil
  "This hook is called just after `erc-send-modify-hook'.
At this point, all modifications from prior hook functions are done.
NOTE: The functions on this hook are called _before_ sending a command
to the server.

This function is called with narrowing, ala `erc-send-modify-hook'."
  :group 'erc-hooks
  :type 'hook
  :options '(erc-make-read-only))

(defcustom erc-send-completed-hook
  (when (fboundp 'emacspeak-auditory-icon)
    (list (byte-compile
           (lambda (_str)
             (emacspeak-auditory-icon 'select-object)))))
  "Hook called after a message has been parsed by ERC.

The single argument to the functions is the unmodified string
which the local user typed."
  :group 'erc-hooks
  :type 'hook)
;; mode-specific tables

(defvar erc-mode-syntax-table
  (let ((syntax-table (make-syntax-table)))
    (modify-syntax-entry ?\" ".   " syntax-table)
    (modify-syntax-entry ?\\ ".   " syntax-table)
    (modify-syntax-entry ?' "w   " syntax-table)
    ;; Make dabbrev-expand useful for nick names
    (modify-syntax-entry ?< "." syntax-table)
    (modify-syntax-entry ?> "." syntax-table)
    syntax-table)
  "Syntax table used while in ERC mode.")

(defvar erc-mode-abbrev-table nil
  "Abbrev table used while in ERC mode.")
(define-abbrev-table 'erc-mode-abbrev-table ())

(defvar erc-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\C-m" #'erc-send-current-line)
    (define-key map "\C-a" #'erc-bol)
    (define-key map [home] #'erc-bol)
    (define-key map "\C-c\C-a" #'erc-bol)
    (define-key map "\C-c\C-b" #'erc-switch-to-buffer)
    (define-key map "\C-c\C-d" #'erc-input-action)
    (define-key map "\C-c\C-e" #'erc-toggle-ctcp-autoresponse)
    (define-key map "\C-c\C-f" #'erc-toggle-flood-control)
    (define-key map "\C-c\C-i" #'erc-invite-only-mode)
    (define-key map "\C-c\C-j" #'erc-join-channel)
    (define-key map "\C-c\C-n" #'erc-channel-names)
    (define-key map "\C-c\C-o" #'erc-get-channel-mode-from-keypress)
    (define-key map "\C-c\C-p" #'erc-part-from-channel)
    (define-key map "\C-c\C-q" #'erc-quit-server)
    (define-key map "\C-c\C-r" #'erc-remove-text-properties-region)
    (define-key map "\C-c\C-t" #'erc-set-topic)
    (define-key map "\C-c\C-u" #'erc-kill-input)
    (define-key map "\C-c\C-x" #'erc-quit-server)
    (define-key map "\M-\t" #'ispell-complete-word)
    (define-key map "\t" #'erc-tab)

    ;; Suppress `font-lock-fontify-block' key binding since it
    ;; destroys face properties.
    (define-key map [remap font-lock-fontify-block] #'undefined)

    map)
  "ERC keymap.")

(defun erc--modify-local-map (mode &rest bindings)
  "Modify `erc-mode-map' on behalf of a global module.
Add or remove `key-valid-p' BINDINGS when toggling MODE."
  (declare (indent 1))
  (while (pcase-let* ((`(,key ,def . ,rest) bindings)
                      (existing (keymap-lookup erc-mode-map key)))
           (if mode
               (when (or (not existing) (eq existing #'undefined))
                 (keymap-set erc-mode-map key def))
             (when (eq existing def)
               (keymap-unset erc-mode-map key t)))
           (setq bindings rest))))

;; Faces

; Honestly, I have a horrible sense of color and the "defaults" below
; are supposed to be really bad. But colors ARE required in IRC to
; convey different parts of conversation. If you think you know better
; defaults - send them to me.

;; Now colors are a bit nicer, at least to my eyes.
;; You may still want to change them to better fit your background.-- S.B.

(defgroup erc-faces nil
  "Faces for ERC."
  :group 'erc)

;; FIXME faces should not end in "-face".
(defface erc-default-face '((t))
  "ERC default face."
  :group 'erc-faces)

(defface erc-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
  "ERC face used for user mode prefix."
  :group 'erc-faces)

(defface erc-my-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
  "ERC face used for my user mode prefix."
  :group 'erc-faces)

(defface erc-direct-msg-face '((t :foreground "IndianRed"))
  "ERC face used for messages you receive in the main erc buffer."
  :group 'erc-faces)

(defface erc-header-line
  '((t :inherit header-line))
  "ERC face used for the header line.

This will only be used if `erc-header-line-face-method' is non-nil."
  :group 'erc-faces)

(defface erc-input-face '((t :foreground "brown"))
  "ERC face used for your input."
  :group 'erc-faces)

(defface erc-prompt-face
  '((t :weight bold :foreground "Black" :background "lightBlue2"))
  "ERC face for the prompt."
  :group 'erc-faces)

(defface erc-notice-face
  '((default :weight bold)
    (((class color) (min-colors 88) (supports :weight semi-bold))
     :weight semi-bold :foreground "SlateBlue")
    (((class color) (min-colors 88)) :foreground "SlateBlue")
    (t :foreground "blue"))
  "ERC face for notices."
  :package-version '(ERC . "5.6")
  :group 'erc-faces)

(defface erc-action-face '((((supports :weight semi-bold)) :weight semi-bold)
                           (t :weight bold))
  "ERC face for actions generated by /ME."
  :package-version '(ERC . "5.6")
  :group 'erc-faces)

(defface erc-error-face '((t :foreground "red"))
  "ERC face for errors."
  :group 'erc-faces)

;; same default color as `erc-input-face'
(defface erc-my-nick-face '((t :weight bold :foreground "brown"))
  "ERC face for your current nickname in messages sent by you.
See also `erc-show-my-nick'."
  :group 'erc-faces)

(defface erc-nick-default-face '((t :weight bold))
  "ERC nickname default face."
  :group 'erc-faces)

(defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
  "ERC nickname face for private messages."
  :group 'erc-faces)

;; Debugging support

;; FIXME if this variable plays some role, indicate that here.
;; Otherwise, deprecate.
(defvar erc-debug-log-file (expand-file-name "ERC.debug")
  "Debug log file name.")

(defvar-local erc-dbuf nil)

;; See comments in `erc-scenarios-base-local-modules' explaining why
;; this is insufficient as a public interface.

(defvar erc--target-priors nil
  "Analogous to `erc--server-reconnecting' but for target buffers.
Bound to local variables from an existing (logical) session's
buffer during local-module setup and `erc-mode-hook' activation.")

(defmacro erc--restore-initialize-priors (mode &rest vars)
  "Restore local VARS for local minor MODE from a previous session."
  (declare (indent 1))
  (let ((priors (make-symbol "priors"))
        (initp (make-symbol "initp"))
        ;;
        forms)
    (while-let ((k (pop vars)))
      (push `(,k (if ,initp (alist-get ',k ,priors) ,(pop vars))) forms))
    `(let* ((,priors (or erc--server-reconnecting erc--target-priors))
            (,initp (and ,priors (alist-get ',mode ,priors))))
       (unless (local-variable-if-set-p ',mode)
         (error "Not a local minor mode var: %s" ',mode))
       (setq ,@(mapcan #'identity (nreverse forms))))))

(defun erc--target-from-string (string)
  "Construct an `erc--target' variant from STRING."
  (funcall (if (erc-channel-p string)
               (if (erc--valid-local-channel-p string)
                   #'make-erc--target-channel-local
                 #'make-erc--target-channel)
             #'make-erc--target)
           :string string :symbol (intern (erc-downcase string))))

(defun erc-once-with-server-event (event f)
  "Run function F the next time EVENT occurs in the `current-buffer'.

You should make sure that `current-buffer' is a server buffer.

This function temporarily adds a function to EVENT's hook to call F with
two arguments (`proc' and `parsed').  After F is called, the function is
removed from EVENT's hook.  F should return either nil
or t, where nil indicates that the other functions on EVENT's hook
should be run too, and t indicates that other functions should
not be run.

Please be sure to use this function in server-buffers.  In
channel-buffers it may not work at all, as it uses the LOCAL
argument of `add-hook' and `remove-hook' to ensure multiserver
capabilities."
  (unless (erc--server-buffer-p)
    (error
     "You should only run `erc-once-with-server-event' in a server buffer"))
  (let ((fun (make-symbol "fun"))
        (hook (erc-get-hook event)))
    (put fun 'erc-original-buffer (current-buffer))
    (fset fun (lambda (proc parsed)
                (with-current-buffer (get fun 'erc-original-buffer)
                  (remove-hook hook fun t))
                (fmakunbound fun)
                (funcall f proc parsed)))
    (add-hook hook fun -95 t)
    fun))

(defun erc--warn-once-before-connect (mode-var &rest args)
  "Display an \"error notice\" once.
Expect ARGS to be `erc-button--display-error-notice-with-keys'
compatible parameters, except without any leading buffers or
processes.  If we're in an ERC buffer with a network process when
called, print the notice immediately.  Otherwise, if we're in a
server buffer, arrange to do so after local modules have been set
up and mode hooks have run.  Otherwise, if MODE-VAR is a global
module, try again at most once the next time `erc-mode-hook'
runs."
  (declare (indent 1))
  (cl-assert (stringp (car args)))
  (if (derived-mode-p 'erc-mode)
      (unless (or (erc-with-server-buffer ; needs `erc-server-process'
                    (apply #'erc-button--display-error-notice-with-keys
                           (current-buffer) args)
                    t)
                  erc--target) ; unlikely
        (let (hook)
          (setq hook
                (lambda (_)
                  (remove-hook 'erc-connect-pre-hook hook t)
                  (apply #'erc-button--display-error-notice-with-keys args)))
          (add-hook 'erc-connect-pre-hook hook nil t)))
    (when (custom-variable-p mode-var)
      (let (hook)
        (setq hook (lambda ()
                     (remove-hook 'erc-mode-hook hook)
                     (apply #'erc--warn-once-before-connect 'erc-fake args)))
        (add-hook 'erc-mode-hook hook)))))

(defun erc-server-buffer ()
  "Return the server buffer for the current buffer's process.
The buffer-local variable `erc-server-process' is used to find
the process buffer."
  (and (erc-server-buffer-live-p)
       (process-buffer erc-server-process)))

(defun erc-server-buffer-live-p ()
  "Return t if the server buffer has not been killed."
  (and (processp erc-server-process)
       (buffer-live-p (process-buffer erc-server-process))))

(define-obsolete-function-alias
  'erc-server-buffer-p 'erc-server-or-unjoined-channel-buffer-p "30.1")
(defun erc-server-or-unjoined-channel-buffer-p (&optional buffer)
  "Return non-nil if argument BUFFER is an ERC server buffer.
If BUFFER is nil, use the current buffer.  For historical
reasons, also return non-nil for channel buffers the client has
parted or been kicked from."
  (with-current-buffer (or buffer (current-buffer))
    (and (eq major-mode 'erc-mode)
         (null (erc-default-target)))))

(defun erc--server-buffer-p (&optional buffer)
  "Return non-nil if BUFFER is an ERC server buffer.
Without BUFFER, use the current buffer."
  (if buffer
      (with-current-buffer buffer
        (and (eq major-mode 'erc-mode) (null erc--target)))
    (and (eq major-mode 'erc-mode) (null erc--target))))

(defun erc-open-server-buffer-p (&optional buffer)
  "Return non-nil if BUFFER is an ERC server buffer with an open IRC process.

If BUFFER is nil, the current buffer is used."
  (and (erc--server-buffer-p buffer)
       (erc-server-process-alive buffer)))

(defun erc-query-buffer-p (&optional buffer)
  "Return non-nil if BUFFER is an ERC query buffer.
If BUFFER is nil, use the current buffer."
  (and-let* ((target (if buffer
                         (progn (when (stringp buffer)
                                  (setq buffer (get-buffer buffer)))
                                (buffer-local-value 'erc--target buffer))
                       erc--target)))
    (not (erc--target-channel-p target))))

(defun erc-ison-p (nick)
  "Return non-nil if NICK is online."
  (interactive "sNick: ")
  (erc-with-server-buffer
    (let ((erc-online-p 'unknown))
      (erc-once-with-server-event
       303
       (lambda (_proc parsed)
         (let ((ison (split-string (aref parsed 3))))
           (setq erc-online-p (car (erc-member-ignore-case nick ison)))
           t)))
      (erc-server-send (format "ISON %s" nick))
      (while (eq erc-online-p 'unknown) (accept-process-output))
      (if (called-interactively-p 'interactive)
          (message "%s is %sonline"
                   (or erc-online-p nick)
                   (if erc-online-p "" "not "))
        erc-online-p))))



;; Last active buffer, to print server messages in the right place

(defvar-local erc-active-buffer nil
  "The current active buffer, the one where the user typed the last command.
Defaults to the server buffer, and should only be set in the
server buffer.")

(defun erc-active-buffer ()
  "Return the value of `erc-active-buffer' for the current server.
Defaults to the server buffer."
  (erc-with-server-buffer
    (if (buffer-live-p erc-active-buffer)
        erc-active-buffer
      (setq erc-active-buffer (current-buffer)))))

(defun erc-set-active-buffer (buffer)
  "Set the value of `erc-active-buffer' to BUFFER."
  (cond ((erc-server-buffer)
         (with-current-buffer (erc-server-buffer)
           (setq erc-active-buffer buffer)))
        (t (setq erc-active-buffer buffer))))

;; Mode activation routines

(define-derived-mode erc-mode fundamental-mode "ERC"
  "Major mode for Emacs IRC."
  :interactive nil
  (setq local-abbrev-table erc-mode-abbrev-table)
  (setq-local next-line-add-newlines nil)
  (setq line-move-ignore-invisible t)
  (setq-local paragraph-separate
              (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
  (setq-local paragraph-start
              (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
  (setq-local completion-ignore-case t)
  (add-hook 'post-command-hook #'erc-check-text-conversion nil t)
  (add-hook 'kill-buffer-hook #'erc-kill-buffer-function nil t)
  (add-hook 'completion-at-point-functions #'erc-complete-word-at-point nil t))

;; activation

(defconst erc-default-server "irc.libera.chat"
  "IRC server to use if it cannot be detected otherwise.")

(defvar erc-default-port 6667
  "IRC port to use if it cannot be detected otherwise.")

(defconst erc-default-port-tls 6697
  "IRC port to use for encrypted connections if it cannot be \
detected otherwise.")

(defconst erc--buffer-display-choices
  `(choice (const :tag "Use value of `erc-buffer-display'" nil)
           (const :tag "Split window and select" window)
           (const :tag "Split window but don't select" window-noselect)
           (const :tag "New frame" frame)
           (const :tag "Don't display" bury)
           (const :tag "Use current window" buffer)
           (choice :tag "Defer to a display function"
                   (function-item display-buffer)
                   (function-item pop-to-buffer)
                   (function :tag "User-defined")))
  "Common choices for buffer-display options.")

(defvaralias 'erc-join-buffer 'erc-buffer-display)
(defcustom erc-buffer-display 'bury
  "How to display a newly created ERC buffer.
This determines ERC's baseline, \"catch-all\" buffer-display
behavior.  It takes a backseat to more specific options, like
`erc-interactive-display', `erc-auto-reconnect-display', and
`erc-receive-query-display'.

The available choices are:

  `window'          - in another window,
  `window-noselect' - in another window, but don't select that one,
  `frame'           - in another frame,
  `bury'            - bury it in a new buffer,
  `buffer'          - in place of the current buffer,
  DISPLAY-FUNCTION  - a `display-buffer'-like function

Here, DISPLAY-FUNCTION should accept a buffer and an ACTION of
the kind described by the Info node `(elisp) Choosing Window'.
At times, ERC may add hints about the calling context to the
ACTION's alist.  Keys are symbols such as user options, like
`erc-buffer-display', or module minor modes, like
`erc-autojoin-mode'.  Values are non-nil constants specific to
each.  For this particular option, possible values include the
symbols

  `JOIN', `PRIVMSG', `NOTICE', `erc', and `erc-tls'.

The first three signify IRC commands received from the server and
the rest entry-point commands responsible for the connection.
When dealing with the latter two, users may prefer to set this
option to `bury' and instead call DISPLAY-FUNCTION directly
on (server) buffers returned by these entry points because the
context leading to their creation is plainly obvious.  For
additional details, see the Info node `(erc) display-buffer'.

Note that when the selected window already shows the current
buffer, ERC pretends this option's value is `bury' unless the
variable `erc-skip-displaying-selected-window-buffer' is nil or
the value of this option is DISPLAY-FUNCTION."
  :package-version '(ERC . "5.5")
  :group 'erc-buffers
  :type (cons 'choice (nthcdr 2 erc--buffer-display-choices)))

(defvaralias 'erc-query-display 'erc-interactive-display)
(defcustom erc-interactive-display 'window
  "How to display buffers as a result of user interaction.
This affects commands like /QUERY and /JOIN when issued
interactively at the prompt.  It does not apply when calling a
handler for such a command, like `erc-cmd-JOIN', from lisp code.
See `erc-buffer-display' for a full description of available
values.

When the value is a user-provided function, ERC may inject a hint
about the invocation context as an extra item in the \"action
alist\" included as part of the second argument.  The item's key
is the symbol `erc-interactive-display' and its value one of

  `/QUERY', `/JOIN', `/RECONNECT', `url', `erc', or `erc-tls'.

All are symbols indicating an inciting user action, such as the
issuance of a slash command, the clicking of a URL hyperlink, or
the invocation of an entry-point command.  See Info node `(erc)
display-buffer' for more."
  :package-version '(ERC . "5.6")
  :group 'erc-buffers
  :type erc--buffer-display-choices)

(defvaralias 'erc-reconnect-display 'erc-auto-reconnect-display)
(defcustom erc-auto-reconnect-display nil
  "How to display a channel buffer when automatically reconnecting.
ERC ignores this option when a user issues a /RECONNECT or
successfully reinvokes `erc-tls' with similar arguments to those
from the prior connection.  See `erc-buffer-display' for a
description of possible values.

When the value is function, ERC may inject a hint about the
calling context as an extra item in the alist making up the tail
of the second, \"action\" argument.  The item's key is the symbol
`erc-auto-reconnect-display' and its value something non-nil."
  :package-version '(ERC . "5.5")
  :group 'erc-buffers
  :type erc--buffer-display-choices)

(defcustom erc-auto-reconnect-display-timeout 10
  "Duration `erc-auto-reconnect-display' remains active.
The countdown starts on MOTD and is canceled early by any
\"slash\" command."
  :package-version '(ERC . "5.6")
  :type 'integer
  :group 'erc-buffers)

(defcustom erc-reconnect-display-server-buffers nil
  "Apply buffer-display options to server buffers when reconnecting.
By default, ERC does not consider `erc-auto-reconnect-display'
for server buffers when automatically reconnecting, nor does it
consider `erc-interactive-display' when users issue a /RECONNECT.
Enabling this tells ERC to always display server buffers
according to those options."
  :package-version '(ERC . "5.6")
  :type 'boolean
  :group 'erc-buffers)

(defcustom erc-frame-alist nil
  "Alist of frame parameters for creating erc frames.
A value of nil means to use `default-frame-alist'."
  :group 'erc-buffers
  :type '(repeat (cons :format "%v"
                       (symbol :tag "Parameter")
                       (sexp :tag "Value"))))

(defcustom erc-frame-dedicated-flag nil
  "Non-nil means the erc frames are dedicated to that buffer.
This only has effect when `erc-join-buffer' is set to `frame'."
  :group 'erc-buffers
  :type 'boolean)

(defcustom erc-reuse-frames t
  "Determines whether new frames are always created.
Non-nil means only create a frame for undisplayed buffers.  Nil
means always create a new frame.  Regardless of its value, ERC
ignores this option unless `erc-join-buffer' is `frame'.  And
like most options in the `erc-buffer' customize group, this has
no effect on server buffers while reconnecting because ERC always
buries those."
  :group 'erc-buffers
  :type 'boolean)

(defvar erc--fallback-channel-prefixes "#&"
  "Prefix chars for distinguishing channel targets when CHANTYPES is unknown.")

(defun erc-channel-p (target)
  "Return non-nil if TARGET is a valid channel name or a channel buffer."
  (cond ((stringp target)
         (and-let*
             (((not (string-empty-p target)))
              (value (let ((entry (erc--get-isupport-entry 'CHANTYPES)))
                       (if entry (cadr entry) erc--fallback-channel-prefixes)))
              ((erc--strpos (aref target 0) value)))))
        ((and-let* (((buffer-live-p target))
                    (target (buffer-local-value 'erc--target target))
                    ((erc--target-channel-p target)))))))

;; For the sake of compatibility, a historical quirk concerning this
;; option, when nil, has been preserved: all buffers are suffixed with
;; the original dialed host name, which is usually something like
;; irc.libera.chat.  Collisions are handled by adding a uniquifying
;; numeric suffix of the form <N>.  Note that channel reassociation
;; behavior involving this option (when nil) was inverted in 28.1 (ERC
;; 5.4 and 5.4.1).  This was regrettable and has since been undone.

(defcustom erc-reuse-buffers t
  "If nil, create new buffers on joining a channel/query.
If non-nil, a new buffer will only be created when you join
channels with same names on different servers, or have query buffers
open with nicks of the same name on different servers.  Otherwise,
the existing buffers will be reused."
  :group 'erc-buffers
  :type 'boolean)

(make-obsolete-variable 'erc-reuse-buffers
                        "old behavior when t now permanent" "29.1")

(defun erc-normalize-port (port)
  "Normalize the port specification PORT to integer form.
PORT may be an integer, a string or a symbol.  If it is a string or a
symbol, it may have these values:
* irc         -> 194
* ircs        -> 994
* ircd        -> 6667
* ircd-dalnet -> 7000"
  ;; These were updated somewhat in 2022 to reflect modern standards
  ;; and practices.  See also:
  ;;
  ;; https://datatracker.ietf.org/doc/html/rfc7194#section-1
  ;; https://www.iana.org/assignments/service-names-port-numbers
  (cond
   ((symbolp port)
    (erc-normalize-port (symbol-name port)))
   ((stringp port)
    (let ((port-nr (string-to-number port)))
      (cond
       ((> port-nr 0)
        port-nr)
       ((string-equal port "irc")
        194)
       ((string-equal port "ircs")
        994)
       ((string-equal port "ircu") 6667) ; 6665-6669
       ((string-equal port "ircd") ; nonstandard (irc-serv is 529)
        6667)
       ((string-equal port "ircs-u") 6697)
       ((string-equal port "ircd-dalnet")
        7000)
       (t
        nil))))
   ((numberp port)
    port)
   (t
    nil)))

(defun erc-port-equal (a b)
  "Check whether ports A and B are equal."
  (= (erc-normalize-port a) (erc-normalize-port b)))

(defun erc-generate-new-buffer-name (server port target &optional tgt-info id)
  "Determine the name of an ERC buffer.
When TGT-INFO is nil, assume this is a server buffer.  If ID is non-nil,
return ID as a string unless a buffer already exists with a live server
process, in which case signal an error.  When ID is nil, return a
temporary name based on SERVER and PORT to be replaced with the network
name when discovered (see `erc-networks--rename-server-buffer').  Allow
either SERVER or PORT (but not both) to be nil to accommodate oddball
`erc-server-connect-function's.

When TGT-INFO is non-nil, expect its string field to match the redundant
param TARGET (retained for compatibility).  Whenever possibly, prefer
returning TGT-INFO's string unmodified.  But when a case-insensitive
collision prevents that, return target@ID when ID is non-nil or
target@network otherwise after renaming the conflicting buffer in the
same manner."
  (when target ; compat
    (setq tgt-info (erc--target-from-string target)))
  (if tgt-info
      (let* ((esid (and erc-networks--id
                        (erc-networks--id-symbol erc-networks--id)))
             (name (if esid
                       (erc-networks--reconcile-buffer-names tgt-info
                                                             erc-networks--id)
                     (erc--target-string tgt-info))))
        (if (and esid (with-suppressed-warnings ((obsolete erc-reuse-buffers))
                        erc-reuse-buffers))
            name
          (generate-new-buffer-name name)))
    (if (and (with-suppressed-warnings ((obsolete erc-reuse-buffers))
               erc-reuse-buffers)
             id)
        (let ((string (symbol-name (erc-networks--id-symbol
                                    (erc-networks--id-create id)))))
          (when-let* ((buf (get-buffer string))
                      ((erc-server-process-alive buf)))
            (user-error  "Session with ID %S already exists" string))
          string)
      (generate-new-buffer-name (if (and server port)
                                    (if (with-suppressed-warnings
                                            ((obsolete erc-reuse-buffers))
                                          erc-reuse-buffers)
                                        (format "%s:%s" server port)
                                      (format "%s:%s/%s" server port server))
                                  (or server port))))))

(defun erc-get-buffer-create (server port target &optional tgt-info id)
  "Create a new buffer based on the arguments."
  (when target ; compat
    (setq tgt-info (erc--target-from-string target)))
  (if (and erc--server-reconnecting
           (not tgt-info)
           (with-suppressed-warnings ((obsolete erc-reuse-buffers))
             erc-reuse-buffers))
      (current-buffer)
    (get-buffer-create
     (erc-generate-new-buffer-name server port nil tgt-info id))))

(defun erc-member-ignore-case (string list)
  "Return non-nil if STRING is a member of LIST.

All strings are compared according to IRC protocol case rules, see
`erc-downcase'."
  (setq string (erc-downcase string))
  (catch 'result
    (while list
      (if (string= string (erc-downcase (car list)))
          (throw 'result list)
        (setq list (cdr list))))))

(defun erc-get-buffer (target &optional proc)
  "Return the buffer matching TARGET in the process PROC.
Without PROC, search all ERC buffers.  For historical reasons,
skip buffers for channels the client has \"PART\"ed or from which
it's been \"KICK\"ed.  Expect users to use a different function
for finding targets independent of \"JOIN\"edness."
  (let ((downcased-target (erc-downcase target)))
    (catch 'buffer
      (erc-buffer-filter
       (lambda ()
         (let ((current (erc-default-target)))
           (and (stringp current)
                (string-equal downcased-target (erc-downcase current))
                (throw 'buffer (current-buffer)))))
       proc))))

(defun erc--buffer-p (buf predicate proc)
  (with-current-buffer buf
    (and (derived-mode-p 'erc-mode)
	 (or (not proc)
	     (eq proc erc-server-process))
	 (funcall predicate)
	 buf)))

(defun erc-buffer-filter (predicate &optional proc)
  "Return a list of `erc-mode' buffers matching certain criteria.
Call PREDICATE without arguments in all ERC buffers or only those
belonging to a non-nil PROC.  Expect it to return non-nil in
buffers that should be included in the returned list.

PROC is either an `erc-server-process', identifying a certain
server connection, or nil which means all open connections."
  (save-excursion
    (delq
     nil
     (mapcar (lambda (buf)
               (when (buffer-live-p buf)
		 (erc--buffer-p buf predicate proc)))
             (buffer-list)))))

(defalias 'erc-buffer-do 'erc-buffer-filter
  "Call FUNCTION in all ERC buffers or only those for PROC.
Expect to be preferred over `erc-buffer-filter' in cases where
the return value goes unused.

\(fn FUNCTION &optional PROC)")

(defun erc-buffer-list (&optional predicate proc)
  "Return a list of ERC buffers.
PREDICATE is a function which executes with every buffer satisfying
the predicate.  If PREDICATE is passed as nil, return a list of all ERC
buffers.  If PROC is given, the buffers local variable `erc-server-process'
needs to match PROC."
  (erc-buffer-filter (or predicate #'always) proc))

(define-obsolete-function-alias 'erc-iswitchb #'erc-switch-to-buffer "25.1")
(defun erc--switch-to-buffer (&optional arg)
  (read-buffer "Switch to ERC buffer: "
	       (when (boundp 'erc-modified-channels-alist)
		 (buffer-name (caar (last erc-modified-channels-alist))))
	       t
	       ;; Only allow ERC buffers in the same session.
	       (let ((proc (unless arg erc-server-process)))
		 (lambda (bufname)
		   (let ((buf (if (consp bufname)
				  (cdr bufname) (get-buffer bufname))))
                     (and buf (erc--buffer-p buf (lambda () t) proc)))))))
(defun erc-switch-to-buffer (&optional arg)
  "Prompt for an ERC buffer to switch to.
When invoked with prefix argument, use all ERC buffers.  Without
prefix ARG, allow only buffers related to same session server.
If `erc-track-mode' is in enabled, put the last element of
`erc-modified-channels-alist' in front of the buffer list."
  (interactive "P")
  (switch-to-buffer (erc--switch-to-buffer arg)))
(defun erc-switch-to-buffer-other-window (&optional arg)
  "Prompt for an ERC buffer to switch to in another window.
When invoked with prefix argument, use all ERC buffers.  Without
prefix ARG, allow only buffers related to same session server.
If `erc-track-mode' is in enabled, put the last element of
`erc-modified-channels-alist' in front of the buffer list."
  (interactive "P")
  (switch-to-buffer-other-window (erc--switch-to-buffer arg)))

(defun erc-channel-list (proc)
  "Return a list of channel buffers.
PROC is the process for the server connection.  If PROC is nil, return
all channel buffers on all servers."
  (erc-buffer-filter
   (lambda ()
     (and (erc-default-target)
          (erc-channel-p (erc-default-target))))
   proc))

(defun erc-buffer-list-with-nick (nick proc)
  "Return buffers containing NICK in the `erc-channel-users' list."
  (with-current-buffer (process-buffer proc)
    (let ((user (gethash (erc-downcase nick) erc-server-users)))
      (if user
          (erc-server-user-buffers user)
        nil))))

;; Some local variables

;; TODO eventually deprecate this variable
;;
;; In the ancient, pre-CVS days (prior to June 2001), this list may
;; have been used for supporting the changing of a buffer's target on
;; the fly (mid-session).  Such usage, which allowed cons cells like
;; (QUERY . bob) to serve as the list's head, was either never fully
;; integrated or was partially clobbered prior to the introduction of
;; version control.  But vestiges remain (see `erc-dcc-chat-mode').
;; And despite appearances, no evidence has emerged that ERC ever
;; supported one-to-many target buffers.  If such a thing was aspired
;; to, it was never realized.
;;
;; New library code should use the `erc--target' struct instead.
;; Third-party code can continue to use this and `erc-default-target'.
(defvar-local erc-default-recipients nil
  "List of default recipients of the current buffer.")

(defvar-local erc-channel-user-limit nil
  "Limit of users per channel.")

(defvar-local erc-channel-key nil
  "Key needed to join channel.")

(defvar-local erc-invitation nil
  "Last invitation channel.")

(defvar-local erc-away nil
  "Non-nil indicates that we are away.

Use `erc-away-time' to access this if you might be in a channel
buffer rather than a server buffer.")

(defvar-local erc-channel-list nil
  "Server channel list.")

(defvar-local erc-bad-nick nil
  "Non-nil indicates that we got a `nick in use' error while connecting.")

(defvar-local erc-logged-in nil
  "Non-nil indicates that we are logged in.")

(defvar-local erc-default-nicks nil
  "The local copy of `erc-nick' - the list of nicks to choose from.")

(defvar-local erc-nick-change-attempt-count 0
  "Used to keep track of how many times an attempt at changing nick is made.")

(defun erc-migrate-modules (mods)
  "Migrate old names of ERC modules to new ones."
  ;; modify `transforms' to specify what needs to be changed
  ;; each item is in the format '(old . new)
  (delete-dups (mapcar #'erc--normalize-module-symbol mods)))

(defun erc--sort-modules (modules)
  "Return a copy of MODULES, deduped and led by sorted built-ins."
  (let (built-in third-party)
    (dolist (mod modules)
      (setq mod (erc--normalize-module-symbol mod))
      (cl-pushnew mod (if (get mod 'erc--module) built-in third-party)))
    `(,@(sort built-in #'string-lessp) ,@(nreverse third-party))))

;;;###autoload(custom-autoload 'erc-modules "erc")

(defcustom erc-modules '( autojoin button completion fill imenu irccontrols
                          list match menu move-to-prompt netsplit
                          networks readonly ring stamp track)
  "Modules to enable while connecting.
When modifying this option in lisp code, use a Custom-friendly
facilitator, like `setopt', or call `erc-update-modules'
afterward.  This ensures a consistent ordering and disables
removed modules.  It also gives packages access to the hook
`erc-before-connect'."
  :get (lambda (sym)
         ;; replace outdated names with their newer equivalents
         (erc-migrate-modules (symbol-value sym)))
  ;; Expect every built-in module to have the symbol property
  ;; `erc--module' set to its canonical symbol (often itself).
  :initialize (lambda (symbol exp)
                ;; Use `cdddr' because (set :greedy t . ,entries)
                (dolist (entry (cdddr (get 'erc-modules 'custom-type)))
                  (when-let* (((eq (car entry) 'const))
                              (s (cadddr entry))) ; (const :tag "..." ,s)
                    (put s 'erc--module s)))
                (custom-initialize-reset symbol exp))
  :set (lambda (sym val)
         ;; disable modules which have just been removed
         (when (and (boundp 'erc-modules) erc-modules val)
           (dolist (module erc-modules)
             (unless (memq module val)
               (let ((f (intern-soft (format "erc-%s-mode" module))))
                 (when (and (fboundp f) (boundp f))
                   (when (symbol-value f)
                     (message "Disabling `erc-%s'" module)
                     (funcall f 0))
                   ;; Disable local module in all ERC buffers.
                   (unless (or (custom-variable-p f)
                               (not (fboundp 'erc-buffer-filter)))
                     (erc-buffer-filter (lambda ()
                                          (when (symbol-value f)
                                            (funcall f 0))
                                          (kill-local-variable f)))))))))
         ;; Calling `set-default-toplevel-value' complicates testing.
         (set sym (erc--sort-modules val))
         ;; Don't initialize modules on load, even though the rare
         ;; third-party module may need it.
         (when (fboundp 'erc-update-modules)
           (unless erc--inside-mode-toggle-p
             (erc-update-modules))))
  :type
  '(set
    :greedy t
    (const :tag "autoaway: Set away status automatically" autoaway)
    (const :tag "autojoin: Join channels automatically" autojoin)
    (const :tag "bufbar: Show ERC buffers in a side window" bufbar)
    (const :tag "button: Buttonize URLs, nicknames, and other text" button)
    (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
           capab-identify)
    (const :tag "command-indicator: Echo command lines." command-indicator)
    (const :tag "completion: Complete nicknames and commands (programmable)"
           completion)
    (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
    (const :tag "fill: Wrap long lines" fill)
    (const :tag "identd: Launch an identd server on port 8113" identd)
    (const :tag "imenu: A simple Imenu integration" imenu)
    (const :tag "irccontrols: Highlight or remove IRC control characters"
           irccontrols)
    (const :tag "keep-place: Leave point above un-viewed text" keep-place)
    (const :tag "list: List channels in a separate buffer" list)
    (const :tag "log: Save buffers in logs" log)
    (const :tag "match: Highlight pals, fools, and other keywords" match)
    (const :tag "menu: Display a menu in ERC buffers" menu)
    (const :tag "move-to-prompt: Move to the prompt when typing text"
           move-to-prompt)
    (const :tag "netsplit: Detect netsplits" netsplit)
    (const :tag "networks: Provide data about IRC networks" networks)
    (const :tag "nickbar: Show nicknames in a dynamic side window" nickbar)
    (const :tag "nicks: Uniquely colorize nicknames in target buffers" nicks)
    (const :tag "noncommands: Deprecated. See module `command-indicator'."
           noncommands)
    (const :tag "notifications: Desktop alerts on PRIVMSG or mentions"
           notifications)
    (const :tag
           "notify: Notify when the online status of certain users changes"
           notify)
    (const :tag "page: Process CTCP PAGE requests from IRC" page)
    (const :tag "readonly: Make displayed lines read-only" readonly)
    (const :tag "replace: Replace text in messages" replace)
    (const :tag "ring: Enable an input history" ring)
    (const :tag "sasl: Enable SASL authentication" sasl)
    (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
           scrolltobottom)
    (const :tag "services: Identify to Nickserv (IRC Services) automatically"
           services)
    (const :tag "smiley: Convert smileys to pretty icons" smiley)
    (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
           sound)
    (const :tag "spelling: Check spelling" spelling)
    (const :tag "stamp: Add timestamps to messages" stamp)
    (const :tag "track: Track channel activity in the mode-line" track)
    (const :tag "truncate: Truncate buffers to a certain size" truncate)
    (const :tag "unmorse: Translate morse code in messages" unmorse)
    (const :tag "xdcc: Act as an XDCC file-server" xdcc)
    (repeat :tag "Others" :inline t symbol))
  :package-version '(ERC . "5.6")
  :group 'erc)

(defun erc-update-modules ()
  "Enable minor mode for every module in `erc-modules'.
Except ignore all local modules, which were introduced in ERC 5.5."
  (erc--update-modules erc-modules)
  nil)

(defvar erc--aberrant-modules nil
  "Modules suspected of being improperly loaded.")

(defun erc--warn-about-aberrant-modules ()
  (when (and erc--aberrant-modules (not erc--target))
    (erc-button--display-error-notice-with-keys-and-warn
     "The following modules likely engage in unfavorable loading practices: "
     (mapconcat (lambda (s) (format "`%s'" s)) erc--aberrant-modules ", ")
     ". Please contact ERC with \\[erc-bug] if you believe this to be untrue."
     " See Info:\"(erc) Module Loading\" for more.")
    (setq erc--aberrant-modules nil)))

(defvar erc--requiring-module-mode-p nil
  "Non-nil while doing (require \\='erc-mymod) for `mymod' in `erc-modules'.
Used for inhibiting potentially recursive `erc-update-modules'
invocations by third-party packages.")

(defun erc--find-mode (sym)
  (setq sym (erc--normalize-module-symbol sym))
  (if-let ((mode (intern-soft (concat "erc-" (symbol-name sym) "-mode")))
           ((and (fboundp mode)
                 (autoload-do-load (symbol-function mode) mode)))
           ((or (get sym 'erc--module)
                (symbol-file mode)
                (ignore (cl-pushnew sym erc--aberrant-modules)))))
      mode
    (and (or (and erc--requiring-module-mode-p
                  ;; Also likely non-nil: (eq sym (car features))
                  (cl-pushnew sym erc--aberrant-modules))
             (let ((erc--requiring-module-mode-p t))
               (require (or (get sym 'erc--feature)
                            (intern (concat "erc-" (symbol-name sym))))
                        nil 'noerror))
             (memq sym erc--aberrant-modules))
         (or mode (setq mode (intern-soft (concat "erc-" (symbol-name sym)
                                                  "-mode"))))
         (fboundp mode)
         mode)))

(defun erc--update-modules (modules)
  (let (local-modes)
    (dolist (module modules local-modes)
      (if-let ((mode (erc--find-mode module)))
          (if (custom-variable-p mode)
              (funcall mode 1)
            (push mode local-modes))
        (error "`%s' is not a known ERC module" module)))))

(defvar erc--updating-modules-p nil
  "Non-nil when running `erc--update-modules' in `erc-open'.
This allows global modules with known or likely dependents (or
some other reason for activating after session initialization) to
conditionally run setup code traditionally reserved for
`erc-mode-hook' in the setup portion of their mode toggle.  Note
that being \"global\", they'll likely want to do so in all ERC
buffers and ensure the code is idempotent.  For example:

  (add-hook \\='erc-mode-hook #\\='erc-foo-setup-fn)
  (unless erc--updating-modules-p
    (erc-with-all-buffers-of-server nil
        (lambda () some-condition-p)
      (erc-foo-setup-fn)))

This means that when a dependent module is initializing and
realizes it's missing some required module \"foo\", it can
confidently call (erc-foo-mode 1) without having to learn
anything about the dependency's implementation.")

(defvar erc--setup-buffer-hook '(erc--warn-about-aberrant-modules)
  "Internal hook for module setup involving windows and frames.")

(defvar erc--display-context nil
  "Extra action alist items passed to `display-buffer'.
Non-nil when a user specifies a custom display action for certain
buffer-display options, like `erc-auto-reconnect-display'.  ERC
pairs the option's symbol with a context-dependent value and adds
the entry to the user-provided alist when calling `pop-to-buffer'
or `display-buffer'.")

(defvar erc-skip-displaying-selected-window-buffer t
  "Whether to forgo showing a buffer that's already being displayed.
But only in the selected window.  This is intended as a crutch
for non-user third-party code that might be slow to adopt the
`display-buffer' function variant available to all buffer-display
options starting in ERC 5.6.  Users with rare requirements, like
wanting to change the window buffer to something other than the
one being processed, should see the Info node `(erc)
display-buffer'.")
(make-obsolete 'erc-show-already-displayed-buffer
               "non-nil behavior to be made permanent" "30.1")

(defvar-local erc--display-buffer-overriding-action nil
  "The value of `display-buffer-overriding-action' when non-nil.
Influences the displaying of new or reassociated ERC buffers.
Reserved for use by built-in modules.")

(defun erc-setup-buffer (buffer)
  "Consults `erc-join-buffer' to find out how to display `BUFFER'."
  (pcase (if (zerop (erc-with-server-buffer
                      erc--server-last-reconnect-count))
             erc-join-buffer
           (or erc-auto-reconnect-display erc-join-buffer))
    ((and (pred functionp) disp-fn (let context erc--display-context))
     (unless (zerop erc--server-last-reconnect-count)
       (push '(erc-auto-reconnect-display . t) context))
     (funcall disp-fn buffer (cons nil context)))
    ((guard (and erc-skip-displaying-selected-window-buffer
                 (eq (window-buffer) buffer))))
    ('window
     (if (active-minibuffer-window)
         (display-buffer buffer)
       (switch-to-buffer-other-window buffer)))
    ('window-noselect
     (display-buffer buffer '(nil (inhibit-same-window . t))))
    ('bury
     nil)
    ('frame
     (when (or (not erc-reuse-frames)
               (not (get-buffer-window buffer t)))
       (let ((frame (make-frame (or erc-frame-alist
                                    default-frame-alist))))
         (raise-frame frame)
         (select-frame frame))
       (switch-to-buffer buffer)
       (when erc-frame-dedicated-flag
         (set-window-dedicated-p (selected-window) t))))
    (_
     (if (active-minibuffer-window)
         (display-buffer buffer)
       (switch-to-buffer buffer)))))

(defun erc--merge-local-modes (new-modes old-vars)
  "Return a cons of two lists, each containing local-module modes.
In the first, put modes to be enabled in a new ERC buffer by
calling their associated functions.  In the second, put modes to
be marked as disabled by setting their associated variables to
nil."
  (if old-vars
      (let ((out (list (reverse new-modes))))
        (pcase-dolist (`(,k . ,v) old-vars)
          (when (and (string-prefix-p "erc-" (symbol-name k))
                     (string-suffix-p "-mode" (symbol-name k))
                     (get k 'erc-module))
            (if v
                (cl-pushnew k (car out))
              (setf (car out) (delq k (car out)))
              (cl-pushnew k (cdr out)))))
        (cons (nreverse (car out)) (nreverse (cdr out))))
    (list new-modes)))

;; This function doubles as a convenient helper for use in unit tests.
;; Prior to 5.6, its contents lived in `erc-open'.

(defun erc--initialize-markers (old-point continued-session)
  "Ensure prompt and its bounding markers have been initialized."
  ;; FIXME erase assertions after code review and additional testing.
  (setq erc-insert-marker (make-marker)
        erc-input-marker (make-marker))
  (if continued-session
      (progn
        ;; Trust existing markers.
        (set-marker erc-insert-marker
                    (alist-get 'erc-insert-marker continued-session))
        (set-marker erc-input-marker
                    (alist-get 'erc-input-marker continued-session))
        (set-marker-insertion-type erc-insert-marker t)
        (cl-assert (= (field-end erc-insert-marker) erc-input-marker))
        (goto-char old-point)
        (let ((erc--hidden-prompt-overlay
               (alist-get 'erc--hidden-prompt-overlay continued-session)))
          (erc--unhide-prompt)))
    (cl-assert (not (get-text-property (point) 'erc-prompt)))
    ;; In the original version from `erc-open', the snippet that
    ;; handled these newline insertions appeared twice close in
    ;; proximity, which was probably unintended.  Nevertheless, we
    ;; preserve the double newlines here for historical reasons.
    (insert "\n\n")
    (set-marker erc-insert-marker (point))
    (erc-display-prompt)
    (set-marker-insertion-type erc-insert-marker t)
    (cl-assert (= (point) (point-max)))))

(defun erc-open (&optional server port nick full-name
                           connect passwd _tgt-list channel process
                           client-certificate user id)
  "Return a new or reinitialized server or target buffer.
If CONNECT is non-nil, connect to SERVER and return its new or
reassociated buffer.  Otherwise, assume PROCESS is non-nil and belongs
to an active session, and return a new or refurbished target buffer for
CHANNEL, which may also be a query target (the parameter name remains
for historical reasons).  Pass SERVER, PORT, NICK, USER, FULL-NAME, and
PASSWD to `erc-determine-parameters' for preserving as session-local
variables.  Do something similar for CLIENT-CERTIFICATE and ID, which
should be as described by `erc-tls'.

Note that ERC ignores TGT-LIST and initializes `erc-default-recipients'
with CHANNEL as its only member.  Note also that this function has the
side effect of setting the current buffer to the one it returns.  Use
`with-current-buffer' or `save-excursion' to nullify this effect."
  (let* ((target (and channel (erc--target-from-string channel)))
         (buffer (erc-get-buffer-create server port nil target id))
         (old-buffer (current-buffer))
         (erc--target-priors (and target ; buf from prior session
                                  (buffer-local-value 'erc--target buffer)
                                  (buffer-local-variables buffer)))
         (old-recon-count erc-server-reconnect-count)
         (old-point nil)
         (delayed-modules nil)
         (continued-session (or erc--server-reconnecting
                                erc--target-priors
                                (and-let* (((not target))
                                           (m (buffer-local-value
                                               'erc-input-marker buffer))
                                           ((marker-position m)))
                                  (buffer-local-variables buffer)))))
    (when connect (run-hook-with-args 'erc-before-connect server port nick))
    (set-buffer buffer)
    (setq old-point (point))
    (setq delayed-modules
          (erc--merge-local-modes (let ((erc--updating-modules-p t))
                                    (erc--update-modules
                                     (erc--sort-modules erc-modules)))
                                  (or erc--server-reconnecting
                                      erc--target-priors)))

    (delay-mode-hooks (erc-mode))

    (setq erc-server-reconnect-count old-recon-count)

    (when (setq erc-server-connected (not connect))
      (setq erc-server-announced-name
            (buffer-local-value 'erc-server-announced-name old-buffer)))
    ;; connection parameters
    (setq erc-server-process process)
    ;; stack of default recipients
    (when channel (setq erc-default-recipients (list channel)))
    (when target
      (setq erc--target target
            erc-network (erc-network)))
    (setq erc-server-current-nick nil)
    ;; Initialize erc-server-users and erc-channel-users
    (if connect
        (progn ;; server buffer
          (setq erc-server-users
                (make-hash-table :test 'equal))
          (setq erc-channel-users nil))
      (progn ;; target buffer
        (setq erc-server-users nil)
        (setq erc-channel-users
              (make-hash-table :test 'equal))))
    (setq erc-channel-topic "")
    ;; limit on the number of users on the channel (mode +l)
    (setq erc-channel-user-limit nil)
    (setq erc-channel-key nil)
    ;; last active buffer, defaults to this one
    (erc-set-active-buffer buffer)
    ;; last invitation channel
    (setq erc-invitation nil)
    ;; Server channel list
    (setq erc-channel-list ())
    ;; login-time 'nick in use' error
    (setq erc-bad-nick nil)
    ;; whether we have logged in
    (setq erc-logged-in nil)
    ;; The local copy of `erc-nick' - the list of nicks to choose
    (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
    ;; client certificate (only useful if connecting over TLS)
    (setq erc-session-client-certificate client-certificate)
    (setq erc-networks--id
          (if connect
              (or (and erc--server-reconnecting
                       (alist-get 'erc-networks--id erc--server-reconnecting))
                  (and id (erc-networks--id-create id)))
            (buffer-local-value 'erc-networks--id old-buffer)))
    ;; debug output buffer
    (setq erc-dbuf
          (when erc-log-p
            (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))

    (erc--initialize-markers old-point continued-session)
    (erc-determine-parameters server port nick full-name user passwd)
    (save-excursion (run-mode-hooks)
                    (dolist (mod (car delayed-modules)) (funcall mod +1))
                    (dolist (var (cdr delayed-modules)) (set var nil)))

    ;; Saving log file on exit
    (run-hook-with-args 'erc-connect-pre-hook buffer)

    (if connect
        (erc-server-connect erc-session-server
                            erc-session-port
                            buffer
                            erc-session-client-certificate)
      (erc-update-mode-line))

    ;; Now display the buffer in a window as per user wishes.
    (when (eq buffer old-buffer) (cl-assert (and connect (not target))))
    (unless (and (not erc-reconnect-display-server-buffers)
                 (eq buffer old-buffer))
      (when erc-log-p
        ;; we can't log to debug buffer, it may not exist yet
        (message "erc: old buffer %s, switching to %s"
                 old-buffer buffer))
      (let ((display-buffer-overriding-action
             (or erc--display-buffer-overriding-action
                 display-buffer-overriding-action)))
        (erc-setup-buffer buffer)
        (run-hooks 'erc--setup-buffer-hook)))

    buffer))

(defun erc-initialize-log-marker (buffer)
  "Initialize the `erc-last-saved-position' marker to a sensible position.
BUFFER is the current buffer."
  (with-current-buffer buffer
    (unless (markerp erc-last-saved-position)
      (setq erc-last-saved-position (make-marker))
      (move-marker erc-last-saved-position
		   (1- (marker-position erc-insert-marker))))))

;; interactive startup

(defvar erc-server-history-list nil
  "IRC server interactive selection history list.")

(defvar erc-nick-history-list nil
  "Nickname interactive selection history list.")

(defun erc-already-logged-in (server port nick)
  "Return the buffers corresponding to a NICK on PORT of a session SERVER.
This is determined by looking for the appropriate buffer and checking
whether the connection is still alive.
If no buffer matches, return nil."
  (erc-buffer-list
   (lambda ()
     (and (erc-server-process-alive)
          (string= erc-session-server server)
          (erc-port-equal erc-session-port port)
          (erc-current-nick-p nick)))))

(defcustom erc-before-connect nil
  "Functions called before connecting to a server.
The functions in this variable gets executed before `erc'
actually invokes `erc-mode' with your input data.  The functions
in here get called with three parameters, SERVER, PORT and NICK."
  :group 'erc-hooks
  :type '(repeat function))

(defcustom erc-after-connect nil
  "Abnormal hook run upon establishing a logical IRC connection.
Runs on MOTD's end when `erc-server-connected' becomes non-nil.
ERC calls members with `erc-server-announced-name', falling back
to the 376/422 message's \"sender\", as well as the current nick,
as given by the 376/422 message's \"target\" parameter, which is
typically the same as that reported by `erc-current-nick'."
  :group 'erc-hooks
  :type '(repeat function))

(defun erc--ensure-url (input)
  (unless (string-match (rx bot "irc" (? "6") (? "s") "://") input)
    (when (and (string-match (rx (? (+ nonl) "@")
                                 (or (group (* (not "[")) ":" (* nonl))
                                     (+ nonl))
                                 ":" (+ (not (any ":]"))) eot)
                             input)
               (match-beginning 1))
      (setq input (concat "[" (substring input (match-beginning 1)) "]")))
    (setq input (concat "irc://" input)))
  input)

(defvar erc--prompt-for-server-function nil)

;;;###autoload
(defun erc-select-read-args ()
  "Prompt for connection parameters and return them in a plist.
By default, collect `:server', `:port', `:nickname', and
`:password'.  With a non-nil prefix argument, also prompt for
`:user' and `:full-name'.  Also return various environmental
properties needed by entry-point commands, like `erc-tls'."
  (let* ((input (let ((d (erc-compute-server)))
                  (if erc--prompt-for-server-function
                      (funcall erc--prompt-for-server-function)
                    (read-string (format-prompt "Server or URL" d)
                                 nil 'erc-server-history-list d))))
         ;; For legacy reasons, also accept a URL without a scheme.
         (url (url-generic-parse-url (erc--ensure-url input)))
         (server (url-host url))
         (sp (and (string-suffix-p "s" (url-type url)) erc-default-port-tls))
         (port (or (url-portspec url)
                   (erc-compute-port
                    (let ((d (erc-compute-port sp))) ; may be a string
                      (read-string (format-prompt "Port" d)
                                   nil nil d)))))
         ;; Trust the user not to connect twice accidentally.  We
         ;; can't use `erc-already-logged-in' to check for an existing
         ;; connection without modifying it to consider USER and PASS.
         (nick (or (url-user url)
                   (let ((d (erc-compute-nick)))
                     (read-string (format-prompt "Nickname" d)
                                  nil 'erc-nick-history-list d))))
         (user (and current-prefix-arg
                    (let ((d (erc-compute-user (url-user url))))
                      (read-string (format-prompt "User" d)
                                   nil nil d))))
         (full (and current-prefix-arg
                    (let ((d (erc-compute-full-name (url-user url))))
                      (read-string (format-prompt "Full name" d)
                                   nil nil d))))
         (passwd (let* ((p (with-suppressed-warnings ((obsolete erc-password))
                             (or (url-password url) erc-password)))
                        (m (if p
                               (format-prompt "Server password" p)
                             "Server password (optional): ")))
                   (if erc-prompt-for-password (read-passwd m nil p) p)))
         (opener (and (or sp (eql port erc-default-port-tls)
                          (and (equal server erc-default-server)
                               (not (string-prefix-p "irc://" input))
                               (eql port erc-default-port)
                               (y-or-n-p "Connect using TLS instead? ")
                               (setq port erc-default-port-tls)))
                      #'erc-open-tls-stream))
         env)
    (when erc-interactive-display
      (push `(erc-join-buffer . ,erc-interactive-display) env))
    (when erc--display-context
      (push `(erc--display-context . ,erc--display-context) env))
    (when opener
      (push `(erc-server-connect-function . ,opener) env))
    (when (and passwd (string= "" passwd))
      (setq passwd nil))
    `( :server ,server :port ,port :nick ,nick ,@(and user `(:user ,user))
       ,@(and passwd `(:password ,passwd)) ,@(and full `(:full-name ,full))
       ,@(and env `(--interactive-env-- ,env)))))

(defmacro erc--with-entrypoint-environment (env &rest body)
  "Run BODY with bindings from ENV alist."
  (declare (indent 1))
  (let ((syms (make-symbol "syms"))
        (vals (make-symbol "vals")))
    `(let (,syms ,vals)
       (pcase-dolist (`(,k . ,v) ,env) (push k ,syms) (push v ,vals))
       (cl-progv ,syms ,vals
         ,@body))))

;;;###autoload
(defun erc-server-select ()
  "Interactively connect to a server from `erc-server-alist'."
  (declare (obsolete erc-tls "30.1"))
  (interactive)
  (let ((erc--prompt-for-server-function #'erc-networks--server-select))
    (call-interactively #'erc)))

;;;###autoload
(cl-defun erc (&key (server (erc-compute-server))
                    (port   (erc-compute-port))
                    (nick   (erc-compute-nick))
                    (user   (erc-compute-user))
                    password
                    (full-name (erc-compute-full-name))
                    id
                    ;; Used by interactive form
                    ((--interactive-env-- --interactive-env--)))
  "Connect to an Internet Relay Chat SERVER on a non-TLS PORT.
Use NICK and USER, when non-nil, to inform the IRC commands of
the same name, possibly factoring in a non-nil FULL-NAME as well.
When PASSWORD is non-nil, also send an opening server password
via the \"PASS\" command.  Interactively, prompt for SERVER,
PORT, NICK, and PASSWORD, along with USER and FULL-NAME when
given a prefix argument.  Non-interactively, expect the rarely
needed ID parameter, when non-nil, to be a symbol or a string for
naming the server buffer and identifying the connection
unequivocally.  Once connected, return the server buffer.  (See
Info node `(erc) Connecting' for details about all mentioned
parameters.)

Together with `erc-tls', this command serves as the main entry
point for ERC, the powerful, modular, and extensible IRC client.
Non-interactively, both commands accept the following keyword
arguments, with their defaults supplied by the indicated
\"compute\" functions:

  :server    `erc-compute-server'
  :port      `erc-compute-port'
  :nick      `erc-compute-nick'
  :user      `erc-compute-user'
  :password   N/A
  :full-name `erc-compute-full-name'
  :id'        N/A

For example, when called in the following manner

   (erc :server \"irc.libera.chat\" :full-name \"J. Random Hacker\")

ERC assigns SERVER and FULL-NAME the associated keyword values
and defers to `erc-compute-port', `erc-compute-user', and
`erc-compute-nick' for those respective parameters.

\(fn &key SERVER PORT NICK USER PASSWORD FULL-NAME ID)"
  (interactive (let ((erc--display-context `((erc-interactive-display . erc)
                                             ,@erc--display-context)))
                 (erc-select-read-args)))
  (unless (assq 'erc--display-context --interactive-env--)
    (push '(erc--display-context . ((erc-buffer-display . erc)))
          --interactive-env--))
  (erc--with-entrypoint-environment --interactive-env--
    (erc-open server port nick full-name t password nil nil nil nil user id)))

;;;###autoload
(defalias 'erc-select #'erc)
(defalias 'erc-ssl #'erc-tls)

;;;###autoload
(cl-defun erc-tls (&key (server (erc-compute-server))
                        (port   (erc-compute-port 'ircs-u))
                        (nick   (erc-compute-nick))
                        (user   (erc-compute-user))
                        password
                        (full-name (erc-compute-full-name))
                        client-certificate
                        id
                        ;; Used by interactive form
                        ((--interactive-env-- --interactive-env--)))
  "Connect to an IRC server over a TLS-encrypted connection.
Interactively, prompt for SERVER, PORT, NICK, and PASSWORD, along
with USER and FULL-NAME when given a prefix argument.
Non-interactively, also accept a CLIENT-CERTIFICATE, which should
be a list containing the file name of the certificate's key
followed by that of the certificate itself.  Alternatively,
accept a value of t instead of a list, to tell ERC to query
`auth-source' for the certificate's details.

Example client certificate (CertFP) usage:

    (erc-tls :server \"irc.libera.chat\" :port 6697
             :client-certificate
             \\='(\"/home/bandali/my-cert.key\"
               \"/home/bandali/my-cert.crt\"))

See the alternative entry-point command `erc' as well as Info
node `(erc) Connecting' for a fuller description of the various
parameters, like ID.

\(fn &key SERVER PORT NICK USER PASSWORD FULL-NAME CLIENT-CERTIFICATE ID)"
  (interactive
   (let ((erc-default-port erc-default-port-tls)
         (erc--display-context `((erc-interactive-display . erc-tls)
                                 ,@erc--display-context)))
     (erc-select-read-args)))
  ;; Bind `erc-server-connect-function' to `erc-open-tls-stream'
  ;; around `erc-open' when a non-default value hasn't been specified
  ;; by the user or the interactive form.  And don't bother checking
  ;; for advice, indirect functions, autoloads, etc.
  (unless (or (assq 'erc-server-connect-function --interactive-env--)
              (not (eq erc-server-connect-function #'erc-open-network-stream)))
    (push '(erc-server-connect-function . erc-open-tls-stream)
          --interactive-env--))
  (unless (assq 'erc--display-context --interactive-env--)
    (push '(erc--display-context . ((erc-buffer-display . erc-tls)))
          --interactive-env--))
  (erc--with-entrypoint-environment --interactive-env--
    (erc-open server port nick full-name t password
              nil nil nil client-certificate user id)))

(defun erc-open-tls-stream (name buffer host port &rest parameters)
  "Open an TLS stream to an IRC server.
The process will be given the name NAME, its target buffer will
be BUFFER.  HOST and PORT specify the connection target.
PARAMETERS should be a sequence of keywords and values, per
`open-network-stream'."
  (let ((p (plist-put parameters :type 'tls))
        args)
    (unless (plist-member p :nowait)
      (setq p (plist-put p :nowait t)))
    (setq args `(,name ,buffer ,host ,port ,@p))
    (apply #'open-network-stream args)))

(defun erc-open-socks-tls-stream (name buffer host service &rest parameters)
  "Connect to an IRC server via SOCKS proxy over TLS.
Defer to the `socks' and `gnutls' libraries to make the actual
connection and perform TLS negotiation.  Expect SERVICE to be a
TLS port number and that the plist PARAMETERS contains a
`:client-certificate' pair when necessary.  Otherwise, assume the
arguments NAME, BUFFER, and HOST to be acceptable to
`open-network-stream' and that users know to check out
`erc-server-connect-function' and Info node `(erc) SOCKS' for
more info, including an important example of how to \"wrap\" this
function with SOCKS credentials."
  (require 'gnutls)
  (require 'socks)
  (let ((proc (socks-open-network-stream name buffer host service))
        (cert-info (plist-get parameters :client-certificate)))
    (gnutls-negotiate :process proc
                      :hostname host
                      :keylist (and cert-info (list cert-info)))))

;;; Displaying error messages

(defun erc-error (&rest args)
  "Pass ARGS to `format', and display the result as an error message.
If `debug-on-error' is set to non-nil, then throw a real error with this
message instead, to make debugging easier."
  (if debug-on-error
      (apply #'error args)
    (apply #'message args)
    (beep)))

(defvar erc--warnings-buffer-name nil
  "Name of possibly existing alternate warnings buffer for unit tests.")

(defun erc--lwarn (type level format-string &rest args)
  "Issue a warning of TYPE and LEVEL with FORMAT-STRING and ARGS."
  (let ((message (substitute-command-keys
                  (apply #'format-message format-string args))))
    (display-warning type message level erc--warnings-buffer-name)))

;;; Debugging the protocol

(defvar erc-debug-irc-protocol-time-format "%FT%T.%6N%z "
  "Timestamp format string for protocol logger.")

(defconst erc-debug-irc-protocol-version "2"
  "Protocol log format version number.
This exists to help tooling track changes to the format.

In version 1, everything before and including the first double CRLF is
front matter, which must also be CRLF terminated.  Lines beginning with
three asterisks must be ignored as comments.  Other lines should be
interpreted as email-style headers.  Folding is not supported.  A second
double CRLF, if present, signals the end of a log.  Session resumption
is not supported.  Logger lines must adhere to the following format:
TIMESTAMP PEER-NAME FLOW-INDICATOR IRC-MESSAGE CRLF.  Outgoing messages
are indicated with a >> and incoming with a <<.

In version 2, certain outgoing passwords are replaced by a string
of ten question marks.")

(defvar erc-debug-irc-protocol nil
  "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".

The buffer is created if it doesn't exist.

NOTE: If this variable is non-nil, and you kill the only
visible \"*erc-protocol*\" buffer, it will be recreated shortly,
but you won't see it.

WARNING: Do not set this variable directly!  Instead, use the
function `erc-toggle-debug-irc-protocol' to toggle its value.")

(defvar erc--debug-irc-protocol-mask-secrets t
  "Whether to hide secrets in a debug log.
They are still visible on screen but are replaced by question
marks when yanked.")

(defun erc--mask-secrets (string)
  (when-let* ((eot (length string))
              (beg (text-property-any 0 eot 'erc-secret t string))
              (end (text-property-not-all beg eot 'erc-secret t string))
              (sec (substring string beg end)))
    (setq string (concat (substring string 0 beg)
                         (make-string 10 ??)
                         (substring string end eot)))
    (put-text-property beg (+ 10 beg) 'face 'erc-inverse-face string)
    (put-text-property beg (+ 10 beg) 'display sec string))
  string)

(defun erc-log-irc-protocol (string &optional outbound)
  "Append STRING to the buffer *erc-protocol*.

This only has any effect if `erc-debug-irc-protocol' is non-nil.

The buffer is created if it doesn't exist.

If OUTBOUND is non-nil, STRING is being sent to the IRC server and
appears in face `erc-input-face' in the buffer.  Lines must already
contain CRLF endings.  A peer is identified by the most precise label
available, starting with the session ID followed by the server-reported
hostname, and falling back to the dialed <server>:<port> pair.

When capturing logs for multiple peers and sorting them into buckets,
such inconsistent labeling may pose a problem until the MOTD is
received.  Setting a fixed `erc-networks--id' can serve as a
workaround."
  (when erc-debug-irc-protocol
    (let ((esid (if-let ((erc-networks--id)
                         (esid (erc-networks--id-symbol erc-networks--id)))
                    (symbol-name esid)
                  (or erc-server-announced-name
                      (format "%s:%s" erc-session-server erc-session-port))))
          (ts (when erc-debug-irc-protocol-time-format
                (format-time-string erc-debug-irc-protocol-time-format))))
      (when (and outbound erc--debug-irc-protocol-mask-secrets)
        (setq string (erc--mask-secrets string)))
      (with-current-buffer (get-buffer-create "*erc-protocol*")
        (save-excursion
          (goto-char (point-max))
          (let ((buffer-undo-list t)
                (inhibit-read-only t))
            (insert (if outbound
                        (concat ts esid " >> " string)
                      ;; Cope with multi-line messages
                      (let ((lines (split-string string "[\r\n]+" t))
                            result)
                        (dolist (line lines)
                          (setq result (concat result ts esid
                                               " << " line "\r\n")))
                        result)))))
        (let ((orig-win (selected-window))
              (debug-buffer-window (get-buffer-window (current-buffer) t)))
          (when debug-buffer-window
            (select-window debug-buffer-window)
            (when (= 1 (count-lines (point) (point-max)))
              (goto-char (point-max))
              (recenter -1))
            (select-window orig-win)))))))

(defun erc-toggle-debug-irc-protocol (&optional arg)
  "Toggle the value of `erc-debug-irc-protocol'.

If ARG is non-nil, show the *erc-protocol* buffer."
  (interactive "P")
  (let* ((buf (get-buffer-create "*erc-protocol*")))
    (with-current-buffer buf
      (view-mode-enter)
      (when (null (current-local-map))
        (let ((inhibit-read-only t)
              (msg (list
                    (concat "Version: " erc-debug-irc-protocol-version)
                    (concat "ERC-Version: " erc-version)
                    (concat "Emacs-Version: " emacs-version)
                    (erc-make-notice
                     (concat "This buffer displays all IRC protocol "
                             "traffic exchanged with servers."))
                    (erc-make-notice "Kill it to disable logging.")
                    (erc-make-notice (substitute-command-keys
                                      "Press \\`t' to toggle.")))))
          (insert (string-join msg "\r\n")))
        (use-local-map (make-sparse-keymap))
        (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
      (add-hook 'kill-buffer-hook
                (lambda () (setq erc-debug-irc-protocol nil))
                nil 'local)
      (goto-char (point-max))
      (let ((inhibit-read-only t))
        (insert (if erc-debug-irc-protocol "\r\n" "")
                (erc-make-notice
                 (format "IRC protocol logging %s at %s"
                         (if erc-debug-irc-protocol "disabled" "enabled")
                         (current-time-string)))
                (if erc-debug-irc-protocol "\r\n" "\r\n\r\n"))))
    (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
    (if (and arg
             (not (get-buffer-window "*erc-protocol*" t)))
        (display-buffer buf t))
    (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
             (if erc-debug-irc-protocol "enabled" "disabled"))))

;;; I/O interface

;; send interface

(defvar erc--send-action-function #'erc--send-action
  "Function to display and send an outgoing CTCP ACTION message.
Called with three arguments: the submitted input, the current
target, and an `erc-server-send' FORCE flag.")

(defun erc-send-action (tgt str &optional force)
  "Send CTCP ACTION information described by STR to TGT."
  (funcall erc--send-action-function tgt str force))

;; Sending and displaying are provided separately to afford modules
;; more flexibility, e.g., to forgo displaying on the way out when
;; expecting the server to echo messages back and/or to associate
;; outgoing messages with IDs generated for `erc--ephemeral'
;; placeholders.
(defun erc--send-action-perform-ctcp (target string force)
  "Send STRING to TARGET, possibly immediately, with FORCE."
  (erc-send-ctcp-message target (format "ACTION %s" string) force))

(defvar erc--use-language-catalog-for-ctcp-action-p nil
  "When non-nil, use `ACTION' entry from language catalog for /ME's.
Otherwise, use `ctcp-action' or `ctcp-action-input' from the
internal `-speaker' catalog.  This is an escape hatch to restore
pre-5.6 behavior for the `font-lock-face' property of incoming
and outgoing \"CTCP ACTION\" messages, whose pre-buttonized state
was a single interval of `erc-input-face' or `erc-action-face'.
Newer modules, like `fill-wrap' and `nicks', are incompatible with
this format style.  If you use this, please ask ERC to expose it
as a public variable via \\[erc-bug] or similar.")

(defun erc--send-action-display (string)
  "Display STRING as an outgoing \"CTCP ACTION\" message.
Propertize the message according to the compatibility flag
`erc--use-language-catalog-for-ctcp-action-p'."
  ;; Allow hooks acting on inserted PRIVMSG and NOTICES to process us.
  (let ((erc--msg-prop-overrides `((erc--ctcp . ACTION)
                                   ,@erc--msg-prop-overrides))
        (nick (erc-current-nick)))
    (if erc--use-language-catalog-for-ctcp-action-p
        (progn (erc--ensure-spkr-prop nick)
               (erc-display-message nil 'input (current-buffer) 'ACTION
                                    ?n (propertize nick 'erc--speaker nick)
                                    ?a string ?u "" ?h ""))
      (let ((erc-current-message-catalog erc--message-speaker-catalog))
        (erc-display-message nil nil (current-buffer) 'ctcp-action-input
                             ?p (erc-get-channel-membership-prefix nick)
                             ?n (erc--speakerize-nick nick) ?m string)))))

(defun erc--send-action (target string force)
  "Display STRING, then send to TARGET as a \"CTCP ACTION\" message."
  (erc--send-action-display string)
  (erc--send-action-perform-ctcp target string force))

;; Display interface

(defun erc--ensure-spkr-prop (nick &optional overrides)
  "Add NICK as `erc--spkr' to the current \"msg props\" environment.
Prefer `erc--msg-props' over `erc--msg-prop-overrides' when both
are available.  Also include any members of the alist OVERRIDES,
when present.  Assume NICK itself to be free of any text props,
and return it."
  (cond (erc--msg-props
         (puthash 'erc--spkr nick erc--msg-props)
         (dolist (entry overrides)
           (puthash (car entry) (cdr entry) erc--msg-props)))
        (erc--msg-prop-overrides
         (setq erc--msg-prop-overrides
               `((erc--spkr . ,nick) ,@overrides ,@erc--msg-prop-overrides))))
  nick)

(defun erc-string-invisible-p (string)
  "Check whether STRING is invisible or not.
I.e. any char in it has the `invisible' property set."
  (text-property-any 0 (length string) 'invisible t string))

(defcustom erc-remove-parsed-property t
  "Whether to remove the erc-parsed text property after displaying a message.

The default is to remove it, since it causes ERC to take up extra
memory.  If you have code that relies on this property, then set
this option to nil.

Note that this option is deprecated because a value of nil is
impractical in prolonged sessions with more than a few channels.
Use `erc-insert-post-hook' or similar and the helper function
`erc-find-parsed-property' and friends to stash the current
`erc-response' object as needed.  And instead of using this for
debugging purposes, try `erc-debug-irc-protocol'."
  :type 'boolean
  :group 'erc)
(make-obsolete-variable 'erc-remove-parsed-property
                        "impractical when non-nil" "30.1")

(define-inline erc--assert-input-bounds ()
  (inline-quote
   (progn (when (and (processp erc-server-process)
                     (eq (current-buffer) (process-buffer erc-server-process)))
            ;; It's believed that these only need syncing immediately
            ;; following the first two insertions in a server buffer.
            (set-marker (process-mark erc-server-process) erc-insert-marker))
          (cl-assert (< erc-insert-marker erc-input-marker))
          (cl-assert (= (field-end erc-insert-marker) erc-input-marker)))))

(defvar erc--merge-prop-behind-p nil
  "When non-nil, put merged prop(s) behind existing.")

(defvar erc--refresh-prompt-hook nil
  "Hook called after refreshing the prompt in the affected buffer.")

(defvar-local erc--inhibit-prompt-display-property-p nil
  "Tell `erc-prompt' related functions to avoid the `display' text prop.
Modules can enable this when needing to reserve the prompt's
display property for some other purpose, such as displaying it
elsewhere, abbreviating it, etc.")

(defconst erc--prompt-properties '( rear-nonsticky t
                                    erc-prompt t ; t or `hidden'
                                    field erc-prompt
                                    front-sticky t
                                    read-only t)
  "Mandatory text properties added to ERC's prompt.")

(defvar erc--refresh-prompt-continue-request nil
  "State flag for refreshing prompt in all buffers.
When the value is zero, functions assigned to the variable
`erc-prompt' can set this to run `erc--refresh-prompt-hook' (1)
or `erc--refresh-prompt' (2) in all buffers of the server.")

(defun erc--refresh-prompt-continue (&optional hooks-only-p)
  "Ask ERC to refresh the prompt in all buffers.
Functions assigned to `erc-prompt' can call this if needing to
recreate the prompt in other buffers as well.  With HOOKS-ONLY-P,
run `erc--refresh-prompt-hook' in other buffers instead of doing
a full refresh."
  (when (and erc--refresh-prompt-continue-request
             (zerop erc--refresh-prompt-continue-request))
    (setq erc--refresh-prompt-continue-request (if hooks-only-p 1 2))))

(defun erc--refresh-prompt ()
  "Re-render ERC's prompt when the option `erc-prompt' is a function."
  (erc--assert-input-bounds)
  (unless (erc--prompt-hidden-p)
    (let ((erc--refresh-prompt-continue-request
           (or erc--refresh-prompt-continue-request 0)))
      (when (functionp erc-prompt)
        (save-excursion
          (goto-char (1- erc-input-marker))
          ;; Avoid `erc-prompt' (the named function), which appends a
          ;; space, and `erc-display-prompt', which propertizes all
          ;; but that space.
          (let ((s (funcall erc-prompt))
                (p (point))
                (erc--merge-prop-behind-p t))
            (erc--merge-prop 0 (length s) 'font-lock-face 'erc-prompt-face s)
            (add-text-properties 0 (length s) erc--prompt-properties s)
            (insert s)
            (delete-region erc-insert-marker p))))
      (run-hooks 'erc--refresh-prompt-hook)
      (when-let (((> erc--refresh-prompt-continue-request 0))
                 (n erc--refresh-prompt-continue-request)
                 (erc--refresh-prompt-continue-request -1)
                 (b (current-buffer)))
        (erc-with-all-buffers-of-server erc-server-process
            (lambda () (not (eq b (current-buffer))))
          (if (= n 1)
              (run-hooks 'erc--refresh-prompt-hook)
            (erc--refresh-prompt)))))))

(defun erc--check-msg-prop (prop &optional val)
  "Return PROP's value in `erc--msg-props' when populated.
If VAL is a list, return non-nil if PROP's value appears in VAL.  If VAL
is otherwise non-nil, return non-nil if VAL compares `eq' to the stored
value.  Otherwise, return the stored value."
  (and-let* ((erc--msg-props)
             (v (gethash prop erc--msg-props)))
    (if (consp val) (memq v val) (if val (eq v val) v))))

(defun erc--memq-msg-prop (prop needle)
  "Return non-nil if msg PROP's value is a list containing NEEDLE."
  (and-let* ((erc--msg-props)
             (haystack (gethash prop erc--msg-props))
             ((consp haystack)))
    (memq needle haystack)))

(defmacro erc--get-inserted-msg-beg-at (point at-start-p)
  (macroexp-let2* nil ((point point)
                       (at-start-p at-start-p))
    `(or (and ,at-start-p ,point)
         (and-let* ((p (previous-single-property-change ,point 'erc--msg)))
           (if (and (= p (1- ,point)) (get-text-property p 'erc--msg))
               p
             (1- p))))))

(defmacro erc--get-inserted-msg-end-at (point at-start-p)
  (macroexp-let2 nil point point
    `(1- (next-single-property-change (if ,at-start-p (1+ ,point) ,point)
                                      'erc--msg nil erc-insert-marker))))

(defun erc--get-inserted-msg-beg (&optional point)
  "Maybe return the start of message in an ERC buffer."
  (erc--get-inserted-msg-beg-at (or point (setq point (point)))
                                (get-text-property point 'erc--msg)))

(defun erc--get-inserted-msg-end (&optional point)
  "Return the end of message in an ERC buffer.
Include any trailing white space before final newline.  Expect
POINT to be less than `erc-insert-marker', and don't bother
considering `erc--insert-marker', for now."
  (erc--get-inserted-msg-end-at (or point (setq point (point)))
                                (get-text-property point 'erc--msg)))

(defun erc--get-inserted-msg-bounds (&optional point)
  "Return bounds of message at POINT in an ERC buffer when found.
Search from POINT, when non-nil, instead of `point'.  Return nil
if not found."
  (let ((at-start-p (get-text-property (or point (setq point (point)))
                                       'erc--msg)))
    (and-let* ((b (erc--get-inserted-msg-beg-at point at-start-p)))
      (cons b (erc--get-inserted-msg-end-at point at-start-p)))))

(defun erc--get-inserted-msg-prop (prop)
  "Return the value of text property PROP for some message at point."
  (and-let* ((stack-pos (erc--get-inserted-msg-beg (point))))
    (get-text-property stack-pos prop)))

;; FIXME improve this nascent "message splicing" facility to include a
;; means for modules to adjust inserted messages on either side of the
;; splice position as well as to modify the spliced-in message itself
;; before and after each insertion-related hook runs.  Also add a
;; counterpart to `erc--with-spliced-insertion' for deletions.
(defvar erc--insert-line-splice-function
  #'erc--insert-before-markers-transplanting-hidden
  "Function to handle in-place insertions away from prompt.
Modules that display \"stateful\" messages, where one message's content
depends on prior messages, should advise this locally as needed.")

(defmacro erc--with-spliced-insertion (marker-or-pos &rest body)
  "In BODY, ensure `erc-insert-line' inserts messages at MARKER-OR-POS.
If MARKER-OR-POS is a marker, let it advance normally (and permanently)
with each insertion.  Allow modules to influence insertion by binding
`erc--insert-line-function' to `erc--insert-line-splice-function' around
BODY.  Note that as of ERC 5.6, this macro cannot handle multiple
successive calls to `erc-insert-line' in BODY, such as when replaying
a history backlog."
  (declare (indent 1))
  (let ((marker (make-symbol "marker")))
    `(progn
       (cl-assert (= ?\n (char-before ,marker-or-pos)))
       (cl-assert (null erc--insert-line-function))
       (let* ((,marker (and (not (markerp ,marker-or-pos))
                            (copy-marker ,marker-or-pos)))
              (erc--insert-marker (or ,marker ,marker-or-pos))
              (erc--insert-line-function erc--insert-line-splice-function))
         (prog1 (progn ,@body)
           (when ,marker (set-marker ,marker nil)))))))

(defun erc--traverse-inserted (beg end fn)
  "Visit messages between BEG and END and run FN in narrowed buffer.
If END is a marker, possibly update its position."
  (unless (markerp end)
    (setq end (set-marker (make-marker) (or end erc-insert-marker))))
  (unless (eq end erc-insert-marker)
    (set-marker end (min erc-insert-marker end)))
  (save-excursion
    (goto-char beg)
    (let ((b (if (get-text-property (point) 'erc--msg)
                 (point)
               (next-single-property-change (point) 'erc--msg nil end))))
      (while-let ((b)
                  ((< b end))
                  (e (next-single-property-change (1+ b) 'erc--msg nil end)))
        (save-restriction
          (narrow-to-region b e)
          (funcall fn))
        (setq b e))))
  (unless (eq end erc-insert-marker)
    (set-marker end nil)))

(defvar erc--insert-invisible-as-intangible-p nil
  "When non-nil, ensure certain invisible messages are also intangible.
That is, single out any message inserted via `erc-insert-line'
that lacks a trailing newline but has a t-valued `invisible'
property anywhere along its length, and ensure it's both
`invisible' t and `intangible' t throughout.  Note that this is
merely an escape hatch for accessing aberrant pre-5.6 behavior
that ERC considers a bug because it applies a practice described
as obsolete in the manual, and it does so heavy-handedly.  That
the old behavior only acted when the input lacked a trailing
newline was likely accidental but is ultimately incidental.  See
info node `(elisp) Special Properties' for specifics.  Beware
that this flag and the behavior it restores may disappear at any
time, so if you need them, please let ERC know with \\[erc-bug].")

(defvar erc--insert-line-function nil
  "When non-nil, an `insert'-like function for inserting messages.
Modules, like `fill-wrap', that leave a marker at the beginning of an
inserted message clearly want that marker to advance along with text
inserted at that position.  This can be addressed by binding this
variable to `insert-before-markers' around calls to `display-message'.")

(defvar erc--insert-marker nil
  "Internal override for `erc-insert-marker'.")

(define-obsolete-function-alias 'erc-display-line-1 'erc-insert-line "30.1")
(defun erc-insert-line (string buffer)
  "Insert STRING in an `erc-mode' BUFFER.
When STRING is nil, do nothing.  Otherwise, start off by running
`erc-insert-pre-hook' in BUFFER with `erc-insert-this' bound to
t.  If the latter remains non-nil afterward, insert STRING into
BUFFER, ensuring a trailing newline.  After that, narrow BUFFER
around STRING, along with its final line ending, and run
`erc-insert-modify' and `erc-insert-post-hook', respectively.  In
all cases, run `erc-insert-done-hook' unnarrowed before exiting,
and update positions in `buffer-undo-list'.

In general, expect to be called from a higher-level insertion
function, like `erc-display-message', especially when modules
should consider STRING as a candidate for formatting with
enhancements like indentation, fontification, timestamping, etc.
Otherwise, when called directly, allow built-in modules to ignore
STRING, which may make it appear incongruous in situ (unless
preformatted or anticipated by third-party members of the various
modification hooks)."
  (when string
    (with-current-buffer (or buffer (process-buffer erc-server-process))
      (let (insert-position)
        ;; Initialize ^ below to thwart rogue `erc-insert-pre-hook'
        ;; members that dare to modify the buffer's length.
        (let ((buffer-undo-list t)
              (inhibit-read-only t))
          (unless (string-suffix-p "\n" string)
            (setq string (concat string "\n"))
            (when (and erc--insert-invisible-as-intangible-p
                       (erc-string-invisible-p string))
              (erc-put-text-properties 0 (length string)
                                       '(invisible intangible) string)))
          (erc-log (concat "erc-display-message: " string
                           (format "(%S)" string) " in buffer "
                           (format "%s" buffer)))
          (setq erc-insert-this t)
          (run-hook-with-args 'erc-insert-pre-hook string)
          (setq insert-position (marker-position (or erc--insert-marker
                                                     erc-insert-marker)))
          (if (null erc-insert-this)
              ;; Leave erc-insert-this set to t as much as possible.  Fran
              ;; Litterio <franl> has seen erc-insert-this set to nil while
              ;; erc-send-pre-hook is running, which should never happen.  This
              ;; may cure it.
              (setq erc-insert-this t)
            (save-excursion ;; to restore point in the new buffer
              (save-restriction
                (widen)
                (goto-char insert-position)
                (if erc--insert-line-function
                    (funcall erc--insert-line-function string)
                  (insert string))
                (erc--assert-input-bounds)
                ;; run insertion hook, with point at restored location
                (save-restriction
                  (narrow-to-region insert-position (point))
                  (run-hooks 'erc-insert-modify-hook)
                  (run-hooks 'erc-insert-post-hook)
                  (when erc-remove-parsed-property
                    (remove-text-properties (point-min) (point-max)
                                            '(erc-parsed nil tags nil)))
                  (cl-assert (> (- (point-max) (point-min)) 1))
                  (let ((props (if erc--msg-props
                                   (erc--order-text-properties-from-hash
                                    erc--msg-props)
                                 '(erc--msg unknown))))
                    (add-text-properties (point-min) (1+ (point-min)) props)))
                (erc--refresh-prompt)))))
        (run-hooks 'erc-insert-done-hook)
        (erc-update-undo-list (- (or (marker-position (or erc--insert-marker
                                                          erc-insert-marker))
                                     (point-max))
                                 insert-position))))))

(defun erc-update-undo-list (shift)
  ;; Translate buffer positions in buffer-undo-list by SHIFT.
  (unless (or (zerop shift) (atom buffer-undo-list))
    (let ((list buffer-undo-list) elt)
      (while list
        (setq elt (car list))
        (cond ((integerp elt)           ; POSITION
               (cl-incf (car list) shift))
              ((or (atom elt)           ; nil, EXTENT
                   ;; (eq t (car elt))  ; (t . TIME)
                   (markerp (car elt))) ; (MARKER . DISTANCE)
               nil)
              ((integerp (car elt))     ; (BEGIN . END)
               (cl-incf (car elt) shift)
               (cl-incf (cdr elt) shift))
              ((stringp (car elt))      ; (TEXT . POSITION)
               (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
              ((null (car elt))         ; (nil PROPERTY VALUE BEG . END)
               (let ((cons (nthcdr 3 elt)))
                 (cl-incf (car cons) shift)
                 (cl-incf (cdr cons) shift))))
        (setq list (cdr list))))))

(defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
  "Regexp which matches all valid characters in a IRC nickname.")

(defun erc-is-valid-nick-p (nick)
  "Check if NICK is a valid IRC nickname."
  (string-match (concat "\\`" erc-valid-nick-regexp "\\'") nick))

(defun erc--route-insertion (string buffer)
  "Insert STRING in BUFFER.
See `erc-display-message' for acceptable BUFFER types."
  (let (seen msg-props)
    (dolist (buf (cond
                  ((bufferp buffer) (list buffer))
                  ((consp buffer)
                   (setq msg-props erc--msg-props)
                   buffer)
                  ((processp buffer) (list (process-buffer buffer)))
                  ((eq 'all buffer)
                   ;; Hmm, or all of the same session server?
                   (erc-buffer-list nil erc-server-process))
                  ((and-let* (((eq 'active buffer))
                              (b (erc-active-buffer)))
                        (list b)))
                  ((erc-server-buffer-live-p)
                   (list (process-buffer erc-server-process)))
                  (t (list (current-buffer)))))
      (when (buffer-live-p buf)
        (when msg-props
          (setq erc--msg-props (copy-hash-table msg-props)))
        (erc-insert-line string buf)
        (setq seen t)))
    (unless (or seen (null buffer))
      (erc--route-insertion string nil))))

(defun erc-display-line (string &optional buffer)
  "Insert STRING in BUFFER as a plain \"local\" message.
Take pains to ensure modification hooks see messages created by
the old pattern (erc-display-line (erc-make-notice) my-buffer) as
being equivalent to a `erc-display-message' TYPE of `notice'."
  (let ((erc--msg-prop-overrides erc--msg-prop-overrides))
    (when (eq 'erc-notice-face (get-text-property 0 'font-lock-face string))
      (unless (assq 'erc--msg erc--msg-prop-overrides)
        (push '(erc--msg . notice) erc--msg-prop-overrides)))
    (erc-display-message nil nil buffer string)))

(defvar erc--merge-text-properties-p nil
  "Non-nil when `erc-put-text-property' defers to `erc--merge-prop'.")

;; To save space, we could maintain a map of all readable property
;; values and optionally dispense archetypal constants in their place
;; in order to ensure all occurrences of some list (a b) across all
;; text-properties in all ERC buffers are actually the same object.
(defun erc--merge-prop (from to prop val &optional object cache-fn)
  "Combine existing PROP values with VAL between FROM and TO in OBJECT.
For spans where PROP is non-nil, cons VAL onto the existing
value, ensuring a proper list.  Otherwise, just set PROP to VAL.
When VAL is itself a list, prepend its members onto an existing
value.  Call CACHE-FN, when given, with the new value for prop.
It must return a suitable replacement or the same value.  See
also `erc-button-add-face'."
  (let ((old (get-text-property from prop object))
        (pos from)
        (end (next-single-property-change from prop object to))
        new)
    (while (< pos to)
      (setq new (if old
                    ;; Can't `nconc' without more info.
                    (if erc--merge-prop-behind-p
                        `(,@(ensure-list old) ,@(ensure-list val))
                      (if (listp val)
                          (append val (ensure-list old))
                        (cons val (ensure-list old))))
                  val))
      (when cache-fn
        (setq new (funcall cache-fn new)))
      (put-text-property pos end prop new object)
      (setq pos end
            old (get-text-property pos prop object)
            end (next-single-property-change pos prop object to)))))

(defun erc--remove-from-prop-value-list (from to prop val &optional object)
  "Remove VAL from text PROP value between FROM and TO.
If current value is VAL itself, remove the property entirely.
When VAL is a list, act as if this function were called
repeatedly with VAL set to each of VAL's members."
  (let ((old (get-text-property from prop object))
        (pos from)
        (end (next-single-property-change from prop object to))
        new)
    (while (< pos to)
      (when old
        (if (setq new (and (consp old) (if (consp val)
                                           (seq-difference old val)
                                         (remq val old))))
            (put-text-property pos end prop
                               (if (cdr new) new (car new)) object)
          (when (pcase val
                  ((pred consp) (or (consp old) (memq old val)))
                  (_ (if (consp old) (memq val old) (eq old val))))
            (remove-text-properties pos end (list prop nil) object))))
      (setq pos end
            old (get-text-property pos prop object)
            end (next-single-property-change pos prop object to)))))

(defun erc--reserve-important-text-props (beg end plist &optional object)
  "Record text-property pairs in PLIST as important between BEG and END.
Also mark the message being inserted as containing these important props
so modules performing destructive modifications can later restore them.
Expect to run in a narrowed buffer at message-insertion time."
  (when erc--msg-props
    (let ((existing (erc--check-msg-prop 'erc--important-prop-names)))
      (puthash 'erc--important-prop-names (cl-union existing (map-keys plist))
               erc--msg-props)))
  (erc--merge-prop beg end 'erc--important-props plist object))

(defun erc--restore-important-text-props (props &optional beg end)
  "Restore PROPS where recorded in the accessible portion of the buffer.
Expect to run in a narrowed buffer at message-insertion time.  Limit the
effect to the region between buffer positions BEG and END, when non-nil.

Callers should be aware that this function fails if the property
`erc--important-props' has an empty value almost anywhere along the
affected region.  Use the function `erc--remove-from-prop-value-list' to
ensure that props with empty values are excised completely."
  (when-let ((registered (erc--check-msg-prop 'erc--important-prop-names))
             (present (seq-intersection props registered))
             (b (or beg (point-min)))
             (e (or end (point-max))))
    (while-let
        (((setq b (text-property-not-all b e 'erc--important-props nil)))
         (val (get-text-property b 'erc--important-props))
         (q (next-single-property-change b 'erc--important-props nil e)))
      (while-let ((k (pop val))
                  (v (pop val)))
        (when (memq k present)
          (put-text-property b q k v)))
      (setq b q))))

(defvar erc-legacy-invisible-bounds-p nil
  "Whether to hide trailing rather than preceding newlines.
Beginning in ERC 5.6, invisibility extends from a message's
preceding newline to its last non-newline character.")
(make-obsolete-variable 'erc-legacy-invisible-bounds-p
                        "decremented interval now permanent" "30.1")

(defun erc--insert-before-markers-transplanting-hidden (string)
  "Insert STRING before markers and migrate any `invisible' props.
Expect to be called with `point' at the start of an inserted message,
i.e., one with an `erc--msg' property.  Check the message prop header
for invisibility props advertised via `erc--hide'.  When found, remove
them from the previous newline, and add them to the newline suffixing
the inserted version of STRING."
  (let* ((after (and (not erc-legacy-invisible-bounds-p)
                     (get-text-property (point) 'erc--hide)))
         (before (and after (get-text-property (1- (point)) 'invisible)))
         (a (and after (ensure-list after)))
         (b (and before (ensure-list before)))
         (new (and before (erc--solo (cl-intersection b a)))))
    (when new
      (erc--remove-from-prop-value-list (1- (point)) (point) 'invisible a))
    (prog1 (insert-before-markers string)
      (when new
        (erc--merge-prop (1- (point)) (point) 'invisible new)))))

(defun erc--hide-message (value)
  "Apply `invisible' text-property with VALUE to current message.
Expect to run in a narrowed buffer during message insertion.
Begin the invisible interval at the previous message's trailing
newline and end before the current message's.  If the preceding
message ends in a double newline or there is no previous message,
don't bother including the preceding newline.  Additionally,
record VALUE as part of the `erc--hide' property in the
\"msg-props\" header."
  (if erc-legacy-invisible-bounds-p
      ;; Before ERC 5.6, this also used to add an `intangible'
      ;; property, but the docs say it's now obsolete.
      (erc--merge-prop (point-min) (point-max) 'invisible value)
    (let ((old-hide (erc--check-msg-prop 'erc--hide))
          (beg (point-min))
          (end (point-max)))
      (puthash 'erc--hide (if old-hide
                              `(,value . ,(ensure-list old-hide))
                            value)
               erc--msg-props)
      (save-restriction
        (widen)
        (when (or (<= beg 4) (= ?\n (char-before (- beg 2))))
          (cl-incf beg))
        (erc--merge-prop (1- beg) (1- end) 'invisible value)))))

(defun erc--toggle-hidden (prop arg)
  "Toggle invisibility for spec member PROP.
Treat ARG in a manner similar to mode toggles defined by
`define-minor-mode'."
  (when arg
    (setq arg (prefix-numeric-value arg)))
  (if (memq prop (ensure-list buffer-invisibility-spec))
      (unless (natnump arg)
        (remove-from-invisibility-spec prop))
    (when (or (not arg) (natnump arg))
      (add-to-invisibility-spec prop))))

(defun erc--delete-inserted-message-naively (beg-or-point &optional end)
  "Remove message between BEG and END.
Do this without updating messages on either side even if their
appearance was somehow influenced by the newly absent message.
Expect BEG and END to match bounds as returned by the function
`erc--get-inserted-msg-bounds'.  Ensure all markers residing at
the start of the deleted message end up at the beginning of the
subsequent message."
  (let ((beg beg-or-point))
    (save-restriction
      (widen)
      (unless end
        (setq end (erc--get-inserted-msg-bounds beg-or-point)
              beg (pop end)))
      (with-silent-modifications
        (if erc-legacy-invisible-bounds-p
            (delete-region beg (1+ end))
          (save-excursion
            (goto-char beg)
            (insert-before-markers
             (substring (delete-and-extract-region (1- (point)) (1+ end))
                        -1))))))))

(defvar erc--ranked-properties
  '( erc--msg erc--spkr erc--ts erc--skip
     erc--cmd erc--hide erc--ctcp erc--ephemeral))

(defun erc--order-text-properties-from-hash (table)
  "Return a plist of text props from items in TABLE.
Ensure props in `erc--ranked-properties' appear last and in
reverse order so they end up sorted in buffer interval plists for
retrieval by `text-properties-at' and friends."
  (let (out)
    (dolist (k erc--ranked-properties)
      (when-let ((v (gethash k table)))
        (remhash k table)
        (setq out (nconc (list k v) out))))
    (maphash (lambda (k v) (setq out (nconc (list k v) out))) table)
    out))

(defun erc-display-message-highlight (type string)
  "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.

See also `erc-make-notice'."
  (cond ((eq type 'notice)
         (erc-make-notice string))
        (t
         (erc-put-text-property
          0 (length string)
          'font-lock-face (or (intern-soft
			       (concat "erc-" (symbol-name type) "-face"))
                              'erc-default-face)
          string)
         string)))

(defvar erc-lurker-state nil
  "Track the time of the last PRIVMSG for each (server,nick) pair.

This is implemented as a hash of hashes, where the outer key is
the canonicalized server name (as returned by
`erc-canonicalize-server-name') and the outer value is a hash
table mapping nicks (as returned by `erc-lurker-maybe-trim') to
the times of their most recently received PRIVMSG on any channel
on the given server.")

(defcustom erc-lurker-trim-nicks t
  "If t, trim trailing `erc-lurker-ignore-chars' from nicks.

This causes e.g. nick and nick\\=` to be considered as the same
individual for activity tracking and lurkiness detection
purposes."
  :group 'erc-lurker
  :type 'boolean)

(defcustom erc-lurker-ignore-chars "`_"
  "Characters at the end of a nick to strip for activity tracking purposes.

See also `erc-lurker-trim-nicks'."
  :group 'erc-lurker
  :type 'string)

(defun erc-lurker-maybe-trim (nick)
  "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.

Returns NICK unmodified unless `erc-lurker-trim-nicks' is
non-nil."
  (if erc-lurker-trim-nicks
      (string-trim-right
       nick (rx-to-string `(+ (in ,@(string-to-list erc-lurker-ignore-chars)))))
    nick))

(defcustom erc-lurker-hide-list nil
  "List of IRC type messages to hide when sent by lurkers.

A typical value would be \(\"JOIN\" \"PART\" \"QUIT\").
See also `erc-lurker-p' and `erc-hide-list'."
  :group 'erc-lurker
  :type 'erc-message-type)

(defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
  "Nicks from which no PRIVMSGs have been received within this
interval (in units of seconds) are considered lurkers by
`erc-lurker-p' and as a result their messages of types in
`erc-lurker-hide-list' will be hidden."
  :group 'erc-lurker
  :type 'integer)

(defun erc-lurker-initialize ()
  "Initialize ERC lurker tracking functionality.

This function adds `erc-lurker-update-status' to
`erc-insert-pre-hook' in order to record the time of each nick's
most recent PRIVMSG as well as initializing the state variable
storing this information."
  (setq erc-lurker-state (make-hash-table :test 'equal))
  (add-hook 'erc-insert-pre-hook #'erc-lurker-update-status))

(defun erc-lurker-cleanup ()
  "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.

This should be called regularly to avoid excessive resource
consumption for long-lived IRC or Emacs sessions."
  (maphash
   (lambda (server hash)
     (maphash
      (lambda (nick last-PRIVMSG-time)
        (when
	    (time-less-p erc-lurker-threshold-time
			 (time-since last-PRIVMSG-time))
          (remhash nick hash)))
      hash)
     (if (zerop (hash-table-count hash))
         (remhash server erc-lurker-state)))
   erc-lurker-state))

(defvar erc-lurker-cleanup-count 0
  "Internal counter variable for use with `erc-lurker-cleanup-interval'.")

(defvar erc-lurker-cleanup-interval 100
  "Frequency of cleaning up stale erc-lurker state.

`erc-lurker-update-status' calls `erc-lurker-cleanup' once for
every `erc-lurker-cleanup-interval' updates to
`erc-lurker-state'.  This is designed to limit the memory
consumption of lurker state during long Emacs sessions and/or ERC
sessions with large numbers of incoming PRIVMSGs.")

(defun erc-lurker-update-status (_message)
  "Update `erc-lurker-state' if necessary.

This function is called from `erc-insert-pre-hook'.  If the
current message is a PRIVMSG, update `erc-lurker-state' to
reflect the fact that its sender has issued a PRIVMSG at the
current time.  Otherwise, take no action.

This function depends on the fact that `erc-display-message'
dynamically binds `erc-message-parsed', which is used to check if
the current message is a PRIVMSG and to determine its sender.
See also `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.

In order to limit memory consumption, this function also calls
`erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
updates of `erc-lurker-state'."
  (when (and (boundp 'erc-message-parsed)
             (erc-response-p erc-message-parsed))
    (let* ((command (erc-response.command erc-message-parsed))
           (sender
            (erc-lurker-maybe-trim
             (car (erc-parse-user
                   (erc-response.sender erc-message-parsed)))))
           (server
            (erc-canonicalize-server-name erc-server-announced-name)))
      (when (equal command "PRIVMSG")
        (when (>= (cl-incf erc-lurker-cleanup-count)
                  erc-lurker-cleanup-interval)
          (setq erc-lurker-cleanup-count 0)
          (erc-lurker-cleanup))
        (unless (gethash server erc-lurker-state)
          (puthash server (make-hash-table :test 'equal) erc-lurker-state))
        (puthash sender (current-time)
                 (gethash server erc-lurker-state))))))

(defun erc-lurker-p (nick)
  "Predicate indicating NICK's lurking status on the current server.

Lurking is the condition where NICK has issued no PRIVMSG on this
server within `erc-lurker-threshold-time'.  See also
`erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
  (unless erc-lurker-state (erc-lurker-initialize))
  (let* ((server
          (erc-canonicalize-server-name erc-server-announced-name))
         (last-PRIVMSG-time
          (gethash (erc-lurker-maybe-trim nick)
                   (gethash server erc-lurker-state (make-hash-table)))))
    (or (null last-PRIVMSG-time)
	(time-less-p erc-lurker-threshold-time
		     (time-since last-PRIVMSG-time)))))

(defcustom erc-common-server-suffixes
  '(("openprojects.net\\'" . "OPN")
    ("freenode.net\\'" . "freenode")
    ("oftc.net\\'" . "OFTC"))
  "Alist of common server name suffixes.
This variable is used in mode-line display to save screen
real estate.  Set it to nil if you want to avoid changing
displayed hostnames."
  :group 'erc-mode-line-and-header
  :type 'alist)

(defun erc-canonicalize-server-name (server)
  "Return canonical network name for SERVER or `erc-server-announced-name'.
SERVER is matched against `erc-common-server-suffixes'."
  (when server
    (or (cdar (cl-remove-if-not
               (lambda (net) (string-match (car net) server))
               erc-common-server-suffixes))
        erc-server-announced-name)))

(defun erc-add-targets (scope target-list)
  (let ((targets
	 (mapcar (lambda (targets) (member scope targets)) target-list)))
    (cdr (apply #'append (delete nil targets)))))

(defun erc-hide-current-message-p (parsed)
  "Predicate indicating whether the parsed ERC response PARSED should be hidden.

Messages are always hidden if the message type of PARSED appears in
`erc-hide-list'.  Message types that appear in `erc-network-hide-list'
or `erc-channel-hide-list' are only hidden if the target matches
the network or channel in the list.  In addition, messages whose type
is a member of `erc-lurker-hide-list' are hidden if `erc-lurker-p'
returns non-nil."
  (let* ((command (erc-response.command parsed))
         (sender (car (erc-parse-user (erc-response.sender parsed))))
         (channel (car (erc-response.command-args parsed)))
         (network (or (and (erc-network) (erc-network-name))
		      (erc-shorten-server-name
		       (or erc-server-announced-name
			   erc-session-server))))
	 (current-hide-list
	  (when erc-network-hide-list
	    (erc-add-targets network erc-network-hide-list)))
	 (current-hide-list
	  (append current-hide-list
		  (when erc-channel-hide-list
		    (erc-add-targets channel erc-channel-hide-list)))))
    (or (member command erc-hide-list)
        (member command current-hide-list)
        (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))

(defun erc-display-message (parsed type buffer msg &rest args)
  "Display MSG in BUFFER.

Insert MSG or text derived from MSG into an ERC buffer, possibly
after applying formatting by way of either a `format-spec' known
to a message-catalog entry or a TYPE known to a specialized
string handler.  Additionally, derive metadata, faces, and other
text properties from the various overloaded parameters, such as
PARSED, when it's an `erc-response' object, and MSG, when it's a
key (symbol) for a \"message catalog\" entry.  Expect ARGS, when
applicable, to be `format-spec' args known to such an entry, and
TYPE, when non-nil, to be a symbol handled by
`erc-display-message-highlight' (necessarily accompanied by a
string MSG).  Expect BUFFER to be among the sort accepted by the
function `erc-display-line'.

When non-nil, expect BUFFER to be a live `erc-mode' buffer, a
list of such buffers, or the symbols `all' or `active'.  If
`all', insert STRING in all buffers for the current session.  If
`active', defer to the function `erc-active-buffer', which may
return the session's server buffer if the previously active
buffer has been killed.  If BUFFER is nil or a network process,
pretend it's set to the appropriate server buffer.  Otherwise,
use the current buffer.

When TYPE is a list of symbols, call handlers from left to right
without influencing how they behave when encountering existing
faces.  As of ERC 5.6, expect a TYPE of (notice error) to insert
MSG with `font-lock-face' as `erc-error-face' throughout.
However, when the list of symbols begins with t, tell compatible
handlers to compose rather than clobber faces.  For example,
expect a TYPE of (t notice error) to result in `font-lock-face'
being (erc-error-face erc-notice-face) throughout MSG when
`erc-notice-highlight-type' is left at its default, `all'.

As of ERC 5.6, assume third-party code will use this function
instead of lower-level ones, like `erc-insert-line', to insert
arbitrary informative messages as if sent by the server.  That
is, tell modules to treat a \"local\" message for which PARSED is
nil like any other server-sent message."
  (let* ((erc--msg-props
          (or erc--msg-props
              (let ((table (make-hash-table))
                    (cmd (and parsed (erc--get-eq-comparable-cmd
                                      (erc-response.command parsed)))))
                (puthash 'erc--msg
                         (cond ((and msg (symbolp msg)) msg)
                               (type (pcase type
                                       ((pred symbolp) type)
                                       ((pred listp)
                                        (intern (mapconcat #'prin1-to-string
                                                           type "-")))
                                       (_ 'unknown)))
                               (t 'unknown))
                         table)
                (when cmd
                  (puthash 'erc--cmd cmd table))
                (when erc--msg-prop-overrides
                  (pcase-dolist (`(,k . ,v) (reverse erc--msg-prop-overrides))
                    (when v (puthash k v table))))
                table)))
         (erc-message-parsed parsed)
         (string (if (symbolp msg) (apply #'erc-format-message msg args) msg)))
    (setq string
          (cond
           ((null type)
            string)
           ((listp type)
            (let ((erc--merge-text-properties-p
                   (and (eq (car type) t) (setq type (cdr type)))))
              (dolist (type type)
                (setq string (erc-display-message-highlight type string))))
            string)
           ((symbolp type)
            (erc-display-message-highlight type string))))

    (if (not (erc-response-p parsed))
        (erc--route-insertion string buffer)
      (unless (erc-hide-current-message-p parsed)
        (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
	(when (erc-response.tags parsed)
	  (erc-put-text-property 0 (length string) 'tags (erc-response.tags parsed)
				 string))
        (erc--route-insertion string buffer)))))

(defun erc-message-type-member (position list)
  "Return non-nil if the erc-parsed text-property at POSITION is in LIST.

This function relies on the erc-parsed text-property being
present."
  (let ((prop-val (erc-get-parsed-vector position)))
    (and prop-val (member (erc-response.command prop-val) list))))

(defvar erc--called-as-input-p nil
  "Non-nil when a user types a \"/slash\" command.
Remains bound until `erc-cmd-SLASH' returns.")

(defvar erc--current-line-input-split nil
  "Current `erc--input-split' instance when processing user input.
This is for special cases in which a \"slash\" command needs
details about the input it's handling or needs to detect whether
it's been dispatched by `erc-send-current-line'.")

(defvar erc--allow-empty-outgoing-lines-p nil
  "Flag to opt out of last-minute padding of empty lines.
Useful to extensions, like `multiline', and for interop with
IRC-adjacent protocols.")

(defvar-local erc-send-input-line-function #'erc-send-input-line
  "Function for sending lines lacking a leading \"slash\" command.
When prompt input starts with a \"slash\" command, like \"/MSG\",
ERC calls a corresponding handler, like `erc-cmd-MSG'.  But
normal \"chat\" input also needs processing, for example, to
convert it into a proper IRC command.  ERC calls this variable's
value to perform that task, which, by default, simply involves
constructing a \"PRIVMSG\" with the current channel or query
partner as the target.  Some libraries, like `erc-dcc', use this
for other purposes.")

(defun erc-send-input-line (target line &optional force)
  "Send LINE to TARGET."
  (when-let ((target)
             (cmem (erc-get-channel-member (erc-current-nick))))
    (setf (erc-channel-user-last-message-time (cdr cmem))
          (erc-compat--current-lisp-time)))
  (when (and (not erc--allow-empty-outgoing-lines-p) (string= line "\n"))
    (setq line " \n"))
  (erc-message "PRIVMSG" (concat target " " line) force))

(defun erc-get-arglist (fun)
  "Return the argument list of a function without the parens."
  (let ((arglist (format "%S" (help-function-arglist fun))))
    (if (string-match "\\`(\\(.*\\))\\'" arglist)
        (match-string 1 arglist)
      arglist)))

(defun erc-command-no-process-p (str)
  "Return non-nil if STR is an ERC command that can be run when the process
is not alive, nil otherwise."
  (let ((fun (erc-extract-command-from-line str)))
    (and fun
         (symbolp (car fun))
         (get (car fun) 'process-not-needed))))

(defun erc-command-name (cmd)
  "For CMD being the function name of a ERC command, something like
erc-cmd-FOO, this returns a string /FOO."
  (let ((command-name (symbol-name cmd)))
    (if (string-match "\\`erc-cmd-\\(.*\\)\\'" command-name)
        (concat "/" (match-string 1 command-name))
      command-name)))

(defun erc-process-input-line (line &optional force no-command)
  "Dispatch a slash-command or chat-input handler from user-input LINE.
If simplistic validation fails, print an error and return nil.
Otherwise, defer to an appropriate handler.  For \"slash\" commands,
like \"/JOIN\", expect a handler, like `erc-cmd-JOIN', to return non-nil
if LINE is fit for echoing as a command line when executing scripts.
For normal chat input, expect a handler to return non-nil if a message
was successfully processed as an outgoing \"PRIVMSG\".  If LINE is a
slash command, and ERC can't find a corresponding handler of the form
`erc-cmd-<COMMAND>', pass LINE to `erc-cmd-default', treating it as a
catch-all handler.  Otherwise, for normal chat input, pass LINE and the
boolean argument FORCE to `erc-send-input-line-function'.  With a
non-nil NO-COMMAND, always treat LINE as normal chat input rather than a
slash command."
  (let ((command-list (erc-extract-command-from-line line)))
    (if (and command-list
             (not no-command))
        (let* ((cmd  (nth 0 command-list))
               (args (nth 1 command-list))
               (erc--called-as-input-p t))
          (erc--server-last-reconnect-display-reset (erc-server-buffer))
          (condition-case nil
              (if (listp args)
                  (apply cmd args)
                (funcall cmd args))
            (wrong-number-of-arguments
             (erc-display-message nil 'error (current-buffer) 'incorrect-args
                                  ?c (erc-command-name cmd)
                                  ?u (or (erc-get-arglist cmd)
                                         "")
                                  ?d (format "%s\n"
                                             (or (documentation cmd) "")))
             nil)))
      (let ((r (erc-default-target)))
        (if r
            (funcall erc-send-input-line-function r line force)
          (erc-display-message nil '(notice error) (current-buffer) 'no-target)
          nil)))))

(defconst erc--shell-parse-regexp
  (rx (or (+ (not (any ?\s ?\t ?\n ?\\ ?\" ?' ?\;)))
          (: ?' (group (* (not ?'))) (? ?'))
          (: ?\" (group (* (or (not (any ?\" ?\\)) (: ?\\ nonl)))) (? ?\"))
          (: ?\\ (group (? (or nonl ?\n)))))))

(defun erc--split-string-shell-cmd (string)
  "Parse whitespace-separated arguments in STRING."
  ;; From `shell--parse-pcomplete-arguments' and friends.  Quirk:
  ;; backslash-escaped characters appearing within spans of double
  ;; quotes are unescaped.
  (with-temp-buffer
    (insert string)
    (let ((end (point))
          args)
      (goto-char (point-min))
      (while (and (skip-chars-forward " \t") (< (point) end))
        (let (arg)
          (while (looking-at erc--shell-parse-regexp)
            (goto-char (match-end 0))
            (cond ((match-beginning 3) ; backslash escape
                   (push (if (= (match-beginning 3) (match-end 3))
                             "\\"
                           (match-string 3))
                         arg))
                  ((match-beginning 2) ; double quote
                   (push (replace-regexp-in-string (rx ?\\ (group nonl))
                                                   "\\1" (match-string 2))
                         arg))
                  ((match-beginning 1) ; single quote
                   (push (match-string 1) arg))
                  (t (push (match-string 0) arg))))
          (push (string-join (nreverse arg)) args)))
      (nreverse args))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                    Input commands handlers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun erc--connected-and-joined-p ()
  (and (erc--current-buffer-joined-p)
       erc-server-connected))

(defun erc-cmd-GMSG (line)
  "Send LINE to all channels on all networks you are on."
  (setq line (string-remove-prefix " " line))
  (erc-with-all-buffers-of-server nil
      #'erc--connected-and-joined-p
    (erc-send-message line)))
(put 'erc-cmd-GMSG 'do-not-parse-args t)

(defun erc-cmd-AMSG (line)
  "Send LINE to all channels of the current network.
Interactively, prompt for the line of text to send."
  (interactive "sSend to all channels on this network: ")
  (setq line (string-remove-prefix " " line))
  (erc-with-all-buffers-of-server erc-server-process
      #'erc--connected-and-joined-p
    (erc-send-message line)))
(put 'erc-cmd-AMSG 'do-not-parse-args t)

(defun erc-cmd-GME (line)
  "Send LINE as an action to all channels on all networks you are on."
  (erc-with-all-buffers-of-server nil
      #'erc--connected-and-joined-p
    (erc-cmd-ME line)))
(put 'erc-cmd-GME 'do-not-parse-args t)

(defun erc-cmd-AME (line)
  "Send LINE as an action to all channels on the current network."
  (erc-with-all-buffers-of-server erc-server-process
      #'erc--connected-and-joined-p
    (erc-cmd-ME line)))
(put 'erc-cmd-AME 'do-not-parse-args t)

(defun erc-cmd-SAY (line)
  "Send LINE to the current query or channel as a message, not a command.

Use this when you want to send a message with a leading `/'.  Note
that since multi-line messages are never a command, you don't
need this when pasting multiple lines of text."
  (if (string-match "^\\s-*$" line)
      nil
    (string-match "^ ?\\(.*\\)" line)
    (erc-send-message (match-string 1 line) nil)))
(put 'erc-cmd-SAY 'do-not-parse-args t)

(defun erc-cmd-SET (line)
  "Set the variable named by the first word in LINE to some VALUE.
VALUE is computed by evaluating the rest of LINE in Lisp."
  (cond
   ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
    (let ((var (read (concat "erc-" (match-string 1 line))))
          (val (read (match-string 2 line))))
      (if (boundp var)
          (progn
            (set var (eval val t))
            (erc-display-message
             nil nil 'active (format "Set %S to %S" var val))
            t)
        (setq var (read (match-string 1 line)))
        (if (boundp var)
            (progn
              (set var (eval val t))
              (erc-display-message
               nil nil 'active (format "Set %S to %S" var val))
              t)
          (erc-display-message nil 'error 'active 'variable-not-bound)
          nil))))
   ((string-match "^\\s-*$" line)
    (erc-display-line
     (concat "Available user variables:\n"
             (apply
              #'concat
              (mapcar
               (lambda (var)
                 (let ((val (symbol-value var)))
                   (concat (format "%S:" var)
                           (if (consp val)
                               (concat "\n" (pp-to-string val))
                             (format " %S\n" val)))))
               (apropos-internal "^erc-" 'custom-variable-p))))
     (current-buffer))
    t)
   (t nil)))
(defalias 'erc-cmd-VAR #'erc-cmd-SET)
(defalias 'erc-cmd-VARIABLE #'erc-cmd-SET)
(put 'erc-cmd-SET 'do-not-parse-args t)
(put 'erc-cmd-SET 'process-not-needed t)

(defun erc-cmd-default (line)
  "Fallback command.

Commands for which no erc-cmd-xxx exists, are tunneled through
this function.  LINE is sent to the server verbatim, and
therefore has to contain the command itself as well."
  (erc-log (format "cmd: DEFAULT: %s" line))
  (erc-server-send (string-trim-right (substring line 1) "[\r\n]"))
  t)

(defvar erc--read-time-period-history nil)

(defun erc--read-time-period (prompt)
  "Read a time period on the \"2h\" format.
If there's no letter spec, the input is interpreted as a number of seconds.

If input is blank, this function returns nil.  Otherwise it
returns the time spec converted to a number of seconds."
  (erc--decode-time-period
   (string-trim (read-string prompt nil 'erc--read-time-period-history))))

(defun erc--decode-time-period (period)
  (progn ; unprogn on next major refactor
    (cond
     ;; Blank input.
     ((zerop (length period))
      nil)
     ;; All-number -- interpret as seconds.
     ((string-match-p "\\`[0-9]+\\'" period)
      (string-to-number period))
     ;; Parse as a time spec.
     (t
      (require 'time-date)
      (require 'iso8601)
      (let ((time (condition-case nil
                      (iso8601-parse-duration
                       (concat (cond
                                ((string-match-p "\\`P" (upcase period))
                                 ;; Somebody typed in a full ISO8601 period.
                                 (upcase period))
                                ((string-match-p "[YD]" (upcase period))
                                 ;; If we have a year/day element,
                                 ;; we have a full spec.
                                 "P")
                                (t
                                 ;; Otherwise it's just a sub-day spec.
                                 "PT"))
                               (upcase period)))
                    (wrong-type-argument nil))))
        (unless time
          (user-error "%s is not a valid time period" period))
        (decoded-time-period time))))))

(defun erc--format-time-period (secs)
  "Return a string with hour/minute/second labels for duration in SECS."
  (let* ((hours (floor secs 3600))
         (minutes (floor (mod secs 3600) 60))
         (seconds (mod secs 60)))
    (cond ((>= secs 3600) (format "%dh%dm%ds" hours minutes (floor seconds)))
          ((>= secs 60) (format "%dm%ds" minutes (floor seconds)))
          (t (format "%ds" (floor seconds))))))

(defun erc--get-ignore-timer-args (inst)
  ;; The `cl-struct' `pcase' pattern and `cl-struct-slot-value' emit
  ;; warnings when compiling because `timer' is un-`:named'.
  (when (and (timerp inst)
             (eq (aref inst (cl-struct-slot-offset 'timer 'function))
                 'erc--unignore-user))
    (aref inst (cl-struct-slot-offset 'timer 'args))))

(defun erc--find-ignore-timer (&rest args)
  "Find an existing ignore timer."
  (cl-find args timer-list :key #'erc--get-ignore-timer-args :test #'equal))

(defun erc-cmd-IGNORE (&optional user timespec)
  "Drop messages from senders, like nick!user@host, matching regexp USER.
With human-readable TIMESPEC, ignore messages from matched senders for
the specified duration, like \"20m\".  Without USER, list the contents
of `erc-ignore-list'."
  (if user
      (let ((quoted (regexp-quote user))
            (prompt "Add a timeout? (Blank for no, or a time spec like 2h): ")
            timeout msg)
        (when (and (not (string= user quoted))
                   (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
                                     quoted)))
          (setq user quoted))
        (unless timespec
          (setq timespec
                (read-string prompt nil 'erc--read-time-period-history)))
        (setq timeout (erc--decode-time-period (string-trim timespec))
              msg (if timeout
                      (format "Now ignoring %s for %s" user
                              (erc--format-time-period timeout))
                    (format "Now ignoring %s" user)))
        (erc-with-server-buffer
          (when timeout
            (if-let ((existing (erc--find-ignore-timer user (current-buffer))))
                (timer-set-time existing (timer-relative-time nil timeout))
              (run-at-time timeout nil #'erc--unignore-user user
                           (current-buffer))))
          (erc-display-message nil 'notice 'active msg)
          (cl-pushnew user erc-ignore-list :test #'equal)))
    (if (null (erc-with-server-buffer erc-ignore-list))
        (erc-display-message nil 'notice 'active "Ignore list is empty")
      (erc-display-message nil 'notice 'active "Ignore list:")
      (erc-with-server-buffer
        (let ((seen (copy-sequence erc-ignore-list)))
          (dolist (timer timer-list)
            (when-let ((args (erc--get-ignore-timer-args timer))
                       ((eq (current-buffer) (nth 1 args)))
                       (user (car args))
                       (delta (- (timer-until timer (current-time))))
                       (duration (erc--format-time-period delta)))
              (setq seen (delete user seen))
              (erc-display-message nil 'notice 'active 'ignore-list
                                   ?p user ?s duration)))
          (dolist (pattern seen)
            (erc-display-message nil 'notice 'active pattern))))))
  t)

(defun erc-cmd-UNIGNORE (user)
  "Remove the first pattern in `erc-ignore-list' matching USER."
  (let ((ignored-nick (car (erc-with-server-buffer
                             (erc-member-ignore-case (regexp-quote user)
                                                     erc-ignore-list)))))
    (unless ignored-nick
      (if (setq ignored-nick (erc-ignored-user-p user))
          (unless (y-or-n-p (format "Remove this regexp (%s)? "
                                    ignored-nick))
            (setq ignored-nick nil))
        (erc-display-message nil 'notice 'active
                             (format "%s is not currently ignored!" user))))
    (when ignored-nick
      (erc--unignore-user ignored-nick (erc-server-buffer))))
  t)

(defun erc--unignore-user (user buffer)
  (when (buffer-live-p buffer)
    (with-current-buffer buffer
      (cl-assert (erc--server-buffer-p))
      (erc-display-message nil 'notice 'active
                           (format "No longer ignoring %s" user))
      (setq erc-ignore-list (delete user erc-ignore-list))
      (when-let ((existing (erc--find-ignore-timer user buffer)))
        (cancel-timer existing)))))

(defvar erc--pre-clear-functions nil
  "Abnormal hook run when truncating buffers.
Called with position indicating boundary of interval to be excised.")

(defun erc-cmd-CLEAR ()
  "Clear messages in current buffer after informing active modules.
Expect modules to perform housekeeping tasks to withstand the
disruption.  When called from lisp code, only clear messages up
to but not including the one occupying the current line."
  (with-silent-modifications
    (let ((max (if (>= (point) erc-insert-marker)
                   (1- erc-insert-marker)
                 (or (erc--get-inserted-msg-beg (point)) (pos-bol)))))
      (run-hook-with-args 'erc--pre-clear-functions max)
      (delete-region (point-min) max)))
  t)
(put 'erc-cmd-CLEAR 'process-not-needed t)

(defun erc-cmd-OPS ()
  "Show the ops in the current channel."
  (interactive)
  (let ((ops nil))
    (if erc-channel-users
        (maphash (lambda (_nick user-data)
                   (let ((cuser (cdr user-data)))
                     (if (and cuser
                              (erc-channel-user-op cuser))
                         (setq ops (cons (erc-server-user-nickname
                                          (car user-data))
                                         ops)))))
                 erc-channel-users))
    (setq ops (sort ops #'string-lessp))
    (if ops
        (erc-display-message
         nil 'notice (current-buffer) 'ops
         ?i (length ops) ?s (if (> (length ops) 1) "s" "")
         ?o (mapconcat #'identity ops " "))
      (erc-display-message nil 'notice (current-buffer) 'ops-none)))
  t)

(defun erc-cmd-COUNTRY (tld)
  "Display the country associated with the top level domain TLD."
  (require 'mail-extr)
  (let ((co (ignore-errors (what-domain tld))))
    (if co
        (erc-display-message
         nil 'notice 'active 'country ?c co ?d tld)
      (erc-display-message
       nil 'notice 'active 'country-unknown ?d tld))
    t))
(put 'erc-cmd-COUNTRY 'process-not-needed t)

(defun erc-cmd-AWAY (line)
  "Mark the user as being away, the reason being indicated by LINE.
If no reason is given, unset away status."
  (when (string-match "^\\s-*\\(.*\\)$" line)
    (let ((reason (match-string 1 line)))
      (erc-log (format "cmd: AWAY: %s" reason))
      (erc-server-send
       (if (string= reason "")
           "AWAY"
         (concat "AWAY :" reason))))
    t))
(put 'erc-cmd-AWAY 'do-not-parse-args t)

(defun erc-cmd-GAWAY (line)
  "Mark the user as being away everywhere, the reason being indicated by LINE."
  ;; on all server buffers.
  (erc-with-all-buffers-of-server nil
    #'erc-open-server-buffer-p
    (erc-cmd-AWAY line)))
(put 'erc-cmd-GAWAY 'do-not-parse-args t)

(defun erc-cmd-CTCP (nick cmd &rest args)
  "Send a Client To Client Protocol message to NICK.

CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
VERSION and so on.  It is called with ARGS."
  (let ((str (concat cmd
                     (when args
                       (concat " " (mapconcat #'identity args " "))))))
    (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
    (erc-send-ctcp-message nick str)
    t))

(defun erc-cmd-HELP (&optional func &rest rest)
  "Popup help information.

If FUNC contains a valid function or variable, help about that
will be displayed.  If FUNC is empty, display an apropos about
ERC commands.  Otherwise, do `apropos' in the ERC namespace
\(\"erc-.*LINE\").

Examples:
To find out about erc and bbdb, do
  /help bbdb.*

For help about the WHOIS command, do:
  /help whois

For a list of user commands (/join /part, ...):
  /help."
  (if func
      (let* ((sym (or (let ((sym (intern-soft
                                  (concat "erc-cmd-" (upcase func)))))
                        (if (and sym (or (boundp sym) (fboundp sym)))
                            sym
                          nil))
                      (let ((sym (intern-soft func)))
                        (if (and sym (or (boundp sym) (fboundp sym)))
                            sym
                          nil))
                      (let ((sym (intern-soft (concat "erc-" func))))
                        (if (and sym (or (boundp sym) (fboundp sym)))
                            sym
                          nil)))))
        (if sym
            (cond
             ((get sym 'erc--cmd-help)
              (when (autoloadp (symbol-function sym))
                (autoload-do-load (symbol-function sym)))
              (apply (get sym 'erc--cmd-help) rest))
             ((boundp sym) (describe-variable sym))
             ((fboundp sym) (describe-function sym))
             (t nil))
          (apropos-command (concat "erc-.*" func) nil
                           (lambda (x)
                             (or (commandp x)
                                 (get x 'custom-type))))
          t))
    (apropos "erc-cmd-")
    (message "Type C-h m to get additional information about keybindings.")
    t))

(defalias 'erc-cmd-H #'erc-cmd-HELP)
(put 'erc-cmd-HELP 'process-not-needed t)

(defcustom erc-auth-source-server-function #'erc-auth-source-search
  "Function to query auth-source for a server password.
Called with a subset of keyword parameters known to
`auth-source-search' and relevant to an opening \"PASS\" command,
if any.  In return, ERC expects a string to send as the server
password, or nil, to skip the \"PASS\" command completely.  An
explicit `:password' argument to entry-point commands `erc' and
`erc-tls' also inhibits lookup, as does setting this option to
nil.  See Info node `(erc) auth-source' for details."
  :package-version '(ERC . "5.5")
  :group 'erc
  :type '(choice (function-item erc-auth-source-search)
                 (const nil)
                 function))

(defcustom erc-auth-source-join-function #'erc-auth-source-search
  "Function to query auth-source on joining a channel.
Called with a subset of keyword arguments known to
`auth-source-search' and relevant to joining a password-protected
channel.  In return, ERC expects a string to use as the channel
\"key\", or nil to just join the channel normally.  Setting the
option itself to nil tells ERC to always forgo consulting
auth-source for channel keys.  For more information, see Info
node `(erc) auth-source'."
  :package-version '(ERC . "5.5")
  :group 'erc
  :type '(choice (function-item erc-auth-source-search)
                 (const nil)
                 function))

(defun erc--auth-source-determine-params-defaults ()
  (let* ((net (and-let* ((erc-networks--id)
                         (esid (erc-networks--id-symbol erc-networks--id))
                         ((symbol-name esid)))))
         (localp (and erc--target (erc--target-channel-local-p erc--target)))
         (hosts (if localp
                    (list erc-server-announced-name erc-session-server net)
                  (list net erc-server-announced-name erc-session-server)))
         (ports (list (cl-typecase erc-session-port
                        (integer (number-to-string erc-session-port))
                        (string (and (string= erc-session-port "irc")
                                     erc-session-port)) ; or nil
                        (t erc-session-port))
                      "irc")))
    (list (cons :host (delq nil hosts))
          (cons :port (delq nil ports))
          (cons :require '(:secret)))))

(defun erc--auth-source-determine-params-merge (&rest plist)
  "Return a plist of merged keyword args to pass to `auth-source-search'.
Combine items in PLIST with others derived from the current connection
context, but prioritize the former.  For keys not present in PLIST,
favor a network ID over an announced server unless `erc--target' is a
local channel.  And treat the dialed server address as a fallback for
the announced name in both cases."
  (let ((defaults (erc--auth-source-determine-params-defaults)))
    `(,@(cl-loop for (key value) on plist by #'cddr
                 for default = (assq key defaults)
                 do (when default (setq defaults (delq default defaults)))
                 append `(,key ,(delete-dups
                                 `(,@(if (consp value) value (list value))
                                   ,@(cdr default)))))
      ,@(cl-loop for (k . v) in defaults append (list k v)))))

(defun erc--auth-source-search (&rest defaults)
  "Ask auth-source for a secret and return it if found.
Use DEFAULTS as keyword arguments for querying auth-source and as a
guide for narrowing results.  Return a string if found or nil otherwise.
The ordering of DEFAULTS influences how results are filtered, as does
the ordering of the members of any individual composite values.  If
necessary, the former takes priority.  For example, if DEFAULTS were to
contain

  :host (\"foo\" \"bar\") :port (\"123\" \"456\")

the secret from an auth-source entry of host foo and port 456 would be
chosen over another of host bar and port 123.  However, if DEFAULTS
looked like

  :port (\"123\" \"456\") :host (\"foo\" \"bar\")

the opposite would be true.  In both cases, two entries with the same
host but different ports would result in the one with port 123 getting
the nod.  Much the same would happen for entries sharing only a port:
the one with host foo would win."
  (when-let*
      ((auth-source-backend-parser-functions
        (erc-compat--auth-source-backend-parser-functions))
       (priority (map-keys defaults))
       (test (lambda (a b)
               (catch 'done
                 (dolist (key priority)
                   (let* ((d (plist-get defaults key))
                          (defval (if (listp d) d (list d)))
                          ;; featurep 'seq via auth-source > json > map
                          (p (seq-position defval (plist-get a key)))
                          (q (seq-position defval (plist-get b key))))
                     (unless (eql p q)
                       (throw 'done (when p (or (not q) (< p q))))))))))
       (plist (copy-sequence defaults)))
    (unless (plist-get plist :max)
      (setq plist (plist-put plist :max 5000))) ; `auth-source-netrc-parse'
    (unless (plist-get defaults :require)
      (setq plist (plist-put plist :require '(:secret))))
    (when-let* ((sorted (sort (apply #'auth-source-search plist) test)))
      (plist-get (car sorted) :secret))))

(defun erc-auth-source-search (&rest plist)
  "Call `auth-source-search', possibly with keyword params in PLIST.
If the search signals an error before returning, `warn' the user
and ask whether to continue connecting anyway."
  ;; These exist as separate helpers in case folks should find them
  ;; useful.  If that's you, please request that they be exported.
  (condition-case err
      (apply #'erc--auth-source-search
             (apply #'erc--auth-source-determine-params-merge plist))
    (error
     (erc--lwarn '(erc auth-source) :error
                 "Problem querying `auth-source': %S. See %S for more."
                 (error-message-string err)
                 '(info "(erc) auth-source Troubleshooting"))
     (when (or noninteractive
               (not (y-or-n-p "Ignore auth-source error and continue? ")))
       (signal (car err) (cdr err))))))

(defun erc-server-join-channel (server channel &optional secret)
  "Join CHANNEL, optionally with SECRET.
Without SECRET, consult auth-source, possibly passing SERVER as the
`:host' query parameter."
  (unless (or secret (not erc-auth-source-join-function))
    (unless server
      (when (and erc-server-announced-name
                 (erc--valid-local-channel-p channel))
        (setq server erc-server-announced-name)))
    (setq secret (apply erc-auth-source-join-function
                        `(,@(and server (list :host server)) :user ,channel))))
  (erc-log (format "cmd: JOIN: %s" channel))
  (erc-server-send (concat "JOIN " channel
                           (and secret (concat " " (erc--unfun secret))))))

(defun erc--valid-local-channel-p (channel)
  "Non-nil when channel is server-local on a network that allows them."
  (and-let* (((eq ?& (aref channel 0)))
             (chan-types (erc--get-isupport-entry 'CHANTYPES 'single))
             ((string-search "&" chan-types)))))

(defun erc-cmd-JOIN (channel &optional key)
  "Join the channel given in CHANNEL, optionally with KEY.
If CHANNEL is specified as \"-invite\", join the channel to which you
were most recently invited.  See also `invitation'."
  (let (chnl)
    (if (string= (upcase channel) "-INVITE")
        (if erc-invitation
            (setq chnl erc-invitation)
          (erc-display-message nil 'error (current-buffer) 'no-invitation))
      (setq chnl (erc-ensure-channel-name channel)))
    (when chnl
      ;; Prevent double joining of same channel on same server.
      (if-let* ((existing (erc-get-buffer chnl erc-server-process))
                ((with-current-buffer existing
                   (erc-get-channel-user (erc-current-nick)))))
          (switch-to-buffer existing)
        (when-let* ; bind `erc-join-buffer' when /JOIN issued
            ((erc--called-as-input-p)
             (fn (lambda (proc parsed)
                   (when-let* ; `fn' wrapper already removed from hook
                       (((equal (car (erc-response.command-args parsed))
                                channel))
                        (sn (erc-extract-nick (erc-response.sender parsed)))
                        ((erc-nick-equal-p sn (erc-current-nick)))
                        (erc-join-buffer (or erc-interactive-display
                                             erc-join-buffer))
                        (erc--display-context `((erc-interactive-display
                                                 . /JOIN)
                                                ,@erc--display-context)))
                     (run-hook-with-args-until-success
                      'erc-server-JOIN-functions proc parsed)
                     t))))
          (erc-with-server-buffer
            (erc-once-with-server-event "JOIN" fn)))
        (erc-server-join-channel nil chnl key))))
  t)

(defalias 'erc-cmd-CHANNEL #'erc-cmd-JOIN)
(defalias 'erc-cmd-J #'erc-cmd-JOIN)

(defvar-local erc-channel-new-member-names nil
  "If non-nil, a names list is currently being received.

If non-nil, this variable is a hash-table that associates
received nicks with t.")

(defun erc-cmd-NAMES (&optional channel)
  "Display the users in CHANNEL.
If CHANNEL is not specified, display the users in the current channel.
This function clears the channel name list first, then sends the
command."
  (let ((tgt (or (and (erc-channel-p channel) channel)
                 (erc-default-target))))
    (if (and tgt (erc-channel-p tgt))
        (progn
          (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
          (erc-with-buffer
              (tgt)
            (erc-channel-begin-receiving-names))
          (erc-server-send (concat "NAMES " tgt)))
      (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
  t)
(defalias 'erc-cmd-N #'erc-cmd-NAMES)

(defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
  "Kick the user indicated in LINE from the current channel.
LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
  (let ((reasonstring (mapconcat #'identity reasonwords " ")))
    (if (string= "" reasonstring)
        (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
    (if (erc-channel-p target)
        (let ((nick reason-or-nick))
          (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
          (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
                           nil target)
          t)
      (when target
        (let ((ch (erc-default-target)))
          (setq reasonstring (concat
                              (if reason-or-nick (concat reason-or-nick " "))
                              reasonstring))
          (if ch
              (progn
                (erc-log
                 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
                (erc-server-send
                 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
            (erc-display-message nil 'error (current-buffer)
                                 'no-default-channel))
          t)))))

(defvar erc-script-args nil)

(defun erc-cmd-LOAD (line)
  "Load the script provided in the LINE.
If LINE continues beyond the file name, the rest of
it is put in a (local) variable `erc-script-args',
which can be used in Emacs Lisp scripts.

The optional FORCE argument is ignored here - you can't force loading
a script after exceeding the flood threshold."
  (cond
   ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
    (let* ((file-to-find (match-string 1 line))
           (erc-script-args (match-string 2 line))
           (file (erc-find-file file-to-find erc-script-path)))
      (erc-log (format "cmd: LOAD: %s" file-to-find))
      (cond
       ((not file)
        (erc-display-message nil 'error (current-buffer)
                             'cannot-find-file ?f file-to-find))
       ((not (file-readable-p file))
        (erc-display-message nil 'error (current-buffer)
                             'cannot-read-file ?f file))
       (t
        (message "Loading `%s'..." file)
        (erc-load-script file)
        (message "Loading `%s'...done" file))))
    t)
   (t nil)))

(defun erc-cmd-WHOIS (first &optional second)
  "Display whois information for the given user.

With one argument, FIRST is the nickname of the user to request
whois information for.

With two arguments, FIRST is the server, and SECOND is the user
nickname.

Specifying the server is useful for getting the time the user has
been idle for, when the user is connected to a different server
on the same IRC network.  (Only the server a user is connected to
knows how long the user has been idle for.)"
  (let ((send (if second
                  (format "WHOIS %s %s" first second)
                (format "WHOIS %s" first))))
    (erc-log (format "cmd: %s" send))
    (erc-server-send send)
    t))
(defalias 'erc-cmd-WI #'erc-cmd-WHOIS)

(defun erc-cmd-WII (nick)
  "Display whois information for NICK, including idle time.

This is a convenience function which calls `erc-cmd-WHOIS' with
the given NICK for both arguments.  Using NICK in place of the
server argument -- effectively delegating to the IRC network the
looking up of the server to which NICK is connected -- is not
standardized, but is widely supported across IRC networks.

See `erc-cmd-WHOIS' for more details."
  (erc-cmd-WHOIS nick nick))

(defun erc-cmd-WHOAMI ()
  "Display whois information about yourself."
  (erc-cmd-WHOIS (erc-current-nick))
  t)

(defun erc-cmd-IDLE (nick)
  "Show the length of time NICK has been idle."
  (let ((origbuf (current-buffer))
        symlist)
    (erc-with-server-buffer
      (push (cons (erc-once-with-server-event
                   311 (lambda (_proc parsed)
                         (string= nick
                                  (nth 1 (erc-response.command-args
                                          parsed)))))
                  'erc-server-311-functions)
            symlist)
      (push (cons (erc-once-with-server-event
                   312 (lambda (_proc parsed)
                         (string= nick
                                  (nth 1 (erc-response.command-args
                                          parsed)))))
                  'erc-server-312-functions)
            symlist)
      (push (cons (erc-once-with-server-event
                   318 (lambda (_proc parsed)
                         (string= nick
                                  (nth 1 (erc-response.command-args
                                          parsed)))))
                  'erc-server-318-functions)
            symlist)
      (push (cons (erc-once-with-server-event
                   319 (lambda (_proc parsed)
                         (string= nick
                                  (nth 1 (erc-response.command-args
                                          parsed)))))
                  'erc-server-319-functions)
            symlist)
      (push (cons (erc-once-with-server-event
                   320 (lambda (_proc parsed)
                         (string= nick
                                  (nth 1 (erc-response.command-args
                                          parsed)))))
                  'erc-server-320-functions)
            symlist)
      (push (cons (erc-once-with-server-event
                   330 (lambda (_proc parsed)
                         (string= nick
                                  (nth 1 (erc-response.command-args
                                          parsed)))))
                  'erc-server-330-functions)
            symlist)
      (push (cons (erc-once-with-server-event
                   317
                   (lambda (_proc parsed)
                     (let ((idleseconds
                            (string-to-number
                             (cl-third
                              (erc-response.command-args parsed)))))
                       (erc-display-message nil 'notice origbuf
                         (format "%s has been idle for %s."
                                 (erc-string-no-properties nick)
                                 (erc-seconds-to-string idleseconds)))
                       t)))
                  'erc-server-317-functions)
            symlist)

      ;; Send the WHOIS command.
      (erc-cmd-WHOIS nick)

      ;; Remove the uninterned symbols from the server hooks that did not run.
      (run-at-time 20 nil (lambda (buf symlist)
                            (with-current-buffer buf
                              (dolist (sym symlist)
                                (let ((hooksym (cdr sym))
                                      (funcsym (car sym)))
                                  (remove-hook hooksym funcsym t)))))
                   (current-buffer) symlist)))
  t)

(defun erc-cmd-DESCRIBE (line)
  "Pose some action to a certain user.
LINE has the format \"USER ACTION\"."
  (cond
   ((string-match
     "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
    (let ((dst (match-string 1 line))
          (s (match-string 2 line)))
      (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
      (erc-send-action dst s))
    t)
   (t nil)))
(put 'erc-cmd-DESCRIBE 'do-not-parse-args t)

(defun erc-cmd-ME (line)
  "Send LINE as an action."
  (cond
   ((string-match "^\\s-\\(.*\\)$" line)
    (let ((s (match-string 1 line)))
      (erc-log (format "cmd: ME: %s" s))
      (erc-send-action (erc-default-target) s))
    t)
   (t nil)))
(put 'erc-cmd-ME 'do-not-parse-args t)

(defun erc-cmd-ME\'S (line)
  "Do a /ME command, but add the string \" \\='s\" to the beginning."
  (erc-cmd-ME (concat " 's" line)))
(put 'erc-cmd-ME\'S 'do-not-parse-args t)

(defun erc-cmd-LASTLOG (line)
  "Show all lines in the current buffer matching the regexp LINE.

If a match spreads across multiple lines, all those lines are shown.

The lines are shown in a buffer named `*Occur*'.
It serves as a menu to find any of the occurrences in this buffer.
\\[describe-mode] in that buffer will explain how.

If LINE contains upper case characters (excluding those preceded by `\\'),
the matching is case-sensitive."
  (occur line)
  t)
(put 'erc-cmd-LASTLOG 'do-not-parse-args t)
(put 'erc-cmd-LASTLOG 'process-not-needed t)

(defvar erc--send-message-nested-function #'erc--send-message-nested
  "Function for inserting and sending slash-command generated text.
When a command like /SV or /SAY modifies or replaces command-line
input originally submitted at the prompt, `erc-send-message'
performs additional processing to ensure said input is fit for
inserting and sending given this \"nested\" meta context.  This
interface variable exists because modules extending fundamental
insertion and sending operations need a say in this processing as
well.")

(defun erc-send-message (line &optional force)
  "Send LINE to the current channel or user and display it.

See also `erc-message' and `erc-display-line'."
  (if (erc--input-split-p erc--current-line-input-split)
      (funcall erc--send-message-nested-function line force)
    (erc--send-message-external line force)))

(defun erc--send-message-external (line force)
  "Send a \"PRIVMSG\" to the default target with optional FORCE.
Expect caller to bind `erc-default-recipients' if needing to
specify a status-prefixed target."
  ;; Almost like an echoed message, but without the `erc--cmd'.
  (let* ((erc-current-message-catalog erc--message-speaker-catalog)
         (target (erc-default-target))
         (erc--msg-prop-overrides `((erc--tmp) ,@erc--msg-prop-overrides))
         ;; This util sets the `erc--spkr' property in ^.
         (trimmed (erc--statusmsg-target target))
         (stmsgindc (and trimmed (substring target 0 1)))
         (queryp (and erc--target (not (erc--target-channel-p erc--target))))
         (args (erc--determine-speaker-message-format-args
                (erc-current-nick) line queryp 'privmsgp 'inputp
                stmsgindc 'prefix)))
    (erc-message "PRIVMSG" (concat target " " line) force)
    (push (cons 'erc--msg (car args)) erc--msg-prop-overrides)
    (apply #'erc-display-message nil nil (current-buffer) args))
  ;; FIXME - treat multiline, run hooks, or remove me?
  ;; FIXME explain this ^ in more detail or remove.
  t)

(defun erc--send-message-nested (input-line force)
  "Process string INPUT-LINE almost as if it's normal chat input.
Expect INPUT-LINE to differ from the `string' slot of the calling
context's `erc--current-line-input-split' object because the
latter is likely a slash command invocation whose handler
generated INPUT-LINE.  Before inserting INPUT-LINE, split it and
run `erc-send-modify-hook' and `erc-send-post-hook' on each
actual outgoing line.  Forgo input validation because this isn't
interactive input, and skip `erc-send-completed-hook' because it
will run just before the outer `erc-send-current-line' call
returns."
  (let* ((erc-flood-protect (not force))
         (lines-obj (erc--make-input-split input-line)))
    (setf (erc--input-split-refoldp lines-obj) t
          (erc--input-split-cmdp lines-obj) nil)
    (erc--send-input-lines (erc--run-send-hooks lines-obj)))
  t)

;; FIXME if the user types /MODE<RET>, LINE becomes "\n", which
;; matches the pattern, so "\n" is sent to the server.  Perhaps
;; instead of `do-not-parse-args', this should just join &rest
;; arguments.
(defun erc-cmd-MODE (line)
  "Change or display the mode value of a channel or user.
The first word specifies the target.  The rest is the mode string
to send.

If only one word is given, display the mode of that target.

A list of valid mode strings for Libera.Chat may be found at
`https://libera.chat/guides/channelmodes' and
`https://libera.chat/guides/usermodes'."
  (cond
   ((string-match "^\\s-\\(.*\\)$" line)
    (let ((s (match-string 1 line)))
      (erc-log (format "cmd: MODE: %s" s))
      (erc-server-send (concat "MODE " s)))
    t)
   (t nil)))
(put 'erc-cmd-MODE 'do-not-parse-args t)

(defun erc-cmd-NOTICE (channel-or-user &rest message)
  "Send a notice to the channel or user given as the first word.
The rest is the message to send."
  (erc-message "NOTICE" (concat channel-or-user " "
                                (mapconcat #'identity message " "))))

(defun erc-cmd-MSG (line)
  "Send a message to the channel or user given as the first word in LINE.

The rest of LINE is the message to send."
  (erc-message "PRIVMSG" line))

(defalias 'erc-cmd-M #'erc-cmd-MSG)
(put 'erc-cmd-MSG 'do-not-parse-args t)

(defun erc-cmd-SQUERY (line)
  "Send a Service Query to the service given as the first word in LINE.

The rest of LINE is the message to send."
  (erc-message "SQUERY" line))
(put 'erc-cmd-SQUERY 'do-not-parse-args t)

(defun erc-cmd-NICK (nick)
  "Change current nickname to NICK."
  (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
  (let ((nicklen (erc-with-server-buffer
                   (erc--get-isupport-entry 'NICKLEN 'single))))
    (and nicklen (> (length nick) (string-to-number nicklen))
         (erc-display-message
          nil 'notice 'active 'nick-too-long
          ?i (length nick) ?l nicklen)))
  (erc-server-send (format "NICK %s" nick))
  (cond (erc-bad-nick
         (erc-set-current-nick nick)
         (erc-update-mode-line)
         (setq erc-bad-nick nil)))
  t)

(defun erc-cmd-PART (line)
  "When LINE is an empty string, leave the current channel.
Otherwise leave the channel indicated by LINE."
  (cond
   ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
    (let* ((ch (match-string 1 line))
           (msg (match-string 2 line))
           (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
      (erc-log (format "cmd: PART: %s: %s" ch reason))
      (erc-server-send (if (string= reason "")
                           (format "PART %s" ch)
                         (format "PART %s :%s" ch reason))
                       nil ch))
    t)
   ((string-match "^\\s-*\\(.*\\)$" line)
    (let* ((ch (erc-default-target))
           (msg (match-string 1 line))
           (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
      (if (and ch (erc-channel-p ch))
          (progn
            (erc-log (format "cmd: PART: %s: %s" ch reason))
            (erc-server-send (if (string= reason "")
                                 (format "PART %s" ch)
                               (format "PART %s :%s" ch reason))
                             nil ch))
        (erc-display-message nil '(notice error) (current-buffer) 'no-target)))
    t)
   (t nil)))
(put 'erc-cmd-PART 'do-not-parse-args t)

(defalias 'erc-cmd-LEAVE #'erc-cmd-PART)

(defun erc-cmd-PING (recipient)
  "Ping RECIPIENT."
  (let ((time (format-time-string "%s.%6N")))
    (erc-log (format "cmd: PING: %s" time))
    (erc-cmd-CTCP recipient "PING" time)))

(defun erc-cmd-QUOTE (line)
  "Send LINE directly to the server.
All the text given as argument is sent to the sever as unmodified,
just as you provided it.  Use this command with care!"
  (cond
   ((string-match "^ ?\\(.+\\)$" line)
    (erc-server-send (match-string 1 line)))
   (t nil)))
(put 'erc-cmd-QUOTE 'do-not-parse-args t)

(defun erc-cmd-QUERY (&optional user)
  "Open a query with USER.
How the query is displayed (in a new window, frame, etc.) depends
on the value of `erc-interactive-display'."
  ;; FIXME: The doc string used to say at the end:
  ;; "If USER is omitted, close the current query buffer if one exists
  ;; - except this is broken now ;-)"
  ;; Does it make sense to have that functionality?  What's wrong with
  ;; `kill-buffer'?  If it makes sense, re-add it.  -- SK @ 2021-11-11
  (interactive
   (list (read-string "Start a query with: ")))
  (unless user
      ;; currently broken, evil hack to display help anyway
                                        ;(erc-delete-query))))
    (signal 'wrong-number-of-arguments '(erc-cmd-QUERY 0)))
  (let ((erc-join-buffer erc-interactive-display)
        (erc--display-context `((erc-interactive-display . /QUERY)
                                ,@erc--display-context)))
    (erc-with-server-buffer
     (erc--open-target user))))

(defalias 'erc-cmd-Q #'erc-cmd-QUERY)

(defun erc-quit/part-reason-default ()
  "Default quit/part message."
  (erc-version nil 'bold-erc))


(defun erc-quit-reason-normal (&optional s)
  "Normal quit message.

If S is non-nil, it will be used as the quit reason."
  (or s (erc-quit/part-reason-default)))

(defun erc-quit-reason-zippy (&optional s)
  "Zippy quit message.

If S is non-nil, it will be used as the quit reason."
  (or s (erc-quit/part-reason-default)))

(make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")

(defun erc-quit-reason-various (s)
  "Choose a quit reason based on S (a string)."
  (let ((res (car (assoc-default (or s "")
                                 erc-quit-reason-various-alist 'string-match))))
    (cond
     ((functionp res) (funcall res))
     ((stringp res) res)
     (s s)
     (t (erc-quit/part-reason-default)))))

(defun erc-part-reason-normal (&optional s)
  "Normal part message.

If S is non-nil, it will be used as the part reason."
  (or s (erc-quit/part-reason-default)))

(defun erc-part-reason-zippy (&optional s)
  "Zippy part message.

If S is non-nil, it will be used as the quit reason."
  (or s (erc-quit/part-reason-default)))

(make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")

(defun erc-part-reason-various (s)
  "Choose a part reason based on S (a string)."
  (let ((res (car (assoc-default (or s "")
                                 erc-part-reason-various-alist 'string-match))))
    (cond
     ((functionp res) (funcall res))
     ((stringp res) res)
     (s s)
     (t (erc-quit/part-reason-default)))))

(defun erc-cmd-QUIT (reason)
  "Disconnect from the current server.
If REASON is omitted, display a default quit message, otherwise display
the message given by REASON."
  (unless reason
    (setq reason ""))
  (cond
   ((string-match "^\\s-*\\(.*\\)$" reason)
    (let* ((s (match-string 1 reason))
           (buffer (erc-server-buffer))
           (reason (funcall erc-quit-reason (if (equal s "") nil s)))
           server-proc)
      (with-current-buffer (if (and buffer
                                    (bufferp buffer))
                               buffer
                             (current-buffer))
        (erc-log (format "cmd: QUIT: %s" reason))
        (setq erc-server-quitting t)
        (erc-set-active-buffer (erc-server-buffer))
        (setq server-proc erc-server-process)
        (erc-server-send (format "QUIT :%s" reason)))
      (run-hook-with-args 'erc-quit-hook server-proc)
      (when erc-kill-queries-on-quit
        (erc-kill-query-buffers server-proc))
      ;; if the process has not been killed within 4 seconds, kill it
      (run-at-time 4 nil
                   (lambda (proc)
                     (when (and (processp proc)
                                (memq (process-status proc) '(run open)))
                       (delete-process proc)))
                   server-proc))
    t)
   (t nil)))

(defalias 'erc-cmd-BYE #'erc-cmd-QUIT)
(defalias 'erc-cmd-EXIT #'erc-cmd-QUIT)
(defalias 'erc-cmd-SIGNOFF #'erc-cmd-QUIT)
(put 'erc-cmd-QUIT 'do-not-parse-args t)
(put 'erc-cmd-QUIT 'process-not-needed t)

(defun erc-cmd-GQUIT (reason)
  "Disconnect from all servers at once with the same quit REASON."
  (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
                                  (erc-cmd-QUIT reason))
  (when erc-kill-queries-on-quit
    ;; if the query buffers have not been killed within 4 seconds,
    ;; kill them
    (run-at-time
     4 nil
     #'erc-buffer-do (lambda () (when erc--target (kill-buffer)))))
  t)

(defalias 'erc-cmd-GQ #'erc-cmd-GQUIT)
(put 'erc-cmd-GQUIT 'do-not-parse-args t)
(put 'erc-cmd-GQUIT 'process-not-needed t)

(defun erc--cmd-reconnect ()
  (let ((buffer (erc-server-buffer))
        (erc-join-buffer erc-interactive-display)
        (erc--display-context `((erc-interactive-display . /RECONNECT)
                                ,@erc--display-context))
        (process nil))
    (unless (buffer-live-p buffer)
      (setq buffer (current-buffer)))
    (with-current-buffer buffer
      (when erc--server-reconnect-timer
        (erc--cancel-auto-reconnect-timer))
      (setq erc-server-quitting nil)
      (with-suppressed-warnings ((obsolete erc-server-reconnecting))
        (setq erc-server-reconnecting t))
      (setq erc-server-reconnect-count 0)
      (setq process (get-buffer-process (erc-server-buffer)))
      (when process
        (delete-process process))
      (erc-server-reconnect)
      (with-suppressed-warnings ((obsolete erc-server-reconnecting)
                                 (obsolete erc-reuse-buffers))
        (if erc-reuse-buffers
            (cl-assert (not erc-server-reconnecting))
          (setq erc-server-reconnecting nil)))))
  t)

(defun erc-cmd-RECONNECT (&rest args)
  "Try reconnecting to the current IRC server.
Alternatively, CANCEL a scheduled attempt for either the current
connection or, with -A, all applicable connections.

\(fn [CANCEL [-A]])"
  (pcase args
    (`("cancel" "-a") (erc-buffer-filter #'erc--cancel-auto-reconnect-timer))
    (`("cancel") (erc-with-server-buffer (erc--cancel-auto-reconnect-timer)))
    (_ (erc--cmd-reconnect))))

(put 'erc-cmd-RECONNECT 'process-not-needed t)

;; FIXME use less speculative error message or lose `condition-case',
;; since most connection failures don't signal anything.
(defun erc-cmd-SERVER (server)
  "Connect to SERVER, leaving existing connection intact."
  (erc-log (format "cmd: SERVER: %s" server))
  (condition-case nil
      (erc :server server :nick (erc-current-nick))
    (error
     (erc-error "Cannot find host: `%s'" server)))
  t)
(put 'erc-cmd-SERVER 'process-not-needed t)

(defun erc-cmd-SV ()
  "Say the current ERC and Emacs version into channel."
  (erc-send-message (format "I'm using ERC %s with GNU Emacs %s (%s%s)%s."
                            erc-version
                            emacs-version
                            system-configuration
                            (concat
                             (cond ((featurep 'motif)
                                    (defvar motif-version-string)
                                    (concat ", " (substring
                                                  motif-version-string 4)))
                                   ((featurep 'gtk)
                                    (defvar gtk-version-string)
                                    (concat ", GTK+ Version "
                                            gtk-version-string))
                                   ((featurep 'x-toolkit) ", X toolkit")
                                   (t ""))
                             (if (and (boundp 'x-toolkit-scroll-bars)
                                      (memq x-toolkit-scroll-bars
                                            '(xaw xaw3d)))
                                 (format ", %s scroll bars"
                                         (capitalize (symbol-name
                                                      x-toolkit-scroll-bars)))
                               "")
                             (if (featurep 'multi-tty) ", multi-tty" ""))
                            (if emacs-build-time
                                (concat " of " (format-time-string
                                                "%Y-%m-%d" emacs-build-time))
                              "")))
  t)

(defun erc-cmd-SM ()
  "Say the current ERC modes into channel."
  (erc-send-message (format "I'm using the following modules: %s!"
                            (erc-modes)))
  t)

(defun erc-cmd-DEOP (&rest people)
  "Remove the operator setting from user(s) given in PEOPLE."
  (when (> (length people) 0)
    (erc-server-send (concat "MODE " (erc-default-target)
                             " -"
                             (make-string (length people) ?o)
                             " "
                             (mapconcat #'identity people " ")))
    t))

(defun erc-cmd-OP (&rest people)
  "Add the operator setting to users(s) given in PEOPLE."
  (when (> (length people) 0)
    (erc-server-send (concat "MODE " (erc-default-target)
                             " +"
                             (make-string (length people) ?o)
                             " "
                             (mapconcat #'identity people " ")))
    t))

(defun erc-cmd-OPME ()
  "Ask ChanServ to op the current nick in the current channel.

This command assumes a ChanServ (channel service) available on
the IRC network which accepts an \"op\" command that takes the
channel name and the user's nick, and that the current nick is
allowed to become an operator in the current channel (typically
means that the user has a +o flag in the channel's access list)."
  (erc-message "PRIVMSG"
               (format "ChanServ op %s %s"
                       (erc-default-target)
                       (erc-current-nick))
               nil))

(defun erc-cmd-DEOPME ()
  "Deop the current nick in the current channel."
  (erc-cmd-DEOP (erc-current-nick)))

(defun erc-cmd-TIME (&optional line)
  "Request the current time and date from the current server."
  (cond
   ((and line (string-match "^\\s-*\\(.*\\)$" line))
    (let ((args (match-string 1 line)))
      (erc-log (format "cmd: TIME: %s" args))
      (erc-server-send (concat "TIME " args)))
    t)
   (t (erc-server-send "TIME"))))
(defalias 'erc-cmd-DATE #'erc-cmd-TIME)

(defun erc-cmd-MOTD (&optional target)
  "Ask server to send the current MOTD.
Some IRCds simply ignore TARGET."
  (letrec ((oneoff (lambda (proc parsed)
                     (with-current-buffer (erc-server-buffer)
                       (cl-assert (eq (current-buffer) (process-buffer proc)))
                       (remove-hook 'erc-server-402-functions h402 t)
                       (remove-hook 'erc-server-376-functions h376 t)
                       (remove-hook 'erc-server-422-functions h422 t))
                     (erc-server-MOTD proc parsed)
                     t))
           (h402 (erc-once-with-server-event 402 oneoff))
           (h376 (erc-once-with-server-event 376 oneoff))
           (h422 (erc-once-with-server-event 422 oneoff)))
    (erc-server-send (concat "MOTD" (and target " ") target))))

(defun erc-cmd-TOPIC (topic)
  "Set or request the topic for a channel.
LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
or the empty string.

If no #CHANNEL is given, the default channel is used.  If TOPIC is
given, the channel topic is modified, otherwise the current topic will
be displayed."
  (cond
   ;; /topic #channel TOPIC
   ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
    (let ((ch (match-string 1 topic))
          (topic (match-string 2 topic)))
      ;; Ignore all-whitespace topics.
      (unless (equal (string-trim topic) "")
	(erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
	(erc-server-send (format "TOPIC %s :%s" ch topic) nil ch)))
    t)
   ;; /topic #channel
   ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
    (let ((ch (match-string 1 topic)))
      (erc-server-send (format "TOPIC %s" ch) nil ch)
      t))
   ;; /topic
   ((string-match "^\\s-*$" topic)
    (let ((ch (erc-default-target)))
      (erc-server-send (format "TOPIC %s" ch) nil ch)
      t))
   ;; /topic TOPIC
   ((string-match "^\\s-*\\(.*\\)$" topic)
    (let ((ch (erc-default-target))
          (topic (match-string 1 topic)))
      (if (and ch (erc-channel-p ch))
          (progn
            (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
            (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
        (erc-display-message nil '(notice error) (current-buffer) 'no-target)))
    t)
   (t nil)))
(defalias 'erc-cmd-T #'erc-cmd-TOPIC)
(put 'erc-cmd-TOPIC 'do-not-parse-args t)

(defun erc-cmd-APPENDTOPIC (topic)
  "Append TOPIC to the current channel topic, separated by a space."
  (let ((oldtopic erc-channel-topic))
    ;; display help when given no arguments
    (when (string-match "^\\s-*$" topic)
      (signal 'wrong-number-of-arguments nil))
    ;; strip trailing ^O
    (when (string-match "\\(.*\\)\C-o" oldtopic)
      (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
(defalias 'erc-cmd-AT #'erc-cmd-APPENDTOPIC)
(put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)

(defun erc-cmd-CLEARTOPIC (&optional channel)
  "Clear the topic for a CHANNEL.
If CHANNEL is not specified, clear the topic for the default channel."
  (interactive "sClear topic of channel (RET is current channel): ")
  (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
    (when chnl
      (erc-server-send (format "TOPIC %s :" chnl))
      t)))

;;; Banlists

(defvar-local erc-channel-banlist nil
  "A list of bans seen for the current channel.

Each ban is an alist of the form:
  (WHOSET . MASK)

The property `received-from-server' indicates whether
or not the ban list has been requested from the server.")
(put 'erc-channel-banlist 'received-from-server nil)

(defvar erc-fill-column)

(defun erc-cmd-BANLIST ()
  "Pretty-print the contents of `erc-channel-banlist'.

The ban list is fetched from the server if necessary."
  (let ((chnl (erc-default-target))
        (chnl-name (buffer-name)))

    (cond
     ((not (erc-channel-p chnl))
      (erc-display-message nil 'notice 'active "You're not on a channel\n"))

     ((not (get 'erc-channel-banlist 'received-from-server))
      (let ((old-367-hook erc-server-367-functions))
        (setq erc-server-367-functions 'erc-banlist-store
              erc-channel-banlist nil)
        ;; fetch the ban list then callback
        (erc-with-server-buffer
          (erc-once-with-server-event
           368
           (lambda (_proc _parsed)
             (with-current-buffer chnl-name
               (put 'erc-channel-banlist 'received-from-server t)
               (setq erc-server-367-functions old-367-hook)
               (erc-cmd-BANLIST)
               t)))
          (erc-server-send (format "MODE %s b" chnl)))))

     ((null erc-channel-banlist)
      (erc-display-message nil 'notice 'active
                           (format "No bans for channel: %s\n" chnl))
      (put 'erc-channel-banlist 'received-from-server nil))

     (t
      (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
                                       erc-fill-column)
                                  (and (boundp 'fill-column)
                                       fill-column)
                                  (1- (window-width))))
             (separator (make-string erc-fill-column ?=))
             (fmt (concat
                   "%-" (number-to-string (/ erc-fill-column 2)) "s"
                   "%" (number-to-string (/ erc-fill-column 2)) "s")))

        (erc-display-message
         nil 'notice 'active
         (format "Ban list for channel: %s\n" (erc-default-target)))

        (erc-display-line separator 'active)
        (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
        (erc-display-line separator 'active)

        (mapc
         (lambda (x)
           (erc-display-line
            (format fmt
                    (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
                    (if (car x)
                        (truncate-string-to-width (car x) (/ erc-fill-column 2))
                      ""))
            'active))
         erc-channel-banlist)

        (erc-display-message nil 'notice 'active "End of Ban list")
        (put 'erc-channel-banlist 'received-from-server nil)))))
  t)

(defalias 'erc-cmd-BL #'erc-cmd-BANLIST)

(defun erc-cmd-MASSUNBAN ()
  "Mass Unban.

Unban all currently banned users in the current channel."
  (let ((chnl (erc-default-target)))
    (cond

     ((not (erc-channel-p chnl))
      (erc-display-message nil 'notice 'active "You're not on a channel\n"))

     ((not (get 'erc-channel-banlist 'received-from-server))
      (let ((old-367-hook erc-server-367-functions))
        (setq erc-server-367-functions 'erc-banlist-store)
        ;; fetch the ban list then callback
        (erc-with-server-buffer
          (erc-once-with-server-event
           368
           (lambda (_proc _parsed)
             (with-current-buffer chnl
               (put 'erc-channel-banlist 'received-from-server t)
               (setq erc-server-367-functions old-367-hook)
               (erc-cmd-MASSUNBAN)
               t)))
          (erc-server-send (format "MODE %s b" chnl)))))

     (t (let ((bans (mapcar #'cdr erc-channel-banlist)))
          (when bans
            ;; Glob the bans into groups of three, and carry out the unban.
            ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
            (mapc
             (lambda (x)
               (erc-server-send
                (format "MODE %s -%s %s" (erc-default-target)
                        (make-string (length x) ?b)
                        (mapconcat #'identity x " "))))
             (erc-group-list bans 3))))
        t))))

(defalias 'erc-cmd-MUB #'erc-cmd-MASSUNBAN)

;;;; End of IRC commands

(defun erc-ensure-channel-name (channel)
  "Return CHANNEL if it is a valid channel name.
Eventually add a # in front of it, if that turns it into a valid channel name."
  (if (erc-channel-p channel)
      channel
    (concat "#" channel)))

(defvar erc--own-property-names
  `( tags erc--speaker erc-parsed display ; core
     ;; `erc--msg-props'
     ,@erc--ranked-properties
     ;; `erc-display-prompt'
     rear-nonsticky erc-prompt field front-sticky read-only
     ;; stamp
     cursor-intangible cursor-sensor-functions isearch-open-invisible
     ;; match
     invisible intangible
     ;; button
     erc-callback erc-data mouse-face keymap
     ;; fill-wrap
     line-prefix wrap-prefix)
  "Props added by ERC that should not survive killing.
Among those left behind by default are `font-lock-face' and
`erc-secret'.")

(defun erc--remove-text-properties (string)
  "Remove text properties in STRING added by ERC.
Specifically, remove any that aren't members of
`erc--own-property-names'."
  (remove-list-of-text-properties 0 (length string)
                                  erc--own-property-names string)
  string)

(defun erc-grab-region (start end)
  "Copy the region between START and END in a recreatable format.

Converts all the IRC text properties in each line of the region
into control codes and writes them to a separate buffer.  The
resulting text may be used directly as a script to generate this
text again."
  (interactive "r")
  (erc-set-active-buffer (current-buffer))
  (save-excursion
    (let* ((cb (current-buffer))
           (buf (generate-new-buffer erc-grab-buffer-name))
           (region (buffer-substring start end))
           (lines (erc-split-multiline-safe region)))
      (set-buffer buf)
      (dolist (line lines)
        (insert (concat line "\n")))
      (set-buffer cb)
      (switch-to-buffer-other-window buf)))
  (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
  (ding))

(defun erc-display-prompt (&optional buffer pos prompt face)
  "Display PROMPT in BUFFER at position POS.
Display an ERC prompt in BUFFER.

If PROMPT is nil, one is constructed with the function `erc-prompt'.
If BUFFER is nil, the `current-buffer' is used.
If POS is nil, PROMPT will be displayed at `point'.
If FACE is non-nil, it will be used to propertize the prompt.  If it is nil,
`erc-prompt-face' will be used."
  (let* ((prompt (or prompt (erc-prompt)))
         (l (length prompt))
         (ob (current-buffer)))
    ;; We cannot use save-excursion because we move point, therefore
    ;; we resort to the ol' ob trick to restore this.
    (when (and buffer (bufferp buffer))
      (set-buffer buffer))

    ;; now save excursion again to store where point and mark are
    ;; in the current buffer
    (save-excursion
      (setq pos (or pos (point)))
      (goto-char pos)
      (when (> l 0)
        ;; Do not extend the text properties when typing at the end
        ;; of the prompt, but stuff typed in front of the prompt
        ;; shall remain part of the prompt.
        (setq prompt (apply #'propertize prompt erc--prompt-properties))
        (erc-put-text-property 0 (1- (length prompt))
                               'font-lock-face (or face 'erc-prompt-face)
                               prompt)
        (insert prompt))
      ;; Set the input marker
      (set-marker erc-input-marker (point)))

    ;; Now we are back at the old position.  If the prompt was
    ;; inserted here or before us, advance point by the length of
    ;; the prompt.
    (when (or (not pos) (<= (point) pos))
      (forward-char l))
    ;; Clear the undo buffer now, so the user can undo his stuff,
    ;; but not the stuff we did. Sneaky!
    (setq buffer-undo-list nil)
    (set-buffer ob)))

;; interactive operations

(defun erc-input-message ()
  "Read input from the minibuffer."
  (interactive)
  (let ((minibuffer-allow-text-properties t)
        (read-map minibuffer-local-map))
    (insert (read-from-minibuffer "Message: "
                                  (string last-command-event)
				  read-map))
    (erc-send-current-line)))

(defvar erc-action-history-list ()
  "History list for interactive action input.")

(defun erc-input-action ()
  "Interactively input a user action and send it to IRC."
  (interactive "")
  (erc-set-active-buffer (current-buffer))
  (let ((action (read-string "Action: " nil 'erc-action-history-list)))
    (if (not (string-match "^\\s-*$" action))
        (erc-send-action (erc-default-target) action))))

(defun erc-join-channel (channel &optional key)
  "Join CHANNEL.

If `point' is at the beginning of a channel name, use that as default."
  (interactive
   (list
    (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
          (table (when (erc-server-buffer-live-p)
                   (set-buffer (process-buffer erc-server-process))
                   erc-channel-list)))
      (completing-read (format-prompt "Join channel" chnl)
                       table nil nil nil nil chnl))
    (when (or current-prefix-arg erc-prompt-for-channel-key)
      (read-string "Channel key (RET for none): "))))
  (erc-cmd-JOIN channel (when (>= (length key) 1) key)))

(defun erc-part-from-channel (reason)
  "Part from the current channel and prompt for a REASON."
  (interactive
    ;; FIXME: Has this ever worked?  We're in the interactive-spec, so the
    ;; argument `reason' can't be in scope yet!
    ;;(if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
    ;;    reason
   (list
    (read-string (concat "Reason for leaving " (erc-default-target) ": "))))
  (erc-cmd-PART (concat (erc-default-target)" " reason)))

(defun erc-set-topic (topic)
  "Prompt for a TOPIC for the current channel."
  (interactive
   (list
    (read-string
     (concat "Set topic of " (erc-default-target) ": ")
     (when erc-channel-topic
       (let ((ss (split-string erc-channel-topic "\C-o")))
         (cons (apply #'concat (if (cdr ss) (butlast ss) ss))
               0))))))
  (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
    (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))

(defun erc-set-channel-limit (&optional limit)
  "Set a LIMIT for the current channel.  Remove limit if nil.
Prompt for one if called interactively."
  (interactive (list (read-string
                      (format "Limit for %s (RET to remove limit): "
                              (erc-default-target)))))
  (let ((tgt (erc-default-target)))
    (erc-server-send (if (and limit (>= (length limit) 1))
                         (format "MODE %s +l %s" tgt limit)
                       (format "MODE %s -l" tgt)))))

(defun erc-set-channel-key (&optional key)
  "Set a KEY for the current channel.  Remove key if nil.
Prompt for one if called interactively."
  (interactive (list (read-string
                      (format "Key for %s (RET to remove key): "
                              (erc-default-target)))))
  (let ((tgt (erc-default-target)))
    (erc-server-send (if (and key (>= (length key) 1))
                         (format "MODE %s +k %s" tgt key)
                       (format "MODE %s -k" tgt)))))

(defun erc-quit-server (reason)
  "Disconnect from current server after prompting for REASON.
`erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
  (interactive (list (read-string
                      (format "Reason for quitting %s: "
                              (or erc-server-announced-name
                                  erc-session-server)))))
  (erc-cmd-QUIT reason))

;; Movement of point

(defun erc-bol ()
  "Move `point' to the beginning of the current line.

This places `point' just after the prompt, or at the beginning of the line."
  (interactive)
  (forward-line 0)
  (when (get-text-property (point) 'erc-prompt)
    (goto-char erc-input-marker))
  (point))

(defun erc-kill-input ()
  "Kill current input line using `erc-bol' followed by `kill-line'."
  (interactive)
  (when (and (erc-bol)
             (/= (point) (point-max))) ;; Prevent a (ding) and an error when
    ;; there's nothing to kill
    (if (boundp 'erc-input-ring-index)
        (setq erc-input-ring-index nil))
    (kill-line)))

(defvar erc--tab-functions nil
  "Functions to try when user hits \\`TAB' outside of input area.
Called with a numeric prefix arg.")

(defun erc-tab (arg)
  "Call `completion-at-point' when typing in the input area.
Otherwise call members of `erc--tab-functions' with a numeric
prefix ARG until one of them returns non-nil."
  (interactive "p")
  (if (>= (point) erc-input-marker)
      (completion-at-point)
    (run-hook-with-args-until-success 'erc--tab-functions arg)))

(defun erc-complete-word-at-point ()
  (run-hook-with-args-until-success 'erc-complete-functions))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;                        IRC SERVER INPUT HANDLING
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;; New Input parsing

; Stolen from ZenIRC. I just wanna test this code, so here is
; experiment area.

;; This shouldn't be a user option but remains so for compatibility.
(define-obsolete-variable-alias
  'erc-default-server-hook 'erc-default-server-functions "30.1")
(defcustom erc-default-server-functions '(erc-handle-unknown-server-response)
  "Abnormal hook for incoming messages without their own handlers.
See `define-erc-response-handler' for more."
  :package-version '(ERC . "5.6")
  :group 'erc-server-hooks
  :type 'hook)

(defun erc-default-server-handler (proc parsed)
  "Default server handler.

Displays PROC and PARSED appropriately using `erc-display-message'."
  (declare (obsolete erc-handle-unknown-server-response "29.1"))
  (erc-display-message
   parsed 'notice proc
   (mapconcat
    #'identity
    (let (res)
      (mapc (lambda (x)
              (if (stringp x)
                  (setq res (append res (list x)))))
            parsed)
      res)
    " ")))

(defvar erc-server-vectors
  '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
  "List of received server messages which ERC does not specifically handle.
See `erc-debug-missing-hooks'.")
;(make-variable-buffer-local 'erc-server-vectors)

(defun erc-debug-missing-hooks (_proc parsed)
  "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
These vectors can be helpful when adding new server message handlers to ERC.
See `erc-default-server-hook'."
  (setq erc-server-vectors `(,@erc-server-vectors ,parsed))
  nil)

(defun erc--open-target (target)
  "Open an ERC buffer on TARGET."
  (erc-open erc-session-server
            erc-session-port
            (erc-current-nick)
            erc-session-user-full-name
            nil
            nil
            (list target)
            target
            erc-server-process
            nil
            erc-session-username
            (erc-networks--id-given erc-networks--id)))

(defun erc-query (target server-buffer)
  "Open a query buffer on TARGET using SERVER-BUFFER.
To change how this query window is displayed, use `let' to bind
`erc-join-buffer' before calling this."
  (declare (obsolete "call `erc-open' in a live server buffer" "29.1"))
  (unless (buffer-live-p server-buffer)
    (error "Couldn't switch to server buffer"))
  (with-current-buffer server-buffer
    (erc--open-target target)))

(defvaralias 'erc-auto-query 'erc-receive-query-display)
(defcustom erc-receive-query-display 'window-noselect
  "If non-nil, create a query buffer each time you receive a private message.
If the buffer doesn't already exist, it is created.

This can be set to a symbol, to control how the new query window
should appear.  The default behavior is to display the buffer in
a new window but not to select it.  See the documentation for
`erc-buffer-display' for a description of available values.

Note that the legacy behavior of forgoing buffer creation
entirely when this option is nil requires setting the
compatibility flag `erc-receive-query-display-defer' to nil.  Use
`erc-ensure-target-buffer-on-privmsg' to achieve the same effect."
  :package-version '(ERC . "5.6")
  :group 'erc-buffers
  :group 'erc-query
  :type erc--buffer-display-choices)

(defvar erc-receive-query-display-defer t
  "How to interpret a null `erc-receive-query-display'.
When this variable is non-nil, ERC defers to `erc-buffer-display'
upon seeing a nil value for `erc-receive-query-display', much
like it does with other buffer-display options, like
`erc-interactive-display'.  Otherwise, when this option is nil,
ERC retains the legacy behavior of not creating a new query
buffer.")

(defvaralias 'erc-query-on-unjoined-chan-privmsg
  'erc-ensure-target-buffer-on-privmsg)
(defcustom erc-ensure-target-buffer-on-privmsg t
  "When non-nil, create a target buffer upon receiving a PRIVMSG.
This includes PRIVMSGs directed to channels.  If you are using an IRC
bouncer, such as dircproxy, to keep a log of channels when you are
disconnected, you should set this option to t.

For queries (direct messages), this option's non-nil meaning is
straightforward: if a buffer doesn't exist for the sender, create
one.  For channels, the use case is more niche and usually
involves receiving playback (via commands like ZNC's
\"PLAYBUFFER\") for channels to which your bouncer is joined but
from which you've \"detached\".

Note that this option was absent from ERC 5.5 because knowledge
of its intended role was \"unavailable\" during a major
refactoring involving buffer management.  The option has since
been restored in ERC 5.6 but now also affects queries in the
manner implied above, which was lost sometime before ERC 5.4."
  :package-version '(ERC . "5.6") ; revived
  :group 'erc-buffers
  :group 'erc-query
  :type '(choice boolean
                 (choice :tag "Create pseudo queries for STATUSMSGs"
                         status)))

(defcustom erc-format-query-as-channel-p t
  "If non-nil, format text from others in a query buffer like in a channel.
Otherwise format like a private message."
  :group 'erc-query
  :type 'boolean)

(defcustom erc-minibuffer-notice nil
  "If non-nil, print ERC notices for the user in the minibuffer.
Only happens when the session buffer isn't visible."
  :group 'erc-display
  :type 'boolean)

(defcustom erc-minibuffer-ignored nil
  "If non-nil, print a message in the minibuffer if we ignored something."
  :group 'erc-ignore
  :type 'boolean)

(defun erc-wash-quit-reason (reason nick login host)
  "Remove duplicate text from quit REASON.
Specifically in relation to NICK (user@host) information.  Returns REASON
unmodified if nothing can be removed.
E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
\"Read error: 110\".  The same applies for \"Ping Timeout\"."
  (setq nick (regexp-quote nick)
        login (regexp-quote login)
        host (regexp-quote host))
  (or (when (string-match (concat "^\\(Read error\\) to "
                                  nick "\\[" host "\\]: "
                                  "\\(.+\\)$")
			  reason)
        (concat (match-string 1 reason) ": " (match-string 2 reason)))
      (when (string-match (concat "^\\(Ping timeout\\) for "
                                  nick "\\[" host "\\]$")
			  reason)
        (match-string 1 reason))
      reason))

(cl-defmethod erc--nickname-in-use-make-request (_nick temp)
  "Request nickname TEMP in place of rejected NICK."
  (erc-cmd-NICK temp))

(defun erc-nickname-in-use (nick reason)
  "If NICK is unavailable, tell the user the REASON.

See also `erc-display-error-notice'."
  (if (or (not erc-try-new-nick-p)
          ;; how many default-nicks are left + one more try...
          (eq erc-nick-change-attempt-count
              (if (consp erc-nick)
                  (+ (length erc-nick) 1)
                1)))
      (erc-display-error-notice
       nil
       (format "Nickname %s is %s, try another." nick reason))
    (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
    (let ((newnick (nth 1 erc-default-nicks))
          (nicklen (erc-with-server-buffer
                     (erc--get-isupport-entry 'NICKLEN 'single))))
      (setq erc-bad-nick t)
      ;; try to use a different nick
      (if erc-default-nicks
          (setq erc-default-nicks (cdr erc-default-nicks)))
      (if (not newnick)
          (setq newnick (concat (truncate-string-to-width
                                 nick
                                 (if (and erc-server-connected nicklen)
                                     (- (string-to-number nicklen)
                                        (length erc-nick-uniquifier))
                                   ;; rfc2812 max nick length = 9
                                   ;; we must assume this is the
                                   ;; server's setting if we haven't
                                   ;; established a connection yet
                                   (- 9 (length erc-nick-uniquifier))))
				erc-nick-uniquifier)))
      (erc--nickname-in-use-make-request nick newnick)
      (erc-display-error-notice
       nil
       (format "Nickname %s is %s, trying %s"
               nick reason newnick)))))

;;; Server messages

;; FIXME remove on next major version release.  This group is all but
;; unused because most `erc-server-FOO-functions' are plain variables
;; and not user options as implied by this doc string.
(defgroup erc-server-hooks nil
  "Server event callbacks.
Every server event - like numeric replies - has its own hook.
Those hooks are all called using `run-hook-with-args-until-success'.
They receive as first argument the process object from where the event
originated from,
and as second argument the event parsed as a vector."
  :group 'erc-hooks)

(defun erc-display-server-message (_proc parsed)
  "Display the message sent by the server as a notice."
  (erc-display-message
   parsed 'notice 'active (erc-response.contents parsed)))

(defun erc-auto-query (proc parsed)
  ;; FIXME: This needs more documentation, unless it's not a user function --
  ;; Lawrence 2004-01-08
  "Put this on `erc-server-PRIVMSG-functions'."
  (when erc-auto-query
    (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
           (target (car (erc-response.command-args parsed)))
           (msg (erc-response.contents parsed))
           (query  (if (not erc-query-on-unjoined-chan-privmsg)
                       nick
                     (if (erc-current-nick-p target)
                         nick
                       target))))
      (and (not (erc-ignored-user-p (erc-response.sender parsed)))
           (or erc-query-on-unjoined-chan-privmsg
               (string= target (erc-current-nick)))
           (not (erc-get-buffer query proc))
           (not (erc-is-message-ctcp-and-not-action-p msg))
           (let ((erc-query-display erc-auto-query))
             (erc-cmd-QUERY query))
           nil))))

(make-obsolete 'erc-auto-query "try erc-cmd-QUERY instead" "29.1")

(defun erc-is-message-ctcp-p (message)
  "Check if MESSAGE is a CTCP message or not."
  (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))

(defun erc-is-message-ctcp-and-not-action-p (message)
  "Check if MESSAGE is a CTCP message or not."
  (and (erc-is-message-ctcp-p message)
       (not (string-match "^\C-aACTION.*\C-a$" message))))

(defun erc--get-speaker-bounds ()
  "Return the bounds of `erc--speaker' text property when present.
Assume buffer is narrowed to the confines of an inserted message."
  (and-let* (((erc--check-msg-prop 'erc--spkr))
             (beg (text-property-not-all (point-min) (point-max)
                                         'erc--speaker nil)))
    (cons beg (next-single-property-change beg 'erc--speaker))))

(defvar erc--cmem-from-nick-function #'erc--cmem-get-existing
  "Function maybe returning a \"channel member\" cons from a nick.
Must return either nil or a cons of an `erc-server-user' and an
`erc-channel-user' (see `erc-channel-users') for use in
formatting a user's nick prior to insertion.  Called in the
appropriate target buffer with the downcased nick, the parsed
NUH, and the current `erc-response' object.")

(defun erc--cmem-get-existing (downcased _nuh _parsed)
  (and erc-channel-users (gethash downcased erc-channel-users)))

(defun erc-format-privmessage (nick msg privp msgp)
  "Format a PRIVMSG in an insertable fashion."
  (let* ((mark-s (if msgp (if privp "*" "<") "-"))
         (mark-e (if msgp (if privp "*" ">") "-"))
         (str    (format "%s%s%s %s" mark-s nick mark-e msg))
         (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
         (nick-prefix-face (get-text-property 0 'font-lock-face nick))
         (prefix-len (or (and nick-prefix-face (text-property-not-all
                                                0 (length nick) 'font-lock-face
                                                nick-prefix-face nick))
                         0))
         (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
    (erc--ensure-spkr-prop nick)
    ;; add text properties to text before the nick, the nick and after the nick
    (erc-put-text-property 0 (length mark-s) 'font-lock-face msg-face str)
    (erc-put-text-properties (+ (length mark-s) prefix-len)
                             (+ (length mark-s) (length nick))
                             '(font-lock-face erc--speaker) str
                             (list nick-face
                                   (substring-no-properties nick prefix-len)))
    (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
                           'font-lock-face msg-face str)
    str))

;; The format strings in the following `-speaker' catalog shouldn't
;; contain any non-protocol words, so they make sense in any language.
;; Note that the following definitions generally avoid `propertize'
;; because it reverses the order of the text properties it's given.
(defvar erc--message-speaker-statusmsg
  #("(%p%n%s) %m"
    0 1 (font-lock-face erc-default-face)
    1 3 (font-lock-face erc-nick-prefix-face)
    3 5 (font-lock-face erc-nick-default-face)
    5 7 (font-lock-face erc-notice-face)
    7 11 (font-lock-face erc-default-face))
  "Message template for in-channel status messages.")

(defvar erc--message-speaker-statusmsg-input
  #("(%p%n%s) %m"
    0 1 (font-lock-face erc-default-face)
    1 3 (font-lock-face erc-my-nick-prefix-face)
    3 5 (font-lock-face erc-my-nick-face)
    5 7 (font-lock-face erc-notice-face)
    7 8 (font-lock-face erc-default-face)
    8 11 (font-lock-face erc-input-face))
  "Message template for echoed status messages.")

(defvar erc--message-speaker-input-chan-privmsg
  #("<%p%n> %m"
    0 1 (font-lock-face erc-default-face)
    1 3 (font-lock-face erc-my-nick-prefix-face)
    3 5 (font-lock-face erc-my-nick-face)
    5 7 (font-lock-face erc-default-face)
    7 9 (font-lock-face erc-input-face))
  "Message template for prompt input or echoed PRIVMSG from own nick.")

(defvar erc--message-speaker-input-query-privmsg
  #("*%n* %m"
    0 1 (font-lock-face erc-direct-msg-face)
    1 3 (font-lock-face erc-my-nick-face)
    3 5 (font-lock-face erc-direct-msg-face)
    5 7 (font-lock-face erc-input-face))
  "Message template for prompt input or echoed PRIVMSG query from own nick.")

(defvar erc--message-speaker-input-query-notice
  #("-%n- %m"
    0 1 (font-lock-face erc-direct-msg-face)
    1 3 (font-lock-face erc-my-nick-face)
    3 5 (font-lock-face erc-direct-msg-face)
    5 7 (font-lock-face erc-input-face))
  "Message template for echoed or spoofed query NOTICE from own nick.")

(defvar erc--message-speaker-input-chan-notice
  #("-%p%n- %m"
    0 1 (font-lock-face erc-default-face)
    1 3 (font-lock-face erc-my-nick-prefix-face)
    3 5 (font-lock-face erc-my-nick-face)
    5 7 (font-lock-face erc-default-face)
    7 9 (font-lock-face erc-input-face))
  "Message template for prompt input or echoed NOTICE from own nick.")

(defvar erc--message-speaker-chan-privmsg
  #("<%p%n> %m"
    0 1 (font-lock-face erc-default-face)
    1 3 (font-lock-face erc-nick-prefix-face)
    3 5 (font-lock-face erc-nick-default-face)
    5 9 (font-lock-face erc-default-face))
  "Message template for a PRIVMSG in a channel.")

(defvar erc--message-speaker-query-privmsg
  #("*%n* %m"
    0 1 (font-lock-face erc-direct-msg-face)
    1 3 (font-lock-face erc-nick-msg-face)
    3 7 (font-lock-face erc-direct-msg-face))
  "Message template for a PRIVMSG in query buffer.")

(defvar erc--message-speaker-chan-notice
  #("-%p%n- %m"
    0 1 (font-lock-face erc-default-face)
    1 3 (font-lock-face erc-nick-prefix-face)
    3 5 (font-lock-face erc-nick-default-face)
    5 9 (font-lock-face erc-default-face))
  "Message template for a NOTICE in a channel.")

(defvar erc--message-speaker-query-notice
  #("-%n- %m"
    0 1 (font-lock-face erc-direct-msg-face)
    1 3 (font-lock-face erc-nick-msg-face)
    3 7 (font-lock-face erc-direct-msg-face))
  "Message template for a NOTICE in a query buffer.")

(defvar erc--message-speaker-ctcp-action
  #("* %p%n %m"
    0 2 (font-lock-face erc-action-face)
    2 4 (font-lock-face (erc-nick-prefix-face erc-action-face))
    4 9 (font-lock-face erc-action-face))
  "Message template for a CTCP ACTION from another user.")

(defvar erc--message-speaker-ctcp-action-input
  (let ((base '(erc-input-face erc-action-face))) ; shared
    (concat (propertize "* " 'font-lock-face base)
            (propertize "%p" 'font-lock-face `(erc-my-nick-prefix-face ,@base))
            (propertize "%n" 'font-lock-face `(erc-my-nick-face ,@base))
            (propertize " %m" 'font-lock-face base)))
  "Message template for a CTCP ACTION from current client.")

(defvar erc--message-speaker-ctcp-action-statusmsg
  #("* (%p%n%s) %m"
    0 3 (font-lock-face erc-action-face)
    3 5 (font-lock-face (erc-nick-prefix-face erc-action-face))
    5 7 (font-lock-face erc-action-face)
    7 9 (font-lock-face (erc-notice-face erc-action-face))
    9 13 (font-lock-face erc-action-face))
  "Template for a CTCP ACTION status message from another chan op.")

(defvar erc--message-speaker-ctcp-action-statusmsg-input
  (let ((base '(erc-input-face erc-action-face))) ; shared
    (concat (propertize "* (" 'font-lock-face base)
            (propertize "%p" 'font-lock-face `(erc-my-nick-prefix-face ,@base))
            (propertize "%n" 'font-lock-face `(erc-my-nick-face ,@base))
            (propertize "%s" 'font-lock-face `(erc-notice-face ,@base))
            (propertize ") %m" 'font-lock-face base)))
  "Template for a CTCP ACTION status message from current client.")

(defun erc--speakerize-nick (nick &optional disp)
  "Propertize NICK with `erc--speaker' if not already present.
Do so to DISP instead if it's non-nil.  In either case, assign
NICK, sans properties, as the `erc--speaker' value.  As a side
effect, pair the latter string (the same `eq'-able object) with
the symbol `erc--spkr' in the \"msg prop\" environment for any
imminent `erc-display-message' invocations.  While doing so,
include any overrides defined in `erc--message-speaker-catalog'."
  (let ((plain-nick (substring-no-properties nick)))
    (erc--ensure-spkr-prop plain-nick (get erc--message-speaker-catalog
                                           'erc--msg-prop-overrides))
    (if (text-property-not-all 0 (length (or disp nick))
                               'erc--speaker nil (or disp nick))
        (or disp nick)
      (propertize (or disp nick) 'erc--speaker plain-nick))))

(defun erc--determine-speaker-message-format-args
    (nick message queryp privmsgp inputp &optional statusmsg prefix disp-nick)
  "Return a list consisting of a \"speaker\"-template key and spec args.
Consider the three flags QUERYP, PRIVMSGP, and INPUTP, as well as
the possibly null STATUSMSG string.  (Combined, these describe
the context of a newly arrived \"PRIVMSG\" or, when PRIVMSGP is
nil, a \"NOTICE\").  Interpret QUERYP to mean that MESSAGE is
directed at the ERC client itself (a direct message), and INPUTP
to mean MESSAGE is an outgoing or echoed message originating from
or meant to simulate prompt input.  Interpret a non-nil STATUSMSG
to mean MESSAGE should be formatted as a special channel message
intended for privileged members of the same or greater status.

After deciding on the template key for the current \"speaker\"
catalog, use the remaining arguments, possibly along with
STATUSMSG, to construct the appropriate spec-args plist forming
the returned list's tail.  In this plist, pair the char ?n with
NICK, the nickname of the speaker and ?m with MESSAGE, the
message body.  When non-nil, assume DISP-NICK to be a possibly
phony display name to take the place of NICK for ?n.  When PREFIX
is non-nil, look up NICK's channel-membership status, possibly
using PREFIX itself if it's an `erc-channel-user' object, which
it must be when called outside of a channel buffer.  Pair the
result with the ?p specifier.  When STATUSMSG is non-nil, pair it
with the ?s specifier.  Ensure unused spec values are the empty
string rather than nil."
  (when prefix
    (setq prefix (erc-get-channel-membership-prefix
                  (if (erc-channel-user-p prefix) prefix nick))))
  (when (and queryp erc--target erc-format-query-as-channel-p
             (not (erc--target-channel-p erc--target)))
    (setq queryp nil))
  (list (cond (statusmsg (if inputp 'statusmsg-input 'statusmsg))
              (privmsgp (if queryp
                            (if inputp 'input-query-privmsg 'query-privmsg)
                          (if inputp 'input-chan-privmsg 'chan-privmsg)))
              (t (if queryp
                     (if inputp 'input-query-notice 'query-notice)
                   (if inputp 'input-chan-notice 'chan-notice))))
        ?p (or prefix "") ?n (erc--speakerize-nick nick disp-nick)
        ?s (or statusmsg "") ?m message))

(defcustom erc-show-speaker-membership-status nil
  "Whether to prefix speakers with their channel status.
For example, when this option is non-nil and some nick \"Alice\"
has operator status in the current channel, ERC displays their
leading \"speaker\" label as <@Alice> instead of <Alice>."
  :package-version '(ERC . "5.6")
  :group 'erc-display
  :type 'boolean)

(define-obsolete-variable-alias 'erc-format-nick-function
  'erc-speaker-from-channel-member-function "30.1")
(defcustom erc-speaker-from-channel-member-function
  #'erc-determine-speaker-from-user
  "Function to determine a message's displayed \"speaker\" label.
Called with an `erc-server-user' object and an `erc-channel-user'
object, both possibly nil.  Use this option to do things like
provide localized display names.  To ask ERC to prepend
channel-membership \"status\" prefixes, like \"@\", to the
returned name, see `erc-show-speaker-membership-status'."
  :package-version '(ERC . "5.6")
  :group 'erc-display
  :type '(choice (function-item erc-determine-speaker-from-user) function))

(define-obsolete-function-alias 'erc-format-nick
  #'erc-determine-speaker-from-user "30.1")
(defun erc-determine-speaker-from-user (&optional user _channel-data)
  "Return nickname slot of `erc-server-user' USER, when non-nil."
  (when user (erc-server-user-nickname user)))

(define-obsolete-function-alias 'erc-get-user-mode-prefix
  #'erc-get-channel-membership-prefix "30.1")
(defun erc-get-channel-membership-prefix (nick-or-cusr)
  "Return channel membership prefix for NICK-OR-CUSR as a string.
Ensure returned string has a `help-echo' text property with the
corresponding verbose membership type, like \"voice\", as its
value.  Expect NICK-OR-CUSR to be an `erc-channel-user' object or
a string nickname, not necessarily downcased.  When called in a
logically connected ERC buffer, use advertised prefix mappings.
For compatibility reasons, don't error when NICK-OR-CUSR is null,
but return nil instead of the empty string.  Otherwise, always
return a possibly empty string."
  (when nick-or-cusr
    (when (stringp nick-or-cusr)
      (setq nick-or-cusr (and erc-channel-members
                              (cdr (erc-get-channel-member nick-or-cusr)))))
    (cond
     ((null nick-or-cusr) "")
     ;; Special-case most common value.
     ((zerop (erc-channel-user-status nick-or-cusr)) "")
     ;; For compatibility, first check whether a parsed prefix exists.
     ((and-let* ((pfx-obj (erc--parsed-prefix)))
        (catch 'done
          (pcase-dolist (`(,letter . ,pfx)
                         (erc--parsed-prefix-alist pfx-obj))
            (when (erc--cusr-status-p nick-or-cusr letter)
              (throw 'done
                     (pcase letter
                       (?q (propertize (string pfx) 'help-echo "owner"))
                       (?a (propertize (string pfx) 'help-echo "admin"))
                       (?o (propertize (string pfx) 'help-echo "operator"))
                       (?h (propertize (string pfx) 'help-echo "half-op"))
                       (?v (propertize (string pfx) 'help-echo "voice"))
                       (_ (string pfx))))))
          "")))
     (t
      (cond ((erc-channel-user-owner nick-or-cusr)
             (propertize "~" 'help-echo "owner"))
            ((erc-channel-user-admin nick-or-cusr)
             (propertize "&" 'help-echo "admin"))
            ((erc-channel-user-op nick-or-cusr)
             (propertize "@" 'help-echo "operator"))
            ((erc-channel-user-halfop nick-or-cusr)
             (propertize "%" 'help-echo "half-op"))
            ((erc-channel-user-voice nick-or-cusr)
             (propertize "+" 'help-echo "voice"))
            (t ""))))))

(defun erc-format-@nick (&optional user channel-data)
  "Format the nickname of USER showing if USER has a voice, is an
operator, half-op, admin or owner.  Owners have \"~\", admins have
\"&\", operators have \"@\" and users with voice have \"+\" as a
prefix.  Use CHANNEL-DATA to determine op and voice status."
  (declare (obsolete "see option `erc-show-speaker-membership-status'" "30.1"))
  (when user
    (let ((nick (erc-server-user-nickname user)))
      (if (not erc--speaker-status-prefix-wanted-p)
          (prog1 nick
            (setq erc--speaker-status-prefix-wanted-p 'erc-format-@nick))
        (concat (propertize
                 (erc-get-channel-membership-prefix channel-data)
                 'font-lock-face 'erc-nick-prefix-face)
                nick)))))

(defun erc-format-my-nick ()
  "Return the beginning of this user's message, correctly propertized."
  (if erc-show-my-nick
      (let* ((open "<")
             (close "> ")
             (nick (erc-current-nick))
             (mode (erc-get-channel-membership-prefix nick)))
        (erc--ensure-spkr-prop nick)
        (concat
         (propertize open 'font-lock-face 'erc-default-face)
         (propertize mode 'font-lock-face 'erc-my-nick-prefix-face)
         (propertize nick 'erc--speaker nick 'font-lock-face 'erc-my-nick-face)
         (propertize close 'font-lock-face 'erc-default-face)))
    (let ((prefix "> "))
      (propertize prefix 'font-lock-face 'erc-default-face))))

(defun erc--format-speaker-input-message (message)
  "Assemble outgoing MESSAGE entered at the prompt for insertion.
Intend \"input\" to refer to interactive prompt input as well as
the group of associated message-format templates from the
\"speaker\" catalog.  Format the speaker portion in a manner
similar to that performed by `erc-format-my-nick', but use either
`erc--message-speaker-input-chan-privmsg' or
`erc--message-speaker-input-query-privmsg' as a formatting
template, with MESSAGE being the actual message body.  Return a
copy with possibly shared text-property values."
  (if-let ((erc-show-my-nick)
           (nick (erc-current-nick))
           (pfx (erc-get-channel-membership-prefix nick))
           (erc-current-message-catalog erc--message-speaker-catalog)
           (key (if (or erc-format-query-as-channel-p
                        (erc--target-channel-p erc--target))
                    'input-chan-privmsg
                  'input-query-privmsg)))
      (progn
        (cond (erc--msg-props (puthash 'erc--msg key erc--msg-props))
              (erc--msg-prop-overrides (push (cons 'erc--msg key)
                                             erc--msg-prop-overrides)))
        (erc-format-message key ?p pfx ?n (erc--speakerize-nick nick)
                            ?m message))
    (propertize (concat "> " message) 'font-lock-face 'erc-input-face)))

(defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
  "Echo a private notice in the default buffer, namely the
target buffer specified by BUFFER, or there is no target buffer,
the server buffer.  This function is designed to be added to
either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
and always returns t."
  (erc-display-message parsed nil buffer s)
  t)

(defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
  "Echo a private notice in BUFFER, if BUFFER is non-nil.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
returns non-nil if BUFFER is non-nil."
  (if buffer
      (progn (erc-display-message parsed nil buffer s) t)
    nil))

(defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
  "Echo a private notice in the minibuffer.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
always returns t."
  (message "%s" (concat "NOTICE: " s))
  t)

(defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
  "Echo a private notice in the server buffer.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
always returns t."
  (erc-display-message parsed nil nil s)
  t)

(defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
  "Echo a private notice in the active buffer if the active
buffer is not the server buffer.  This function is designed to be
added to either `erc-echo-notice-hook' or
`erc-echo-notice-always-hook', and returns non-nil if the active
buffer is not the server buffer."
  (if (not (eq (erc-server-buffer) (erc-active-buffer)))
      (progn (erc-display-message parsed nil 'active s) t)
    nil))

(defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
  "Echo a private notice in the active buffer.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
always returns t."
  (erc-display-message parsed nil 'active s)
  t)

(defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
  "Echo a private notice in all of the buffers for which SENDER is a member.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
returns non-nil if there is at least one buffer for which the
sender is a member.

See also: `erc-echo-notice-in-first-user-buffer',
`erc-buffer-list-with-nick'."
  (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
    (if buffers
        (progn (erc-display-message parsed nil buffers s) t)
      nil)))

(defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
  "Echo a private notice in BUFFER and in all buffers for which SENDER is a member.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
returns non-nil if there is at least one buffer for which the
sender is a member or the default target.

See also: `erc-echo-notice-in-user-buffers',
`erc-buffer-list-with-nick'."
  (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
    (unless (memq buffer buffers) (push buffer buffers))
    (if buffers                         ;FIXME: How could it be nil?
        (progn (erc-display-message parsed nil buffers s) t)
      nil)))

(defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
  "Echo a private notice in one of the buffers for which SENDER is a member.
This function is designed to be added to either
`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
returns non-nil if there is at least one buffer for which the
sender is a member.

See also: `erc-echo-notice-in-user-buffers',
`erc-buffer-list-with-nick'."
  (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
    (if buffers
        (progn (erc-display-message parsed nil (car buffers) s) t)
      nil)))

;;; Ban manipulation

(defun erc-banlist-store (proc parsed)
  "Record ban entries for a channel."
  (pcase-let ((`(,channel ,mask ,whoset)
               (cdr (erc-response.command-args parsed))))
    ;; Determine to which buffer the message corresponds
    (let ((buffer (erc-get-buffer channel proc)))
      (with-current-buffer buffer
        (unless (member (cons whoset mask) erc-channel-banlist)
          (setq erc-channel-banlist (cons (cons whoset mask)
                                          erc-channel-banlist))))))
  nil)

(defun erc-banlist-finished (proc parsed)
  "Record that we have received the banlist."
  (let* ((channel (nth 1 (erc-response.command-args parsed)))
         (buffer (erc-get-buffer channel proc)))
    (with-current-buffer buffer
      (put 'erc-channel-banlist 'received-from-server t)))
  t)                                    ; suppress the 'end of banlist' message

(defun erc-banlist-update (proc parsed)
  "Check MODE commands for bans and update the banlist appropriately."
  ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
  (let* ((tgt (car (erc-response.command-args parsed)))
         (mode (erc-response.contents parsed))
         (whoset (erc-response.sender parsed))
         (buffer (erc-get-buffer tgt proc)))
    (when buffer
      (with-current-buffer buffer
        (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
              ((string-match "^\\([+-]\\)b" mode)
               ;; This is a ban
               (cond
                ((string-match "^-" mode)
                 ;; Remove the unbanned masks from the ban list
                 (setq erc-channel-banlist
                       (cl-delete-if
                        (lambda (y)
                          (member (upcase (cdr y))
                                  (mapcar #'upcase
                                          (cdr (split-string mode)))))
                        erc-channel-banlist)))
                ((string-match "^\\+" mode)
                 ;; Add the banned mask(s) to the ban list
                 (mapc
                  (lambda (mask)
                    (unless (member (cons whoset mask) erc-channel-banlist)
                      (setq erc-channel-banlist
                            (cons (cons whoset mask) erc-channel-banlist))))
                  (cdr (split-string mode))))))))))
  nil)

;; used for the banlist cmds
(defun erc-group-list (list n)
  "Group LIST into sublists of length N."
  (cond ((null list) nil)
        ((null (nthcdr n list)) (list list))
        (t (cons (cl-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))


;;; MOTD numreplies

(defun erc-handle-login ()
  "Handle the logging in process of connection."
  (unless erc-logged-in
    (setq erc-logged-in t)
    (message "Logging in as `%s'... done" (erc-current-nick))
    ;; execute a startup script
    (let ((f (erc-select-startup-file)))
      (when f
        (erc-load-script f)))))

(defun erc-connection-established (proc parsed)
  "Set user mode and run `erc-after-connect' hook in server buffer."
  (with-current-buffer (process-buffer proc)
    (unless erc-server-connected ; only once per session
      (let ((server (or erc-server-announced-name
                        (erc-response.sender parsed)))
            (nick (car (erc-response.command-args parsed)))
            (buffer (process-buffer proc)))
        (setq erc-server-connected t)
        (setq erc--server-last-reconnect-count erc-server-reconnect-count
              erc-server-reconnect-count 0)
        (setq erc--server-reconnect-display-timer
              (run-at-time erc-auto-reconnect-display-timeout nil
                           #'erc--server-last-reconnect-display-reset
                           (current-buffer)))
        (add-hook 'erc-disconnected-hook
                  #'erc--server-last-reconnect-on-disconnect nil t)
        (erc-update-mode-line)
        (erc-set-initial-user-mode nick buffer)
        (erc-server-setup-periodical-ping buffer)
        (when erc-unhide-query-prompt
          (erc-with-all-buffers-of-server erc-server-process nil
            (when (and erc--target (not (erc--target-channel-p erc--target)))
              (erc--unhide-prompt))))
        (run-hook-with-args 'erc-after-connect server nick)))))

(defun erc-set-initial-user-mode (nick buffer)
  "If `erc-user-mode' is non-nil for NICK, set the user modes.
The server buffer is given by BUFFER."
  (with-current-buffer buffer
    (when erc-user-mode
      (let* ((mode (if (functionp erc-user-mode)
                       (funcall erc-user-mode)
                     erc-user-mode))
             (groups (erc--parse-user-modes mode (erc--user-modes) t))
             (superfluous (last groups 2))
             (redundant-want (car superfluous))
             (redundant-drop (cadr superfluous)))
        (when redundant-want
          (erc-display-message nil 'notice buffer 'user-mode-redundant-add
                               ?m (apply #'string redundant-want)))
        (when redundant-drop
          (erc-display-message nil 'notice buffer 'user-mode-redundant-drop
                               ?m (apply #'string redundant-drop)))
        (when (stringp mode)
          (erc-log (format "changing mode for %s to %s" nick mode))
          (erc-server-send (format "MODE %s %s" nick mode)))))))

(defun erc-display-error-notice (parsed string)
  "Display STRING as an error notice.

See also `erc-display-message'."
  (erc-display-message
   parsed '(notice error) 'active string))

(defun erc-process-ctcp-query (proc parsed nick login host)
  ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
  "Process a CTCP query."
  (let ((queries (delete "" (split-string (erc-response.contents parsed)
                                          "\C-a"))))
    (if (> (length queries) 4)
        (erc-display-message
         parsed (list 'notice 'error) proc 'ctcp-too-many)
      (if (= 0 (length queries))
          (erc-display-message
           parsed (list 'notice 'error) proc
           'ctcp-empty ?n nick)
        (while queries
          (let* ((type (upcase (car (split-string (car queries)))))
                 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook")))
                 (erc--msg-prop-overrides `((erc--ctcp . ,(intern type))
                                            ,@erc--msg-prop-overrides)))
            (if (and hook (boundp hook))
                (if (string-equal type "ACTION")
                    (run-hook-with-args-until-success
                     hook proc parsed nick login host
                     (car (erc-response.command-args parsed))
                     (car queries))
                  (when erc-paranoid
                    (if (erc-current-nick-p
                         (car (erc-response.command-args parsed)))
                        (erc-display-message
                         parsed 'error 'active 'ctcp-request
                         ?n nick ?u login ?h host ?r (car queries))
                      (erc-display-message
                       parsed 'error 'active 'ctcp-request-to
                       ?n nick ?u login ?h host ?r (car queries)
                       ?t (car (erc-response.command-args parsed)))))
                  (run-hook-with-args-until-success
                   hook proc nick login host
                   (car (erc-response.command-args parsed))
                   (car queries)))
              (erc-display-message
               parsed (list 'notice 'error) proc
               'undefined-ctcp)))
          (setq queries (cdr queries)))))))

(defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))

(defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
  "Respond to a CTCP ACTION query."
  (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
    (let ((s (match-string 1 msg))
          (buf (or (erc-get-buffer to proc)
                   (erc-get-buffer nick proc)
                   (process-buffer proc)))
          (selfp (erc-current-nick-p nick)))
      (if erc--use-language-catalog-for-ctcp-action-p
          (progn
            (erc--ensure-spkr-prop nick)
            (setq nick (propertize nick 'erc--speaker nick))
            (erc-display-message parsed (if selfp 'input 'action) buf
                                 'ACTION ?n nick ?u login ?h host ?a s))
        (let* ((obj (and (erc--ctcp-response-p parsed) parsed))
               (buffer (and obj (erc--ctcp-response-buffer obj)))
               (stsmsg (and obj (erc--ctcp-response-statusmsg obj)))
               (prefix (and obj (erc--ctcp-response-prefix obj)))
               (dispnm (and obj (erc--ctcp-response-dispname obj)))
               (erc-current-message-catalog erc--message-speaker-catalog))
          (erc-display-message
           parsed nil (or buffer buf)
           (if selfp
               (if stsmsg 'ctcp-action-statusmsg-input 'ctcp-action-input)
             (if stsmsg 'ctcp-action-statusmsg 'ctcp-action))
           ?s (or stsmsg to)
           ?p (or (and (erc-channel-user-p prefix)
                       (erc-get-channel-membership-prefix prefix))
                  "")
           ?n (erc--speakerize-nick nick dispnm)
           ?m s))))))

(defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))

(defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
  "Respond to a CTCP CLIENTINFO query."
  (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
    (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
      (unless erc-disable-ctcp-replies
        (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
  nil)

(defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
(defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
  "Respond to a CTCP ECHO query."
  (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
    (let ((s (match-string 1 msg)))
      (unless erc-disable-ctcp-replies
        (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
  nil)

(defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
(defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
  "Respond to a CTCP FINGER query."
  (unless erc-disable-ctcp-replies
    (let ((s (if erc-anonymous-login
                 (format "FINGER I'm %s." (erc-current-nick))
               (format "FINGER %s (%s@%s)."
                       (user-full-name)
                       (user-login-name)
                       (system-name))))
          (ns (erc-time-diff erc-server-last-sent-time nil)))
      (when (> ns 0)
        (setq s (concat s " Idle for " (erc-sec-to-time ns))))
      (erc-send-ctcp-notice nick s)))
  nil)

(defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
(defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
  "Respond to a CTCP PING query."
  (when (string-match "^PING\\s-+\\(.*\\)" msg)
    (unless erc-disable-ctcp-replies
      (let ((arg (match-string 1 msg)))
        (erc-send-ctcp-notice nick (format "PING %s" arg)))))
  nil)

(defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
(defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
  "Respond to a CTCP TIME query."
  (unless erc-disable-ctcp-replies
    (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
  nil)

(defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
(defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
  "Respond to a CTCP USERINFO query."
  (unless erc-disable-ctcp-replies
    (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
  nil)

(defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
(defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
  "Respond to a CTCP VERSION query."
  (unless erc-disable-ctcp-replies
    (erc-send-ctcp-notice
     nick (format
           "VERSION %s (\C-b%s\C-b)"
           (erc-version nil 'bold-erc)
           erc-official-location)))
  nil)

(defun erc-process-ctcp-reply (proc parsed nick login host msg)
  "Process MSG as a CTCP reply."
  (let* ((type (car (split-string msg)))
         (hook (intern-soft (concat "erc-ctcp-reply-" type "-hook")))
         ;; Help `erc-display-message' by ensuring subsequent
         ;; insertions retain the necessary props.
         (cmd (erc--get-eq-comparable-cmd (erc-response.command parsed)))
         (erc--msg-prop-overrides `((erc--ctcp . ,(and hook (intern type)))
                                    (erc--cmd . ,cmd)
                                    ,@erc--msg-prop-overrides)))
    (if (and hook (boundp hook))
        (run-hook-with-args-until-success
         hook proc nick login host
         (car (erc-response.command-args parsed)) msg)
      (erc-display-message
       parsed 'notice 'active
       'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))

(defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
(defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
  "Handle a CTCP ECHO reply."
  (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
    (let ((message (match-string 1 msg)))
      (erc-display-message
       nil '(notice action) 'active
       'CTCP-ECHO ?n nick ?m message)))
  nil)

(defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
(defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
  "Handle a CTCP CLIENTINFO reply."
  (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
    (let ((message (match-string 1 msg)))
      (erc-display-message
       nil 'notice 'active
       'CTCP-CLIENTINFO ?n nick ?m message)))
  nil)

(defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
(defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
  "Handle a CTCP FINGER reply."
  (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
    (let ((message (match-string 1 msg)))
      (erc-display-message
       nil 'notice 'active
       'CTCP-FINGER ?n nick ?m message)))
  nil)

(defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
(defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
  "Handle a CTCP PING reply."
  (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
      nil
    (let ((time (match-string 1 msg)))
      (condition-case nil
          (let ((delta (erc-time-diff (string-to-number time) nil)))
            (erc-display-message
             nil 'notice 'active
             'CTCP-PING ?n nick
             ?t (erc-sec-to-time delta)))
        (range-error
         (erc-display-message
          nil 'error 'active
          'bad-ping-response ?n nick ?t time))))))

(defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
(defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
  "Handle a CTCP TIME reply."
  (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
    (let ((message (match-string 1 msg)))
      (erc-display-message
       nil 'notice 'active
       'CTCP-TIME ?n nick ?m message)))
  nil)

(defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
(defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
  "Handle a CTCP VERSION reply."
  (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
    (let ((message (match-string 1 msg)))
      (erc-display-message
       nil 'notice 'active
       'CTCP-VERSION ?n nick ?m message)))
  nil)

(defun erc-process-away (proc away-p)
  "Toggle the away status of the user depending on the value of AWAY-P.

If nil, set the user as away.
If non-nil, return from being away."
  (let ((sessionbuf (process-buffer proc)))
    (when sessionbuf
      (with-current-buffer sessionbuf
        (when erc-away-nickname
          (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
                           erc-away-nickname away-p))
          (erc-cmd-NICK (if away-p
                            erc-away-nickname
                          erc-nick)))
        (cond
         (away-p
          (setq erc-away (current-time)))
         (t
          (let ((away-time erc-away))
            ;; away must be set to NIL BEFORE sending anything to prevent
            ;; an infinite recursion
            (setq erc-away nil)
            (with-current-buffer (erc-active-buffer)
              (when erc-public-away-p
                (erc-send-action
                 (erc-default-target)
                 (if away-time
                     (format "is back (gone for %s)"
                             (erc-sec-to-time (erc-time-diff away-time nil)))
                   "is back")))))))))
    (erc-update-mode-line)))

;;;; List of channel members handling

(defun erc-channel-begin-receiving-names ()
  "Internal function.

Used when a channel names list is about to be received.  Should
be called with the current buffer set to the channel buffer.

See also `erc-channel-end-receiving-names'."
  (setq erc-channel-new-member-names (make-hash-table :test 'equal)))

(defun erc-channel-end-receiving-names ()
  "Internal function.

Used to fix `erc-channel-users' after a channel names list has been
received.  Should be called with the current buffer set to the
channel buffer.

See also `erc-channel-begin-receiving-names'."
  (maphash (lambda (nick _user)
             (if (null (gethash nick erc-channel-new-member-names))
                 (erc-remove-channel-user nick)))
           erc-channel-users)
  (setq erc-channel-new-member-names nil))

(defun erc-parse-prefix ()
  "Return an alist of valid prefix character types and their representations.
For example, if the current ISUPPORT \"PREFIX\" is \"(ov)@+\",
return an alist `equal' to ((?v . ?+) (?o . ?@)).  For historical
reasons, ensure the ordering of the returned alist is opposite
that of the advertised parameter."
  (let* ((str (or (erc--get-isupport-entry 'PREFIX t) "(qaohv)~&@%+"))
         (i 0)
         (j (string-search ")" str))
         collected)
    (when j
      (while-let ((u (aref str (cl-incf i)))
                  ((not (=  ?\) u))))
        (push (cons u (aref str (cl-incf j))) collected)))
    collected))

(defvar-local erc--parsed-prefix nil
  "Possibly stale `erc--parsed-prefix' struct instance for the server.
Use the \"getter\" function of the same name to obtain the current
value.")

(defun erc--parsed-prefix ()
  "Return possibly cached `erc--parsed-prefix' object for the server.
Ensure the returned value describes the most recent \"PREFIX\"
parameter advertised by the current server, with the original
ordering intact.  If no such parameter has yet arrived, return a
stand-in from the fallback value \"(qaohv)~&@%+\"."
  (erc--with-isupport-data PREFIX erc--parsed-prefix
    (let ((alist (erc-parse-prefix)))
      (make-erc--parsed-prefix
       :key key
       :letters (apply #'string (map-keys alist))
       :statuses (apply #'string (map-values alist))
       :alist (nreverse alist)))))

(defun erc--get-prefix-flag (char &optional parsed-prefix from-prefix-p)
  "Return numeric rank for CHAR or nil if unknown.
For example, given letters \"qaohv\" return 1 for ?v, 2 for ?h,
and 4 for ?o, etc.  If given, expect PARSED-PREFIX to be a
`erc--parsed-prefix' object.  With FROM-PREFIX-P, expect CHAR to
be a prefix instead."
  (and-let* ((obj (or parsed-prefix (erc--parsed-prefix)))
             (pos (erc--strpos char (if from-prefix-p
                                        (erc--parsed-prefix-statuses obj)
                                      (erc--parsed-prefix-letters obj)))))
    (ash 1 pos)))

(defun erc--init-cusr-fallback-status (voice halfop op admin owner)
  "Return channel-membership based on traditional status semantics.
Massage boolean switches VOICE, HALFOP, OP, ADMIN, and OWNER into
an internal numeric value suitable for the `status' slot of a new
`erc-channel-user' object."
  (let ((pfx (erc--parsed-prefix)))
    (+ (if voice  (if pfx (or (erc--get-prefix-flag ?v pfx) 0)  1) 0)
       (if halfop (if pfx (or (erc--get-prefix-flag ?h pfx) 0)  2) 0)
       (if op     (if pfx (or (erc--get-prefix-flag ?o pfx) 0)  4) 0)
       (if admin  (if pfx (or (erc--get-prefix-flag ?a pfx) 0)  8) 0)
       (if owner  (if pfx (or (erc--get-prefix-flag ?q pfx) 0) 16) 0))))

(defun erc--compute-cusr-fallback-status (current v h o a q)
  "Return current channel membership after toggling V H O A Q as requested.
Assume `erc--parsed-prefix' is non-nil in the current buffer.
Expect status switches V, H, O, A, Q, when non-nil, to be the
symbol `on' or `off'.  Return an internal numeric value suitable
for the `status' slot of an `erc-channel-user' object."
  (let (on off)
    (when v (push (or (erc--get-prefix-flag ?v) 0) (if (eq v 'on) on off)))
    (when h (push (or (erc--get-prefix-flag ?h) 0) (if (eq h 'on) on off)))
    (when o (push (or (erc--get-prefix-flag ?o) 0) (if (eq o 'on) on off)))
    (when a (push (or (erc--get-prefix-flag ?a) 0) (if (eq a 'on) on off)))
    (when q (push (or (erc--get-prefix-flag ?q) 0) (if (eq q 'on) on off)))
    (when on (setq current (apply #'logior current on)))
    (when off (setq current (apply #'logand current (mapcar #'lognot off)))))
  current)

(defcustom erc-channel-members-changed-hook nil
  "This hook is called every time the variable `channel-members' changes.
The buffer where the change happened is current while this hook is called."
  :group 'erc-hooks
  :type 'hook)

(defun erc--partition-prefixed-names (name)
  "From NAME, return a list of (STATUS NICK LOGIN HOST).
Expect NAME to be a prefixed name, like @bob."
  (unless (string-empty-p name)
    (let* ((status (erc--get-prefix-flag (aref name 0) nil 'from-prefix-p))
           (nick (if status (substring name 1) name)))
      (unless (string-empty-p nick)
        (list status nick nil nil)))))

(defun erc-channel-receive-names (names-string)
  "Update `erc-channel-members' from NAMES-STRING.
Expect NAMES-STRING to resemble the trailing argument of a 353
RPL_NAMREPLY.  Call internal handlers for parsing individual
names, whose expected composition may differ depending on enabled
extensions."
  (let ((names (delete "" (split-string names-string)))
        (erc-channel-members-changed-hook nil))
    (dolist (name names)
      (when-let ((args (erc--partition-prefixed-names name)))
        (pcase-let* ((`(,status ,nick ,login ,host) args)
                     (cmem (erc-get-channel-user nick)))
          (progn
	    ;; If we didn't issue the NAMES request (consider two clients
	    ;; talking to an IRC proxy), `erc-channel-begin-receiving-names'
	    ;; will not have been called, so we have to do it here.
	    (unless erc-channel-new-member-names
	      (erc-channel-begin-receiving-names))
            (puthash (erc-downcase nick) t erc-channel-new-member-names)
            (if cmem
                (erc--update-current-channel-member cmem status nil
                                                    nick host login)
              (erc--create-current-channel-member nick status nil
                                                  nick host login)))))))
  (run-hooks 'erc-channel-members-changed-hook))

(defun erc-update-user-nick (nick &optional new-nick
                                  host login full-name info)
  "Update the stored user information for the user with nickname NICK.

See also: `erc-update-user'."
  (erc-update-user (erc-get-server-user nick) new-nick
                   host login full-name info))

(defun erc-update-user (user &optional new-nick
                             host login full-name info)
  "Update user info for USER.
USER must be an erc-server-user struct.  Any of NEW-NICK, HOST,
LOGIN, FULL-NAME, INFO which are non-nil and not equal to the
existing values for USER are used to replace the stored values in
USER.

If, and only if, a change is made,
`erc-channel-members-changed-hook' is run for each channel for
which USER is a member, and t is returned."
  (let (changed)
    (when user
      (when (and new-nick
                 (not (equal (erc-server-user-nickname user)
                             new-nick)))
        (setq changed t)
        (erc-change-user-nickname user new-nick))
      (when (and host
                 (not (equal (erc-server-user-host user) host)))
        (setq changed t)
        (setf (erc-server-user-host user) host))
      (when (and login
                 (not (equal (erc-server-user-login user) login)))
        (setq changed t)
        (setf (erc-server-user-login user) login))
      (when (and full-name
                 (not (equal (erc-server-user-full-name user)
                             full-name)))
        (setq changed t)
        (setf (erc-server-user-full-name user) full-name))
      (when (and info
                 (not (equal (erc-server-user-info user) info)))
        (setq changed t)
        (setf (erc-server-user-info user) info))
      (if changed
          (dolist (buf (erc-server-user-buffers user))
            (if (buffer-live-p buf)
                (with-current-buffer buf
                  (run-hooks 'erc-channel-members-changed-hook))))))
    changed))

(defun erc--create-current-channel-member
    (nick status timep &optional new-nick host login full-name info)
  "Add an `erc-channel-member' entry for NICK.
Create a new `erc-server-users' entry if necessary, and ensure
`erc-channel-members-changed-hook' runs exactly once, regardless.
Pass STATUS to the `erc-channel-user' constructor.  With TIMEP,
assume NICK has just spoken, and initialize `last-message-time'.
Pass NEW-NICK, HOST, LOGIN, FULL-NAME, and INFO to
`erc-update-user' if a server user exists and otherwise to the
`erc-server-user' constructor."
  (cl-assert (null (erc-get-channel-member nick)))
  (let* ((user-changed-p nil)
         (down (erc-downcase nick))
         (user (gethash down (erc-with-server-buffer erc-server-users))))
    (if user
        (progn
          (cl-pushnew (current-buffer) (erc-server-user-buffers user))
          ;; Update *after* ^ so hook has chance to run.
          (setf user-changed-p (erc-update-user user new-nick host login
                                                full-name info)))
      (erc-add-server-user nick
                           (setq user (make-erc-server-user
                                       :nickname (or new-nick nick)
                                       :host host
                                       :full-name full-name
                                       :login login
                                       :info nil
                                       :buffers (list (current-buffer))))))
    (let ((cusr (erc-channel-user--make
                 :status (or status 0)
                 :last-message-time (and timep
                                         (erc-compat--current-lisp-time)))))
      (puthash down (cons user cusr) erc-channel-users))
    ;; An existing `cusr' was changed or a new one was added, and
    ;; `user' was not updated, though possibly just created (since
    ;; `erc-update-user' runs this same hook in all a user's buffers).
    (unless user-changed-p
      (run-hooks 'erc-channel-members-changed-hook))
    t))

(defun erc--update-current-channel-member (cmem status timep &rest user-args)
  "Update existing `erc-channel-member' entry.
Set the `status' slot of the entry's `erc-channel-user' side to
STATUS and, with TIMEP, update its `last-message-time'.  When
actual changes are made, run `erc-channel-members-changed-hook',
and return non-nil."
  (cl-assert cmem)
  (let ((cusr (cdr cmem))
        (user (car cmem))
        cusr-changed-p user-changed-p)
    (when (and status (/= status (erc-channel-user-status cusr)))
      (setf (erc-channel-user-status cusr) status
            cusr-changed-p t))
    (when timep
      (setf (erc-channel-user-last-message-time cusr)
            (erc-compat--current-lisp-time)))
    ;; Ensure `erc-channel-members-changed-hook' runs on change.
    (cl-assert (memq (current-buffer) (erc-server-user-buffers user)))
    (setq user-changed-p (apply #'erc-update-user user user-args))
    ;; An existing `cusr' was changed or a new one was added, and
    ;; `user' was not updated, though possibly just created (since
    ;; `erc-update-user' runs this same hook in all a user's buffers).
    (when (and cusr-changed-p (null user-changed-p))
      (run-hooks 'erc-channel-members-changed-hook))
    (erc-log (format "update-member: user = %S, cusr = %S" user cusr))
    (or cusr-changed-p user-changed-p)))

(defun erc-update-current-channel-member
    (nick new-nick &optional addp voice halfop op admin owner host login
          full-name info update-message-time)
  "Update or create entry for NICK in current `erc-channel-members' table.
With ADDP, ensure an entry exists.  When an entry does exist or
when ADDP is non-nil and an `erc-server-users' entry already
exists, call `erc-update-user' with NEW-NICK, HOST, LOGIN,
FULL-NAME, and INFO.  Expect any non-nil membership
status switches among VOICE, HALFOP, OP, ADMIN, and OWNER to be
the symbol `on' or `off' when needing to influence a new or
existing `erc-channel-user' object's `status' slot.  Likewise,
when UPDATE-MESSAGE-TIME is non-nil, update or initialize the
`last-message-time' slot to the current-time.  If changes occur,
including creation, run `erc-channel-members-changed-hook'.
Return non-nil when meaningful changes, including creation, have
occurred.

Without ADDP, do nothing unless a `erc-channel-members' entry
exists.  When it doesn't, assume the sender is a non-joined
entity, like the server itself or a historical speaker, or assume
the prior buffer for the channel was killed without parting."
(let* ((cmem (erc-get-channel-member nick))
       (status (and (or voice halfop op admin owner)
                    (if cmem
                        (erc--compute-cusr-fallback-status
                         (erc-channel-user-status (cdr cmem))
                         voice halfop op admin owner)
                      (erc--init-cusr-fallback-status
                       (and voice  (eq voice  'on))
                       (and halfop (eq halfop 'on))
                       (and op     (eq op     'on))
                       (and admin  (eq admin  'on))
                       (and owner  (eq owner  'on)))))))
  (if cmem
      (erc--update-current-channel-member cmem status update-message-time
                                          new-nick host login
                                          full-name info)
    (when addp
      (erc--create-current-channel-member nick status update-message-time
                                          new-nick host login
                                          full-name info)))))

(defun erc-update-channel-member (channel nick new-nick
                                          &optional add voice halfop op admin owner host login
                                          full-name info update-message-time)
  "Update user and channel for user with nickname NICK in channel CHANNEL.

See also: `erc-update-current-channel-member'."
  (erc-with-buffer
      (channel)
    (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
                                       login full-name info
                                       update-message-time)))

(defun erc-remove-current-channel-member (nick)
  "Remove NICK from current channel membership list.
Runs `erc-channel-members-changed-hook'."
  (let ((channel-data (erc-get-channel-user nick)))
    (when channel-data
      (erc-remove-channel-user nick)
      (run-hooks 'erc-channel-members-changed-hook))))

(defun erc-remove-channel-member (channel nick)
  "Remove NICK from CHANNEL's membership list.

See also `erc-remove-current-channel-member'."
  (erc-with-buffer
      (channel)
    (erc-remove-current-channel-member nick)))

(defun erc-update-channel-topic (channel topic &optional modify)
  "Find a buffer for CHANNEL and set the TOPIC for it.

If optional MODIFY is `append' or `prepend', then append or prepend the
TOPIC string to the current topic."
  (erc-with-buffer (channel)
    (cond ((eq modify 'append)
           (setq erc-channel-topic (concat erc-channel-topic topic)))
          ((eq modify 'prepend)
           (setq erc-channel-topic (concat topic erc-channel-topic)))
          (t (setq erc-channel-topic topic)))
    (erc-update-mode-line-buffer (current-buffer))))

(defun erc-set-modes (tgt mode-string)
  "Set the modes for the TGT provided as MODE-STRING."
  (declare (obsolete "see comment atop `erc--update-modes'" "30.1"))
  (let* ((modes (with-suppressed-warnings ((obsolete erc-parse-modes))
                  (erc-parse-modes mode-string)))
         (add-modes (nth 0 modes))
         ;; list of triples: (mode-char 'on/'off argument)
         (arg-modes (nth 2 modes)))
    (cond ((erc-channel-p tgt); channel modes
           (let ((buf (and erc-server-process
                           (erc-get-buffer tgt erc-server-process))))
             (when buf
               (with-current-buffer buf
                 (setq erc-channel-modes add-modes)
                 (setq erc-channel-user-limit nil)
                 (setq erc-channel-key nil)
                 (while arg-modes
                   (let ((mode (nth 0 (car arg-modes)))
                         (onoff (nth 1 (car arg-modes)))
                         (arg (nth 2 (car arg-modes))))
                     (cond ((string-match "^[Ll]" mode)
                            (erc-update-channel-limit tgt onoff arg))
                           ((string-match "^[Kk]" mode)
                            (erc-update-channel-key tgt onoff arg))
                           (t nil)))
                   (setq arg-modes (cdr arg-modes)))
                 (erc-update-mode-line-buffer buf)))))
          ;; we do not keep our nick's modes yet
          ;;(t (setq erc-user-modes add-modes))
          )
    ))

(defun erc-sort-strings (list-of-strings)
  "Sort LIST-OF-STRINGS in lexicographic order.

Side-effect free."
  (sort (copy-sequence list-of-strings) #'string<))

(defun erc-parse-modes (mode-string)
  "Parse MODE-STRING into a list.

Returns a list of three elements:

  (ADD-MODES REMOVE-MODES ARG-MODES).

The add-modes and remove-modes are lists of single-character strings
for modes without parameters to add and remove respectively.  The
arg-modes is a list of triples of the form:

  (MODE-CHAR ON/OFF ARGUMENT)."
  (declare (obsolete "see comment atop `erc--update-modes'" "30.1"))
  (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
      (let ((chars (mapcar #'char-to-string (match-string 1 mode-string)))
            ;; arguments in channel modes
            (args-str (match-string 2 mode-string))
            (args nil)
            (add-modes nil)
            (remove-modes nil)
            (arg-modes nil); list of triples: (mode-char 'on/'off argument)
            (add-p t))
        ;; make the argument list
        (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
          (setq args (cons (match-string 1 args-str) args))
          (setq args-str (match-string 2 args-str)))
        (setq args (nreverse args))
        ;; collect what modes changed, and match them with arguments
        (while chars
          (cond ((string= (car chars) "+") (setq add-p t))
                ((string= (car chars) "-") (setq add-p nil))
                ((string-match "^[qaovhbQAOVHB]" (car chars))
                 (setq arg-modes (cons (list (car chars)
                                             (if add-p 'on 'off)
                                             (if args (car args) nil))
                                       arg-modes))
                 (if args (setq args (cdr args))))
                ((string-match "^[LlKk]" (car chars))
                 (setq arg-modes (cons (list (car chars)
                                             (if add-p 'on 'off)
                                             (if (and add-p args)
                                                 (car args) nil))
                                       arg-modes))
                 (if (and add-p args) (setq args (cdr args))))
                (add-p (setq add-modes (cons (car chars) add-modes)))
                (t (setq remove-modes (cons (car chars) remove-modes))))
          (setq chars (cdr chars)))
        (setq add-modes (nreverse add-modes))
        (setq remove-modes (nreverse remove-modes))
        (setq arg-modes (nreverse arg-modes))
        (list add-modes remove-modes arg-modes))
    nil))

(defun erc-update-modes (tgt mode-string &optional _nick _host _login)
  "Update the mode information for TGT, provided as MODE-STRING.
Optional arguments: NICK, HOST and LOGIN - the attributes of the
person who changed the modes."
  (declare (obsolete "see comment atop `erc--update-modes'" "30.1"))
  ;; FIXME: neither of nick, host, and login are used!
  (let* ((modes (with-suppressed-warnings ((obsolete erc-parse-modes))
                  (erc-parse-modes mode-string)))
         (add-modes (nth 0 modes))
         (remove-modes (nth 1 modes))
         ;; list of triples: (mode-char 'on/'off argument)
         (arg-modes (nth 2 modes)))
    ;; now parse the modes changes and do the updates
    (cond ((erc-channel-p tgt); channel modes
           (let ((buf (and erc-server-process
                           (erc-get-buffer tgt erc-server-process))))
             (when buf
               ;; FIXME! This used to have an original buffer
               ;; variable, but it never switched back to the original
               ;; buffer. Is this wanted behavior?
               (set-buffer buf)
               (if (not (boundp 'erc-channel-modes))
                   (setq erc-channel-modes nil))
               (while remove-modes
                 (setq erc-channel-modes (delete (car remove-modes)
                                                 erc-channel-modes)
                       remove-modes (cdr remove-modes)))
               (while add-modes
                 (setq erc-channel-modes (cons (car add-modes)
                                               erc-channel-modes)
                       add-modes (cdr add-modes)))
               (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
               (while arg-modes
                 (let ((mode (nth 0 (car arg-modes)))
                       (onoff (nth 1 (car arg-modes)))
                       (arg (nth 2 (car arg-modes))))
                   (cond ((string-match "^[Vv]" mode)
                          (erc-update-channel-member tgt arg arg nil onoff))
                         ((string-match "^[hH]" mode)
                          (erc-update-channel-member tgt arg arg nil nil onoff))
                         ((string-match "^[oO]" mode)
                          (erc-update-channel-member tgt arg arg nil nil nil onoff))
                         ((string-match "^[aA]" mode)
                          (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
                         ((string-match "^[qQ]" mode)
                          (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
                         ((string-match "^[Ll]" mode)
                          (erc-update-channel-limit tgt onoff arg))
                         ((string-match "^[Kk]" mode)
                          (erc-update-channel-key tgt onoff arg))
                         (t nil)); only ops are tracked now
                   (setq arg-modes (cdr arg-modes))))
               (erc-update-mode-line buf))))
          ;; nick modes - ignored at this point
          (t nil))))

(defvar-local erc--channel-modes nil
  "When non-nil, a hash table of current channel modes.
Keys are characters.  Values are either a string, for types A-C,
or t, for type D.")

(defvar-local erc--channel-mode-types nil
  "Possibly stale `erc--channel-mode-types' instance for the server.
Use the getter of the same name to retrieve the current value.")

(defvar-local erc--mode-line-mode-string nil
  "Computed mode-line or header-line component for user/channel modes.")

(defvar erc--mode-line-chanmodes-arg-len 10
  "Max length at which to truncate channel-mode args in header line.")

(defun erc--channel-mode-types ()
  "Return variable `erc--channel-mode-types', possibly initializing it."
  (erc--with-isupport-data CHANMODES erc--channel-mode-types
    (let ((types (or key '(nil "Kk" "Ll" nil)))
          (ct (make-char-table 'erc--channel-mode-types))
          (type ?a))
      (dolist (cs types)
        (erc--doarray (c cs)
          (aset ct c type))
        (cl-incf type))
      (make-erc--channel-mode-types :key key
                                    :fallbackp (null key)
                                    :table ct))))

(defun erc--process-channel-modes (string args &optional status-letters)
  "Parse channel \"MODE\" changes and call unary letter handlers.
Update `erc-channel-modes' and `erc--channel-modes'.  With
STATUS-LETTERS, also update channel membership prefixes.  Expect
STRING to be the second argument from an incoming \"MODE\"
command and ARGS to be the remaining arguments, which should
complement relevant letters in STRING."
  (cl-assert (erc--target-channel-p erc--target))
  (let* ((obj (erc--channel-mode-types))
         (table (erc--channel-mode-types-table obj))
         (fallbackp (erc--channel-mode-types-fallbackp obj))
         (+p t))
    (erc--doarray (c string)
      (cond ((= ?+ c) (setq +p t))
            ((= ?- c) (setq +p nil))
            ((and status-letters (string-search (string c) status-letters))
             (erc--cusr-change-status (pop args) c +p))
            ((and-let* ((group (or (aref table c) (and fallbackp ?d))))
               (erc--handle-channel-mode group c +p
                                         (and (/= group ?d)
                                              (or (/= group ?c) +p)
                                              (pop args)))
               t))
            ((not fallbackp)
             (erc-display-message nil '(notice error) (erc-server-buffer)
                                  'channel-mode-unknown ?c (string c)))))
    (setq erc-channel-modes (sort erc-channel-modes #'string<))
    (setq erc--mode-line-mode-string
          (concat "+" (erc--channel-modes erc--mode-line-chanmodes-arg-len)))
    (erc-update-mode-line (current-buffer))))

(defvar-local erc--user-modes nil
  "Sorted list of current user \"MODE\" letters.
Analogous to `erc-channel-modes' but chars rather than strings.")

(defun erc--user-modes (&optional as-type)
  "Return user \"MODE\" letters in a form described by AS-TYPE.
When AS-TYPE is the symbol `strings' (plural), return a list of
strings.  When it's `string' (singular), return the same list
concatenated into a single string.  When AS-TYPE is nil, return a
list of chars."
  (let ((modes (or erc--user-modes (erc-with-server-buffer erc--user-modes))))
    (pcase as-type
      ('strings (mapcar #'char-to-string modes))
      ('string (apply #'string modes))
      (_ modes))))

(defun erc--channel-modes (&optional as-type sep)
  "Return channel \"MODE\" settings in a form described by AS-TYPE.
When AS-TYPE is the symbol `strings' (plural), return letter keys
as a list of sorted string.  When it's `string' (singular),
return keys as a single string.  When it's a number N, return a
single string consisting of the concatenated and sorted keys
followed by a space and then their corresponding args, each
truncated to N chars max.  ERC joins these args together with
SEP, which defaults to a single space.  Otherwise, return a
sorted alist of letter and arg pairs.  In all cases that include
values, respect `erc-show-channel-key-p' and optionally omit the
secret key associated with the letter k."
  (and-let* ((modes erc--channel-modes)
             (tobj (erc--channel-mode-types))
             (types (erc--channel-mode-types-table tobj)))
    (let (out)
      (maphash (lambda (k v)
                 (unless (eq ?a (aref types k))
                   (push (cons k
                               (and (not (eq t v))
                                    (not (and (eq k ?k)
                                              (not (bound-and-true-p
                                                    erc-show-channel-key-p))))
                                    v))
                         out)))
               modes)
      (setq out (cl-sort out #'< :key #'car))
      (pcase as-type
        ('strings (mapcar (lambda (o) (char-to-string (car o))) out))
        ('string (apply #'string (mapcar #'car out)))
        ((and (pred natnump) c)
         (let (keys vals)
           (pcase-dolist (`(,k . ,v) out)
             (when v
               (push (if (> (length v) c)
                         (with-memoization
                             (gethash (list c k v)
                                      (erc--channel-mode-types-shortargs tobj))
                           (truncate-string-to-width v c 0 nil t))
                       v)
                     vals))
             (push k keys))
           (concat (apply #'string (nreverse keys)) (and vals " ")
                   (string-join (nreverse vals) (or sep " ")))))
        (_ out)))))

(defun erc--parse-user-modes (string &optional current extrap)
  "Return lists of chars from STRING to add to and drop from CURRENT.
Expect STRING to be a so-called \"modestring\", the second
parameter of a \"MODE\" command, here containing only valid
user-mode letters.  Expect CURRENT to be a list of chars
resembling those found in `erc--user-modes'.  With EXTRAP, return
two additional lists of chars: those that would be added were
they not already present in CURRENT and those that would be
dropped were they not already absent."
  (let ((addp t)
        ;;
        redundant-add redundant-drop adding dropping)
    (erc--doarray (c string)
      (pcase c
        (?+ (setq addp t))
        (?- (setq addp nil))
        (_ (push c (let ((hasp (and current (memq c current))))
                     (if addp
                         (if hasp redundant-add adding)
                       (if hasp dropping redundant-drop)))))))
    (if extrap
        (list (nreverse adding) (nreverse dropping)
              (nreverse redundant-add) (nreverse redundant-drop))
      (list (nreverse adding) (nreverse dropping)))))

(defun erc--update-user-modes (string)
  "Update `erc--user-modes' from \"MODE\" STRING.
Return its value, a list of characters sorted by character code."
  (prog1
      (setq erc--user-modes
            (pcase-let ((`(,adding ,dropping)
                         (erc--parse-user-modes string erc--user-modes)))
              (sort (seq-difference (nconc erc--user-modes adding) dropping)
                    #'<)))
    (setq erc--mode-line-mode-string
          (concat "+" (erc--user-modes 'string)))))

(defun erc--update-channel-modes (string &rest args)
  "Update `erc-channel-modes' and call individual mode handlers.
Also update membership prefixes, as needed.  Expect STRING to be
a \"modestring\" and ARGS to match mode-specific parameters."
  (let ((status-letters (or (erc-with-server-buffer
                              (erc--parsed-prefix-letters
                               (erc--parsed-prefix)))
                            "qaovhbQAOVHB")))
    (erc--process-channel-modes string args status-letters)))

;; XXX this comment is referenced elsewhere (grep before deleting).
;;
;; The function `erc-update-modes' was deprecated in ERC 5.6 with no
;; immediate public replacement.  Third parties needing such a thing
;; are encouraged to write to emacs-erc@gnu.org with ideas for a
;; mode-handler API, possibly one incorporating letter-specific
;; handlers, like `erc--handle-channel-mode' (below), which only
;; handles mode types A-C.
(defun erc--update-modes (raw-args)
  "Handle user or channel \"MODE\" update from server.
Expect RAW-ARGS be a list consisting of a \"modestring\" followed
by mode-specific arguments."
  (if (and erc--target (erc--target-channel-p erc--target))
      (apply #'erc--update-channel-modes raw-args)
    (erc--update-user-modes (car raw-args))))

(defun erc--init-channel-modes (channel raw-args)
  "Set CHANNEL modes from RAW-ARGS.
Expect RAW-ARGS to be a \"modestring\" without any status-prefix
chars, followed by applicable arguments."
  (erc-with-buffer (channel)
    (erc--process-channel-modes (car raw-args) (cdr raw-args))))

(cl-defgeneric erc--handle-channel-mode (type letter state arg)
  "Handle a STATE change for mode LETTER of TYPE with ARG.
Expect to be called in the affected target buffer.  Expect TYPE
to be a character, like ?a, representing an advertised
\"CHANMODES\" group.  Expect LETTER to also be a character, and
expect STATE to be a boolean and ARGUMENT either a string or nil."
  (erc-log (format "Channel-mode %c (type %s, arg %S) %s"
                   letter type arg (if state 'enabled 'disabled))))

(cl-defmethod erc--handle-channel-mode :before (type c state arg)
  "Record STATE change for mode letter C.
When STATE is non-nil, add or update C's mapping in
`erc--channel-modes', associating it with ARG if C takes a
parameter and t otherwise.  When STATE is nil, forget the
mapping.  For type A, add up update a permanent mapping for C,
associating it with an integer indicating a running total of
STATE changes since joining the channel.  In most cases, this
won't match the number known to the server."
  (unless erc--channel-modes
    (cl-assert (erc--target-channel-p erc--target))
    (setq erc--channel-modes (make-hash-table)))
  (if (= type ?a)
      (cl-callf (lambda (s) (+ (or s 0) (if state +1 -1)))
          (gethash c erc--channel-modes))
    (if state
        (puthash c (or arg t) erc--channel-modes)
      (remhash c erc--channel-modes))))

(cl-defmethod erc--handle-channel-mode :before ((_ (eql ?d)) c state _)
  "Update `erc-channel-modes' for any character C of nullary type D.
Remember when STATE is non-nil and forget otherwise."
  (setq erc-channel-modes
        (if state
            (cl-pushnew (char-to-string c) erc-channel-modes :test #'equal)
          (delete (char-to-string c) erc-channel-modes))))

;; We could specialize on type C, but that may be too brittle.
(cl-defmethod erc--handle-channel-mode (_ (_ (eql ?l)) state arg)
  "Update channel user limit, remembering ARG when STATE is non-nil."
  (erc-update-channel-limit (erc--target-string erc--target)
                            (if state 'on 'off)
                            arg))

;; We could specialize on type B, but that may be too brittle.
(cl-defmethod erc--handle-channel-mode (_ (_ (eql ?k)) state arg)
  "Update channel key, remembering ARG when state is non-nil."
  ;; Mimic old parsing behavior in which an ARG of "*" was discarded
  ;; even though `erc-update-channel-limit' checks STATE first.
  (erc-update-channel-key (erc--target-string erc--target)
                          (if state 'on 'off)
                          (if (equal arg "*") nil arg)))

(defun erc-update-channel-limit (channel onoff n)
  "Update CHANNEL's user limit to N.
Expect ONOFF to be `on' when the mode is being enabled and `off'
otherwise.  And because this mode is of \"type C\", expect N to
be non-nil only when enabling."
  (if (or (not (eq onoff 'on))
          (and (stringp n) (string-match "^[0-9]+$" n)))
      (erc-with-buffer
          (channel)
        (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
              (t (setq erc-channel-user-limit nil))))))

(defun erc-update-channel-key (channel onoff key)
  "Update CHANNEL's key to KEY if ONOFF is `on' or to nil if it's `off'."
  (erc-with-buffer
      (channel)
    (cond ((eq onoff 'on) (setq erc-channel-key key))
          (t (setq erc-channel-key nil)))))

(defun erc-handle-user-status-change (type nlh &optional l)
  "Handle changes in any user's status.

So far, only nick change is handled.

Generally, the TYPE argument is a symbol describing the change type, NLH is
a list containing the original nickname, login name and hostname for the user,
and L is a list containing additional TYPE-specific arguments.

So far the following TYPE/L pairs are supported:

       Event                    TYPE                   L

    nickname change            `nick'                (NEW-NICK)"
  (erc-log (format "user-change: type: %S  nlh: %S  l: %S" type nlh l))
  (cond
   ;; nickname change
   ((equal type 'nick)
    t)
   (t
    nil)))

(defun erc-highlight-notice (s)
  "Highlight notice message S and return it.
See also variable `erc-notice-highlight-type'."
  (cond
   ((eq erc-notice-highlight-type 'prefix)
    (erc-put-text-property 0 (length erc-notice-prefix)
                           'font-lock-face 'erc-notice-face s)
    s)
   ((eq erc-notice-highlight-type 'all)
    (erc-put-text-property 0 (length s) 'font-lock-face 'erc-notice-face s)
    s)
   (t s)))

(defun erc-make-notice (message)
  "Notify the user of MESSAGE."
  (when erc-minibuffer-notice
    (message "%s" message))
  (erc-highlight-notice (concat erc-notice-prefix message)))

(defun erc-highlight-error (s)
  "Highlight error message S and return it."
  (erc-put-text-property 0 (length s) 'font-lock-face 'erc-error-face s)
  s)

(defun erc-put-text-property (start end property value &optional object)
  "Set text-property for an object (usually a string).
START and END define the characters covered.
PROPERTY is the text-property set, usually the symbol `face'.
VALUE is the value for the text-property, usually a face symbol such as
the face `bold' or `erc-pal-face'.
OBJECT is a string which will be modified and returned.
OBJECT is modified without being copied first.

You can redefine or `defadvice' this function in order to add
EmacsSpeak support."
  (if erc--merge-text-properties-p
      (erc--merge-prop start end property value object)
    (put-text-property start end property value object)))

(defalias 'erc-list 'ensure-list)

(defconst erc--parse-user-regexp-pedantic
  (rx bot (? (? (group (+ (not (any "!@\r\n"))))) "!")
      (? (? (group (+ nonl))) "@")
      (? (group (+ nonl))) eot))

(defconst erc--parse-user-regexp-legacy
  "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$")

(defvar erc--parse-user-regexp erc--parse-user-regexp-legacy)

(defun erc-parse-user (string)
  "Parse STRING as a user specification (nick!login@host).

Return a list of the three separate tokens."
  (cond
   ((string-match erc--parse-user-regexp string)
    (list (match-string 1 string)
          (match-string 2 string)
          (match-string 3 string)))
   ;; Some bogus bouncers send Nick!(null), try to live with that.
   ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
    (list (match-string 1 string)
          ""
          (match-string 2 string)))
   (t
    (list string "" ""))))

(defun erc--parse-nuh (string)
  "Match STRING against `erc--parse-user-regexp-pedantic'.
Return nil or matching groups representing nick, login, and host,
any of which may be nil.  Expect STRING not to contain leading
prefix chars.  Return an empty nick component to indicate further
processing is required based on context.  Interpret a lone token
lacking delimiters or one with only a leading \"!\" as a host.

See associated unit test for precise behavior."
  (when (string-match erc--parse-user-regexp-pedantic string)
    (list (match-string 1 string)
          (match-string 2 string)
          (match-string 3 string))))

(defun erc--shuffle-nuh-nickward (nick login host)
  "Interpret results of `erc--parse-nuh', promoting loners to nicks."
  (cond (nick (cl-assert (null login)) (list nick login host))
        ((and (null login) host) (list host nil nil))
        ((and login (null host)) (list login nil nil))))

(defun erc-extract-nick (string)
  "Return the nick corresponding to a user specification STRING.

See also `erc-parse-user'."
  (car (erc-parse-user string)))

(defun erc-put-text-properties (start end properties
                                      &optional object value-list)
  "Set text-properties for OBJECT.

START and END describe positions in OBJECT.
If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
each property to the corresponding value in VALUE-LIST."
  (unless value-list
    (setq value-list (mapcar (lambda (_x) t)
                             properties)))
  (while (and properties value-list)
    (erc-put-text-property
     start end (pop properties) (pop value-list) object)))

;;; Input area handling:

(defun erc-beg-of-input-line ()
  "Return the value of `point' at the beginning of the input line.

Specifically, return the position of `erc-insert-marker'."
  (or (and (boundp 'erc-insert-marker)
           (markerp erc-insert-marker))
      (error "erc-insert-marker has no value, please report a bug"))
  (marker-position erc-insert-marker))

(defun erc-end-of-input-line ()
  "Return the value of `point' at the end of the input line."
  (point-max))

(defvar erc-last-input-time 0
  "Time of last successful call to `erc-send-current-line'.
If that function has never been called, the value is 0.")

(defcustom erc-accidental-paste-threshold-seconds 0.2
  "Minimum time, in seconds, before sending new lines via IRC.
If the value is a number, `erc-send-current-line' signals an error
if its previous invocation was fewer than this many seconds ago.
If the value is nil, `erc-send-current-line' always considers any
submitted line to be intentional.

This option mainly prevents text accidentally entered into Emacs
from being sent to the server.  Offending sources include
terminal multiplexers, desktop-automation scripts, and anything
capable of rapidly submitting successive lines of prompt input.
For example, if you could somehow manage to type \"one \\`RET'
two \\`RET' three \\`RET'\" at the prompt in less than
`erc-accidental-paste-threshold-seconds', ERC would send \"one\"
to the server, leave \"two\" at the prompt, and insert \"three\"
into an \"overflow\" buffer.  See `erc-inhibit-multiline-input'
and `erc-warn-about-blank-lines' for suppression involving input
yanked from the clipboard or the kill ring, which is a related
but separate concern.

Users of terminal multiplexers, in particular, should look into
support for \"bracketed pasting\", provided on the Emacs side by
libraries like `xterm' (and usually enabled by default).  When
everything's working smoothly, Emacs transparently arranges for
pasted text to appear on the kill ring, regardless of any
read-only warnings you may encounter.  And when point is in the
prompt area, ERC automatically yanks that text for previewing but
holds off on submitting it, for obvious reasons."
  :group 'erc
  :version "26.1"
  :type '(choice number (other :tag "disabled" nil)))

(defvar erc--input-line-delim-regexp (rx (| (: (? ?\r) ?\n) ?\r)))

(defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
  "Regular expression used for matching commands in ERC.")

(defun erc--check-prompt-input-for-excess-lines (_ lines)
  "Return non-nil when trying to send too many LINES."
  (when erc-inhibit-multiline-input
    (let ((max (if (eq erc-inhibit-multiline-input t)
                   2
                 erc-inhibit-multiline-input))
          (seen 0)
          last msg)
      (while (and lines (setq last (pop lines)) (< (cl-incf seen) max)))
      (when (= seen max)
        (push last lines)
        (setq msg
              (format "-- exceeded by %d (%d chars)"
                      (length lines)
                      (apply #'+ (mapcar #'length lines))))
        (unless (and erc-ask-about-multiline-input
                     (y-or-n-p (concat "Send input " msg "?")))
          (concat "Too many lines " msg))))))

(defun erc--check-prompt-input-for-something (string _)
  (when (string-empty-p string)
    (if erc-warn-about-blank-lines
        "Blank line - ignoring..."
      'invalid)))

(defun erc--count-blank-lines (lines)
  "Report on the number of whitespace-only and empty LINES.
Return a list of (BLANKS TO-PAD TO-STRIP).  Expect caller to know
that BLANKS includes non-empty whitespace-only lines and that no
padding or stripping has yet occurred."
  (let ((real 0) (total 0) (pad 0) (strip 0))
    (dolist (line lines)
      (if (string-match (rx bot (* (in " \t\f")) eot) line)
          (progn
            (cl-incf total)
            (if (zerop (match-end 0))
                (cl-incf strip)
              (cl-incf pad strip)
              (setq strip 0)))
        (cl-incf real)
        (unless (zerop strip)
          (cl-incf pad strip)
          (setq strip 0))))
    (when (and (zerop real) (not (zerop total)) (= total (+ pad strip)))
      (cl-incf strip (1- pad))
      (setq pad 1))
    (list total pad strip)))

(defvar erc--check-prompt-explanation nil
  "List of strings to print if no validator returns non-nil.")

(defun erc--check-prompt-input-for-multiline-blanks (_ lines)
  "Return non-nil when multiline prompt input has blank LINES.
Consider newlines to be intervening delimiters, meaning the empty
\"logical\" line between a trailing newline and `eob' constitutes
a separate message."
  (pcase-let ((`(,total ,pad ,strip)(erc--count-blank-lines lines)))
    (cond ((zerop total) nil)
          ((and erc-warn-about-blank-lines erc-send-whitespace-lines)
           (let (msg args)
             (unless (zerop strip)
               (push "stripping (%d)" msg)
               (push strip args))
             (unless (zerop pad)
               (when msg
                 (push "and" msg))
               (push "padding (%d)" msg)
               (push pad args))
             (when msg
               (push "blank" msg)
               (push (if (> (apply #'+ args) 1) "lines" "line") msg))
             (when msg
               (setf msg (nreverse msg)
                     (car msg) (capitalize (car msg))))
             (when msg
               (push (apply #'format (string-join msg " ") (nreverse args))
                     erc--check-prompt-explanation)
               nil)))
          (erc-warn-about-blank-lines
           (concat (if (= total 1)
                       (if (zerop strip) "Blank" "Trailing")
                     (if (= total strip)
                         (format "%d trailing" strip)
                       (format "%d blank" total)))
                   (and (> total 1) (/= total strip) (not (zerop strip))
                        (format " (%d trailing)" strip))
                   (if (= total 1) " line" " lines")
                   " detected (see `erc-send-whitespace-lines')"))
          (erc-send-whitespace-lines nil)
          (t 'invalid))))

(defun erc--check-prompt-input-for-point-in-bounds (_ _)
  "Return non-nil when point is before prompt."
  (when (< (point) (erc-beg-of-input-line))
    "Point is not in the input area"))

;; Originally, `erc-send-current-line' inhibited sends whenever a
;; server buffer was missing.  In 2007, this was narrowed to
;; occurrences involving process-dependent commands.  However, the
;; accompanying error message, which was identical to that emitted by
;; `erc-server-send', "ERC: No process running", was always inaccurate
;; because a server buffer can be alive and its process dead.
(defun erc--check-prompt-input-for-running-process (string _)
  "Return non-nil if STRING is a slash command missing a process.
Also do so when the server buffer has been killed."
  ;; Even if the server buffer has been killed, the user should still
  ;; be able to /reconnect and recall previous commands.
  (and (not (erc-command-no-process-p string))
       (or (and (not (erc-server-buffer-live-p)) "Server buffer missing")
           (and (not (erc-server-process-alive)) "Process not running"))))

(defun erc--check-prompt-input-for-multiline-command (line lines)
  "Return non-nil when non-blank lines follow a command line."
  (when (and (cdr lines)
             (string-match erc-command-regexp line)
             (seq-drop-while #'string-empty-p (reverse (cdr lines))))
    "Excess input after command line"))

(defvar erc--check-prompt-input-functions
  '(erc--check-prompt-input-for-point-in-bounds
    erc--check-prompt-input-for-something
    erc--check-prompt-input-for-multiline-command
    erc--check-prompt-input-for-multiline-blanks
    erc--check-prompt-input-for-running-process
    erc--check-prompt-input-for-excess-lines)
  "Validators for user input typed at prompt.
Called with two arguments: the current input submitted by the
user, as a string, along with the same input as a list of
strings.  If any member function returns non-nil, ERC abandons
processing and leaves pending input untouched in the prompt area.
When the returned value is a string, ERC passes it to
`user-error'.  Any other non-nil value tells ERC to abort
silently.  If all members return nil, and the variable
`erc--check-prompt-explanation' is a nonempty list of strings,
ERC prints them as a single message joined by newlines.")

(defun erc--run-input-validation-checks (state)
  "Run input checkers from STATE, an `erc--input-split' object."
  (let* ((erc--check-prompt-explanation nil)
         (msg (run-hook-with-args-until-success
               'erc--check-prompt-input-functions
               (erc--input-split-string state)
               (erc--input-split-lines state))))
    (cond ((stringp msg) (user-error msg))
          (msg (push msg (erc--input-split-abortp state)))
          (erc--check-prompt-explanation
           (message "%s" (string-join (nreverse erc--check-prompt-explanation)
                                      "\n"))))))

(defun erc--inhibit-slash-cmd-insertion (state)
  "Don't insert STATE object's message if it's a \"slash\" command."
  (when (erc--input-split-cmdp state)
    (setf (erc--input-split-insertp state) nil)))

(defun erc--make-input-split (string)
  (make-erc--input-split
   :string string
   :insertp erc-insert-this
   :sendp erc-send-this
   :lines (split-string string erc--input-line-delim-regexp)
   :cmdp (string-match erc-command-regexp string)))

(defun erc-send-current-line ()
  "Parse current line and send it to IRC."
  (interactive)
  (let ((now (current-time)))
    (if (or (not erc-accidental-paste-threshold-seconds)
            (time-less-p erc-accidental-paste-threshold-seconds
			 (time-subtract now erc-last-input-time)))
        (save-restriction
          ;; If there's an abbrev at the end of the line, expand it.
          (when (and abbrev-mode
                     (eolp))
            (expand-abbrev))
          (widen)
          (let* ((str (erc-user-input))
                 (state (erc--make-input-split str)))
            (run-hook-with-args 'erc--input-review-functions state)
            (when-let (((not (erc--input-split-abortp state)))
                       (inhibit-read-only t)
                       (erc--current-line-input-split state)
                       (old-buf (current-buffer)))
              (progn ; unprogn this during next major surgery
                (erc-set-active-buffer (current-buffer))
                ;; Kill the input and the prompt
                (delete-region erc-input-marker (erc-end-of-input-line))
                (unwind-protect
                    (erc--send-input-lines (erc--run-send-hooks state))
                  ;; Fix the buffer if the command didn't kill it
                  (when (buffer-live-p old-buf)
                    (with-current-buffer old-buf
                      (save-restriction
                        (widen)
                        (let ((buffer-modified (buffer-modified-p)))
                          (set-buffer-modified-p buffer-modified))))))

                ;; Only when last hook has been run...
                (run-hook-with-args 'erc-send-completed-hook str)))
            (setq erc-last-input-time now)))
      (switch-to-buffer "*ERC Accidental Paste Overflow*")
      (lwarn 'erc :warning
             "You seem to have accidentally pasted some text!"))))

(defun erc-user-input ()
  "Return the input of the user in the current buffer."
  (buffer-substring-no-properties
   erc-input-marker
   (erc-end-of-input-line)))

(defun erc--discard-trailing-multiline-nulls (state)
  "Remove trailing empty lines from STATE, an `erc--input-split' object.
When all lines are empty, remove all but the first."
  (when (erc--input-split-lines state)
    (let ((reversed (nreverse (erc--input-split-lines state))))
      (while (and (cdr reversed) (string-empty-p (car reversed)))
        (setq reversed (cdr reversed)))
      (setf (erc--input-split-lines state) (nreverse reversed)))))

(defun erc--split-lines (state)
  "Partition non-command input into lines of protocol-compliant length."
  ;; Prior to ERC 5.6, line splitting used to be predicated on
  ;; `erc-flood-protect' being non-nil.
  (unless (or (zerop erc-split-line-length) (erc--input-split-cmdp state))
    (setf (erc--input-split-lines state)
          (mapcan #'erc--split-line (erc--input-split-lines state)))))

(defun erc--run-send-hooks (lines-obj)
  "Run send-related hooks that operate on the entire prompt input.
Sequester some of the back and forth involved in honoring old
interfaces, such as the reconstituting and re-splitting of
multiline input.  Optionally readjust lines to protocol length
limits and pad empty ones, knowing full well that additional
processing may still corrupt messages before they reach the send
queue.  Expect LINES-OBJ to be an `erc--input-split' object."
  (progn ; FIXME remove `progn' after code review.
    (with-suppressed-warnings ((lexical str) (obsolete erc-send-this))
      (defvar str) ; see note in string `erc-send-input'.
      (let* ((str (string-join (erc--input-split-lines lines-obj) "\n"))
             (erc-send-this (erc--input-split-sendp lines-obj))
             (erc-insert-this (erc--input-split-insertp lines-obj))
             (state (progn
                      ;; This may change `str' and `erc-*-this'.
                      (run-hook-with-args 'erc-send-pre-hook str)
                      (make-erc-input
                       :string str
                       :insertp erc-insert-this
                       :sendp erc-send-this
                       :substxt (erc--input-split-substxt lines-obj)
                       :refoldp (erc--input-split-refoldp lines-obj)))))
        (run-hook-with-args 'erc-pre-send-functions state)
        (setf (erc--input-split-sendp lines-obj) (erc-input-sendp state)
              (erc--input-split-insertp lines-obj) (erc-input-insertp state)
              (erc--input-split-substxt lines-obj) (erc-input-substxt state)
              (erc--input-split-refoldp lines-obj) (erc-input-refoldp state)
              ;; See note in test of same name re trailing newlines.
              (erc--input-split-lines lines-obj)
              (let ((lines (split-string (erc-input-string state)
                                         erc--input-line-delim-regexp)))
                (if erc--allow-empty-outgoing-lines-p
                    lines
                  (cl-nsubst " " "" lines :test #'equal))))
        (when (erc--input-split-refoldp lines-obj)
          (erc--split-lines lines-obj)))))
  (when (and (erc--input-split-cmdp lines-obj)
             (cdr (erc--input-split-lines lines-obj)))
    (user-error "Multiline command detected" ))
  lines-obj)

(defun erc--send-input-lines (lines-obj)
  "Send lines in `erc--input-split-lines' object LINES-OBJ."
  (when (erc--input-split-sendp lines-obj)
    (let ((insertp (erc--input-split-insertp lines-obj))
          (substxt (erc--input-split-substxt lines-obj)))
      (when (and insertp substxt)
        (setq insertp nil)
        (if (functionp substxt)
            (apply substxt (erc--input-split-lines lines-obj))
          (erc-display-msg substxt)))
      (dolist (line (erc--input-split-lines lines-obj))
        (when insertp
          (erc-display-msg line))
        (erc-process-input-line (concat line "\n")
                                (null erc-flood-protect)
                                (not (erc--input-split-cmdp lines-obj)))))))

(defun erc-send-input (input &optional skip-ws-chk)
  "Treat INPUT as typed in by the user.
It is assumed that the input and the prompt is already deleted.
Return non-nil only if we actually send anything."
  ;; Handle different kinds of inputs
  (if (and (not skip-ws-chk)
           (erc--check-prompt-input-for-multiline-blanks
            input (split-string input erc--input-line-delim-regexp)))
      (when erc-warn-about-blank-lines
        (message "Blank line - ignoring...") ; compat
        (beep))
    ;; This dynamic variable is used by `erc-send-pre-hook'.  It's
    ;; obsolete, and when it's finally removed, this binding should
    ;; also be removed.
    (with-suppressed-warnings ((lexical str))
      (defvar str))
    (let ((str input)
          (erc-insert-this t)
	  (erc-send-this t)
	  state)
      ;; The calling convention of `erc-send-pre-hook' is that it
      ;; should change the dynamic variable `str' or set
      ;; `erc-send-this' to nil.  This has now been deprecated:
      ;; Instead `erc-pre-send-functions' is used as a filter to do
      ;; allow both changing and suppressing the string.
      (run-hook-with-args 'erc-send-pre-hook input)
      (setq state (make-erc-input :string str ;May be != from `input' now!
				  :insertp erc-insert-this
				  :sendp erc-send-this))
      (run-hook-with-args 'erc-pre-send-functions state)
      (when (and (erc-input-sendp state)
                 erc-send-this)
        (if-let* ((first (split-string (erc-input-string state)
                                       erc--input-line-delim-regexp))
                  (split (mapcan #'erc--split-line first))
                  (lines (nreverse (seq-drop-while #'string-empty-p
                                                   (nreverse split))))
                  ((string-match erc-command-regexp (car lines))))
            (progn
              ;; Asking users what to do here might make more sense.
              (cl-assert (not (cdr lines)))
              ;; The `force' arg (here t) is ignored for command lines.
              (erc-process-input-line (concat (car lines) "\n") t nil))
          (progn ; temporarily preserve indentation
            (dolist (line lines)
              (progn ; temporarily preserve indentation
                (when (erc-input-insertp state)
                  (erc-display-msg line))
                (erc-process-input-line (concat line "\n")
                                        (null erc-flood-protect) t))))
          t)))))

(defun erc-display-msg (line)
  "Insert LINE into current buffer and run \"send\" hooks.
Treat LINE as input submitted interactively at the prompt, such
as outgoing chat messages and echoed slash commands."
  (when erc-insert-this
    (save-excursion
      (erc--assert-input-bounds)
      (let ((insert-position (marker-position (goto-char erc-insert-marker)))
            (erc--msg-props (or erc--msg-props
                                (let ((ovs (seq-filter
                                            #'cdr erc--msg-prop-overrides)))
                                  (map-into `((erc--msg . msg) ,@(reverse ovs))
                                            'hash-table)))))
        (insert (erc--format-speaker-input-message line) "\n")
        (save-restriction
          (narrow-to-region insert-position (point))
          (run-hooks 'erc-send-modify-hook)
          (run-hooks 'erc-send-post-hook)
          (cl-assert (> (- (point-max) (point-min)) 1))
          (add-text-properties (point-min) (1+ (point-min))
                               (erc--order-text-properties-from-hash
                                erc--msg-props)))
        (erc--refresh-prompt)))))

(defun erc-command-symbol (command)
  "Return the ERC command symbol for COMMAND if it exists and is bound."
  (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
    (when (fboundp cmd) cmd)))

(defun erc-extract-command-from-line (line)
  "Extract a \"slash command\" and its args from a prompt-input LINE.
If LINE doesn't start with a slash command, return nil.  If it
does, meaning the pattern `erc-command-regexp' matches, return a
list of the form (COMMAND ARGS), where COMMAND is either a symbol
for a known handler function or `erc-cmd-default' if unknown.
When COMMAND has the symbol property `do-not-parse-args', return
a string in place of ARGS: that is, either LINE itself, when LINE
consists of only whitespace, or LINE stripped of any trailing
whitespace, including a final newline.  When COMMAND lacks the
symbol property `do-not-parse-args', return a possibly empty list
of non-whitespace tokens.  Do not perform any shell-style parsing
of quoted or escaped substrings."
  (when (string-match erc-command-regexp line)
    (let* ((cmd (erc-command-symbol (match-string 1 line)))
           ;; note: return is nil, we apply this simply for side effects
           (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
                           (setq cmd (symbol-function cmd))))
           (cmd-fun (or cmd #'erc-cmd-default))
           (arg (if cmd
                    (if (get cmd-fun 'do-not-parse-args)
                        (format "%s" (match-string 2 line))
                      (delete "" (split-string (erc-trim-string
                                                (match-string 2 line)) " ")))
                  line)))
      (list cmd-fun arg))))

(defun erc-split-multiline-safe (string)
  "Split STRING, containing multiple lines and return them in a list.
Do it only for STRING as the complete input, do not carry unfinished
strings over to the next call."
  (let ((l ())
        (i0 0)
        (doit t))
    (while doit
      (let ((i (string-match "\r?\n" string i0))
            (s (substring string i0)))
        (cond (i (setq l (cons (substring string i0 i) l))
                 (setq i0 (match-end 0)))
              ((> (length s) 0)
               (setq l (cons s l))(setq doit nil))
              (t (setq doit nil)))))
    (nreverse l)))

;; nick handling

(defun erc-set-current-nick (nick)
  "Set the current nickname to NICK."
  (with-current-buffer (if (buffer-live-p (erc-server-buffer))
                           (erc-server-buffer)
                         (current-buffer))
    (unless (equal erc-server-current-nick nick)
      (setq erc-server-current-nick nick)
      ;; This seems sensible but may well be superfluous.  Should
      ;; really prove that it's actually needed via test scenario.
      (when erc-server-connected
        (erc-networks--id-reload erc-networks--id)))
    nick))

(defun erc-current-nick ()
  "Return the current nickname."
  (with-current-buffer (if (buffer-live-p (erc-server-buffer))
                           (erc-server-buffer)
                         (current-buffer))
    erc-server-current-nick))

(defun erc-current-nick-p (nick)
  "Return non-nil if NICK is the current nickname."
  (erc-nick-equal-p nick (erc-current-nick)))

(defun erc-nick-equal-p (nick1 nick2)
  "Return non-nil if NICK1 and NICK2 are the same.

This matches strings according to the IRC protocol's case convention.

See also `erc-downcase'."
  (string= (erc-downcase nick1)
           (erc-downcase nick2)))

;; default target handling

(defun erc--current-buffer-joined-p ()
  "Return non-nil if the current buffer is a channel and is joined."
  (and (erc--target-channel-p erc--target)
       (erc--target-channel-joined-p erc--target)
       t))

(defun erc-default-target ()
  "Return the current channel or query target, if any.
For historical reasons, return nil in channel buffers if not
currently joined."
  (let ((tgt (car erc-default-recipients)))
    (cond
     ((not tgt) nil)
     ((listp tgt) (cdr tgt))
     (t tgt))))

(defun erc-add-default-channel (channel)
  "Add CHANNEL to the default channel list."
  (declare (obsolete "use `erc-cmd-JOIN' or similar instead" "29.1"))
  (let ((chl (downcase channel)))
    (setq erc-default-recipients
          (cons chl erc-default-recipients))))

(defun erc-delete-default-channel (channel &optional buffer)
  "Delete CHANNEL from the default channel list."
  (declare (obsolete "use `erc-cmd-PART' or similar instead" "29.1"))
  (with-current-buffer (if (and buffer
                                (bufferp buffer))
                           buffer
                         (current-buffer))
    (setq erc-default-recipients (delete (downcase channel)
                                         erc-default-recipients))))

(defun erc-add-query (nickname)
  "Add QUERY'd NICKNAME to the default channel list.

The previous default target of QUERY type gets removed."
  (declare (obsolete "use `erc-cmd-QUERY' or similar instead" "29.1"))
  (let ((d1 (car erc-default-recipients))
        (d2 (cdr erc-default-recipients))
        (qt (cons 'QUERY (downcase nickname))))
    (setq erc-default-recipients (cons qt (if (and (listp d1)
                                                   (eq (car d1) 'QUERY))
                                              d2
                                            erc-default-recipients)))))

(defun erc-delete-query ()
  "Delete the topmost target if it is a QUERY."
  (declare (obsolete "use one query buffer per target instead" "29.1"))
  (let ((d1 (car erc-default-recipients))
        (d2 (cdr erc-default-recipients)))
    (if (and (listp d1)
             (eq (car d1) 'QUERY))
        (setq erc-default-recipients d2)
      (error "Current target is not a QUERY"))))

;; FIXME move all ignore-related functionality to its own module,
;; required and enabled by default (until some major version change).
(defun erc-ignored-user-p (spec)
  "Return non-nil if SPEC matches something in `erc-ignore-list'.

Takes a full SPEC of a user in the form \"nick!login@host\", and
matches against all the regexp's in `erc-ignore-list'.  If any
match, returns that regexp."
  (catch 'found
    (dolist (ignored (erc-with-server-buffer erc-ignore-list))
      (if (string-match ignored spec)
          (throw 'found ignored)))))

(defun erc-ignored-reply-p (msg tgt proc)
  ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
  "Return non-nil if MSG matches something in `erc-ignore-reply-list'.

Takes a message MSG to a channel and returns non-nil if the addressed
user matches any regexp in `erc-ignore-reply-list'."
  (let ((target-nick (erc-message-target msg)))
    (if (not target-nick)
        nil
      (erc-with-buffer (tgt proc)
        (let ((user (erc-get-server-user target-nick)))
          (when user
            (erc-list-match erc-ignore-reply-list
                            (erc-user-spec user))))))))

(defun erc-message-target (msg)
  "Return the addressed target in MSG.

The addressed target is the string before the first colon in MSG."
  (if (string-match "^\\([^: \n]*\\):" msg)
      (match-string 1 msg)
    nil))

(defun erc-user-spec (user)
  "Create a nick!user@host spec from a user struct."
  (let ((nick (erc-server-user-nickname user))
        (host (erc-server-user-host user))
        (login (erc-server-user-login user)))
    (concat (or nick "")
            "!"
            (or login "")
            "@"
            (or host ""))))

(defun erc-list-match (lst str)
  "Return non-nil if any regexp in LST matches STR."
  (and lst (string-match (string-join lst "\\|") str)))

;; other "toggles"

(defun erc-toggle-ctcp-autoresponse (&optional arg)
  "Toggle automatic CTCP replies (like VERSION and PING).

If ARG is positive, turns CTCP replies on.

If ARG is non-nil and not positive, turns CTCP replies off."
  (interactive "P")
  (cond ((and (numberp arg) (> arg 0))
         (setq erc-disable-ctcp-replies t))
        (arg (setq erc-disable-ctcp-replies nil))
        (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
  (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))

(defun erc-toggle-flood-control (&optional arg)
  "Toggle use of flood control on sent messages.

If ARG is positive, use flood control.
If ARG is non-nil and not positive, do not use flood control.

See `erc-server-flood-margin' for an explanation of the available
flood control parameters."
  (interactive "P")
  (cond ((and (numberp arg) (> arg 0))
         (setq erc-flood-protect t))
        (arg (setq erc-flood-protect nil))
        (t (setq erc-flood-protect (not erc-flood-protect))))
  (message "ERC flood control is %s"
           (cond (erc-flood-protect "ON")
                 (t "OFF"))))

;; Some useful channel and nick commands for fast key bindings

(defun erc-invite-only-mode (&optional arg)
  "Turn on the invite only mode (+i) for the current channel.

If ARG is non-nil, turn this mode off (-i).

This command is sent even if excess flood is detected."
  (interactive "P")
  (erc-set-active-buffer (current-buffer))
  (let ((tgt (erc-default-target)))
    (if (or (not tgt) (not (erc-channel-p tgt)))
        (erc-display-message nil 'error (current-buffer) 'no-target)
      (erc-load-irc-script-lines
       (list (concat "/mode " tgt (if arg " -i" " +i")))
       t))))

(defun erc-get-channel-mode-from-keypress (key)
  "Read a key sequence and call the corresponding channel mode function.
After doing C-c C-o, type in a channel mode letter.

C-g means quit.
RET lets you type more than one mode at a time.
If \"l\" is pressed, `erc-set-channel-limit' gets called.
If \"k\" is pressed, `erc-set-channel-key' gets called.
Anything else will be sent to `erc-toggle-channel-mode'."
  (interactive "kChannel mode (RET to set more than one): ")
  (cond ((equal key "\C-g")
         (keyboard-quit))
        ((equal key "\C-m")
         (erc-insert-mode-command))
        ((equal key "l")
         (call-interactively 'erc-set-channel-limit))
        ((equal key "k")
         (call-interactively 'erc-set-channel-key))
        (t (erc-toggle-channel-mode key))))

(defun erc-toggle-channel-mode (mode &optional channel)
  "Toggle channel MODE.

If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
`erc-default-target'."
  (interactive "P")
  (erc-set-active-buffer (current-buffer))
  (let ((tgt (or channel (erc-default-target))))
    (if (or (null tgt) (null (erc-channel-p tgt)))
        (erc-display-message nil 'error 'active 'no-target)
      (let* ((active (member mode erc-channel-modes))
             (newstate (if active "OFF" "ON")))
        (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
        (message "Toggle channel mode %s %s" mode newstate)
        (erc-server-send (format "MODE %s %s%s"
                                 tgt (if active "-" "+") mode))))))

(defun erc-insert-mode-command ()
  "Insert the line \"/mode <current target> \" at `point'."
  (interactive)
  (let ((tgt (erc-default-target)))
    (if tgt (insert (concat "/mode " tgt " "))
      (erc-display-message nil 'error (current-buffer) 'no-target))))

(defun erc-channel-names ()
  "Run \"/names #channel\" in the current channel."
  (interactive)
  (erc-set-active-buffer (current-buffer))
  (let ((tgt (erc-default-target)))
    (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
      (erc-display-message nil 'error (current-buffer) 'no-target))))

(defun erc-remove-text-properties-region (start end &optional object)
  "Clears the region (START,END) in OBJECT from all colors, etc."
  (interactive "r")
  (save-excursion
    (let ((inhibit-read-only t))
      (set-text-properties start end nil object))))
(put 'erc-remove-text-properties-region 'disabled t)

;; script execution and startup

(defun erc-find-file (file &optional path)
  "Search for a FILE in the filesystem.
First the `default-directory' is searched for FILE, then any directories
specified in the list PATH.

If FILE is found, return the path to it."
  (let ((filepath file))
    (if (file-readable-p filepath) filepath
      (while (and path
                  (progn (setq filepath (expand-file-name file (car path)))
                         (not (file-readable-p filepath))))
        (setq path (cdr path)))
      (if path filepath nil))))

(defun erc-select-startup-file ()
  "Select an ERC startup file.
See also `erc-startup-file-list'."
  (catch 'found
    (dolist (f erc-startup-file-list)
      (setq f (convert-standard-filename f))
      (when (file-readable-p f)
        (throw 'found f)))))

(defun erc-find-script-file (file)
  "Search for FILE in `default-directory', and any in `erc-script-path'."
  (erc-find-file file erc-script-path))

(defun erc-load-script (file)
  "Load a script from FILE.

FILE must be the full name, it is not searched in the
`erc-script-path'.  If the filename ends with `.el', then load it
as an Emacs Lisp program.  Otherwise, treat it as a regular IRC
script."
  (erc-log (concat "erc-load-script: " file))
  (cond
   ((string-match "\\.el\\'" file)
    (load file))
   (t
    (erc-load-irc-script file))))

(defun erc-process-script-line (line &optional args)
  "Process an IRC script LINE.

Does script-specific substitutions (script arguments, current nick,
server, etc.) in LINE and returns it.

Substitutions are: %C and %c = current target (channel or nick),
%S %s = current server, %N %n = my current nick, and %x is x verbatim,
where x is any other character;
$* = the entire argument string, $1 = the first argument, $2 = the second,
and so on."
  (if (not args) (setq args ""))
  (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
         (percent-regexp "\\(%.\\)")
         (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
         (tgt (erc-default-target))
         (server (and (boundp 'erc-session-server) erc-session-server))
         (nick (erc-current-nick))
         (res "")
         (tmp nil)
         (arg-list nil)
         (arg-num 0))
    (if (not tgt) (setq tgt ""))
    (if (not server) (setq server ""))
    (if (not nick) (setq nick ""))
    ;; First, compute the argument list
    (setq tmp args)
    (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
      (setq arg-list (cons (match-string 1 tmp) arg-list))
      (setq tmp (match-string 2 tmp)))
    (setq arg-list (nreverse arg-list))
    (setq arg-num (length arg-list))
    ;; now do the substitution
    (setq tmp (string-match esc-regexp line))
    (while tmp
      ;;(message "beginning of while: tmp=%S" tmp)
      (let* ((hd (substring line 0 tmp))
             (esc "")
             (subst "")
             (tail (substring line tmp)))
        (cond ((string-match (concat "^" arg-esc-regexp) tail)
               (setq esc (match-string 1 tail))
               (setq tail (substring tail (match-end 1))))
              ((string-match (concat "^" percent-regexp) tail)
               (setq esc (match-string 1 tail))
               (setq tail (substring tail (match-end 1)))))
        ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
        (setq res (concat res hd))
        (setq subst
              (cond ((string= esc "") "")
                    ((string-match "^\\$\\*$" esc) args)
                    ((string-match "^\\$\\([0-9]+\\)$" esc)
                     (let ((n (string-to-number (match-string 1 esc))))
                       (message "n = %S, integerp(n)=%S" n (integerp n))
                       (if (<= n arg-num) (nth (1- n) arg-list) "")))
                    ((string-match "^%[Cc]$" esc) tgt)
                    ((string-match "^%[Ss]$" esc) server)
                    ((string-match "^%[Nn]$" esc) nick)
                    ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
                    (t (erc-log (format "Bad escape sequence in %s: %S\n"
                                        'erc-process-script-line esc))
                       (message "BUG IN ERC: esc=%S" esc)
                       "")))
        (setq line tail)
        (setq tmp (string-match esc-regexp line))
        (setq res (concat res subst))
        ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
        ))
    (setq res (concat res line))
    res))

(defun erc-load-irc-script (file &optional force)
  "Load an IRC script from FILE."
  (erc-log (concat "erc-load-script: " file))
  (let ((str (with-temp-buffer
               (insert-file-contents file)
               (buffer-string))))
    (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))

;; authentication

(defun erc--unfun (maybe-fn)
  "Return MAYBE-FN or whatever it returns."
  (let ((s (if (functionp maybe-fn) (funcall maybe-fn) maybe-fn)))
    (when (and erc-debug-irc-protocol
               erc--debug-irc-protocol-mask-secrets
               (stringp s))
      (put-text-property 0 (length s) 'erc-secret t s))
    s))

(defun erc-login ()
  "Perform user authentication at the IRC server."
  (erc-log (format "login: nick: %s, user: %s %s %s :%s"
                   (erc-current-nick)
                   (user-login-name)
                   (or erc-system-name (system-name))
                   erc-session-server
                   erc-session-user-full-name))
  (if erc-session-password
      (erc-server-send (concat "PASS :" (erc--unfun erc-session-password)))
    (message "Logging in without password"))
  (erc-server-send (format "NICK %s" (erc-current-nick)))
  (erc-server-send
   (format "USER %s %s %s :%s"
           ;; hacked - S.B.
           erc-session-username
           "0" "*"
           erc-session-user-full-name))
  (erc-update-mode-line))

;; connection properties' heuristics

(defun erc-determine-parameters (&optional server port nick name user passwd)
  "Determine the connection and authentication parameters.
Sets the buffer local variables:

- `erc-session-connector'
- `erc-session-server'
- `erc-session-port'
- `erc-session-user-full-name'
- `erc-session-username'
- `erc-session-password'
- `erc-server-current-nick'"
  (setq erc-session-connector erc-server-connect-function
        erc-session-server (erc-compute-server server)
        erc-session-port (or port erc-default-port)
        erc-session-user-full-name (erc-compute-full-name name)
        erc-session-username (erc-compute-user user)
        erc-session-password (erc--compute-server-password passwd nick))
  (erc-set-current-nick (erc-compute-nick nick)))

(defun erc-compute-server (&optional server)
  "Return an IRC server name.

This tries a number of increasingly more default methods until a
non-nil value is found.

- SERVER (the argument passed to this function)
- The `erc-server' option
- The value of the IRCSERVER environment variable
- The `erc-default-server' variable"
  (or server
      erc-server
      (getenv "IRCSERVER")
      erc-default-server))

(defun erc-compute-user (&optional user)
  "Return a suitable value for the session user name."
  (or user (if erc-anonymous-login erc-email-userid (user-login-name))))

(defun erc-compute-nick (&optional nick)
  "Return user's IRC nick.

This tries a number of increasingly more default methods until a
non-nil value is found.

- NICK (the argument passed to this function)
- The `erc-nick' option
- The value of the IRCNICK environment variable
- The result from the `user-login-name' function"
  (or nick
      (if (consp erc-nick) (car erc-nick) erc-nick)
      (getenv "IRCNICK")
      (user-login-name)))

(defun erc--compute-server-password (password nick)
  "Maybe provide a PASSWORD argument for the IRC \"PASS\" command.
When `erc-auth-source-server-function' is non-nil, call it with NICK for
the user field and use whatever it returns as the server password."
  (or password (and erc-auth-source-server-function
                    (not erc--server-reconnecting)
                    (not erc--target)
                    (funcall erc-auth-source-server-function :user nick))))

(defun erc-compute-full-name (&optional full-name)
  "Return user's full name.

This tries a number of increasingly more default methods until a
non-nil value is found.

- FULL-NAME (the argument passed to this function)
- The `erc-user-full-name' option
- The value of the IRCNAME environment variable
- The result from the `user-full-name' function"
  (or full-name
      erc-user-full-name
      (getenv "IRCNAME")
      (if erc-anonymous-login "unknown" nil)
      (user-full-name)))

(defun erc-compute-port (&optional port)
  "Return a port for an IRC server.

This tries a number of increasingly more default methods until a
non-nil value is found.

- PORT (the argument passed to this function)
- The `erc-port' option
- The `erc-default-port' variable"
  (erc-normalize-port (or port erc-port erc-default-port)))

;; time routines

(define-obsolete-function-alias 'erc-string-to-emacs-time #'string-to-number
  "27.1")

(defalias 'erc-emacs-time-to-erc-time #'float-time)
(defalias 'erc-current-time #'float-time)

(defun erc-time-diff (t1 t2)
  "Return the absolute value of the difference in seconds between T1 and T2."
  (abs (float-time (time-subtract t1 t2))))

(defun erc-time-gt (t1 t2)
  "Check whether T1 > T2."
  (declare (obsolete time-less-p "27.1"))
  (time-less-p t2 t1))

(defun erc-sec-to-time (ns)
  "Convert NS to a time string HH:MM.SS."
  (setq ns (truncate ns))
  (format "%02d:%02d.%02d"
          (/ ns 3600)
          (/ (% ns 3600) 60)
          (% ns 60)))

(defun erc-seconds-to-string (seconds)
  "Convert a number of SECONDS into an English phrase."
  (let (days hours minutes format-args output)
    (setq days          (/ seconds 86400)
          seconds       (% seconds 86400)
          hours         (/ seconds 3600)
          seconds       (% seconds 3600)
          minutes       (/ seconds 60)
          seconds       (% seconds 60)
          format-args   (if (> days 0)
                            `("%d days, %d hours, %d minutes, %d seconds"
                              ,days ,hours ,minutes ,seconds)
                          (if (> hours 0)
                              `("%d hours, %d minutes, %d seconds"
                                ,hours ,minutes ,seconds)
                            (if (> minutes 0)
                                `("%d minutes, %d seconds" ,minutes ,seconds)
                              `("%d seconds" ,seconds))))
          output        (apply #'format format-args))
    ;; Change all "1 units" to "1 unit".
    (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
      (setq output (replace-match "" nil nil output 2)))
    output))


;; info

(defconst erc-clientinfo-alist
  '(("ACTION" . "is used to inform about one's current activity")
    ("CLIENTINFO" . "gives help on CTCP commands supported by client")
    ("ECHO" . "echoes its arguments back")
    ("FINGER" . "shows user's name, location, and idle time")
    ("PING" . "measures delay between peers")
    ("TIME" . "shows client-side time")
    ("USERINFO" . "shows information provided by a user")
    ("VERSION" . "shows client type and version"))
  "Alist of CTCP CLIENTINFO for ERC commands.")

(defun erc-client-info (s)
  "Return CTCP CLIENTINFO on command S.
If S is nil or an empty string then return general CLIENTINFO."
  (if (or (not s) (string= s ""))
      (concat
       (apply #'concat
              (mapcar (lambda (e)
                        (concat (car e) " "))
                      erc-clientinfo-alist))
       ": use CLIENTINFO <COMMAND> to get more specific information")
    (let ((h (assoc (upcase s) erc-clientinfo-alist)))
      (if h
          (concat s " " (cdr h))
        (concat s ": unknown command")))))

;; Hook functions

(defun erc-directory-writable-p (dir)
  "Determine whether DIR is a writable directory.
If it doesn't exist, create it."
  (unless (file-attributes dir) (make-directory dir))
  (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))

;; FIXME make function obsolete or alias to something less confusing.
(defun erc-kill-query-buffers (process)
  "Kill all target buffers of PROCESS, including channel buffers.
Do nothing if PROCESS is not a process object."
  ;; here, we only want to match the channel buffers, to avoid
  ;; "selecting killed buffers" b0rkage.
  (when (processp process)
    (erc-with-all-buffers-of-server process (lambda () erc--target)
      (kill-buffer (current-buffer)))))

(defun erc-nick-at-point ()
  "Give information about the nickname at `point'.

If called interactively, give a human readable message in the
minibuffer.  If called programmatically, return the corresponding
entry of `channel-members'."
  (interactive)
  (require 'thingatpt)
  (let* ((word (word-at-point))
         (channel-data (erc-get-channel-user word))
         (cuser (cdr channel-data))
         (user (if channel-data
                   (car channel-data)
                 (erc-get-server-user word)))
         host login full-name nick voice halfop op admin owner)
    (when user
      (setq nick (erc-server-user-nickname user)
            host (erc-server-user-host user)
            login (erc-server-user-login user)
            full-name (erc-server-user-full-name user))
      (if cuser
          (setq voice (erc-channel-user-voice cuser)
                halfop (erc-channel-user-halfop cuser)
                op (erc-channel-user-op cuser)
                admin (erc-channel-user-admin cuser)
                owner (erc-channel-user-owner cuser))))
    (if (called-interactively-p 'interactive)
        (message "%s is %s@%s%s%s"
                 nick login host
                 (if full-name (format " (%s)" full-name) "")
                 (if (or voice halfop op admin owner)
                     (format " and is +%s%s%s%s%s on %s"
                             (if voice "v" "")
                             (if halfop "h" "")
                             (if op "o" "")
                             (if admin "a" "")
                             (if owner "q" "")
                             (erc-default-target))
                   ""))
      user)))

(defun erc-away-time ()
  "Return non-nil if the current ERC process is set away.

In particular, the time that we were set away is returned.
See `current-time' for details on the time format."
  (erc-with-server-buffer erc-away))

;; Mode line handling

(defcustom erc-mode-line-format "%S %a"
  "A string to be formatted and shown in the mode-line in `erc-mode'.

The string is formatted using `format-spec' and the result is set as the value
of `mode-line-buffer-identification'.

The following characters are replaced:
%a: String indicating away status or \"\" if you are not away
%l: The estimated lag time to the server
%m: The modes of the channel
%n: The current nick name
%N: The name of the network
%o: The topic of the channel
%p: The session port
%t: The name of the target (channel, nickname, or servername:port)
%s: In the server-buffer, this gets filled with the value of
    `erc-server-announced-name', in a channel, the value of
    (erc-default-target) also get concatenated.
%S: In the server-buffer, this gets filled with the value of
    `erc-network', in a channel, the value of (erc-default-target)
    also get concatenated."
  :group 'erc-mode-line-and-header
  :type 'string)

(defcustom erc-header-line-format "%n on %t (%m,%l) %o"
  "A string to be formatted and shown in the header-line in `erc-mode'.

Set this to nil if you do not want the header line to be
displayed.

See `erc-mode-line-format' for which characters are can be used."
  :group 'erc-mode-line-and-header
  :set (lambda (sym val)
         (set sym val)
         (when (fboundp 'erc-update-mode-line)
           (erc-update-mode-line nil)))
  :type '(choice (const :tag "Disabled" nil)
                 string))

;; This should optionally support the built-in `tab-bar'.
(defcustom erc-header-line-uses-tabbar-p nil
  "Use `tabbar-mode' integration instead of the header line.
This concerns a historical integration with the external library
`tabbar' <https://www.emacswiki.org/emacs/tabbar.el>, which
shouldn't be confused with the built-in `tab-bar' described in
Info node `(emacs) Tab Bars'."
  :group 'erc-mode-line-and-header
  :type 'boolean)

(defcustom erc-header-line-uses-help-echo-p t
  "Show header line in echo area or as a tooltip
when point moves to the header line."
  :group 'erc-mode-line-and-header
  :type 'boolean)

(defcustom erc-header-line-face-method nil
  "Determine what method to use when colorizing the header line text.

If nil, don't colorize the header text.
If given a function, call it and use the resulting face name.
Otherwise, use the `erc-header-line' face."
  :group 'erc-mode-line-and-header
  :type '(choice (const :tag "Don't colorize" nil)
                 (const :tag "Use the erc-header-line face" t)
                 (function :tag "Call a function")))

(defcustom erc-show-channel-key-p t
  "Show the channel key in the header line."
  :group 'erc-paranoia
  :type 'boolean)

(defcustom erc-mode-line-away-status-format
  "(AWAY since %a %b %d %H:%M) "
  "When you're away on a server, this is shown in the mode line.
This should be a string with substitution variables recognized by
`format-time-string'."
  :group 'erc-mode-line-and-header
  :type 'string)

(defun erc-shorten-server-name (server)
  "Shorten SERVER name according to `erc-common-server-suffixes'."
  (if (stringp server)
      (with-temp-buffer
        (insert server)
        (let ((alist erc-common-server-suffixes))
          (while alist
            (goto-char (point-min))
            (if (re-search-forward (caar alist) nil t)
                (replace-match (cdar alist)))
            (setq alist (cdr alist))))
        (buffer-string))))

(defun erc-format-target ()
  "Return the name of the target (channel or nickname or servername:port)."
  (let ((target (erc-default-target)))
    (or target
        (concat (erc-shorten-server-name
                 (or erc-server-announced-name
                     erc-session-server))
                ":" (erc-port-to-string erc-session-port)))))

(defun erc-format-target-and/or-server ()
  "Return the server name or the current target and server name combined."
  (let ((server-name (erc-shorten-server-name
                      (or erc-server-announced-name
                          erc-session-server))))
    (cond ((erc-default-target)
           (concat (erc-string-no-properties (erc-default-target))
                   "@" server-name))
          (server-name server-name)
          (t (buffer-name (current-buffer))))))

(defun erc-format-network ()
  "Return the name of the network we are currently on."
  (erc-network-name))

(defun erc-format-target-and/or-network ()
  "Return the network or the current target and network combined.
If the name of the network is not available, then use the
shortened server name instead."
  (if-let ((erc--target)
           (name (if-let ((erc-networks--id)
                          (esid (erc-networks--id-symbol erc-networks--id)))
                     (symbol-name esid)
                   (erc-shorten-server-name (or erc-server-announced-name
                                                erc-session-server)))))
      (concat (erc--target-string erc--target) "@" name)
    (buffer-name)))

(defun erc-format-away-status ()
  "Return a formatted `erc-mode-line-away-status-format' if `erc-away' is non-nil."
  (let ((a (erc-away-time)))
    (if a
        (format-time-string erc-mode-line-away-status-format a)
      "")))

(defvar-local erc--away-indicator nil
  "Cons containing an away indicator for the connection.")

(defvar erc-away-status-indicator "A"
  "String shown by various formatting facilities to indicate away status.
Currently only used by the option `erc-prompt-format'.")

(defun erc--format-away-indicator ()
  "Return char with `display' property of `erc--away-indicator'."
  (and-let* ((indicator (erc-with-server-buffer
                          (or erc--away-indicator
                              (setq erc--away-indicator (list "")))))
             (newcar (if (erc-away-time) erc-away-status-indicator "")))
    ;; Inform other buffers of the change when necessary.
    (let ((dispp (not erc--inhibit-prompt-display-property-p)))
      (unless (eq newcar (car indicator))
        (erc--refresh-prompt-continue (and dispp 'hooks-only-p))
        (setcar indicator newcar))
      (if dispp
          (propertize "(away?)" 'display indicator)
        newcar))))

(defvar-local erc--user-modes-indicator nil
  "Cons containing connection-wide indicator for user modes.")

;; If adding more of these functions, should factor out commonalities.
;; As of ERC 5.6, this is identical to the away variant aside from
;; the var names and `eq', which isn't important.
(defun erc--format-user-modes ()
  "Return server's user modes as a string"
  (and-let* ((indicator (erc-with-server-buffer
                          (or erc--user-modes-indicator
                              (setq erc--user-modes-indicator (list "")))))
             (newcar (erc--user-modes 'string)))
    (let ((dispp (not erc--inhibit-prompt-display-property-p)))
      (unless (string= newcar (car indicator))
        (erc--refresh-prompt-continue (and dispp 'hooks-only-p))
        (setcar indicator newcar))
      (if dispp
          (propertize "(user-modes?)" 'display indicator)
        newcar))))

(defun erc--format-channel-status-prefix ()
  "Return the current channel membership prefix."
  (and (erc--target-channel-p erc--target)
       (erc-get-channel-membership-prefix (erc-current-nick))))

(defun erc--format-modes (&optional no-query-p)
  "Return a string of channel modes in channels and user modes elsewhere.
With NO-QUERY-P, return nil instead of user modes in query
buffers.  Also return nil when mode information is unavailable."
  (cond ((erc--target-channel-p erc--target)
         (erc--channel-modes 'string))
        ((not (and erc--target no-query-p))
         (erc--format-user-modes))))

(defun erc-format-channel-modes ()
  "Return the current channel's modes."
  (concat (apply #'concat
                 "+" erc-channel-modes)
          (cond ((and erc-channel-user-limit erc-channel-key)
                 (if erc-show-channel-key-p
                     (format "lk %.0f %s" erc-channel-user-limit
                             erc-channel-key)
                   (format "kl %.0f" erc-channel-user-limit)))
                (erc-channel-user-limit
                 ;; Emacs has no bignums
                 (format "l %.0f" erc-channel-user-limit))
                (erc-channel-key
                 (if erc-show-channel-key-p
                     (format "k %s" erc-channel-key)
                   "k"))
                (t nil))))

(defun erc-format-lag-time ()
  "Return the estimated lag time to server, `erc-server-lag'."
  (let ((lag (erc-with-server-buffer erc-server-lag)))
    (cond (lag (format "lag:%.0f" lag))
          (t ""))))

;; TODO when ERC drops Emacs 28, replace the expressions in the format
;; spec below with functions.
(defun erc-update-mode-line-buffer (buffer)
  "Update the mode line in a single ERC buffer BUFFER."
  (with-current-buffer buffer
    (let ((spec `((?a . ,(erc-format-away-status))
                  (?l . ,(erc-format-lag-time))
                  (?m . ,(or erc--mode-line-mode-string ""))
                  (?n . ,(or (erc-current-nick) ""))
                  (?N . ,(erc-format-network))
                  (?o . ,(or (erc-controls-strip erc-channel-topic) ""))
                  (?p . ,(erc-port-to-string erc-session-port))
                  (?s . ,(erc-format-target-and/or-server))
                  (?S . ,(erc-format-target-and/or-network))
                  (?t . ,(erc-format-target))))
          (process-status (cond ((erc-server-process-alive buffer)
                                 (unless erc-server-connected
                                   ": connecting"))
                                ((erc-with-server-buffer
                                   erc--server-reconnect-timer)
                                 erc--mode-line-process-reconnecting)
                                (t
                                 ": CLOSED")))
          (face (cond ((eq erc-header-line-face-method nil)
                       nil)
                      ((functionp erc-header-line-face-method)
                       (funcall erc-header-line-face-method))
                      (t
                       'erc-header-line))))
      (setq mode-line-buffer-identification
            (list (format-spec erc-mode-line-format spec)))
      (setq mode-line-process process-status)
      (let ((header (if erc-header-line-format
                        (format-spec erc-header-line-format spec)
                      nil)))
        (cond (erc-header-line-uses-tabbar-p
               (when (boundp 'tabbar--local-hlf)
                 (setq-local tabbar--local-hlf header-line-format))
               (kill-local-variable 'header-line-format))
              ((null header)
               (setq header-line-format nil))
              (erc-header-line-uses-help-echo-p
               (let ((help-echo (with-temp-buffer
                                  (insert header)
                                  (fill-region (point-min) (point-max))
                                  (buffer-string))))
                 (setq header-line-format
                       (string-replace
                        "%"
                        "%%"
                        (if face
                            (propertize header 'help-echo help-echo 'face face)
                          (propertize header 'help-echo help-echo))))))
              (t (setq header-line-format
                       (if face
                           (propertize header 'face face)
                         header))))))
    (force-mode-line-update)))

(defun erc-update-mode-line (&optional buffer)
  "Update the mode line in BUFFER.

If BUFFER is nil, update the mode line in all ERC buffers."
  (if (and buffer (bufferp buffer))
      (erc-update-mode-line-buffer buffer)
    (dolist (buf (erc-buffer-list))
      (when (buffer-live-p buf)
        (erc-update-mode-line-buffer buf)))))

;; Miscellaneous

(defun erc-bug (subject)
  "Send a bug report to the Emacs bug tracker and ERC mailing list."
  (interactive "sBug Subject: ")
  (report-emacs-bug
   (format "ERC %s: %s" erc-version subject))
  (save-excursion
    (goto-char (point-min))
    (insert "X-Debbugs-CC: emacs-erc@gnu.org\n")))

(defconst erc--news-url
  "https://git.savannah.gnu.org/cgit/emacs.git/plain/etc/ERC-NEWS")

(defvar erc--news-temp-file nil)

(defun erc-news (arg)
  "Show ERC news in a manner similar to `view-emacs-news'.
With ARG, download and display the latest revision, which may
contain more up-to-date information, even for older versions."
  (interactive "P")
  (find-file
   (or (and erc--news-temp-file
            (time-less-p (current-time) (car erc--news-temp-file))
            (not (and arg (y-or-n-p (format "Re-fetch? "))))
            (cdr erc--news-temp-file))
       (and arg
            (with-current-buffer (url-retrieve-synchronously erc--news-url)
              (goto-char (point-min))
              (search-forward "200 OK" (pos-eol))
              (search-forward "\n\n")
              (delete-region (point-min) (point))
              ;; May warn about file having changed on disk (unless
              ;; `query-about-changed-file' is nil on 28+).
              (let ((tempfile (or (cdr erc--news-temp-file)
                                  (make-temp-file "erc-news."))))
                (write-region (point-min) (point-max) tempfile)
                (kill-buffer)
                (cdr (setq erc--news-temp-file
                           (cons (time-add (current-time) (* 60 60 12))
                                 tempfile))))))
       (and-let* ((file (or (eval-when-compile (macroexp-file-name))
                            (locate-library "erc")))
                  (dir (file-name-directory file))
                  (adjacent (expand-file-name "ERC-NEWS" dir))
                  ((file-exists-p adjacent)))
         adjacent)
       (expand-file-name "ERC-NEWS" data-directory)))
  (when (fboundp 'emacs-news-view-mode)
    (emacs-news-view-mode))
  (goto-char (point-min))
  (let ((v (mapcar #'number-to-string
                   (seq-take-while #'natnump (version-to-list erc-version)))))
    (while (and v (not (search-forward (concat "\014\n* Changes in ERC "
                                               (string-join v "."))
                                       nil t)))
      (setq v (butlast v))))
  (beginning-of-line))

(defun erc-port-to-string (p)
  "Convert port P to a string.
P may be an integer or a service name."
  (if (integerp p)
      (int-to-string p)
    p))

(defun erc-string-to-port (s)
  "Convert string S to either an integer port number or a service name."
  (if (numberp s)
      s
    (let ((n (string-to-number s)))
      (if (= n 0)
          s
        n))))

(defun erc-version (&optional here bold-erc)
  "Show the version number of ERC in the minibuffer.
If optional argument HERE is non-nil, insert version number at point.
If optional argument BOLD-ERC is non-nil, display \"ERC\" as bold."
  (interactive "P")
  (let ((version-string
         (format "%s %s (IRC client for GNU Emacs %s)"
                 (if bold-erc
                     "\C-bERC\C-b"
                   "ERC")
                 erc-version
                 emacs-version)))
    (if here
        (insert version-string)
      (if (called-interactively-p 'interactive)
          (message "%s" version-string)
        version-string))))

(defun erc-modes (&optional here)
  "Show the active ERC modes in the minibuffer.
If optional argument HERE is non-nil, insert version number at point."
  (interactive "P")
  (let ((string
         (mapconcat #'identity
                    (let (modes (case-fold-search nil))
                      (dolist (var (apropos-internal "^erc-.*mode$"))
                        (when (and (boundp var)
                                   (get var 'erc-module)
                                   (symbol-value var))
                          (setq modes (cons (concat "`" (symbol-name var) "'")
                                            modes))))
                      (sort modes #'string<))
                    ", ")))
    (if here
        (insert string)
      (if (called-interactively-p 'interactive)
          (message "%s" string)
        string))))

(defun erc-trim-string (s)
  "Trim leading and trailing spaces off S."
  (cond
   ((not (stringp s)) nil)
   ((string-match "^\\s-*$" s)
    "")
   ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
    (match-string 1 s))
   (t
    s)))

(defun erc-arrange-session-in-multiple-windows ()
  "Open a window for every non-server buffer related to `erc-session-server'.

All windows are opened in the current frame."
  (interactive)
  (unless erc-server-process
    (error "No erc-server-process found in current buffer"))
  (let ((bufs (erc-buffer-list nil erc-server-process)))
    (when bufs
      (delete-other-windows)
      (switch-to-buffer (car bufs))
      (setq bufs (cdr bufs))
      (while bufs
        (split-window)
        (other-window 1)
        (switch-to-buffer (car bufs))
        (setq bufs (cdr bufs))
        (balance-windows)))))

(defun erc-popup-input-buffer ()
  "Provide an input buffer."
  (interactive)
  (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
        (mode (intern
               (completing-read
                "Mode: "
                (mapcar (lambda (e)
                          (list (symbol-name e)))
                        (apropos-internal "-mode\\'" 'commandp))
                nil t))))
    (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
    (funcall mode)
    (narrow-to-region (point) (point))
    (shrink-window-if-larger-than-buffer)))

;;; Message catalog

(define-inline erc--make-message-variable-name (catalog key softp)
  "Return variable name conforming to ERC's message-catalog interface.
Given a CATALOG symbol `mycat' and format-string KEY `mykey',
also a symbol, return the symbol `erc-message-mycat-mykey'.  With
SOFTP, only do so when defined as a variable."
  (inline-quote
   (let* ((catname (symbol-name ,catalog))
          (prefix (if (eq ?- (aref catname 0)) "erc--message" "erc-message-"))
          (name (concat prefix catname "-" (symbol-name ,key))))
     (if ,softp
         (and-let* ((s (intern-soft name)) ((boundp s))) s)
       (intern name)))))

(defun erc-make-message-variable-name (catalog entry)
  "Create a variable name corresponding to CATALOG's ENTRY."
  (erc--make-message-variable-name catalog entry nil))

(defun erc-define-catalog-entry (catalog entry format-spec)
  "Set CATALOG's ENTRY to FORMAT-SPEC."
  (declare (obsolete "define manually using `defvar' instead" "30.1"))
  (set (erc-make-message-variable-name catalog entry)
       format-spec))

(defun erc-define-catalog (catalog entries)
  "Define a CATALOG according to ENTRIES."
  (declare (obsolete erc-define-message-format-catalog "30.1"))
  (with-suppressed-warnings ((obsolete erc-define-catalog-entry))
    (dolist (entry entries)
      (erc-define-catalog-entry catalog (car entry) (cdr entry)))))

(erc--define-catalog english
  ((bad-ping-response . "Unexpected PING response from %n (time %t)")
   (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
   (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
   (cannot-find-file . "Cannot find file %f")
   (cannot-read-file . "Cannot read file %f")
   (channel-mode-unknown . "Unknown channel mode: %c")
   (connect . "Connecting to %S:%p... ")
   (country . "%c")
   (country-unknown . "%d: No such domain")
   (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
   (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
   (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
   (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
   (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
   (flood-strict-mode
    . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
   (disconnected
    . "\n\n*** Connection failed!  Re-establishing connection...\n")
   (disconnected-noreconnect
    . "\n\n*** Connection failed!  Not re-establishing connection.\n")
   (ignore-list . "%-8p %s")
   (reconnecting . "Reconnecting in %ms: attempt %i/%n ...")
   (reconnect-canceled . "Canceled %u reconnect timer with %cs to go...")
   (finished . "\n\n*** ERC finished ***\n")
   (terminated . "\n\n*** ERC terminated: %e\n")
   (login . "Logging in as `%n'...")
   (nick-in-use . "%n is in use. Choose new nickname: ")
   (nick-too-long
    . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
   (no-default-channel . "No default channel")
   (no-invitation . "You've got no invitation")
   (no-target . "No target")
   (ops . "%i operator%s: %o")
   (ops-none . "No operators in this channel.")
   (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
   (user-mode-redundant-add
    . "Already have user mode(s): %m. Requesting again anyway.")
   (user-mode-redundant-drop
    . "Already without user mode(s): %m. Requesting removal anyway.")
   (variable-not-bound . "Variable not bound!")
   (ACTION . "* %n %a")
   (CTCP-CLIENTINFO . "Client info for %n: %m")
   (CTCP-ECHO . "Echo %n: %m")
   (CTCP-FINGER . "Finger info for %n: %m")
   (CTCP-PING . "Ping time to %n is %t")
   (CTCP-TIME . "Time by %n is %m")
   (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
   (CTCP-VERSION . "Version for %n is %m")
   (ERROR  . "==> ERROR from %s: %c\n")
   (INVITE . "%n (%u@%h) invites you to channel %c")
   (JOIN   . "%n (%u@%h) has joined channel %c")
   (JOIN-you . "You have joined channel %c")
   (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
   (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
   (KICK-by-you . "You have kicked %k off channel %c: %r")
   (MODE   . "%n (%u@%h) has changed mode for %t to %m")
   (MODE-nick . "%n has changed mode for %t to %m")
   (NICK   . "%n (%u@%h) is now known as %N")
   (NICK-you . "Your new nickname is %N")
   (PART   . #'erc-message-english-PART)
   (PING   . "PING from server (last: %s sec. ago)")
   (PONG   . "PONG from %h (%i second%s)")
   (QUIT   . "%n (%u@%h) has quit: %r")
   (TOPIC  . "%n (%u@%h) has set the topic for %c: \"%T\"")
   (WALLOPS . "Wallops from %n: %m")
   (s004   . "%s %v %U %C")
   (s221   . "User modes for %n: %m")
   (s252   . "%i operator(s) online")
   (s253   . "%i unknown connection(s)")
   (s254   . "%i channels formed")
   (s275   . "%n %m")
   (s301   . "%n is AWAY: %r")
   (s303   . "Is online: %n")
   (s305   . "%m")
   (s306   . "%m")
   (s307   . "%n %m")
   (s311   . "%n is %f (%u@%h)")
   (s312   . "%n is/was on server %s (%c)")
   (s313   . "%n is an IRC operator")
   (s314   . "%n was %f (%u@%h)")
   (s317   . "%n has been idle for %i")
   (s317-on-since . "%n has been idle for %i, on since %t")
   (s319   . "%n is on channel(s): %c")
   (s320   . "%n is an identified user")
   (s321   . "Channel  Users  Topic")
   (s322   . "%c [%u] %t")
   (s324   . "%c modes: %m")
   (s328   . "%c URL: %u")
   (s329   . "%c was created on %t")
   (s330   . "%n %a %i")
   (s331   . "No topic is set for %c")
   (s332   . "Topic for %c: %T")
   (s333   . "%c: topic set by %n, %t")
   (s341   . "Inviting %n to channel %c")
   (s352   . "%-11c %-10n %-4a %u@%h (%f)")
   (s353   . "Users on %c: %u")
   (s367   . "Ban for %b on %c")
   (s367-set-by . "Ban for %b on %c set by %s on %t")
   (s368   . "Banlist of %c ends.")
   (s379   . "%c: Forwarded to %f")
   (s391   . "The time at %s is %t")
   (s396   . "Your visible host has changed to %s")
   (s401   . "%n: No such nick/channel")
   (s402   . "%c: No such server")
   (s403   . "%c: No such channel")
   (s404   . "%c: Cannot send to channel")
   (s405   . "%c: You have joined too many channels")
   (s406   . "%n: There was no such nickname")
   (s412   . "No text to send")
   (s421   . "%c: Unknown command")
   (s431   . "No nickname given")
   (s432   . "%n is an erroneous nickname")
   (s442   . "%c: You're not on that channel")
   (s445   . "SUMMON has been disabled")
   (s446   . "USERS has been disabled")
   (s451   . "You have not registered")
   (s461   . "%c: not enough parameters")
   (s462   . "Unauthorized command (already registered)")
   (s463   . "Your host isn't among the privileged")
   (s464   . "Password incorrect")
   (s465   . "You are banned from this server")
   (s471   . "Max occupancy for channel %c exceeded: %s")
   (s473   . "Channel %c is invitation only")
   (s474   . "You can't join %c because you're banned (+b)")
   (s475   . "You must specify the correct channel key (+k) to join %c")
   (s481   . "Permission Denied - You're not an IRC operator")
   (s482   . "You need to be a channel operator of %c to do that")
   (s483   . "You can't kill a server!")
   (s484   . "Your connection is restricted!")
   (s485   . "You're not the original channel operator")
   (s491   . "No O-lines for your host")
   (s501   . "Unknown MODE flag")
   (s502   . "You can't change modes for other users")
   (s671   . "%n %a")))

(defun erc-message-english-PART (&rest args)
  "Format a proper PART message.

This function is an example on what could be done with formatting
functions."
  (let ((nick (cadr (memq ?n args)))
        (user (cadr (memq ?u args)))
        (host (cadr (memq ?h args)))
        (channel (cadr (memq ?c args)))
        (reason (cadr (memq ?r args))))
    (if (string= nick (erc-current-nick))
        (format "You have left channel %s" channel)
      (format "%s (%s@%s) has left channel %s%s"
              nick user host channel
              (if (not (string= reason ""))
                  (format ": %s"
                          (string-replace "%" "%%" reason))
                "")))))

(defun erc-retrieve-catalog-entry (key &optional catalog)
  "Retrieve `format-spec' entry for symbol KEY in CATALOG.
Without symbol CATALOG, use `erc-current-message-catalog'.  If
lookup fails, try the latter's `default-toplevel-value' if it's
not the same as CATALOG.  Failing that, try the `english' catalog
if yet untried."
  (unless catalog (setq catalog erc-current-message-catalog))
  (symbol-value
   (or (erc--make-message-variable-name catalog key 'softp)
       (let ((parent catalog)
             last)
         (while (and (setq parent (get parent 'erc--base-format-catalog))
                     (not (setq last (erc--make-message-variable-name
                                      parent key 'softp)))))
         last)
       (let ((default (default-toplevel-value 'erc-current-message-catalog)))
         (or (and (not (eq default catalog))
                  (erc--make-message-variable-name default key 'softp))
             (and (not (memq 'english (list default catalog)))
                  (erc--make-message-variable-name 'english key 'softp)))))))

(defun erc-format-message (msg &rest args)
  "Format MSG according to ARGS.

See also `format-spec'."
  (when (eq (logand (length args) 1) 1) ; oddp
    (error "Obscure usage of this function appeared"))
  (let ((entry (erc-retrieve-catalog-entry msg)))
    (when (not entry)
      (error "No format spec for message %s" msg))
    (when (functionp entry)
      (setq entry (apply entry args)))
    (format-spec entry (apply #'format-spec-make args) 'ignore)))

;;; Various hook functions

(defcustom erc-kill-server-hook '(erc-kill-server
                                  erc-networks-shrink-ids-and-buffer-names)
  "Invoked whenever a live server buffer is killed via `kill-buffer'."
  :package-version '(ERC . "5.5")
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-kill-channel-hook
  '(erc-part-channel-on-kill
    erc-networks-shrink-ids-and-buffer-names
    erc-networks-rename-surviving-target-buffer)
  "Invoked whenever a channel-buffer is killed via `kill-buffer'."
  :package-version '(ERC . "5.5")
  :group 'erc-hooks
  :type 'hook)

(defcustom erc-kill-buffer-hook
  '(erc-networks-shrink-ids-and-buffer-names
    erc-networks-rename-surviving-target-buffer)
  "Hook run whenever a query buffer is killed.

See also `kill-buffer'."
  :package-version '(ERC . "5.5")
  :group 'erc-hooks
  :type 'hook)

;; FIXME alias and deprecate current *-function suffixed name.
(defun erc-kill-buffer-function ()
  "Function to call when an ERC buffer is killed.
This function should be on `kill-buffer-hook'.
When the current buffer is in `erc-mode', this function will run
one of the following hooks:
`erc-kill-server-hook' if the server buffer was killed,
`erc-kill-channel-hook' if a channel buffer was killed,
or `erc-kill-buffer-hook' if any other buffer."
  (when (eq major-mode 'erc-mode)
    (erc-remove-channel-users)
    (cond
     ((eq (erc-server-buffer) (current-buffer))
      (run-hooks 'erc-kill-server-hook))
     ((erc--target-channel-p erc--target)
      (run-hooks 'erc-kill-channel-hook))
     (t
      (run-hooks 'erc-kill-buffer-hook)))))

(declare-function set-text-conversion-style "textconv.c")

(defun erc-check-text-conversion ()
  "Check if point is within the ERC prompt and toggle text conversion.
If `text-conversion-style' is not `action' if point is within the
prompt or `nil' otherwise, set it to such a value, so as to
guarantee that the input method functions properly for the
purpose of typing within the ERC prompt."
  (when (and (eq major-mode 'erc-mode)
             (fboundp 'set-text-conversion-style))
    (defvar text-conversion-style) ; avoid free variable warning on <=29
    (if (>= (point) (erc-beg-of-input-line))
        (unless (eq text-conversion-style 'action)
          (set-text-conversion-style 'action))
      (unless (not text-conversion-style)
        (set-text-conversion-style nil)))))

(defun erc-kill-server ()
  "Sends a QUIT command to the server when the server buffer is killed.
This function should be on `erc-kill-server-hook'."
  (when (erc-server-process-alive)
    (setq erc-server-quitting t)
    (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))

(define-obsolete-function-alias 'erc-kill-channel #'erc-part-channel-on-kill
  "30.1")
(defun erc-part-channel-on-kill ()
  "Send a \"PART\" when killing a channel buffer."
  (when (and (not erc-killing-buffer-on-part-p)
             (erc-server-process-alive))
    (let ((tgt (erc-default-target)))
      (if tgt
         (erc-server-send (format "PART %s :%s" tgt
                                  (funcall erc-part-reason nil))
                          nil tgt)))))

;;; Dealing with `erc-parsed'

(defun erc-find-parsed-property ()
  "Find the next occurrence of the `erc-parsed' text property."
  (text-property-not-all (point-min) (point-max) 'erc-parsed nil))

(defun erc-restore-text-properties ()
  "Ensure the `erc-parsed' and `tags' props cover the entire message."
  (when-let ((parsed-posn (erc-find-parsed-property))
              (found (erc-get-parsed-vector parsed-posn)))
    (put-text-property (point-min) (point-max) 'erc-parsed found)
    (when-let ((tags (get-text-property parsed-posn 'tags)))
      (put-text-property (point-min) (point-max) 'tags tags))))

(defun erc-get-parsed-vector (point)
  "Return the whole parsed vector on POINT."
  (get-text-property point 'erc-parsed))

(defun erc-get-parsed-vector-nick (vect)
  "Return nickname in the parsed vector VECT."
  (let* ((untreated-nick (and vect (erc-response.sender vect)))
         (maybe-nick (when untreated-nick
                       (car (split-string untreated-nick "!")))))
    (when (and (not (null maybe-nick))
               (erc-is-valid-nick-p maybe-nick))
      untreated-nick)))

(defun erc-get-parsed-vector-type (vect)
  "Return message type in the parsed vector VECT."
  (and vect
       (erc-response.command vect)))

(defun erc--get-eq-comparable-cmd (command)
  "Return a symbol or a fixnum representing a message's COMMAND.
See also `erc-message-type'."
  ;; IRC numerics are three-digit numbers, possibly with leading 0s.
  ;; To invert: (if (numberp o) (format "%03d" o) (symbol-name o))
  (if-let ((n (string-to-number command)) ((zerop n))) (intern command) n))

;; Teach url.el how to open irc:// URLs with ERC.
;; To activate, customize `url-irc-function' to `url-irc-erc'.

(defcustom erc-url-connect-function nil
  "When non-nil, a function used to connect to an IRC URL.
Called with a string meant to represent a URL scheme, like
\"ircs\", followed by any number of keyword arguments recognized
by `erc' and `erc-tls'."
  :group 'erc
  :package-version '(ERC . "5.5")
  :type '(choice (const nil) function))

(defun erc--url-default-connect-function (scheme &rest plist)
  (let* ((ircsp (if scheme
                    (string-suffix-p "s" scheme)
                  (or (eql 6697 (plist-get plist :port))
                      (yes-or-no-p "Connect using TLS? "))))
         (erc-server (plist-get plist :server))
         (erc-port (or (plist-get plist :port)
                       (and ircsp (erc-normalize-port 'ircs-u))
                       erc-port))
         (erc-nick (or (plist-get plist :nick) erc-nick))
         (erc-password (plist-get plist :password))
         (args (erc-select-read-args)))
    (unless ircsp
      (setq ircsp (eql 6697 erc-port)))
    (apply (if ircsp #'erc-tls #'erc) args)))

;;;###autoload
(defun erc-handle-irc-url (host port channel nick password &optional scheme)
  "Use ERC to IRC on HOST:PORT in CHANNEL.
If ERC is already connected to HOST:PORT, simply /join CHANNEL.
Otherwise, connect to HOST:PORT as NICK and /join CHANNEL.

Beginning with ERC 5.5, new connections require human intervention.
Customize `erc-url-connect-function' to override this."
  (when (eql port 0) (setq port nil))
  (let* ((net (erc-networks--determine host))
         (erc--display-context `((erc-interactive-display . url)
                                 ,@erc--display-context))
         (server-buffer
          ;; Viable matches may slip through the cracks for unknown
          ;; networks.  Additional passes could likely improve things.
          (car (erc-buffer-filter
                (lambda ()
                  (and (not erc--target)
                       (erc-server-process-alive)
                       ;; Always trust a matched network.
                       (or (and net (eq net (erc-network)))
                           (and (string-equal erc-session-server host)
                                ;; Ports only matter when dialed hosts
                                ;; match and we have sufficient info.
                                (or (not port)
                                    (= (erc-normalize-port erc-session-port)
                                       port)))))))))
         key deferred)
    (unless server-buffer
      (setq deferred t
            server-buffer (apply (or erc-url-connect-function
                                     #'erc--url-default-connect-function)
                                 scheme
                                 :server host
                                 `(,@(and port (list :port port))
                                   ,@(and nick (list :nick nick))
                                   ,@(and password `(:password ,password))))))
    (when channel
      ;; These aren't percent-decoded by default
      (when (string-prefix-p "%" channel)
        (setq channel (url-unhex-string channel)))
      (cl-multiple-value-setq (channel key) (split-string channel "[?]"))
      (if deferred
          ;; Alternatively, we could make this a defmethod, so when
          ;; autojoin is loaded, it can do its own thing.  Also, as
          ;; with `erc-once-with-server-event', it's fine to set local
          ;; hooks here because they're killed when reconnecting.
          (with-current-buffer server-buffer
            (letrec ((f (lambda (&rest _)
                          (remove-hook 'erc-after-connect f t)
                          (erc-cmd-JOIN channel key))))
              (add-hook 'erc-after-connect f nil t)))
        (with-current-buffer server-buffer
          (erc-cmd-JOIN channel key))))))

(provide 'erc)

;;; erc.el ends here

debug log:

solving c174ca4b30e ...
found c174ca4b30e in https://yhetil.org/emacs-bugs/87wmnt5bun.fsf__30326.6469318018$1715918366$gmane$org@neverwas.me/
found a1c94288e8d in https://yhetil.org/emacs-bugs/87seyj6wgh.fsf__27960.7857389905$1715779469$gmane$org@neverwas.me/
found c92fd42322a in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 c92fd42322ac784f5ba19f29b90cc4425e902d5f	lisp/erc/erc.el

applying [1/2] https://yhetil.org/emacs-bugs/87seyj6wgh.fsf__27960.7857389905$1715779469$gmane$org@neverwas.me/
diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index c92fd42322a..a1c94288e8d 100644


applying [2/2] https://yhetil.org/emacs-bugs/87wmnt5bun.fsf__30326.6469318018$1715918366$gmane$org@neverwas.me/
diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el
index a1c94288e8d..c174ca4b30e 100644

Checking patch lisp/erc/erc.el...
Applied patch lisp/erc/erc.el cleanly.
Checking patch lisp/erc/erc.el...
Applied patch lisp/erc/erc.el cleanly.

skipping https://yhetil.org/emacs-bugs/87wmnt5bun.fsf__30326.6469318018$1715918366$gmane$org@neverwas.me/ for c174ca4b30e
index at:
100644 c174ca4b30ec1ea81906fef2ab2318c148610114	lisp/erc/erc.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).