This file is indexed.

/usr/share/irssi/scripts/friends_shasta.pl is in irssi-scripts 20120326.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#!/usr/bin/perl -w
#
# This script may not work with irssi older than 0.8.5!
#
# Historical author of this script is Erkki Seppala <flux@inside.org>
# Now it's maintained by me, so i'm listed as an author.
# 
# $Id: friends.pl,v 1.3 2003/11/09 21:11:45 shasta Exp $ 

use strict;
use vars qw($VERSION %IRSSI);

$VERSION = "2.4.9";
%IRSSI = (
    authors	=> 'Jakub Jankowski',
    contact	=> 'shasta@toxcorp.com',
    name	=> 'Friends',
    description	=> 'Maintains list of people you know.',
    license	=> 'GNU GPLv2 or later',
    url		=> 'http://toxcorp.com/irc/irssi/friends/',
    changed	=> 'Sun Oct 9 22:12:43 2003'
);

use Irssi 20011201.0100 ();
use Irssi::Irc;

# friends.pl
my $friends_version = $VERSION . " (20031109)";

# release note, if any
my $release_note = "Please read http://toxcorp.com/irc/irssi/friends/current/README\n";

##############################################
# These variables are adjustable with /set
# but here are some 'safe' defaults:

# do you want to process CTCP queries?
my $default_friends_use_ctcp = 1;

# space-separated list of allowed (implemented ;) CTCP commands
my $default_friends_ctcp_commands = "OP VOICE LIMIT KEY INVITE PASS IDENT UNBAN";

# do you want to learn new users?
my $default_friends_learn = 1;

# do you want to autovoice already opped nicks?
my $default_friends_voice_opped = 0;

# do you want to show additional info with /whois?
my $default_friends_show_whois_extra = 1;

# which flags do you want to add automatically with /addfriend? (case *sensitive*)
my $default_friends_default_flags = "";

# default path to friendlist
my $default_friends_file = Irssi::get_irssi_dir() . "/friends";

# do you want to save friendlist every time irssi's setup is saved
my $default_friends_autosave = 0;

# do you want to backup your friendlist upon a save
my $default_friends_backup_friendlist = 1;

# backup suffix to use (unixtime if empty)
my $default_friends_backup_suffix = ".backup";

# do you want to show friend's flags while he joins a channel?
my $default_friends_show_flags_on_join = 1;

# do you want to revenge?
my $default_friends_revenge = 1;

# revenge mode:
# 0 Deop the user.
# 1 Deop the user and give them the +D flag for the channel.
# 2 Deop the user, give them the +D flag for the channel, and kick them.
# 3 Deop the user, give them the +D flag for the channel, kick, and ban them.
my $default_friends_revenge_mode = 0;

# do you want /findfriends to print info in separate windows for separate chans?
my $default_friends_findfriends_to_windows = 0;

# maximum size of operationQueue
my $default_friends_max_queue_size = 20;

# min delaytime
my $default_delay_min = 10;

# max delaytime
my $default_delay_max = 60;

###############################################################

# registering themes
Irssi::theme_register([
	'friends_empty',		'Your friendlist is empty. Add items with /ADDFRIEND',
	'friends_notenoughargs',	'Not enough arguments. Usage: $0',
	'friends_badargs',		'Bad arguments. Usage: $0',
	'friends_nosuch',		'No such friend %R$0%n',
	'friends_notonchan',		'Not on channel {hilight $0}',
	'friends_endof',		'End of $0 $1',
	'friends_badhandle',		'Wrong handle: %R$0%n. $1',
	'friends_notuniqhandle',	'Handle %R$0%n already exists, choose another one',
	'friends_version',		'friends.pl\'s version: {hilight $0} [$1]',
	'friends_file_written',		'friendlist written on: {hilight $0}',
	'friends_file_version',		'friendlist written with: {hilight $0} [$1]',
	'friends_filetooold',		'Friendfile too old, loading aborted',
	'friends_loaded',		'Loaded {hilight $0} friends from $1',
	'friends_saved',		'Saved {hilight $0} friends to $1',
	'friends_duplicate',		'Skipping %R$0%n [duplicate?]',
	'friends_checking',		'Checking {hilight $0} took {hilight $1} secs [on $2]',
	'friends_line_head',		'[$[!-3]0] Handle: %R$1%n, flags: %C$2%n [password: $3]',
	'friends_line_hosts',		'$[-6]9 Hosts: $0',
	'friends_line_chan',		'$[-6]9 Channel {hilight $0}: Flags: %c$1%n, Delay: $2',
	'friends_line_comment',		'$[-6]9 Comment: $0',
	'friends_line_currentnick',	'$[-6]9 [$1] Current nick: {nick $0}',
	'friends_line_channelson',	'$[-6]9 [$1] Currently sharing with you: $0',
	'friends_joined',		'{nick $0} is a friend, handle: %R$1%n, global flags: %C$2%n, flags for {hilight $3}: %C$4%n',
	'friends_whois',		'{whois friend handle: {hilight $0}, global flags: $1}',
	'friends_queue_empty',		'Operation queue is empty',
	'friends_queue_line1',		'[$[!-2]0] Operation: %R$1%n secs left before {hilight $2}',
	'friends_queue_line2',		'     (Server: {hilight $0}, Channel: {hilight $1}, Nicklist: $2)',
	'friends_queue_nosuch',		'No such entry in operation queue ($0)',
	'friends_queue_removed',	'$0 queues: {hilight $1} [$2]',
	'friends_friendlist',		'{hilight Friendlist} [$0]:',
	'friends_friendlist_count',	'Listed {hilight $0} friend$1',
	'friends_findfriends',		'Looking for %R$2%n on channel {hilight $0} [on $1]:',
	'friends_already_added',	'Nick {hilight $0} matches one of %R$1%n\'s hosts',
	'friends_added',		'Added %R$0%n to friendlist',
	'friends_removed',		'Removed %R$0%n from friendlist',
	'friends_comment_added',	'Added comment line to %R$0%n ($1)',
	'friends_comment_removed',	'Removed comment line from %R$0%n',
	'friends_host_added',		'Added {hilight $1} to %R$0%n',
	'friends_host_removed',		'Removed {hilight $1} from %R$0%n',
	'friends_host_exists',		'Hostmask {hilight $1} overlaps with one of the already added to %R$0%n',
	'friends_host_notexists',	'%R$0%n does not have {hilight $1} in hostlist',
	'friends_chanrec_removed',	'Removed {hilight $1} record from %R$0%n',
	'friends_chanrec_notexists',	'%R$0%n does not have {hilight $1} record',
	'friends_changed_handle',	'Changed {hilight $0} to %R$1%n',
	'friends_changed_delay',	'Changed %R$0%n\'s delay value on {hilight $1} to %c$2%n',
	'friends_chflagexec',		'Executing %c$0%n for %R$1%n ($2)',
	'friends_currentflags',		'Current {channel $2} flags for %R$1%n are: %c$0%n',
	'friends_chpassexec',		'Altered password for %R$0%n',
	'friends_ctcprequest',		'%R$0%n asks for {hilight $1} on {hilight $2}',
	'friends_ctcppass',		'Password for %R$0%n altered by $1',
	'friends_ctcpident',		'CTCP IDENT for %R$0%n from {hilight $1} succeeded',
	'friends_ctcpfail',		'Failed CTCP {hilight $0} from %R$1%n. $2',
	'friends_optree_header',	'Opping tree:',
	'friends_optree_line1',		'%R$0%n has opped these:',
	'friends_optree_line2',		'{hilight $[!-4]0} times: $1',
	'friends_general',		'$0',
	'friends_notice',		'[%RN%n] $0'
]);

my @friends = ();
my $all_regexp_hosts = {};
my $all_hosts = {};
my $all_handles = {};
my @operationQueue = ();
my $timerHandle = undef;
my $friends_file_version;
my $friends_file_written;

my $friends_PLAIN_HOSTS = 0;
my $friends_REGEXP_HOSTS = 1;

# Idea of moving userhost to a regexp and
# the subroutine userhost_to_regexp were adapted from people.pl,
# an userlist script made by Marcin 'Qrczak' Kowalczyk.
# You can get that script from http://qrnik.knm.org.pl/~qrczak/irssi/people.pl
# or from http://scripts.irssi.org/

# HostToRegexp
my %htr = ();
# fill the hash
foreach my $i (0..255) {
	my $ch = chr($i);
	$htr{$ch} = "\Q$ch\E";
}
# wildcards to regexp
$htr{'?'} = '.';
$htr{'*'} = '.*';

# str userhost_to_regexp($userhost)
# translates userhost to a regexp
# lowercases host-part
sub userhost_to_regexp($) {
	my ($mask) = @_;
	$mask = lowercase_hostpart($mask);
	$mask =~ s/(.)/$htr{$1}/g;
	return $mask;
}

# str lowercase_hostpart($userhost)
# returns userhost with host-part loweracased
sub lowercase_hostpart($) {
	my ($host) = @_;
	$host =~ s/(.+)\@(.+)/sprintf("%s@%s", $1, lc($2));/eg;
	return $host;
}

# void print_version($what)
# print's version of script/userlist
sub print_version($) {
	my ($what) = @_;
	$what = lc($what);

	if ($what eq "filever") {
		if ($friends_file_version) {
			my ($verbal, $numeric) = $friends_file_version =~ /^(.+)\ \(([0-9]+)\)$/;
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_file_version', $verbal, $numeric);
		} else {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_empty');
		}
	} elsif ($what eq "filewritten" && $friends_file_written) {
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($friends_file_written);
		my $written = sprintf("%4d%02d%02d %02d:%02d:%02d", ($year+1900), ($mon+1), $mday, $hour, $min, $sec);
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_file_written', $written);
	} else {
		my ($verbal, $numerical) = $friends_version =~ /^(.+)\ \(([0-9]+)\)$/;
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_version', $verbal, $numerical);
	}
}

# void print_releasenote()
# suprisingly, prints a release note ;^)
sub print_releasenote {
	foreach my $line (split(/\n/, $release_note)) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notice', $line);
	}
}

# str friends_crypt($plain)
# returns crypt()ed $plain, using random salt;
# or "" if $plain is empty
sub friends_crypt {
	return if ($_[0] eq "");
	return crypt("$_[0]", (join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
}

# bool friend_passwdok($idx, $pwd)
# returns 1 if password is ok, 0 if isn't
sub friends_passwdok {
	my ($idx, $pwd) = @_;
	return 1 if (crypt("$pwd", $friends[$idx]->{password}) eq $friends[$idx]->{password});
	return 0;
}

# arr get_friends_channels($idx)
# returns list of $friends[$idx] channels
sub get_friends_channels {
	return keys(%{$friends[$_[0]]->{channels}});
}

# arr get_friends_hosts($idx, $type)
# returns list of $friends[$idx] regexp-hostmask if $type=$friends_REGEXP_HOSTS
# returns list of plain-hostmasks if $type=$friends_PLAIN_HOSTS
sub get_friends_hosts($$) {
	if ($_[1] == $friends_REGEXP_HOSTS) {
		return keys(%{$friends[$_[0]]->{regexp_hosts}});
	} elsif ($_[1] == $friends_PLAIN_HOSTS) {
		return keys(%{$friends[$_[0]]->{hosts}});
	}
	return undef;
}

# str get_friends_flags($idx[, $chan])
# returns list of $chan flags for $idx
# $chan can be also 'global' or undef
# case insensitive about the $chan
sub get_friends_flags {
	my ($idx, $chan) = @_;
	$chan = lc($chan);
	if ($chan eq "" || $chan eq "global") {
		return $friends[$idx]->{globflags};
	} else {
		foreach my $friendschan (get_friends_channels($idx)) {
			if ($chan eq lc($friendschan)) {
				return $friends[$idx]->{channels}->{$friendschan}->{flags};
			}
		}
	}
	return;
}

# str get_friends_delay($idx[, $chan])
# returns $chan delay for $idx
# returns "" if $chan is 'global' or undef
# case insensitive about the $chan
sub get_friends_delay {
	my ($idx, $chan) = @_;
	$chan = lc($chan);
	if ($chan && $chan ne "global") {
		foreach my $friendschan (get_friends_channels($idx)) {
			if ($chan eq lc($friendschan)) {
				return undef if ($friends[$idx]->{channels}->{$friendschan}->{delay} eq '');
				return $friends[$idx]->{channels}->{$friendschan}->{delay};
			}
		}
	}
	return;
}

# struct friend new_friend($handle, $hoststr, $globflags, $chanflagstr, $password, $comment)
# hoststr is: *!foo@host1 *!bar@host2 *!?baz@host3
# chanstr is: #chan1,flags,delay #chan2,flags,delay
sub new_friend {
	my $friend = {};
	my $idx = scalar(@friends);
	$friend->{handle} = $_[0];
	$all_handles->{lc($_[0])} = $idx;
	$friend->{globflags} = $_[2];
	$friend->{password} = $_[4];
	$friend->{comment} = $_[5];
	$friend->{friends} = [];

	foreach my $host (split(/ +/, $_[1])) {
		my $regexp_host = userhost_to_regexp($host);
		my ($firstalpha) = $host =~ /\@(.)/;
		$firstalpha = lc($firstalpha);

		$friend->{hosts}->{$host} = $regexp_host;
		$friend->{regexp_hosts}->{$regexp_host} = $host;
		$all_regexp_hosts->{allhosts}->{$regexp_host} = lc($_[0]);
		$all_regexp_hosts->{$firstalpha}->{$regexp_host} = lc($_[0]);
		$all_hosts->{$host} = lc($_[0]);
	}

	foreach my $cfd (split(/ +/, $_[3])) {
		# $cfd format: #foobar,oikl,15 (channelname,flags,delay)
		my ($channel, $flags, $delay) = split(",", $cfd, 3);
		$friend->{channels}->{$channel}->{exist} = 1;
		$friend->{channels}->{$channel}->{flags} = $flags;
		$friend->{channels}->{$channel}->{delay} = $delay;
	}

	return $friend;
}

# get_regexp_hosts_by_letter($letter)
# returns those regexp masks whose host part begins with $letter, '?' or '*'
sub get_regexp_hosts_by_letter($) {
	my $l = lc(substr($_[0], 0, 1));
	my @tmphosts = ();
	push(@tmphosts, keys(%{$all_regexp_hosts->{$l}}));
	push(@tmphosts, keys(%{$all_regexp_hosts->{'?'}}));
	push(@tmphosts, keys(%{$all_regexp_hosts->{'*'}}));
	return @tmphosts;
}

# bool is_allowed_flag($flag)
# will be obsolete, soon.
sub is_allowed_flag { return 1; }

# bool is_ctcp_command($command)
# check if $command is one of the implemented ctcp commands
sub is_ctcp_command {
	my ($command) = @_;
	$command = uc($command);
	foreach my $allowed (split(/[,\ \|]+/, uc(Irssi::settings_get_str('friends_ctcp_commands')))) {
		return 1 if ($command eq $allowed);
	}
	return 0;
}

# int get_idx($nick, $userhost)
# returns idx of the friend or -1 if not a friend
# The New Approach (TM) :)
sub get_idx($$) {
	my ($nick, $userhost) = @_;
	$userhost = lowercase_hostpart($nick.'!'.$userhost);
	my ($letter) = $userhost =~ /\@(.)/;
	my $idx = -1;

	foreach my $regexp_host (get_regexp_hosts_by_letter($letter)) {
		if ($userhost =~ /^$regexp_host$/) {
			return get_idxbyhand($all_regexp_hosts->{allhosts}->{$regexp_host});
		}
	}

	return -1;
}

# int get_idxbyhand($handle)
# returns $idx of friend with $handle or -1 if no such handle
# case insensitive
sub get_idxbyhand($) {
	my $handle = lc($_[0]);
	if (exists $all_handles->{$handle}) {
		return $all_handles->{$handle};
	}
	return -1;
}

# int get_handbyidx($idx)
# returns $handle of friend with $idx or undef if no such $idx
# case sensitive
sub get_handbyidx($) {
	my ($idx) = @_;
	return undef unless ($idx > -1 && $idx < scalar(@friends));
	return $friends[$idx]->{handle};
}

# bool friend_has_host($idx, $host)
# checks wheter $host matches any of $friend[$idx]'s hostmasks
# The New Approach (TM)
sub friend_has_host($$) {
	my ($idx, $host) = @_;
	$host = lowercase_hostpart($host);
	foreach my $regexp_host (keys (%{$friends[$idx]->{regexp_hosts}})) {
		return 1 if ($host =~ /^$regexp_host$/);
	}
	return 0;
}

# void add_host($idx, $host)
# adds $host wherever it's needed
# $friends[$idx]->{handle} is A MUST for add_host() to work properly.
sub add_host($$) {
	my ($idx, $host) = @_;
	my $regexp_host = userhost_to_regexp($host);
	my ($firstalpha) = $host =~ /\@(.)/;
	$firstalpha = lc($firstalpha);

	$friends[$idx]->{hosts}->{$host} = $regexp_host;
	$friends[$idx]->{regexp_hosts}->{$regexp_host} = $host;
	$all_regexp_hosts->{allhosts}->{$regexp_host} = lc($friends[$idx]->{handle});
	$all_regexp_hosts->{$firstalpha}->{$regexp_host} = lc($friends[$idx]->{handle});
	$all_hosts->{$host} = lc($friends[$idx]->{handle});
}

# int del_host($idx, $host)
# deletes $host from wherever it is
# if given $host arg is '*', removes all hosts of this friend
sub del_host($$) {
	my ($idx, $host) = @_;
	my $deleted = 0;

	foreach my $regexp_host (keys (%{$friends[$idx]->{regexp_hosts}})) {
		if ($host eq '*' || $host =~ /^$regexp_host$/) {
			my $plain_host = $friends[$idx]->{regexp_hosts}->{$regexp_host};
			my ($l) = $plain_host =~ /\@(.)/;

			delete $friends[$idx]->{hosts}->{$plain_host};
			delete $friends[$idx]->{regexp_hosts}->{$regexp_host};
			delete $all_regexp_hosts->{allhosts}->{$regexp_host};
			delete $all_regexp_hosts->{$l}->{$regexp_host};
			delete $all_hosts->{$plain_host};
			$deleted++;
		}
	}
	return $deleted;
}

# bool friend_has_chanrec($idx, $chan)
# checks wheter $friend[$idx] has a $chan record
# case insensitive
sub friend_has_chanrec {
	my ($idx, $chan) = @_;
	$chan = lc($chan);
	foreach my $friendschan (get_friends_channels($idx)) {
		return 1 if ($chan eq lc($friendschan));
	}
	return 0;
}

# bool add_chanrec($idx, $chan)
# adds an empty $chan record to $friends[$idx]
# case sensitive
sub add_chanrec {
	my ($idx, $chan) = @_;
	return 0 unless ($idx > -1 && $idx < scalar(@friends));
	$friends[$idx]->{channels}->{$chan}->{exist} = 1;
	return 1;
}

# bool del_chanrec($idx, $chan)
# deletes $chan record from $friends[$idx]
# case *in*sensitive
sub del_chanrec {
	my ($idx, $chan) = @_;
	my $deleted = 0;
	foreach my $friendschan (get_friends_channels($idx)) {
		if (lc($chan) eq lc($friendschan)) {
			delete $friends[$idx]->{channels}->{$friendschan};
			$deleted = 1;
		}
	}
	return $deleted;
}

# arr del_friend($idxs)
# removes friends
# removes all hosts corresponding to this friend
# returns array of removed friends
sub del_friend($) {
	my ($idxlist) = @_;
	my @idxs = split(/ /, $idxlist);
	return -1 unless (scalar(@idxs) > 0);
	my @tmp = ();
	my @result = ();
	my @todelete = ();

	foreach my $idx (@idxs) {
		my $handle = get_handbyidx($idx);
		if (!(!defined $handle || grep(/^\Q$handle\E$/i, @todelete))) {
			push(@todelete, $handle);
			del_host($idx, '*');
		}
	}
	for (my $idx = 0; $idx < @friends; $idx++) {
		if (grep(/^\Q$friends[$idx]->{handle}\E$/i, @todelete)) {
			push(@result, $friends[$idx]);
		} else {
			push(@tmp, $friends[$idx]);
		}
	}
	@friends = @tmp;
	update_allhandles();
	return @result;
}

# void update_all_handles()
# updates $all_handles
sub update_allhandles {
	$all_handles = {};
	for (my $idx = 0; $idx < @friends; $idx++) {
		$all_handles->{lc($friends[$idx]->{handle})} = $idx
	}
}

# bool is_unique_handle($handle)
# checks if the $handle is unique for the whole friendlist
# returns 1 if there's no such $handle
# returns 0 if there is one.
sub is_unique_handle($) {
	return !exists $all_handles->{lc($_[0])};
}

# str choose_handle($proposed)
# tries to choose a handle, closest to the $proposed one
sub choose_handle {
	my ($proposed) = @_;
	my $counter = 0;
	my $handle = $proposed;

	# do this until we have an unique handle
	while (!is_unique_handle($handle)) {
		if (($handle !~ /([0-9]+)$/) && !$counter) {
			# first, if handle doesn't end with a digit, append '2'
			# (but only in first step)
			$handle .= "2";
		} elsif ($counter < 85) {
			# later, increase the trailing number by one
			# do that 84 times
			my ($number) = $handle =~ /([0-9]+)$/;
			++$number;
			$handle =~ s/([0-9]+)$/$number/;
		} elsif ($counter == 85) {
			# then, if it didn't helped, make $handle = $proposed."_"
			$handle = $proposed . "_";
		} elsif ($counter < 90) {
			# if still unsuccessful, append "_" to the handle
			# do that 4 times
			$handle .= "_";
		} else {
			# if THAT didn't help -- make some silly handle
			# and exit the loop
			$handle = $proposed.'_'.(join '', (0..9, 'a'..'z')[rand 36, rand 36, rand 36, rand 36]);
			last;
		}
		++$counter;
	}

	# return our glorious handle ;-)
	return $handle;
}

# bool friend_has_flag($idx, $flag[, $chan])
# returns true if $friends[$idx] has $flag for $chan
# (checks global flags, if $chan is 'global' or undef)
# returns false if hasn't
# case sensitive about the FLAG
# case insensitive about the chan.
sub friend_has_flag {
	my ($idx, $flag, $chan) = @_;
	$chan = "global" unless ($chan ne '');

	return 1 if (get_friends_flags($idx, $chan) =~ /\Q$flag\E/);
	return 0;
}

# bool friend_is_wrapper($idx, $chan, $goodflag, $badflag)
# something to replace friend_is_* subs
# true on: ($channel +$goodflag OR global +$goodflag) AND ($badflag == "" OR NOT $channel +$badflag))
sub friend_is_wrapper($$$$) {
	my ($idx, $chan, $goodflag, $badflag) = @_;
	return 0 unless ($idx > -1);
	if ((friend_has_flag($idx, $goodflag, $chan) ||
		 friend_has_flag($idx, $goodflag, undef)) && 
		($badflag eq "" || !friend_has_flag($idx, $badflag, $chan))) {
		return 1;
	}
	return 0;
}

# bool add_flag($idx, $flag[, $chan])
# adds $flag to $idx's $chan flags
# $chan can be 'global' or undef
# case insensitive about the $chan -- chooses the proper case.
# returns 1 on success
sub add_flag {
	my ($idx, $flag, $chan) = @_;
	$chan = lc($chan);
	if ($chan eq "" || $chan eq "global") {
		$friends[$idx]->{globflags} .= $flag;
		return 1;
	} else {
		foreach my $friendschan (get_friends_channels($idx)) {
			if ($chan eq lc($friendschan)) {
				$friends[$idx]->{channels}->{$friendschan}->{flags} .= $flag;
				return 1;
			}
		}
	}
	return 0;
}

# bool del_flag($idx, $flag[, $chan])
# removes $flag from $idx's $chan flags
# $chan can be 'global' or undef
# case insensitive about the $chan -- chooses the proper case.
sub del_flag {
	my ($idx, $flag, $chan) = @_;
	$chan = lc($chan);
	if ($chan eq "" || $chan eq "global") {
		$friends[$idx]->{globflags} =~ s/\Q$flag\E//g;
		return 1;
	} else {
		foreach my $friendschan (get_friends_channels($idx)) {
			if ($chan eq lc($friendschan)) {
				$friends[$idx]->{channels}->{$friendschan}->{flags} =~ s/\Q$flag\E//i;
				return 1;
			}
		}
	}
	return 0;
}

# bool change_delay($idx, $delay, $chan)
# alters $idx's delay time for $chan
# fails if $chan is 'global' or undef
sub change_delay {
	my ($idx, $delay, $chan) = @_;
	$chan = lc($chan);
	if ($chan && $chan ne "global") {
		foreach my $friendschan (get_friends_channels($idx)) {
			if ($chan eq lc($friendschan)) {
				$friends[$idx]->{channels}->{$friendschan}->{delay} = $delay;
				return 1;
			}
		}
	}
	return 0;
}

# void list_friend($window, $who, @data)
# prints an info line about certain friend.
# $who may be handle or idx
# if you want to improve the look of the script, you should
# change /format friends_*, probably.
sub list_friend {
	my ($win, $who, @data) = @_;
	my $idx = $who;

	$idx = get_idxbyhand($who) unless ($who =~ /^[0-9]+$/);

	return unless ($idx > -1 && $idx < scalar(@friends));

	my $globflags = get_friends_flags($idx, undef);

	$win = Irssi::active_win() unless ($win);

	$win->printformat(MSGLEVEL_CRAP, 'friends_line_head',
		$idx,
		get_handbyidx($idx),
		(($globflags) ? "$globflags" : "[none]"),
		(($friends[$idx]->{password}) ? "yes" : "no"));

	$win->printformat(MSGLEVEL_CRAP, 'friends_line_hosts',
		join(", ", get_friends_hosts($idx, $friends_PLAIN_HOSTS)) );

	foreach my $chan (get_friends_channels($idx)) {
		my $flags = get_friends_flags($idx, $chan);
		my $delay = get_friends_delay($idx, $chan);
		$win->printformat(MSGLEVEL_CRAP, 'friends_line_chan', 
			$chan,
			(($flags) ? "$flags" : "[none]"),
			(defined($delay) ? "$delay" : "random"));
	}

	if ($friends[$idx]->{comment}) {
		$win->printformat(MSGLEVEL_CRAP, 'friends_line_comment', $friends[$idx]->{comment});
	}

	for my $item (@data) {
		my ($ircnet, $nick, $chanstr) = split(" ", $item);
		next unless (defined $ircnet);
		$win->printformat(MSGLEVEL_CRAP, 'friends_line_currentnick', $nick, $ircnet) if ($nick ne '');;
		$win->printformat(MSGLEVEL_CRAP, 'friends_line_channelson', join(", ", split(/,/, $chanstr)), $ircnet) if ($chanstr ne '');
	}
}

# void add_operation($server, "#channel", "op|voice|deop|devoice|kick|kickban", timeout, "nick1", "nick2", ...)
# adds a delayed (or not) operation
sub add_operation {
	my ($server, $channel, $operation, $timeout, @nicks) = @_;

	# my dear queue, don't grow too big, mmkay? ;^)
	my $maxsize = Irssi::settings_get_int('friends_max_queue_size');
	$maxsize = $default_friends_max_queue_size unless ($maxsize > 0);
	return if (@operationQueue >= $maxsize);

	push(@operationQueue,
	{
		server=>$server,		# server object
		left=>$timeout,			# seconds left
		nicks=>[ @nicks ],		# array of nicks
		channel=>$channel,		# channel name
		operation=>$operation	# operation ("op", "voice" and so on)
	});

	$timerHandle = Irssi::timeout_add(1000, 'timer_handler', 0) unless (defined $timerHandle);
}

# void timer_handler()
# handles delay timer
sub timer_handler {
	my @ops = ();

	# splice out expired timeouts. if they are expired, move them to
	# local ops-queue. this allows creating new operations to the queue
	# in the operation. (we're not (yet) doing that)

	for (my $c = 0; $c < @operationQueue;) {
		if ($operationQueue[$c]->{left} <= 0) {
			push(@ops, splice(@operationQueue, $c, 1));
		} else {
			++$c;
		}
	}

	for (my $c = 0; $c < @ops; ++$c) {
		my $op = $ops[$c];
		my $channel = $op->{server}->channel_find($op->{channel});

		# check if $channel is still active (you might've parted)
		if ($channel) {
			my @operationNicks = ();
			foreach my $nickStr (@{$op->{nicks}}) {
				my $nick = $channel->nick_find($nickStr);
				# check if there's still such nick (it might've quit/parted)
				if ($nick) {
					if ($op->{operation} eq "op" && !$nick->{op}) {
						push(@operationNicks, $nick->{nick});
					}
					if ($op->{operation} eq "voice" && !$nick->{voice} &&
						(!$nick->{op} || Irssi::settings_get_bool('friends_voice_opped'))) {
						push(@operationNicks, $nick->{nick});
					}
					if ($op->{operation} eq "deop" && $nick->{op}) {
						push(@operationNicks, $nick->{nick});
					}
					if ($op->{operation} eq "devoice" && $nick->{voice}) {
						push(@operationNicks, $nick->{nick});
					}
					if ($op->{operation} eq "kick") {
						push(@operationNicks, $nick->{nick});
					}
					if ($op->{operation} eq "kickban") {
						push(@operationNicks, $nick->{nick});
					}
				}
			}
			# final stage: issue desired command if we're a chanop
			$channel->command($op->{operation}." ".join(" ", @operationNicks)) if ($channel->{chanop});
		}
	}

	# decrement timeouts.
	for (my $c = 0; $c < @operationQueue; ++$c) {
		--$operationQueue[$c]->{left};
	}

	# if operation queue is empty, remove timer.
	if (!@operationQueue && $timerHandle) {
		Irssi::timeout_remove($timerHandle);
		$timerHandle = undef;
	}
}

# str replace_home($string)
# replaces '~' with current $ENV{HOME}
sub replace_home($) {
	my ($string) = @_;
	my $home = $ENV{HOME};
	return undef unless ($string);
	$string =~ s/^\~/$home/;
	return $string;
}

# void load_friends($inputfile)
# loads friends from file. uses $inputfile if supplied.
# if not, uses friends_file setting. if this setting is empty,
# uses default -- $friends_file
sub load_friends {
	my ($inputfile) = @_;
	my $friendfile = undef;

	if (defined($inputfile)) {
		$friendfile = replace_home($inputfile);
	} else {
		$friendfile = replace_home(Irssi::settings_get_str('friends_file'));
	}

	$friendfile = $default_friends_file unless (defined $friendfile);

	if (-e $friendfile && -r $friendfile) {
		@friends = ();
		$all_hosts = {};
		$all_regexp_hosts = {};
		$all_handles = {};

		local *F;
		open(F, "<$friendfile") or return -1;
		local $/ = "\n";
		while (<F>) {
			my ($handle, $hosts, $globflags, $chanstr, $password, $comment);
			chop;

			# dealing with empty lines
			next if (/^[\w]*$/);

			# dealing with comments
			if (/^\#/) {
				# script version
				if (/^\# version = (.+)/) { $friends_file_version = $1; }
				# timestamp
				if (/^\# written = ([0-9]+)/) { $friends_file_written = $1; }
				next;
			}

			# split by '%'
			my @fields = split("%", $_);
			foreach my $field (@fields) {
				if ($field =~ /^handle=(.*)$/) { $handle = $1; }
				elsif ($field =~ /^hosts=(.*)$/) { $hosts = $1; }
				elsif ($field =~ /^globflags=(.*)$/) { $globflags = $1; }
				elsif ($field =~ /^chanflags=(.*)$/) { $chanstr = $1; }
				elsif ($field =~ /^password=(.*)$/) { $password = $1; }
				elsif ($field =~ /^comment=(.*)$/) { $comment = $1; }
			}

			# handle cannot start with a digit
			# skip friend if it does
			next if ($handle =~ /^[0-9]/);

			# if all fields were processed, and $handle is unique,
			# make a friend and add it to $friends
			if (is_unique_handle($handle)) {
				push(@friends, new_friend($handle, $hosts, $globflags, $chanstr, $password, $comment));
			} else {
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_duplicate', $handle);
			}
		}

		close(F);

		# if everything's ok -- print a message
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_loaded', scalar(@friends), $friendfile);
	} else {
		# whoops, bail out, but do not clear the friendlist.
		Irssi::print("Cannot load $friendfile");
	}
}

# void cmd_loadfriends($data, $server, $channel)
# handles /loadfriends [file]
sub cmd_loadfriends {
	my ($file) = split(/ +/, $_[0]);
	load_friends($file);
}

# void save_friends($auto)
# saving friends to file
sub save_friends {
	my ($auto, $inputfile) = @_;
	local *F;
	my $friendfile = undef;
	my $backup_suffix = Irssi::settings_get_str('friends_backup_suffix');
	$backup_suffix = "." . time if ($backup_suffix eq '');

	if (defined $inputfile) {
		$friendfile = replace_home($inputfile);
	} else {
		$friendfile = replace_home(Irssi::settings_get_str('friends_file'));
	}
	$friendfile = $default_friends_file unless (defined $friendfile);

	my $backupfile = $friendfile . $backup_suffix;
	my $tmpfile = $friendfile . ".tmp" . time;

	# be sane
	my $old_umask = umask(077);

	if (!defined open(F, ">$tmpfile")) {
		Irssi::print("Couldn't open $tmpfile for writing");
		return 0;
	}

	# write script's version and update corresponding variable
	$friends_file_version = $friends_version;
	print(F "# version = $friends_file_version\n");
	# write current unixtime and update corresponding variable
	$friends_file_written = time;
	print(F "# written = $friends_file_written\n");

	# go through all entries
	for (my $idx = 0; $idx < @friends; ++$idx) {
		# get friend's channels, corresponding flags and delay values
		# then put them as c,f,d fields into @chanstr
		my @chanstr = ();
		foreach my $chan (get_friends_channels($idx)) {
			$chan =~ s/\%//g;
			push(@chanstr, $chan.",".(get_friends_flags($idx, $chan)).",".
				(get_friends_delay($idx, $chan)));
		}

		# write the actual line
		print(F join("%",
			"handle=".get_handbyidx($idx),
			"hosts=".(join(" ", get_friends_hosts($idx, $friends_PLAIN_HOSTS))),
			"globflags=".(get_friends_flags($idx, undef)),
			"chanflags=".(join(" ", @chanstr)),
			"password=".$friends[$idx]->{password},
			"comment=".$friends[$idx]->{comment},
			"\n"));
	}
	# done.

	close(F);

	rename($friendfile, $backupfile) if (Irssi::settings_get_bool('friends_backup_friendlist'));
	rename($tmpfile, $friendfile);

	Irssi::printformat(MSGLEVEL_CRAP, 'friends_saved', scalar(@friends), $friendfile) unless ($auto);

	# restore umask
	umask($old_umask);
}

# void cmd_savefriends($data, $server, $channel)
# handles /savefriends [filename]
sub cmd_savefriends {
	my ($file) = split(/ +/, $_[0]);
	eval {
		save_friends(0, $file);
	};
	Irssi::print("Saving friendlist failed: $?") if ($?);
}

# void event_setup_saved($config, $auto)
# calls save_friends to save friendslist while saving irssi's setup
# (if friends_autosave is turned on)
sub event_setup_saved {
	my ($config, $auto) = @_;
	return unless (Irssi::settings_get_bool('friends_autosave'));
	eval {
		save_friends($auto);
	};
	Irssi::print("Saving friendlist failed: $?") if ($?);
}

# void event_setup_reread($config)
# calls load_friends() while setup is re-readed
# (if friends_autosave is turned on)
sub event_setup_reread {
	load_friends() if (Irssi::settings_get_bool('friends_autosave'));
}

# int calculate_delay($idx, $chan)
# calculates delay
sub calculate_delay {
	my ($idx, $chan) = @_;
	my $delay = get_friends_delay($idx, $chan);
	my $min = Irssi::settings_get_int('friends_delay_min');
	my $max = Irssi::settings_get_int('friends_delay_max');

	# lazy man's sanity checks :-P
	$min = $default_delay_min if $min < 0;
	$max = $default_delay_max if $min > $max;
	$max = $max + $min if $min > $max;

	# make a random delay unless we've got a fixed delay time already
	$delay = int(rand ($max - $min)) + $min unless ($delay =~ /^[0-9]+$/);

	return $delay;
}

# void check_friends($server, $channelstr, $options, @nickstocheck)
# checks the given nicklist, channelname and server against the friendlist
sub check_friends {
	my ($server, $channelName, $options, @nicks) = @_;
	my $channel = $server->channel_find($channelName);
	my $delay = 30;
	my %opList = ();
	my %voiceList = ();

	# server and channel -- a must.
	return unless ($server && $channelName);

	# proper !channels support, hopefully
	my $noPrefix = $channelName;
	$noPrefix = '!' . substr($channelName, 6) if ($channelName =~ /^\!/);

	# get settings
	my $voice_opped = Irssi::settings_get_bool('friends_voice_opped');

	# for each nick from the given list
	foreach my $nick (@nicks) {
		# check if $nick is a friend
		if ((my $idx = get_idx($nick->{nick}, $nick->{host})) > -1) {

			# notify about the join if "showjoins" is set
			if ($options =~ /showjoins/) {
				my $globflags = get_friends_flags($idx, undef);
				my $chanflags = get_friends_flags($idx, $noPrefix);

				my $win = $server->window_item_find($channelName);
				$win = Irssi::active_win() unless ($win);
				$win->printformat(MSGLEVEL_CRAP, 'friends_joined',
					$nick->{nick},
					get_handbyidx($idx),
					($globflags) ? $globflags : "[none]",
					$noPrefix,
					($chanflags) ? $chanflags : "[none]");
			}

			# notice1: password doesn't matter in this loop
			# notice2: channel flags take precedence over the global ones

			# handle auto-(op|voice)
			if (friend_is_wrapper($idx, $noPrefix, "a", undef)) {
				# add $nick to opList{delay} if he is a valid op
				# and isn't opped already
				# 'valid op' means: (chanflag +o OR globflag +o) AND NOT chanflag +d
				if (friend_is_wrapper($idx, $noPrefix, "o", "d") && !$nick->{op}) {
					# calculate delay, add to $opList{$delay}
					$delay = calculate_delay($idx, $noPrefix);
					$opList{$delay}->{$nick->{nick}} = 1;
				}
				# add $nick to voiceList{delay} if he is a valid voice
				# and isn't voiced already
				if (friend_is_wrapper($idx, $noPrefix, "v", undef) && !$nick->{voice} &&
					(!$nick->{op} || $voice_opped)) {
					# calculate delay, add to $voiceList{$delay}
					$delay = calculate_delay($idx, $noPrefix);
					$voiceList{$delay}->{$nick->{nick}} = 1;
				}
			}
		}
	}

	# opping
	foreach my $delay (keys %opList) {
		add_operation($server, $channelName, "op", $delay, keys %{$opList{$delay}});
	}
	# voicing
	foreach my $delay (keys %voiceList) {
		add_operation($server, $channelName, "voice", $delay, keys %{$voiceList{$delay}});
	}

	timer_handler();
}

# void event_kick($server, $data, $nick)
# handles kicks (for revenging)
sub event_kick {
	my ($server, $data, $kicker) = @_;
	my ($channel, $kicked, $reason) = $data =~ /^([^ ]+) ([^ ]+) :(.*)$/;
	my $channelInfo = $server->channel_find($channel);
	my $myNick = $server->{nick};
	my $victimInfo = undef;
	my $kickerInfo = undef;
	my $victimIdx = -1;
	my $kickerIdx = -1;
	my $noPrefix = $channel;
	$noPrefix = '!' . substr($channel, 6) if ($channel =~ /^\!/);

	return unless ($channelInfo);

	# don't bother checking our own kicks, or self-kicks
	return if ($kicker eq $myNick || $kicker eq $kicked);

	$victimInfo = $channelInfo->nick_find($kicked);
	$kickerInfo = $channelInfo->nick_find($kicker);
	# we'll need both
	return unless ($victimInfo && $kickerInfo);

	$victimIdx = get_idx($victimInfo->{nick}, $victimInfo->{host});
	$kickerIdx = get_idx($kickerInfo->{nick}, $kickerInfo->{host});

	# check if we know the victim, and it wasn't a master who deopped
	if ($victimIdx > -1 && !friend_is_wrapper($kickerIdx, $noPrefix, "m", undef)) {
		# RRRRREVENGE!
		my $revengemode = Irssi::settings_get_int('friends_revenge_mode');
		if (Irssi::settings_get_bool('friends_revenge') && ($revengemode > -1 && $revengemode < 4) &&
		    friend_is_wrapper($victimIdx, $noPrefix, "p", undef)) {
			# 0 Deop the user.
			add_operation($server, $channel, "deop", 1, $kicker);
			if ($revengemode > 0) {
				# 1 Deop the user and give them the +D flag for the channel.
				if ($kickerIdx < 0) {
					push(@friends, new_friend(
						choose_handle("bad1"),		# handle
						"*!".$kickerInfo->{host}, 	# hostmask
						undef,				# globflags
						$noPrefix.",D,",		# channel,chanflags,chandelay
						undef,				# password
						"Kicked ".get_handbyidx($victimIdx)." off $noPrefix on $server->{tag}"));
				} else {
					friends_chflags($kickerIdx, "+D", $noPrefix);
				}
				if ($revengemode > 1 && $channelInfo->{chanop}) {
					# 2 Deop the user, give them the +D flag for the channel, and kick them.
					$channelInfo->command("KICK ". $channel . " ".$kicker. " Don't mess with my friends[.pl]");
					if ($revengemode > 2) {
						# 3 Deop the user, give them the +D flag for the channel, kick, and ban them.
						$channelInfo->command("MODE ". $channel ." +b *!".$kickerInfo->{host});
					}
				}
			}
		}
	}
}

# void event_modechange($server, $data, $nick)
# handles modechanges and learning
sub event_modechange {
	my ($server, $data, $nick) = @_;
	my ($channel, $modeStr, $nickStr) = $data =~ /^([^ ]+) ([^ ]+) (.*)$/;
	my @modeargs = split(" ", $nickStr);
	my $ptr = 0;
	my $mode = undef;
	my $gotOpped = 0;
	my $learnFriends = Irssi::settings_get_bool('friends_learn');
	my $opperInfo = undef;
	my $opperIdx = -1;
	my $learnFromOpper = 0;
	my $channelInfo = $server->channel_find($channel);
	my $myNick = $server->{nick};
	# !channels support :)
	my $noPrefix = $channel;
	$noPrefix = '!' . substr($channel, 6) if ($channel =~ /^\!/);

	# don't bother checking our own modes
	return if ($nick eq $myNick);

	# we need $channelInfo to do almost every other things;
	return unless (defined $channelInfo);

	$opperInfo = $channelInfo->nick_find($nick);
	$opperIdx = get_idx($opperInfo->{nick}, $opperInfo->{host}) if ($opperInfo);

	# learn if learning is enabled, 
	# we know the opper, and we're allowed to learn from him
	if ($learnFriends && $opperIdx > -1 &&
	    (friend_is_wrapper($opperIdx, $noPrefix, "F", undef))) {
		$learnFromOpper = 1;
	}

	# process the mode string
	foreach my $char (split(//, $modeStr)) {

		if ($char eq "+") { $mode = "+";
		} elsif ($char eq "-") { $mode = "-";

		# op/deop, it wasn't a self-op/deop
		} elsif (lc($char) eq "o" && ($nick ne $modeargs[$ptr])) {
			my $victim = $channelInfo->nick_find($modeargs[$ptr]);
			my $victimIdx = -1;
			$victimIdx = get_idx($victim->{nick}, $victim->{host}) if ($victim);

			# someone +o foobar
			if ($mode eq "+") {
				# hooray, i got opped!
				if ($modeargs[$ptr] eq $myNick) {
					$gotOpped = 1;
				# should learn?
				} elsif ($learnFromOpper && $victim) {
					# handle the learning stuff.
					my $friend;

					if ($victimIdx == -1) {
						# we got someone not known before
						# choose a handle for him and add him to our friendlist with +L $noPrefix
						$friend = new_friend(
							choose_handle($modeargs[$ptr]),		# handle
							"*!".$victim->{host}, 			# hostmask
							undef,					# globflags
							$noPrefix.",L,",			# channel,chanflags,chandelay
							undef,					# password
							"Learnt (opped by $friends[$opperIdx]->{handle} on $noPrefix\@$server->{tag})"	# comment
						);
						push(@friends, $friend);
					} else {
						# we know him already
						$friend = $friends[$victimIdx];
					}

					if ($victimIdx == -1 || get_friends_flags($victimIdx, $noPrefix) eq "L") {
						# add him to the opper's friendlist
						# ($opperIdx != -1, we've checked that with $learnFromOpper earlier)
						push(@{$friends[$opperIdx]->{friends}}, $friend);
					}

				} elsif (friend_is_wrapper($victimIdx, $noPrefix, "D", undef) && !friend_is_wrapper($opperIdx, $noPrefix, "m", undef)) {
					add_operation($server, $channel, "deop", 1, $modeargs[$ptr]);
				}

			# deop
			} elsif ($mode eq "-") {
				if ($victim) {
					# check if we know the victim, and it wasn't a master who deopped
					if ($victimIdx > -1 && !friend_is_wrapper($opperIdx, $noPrefix, "m", undef)) {
						# RRRRREVENGE!
						my $revengemode = Irssi::settings_get_int('friends_revenge_mode');
						if (Irssi::settings_get_bool('friends_revenge') && ($revengemode > -1 && $revengemode < 4) &&
						    friend_is_wrapper($victimIdx, $noPrefix, "p", undef)) {
							# 0 Deop the user.
							add_operation($server, $channel, "deop", 1, $nick);
							if ($revengemode > 0 && $opperInfo) {
								# 1 Deop the user and give them the +D flag for the channel.
								if ($opperIdx < 0) {
									push(@friends, new_friend(
										choose_handle("bad1"),		# handle
										"*!".$opperInfo->{host}, 	# hostmask
										undef,				# globflags
										$noPrefix.",D,",		# channel,chanflags,chandelay
										undef,				# password
										"Deopped ".get_handbyidx($victimIdx)." on $noPrefix\@$server->{tag}"));
								} else {
									friends_chflags($opperIdx, "+D", $noPrefix);
								}

								if ($revengemode > 1 && $channelInfo->{chanop}) {
									# 2 Deop the user, give them the +D flag for the channel, and kick them.
									$channelInfo->command("KICK ". $channel . " ".$opperInfo->{nick}. " Don't mess with my friends[.pl]");
									if ($revengemode > 2) {
										# 3 Deop the user, give them the +D flag for the channel, kick, and ban them.
										$channelInfo->command("MODE ". $channel ." +b *!".$opperInfo->{host});
									}
								}
							}
						}
						# if a +r'ed person was deopped, perform a reop
						if (friend_is_wrapper($victimIdx, $noPrefix, "r", "d")) {
							add_operation($server, $channel, "op", calculate_delay($victimIdx, $channel), $modeargs[$ptr])
						}
					}
				}
			}
			# increase pointer, 'o' mode has argument, *always*
			$ptr++;
		} elsif ($char =~ /[beIqdhvk]/ || ($char eq "l" && $mode eq "+")) {
			# increase pointer, these modes have arguments as well
			$ptr++;
		}
	}

	if ($gotOpped) {
		# calling check_friends with !BLARHchannel, since removing BLARH is done there
		check_friends($server, $channel, undef, $channelInfo->nicks());
	}
}

# void event_massjoin($channel, $nicklist)
# handles join event
sub event_massjoin {
	my ($channel, $nicksList) = @_;
	my @nicks = @{$nicksList};
	my $server = $channel->{'server'};
	my $channelName = $channel->{name};
	my $options;
	$options = "showjoins|" if Irssi::settings_get_bool("friends_show_flags_on_join");

	my $begin = time;

	check_friends($server, $channelName, $options, @nicks);

	if ((my $duration = time - $begin) >= 1) {
		# if checking took more than 1 second -- print a message about it
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_checking', $channelName, $duration, $server->{address});
	}
}

# void event_nicklist_changed($channel, $nick, $oldnick)
# some kind of nick-tracking
# alters operationQueue if someone from there has changed nick
sub event_nicklist_changed {
	my ($channel, $nick, $oldnick) = @_;

	# nicknames are case insensitive
	return if (lc($oldnick) eq lc($nick->{nick}));

	# cycle through all operation queues
	for (my $c = 0; $c < @operationQueue; ++$c) {
		# temporary array
		my @nickarr = ();
		# is there any nick in this queue that needs altering?
		my $found = 0;

		# skip if tags don't match
		next unless ($operationQueue[$c]->{server}->{tag} eq $channel->{server}->{tag});

		# cycle through all nicks in single operation queue
		foreach my $opnick (@{$operationQueue[$c]->{nicks}}) {
			# if $oldnick was in the queue
			if (lc($oldnick) eq lc($opnick)) {
				# ... replace it with the new one
				push(@nickarr, $nick->{nick});
				$found = 1;
			} else {
				# ... else -- keep the old one
				push(@nickarr, $opnick);
			}
		}

		# replace $opQ[$c]->{nicks} with our new nicklist if any nick needed updating
		$operationQueue[$c]->{nicks} = [ @nickarr ] if ($found);
	}
}

# void event_server_disconnected($server, $anything)
# removes all queues related to $server from @operationQueue
sub event_server_disconnected {
	my ($server, $anything) = @_;
	my @removed = ();

	# cycle through all operation queues
	for (my $c = 0; $c < @operationQueue;) {
		if ($operationQueue[$c]->{server}->{tag} eq $server->{tag}) {
			push(@removed, splice(@operationQueue, $c, 1));
		} else {
			++$c;
		}
	}

	# if operation queue is empty, remove the timer.
	if (scalar(@removed) && !@operationQueue && $timerHandle) {
		Irssi::timeout_remove($timerHandle);
		$timerHandle = undef;
	}
}

# void cmd_opfriends($data, $server, $channel)
# handles /opfriends #channel
sub cmd_opfriends {
	my ($data, $server, $channel) = @_;
	my ($chan) = split(/ +/, $data);
	my $usage = "/OPFRIENDS [channel]";
	my @chanstocheck = ();

	if (!$server) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "No server item in current window");
		return;
	}

	# no argument given
	if ($chan eq "") {
		if (!$channel) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "No usable channel item in current window");
			return;
		} elsif ($channel->{type} ne "CHANNEL") {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "Current window item is not a channel");
			return;
		} else {
			push(@chanstocheck, $channel->{name});
		}
	# all channels on current server
	} elsif ($chan eq "*") {
		foreach my $c ($server->channels()) {
			push(@chanstocheck, $c->{name});
		}
	# specified channel on current server
	} else {
		push(@chanstocheck, $chan);
	}

	foreach my $channelName (@chanstocheck) {
		my $chanInfo = $server->channel_find($channelName);
		if (!$chanInfo) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_notonchan', $channelName);
			next;
		}

		# !channels support
		my $noPrefix = $chanInfo->{name};
		$noPrefix = '!' . substr($chanInfo->{name}, 6) if ($chanInfo->{name} =~ /^\!/);

		my @opnicks = ();
		foreach my $nick ($chanInfo->nicks()) {
			# skip already opped nicks
			next if ($nick->{op});
			# check for friends
			my $idx = get_idx($nick->{nick}, $nick->{host});
			# skip not-friends
			next unless ($idx > -1);
			# add $nick's nick to oplist if enough flags for this channel
			push(@opnicks, $nick->{nick}) if (friend_is_wrapper($idx, $noPrefix, "o", "d"));
		}

		# add stuff to the operation queue
		add_operation($server, $noPrefix, "op", "0", @opnicks);
	}

	timer_handler();
}

# void cmd_queue($data, $server, $channel)
# expands to queue show|purge|flush
sub cmd_queue($$$) {
	my ($data, $server, $channel) = @_;
	Irssi::command_runsub("queue", $data, $server, $channel);
}

# bool queue_flush_expand(%what)
# "... and few lines of The Magic Code. Now. Your poison is ready."
sub queue_flush_expand {
	my ($flush) = @_;
	my $result = 0;

	foreach my $s (keys(%{$flush})) {
		# is this server active?
		my $server = Irssi::server_find_tag($s);
		next unless (defined $server);

		foreach my $c (keys(%{$flush->{$s}})) {
			# is this channel active?
			my $channel = $server->channel_find($c);
			next unless (defined $channel);

			# for each pending operation
			foreach my $o (sort keys(%{$flush->{$s}->{$c}})) {
				my @nicklist = ();
				foreach my $nickStr (sort keys(%{$flush->{$s}->{$c}->{$o}})) {
					# is this nick still here?
					if (my $nick = $channel->nick_find($nickStr)) {
						push(@nicklist, $nick->{nick});
					}
				}

				if (my $nickstr = join(" ", @nicklist)) {
					$channel->command($o." ".$nickstr);
					$result = 1;
				}
			}
		}
	}
	return $result;
}

# void queue_show($data, $server, $channel)
# handles /QUEUE SHOW
# prints @operationQueue's contents
sub cmd_queue_show {
	if (!@operationQueue) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_empty');
		return;
	}

	# cycle through all operation queues
	for (my $c = 0; $c < @operationQueue; ++$c) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_line1', 
			$c,
			$operationQueue[$c]->{left},
			$operationQueue[$c]->{operation}
		);
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_line2', 
			$operationQueue[$c]->{server}->{address},
			$operationQueue[$c]->{channel},
			join(", ", @{$operationQueue[$c]->{nicks}})
		);
	}
}

# void cmd_queue_flush($data, $server, $channel)
# handles /QUEUE FLUSH <number|all>
# flushes given/all queue(s)
sub cmd_queue_flush {
	my ($data) = split(/ +/, $_[0]);
	my $usage = "/QUEUE FLUSH <number|all>";
	my @flushqueue = ();
	my $flushdata = {};
	my @removed = ();

	if (!@operationQueue) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_empty');
		return;
	}

	if ($data eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	if ($data =~ /^all/i) {
		@flushqueue = @operationQueue;
		@operationQueue = ();
		push(@removed, $data);
	} elsif ($data =~ /^[0-9,]+$/) {
		my $numstr = join(" ", split(/,/, $data));
		for (my $num = 0; $num < @operationQueue;) {
			if ($numstr =~ /\b$num\b/) {
				push(@flushqueue, splice(@operationQueue, $num, 1));
				push(@removed, $num);
			} else {
				$num++
			}
		}
	} else {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badargs', $usage);
		return;
	}

	if (@flushqueue) {
		# don't ask... ;^)
		foreach my $q (@flushqueue) {
			my $s = $q->{server}->{tag};
			my $c = $q->{channel};
			my $o = $q->{operation};
			foreach my $n (@{$q->{nicks}}) {
				$flushdata->{$s}->{$c}->{$o}->{$n} = 1 unless ($o eq "voice" && 
					exists $flushdata->{$s}->{$c}->{op}->{$n} && 
					!Irssi::settings_get_bool('friends_voice_opped'));
			}
		}
		my $result = ((queue_flush_expand($flushdata)) ? "seems ok" : "looks like nothing done");
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_removed', "Flushed", join(", ", @removed), $result);
	}

	if (!@operationQueue && $timerHandle) {
		Irssi::timeout_remove($timerHandle);
		$timerHandle = undef;
	}
}

# void cmd_queue_purge($data, $server, $channel)
# handles /QUEUE PURGE <number|all>
# removes given/all queue(s)
sub cmd_queue_purge {
	my ($data) = split(/ +/, $_[0]);
	my $usage = "/QUEUE PURGE <number|all>";
	my $result;
	my @removed;

	if (!@operationQueue) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_empty');
		return;
	}

	if ($data eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	if ($data =~ /^all/i) {
		@operationQueue = ();
		$result = "OK";
		push(@removed, $data);
	} elsif ($data =~ /^[0-9,]+$/) {
		my $numstr = join(" ", split(/,/, $data));
		for (my $num = 0; $num < @operationQueue;) {
			if ($numstr =~ /\b$num\b/) {
				splice(@operationQueue, $num, 1);
				push(@removed, $num);
				$result = "OK";
			} else {
				$num++
			}
		}
	} else {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badargs', $usage);
		return;
	}

	Irssi::printformat(MSGLEVEL_CRAP, 'friends_queue_removed', "Purged", join(", ", @removed), $result) if (defined $result);

	if (!@operationQueue && $timerHandle) {
		Irssi::timeout_remove($timerHandle);
		$timerHandle = undef;
	}
}

# void friends_chflags($idx, $string[, $chan])
# parses the $string and calls add_flag() or del_flag()
sub friends_chflags {
	my ($idx, $string, $chan) = @_;
	my $mode = undef;
	my $char;

	$chan = "global" if ($chan eq "" || lc($chan) eq "global");

	foreach my $char (split(//, $string)) {
		if ($char eq "+") { $mode = "+";
		} elsif ($char eq "-") { $mode = "-";
		} elsif ($mode) {
			if ($mode eq "+") {
				# ADDING flags
				# add chan record, if needed
				add_chanrec($idx, $chan) if ($chan ne "global" && !friend_has_chanrec($idx, $chan));
				if (!friend_has_flag($idx, $char, $chan)) {
					# add this flag if he doesn't have it yet
					add_flag($idx, $char, $chan);
				}
			} elsif ($mode eq "-") {
				# REMOVING flags
				if ($chan eq "global" || friend_has_chanrec($idx, $chan)) {
					del_flag($idx, $char, $chan);
				}
			}
		}
	}
}

# void cmd_chflags($data, $server, $channel)
# handles /chflags <handle> <+-flags> [#channel]
sub cmd_chflags {
	my ($handle, $flags, @chans) = split(/ +/, $_[0]);
	my $usage = "/CHFLAGS <handle> <+/-flags> [#channel1] [#channel2] ...";

	# strip %'s
	$handle =~ s/\%//g;

	# not enough args
	if ($handle eq "" || $flags eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# bad args
	# if the 'flags' part doesn't start with + or -
	if ($flags !~ /^[\+\-]/) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badargs', $usage);
		return;
	}

	# get idx, yell and return if it isn't valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	# if #channel wasn't specified -- we'll deal with global flags
	push(@chans, "global") unless (@chans);

	# go through all channels specified
	foreach my $chan (@chans) {
		# strip %'s
		$chan =~ s/\%//g;

		# 'executing +foo-bar for someone (where)'
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_chflagexec', $flags, get_handbyidx($idx), $chan);
		# make changes
		friends_chflags($idx, $flags, $chan);

		my $flagstr = get_friends_flags($idx, $chan);
		# 'current $chan flags for someone are: +blah/[none]'
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_currentflags', (($flagstr) ? $flagstr : "[none]"), get_handbyidx($idx), $chan);
	}
}

# void cmd_chhandle($data, $server, $channel)
# handles /chhandle <oldhandle> <newhandle>
sub cmd_chhandle {
	my ($oldhandle, $newhandle) = split(/ +/, $_[0]);
	my $usage = "/CHHANDLE <oldhandle> <newhandle>";

	# strip %'s
	$newhandle =~ s/\%//g;

	# not enough args
	if ($oldhandle eq "" || $newhandle eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($oldhandle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $oldhandle);
		return;
	}

	# proper case for later printformat
	$oldhandle = get_handbyidx($idx);

	# handle cannot start with a digit
	if ($newhandle =~ /^[0-9]/) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badhandle', $newhandle, 
			"Handle may not start with a digit");
		return;
	}

	if (lc($newhandle) eq lc($oldhandle)) {
		# funny case, only changes case of letters, omit the whole change_handle()
		$friends[$idx]->{handle} = $newhandle;
	} else {
		# check if $newhandle is unique
		# if not, print appropriate message and return
		if (!is_unique_handle($newhandle)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_notuniqhandle', $newhandle);
			return;
		}
		# ok, everything seems fine now, let's change the handle.
		change_handle($oldhandle, $newhandle);
	}

	# ... and print a message
	Irssi::printformat(MSGLEVEL_CRAP, 'friends_changed_handle', $oldhandle, $newhandle);
}

# void change_handle($oldhandle, $newhandle)
# changes handle in appropriate structures
sub change_handle($$) {
	my ($old, $new) = @_;
	my $idx = get_idxbyhand($old);
	my $lc_new = lc($new);
	foreach my $host (get_friends_hosts($idx, $friends_PLAIN_HOSTS)) {
		my ($l) = $host =~ /\@(.)/;
		my $regexp_host = userhost_to_regexp($host);
		$all_regexp_hosts->{allhosts}->{$regexp_host} = $lc_new;
		$all_regexp_hosts->{lc($l)}->{$regexp_host} = $lc_new;
		$all_hosts->{$host} = $lc_new;
		delete $all_handles->{lc($old)};
		$all_handles->{$lc_new} = $idx;
		$friends[$idx]->{handle} = $new;
	}
}

# void cmd_chpass($data, $server, $channel)
# handles /chpass <handle> [pass]
# if pass is empty, removes password
# otherwise, crypts it and sets as current one
sub cmd_chpass {
	my ($handle, $pass) = split(/ +/, $_[0]);
	my $usage = "/CHPASS <handle> [newpassword]";

	# not enough args
	if ($handle eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	# crypt and set password. then print a message
	$friends[$idx]->{password} = friends_crypt("$pass");
	Irssi::printformat(MSGLEVEL_CRAP, 'friends_chpassexec', get_handbyidx($idx));
}

# void cmd_chdelay($data, $server, $channel)
# handles /chdelay <handle> <delay> <#channel>
# use delay=0 to get instant opping
# use delay>0 to get fixed opping delay
# use delay='random' or delay='none' or delay = 'remove'
#  to remove fixed delay (make it random)
sub cmd_chdelay {
	my ($handle, $delay, $chan) = split(/ +/, $_[0]);
	my $usage = "/CHDELAY <handle> <delay> <#channel>";
	my $value = undef;

	# strip %'s
	$chan =~ s/\%//g;

	# not enough args
	if ($handle eq "" || $delay eq "" || $chan eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# if $chan doesn't start with one of the [!&#+]
	if ($chan !~ /^[\!\&\#\+]/) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badargs', $usage);
		return;
	}

	# check validness of $delay
	if ($delay =~ /^[0-9]+$/) {
		# numeric value
		$value = $delay;
	} elsif ($delay =~ /^(remove|random|none)$/i) {
		# 'remove', 'random' or 'none'
		$value = undef;
	} else {
		# badargs, return
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	# check if $idx has got $chan record.
	# add one if needed
	add_chanrec($idx, $chan) unless (friend_has_chanrec($idx, $chan));

	# finally, set it, and print a message
	change_delay($idx, $value, $chan);
	Irssi::printformat(MSGLEVEL_CRAP, 'friends_changed_delay', get_handbyidx($idx),
		$chan, (defined($value) ? $value : "[random]"));
}

# void cmd_comment($data, $server, $channel)
# handles /comment <handle> [comment]
# if comment is empty, removes it
# otherwise, sets it as the current one
sub cmd_comment {
	my ($handle, $comment) = split(" ", $_[0], 2);
	my $usage = "/COMMENT <handle> [comment]";

	# not enough args
	if ($handle eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	# remove %'s and trailing spaces (just-in-case ;)
	$comment =~ s/\%//g;
	$comment =~ s/[\ ]+$//;

	# finally, set it, and print a message
	$friends[$idx]->{comment} = $comment;

	if ($comment ne '') {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_comment_added', get_handbyidx($idx), $comment);
	} else {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_comment_removed', get_handbyidx($idx));
	}
}

# void cmd_listfriend($data, $server, $chanel)
# handles /listfriends [what]
# 'what' can be either handle, channel name, 1,2,5,15-style, host mask or empty.
sub cmd_listfriends {
	if (@friends == 0) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_empty');
	} else {
		my ($data) = @_;
		my $counter = 0;
		# remove whitespaces
		$data =~ s/[\t\ ]+//g;
		my $win = Irssi::active_win();

		if ($data =~ /^[\!\&\#\+]/) {
			# deal with channel
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_friendlist', "channel " . $data);
			for (my $idx = 0; $idx < @friends; ++$idx) {
				if (friend_has_chanrec($idx, $data)) {
					list_friend($win, $idx, undef);
					$counter++;
				}
			}
		} elsif ($data =~ /^[0-9,]+$/) {
			# deal with 1,2,5,15 style
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_friendlist', $data);
			foreach my $idx (split(/,/, $data)) {
				if ($idx < @friends) {
					list_friend($win, $idx, undef);
					$counter++;
				}
			}
		} elsif ($data =~ /^.*\!.*\@.*$/) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_friendlist', "matching " . $data);
			# /* FIXME */
			my $regexp_data = userhost_to_regexp($data);
			for (my $idx = 0; $idx < @friends; ++$idx) {
				foreach my $regexp_host (get_friends_hosts($idx, $friends_REGEXP_HOSTS)) {
					if ($data =~ /^$regexp_host$/ || $friends[$idx]->{regexp_hosts}->{$regexp_host} =~ /^$regexp_data$/) {
						list_friend($win, $idx, undef);
						$counter++;
						last;
					}
				}
			}
		} elsif ($data ne "") {
			if ((my $idx = get_idxbyhand($data)) > -1) {
				# deal with handle
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_friendlist', $data);
				list_friend($win, $idx, undef);
				$counter++;
			} else {
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $data);
			}
		} else {
			# deal with every entry
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_friendlist', "all");
			for (my $idx = 0; $idx < @friends; ++$idx) {
				list_friend($win, $idx, undef);
				$counter++;
			}
		}
		if ($counter) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_friendlist_count', $counter, (($counter > 1) ? "s" : ""));
		}
	}
}

# void cmd_addfriend($data, $server, $channel)
# handles /addfriend <handle> <hostmask> [flags]
# if 'flags' is empty, uses friends_default_flags instead
sub cmd_addfriend {
	my ($handle, $host, $flags) = split(/ +/, $_[0]);
	my $server = $_[1];
	my $usage = "/ADDFRIEND <handle|nick> [<hostmask> [flags]]";

	# strip %'s
	$handle =~ s/\%//g;
	$host =~ s/\%//g;

	# not enough args
	if ($handle eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# handle cannot start with a digit
	if ($handle =~ /^[0-9]/) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_badhandle', $handle, "Handle may not start with a digit");
		return;
	}

	# assume we want /addfriend somenick
	if ($host eq "") {
		# no server item in current window
		if (!$server) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "No server item in current window");
			return;
		}

		# redirect userhost reply to event_isfriend_userhost()
		# caution: This works only with Irssi 0.7.98.CVS (20011117) and newer
		$server->redirect_event("userhost", 1, $handle, 0, undef, {
					"event 302" => "redir userhost_addfriend"});
		# send our query
		$server->send_raw("USERHOST :$handle");
		return;
	}

	# check must be unique
	if (!is_unique_handle($handle)) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notuniqhandle', $handle);
		return;
	}

	# add friend.
	push(@friends, new_friend($handle, $host, undef, undef, undef, undef));
	Irssi::printformat(MSGLEVEL_CRAP, 'friends_added', $handle);

	# check 'flags' parameter, add default flags if empty.
	$flags = Irssi::settings_get_str('friends_default_flags') unless ($flags);

	# add flags and print them if needed
	if ($flags) {
		# check if $flags start with a '+'. if not, prepend one.
		$flags = "+".$flags unless ($flags =~ /^\+/);

		# our new friend should have $idx=(scalar(@friends)-1) now, so we'll use it.
		my $idx = scalar(@friends) - 1;

		friends_chflags($idx, $flags, "global");
		$flags = get_friends_flags($idx, undef);
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_currentflags', $flags, $handle, "global") if ($flags);
	}
}

# void event_addfriend_userhost($server, $reply, $servername)
# handles redirected USERHOST replies
# (part of /addfriend)
sub event_addfriend_userhost {
	my ($mynick, $reply) = split(/ +/, $_[1]);
	my $server = $_[0];
	my ($nick, $user, $host) = $reply =~ /^:?([^\*=]*)\*?=.(.*)@(.*)/;
	my $string = $nick . '!' . $user . '@' . $host;
	my $friend_matched = 0;

	# try matching ONLY if the response is positive
	if (defined $nick && defined $user && defined $host) {
		if ((my $idx = get_idx($nick, $user.'@'.$host)) > -1) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_already_added', $nick, get_handbyidx($idx));
			return;
		}
		# handle
		my $handle = choose_handle($nick);
		# *~^=-ident
		$user =~ s/^[\~\+\-\^\=]+/\*/;

		# add friend.
		push(@friends, new_friend($handle, '*!'.$user.'@'.$host, undef, undef, undef, undef));
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_added', $handle);
		return;
	}

	# failed
	Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "No such nick");
}

# void cmd_delfriend($data, $server, $channel)
# handles /delfriend <handle|number>
# supports /delfriend 2-5,foohand,1,4,10,11-22
sub cmd_delfriend {
	my ($who) = split(/ +/, $_[0]);
	my $usage = "/DELFRIEND <handle|number>";

	# strip %'s
	$who =~ s/\%//g;

	# not enough args
	if ($who eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	my @todelete = ();
	foreach my $what (split(/[\ ,]/, $who)) {
		if ($what =~ /^[0-9]+$/) {
			# /delfriend 15
			next unless ($what > -1 && $what < scalar(@friends));
			push(@todelete, $what) unless (grep(/^$what$/, @todelete));
		} elsif ($what =~ /^([0-9]+)\-([0-9]+)$/) {
			# /delfriend 2-10
			my ($start, $end) = $what =~ /([0-9]+)\-([0-9]+)/;
			next if ($start > $end);
			for my $i ($start .. $end) {
				next unless ($i > -1 && $i < scalar(@friends));
				push(@todelete, $i) unless (grep(/^$i$/, @todelete));
			}
		} else {
			# /delfriend foobar
			my $delidx = get_idxbyhand($what);
			push(@todelete, $delidx) unless ($delidx < 0 || grep(/^$delidx$/, @todelete));
		}
	}
	@todelete = sort {$a <=> $b} @todelete;

	return unless (@todelete);

	my @result = del_friend(join(" ", @todelete));
	foreach my $deleted (@result) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_removed', $deleted->{handle});
	}
}

# void cmd_addhost($data, $server, $channel)
# handles /addhost <handle> <hostmask1> [hostmask2] ...
# hostmask may not overlap with any of the current ones
sub cmd_addhost {
	my ($handle, @hosts) = split(/ +/, $_[0]);
	my $usage = "/ADDHOST <handle> <hostmask1> [hostmask2] [hostmask3] ...";

	# not enough args
	if ($handle eq "" || !@hosts) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	for (my $i = 0; $i < scalar(@hosts); $i++) {
		my $data = $hosts[$i];
		$data =~ s/\%//g;
		my $regexp_data = userhost_to_regexp($data);
		my $found = 0;
		my $who = "";

		# /* FIXME */
		foreach my $plain_host (keys %{$all_hosts}) {
			if (!$found && $plain_host =~ /^$regexp_data$/) {
				$found = 1;
				$who = get_handbyidx(get_idxbyhand($all_hosts->{$plain_host}));
				last;
			}
		}

		# /* FIXME again */
		foreach my $regexp_host (get_friends_hosts($idx, $friends_REGEXP_HOSTS)) {
			last if ($found);
			if ($data =~ /^$regexp_host$/ || $friends[$idx]->{regexp_hosts}->{$regexp_host} =~ /^$regexp_data$/) {
				$found = 1;
				$who = get_handbyidx($idx);
				last;
			}
		}

		if (!$found) {
			add_host($idx, $data);
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_host_added', get_handbyidx($idx), $data);
		} else {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_host_exists', $who, $data);
		}
	}
}

# void cmd_delhost($data, $server, $channel)
# handles /delhost <handle> <hostmask>
# hostmask should be EXACTLY the same as one in $friends[$idx]->{hosts}
sub cmd_delhost {
	my ($handle, $host) = split(/ +/, $_[0]);
	my $usage = "/DELHOST <handle> <hostmask>";

	# strip %'s
	$host =~ s/\%//g;

	# not enough args
	if ($handle eq "" || $host eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	# delete host, print appropriate message
	if (del_host($idx, $host)) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_host_removed', get_handbyidx($idx), $host);
	} else {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_host_notexists', get_handbyidx($idx), $host);
	}
}

# void cmd_delchanrec($data, $server, $channel)
# handles /delchanrec <handle> <#channel>
sub cmd_delchanrec {
	my ($handle, $chan) = split(/ +/, $_[0]);
	my $usage = "/DELCHANREC <handle> <#channel>";

	# strip %'s
	$chan =~ s/\%//g;

	# not enough args
	if ($handle eq "" || $chan eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# get idx, yell and return if it's not valid
	my $idx = get_idxbyhand($handle);
	if ($idx == -1) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $handle);
		return;
	}

	# delete chanrec, print appropriate message
	if (del_chanrec($idx, $chan)) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_chanrec_removed', get_handbyidx($idx), $chan);
	} else {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_chanrec_notexists', get_handbyidx($idx), $chan);
	}
}

# void cmd_findfriends($data, $server, $channel)
# handles /findfriends [handle]
# prints online friends
sub cmd_findfriends {
	my ($data) = split(/ +/, $_[0]);
	my $f2w = Irssi::settings_get_str('friends_findfriends_to_windows');
	my $win = undef;
	my $lc_data = lc($data);
	$win = Irssi::active_win() unless ($f2w || $data eq '');

	# gathering info
	my $by_hand = {};
	foreach my $channel (Irssi::channels()) {
		my $myNick = $channel->{server}->{nick};
		my $tag = lc($channel->{server}->{tag});
		foreach my $nick ($channel->nicks()) {
			# don't count myself
			next if ($nick->{nick} eq $myNick);
			if ((my $idx = get_idx($nick->{nick}, $nick->{host})) > -1) {
				$by_hand->{lc($friends[$idx]->{handle})}->{$tag}->{$channel->{name}} = $nick->{nick};
			}
		}
	}

	# looking for a specified handle
	if ($data ne '') {
		my $handle = undef;
		foreach my $h (keys %{$by_hand}) {
			next if ($lc_data ne $h);
			$handle = $h;
			last;
		}
		return unless (defined $handle);

		# tricky part.
		my @data = ();
		foreach my $ircnet (keys %{$by_hand->{$handle}}) {
			my ($nick, $chan);
			foreach $chan (keys %{$by_hand->{$handle}->{$ircnet}}) {
				$nick = $by_hand->{$handle}->{$ircnet}->{$chan};
				last;
			}
			my $chanstr = join(",", sort keys %{$by_hand->{$handle}->{$ircnet}});
			push(@data, join(" ", $ircnet, $nick, $chanstr));
		}
		# list them.
		list_friend(Irssi::active_win(), $handle, @data);

	# looking for anyone
	} else {
		foreach my $handle (keys %{$by_hand}) {
			foreach my $ircnet (keys %{$by_hand->{$handle}}) {
				my $server = Irssi::server_find_tag($ircnet);
				next unless (defined $server);
				foreach my $chan (sort keys %{$by_hand->{$handle}->{$ircnet}}) {
					my @data = ();
					my $nick = $by_hand->{$handle}->{$ircnet}->{$chan};
					$win = $server->window_item_find($chan);
					$win = Irssi::active_win() unless (defined $win && $f2w);
					my $chanstr = join(",", sort keys %{$by_hand->{$handle}->{$ircnet}});
					push(@data, join(" ", $ircnet, $nick, $chanstr));
					list_friend($win, $handle, @data);
				}
			}
		}
	}
}

# void cmd_isfriend($data, $server, $channel)
# handles /isfriend <nick>
sub cmd_isfriend {
	my ($data, $server, $channel) = @_;
	my $usage = "/ISFRIEND <nick>";

	# remove trailing spaces
	$data =~ s/[\t\ ]+$//;

	# not enough args
	if ($data eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_notenoughargs', $usage);
		return;
	}

	# no server item in current window
	if (!$server) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "No server item in current window");
		return;
	}

	# redirect userhost reply to event_isfriend_userhost()
	# caution: This works only with Irssi 0.7.98.CVS (20011117) and newer
	$server->redirect_event("userhost", 1, $data, 0, undef, {
				"event 302" => "redir userhost_friends"});
	# send our query
	$server->send_raw("USERHOST :$data");
}

# void event_isfriend_userhost($server, $reply, $servername)
# handles redirected USERHOST replies
# (part of /isfriend)
sub event_isfriend_userhost {
	my ($mynick, $reply) = split(/ +/, $_[1]);
	my $server = $_[0];
	my ($nick, $user, $host) = $reply =~ /^:?([^\*=]*)\*?=.(.*)@(.*)/;
	my $string = $nick . '!' . $user . '@' . $host;
	my $friend_matched = 0;

	# try matching ONLY if the response is positive
	if (defined $nick && defined $user && defined $host) {
		if ((my $idx = get_idx($nick, $user.'@'.$host)) > -1) {
			my @chans = ();
			foreach my $channel ($server->channels()) {
				push(@chans, $channel->{name}) if ($channel->nick_find($nick));
			}
			my $chanstr = join(",", @chans);
			list_friend(Irssi::active_win(), $idx, join(" ", $server->{tag}, $nick, $chanstr));
			$friend_matched++;
		}
	}

	# print message
	if ($friend_matched) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_endof', "/isfriend", $nick);
	} else {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_nosuch', $nick);
	}
}

# void event_whois($server, $text, $servername)
# handles additional whois data
sub event_whois {
	my ($server, $text, $servername) = @_;
	return unless (Irssi::settings_get_bool('friends_show_whois_extra'));

	my ($on, $nick, $user, $host, $as, $rn) = split(/[\ ]:?/, $text, 6);
	my $idx = get_idx($nick, $user.'@'.$host);
	return unless ($idx > -1);

	$server->printformat($nick, MSGLEVEL_CRAP, 'friends_whois', get_handbyidx($idx), ($friends[$idx]->{globflags} ? $friends[$idx]->{globflags} : "none"));
}

# void cmd_flushlearnt($data, $server, $channel)
# cycles through all users and removes every chanrec with flag L
# then, if no other stuff left (specific delay, other chanrecs,
# global flags, password maybe) -- deletes user.
# clears the opping tree too
sub cmd_flushlearnt {
	my @todelete = ();
	# cycle through the whole friendlist
	for (my $idx = 0; $idx < @friends; ++$idx) {
		my $was_learnt = 0;

		# foreach friend, clear his opping tree
		$friends[$idx]->{friends} = [];

		# now go through all friend's channel entries
		foreach my $chan (get_friends_channels($idx)) {
			# if 'L' is the only flag for this chan
			if (get_friends_flags($idx, $chan) eq "L") {
				# remove channel record and print a message
				$was_learnt = del_chanrec($idx, $chan);
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_chanrec_removed', get_handbyidx($idx), $chan);
			}
		}

		# delete friend, if he has exactly 1 host, no global flags,
		# neither password, nor chanrecs, and he was learnt.
		if ($was_learnt && scalar(get_friends_hosts($idx, $friends_REGEXP_HOSTS)) == 1  && !get_friends_flags($idx, undef) &&
			!get_friends_channels($idx) && !$friends[$idx]->{password}) {
			push(@todelete, $idx) unless (grep(/^$idx$/, @todelete));
		}
	}
	return unless @todelete;

	@todelete = sort {$a <=> $b} @todelete;
	my @result = del_friend(join(" ", @todelete));
	foreach my $deleted (@result) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_removed', $deleted->{handle});
	}
}

# void cmd_opping_tree($data, $server, $channel)
# prints the Opping Tree
sub cmd_oppingtree {
	my $found = 0;
	# cycle through the whole friendlist
	for (my $idx = 0; $idx < @friends; ++$idx) {
		# get friend's friends
		my @friendFriends = @{$friends[$idx]->{friends}};
		if (@friendFriends) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "Opping tree:") unless ($found);
			$found = 1;
			# print info about our friend
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_optree_line1', get_handbyidx($idx));
			my %masks;
			# get all masks opped by him
			foreach my $friend (@friendFriends) {
				foreach my $host (keys(%{$friend->{hosts}})) {
					$masks{$host}++;
					last;
				}
			}
			# print them, along with the opcount
			foreach my $friend (sort keys %masks) {
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_optree_line2', $masks{$friend}, $friend);
			}
		}
	}
	Irssi::printformat(MSGLEVEL_CRAP, 'friends_general', "Opping tree is empty.") unless ($found);
}

# void event_ctcpmsg($server, $args, $sender, $senderhsot, $target)
# handles ctcp requests
sub event_ctcpmsg {
	my ($server, $args, $sender, $userhost, $target) = @_;

	# return, if ctcp is not for us
	my $myNick = $server->{nick};
	return if (lc($target) ne lc($myNick));

	# return, if we don't process ctcp requests
	return unless (Irssi::settings_get_bool('friends_use_ctcp'));

	# return in case of strange things
	return unless (defined $sender && defined $userhost);

	my @cmdargs = split(/ +/, $args);

	# prepare arguments:
	# get 1st arg, uppercase it
	my $command = uc($cmdargs[0]);
	# get 2nd arg
	my $channelName = $cmdargs[1];
	# get 3rd arg
	my $password = $cmdargs[2];

	# check if $command is one of friends_ctcp_commands. return if it isn't
	return unless (is_ctcp_command($command));

	# this is supposed to be processed BEFORE any other ctcp commands
	# /ctcp nick IDENT handle password
	if ($command eq "IDENT") {
		my $idxguess = get_idxbyhand($channelName);
		# looks like a valid friend, password already set, provided password looks fine
		if ($idxguess > -1 && $friends[$idxguess]->{password} ne "" && friends_passwdok($idxguess, $password)) {
			# do the IDENT stuff here.
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpident', $channelName, $sender.'!'.$userhost);
			add_host($idxguess, "*!$userhost");
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_host_added', $channelName, '*!'.$userhost);
			$server->command("/^NOTICE $sender Identified as " . get_handbyidx($idxguess));
		} else {
			my $reason = "No reason ;)";
			if ($idxguess < 0) {
				$reason = "No such handle: $channelName";
			} elsif ($friends[$idxguess]->{password} eq "") {
				$reason = "Can't IDENT $channelName without password set";
			} elsif (!friends_passwdok($idxguess, $password)) {
				$reason = "Bad password for $channelName";
			}
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $sender.'!'.$userhost, $reason);
		}
		goto SIGSTOP;
	}

	my $idx = get_idx($sender, $userhost);

	# if get_idx* failed, return.
	if ($idx == -1) {
		my $reason = "Not a friend" . (($command ne "PASS") ? " for $channelName" : "");
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $sender.'!'.$userhost, $reason);
		goto SIGSTOP;
	}

	# we'll use handle instead of $sender!$userhost in messages
	my $handle = get_handbyidx($idx);

	# check if $channelName was supplied.
	# (first argument, should be always given)
	if ($channelName eq "") {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough arguments");
		goto SIGSTOP;
	}

	# /ctcp nick PASS pass [newpass]
	if ($command eq "PASS") {
		# if someone has password already set - we can only *change* it
		if ($friends[$idx]->{password}) {
			# if cmdargs[1] ($channelName, that is) is a valid password (current)
			if (!friends_passwdok($idx, $channelName)) {
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
				goto SIGSTOP;
			}
			# and $cmdargs[2] ($password, that is) contains something ...
			if (defined $password) {
				# ... process allowed password change.
				# in this case, old password is in $channelName
				# and new password is in $password
				$friends[$idx]->{password} = friends_crypt("$password");
				Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcppass', $handle, $sender."!".$userhost);
				# send a quiet notice to sender
				$server->command("/^NOTICE $sender Password changed to: $password");
			} else {
				# in this case, notify sender about his current password quietly
				$server->command("/^NOTICE $sender You already have a password set");
			}
		# if $idx doesn't have a password, we will *set* it
		} else {
			# in this case, new password is in $channelName
			# and $password is unused
			$friends[$idx]->{password} = friends_crypt("$channelName");
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcppass', $handle, $sender.'!'.$userhost);
			# send a quiet notice to sender
			$server->command("/^NOTICE $sender Password set to: $channelName");
		}
		goto SIGSTOP;
	}

	# get channel object. if not found -- yell, stop the signal, and return
	my $channel = $server->channel_find($channelName);
	if (!$channel) {
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not on channel $channelName");
		goto SIGSTOP;
	}

	my $sender_rec = $channel->nick_find($sender);

	# /ctcp nick OP #channel password
	if ($command eq "OP") {
		if (!friend_is_wrapper($idx, $channelName, "o", "d")) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough flags");
			goto SIGSTOP;
		}
		if (!friends_passwdok($idx, $password)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
			goto SIGSTOP;
		}

		# process allowed opping
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcprequest', $handle, $command, $channelName);
		$channel->command("op $sender") if ($sender_rec && !$sender_rec->{op});
		goto SIGSTOP;

	# /ctcp nick VOICE #channel password
	} elsif ($command eq "VOICE") {
		if (!friend_is_wrapper($idx, $channelName, "v", undef)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough flags");
			goto SIGSTOP;
		}
		if (!friends_passwdok($idx, $password)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
			goto SIGSTOP;
		}

		# process allowed voicing
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcprequest', $handle, $command, $channelName);
		$channel->command("voice $sender") if ($sender_rec && !$sender_rec->{voice});
		goto SIGSTOP;

	# /ctcp nick INVITE #channel password
	} elsif ($command eq "INVITE") {
		if (!friend_is_wrapper($idx, $channelName, "i", undef)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough flags");
			goto SIGSTOP;
		}
		if (!friends_passwdok($idx, $password)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
			goto SIGSTOP;
		}

		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcprequest', $handle, $command, $channelName);
		if (!$channel->{chanop} && !$sender_rec) {
			# friend is outside channel, but we're not opped
			$server->command("/^NOTICE $sender I'm not opped on $channelName");
		} elsif (!$sender_rec) {
			# process allowed invite
			$channel->command("invite $sender");
		}
		goto SIGSTOP;

	# /ctcp nick KEY #channel password
	} elsif ($command eq "KEY") {
		if (!friend_is_wrapper($idx, $channelName, "k", undef)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough flags");
			goto SIGSTOP;
		}
		if (!friends_passwdok($idx, $password)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
			goto SIGSTOP;
		}

		# process allowed key giving
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcprequest', $handle, $command, $channelName);
		if ($channel->{key} && !$sender_rec) {
			# give a key if channel is +k'ed and $sender is not on $channelName
			$server->command("/^NOTICE $sender Key for $channelName is: $channel->{key}");
		}
		goto SIGSTOP;

	# /ctcp nick UNBAN #channel password
	} elsif ($command eq "UNBAN") {
		if (!friend_is_wrapper($idx, $channelName, "u", undef)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough flags");
			goto SIGSTOP;
		}
		if (!friends_passwdok($idx, $password)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
			goto SIGSTOP;
		}

		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcprequest', $handle, $command, $channelName);
		if (!$channel->{chanop}) {
			# notify him that we're not opped, unless he's here and he can see that ;^)
			$server->command("/^NOTICE $sender I'm not opped on $channelName") if (!$sender_rec);
		} else {
			# process allowed unban
			foreach my $ban ($channel->bans()) {
				if ($server->mask_match_address($ban->{ban}, $sender, $userhost)) {
					$server->command("MODE $channelName -b $ban->{ban}");
				}
			}
		}
		goto SIGSTOP;

	# /ctcp nick LIMIT #channel password
	} elsif ($command eq "LIMIT") {
		if (!friend_is_wrapper($idx, $channelName, "l", undef)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Not enough flags");
			goto SIGSTOP;
		}
		if (!friends_passwdok($idx, $password)) {
			Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcpfail', $command, $handle, "Bad password");
			goto SIGSTOP;
		}

		# process allowed limit raising
		Irssi::printformat(MSGLEVEL_CRAP, 'friends_ctcprequest', $handle, $command, $channelName);
		if (!$channel->{chanop}) {
			# notify him that we're not opped, unless he's here and he can see that ;^)
			$server->command("/^NOTICE $sender I'm not opped on $channelName") if (!$sender_rec);
		} else {
			my @nicks = $channel->nicks();
			if ($channel->{limit} && $channel->{limit} <= scalar(@nicks)) {
				# raise the limit if it's needed
				$server->command("MODE $channelName +l " . (scalar(@nicks) + 1));
			}
		}
		goto SIGSTOP;
	}

	# stop the signal if we processed the request
SIGSTOP:
	Irssi::signal_stop();
}

# void cmd_friendsversion($data, $server, $channel)
# handles /friendsversion
# prints script's and friendlist's version
sub cmd_friendsversion() {
	print_version("script");
	print_version("filever");
	print_version("filewritten");
}

# settings
Irssi::settings_add_int('misc', 'friends_delay_min', $default_delay_min);
Irssi::settings_add_int('misc', 'friends_delay_max', $default_delay_max);
Irssi::settings_add_int('misc', 'friends_max_queue_size', $default_friends_max_queue_size);
Irssi::settings_add_int('misc', 'friends_revenge_mode', $default_friends_revenge_mode);
Irssi::settings_add_bool('misc', 'friends_revenge', $default_friends_revenge);
Irssi::settings_add_bool('misc', 'friends_learn', $default_friends_learn);
Irssi::settings_add_bool('misc', 'friends_voice_opped', $default_friends_voice_opped);
Irssi::settings_add_bool('misc', 'friends_use_ctcp', $default_friends_use_ctcp);
Irssi::settings_add_bool('misc', 'friends_autosave', $default_friends_autosave);
Irssi::settings_add_bool('misc', 'friends_backup_friendlist', $default_friends_backup_friendlist);
Irssi::settings_add_bool('misc', 'friends_show_flags_on_join', $default_friends_show_flags_on_join);
Irssi::settings_add_bool('misc', 'friends_findfriends_to_windows', $default_friends_findfriends_to_windows);
Irssi::settings_add_bool('misc', 'friends_show_whois_extra', $default_friends_show_whois_extra);
Irssi::settings_add_str('misc', 'friends_ctcp_commands', $default_friends_ctcp_commands);
Irssi::settings_add_str('misc', 'friends_default_flags', $default_friends_default_flags);
Irssi::settings_add_str('misc', 'friends_file', $default_friends_file);
Irssi::settings_add_str('misc', 'friends_backup_suffix', $default_friends_backup_suffix);

# commands
Irssi::command_bind('addfriend', 'cmd_addfriend');
Irssi::command_bind('delfriend', 'cmd_delfriend');
Irssi::command_bind('addhost', 'cmd_addhost');
Irssi::command_bind('delhost', 'cmd_delhost');
Irssi::command_bind('delchanrec', 'cmd_delchanrec');
Irssi::command_bind('chhandle', 'cmd_chhandle');
Irssi::command_bind('chdelay', 'cmd_chdelay');
Irssi::command_bind('loadfriends', 'cmd_loadfriends');
Irssi::command_bind('savefriends', 'cmd_savefriends');
Irssi::command_bind('listfriends', 'cmd_listfriends');
Irssi::command_bind('findfriends', 'cmd_findfriends');
Irssi::command_bind('isfriend', 'cmd_isfriend');
Irssi::command_bind('chflags', 'cmd_chflags');
Irssi::command_bind('chpass', 'cmd_chpass');
Irssi::command_bind('comment', 'cmd_comment');
Irssi::command_bind('oppingtree', 'cmd_oppingtree');
Irssi::command_bind('opfriends', 'cmd_opfriends');
Irssi::command_bind('queue', 'cmd_queue');
Irssi::command_bind('queue show', 'cmd_queue_show');
Irssi::command_bind('queue flush', 'cmd_queue_flush');
Irssi::command_bind('queue purge', 'cmd_queue_purge');
Irssi::command_bind('flushlearnt', 'cmd_flushlearnt');
Irssi::command_bind('friendsversion', 'cmd_friendsversion');

# events
Irssi::signal_add_last('massjoin', 'event_massjoin');
Irssi::signal_add_last('event mode', 'event_modechange');
Irssi::signal_add_last('event 311', 'event_whois');
Irssi::signal_add('default ctcp msg', 'event_ctcpmsg');
Irssi::signal_add('redir userhost_friends', 'event_isfriend_userhost');
Irssi::signal_add('redir userhost_addfriend', 'event_addfriend_userhost');
Irssi::signal_add('setup saved', 'event_setup_saved');
Irssi::signal_add('setup reread', 'event_setup_reread');
Irssi::signal_add('nicklist changed', 'event_nicklist_changed');
Irssi::signal_add('server disconnected', 'event_server_disconnected');
Irssi::signal_add('server connect failed', 'event_server_disconnected');
Irssi::signal_add_first('event kick', 'event_kick');

print_releasenote() if (defined($release_note));
load_friends();