This file is indexed.

/usr/share/perl5/Net/Hotline/Client.pm is in libnet-hotline-perl 0.83-2.

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
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
package Net::Hotline::Client;

## Copyright(c) 1998-2002 by John C. Siracusa.  All rights reserved.  This
## program is free software; you can redistribute it and/or modify it under
## the same terms as Perl itself.

use strict;

use vars qw(@ISA $VERSION $DEBUG);

use Carp;
use IO::File;
use IO::Socket;
use Net::Hotline::User;
use Net::Hotline::Task;
use Net::Hotline::PrivateChat;
use Net::Hotline::FileListItem;
use Net::Hotline::FileInfoItem;
use Net::Hotline::TrackerListItem;
use Net::Hotline::Protocol::Packet;
use Net::Hotline::Protocol::Header;
use Net::Hotline::Shared qw(:all);
use Net::Hotline::Constants qw(:all);

if($^O eq 'MacOS') # "#ifdef", where have you gone...
{
  require Mac::MoreFiles;
  require Mac::Files;
}

use AutoLoader 'AUTOLOAD';

#
# Class attributes
#

$VERSION = '0.83';
$DEBUG   = 0;

# CRC perl code lifted from Convert::BinHex by Eryq (eryq@enteract.com)
# An array useful for CRC calculations that use 0x1021 as the "seed":
my(@CRC_MAGIC) = (
    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
    0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
    0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
    0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
    0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
    0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
    0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
    0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
    0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
    0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
    0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
    0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
    0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
    0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
    0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
    0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
    0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
    0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
    0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
    0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
    0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
    0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
    0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
    0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
    0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
    0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
    0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
    0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
    0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
    0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
    0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
    0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
);

1;

#
# Non-autoloaded object methods
#

sub new
{
  my($class) = shift;

  my($self) =
  {
    'NICK'         => undef,
    'LOGIN'        => undef,
    'COLOR'        => undef,
    'SERVER_PORT'  => undef,
    'SERVER_ADDR'  => undef,
    'TRACKER_ADDR' => undef,

    'SOCKET'       => undef,
    'BLOCKING'     => 1,
    'SERVER'       => undef,
    'SEQNUM'       => 1,

    'USER_LIST'    => undef,
    'NEWS'         => undef,
    'FILES'        => undef,
    'AGREEMENT'    => undef,
    'PCHATS'       => undef,
    'TASKS'        => undef,

    'FILE_INFO'    => undef,

    'HANDLERS'  =>
    {
      'AGREEMENT'     => undef,
      'BAN'           => undef,
      'CHAT'          => undef,
      'CHAT_ACTION'   => undef,
      'COLOR'         => undef,
      'EVENT'         => undef,
      'FILE_DELETE'   => undef,
      'FILE_GET'      => undef,
      'FILE_GET_INFO' => undef,
      'FILE_LIST'     => undef,
      'FILE_MKDIR'    => undef,
      'FILE_MOVE'     => undef,
      'FILE_SET_INFO' => undef,
      'ICON'          => undef,
      'JOIN'          => undef,
      'KICK'          => undef,
      'LEAVE'         => undef,
      'LOGIN'         => undef,
      'MSG'           => undef,
      'NEWS'          => undef,
      'NEWS_POST'     => undef,
      'NEWS_POSTED'   => undef,
      'NICK'          => undef,
      'PCHAT_ACCEPT'  => undef,
      'PCHAT_CREATE'  => undef,
      'PCHAT_INVITE'  => undef,
      'PCHAT_JOIN'    => undef,
      'PCHAT_LEAVE'   => undef,
      'PCHAT_SUBJECT' => undef,
      'QUIT'          => undef,
      'SEND_MSG'      => undef,
      'SERVER_MSG'    => undef,
      'TASK_ERROR'    => undef,
      'USER_GETINFO'  => undef,
      'USER_LIST'     => undef
    },

    'BLOCKING_TASKS'         => undef,
    'DEFAULT_HANDLERS'       => undef,
    'HANDLERS_WHEN_BLOCKING' => undef,

    'LOGGED_IN'       => undef,

    'EVENT_TIMING'    => 1,
    'CONNECT_TIMEOUT' => 15,
    'PATH_SEPARATOR'  => HTLC_PATH_SEPARATOR,
    'HTXF_BUFSIZE'    => HTXF_BUFSIZE,

    'DOWNLOADS_DIR'   => undef,
    'DATA_FORK_EXT'   => '.data',
    'RSRC_FORK_EXT'   => '.rsrc',

    'LAST_ACTIVITY'   => time(),
    'LAST_ERROR'      => undef,
    'MACOS'           => ($^O eq 'MacOS') ? 1 : 0
  };

  bless  $self, $class;
  return $self;
}

sub agreement { $_[0]->{'AGREEMENT'} }

sub blocking
{
  my($self, $blocking) = @_;

  return $self->{'BLOCKING'}  unless(@_ == 2);

  if(ref($self->{'SERVER'}) && $self->{'SERVER'}->opened())
  {
    _set_blocking($self->{'SERVER'}, $blocking);
  }

  $self->{'BLOCKING'} = (($blocking) ? 1 : 0);
  return $self->{'BLOCKING'};
}

sub blocking_tasks
{
  my($self, $arg) = @_;
  $self->{'BLOCKING_TASKS'} = ($arg) ? 1 : 0  if(@_ == 2);
  return $self->{'BLOCKING_TASKS'};
}

sub connect_timeout
{
  my($self, $secs) = @_;
  $self->{'CONNECT_TIMEOUT'} = $secs  if($secs =~ /^\d+$/);
  return $self->{'CONNECT_TIMEOUT'};
}

sub default_handlers
{
  my($self, $arg) = @_;
  $self->{'DEFAULT_HANDLERS'} = ($arg) ? 1 : 0  if(@_ == 2);
  return $self->{'DEFAULT_HANDLERS'};
}

sub downloads_dir
{
  my($self, $dir) = @_;
  $self->{'DOWNLOADS_DIR'} = $dir  if(-d $dir);
  return $self->{'DOWNLOADS_DIR'};
}

sub data_fork_extension
{
  my($self, $ext) = @_;
  croak("The data fork extension may not be the same as the resource fork extension!")
    if($ext eq $self->{'DATA_FORK_EXT'}); 
  $self->{'DATA_FORK_EXT'} = $ext  if(defined($ext));
  return $self->{'DATA_FORK_EXT'};
}

sub event_timing
{
  my($self, $secs) = @_;

  if(defined($secs))
  {
    croak qw(Bad argument to event_timing() - "$secs")  if($secs =~ /[^0-9.]/);
    $self->{'EVENT_TIMING'} = $secs;
  }

  return $self->{'EVENT_TIMING'};
}

sub files    { $_[0]->{'FILES'}    }
sub handlers { $_[0]->{'HANDLERS'} }

sub handlers_during_blocking_tasks
{
  my($self, $arg) = @_;
  $self->{'HANDLERS_WHEN_BLOCKING'} = ($arg) ? 1 : 0  if(@_ == 2);
  return $self->{'HANDLERS_WHEN_BLOCKING'};
}

sub last_error  { $_[0]->{'LAST_ERROR'} }
sub clear_error { $_[0]->{'LAST_ERROR'} = undef }

sub xfer_bufsize
{
  my($self, $size) = @_;
  $self->{'HTXF_BUFSIZE'} = $size  if($size =~ /^\d+$/);
  return $self->{'HTXF_BUFSIZE'};
}

sub last_activity
{
  my($self) = shift;
  return $self->{'LAST_ACTIVITY'};
}

sub news { $_[0]->{'NEWS'} }

sub path_separator
{
  my($self, $separator) = @_;
  $self->{'PATH_SEPARATOR'} = $separator  if($separator =~ /^.$/);
  return $self->{'PATH_SEPARATOR'};
}

sub rsrc_fork_extension
{
  my($self, $ext) = @_;
  croak("The resource fork extension may not be the same as the data fork extension!")
    if($ext eq $self->{'RSRC_FORK_EXT'}); 
  $self->{'RSRC_FORK_EXT'} = $ext  if(defined($ext));
  return $self->{'RSRC_FORK_EXT'};
}

sub pchats   { $_[0]->{'PCHATS'}      }
sub userlist { $_[0]->{'USER_LIST'}   }

sub server
{
  $_[0]->{'SERVER_ADDR'} .
    ($_[0]->{'SERVER_PORT'} ne HTLS_TCPPORT) ?
      ":$_[0]->{'SERVER_PORT'}" : '';
}

sub connect
{
  my($self, $server) = @_;

  my($address, $port);

  if(($address = $server) =~ s/^([^ :]+)(?:[: ](\d+))?$/$1/)
  {
    $port = $2 || HTLS_TCPPORT;
  }
  else
  {
    croak("Bad server address: $server");
  }

  eval
  {
    $SIG{'ALRM'} = sub { die "timeout" };
    alarm($self->{'CONNECT_TIMEOUT'});

    $self->{'SERVER'} = 
      IO::Socket::INET->new(PeerAddr =>$address,
                            PeerPort =>$port,
                            Proto    =>'tcp');

    alarm(0);
    $SIG{'ALRM'} = 'DEFAULT';
  };

  if($@ =~ /timeout/)
  {
    $self->{'LAST_ERROR'} = "Timed out after $self->{'CONNECT_TIMEOUT'} seconds";
    return;
  }

  if(!$self->{'SERVER'} || $@)
  {
    $self->{'LAST_ERROR'} = $@ || $! || 'Connection failed';
    return;
  }

  $self->{'SERVER'}->autoflush(1);

  $self->{'SERVER_ADDR'} = $address;
  $self->{'SERVER_PORT'} = $port;

  return(1);
}

sub disconnect
{
  my($self) = shift;

  if(ref($self->{'SERVER'}) && $self->{'SERVER'}->opened())
  {
    $self->{'SERVER'}->close();
    $self->{'LOGGED_IN'} = undef;
    $self->{'SERVER_ADDR'} = undef;
    return(1);
  }

  $self->{'LAST_ERROR'} = 'Not connected.';
  return;
}

sub login
{
  my($self, %args) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_login_now(%args); 
  }
  else
  {
    return $self->_login(%args); 
  }
}

sub _login_now
{
  my($self, %args) = @_;

  my($no_news, $no_userlist, $task_num, $task, $packet);

  $no_news     = $args{'NoNews'};
  $no_userlist = $args{'NoUserList'};

  $args{'NoNews'} = $args{'NoUserList'} = undef;

  $task_num = $self->_login(%args);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    $self->disconnect();
    return;
  }

  unless($no_news)
  {
    unless($self->get_news())
    {
      $self->{'LAST_ERROR'} = "Login succeeded, but could not get news.";
      return("0E-0");
    }
  }

  unless($no_userlist)
  {
    unless($self->get_userlist())
    {
      $self->{'LAST_ERROR'} = "Login succeeded, but could not get userlist";
      return("0E-0");
    }
  }

  return(1);
}

sub _login
{
  my($self, %args) = @_;

  my($nick, $login, $password, $icon, $enc_login, $enc_password,
     $proto_header, $data, $response, $task_num, $server);

  $server = $self->{'SERVER'} or croak "Not connected to a server";

  unless($server->opened())
  {
    $self->{'LAST_ERROR'} = "login() called before connect()";
    return;
  }

  $nick  = $args{'Nickname'} || HTLC_DEFAULT_NICK;
  $login = $args{'Login'}    || HTLC_DEFAULT_LOGIN;
  $icon  = $args{'Icon'}     || HTLC_DEFAULT_ICON;
  $password = $args{'Password'};

  $self->{'NICK'}  = $nick;
  $self->{'LOGIN'} = $login;
  $self->{'ICON'}  = $icon;

  _hlc_write($self, $server, \HTLC_MAGIC, HTLC_MAGIC_LEN) || return;
  _hlc_read($self, $server, \$response, HTLS_MAGIC_LEN) || return;

  if($response ne HTLS_MAGIC)
  {
    $self->{'LAST_ERROR'} = "Handshake failed.  Not a hotline server?";
    $self->disconnect();
    return;
  }

  $enc_login    = _encode($login);
  $enc_password = _encode($password);

  $proto_header = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_LOGIN);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_PROTO_HDR + 
                     length($enc_login) +
                     length($enc_password) +
                     length($nick));
  $proto_header->len2($proto_header->len);

  my($fmt) = 'nnna*nna*nna*nnn';

  $data = $proto_header->header() .
          pack($fmt, 0x0004,                  # Num atoms

                     HTLC_DATA_LOGIN,         # Atom type
                     length($enc_login),      # Atom length
                     $enc_login,              # Atom data

                     HTLC_DATA_PASSWORD,      # Atom type
                     length($enc_password),   # Atom length
                     $enc_password,           # Atom data

                     HTLC_DATA_NICKNAME,      # Atom type
                     length($nick),           # Atom length
                     $nick,                   # Atom data

                     HTLC_DATA_ICON,          # Atom type
                     0x0002,                  # Atom length
                     $icon);                  # Atom data

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: LOGIN - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_LOGIN, time());
  }
  else { return }

  unless($args{'NoUserList'})
  {
    $self->req_userlist();
  }

  unless($args{'NoNews'})
  {
    $self->req_news();
  }

  _set_blocking($server, $self->{'BLOCKING'});

  return($task_num);
}

sub run
{
  my($self) = shift;

  my($server) = $self->{'SERVER'}  or croak "Not connected to a server";
  return  unless($server->opened());

  my($ret, $packet);

  $packet = new Net::Hotline::Protocol::Packet;

  while($ret = $packet->read_parse($server, $self->{'BLOCKING'}))
  {
    _process_packet($self, $packet, $ret) || return(1);
  }

  return(1);
}

sub _process_packet
{
  my($self, $packet, $ret, $blocking_task) = @_;

  my($data_ref, $type, $use_handlers);

  $use_handlers = !($blocking_task && !$self->{'HANDLERS_WHEN_BLOCKING'});

  $type = $packet->{'TYPE'};

  if($ret == HTLC_EWOULDBLOCK) # Idle event
  {
    if(defined($self->{'HANDLERS'}->{'EVENT'}))
    {
      &{$self->{'HANDLERS'}->{'EVENT'}}($self, 1);
    }

    select(undef, undef, undef, $self->{'EVENT_TIMING'});
    return(1);
  }

  $self->{'LAST_ACTIVITY'} = time();

  if(defined($self->{'HANDLERS'}->{'EVENT'})) # Non-idle event
  {
    &{$self->{'HANDLERS'}->{'EVENT'}}($self, 0);
  }

  _debug("Packet type = $type\n");

  if($type == HTLS_HDR_USER_LEAVE)
  {
    # Hotline server *BUG* - you may get a "disconnect" packet for a
    # socket _before_ you get the "connect" packet for that socket!
    # In fact, the "connect" packet will never arrive in this case.

    if(defined($packet->{'SOCKET'}) &&
       defined($self->{'USER_LIST'}->{$packet->{'SOCKET'}}))
    {
      my($user) = $self->{'USER_LIST'}->{$packet->{'SOCKET'}};

      delete $self->{'USER_LIST'}->{$packet->{'SOCKET'}};

      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'LEAVE'}))
        {
          &{$self->{'HANDLERS'}->{'LEAVE'}}($self, $user);
        }
        elsif($self->{'DEFAULT_HANDLERS'})
        {       
          print "USER LEFT: ", $user->nick(), "\n";
        }
      }
    }
  }
  elsif($type == HTLS_HDR_TASK)
  {
    my($task) = $self->{'TASKS'}->{$packet->{'TASK_NUM'}};

    my($task_type) = $task->type();

    $task->finish(time());

    if(defined($packet->{'TASK_ERROR'}))
    {
      $task->error(1);
      $task->error_text($packet->{'TASK_ERROR'});

      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'TASK_ERROR'}))
        {
          &{$self->{'HANDLERS'}->{'TASK_ERROR'}}($self, $task);
        }
        else
        {
          print "TASK ERROR(", $task->num(), ':', $task->type(), ") ",
                $task->error_text(), "\n";
        }
      }
    }
    else
    {
      $task->error(0);

      if($task_type == HTLC_TASK_USER_LIST && defined($packet->{'USER_LIST'}))
      {
        $self->{'USER_LIST'} = $packet->{'USER_LIST'};

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'USER_LIST'}))
          {
            &{$self->{'HANDLERS'}->{'USER_LIST'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "GET USER LIST: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_FILE_LIST)
      {
        my($path);

        $task->path("")  unless(length($task->path()));
        $path = $task->path();

        if($packet->{'FILE_LIST'})
        {
          $self->{'FILES'}->{$path} = $packet->{'FILE_LIST'};
        }
        else
        {
          $self->{'FILES'}->{$path} = [];
        }

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_LIST'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_LIST'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "GET FILE LIST: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_NEWS && defined($packet->{'DATA'}))
      {
        my(@news) = split(/_{58}/, $packet->{'DATA'});

        $self->{'NEWS'} = \@news;

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'NEWS'}))
          {
            &{$self->{'HANDLERS'}->{'NEWS'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "GET NEWS: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_USER_INFO && defined($packet->{'DATA'}))
      {
        my($user) = $self->{'USER_LIST'}->{$task->socket()};

        $user->info($packet->{'DATA'});

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'USER_GETINFO'}))
          {
            &{$self->{'HANDLERS'}->{'USER_GETINFO'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "GET USER INFO: Task complete.\n";
          }
        }

        _debug("USER_GETINFO for: $packet->{'NICK'} (", $task->socket(), ")\n",
               $packet->{'DATA'}, "\n");
      }
      elsif($task_type == HTLC_TASK_FILE_INFO)
      {
        my($path, $file_info);

        $task->path("")  unless(length($task->path));
        $path = $task->path();

        $file_info = $self->{'FILE_INFO'} = new Net::Hotline::FileInfoItem();

        $file_info->icon($packet->{'FILE_ICON'});
        $file_info->type($packet->{'FILE_TYPE'});
        $file_info->creator($packet->{'FILE_CREATOR'});
        $file_info->size($packet->{'FILE_SIZE'});
        $file_info->name($packet->{'FILE_NAME'});
        $file_info->comment($packet->{'FILE_COMMENT'});
        $file_info->ctime($packet->{'FILE_CTIME'});
        $file_info->mtime($packet->{'FILE_MTIME'});

        if($use_handlers)
        {      
          if(defined($self->{'HANDLERS'}->{'FILE_GET_INFO'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_GET_INFO'}}($self, $task, $file_info);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "FILE_GET_INFO: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_LOGIN)
      {
        $self->{'LOGGED_IN'} = 1;

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'LOGIN'}))
          {
            &{$self->{'HANDLERS'}->{'LOGIN'}}($self);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "LOGIN: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_NEWS_POST)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'NEWS_POST'}))
          {
            &{$self->{'HANDLERS'}->{'NEWS_POST'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "POST NEWS: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_SEND_MSG)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'SEND_MSG'}))
          {
            &{$self->{'HANDLERS'}->{'SEND_MSG'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "SEND MSG: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_KICK)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'KICK'}))
          {
            &{$self->{'HANDLERS'}->{'KICK'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "KICK: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_BAN)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'BAN'}))
          {
            &{$self->{'HANDLERS'}->{'BAN'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "BAN: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_SET_INFO)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_SET_INFO'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_SET_INFO'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "SET INFO: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_FILE_DELETE)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_DELETE'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_DELETE'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "DELETE FILE: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_FILE_MKDIR)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_MKDIR'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_MKDIR'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "CREATE FOLDER: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_FILE_MOVE)
      {
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_MOVE'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_MOVE'}}($self, $task);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "MOVE FILE: Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_FILE_GET)
      {
        my($size) = $packet->{'HTXF_SIZE'};
        my($ref)  = $packet->{'HTXF_REF'};

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_GET'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_GET'}}($self, $task, $ref, $size);
          }
          else
          {
            print "GET FILE: Starting download (ref = $ref, size = $size)\n"
              if($self->{'DEFAULT_HANDLERS'});

            $self->recv_file($task, $ref, $size);
          }
        }
      }
      elsif($task_type == HTLC_TASK_FILE_PUT)
      {
        my($ref)    = $packet->{'HTXF_REF'};
        my($resume) = $packet->{'HTXF_RFLT'};
        my($size)   = ${$task->misc()}[0] + ${$task->misc()}[1];

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'FILE_PUT'}))
          {
            &{$self->{'HANDLERS'}->{'FILE_PUT'}}($self, $task, $ref, $size, $resume);
          }
          else
          {
            print "GET PUT: Starting upload (ref = $ref)\n"
              if($self->{'DEFAULT_HANDLERS'});

            $self->send_file($task, $ref, $size, $resume);
          }
        }
      }
      elsif($task_type == HTLC_TASK_PCHAT_CREATE)
      {
        my($ref)    = $packet->{'PCHAT_REF'};
        my($user)   = $self->{'USER_LIST'}->{$packet->{'SOCKET'}};
        my($pchat)  = $self->{'PCHATS'}->{$ref} = new Net::Hotline::PrivateChat;

        $pchat->reference($ref);
        $pchat->userlist({ $packet->{'SOCKET'} => $user });

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'PCHAT_CREATE'}))
          {
            &{$self->{'HANDLERS'}->{'PCHAT_CREATE'}}($self, $task, $pchat);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "CREATE PCHAT($ref): Task complete.\n";
          }
        }
      }
      elsif($task_type == HTLC_TASK_PCHAT_ACCEPT)
      {
        my($ref) = $task->misc();

        my($userlist);
        
        # Create userlist of references to the main userlist rather
        # than new user objects (as returned in the packet)
        foreach my $socket (keys(%{$packet->{'USER_LIST'}}))
        {
          $userlist->{$socket} = $self->{'USER_LIST'}->{$socket};
        }

        my($pchat)  = $self->{'PCHATS'}->{$ref} =
          new Net::Hotline::PrivateChat($ref, $userlist);
        
        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'PCHAT_ACCEPT'}))
          {
            &{$self->{'HANDLERS'}->{'PCHAT_ACCEPT'}}($self, $task, $pchat);
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "ACCEPT PCHAT INVITE($ref): Task complete.\n";
          }
        }
      }
    }
    # Reclaim memory
    delete $self->{'TASKS'}->{$packet->{'TASK_NUM'}};
  }
  elsif($type == HTLS_HDR_AGREEMENT)
  {
    $self->{'AGREEMENT'} = $packet->{'DATA'};

    if(defined($packet->{'DATA'}))
    {
      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'AGREEMENT'}))
        {
          &{$self->{'HANDLERS'}->{'AGREEMENT'}}($self, \$packet->{'DATA'});
        }
        elsif($self->{'DEFAULT_HANDLERS'})
        {
          print "AGREEMENT:\n", $packet->{'DATA'}, "\n";
        }
      }
    }
  }
  elsif($type == HTLS_HDR_MSG)
  {
    my($user) = $self->{'USER_LIST'}->{$packet->{'SOCKET'}};

    # User-to-user message
    if(defined($user) && defined($packet->{'DATA'}))
    {
      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'MSG'}))
        {
          &{$self->{'HANDLERS'}->{'MSG'}}($self, $user, \$packet->{'DATA'}, \$packet->{'REPLY_TO'});
        }
        elsif($self->{'DEFAULT_HANDLERS'})
        {
          print "MSG: ", $user->nick(), "(", 
                         $packet->{'SOCKET'}, ") ", 
                         $packet->{'DATA'};

          if($packet->{'IS_REPLY'})
          {
            print " (In reply to: $packet->{'REPLY_TO'}])";
          }

          print "\n";
        }
      }
    }
    elsif(defined($packet->{'DATA'})) # Server message
    {
      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'SERVER_MSG'}))
        {
          &{$self->{'HANDLERS'}->{'SERVER_MSG'}}($self, \$packet->{'DATA'});
        }
        elsif($self->{'DEFAULT_HANDLERS'})
        {
          print "SERVER MSG: ", $packet->{'DATA'}, "\n";
        }
      }
    }
  }
  elsif($type == HTLS_HDR_USER_CHANGE)
  {
    if(defined($packet->{'NICK'}) && defined($packet->{'SOCKET'}) &&
       defined($packet->{'ICON'}) && defined($packet->{'COLOR'}))
    {
      if(defined($self->{'USER_LIST'}->{$packet->{'SOCKET'}}))
      {
        my($user) = $self->{'USER_LIST'}->{$packet->{'SOCKET'}};

        if($user->nick() ne $packet->{'NICK'})
        {
          my($old_nick) = $user->nick();

          $user->nick($packet->{'NICK'});

          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'NICK'}))
            {
              &{$self->{'HANDLERS'}->{'NICK'}}($self, $user, $old_nick, $user->nick());
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "USER CHANGE: $old_nick is now known as ", $user->nick(), "\n";
            }
          }
        }
        elsif($user->icon() ne $packet->{'ICON'})
        {
          my($old_icon) = $user->icon();

          $user->icon($packet->{'ICON'});

          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'ICON'}))
            {
              &{$self->{'HANDLERS'}->{'ICON'}}($self, $user, $old_icon, $user->icon());
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "USER CHANGE: ", $user->nick(),
                    " icon changed from $old_icon to ",
                    $user->icon(), "\n";
            }
          }
        }
        elsif($user->color() ne $packet->{'COLOR'})
        {
          my($old_color) = $user->color();

          $user->color($packet->{'COLOR'});

          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'COLOR'}))
            {
              &{$self->{'HANDLERS'}->{'COLOR'}}($self, $user, $old_color, $user->color());
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "USER CHANGE: ", $user->nick(),
                    " color changed from $old_color to ",
                    $user->color(), "\n";
            }
          }
        }
      }
      else
      {
        $self->{'USER_LIST'}->{$packet->{'SOCKET'}} =
          new Net::Hotline::User($packet->{'SOCKET'},
                            $packet->{'NICK'},
                            undef,
                            $packet->{'ICON'},
                            $packet->{'COLOR'});

        if($use_handlers)
        {
          if(defined($self->{'HANDLERS'}->{'JOIN'}))
          {
            &{$self->{'HANDLERS'}->{'JOIN'}}($self, $self->{'USER_LIST'}->{$packet->{'SOCKET'}});
          }
          elsif($self->{'DEFAULT_HANDLERS'})
          {
            print "JOINED:\n",
                  "  Nick: $packet->{'NICK'}\n",
                  "  Icon: $packet->{'ICON'}\n",
                  "Socket: $packet->{'SOCKET'}\n",
                  " Color: $packet->{'COLOR'}\n";
          }
        }
      }
    }
  }
  elsif($type == HTLS_HDR_CHAT)
  {
    if(defined($packet->{'DATA'}))
    {
      $packet->{'DATA'} =~ s/^\n//s;

      my($ref) = $packet->{'PCHAT_REF'};

      if($ref) # Priate chat
      {
        # Private chat "action"
        if($packet->{'DATA'} =~ /^ \*\*\* /)
        {
          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'PCHAT_ACTION'}))
            {
              &{$self->{'HANDLERS'}->{'PCHAT_ACTION'}}($self, $ref, \$packet->{'DATA'});
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "PCHAT($ref) ACTION: ", $packet->{'DATA'}, "\n";
            }
          }
        }
        else # Regular private chat
        {
          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'PCHAT_CHAT'}))
            {
              &{$self->{'HANDLERS'}->{'PCHAT_CHAT'}}($self, $ref, \$packet->{'DATA'});
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "PCHAT($ref): ", $packet->{'DATA'}, "\n";
            }
          }
        }
      }
      else # Regular chat
      {
        # Chat "action"
        if($packet->{'DATA'} =~ /^ \*\*\* /)
        {
          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'CHAT_ACTION'}))
            {
              &{$self->{'HANDLERS'}->{'CHAT_ACTION'}}($self, \$packet->{'DATA'});
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "CHAT ACTION: ", $packet->{'DATA'}, "\n";
            }
          }
        }
        else # Regular chat
        {
          if($use_handlers)
          {
            if(defined($self->{'HANDLERS'}->{'CHAT'}))
            {
              &{$self->{'HANDLERS'}->{'CHAT'}}($self, \$packet->{'DATA'});
            }
            elsif($self->{'DEFAULT_HANDLERS'})
            {
              print "CHAT: ", $packet->{'DATA'}, "\n";
            }
          }
        }
      }
    }
  }
  elsif($type == HTLS_HDR_NEWS_POST)
  {
    my($post) = $packet->{'DATA'};

    if(defined($post))
    {
      $post =~ s/@{[HTLC_NEWLINE]}/\n/osg;
      $post =~ s/_{58}//sg;

      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'NEWS_POSTED'}))
        {
          &{$self->{'HANDLERS'}->{'NEWS_POSTED'}}($self, \$post);
        }
        elsif($self->{'DEFAULT_HANDLERS'})
        {
          print "NEWS: New post made.\n";
        }
      }
    }
  }
  elsif($type == HTLS_HDR_POLITE_QUIT ||
        $type eq 'DISCONNECTED')
  {
    if(defined($packet->{'DATA'}))
    {
      if($use_handlers)
      {
        if(defined($self->{'HANDLERS'}->{'QUIT'}))
        {
          &{$self->{'HANDLERS'}->{'QUIT'}}($self, \$packet->{'DATA'});
        }
        elsif($self->{'DEFAULT_HANDLERS'})
        {
          print "CONNECTION CLOSED: ", $packet->{'DATA'}, "\n";
        }
      }
    }
    elsif($self->{'DEFAULT_HANDLERS'})
    {
      if($use_handlers)
      {
        print "CONNECTION CLOSED\n";
      }
    }

    $self->disconnect();
    return(0);
  }
  elsif($type == HTLS_HDR_PCHAT_INVITE)
  {
    if($use_handlers)
    {
      if(defined($self->{'HANDLERS'}->{'PCHAT_INVITE'}))
      {
        &{$self->{'HANDLERS'}->{'PCHAT_INVITE'}}($self, $packet->{'PCHAT_REF'},
                                                 $packet->{'SOCKET'},
                                                 $packet->{'NICK'});
      }
      elsif($self->{'DEFAULT_HANDLERS'})
      {
        print "PCHAT INVITE($packet->{'PCHAT_REF'}) from $packet->{'NICK'}($packet->{'SOCKET'})",
              "($packet->{'SOCKET)'})\n";
      }
    }
  }
  elsif($type == HTLS_HDR_PCHAT_USER_JOIN)
  {
    my($ref)    = $packet->{'PCHAT_REF'};
    my($socket) = $packet->{'SOCKET'};
    my($pchat)  = $self->{'PCHATS'}->{$ref};

    $pchat->userlist()->{$socket} = $self->{'USER_LIST'}->{$socket};

    if($use_handlers)
    {
      if(defined($self->{'HANDLERS'}->{'PCHAT_JOIN'}))
      {
        &{$self->{'HANDLERS'}->{'PCHAT_JOIN'}}($self, $pchat, $socket);
      }
      elsif($self->{'DEFAULT_HANDLERS'})
      {
        print "PCHAT($ref)  JOIN($socket)\n";
      }
    }
  }
  elsif($type == HTLS_HDR_PCHAT_USER_LEAVE)
  {
    my($ref)    = $packet->{'PCHAT_REF'};
    my($socket) = $packet->{'SOCKET'};
    my($pchat)  = $self->{'PCHATS'}->{$ref};

    delete $pchat->userlist()->{$socket};

    if($use_handlers)
    {
      if(defined($self->{'HANDLERS'}->{'PCHAT_LEAVE'}))
      {
        &{$self->{'HANDLERS'}->{'PCHAT_LEAVE'}}($self, $pchat, $socket);
      }
      elsif($self->{'DEFAULT_HANDLERS'})
      {
        print "PCHAT($ref)  LEAVE($socket)\n";
      }
    }
  }
  elsif($type == HTLS_HDR_PCHAT_SUBJECT)
  {
    my($pchat) = $self->{'PCHATS'}->{$packet->{'PCHAT_REF'}};
    
    $pchat->subject($packet->{'DATA'});

    if($use_handlers)
    {
      if(defined($self->{'HANDLERS'}->{'PCHAT_SUBJECT'}))
      {
        &{$self->{'HANDLERS'}->{'PCHAT_SUBJECT'}}($self, $pchat, \$packet->{'DATA'});
      }
      elsif($self->{'DEFAULT_HANDLERS'})
      {
        print "PCHAT(", $pchat->reference(), ") Subject set to: $packet->{'DATA'}\n";
      }
    }
  }

  return(1);
}

sub _handler
{
  my($self, $code_ref, $type) = @_;

  if(defined($code_ref))
  {
    if(ref($code_ref) eq 'CODE')
    {
      $self->{'HANDLERS'}->{$type} = $code_ref;
    }
  }

  return $self->{'HANDLERS'}->{$type};
}

sub _next_seqnum
{
  my($self) = shift;

  return $self->{'SEQNUM'}++;
}

sub agreement_handler     { return _handler($_[0], $_[1], 'AGREEMENT')     }
sub ban_handler           { return _handler($_[0], $_[1], 'BAN')           }
sub chat_handler          { return _handler($_[0], $_[1], 'CHAT')          }
sub chat_action_handler   { return _handler($_[0], $_[1], 'CHAT_ACTION')   }
sub color_handler         { return _handler($_[0], $_[1], 'COLOR')         }
sub event_loop_handler    { return _handler($_[0], $_[1], 'EVENT')         }
sub delete_file_handler   { return _handler($_[0], $_[1], 'FILE_DELETE')   }
sub get_file_handler      { return _handler($_[0], $_[1], 'FILE_GET')      }
sub put_file_handler      { return _handler($_[0], $_[1], 'FILE_PUT')      }
sub file_info_handler     { return _handler($_[0], $_[1], 'FILE_GET_INFO') }
sub file_list_handler     { return _handler($_[0], $_[1], 'FILE_LIST')     }
sub new_folder_handler    { return _handler($_[0], $_[1], 'FILE_MKDIR')    }
sub move_file_handler     { return _handler($_[0], $_[1], 'FILE_MOVE')     }
sub set_file_info_handler { return _handler($_[0], $_[1], 'FILE_SET_INFO') }
sub icon_handler          { return _handler($_[0], $_[1], 'ICON')          }
sub join_handler          { return _handler($_[0], $_[1], 'JOIN')          }
sub kick_handler          { return _handler($_[0], $_[1], 'KICK')          }
sub leave_handler         { return _handler($_[0], $_[1], 'LEAVE')         }
sub login_handler         { return _handler($_[0], $_[1], 'LOGIN')         }
sub msg_handler           { return _handler($_[0], $_[1], 'MSG')           }
sub news_handler          { return _handler($_[0], $_[1], 'NEWS')          }
sub post_news_handler     { return _handler($_[0], $_[1], 'NEWS_POST')     }
sub news_posted_handler   { return _handler($_[0], $_[1], 'NEWS_POSTED')   }
sub nick_handler          { return _handler($_[0], $_[1], 'NICK')          }
sub pchat_accept_handler  { return _handler($_[0], $_[1], 'PCHAT_ACCEPT')  }
sub pchat_action_handler  { return _handler($_[0], $_[1], 'PCHAT_ACTION')  }
sub pchat_chat_handler    { return _handler($_[0], $_[1], 'PCHAT_CHAT')    }
sub pchat_create_handler  { return _handler($_[0], $_[1], 'PCHAT_CREATE')  }
sub pchat_invite_handler  { return _handler($_[0], $_[1], 'PCHAT_INVITE')  }
sub pchat_join_handler    { return _handler($_[0], $_[1], 'PCHAT_JOIN')    }
sub pchat_leave_handler   { return _handler($_[0], $_[1], 'PCHAT_LEAVE')   }
sub pchat_subject_handler { return _handler($_[0], $_[1], 'PCHAT_SUBJECT') }
sub quit_handler          { return _handler($_[0], $_[1], 'QUIT')          }
sub send_msg_handler      { return _handler($_[0], $_[1], 'SEND_MSG')      }
sub server_msg_handler    { return _handler($_[0], $_[1], 'SERVER_MSG')    }
sub task_error_handler    { return _handler($_[0], $_[1], 'TASK_ERROR')    }
sub user_info_handler     { return _handler($_[0], $_[1], 'USER_GETINFO')  }
sub user_list_handler     { return _handler($_[0], $_[1], 'USER_LIST')     }

#
# Package subroutines
#

sub version { $Net::Hotline::Client::VERSION }

sub debug
{ 
  if(@_ == 1 && !ref($_[0]))
  {
    $Net::Hotline::Client::DEBUG = ($_[0]) ? 1 : 0;
  }
  elsif(@_ == 2 && ref($_[0]) eq 'Net::Hotline::Client')
  {
    $Net::Hotline::Client::DEBUG = ($_[1]) ? 1 : 0;
  }

  return $Net::Hotline::Client::DEBUG;
}

sub _hlc_write
{
  my($self, $fh, $data_ref, $len) = @_;

  return("0-E0")  if($len == 0 || !defined($len));
  
  unless(_write($fh, $data_ref, $len) == $len)
  {
    $self->{'LAST_ERROR'} = "Write error: $!";
    return;
  }

  return($len);
}

sub _hlc_read
{
  my($self, $fh, $data_ref, $len) = @_;

  return("0-E0")  if($len == 0 || !defined($len));

  unless(_read($fh, $data_ref, $len) == $len)
  {
    $self->{'LAST_ERROR'} = "Read error: $!";
    return;
  }

  return($len);
}

sub _hlc_buffered_read
{
  my($self, $fh, $data_ref, $len) = @_;

  return("0-E0")  if($len == 0 || !defined($len));

  unless(read($fh, $$data_ref, $len) == $len)
  {
    $self->{'LAST_ERROR'} = "Read error: $!";
    return;
  }

  return($len);
}

# Macbinary CRC perl code from Convert::BinHex by Eryq (eryq@enteract.com)
# (It needs access to the lexical @CRC_MAGIC, so it can't be auto-loaded)
sub macbin_crc
{
  shift if(ref($_[0]));

  my($len) = length($_[0]);
  my($crc) = $_[1];

  for(my $i = 0; $i < $len; $i++)
  {
    ($crc ^= (vec($_[0], $i, 8) << 8)) &= 0xFFFF;
    $crc = ($crc << 8) ^ $CRC_MAGIC[$crc >> 8];
  }
  return $crc;
}

#
# Satisfy autoloader's ridiculous *8-character* unique name limit :-/
#

sub get_filelist { al01_get_filelist(@_) }
sub get_fileinfo { al02_get_fileinfo(@_) }
sub get_userinfo { al03_get_userinfo(@_) }
sub user_by_nick { al04_user_by_nick(@_) }
sub req_userlist { al05_req_userlist(@_) }
sub req_filelist { al06_req_filelist(@_) }
sub pchat_action { al07_pchat_action(@_) }
sub get_file     { al08_get_file(@_)     }
sub put_file     { al09_put_file(@_)     }

# Internal functions that were also munged up:

# _al01_put_file_resume_now
# _al02_get_file_resume_now
# _al03_delete_file_now
# _al04_new_folder_now
# _al05_put_file_now
# _al06_put_file_resume
# _al07_get_file_now
# _al08_get_file_resume
# _al09_file_action_stub
# _al10_post_news_now
# _al11_pchat_invite_now
# _al12_pchat_accept_now
# _al13_comment_now

__END__

#
# Auto-loaded methods and subroutines
#

sub logged_in { $_[0]->{'LOGGED_IN'} }

sub connected
{
  (ref($_[0]->{'SERVER'}) && $_[0]->{'SERVER'}->opened()) ? 1 : 0;
}

sub _blocking_task
{
  my($self, $task_num) = @_;

  my($packet, $ret);

  $packet = new Net::Hotline::Protocol::Packet;

  while($ret = $packet->read_parse($self->{'SERVER'}, $self->{'BLOCKING'}))
  {
    _process_packet($self, $packet, $ret, 'blocking task');

    if($packet->{'TYPE'} == HTLS_HDR_TASK &&
       $packet->{'TASK_NUM'} == $task_num)
    {
      return($packet);
    }
  }
}

sub al01_get_filelist
{
  my($self, $path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->req_filelist($path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return(0);
  }

  $path = $task->path();
  $path = ""  unless(length($path));

  if(wantarray)
  {
    return @{$self->{'FILES'}->{$path}};
  }
  else
  {
    return $self->{'FILES'}->{$path};
  }
}

sub al06_req_filelist
{
  my($self, $path) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data, $task_num, @path_parts, $path_part, $data_length, $length,
     $save_path);

  $path =~ s/^$self->{'PATH_SEPARATOR'}//;
  $path =~ s/$self->{'PATH_SEPARATOR'}$//;

  if(length($path))
  {
    $save_path = $path;
    @path_parts = split($self->{'PATH_SEPARATOR'}, $path);
    $path =~ s/$self->{'PATH_SEPARATOR'}//g;

    if(length($path) > HTLC_MAX_PATHLEN)
    {
      croak("Maximum path length exceeded");
    }

    # 2 null bytes, the 1 byte for length, and the length of the path part
    $data_length = (3 * scalar(@path_parts)) + length($path);
    $length = SIZEOF_HL_LONG_HDR + $data_length;
  }
  else
  {
    $length = 2; # Two null bytes
  }

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_FILE_LIST);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len($length);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  if(length($path))
  {
    $data .= pack("n4", 0x0001,               # Number of atoms
                        HTLC_DATA_DIRECTORY,  # Atom type
                        $data_length + 2,     # Atom length

                        scalar(@path_parts)); # Number of path parts

    foreach $path_part (@path_parts)          # Path parts data
    {
      if(length($path_part) > HTLC_MAX_PATHLEN)
      {
        croak("Maximum path part length exceeded");
      }

      $data .= pack("nCa*", 0x0000,           # 2 null bytes
                            length $path_part,# Length
                            $path_part);      # Path part
    }
  }
  else
  {
    $data .= pack("n", 0x0000);
  }

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: FILE_LIST - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_FILE_LIST, time(), undef, $save_path);
    return($task_num);
  }
  else { return }
}

sub al03_get_userinfo
{
  my($self, $socket) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->req_userinfo($socket);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return $self->{'USER_LIST'}->{$task->socket()}->info();
}

sub req_userinfo
{
  my($self, $socket) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data, $task_num);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_USER_GETINFO);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_LONG_HDR);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n4", 0x0001,                  # Number of atoms

                     HTLC_DATA_SOCKET,        # Atom type
                     0x0002,                  # Atom length
                     $socket);                # Atom data

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: USER_GETINFO - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_USER_INFO, time(), $socket);
    return($task_num);
  }
  else { return }
}

sub al02_get_fileinfo
{
  my($self, $path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->req_fileinfo($path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return $self->{'FILE_INFO'};
}

sub req_fileinfo
{
  return _file_action_simple($_[0], $_[1], HTLC_HDR_FILE_GETINFO, HTLC_TASK_FILE_INFO, 'GET FILE INFO');
}

sub delete_file
{
  my($self, $path) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al03_delete_file_now($path); 
  }
  else
  {
    return $self->_delete_file($path); 
  }
}

sub _al03_delete_file_now
{
  my($self, $path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_delete_file($path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _delete_file
{
  return _file_action_simple($_[0], $_[1], HTLC_HDR_FILE_DELETE, HTLC_TASK_FILE_DELETE, 'DELETE FILE');
}

sub new_folder
{
  my($self, $path) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al04_new_folder_now($path); 
  }
  else
  {
    return $self->_new_folder($path); 
  }
}

sub _al04_new_folder_now
{
  my($self, $path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_new_folder($path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _new_folder
{
  return _file_action_simple($_[0], $_[1], HTLC_HDR_FILE_MKDIR, HTLC_TASK_FILE_MKDIR, 'NEW FOLDER');
}

sub al09_put_file
{
  my($self, $src_path, $dest_path, $comments) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al05_put_file_now($src_path, $dest_path, $comments); 
  }
  else
  {
    return $self->_put_file($src_path, $dest_path, $comments);
  }
}

sub _al05_put_file_now
{
  my($self, $src_path, $dest_path, $comments) = @_;

  my($task, $task_num, $packet, $size);

  $task_num = $self->_put_file($src_path, $dest_path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  $size = ${$task->misc()}[0] + ${$task->misc()}[1];

  if(wantarray)
  {
    return($task, $packet->{'HTXF_REF'}, $size);
  }
  else
  {
    return [ $task, $packet->{'HTXF_REF'}, $size ];
  }
}

sub _put_file
{
  my($self, $src_path, $dest_path, $comments) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  croak("Not connected.")  unless($server->opened());

  unless(-e $src_path)
  {
    $self->{'LAST_ERROR'} = "File does not exist: $src_path";
    return;
  }

  my($local_sep, $remote_sep, $src_file, $data, $task_num, $length,
     $num_atoms, $data_len, $rsrc_len, $finder_flags, $type, $creator);

  $local_sep = PATH_SEPARATOR;
  $remote_sep = $self->{'PATH_SEPARATOR'};

  ($src_file = $src_path) =~ s/.*?$local_sep([^$local_sep]+)$/$1/o;
  $dest_path = "$dest_path$remote_sep$src_file";

  ($data, $task_num) = _al09_file_action_stub($self, $dest_path, HTLC_HDR_FILE_PUT);

  # Set new length: old length plus 8 bytes for the size atom
  $length    = (unpack("N", substr($data, 16, 4)) + 8);
  substr($data, 16, 4) = pack("N", $length);
  substr($data, 12, 4) = pack("N", $length);

  # Set new num atoms: old num atoms + 1
  $num_atoms = (unpack("n", substr($data, 20, 2)) + 1);
  substr($data, 20, 2) = pack("n", $num_atoms);

  # Fork lengths
  $data_len = (stat($src_path))[7];
  $rsrc_len = 0;

  # Mac OS specific information: resource fork length and finder comments
  if($self->{'MACOS'})
  {
    my($fsspec, $finder_comments, $res_fd, $rsrc_fh, $cat, $finfo);

    $fsspec = MacPerl::MakeFSSpec($src_path);

    # Get finder comments
    unless(defined($comments))
    {
      $finder_comments = Mac::MoreFiles::FSpDTGetComment($fsspec);
      $comments = $finder_comments  if(length($finder_comments));
    }

    $cat   = Mac::Files::FSpGetCatInfo($fsspec);
    $finfo = $cat->ioFlFndrInfo();

    # Get finder flags, type, and creator
    $finder_flags = $finfo->fdFlags();
    $type         = $finfo->fdType();
    $creator      = $finfo->fdCreator();

    # Protect from compile-time errors on non-Mac OS systems that don't
    # define O_RSRC in Fcntl
    eval '$res_fd = POSIX::open($src_path, O_RDONLY | O_RSRC)';

    $rsrc_fh = new IO::File;

    unless($rsrc_fh->fdopen($res_fd, "r"))
    {
      $self->{'LAST_ERROR'} = "Couldn't open Mac resource fork: $@";
      return;    
    }

    $rsrc_fh->seek(0, SEEK_END);    # Fast forward to end
    $rsrc_len = $rsrc_fh->tell();   # Get size
    $rsrc_fh->seek(0, SEEK_SET);    # Rewind
  }
  else
  {
    ($type, $creator) = ("BINA", "????");
  }

  # Total length of the upload to come: 111 bytes for type/creator/etc.
  # + 1 byte for the file name length + the file name + 2 bytes for the
  # comments length + the comments + 2 fork headers + the size of the
  # file to be uploaded (size of data fork plus size of resource fork).
  $length = (SIZEOF_HL_FILE_UPLOAD_HDR + 1 + length($src_file) + 2 +
             length($comments) + (2 * SIZEOF_HL_FILE_FORK_HDR) +
             $data_len + $rsrc_len);

  # 00 00 00 CB  00 00 00 06  00 00 00 00  00 00 00 21  ...............!
  # 00 00 00 21  00 03 00 C9  00 05 74 65  78 74 32 00  ...!......text2.
  # CA 00 0C 00  01 00 00 07  55 70 6C 6F  61 64 73 00  ........Uploads.
  # 6C 00 02 03  94                                     l....

  # Add size argument
  $data .= pack("nnN", HTLC_DATA_HTXF_SIZE,   # Atom type
                       0x0004,                # Atom length
                       $length);              # Atom data

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: PUT FILE - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_FILE_PUT, time(), undef,
                             [ $src_path, $dest_path ],
                             [ $data_len, $rsrc_len, $comments, $finder_flags,
                               $type, $creator, $length ]);
    return($task_num);
  }
  else { return }
}

sub put_file_resume
{
  my($self, $src_path, $dest_path, $comments) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al01_put_file_resume_now($src_path, $dest_path, $comments); 
  }
  else
  {
    return $self->_al06_put_file_resume($src_path, $dest_path, $comments); 
  }
}

sub _al01_put_file_resume_now
{
  my($self, $src_path, $dest_path, $comments) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_al06_put_file_resume($src_path, $dest_path, $comments);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  if(wantarray)
  {
    return($task, $packet->{'HTXF_REF'}, ${$task->misc()}[6], $packet->{'HTXF_RFLT'});
  }
  else
  {
    return [ $task, $packet->{'HTXF_REF'}, ${$task->misc()}[6], $packet->{'HTXF_RFLT'} ];
  }
}

sub _al06_put_file_resume
{
  my($self, $src_path, $dest_path, $comments) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  croak("Not connected.")  unless($server->opened());

  unless(-e $src_path)
  {
    $self->{'LAST_ERROR'} = "File does not exist: $src_path";
    return;
  }

  my($local_sep, $remote_sep, $src_file, $data, $task_num, $length,
     $num_atoms, $data_len, $rsrc_len, $finder_flags, $type, $creator);

  $local_sep = PATH_SEPARATOR;
  $remote_sep = $self->{'PATH_SEPARATOR'};

  ($src_file = $src_path) =~ s/.*?$local_sep([^$local_sep]+)$/$1/o;
  $dest_path = "$dest_path$remote_sep$src_file";

  ($data, $task_num) = _al09_file_action_stub($self, $dest_path, HTLC_HDR_FILE_PUT);

  # Add upload resume magic
  $data .= HTXF_RESUME_MAGIC;

  # Set new length: old length plus the length of HTXF_RESUME_MAGIC
  $length    = (unpack("N", substr($data, 16, 4)) + length(HTXF_RESUME_MAGIC));
  substr($data, 16, 4) = pack("N", $length);
  substr($data, 12, 4) = pack("N", $length);

  # Set new num atoms: old num atoms + 1
  $num_atoms = (unpack("n", substr($data, 20, 2)) + 1);
  substr($data, 20, 2) = pack("n", $num_atoms);

  # Fork lengths
  $data_len = (stat($src_path))[7];
  $rsrc_len = 0;

  # Mac OS specific information: resource fork length and finder comments
  if($self->{'MACOS'})
  {
    my($fsspec, $finder_comments, $res_fd, $rsrc_fh, $cat, $finfo);

    $fsspec = MacPerl::MakeFSSpec($src_path);

    # Get finder comments
    unless(defined($comments))
    {
      $finder_comments = Mac::MoreFiles::FSpDTGetComment($fsspec);
      $comments = $finder_comments  if(length($finder_comments));
    }

    $cat   = Mac::Files::FSpGetCatInfo($fsspec);
    $finfo = $cat->ioFlFndrInfo();

    # Get finder flags, type, and creator
    $finder_flags = $finfo->fdFlags();
    $type         = $finfo->fdType();
    $creator      = $finfo->fdCreator();

    # Protect from compile-time errors on non-Mac OS systems that don't
    # define O_RSRC in Fcntl
    eval '$res_fd = POSIX::open($src_path, O_RDONLY | O_RSRC)';

    $rsrc_fh = new IO::File;

    unless($rsrc_fh->fdopen($res_fd, "r"))
    {
      $self->{'LAST_ERROR'} = "Couldn't open Mac resource fork: $@";
      return;    
    }

    $rsrc_fh->seek(0, SEEK_END);    # Fast forward to end
    $rsrc_len = $rsrc_fh->tell();   # Get size
    $rsrc_fh->seek(0, SEEK_SET);    # Rewind
  }
  else
  {
    ($type, $creator) = ("BINA", "????");
  }

  # Total length of the upload to come: 111 bytes for type/creator/etc.
  # + 1 byte for the file name length + the file name + 2 bytes for the
  # comments length + the comments + 2 fork headers + the size of the
  # file to be uploaded (size of data fork plus size of resource fork).
  $length = (SIZEOF_HL_FILE_UPLOAD_HDR + 1 + length($src_file) + 2 +
             length($comments) + (2 * SIZEOF_HL_FILE_FORK_HDR) +
             $data_len + $rsrc_len);

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: PUT FILE - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_FILE_PUT, time(), undef,
                             [ $src_path, $dest_path ],
                             [ $data_len, $rsrc_len, $comments, $finder_flags,
                               $type, $creator, $length ]);
    return($task_num);
  }
  else { return }
}

sub al08_get_file
{
  my($self, $path) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al07_get_file_now($path); 
  }
  else
  {
    return $self->_get_file($path); 
  }
}

sub _al07_get_file_now
{
  my($self, $path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_get_file($path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  if(wantarray)
  {
    return(($task, $packet->{'HTXF_REF'},  $packet->{'HTXF_SIZE'}));
  }
  else
  {
    return [ $task, $packet->{'HTXF_REF'},  $packet->{'HTXF_SIZE'} ];
  }
}

sub _get_file
{
  my($self, $path) = @_;

  my($local_sep, $remote_sep, $dest_dir, $task_num, $data_file, $rsrc_file);

  $local_sep  = PATH_SEPARATOR;
  $remote_sep = $self->{'PATH_SEPARATOR'};

  $dest_dir = $self->{'DOWNLOADS_DIR'};
  $dest_dir .= $local_sep  if($dest_dir =~ /\S/ && $dest_dir !~ /$local_sep$/o);

  ($data_file = $path) =~ s/.*?$remote_sep([^$remote_sep]+)$/$1/;

  if($self->{'MACOS'})
  {
    $rsrc_file = undef;
  }
  else
  {
    $rsrc_file = "$data_file$self->{'RSRC_FORK_EXT'}";
    $data_file = "$data_file$self->{'DATA_FORK_EXT'}";
  }

  $task_num = _file_action_simple($self, $path, HTLC_HDR_FILE_GET, HTLC_TASK_FILE_GET, 'GET FILE');

  return  unless(defined($task_num));

  $self->{'TASKS'}->{$task_num}->path([ $path, $data_file, $rsrc_file ]);

  return($task_num);
}

sub get_file_resume
{
  my($self, $path) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al02_get_file_resume_now($path); 
  }
  else
  {
    return $self->_al08_get_file_resume($path); 
  }
}

sub _al02_get_file_resume_now
{
  my($self, $path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_al08_get_file_resume($path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  if(wantarray)
  {
    return(($task, $packet->{'HTXF_REF'},  $packet->{'HTXF_SIZE'}));
  }
  else
  {
    return [ $task, $packet->{'HTXF_REF'},  $packet->{'HTXF_SIZE'} ];
  }
}

sub _al08_get_file_resume
{
  my($self, $path) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  croak("Not connected.")  unless($server->opened());

  my($local_sep, $remote_sep, $dest_dir, $data, $more_data, $task_num,
     $length, $data_file, $data_pos, $rsrc_file, $rsrc_pos);

  $local_sep  = PATH_SEPARATOR;
  $remote_sep = $self->{'PATH_SEPARATOR'};

  $dest_dir = $self->{'DOWNLOADS_DIR'};
  $dest_dir .= $local_sep  if($dest_dir =~ /\S/ && $dest_dir !~ /$local_sep$/o);

  ($data, $task_num) = _al09_file_action_stub($self, $path, HTLC_HDR_FILE_GET);

  $data_file = $path;
  
  if($data_file =~ /$remote_sep([^$remote_sep]+)$/)
  {
    $data_file = "$dest_dir$1";
  }
  else
  {
    $data_file = "$dest_dir$data_file";
  }

  if($self->{'MACOS'})
  {
    $rsrc_file = undef;
  }
  else
  {
    $rsrc_file = "$data_file$self->{'RSRC_FORK_EXT'}";
    $data_file = "$data_file$self->{'DATA_FORK_EXT'}";
  }

  unless(-e $data_file || -e $rsrc_file)
  {
    $self->{'LAST_ERROR'} = "Can't resume download: partial download does not exist.";
    return;
  }

  # Get data fork position
  $data_pos = (stat($data_file))[7];

  # Get resource fork position
  if($self->{'MACOS'})
  {
    my($res_fd, $rsrc_fh);

    # Protect from compile-time errors on non-Mac OS systems that don't
    # define O_RSRC in Fcntl
    eval '$res_fd = POSIX::open($data_file, O_RDONLY | O_RSRC)';

    $rsrc_fh = new IO::File;

    unless($rsrc_fh->fdopen($res_fd, "r"))
    {
      $self->{'LAST_ERROR'} = "Couldn't open Mac resource fork: $@";
      return;    
    }

    $rsrc_fh->seek(0, SEEK_END);    # Fast forward to end
    $rsrc_pos = $rsrc_fh->tell();   # Get size
    $rsrc_fh->seek(0, SEEK_SET);    # Rewind
  }
  else
  {
    $rsrc_pos = (stat($rsrc_file))[7];
  }

  $length = unpack("N", substr($data, 16, 4));
  $length += 78;

  # Set new length
  substr($data, 12, 4)  = pack("N", $length);
  substr($data, 16, 4) = pack("N", $length);

  # Set new num atoms
  my($num_atoms) = unpack("n", substr($data, 20, 2));
  substr($data, 20, 2) = pack("n", $num_atoms + 1);

  # 00 CB 00 4A  52 46 4C 54  00 01 00 00  00 00 00 00  ...JRFLT........
  # 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
  # 00 00 00 00  00 00 00 00  00 00 00 00  00 02 44 41  ..............DA
  # 54 41 00 00  1B EA 00 00  00 00 00 00  00 00 4D 41  TA............MA
  # 43 52 00 00  00 00 00 00  00 00 00 00  00 00        CR............
  $more_data = pack("x78");

  substr($more_data, 0, 2) = pack("n", HTLC_DATA_RFLT);
  substr($more_data, 2, 2) = pack("n", 0x004A);
  substr($more_data, 4, 4) = HTXF_RFLT_MAGIC;
  substr($more_data, 8, 2) = pack("n", 0x0001);

  substr($more_data, 45, 1) = pack("C", 0x02);
  substr($more_data, 46, 4) = 'DATA';
  substr($more_data, 50, 4) = pack("N", $data_pos);

  substr($more_data, 62, 4) = 'MACR';
  substr($more_data, 66, 4) = pack("N", $rsrc_pos);

  $data .= $more_data;

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: GET FILE - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_FILE_GET, time(), undef, 
                             [ $path, $data_file, $rsrc_file ]);
    return($task_num);
  }
  else { return }
}

sub _al09_file_action_stub
{
  my($self, $path, $type) = @_;

  my($data, @path_parts, $length, $file, $dir_len);

  $path =~ s/^$self->{'PATH_SEPARATOR'}//;
  $path =~ s/$self->{'PATH_SEPARATOR'}$//;
  @path_parts = split($self->{'PATH_SEPARATOR'}, $path);
  $path =~ s/$self->{'PATH_SEPARATOR'}//g;

  if(length($path) > HTLC_MAX_PATHLEN)
  {
    croak("Maximum path length exceeded");
  }

  $file = pop(@path_parts);

  # File part: 2 bytes num atoms, 2 bytes for atom len,
  # 2 bytes for file name length
  $length = (2 + 2 + 2 + length($file));

  if(@path_parts)
  {
    $dir_len = length(join('', @path_parts));
    # Path part: 2 bytes for atom type, 2 bytes for atom len
    # 2 bytes for num path components, and 2 null bytes and
    # 1 byte path part length for each path part
    $length += (2 + 2 + 2 + (3 * @path_parts));
    $length += $dir_len;
  }

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type($type);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len($length);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  $data .= pack("n3a*", @path_parts ? 2 : 1,  # Number of atoms
                        HTLC_DATA_FILE,       # Atom type
                        length($file),        # Atom length
                        $file);               # Atom data

  if(@path_parts)
  {
    $data .= pack("n3", HTLC_DATA_DIRECTORY,  # Atom type
                        $dir_len + 2 + (3 * scalar(@path_parts)),
                                              # Atom length
                        scalar(@path_parts)); # Num path parts

    my($path_part);

    foreach $path_part (@path_parts)          # Path parts data
    {
      if(length($path_part) > HTLC_MAX_PATHLEN)
      {
        croak("Maximum path part length exceeded");
      }

      $data .= pack("nCa*", 0x0000,            # 2 null bytes
                            length($path_part),# Length
                            $path_part);       # Path part
    }
  }

  return($data, $proto_header->seq());
}

sub _file_action_simple
{
  my($self, $path, $type, $task_type, $task_name) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && length($path));

  my($data, $task_num) = _al09_file_action_stub($self, $path, $type);

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: $task_name - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, $task_type, time(), undef, $path);
    return($task_num);
  }
  else { return }
}

sub move
{
  my($self, $src_path, $dest_path) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_move_now($src_path, $dest_path); 
  }
  else
  {
    return $self->_move($src_path, $dest_path);
  }
}

sub _move_now
{
  my($self, $src_path, $dest_path) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_move($src_path, $dest_path);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _move
{
  my($self, $src_path, $dest_path) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && length($src_path)  && length($dest_path));

  my($data, $task_num, $length, $num_atoms);
  my(@src_path_parts, $save_src_path, $src_file, $src_dir_len);
  my(@dest_path_parts, $save_dest_path, $dest_dir_len);

  # Source:

  $src_path =~ s/^$self->{'PATH_SEPARATOR'}//;
  $src_path =~ s/$self->{'PATH_SEPARATOR'}$//;
  $save_src_path = $src_path;
  @src_path_parts = split($self->{'PATH_SEPARATOR'}, $src_path);
  $src_path =~ s/$self->{'PATH_SEPARATOR'}//g;

  if(length($src_path) > HTLC_MAX_PATHLEN)
  {
    croak("Maximum path length exceeded");
  }

  $src_file = pop(@src_path_parts);

  # Source part: 2 bytes num atoms, 2 bytes for atom type,
  # 2 bytes for file name length
  $length = (2 + 2 + 2 + length($src_file));

  if(@src_path_parts)
  {
    $src_dir_len = length(join('', @src_path_parts));
    # Path part: 2 bytes for atom type, 2 bytes for atom len
    # 2 bytes for num path components, and 2 null bytes and
    # 1 byte path part length for each path part
    $length += (2 + 2 + 2 + (3 * @src_path_parts));
    $length += $src_dir_len;
  }

  # Destination:

  $dest_path =~ s/^$self->{'PATH_SEPARATOR'}//;
  $dest_path =~ s/$self->{'PATH_SEPARATOR'}$//;
  $save_dest_path = $dest_path;
  @dest_path_parts = split($self->{'PATH_SEPARATOR'}, $dest_path);
  $dest_path =~ s/$self->{'PATH_SEPARATOR'}//g;

  if(length($dest_path) > HTLC_MAX_PATHLEN)
  {
    croak("Maximum path length exceeded");
  }

  if(@dest_path_parts)
  {
    $dest_dir_len = length(join('', @dest_path_parts));
    # Path part: 2 bytes for atom type, 2 bytes for atom len
    # 2 bytes for num path components, and 2 null bytes and
    # 1 byte path part length for each path part
    $length += (2 + 2 + 2 + (3 * @dest_path_parts));
    $length += $dest_dir_len;
  }

  # Build packet

  if(@src_path_parts && @dest_path_parts) { $num_atoms = 3 }
  else                                    { $num_atoms = 2 }

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_FILE_MOVE);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len($length);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  $data .= pack("n3a*", $num_atoms,           # Number of atoms
                        HTLC_DATA_FILE,       # Atom type
                        length($src_file),    # Atom length
                        $src_file);           # Atom data

  if(@src_path_parts)
  {
    $data .= pack("n3", HTLC_DATA_DIRECTORY,  # Atom type
                        $src_dir_len + 2 + (3 * scalar(@src_path_parts)),
                                              # Atom length
                        scalar(@src_path_parts));
                                              # Num path parts

    my($path_part);

    foreach $path_part (@src_path_parts)      # Path parts data
    {
      if(length($path_part) > HTLC_MAX_PATHLEN)
      {
        croak("Maximum path part length exceeded");
      }

      $data .= pack("nCa*", 0x0000,           # 2 null bytes
                            length $path_part,# Length
                            $path_part);      # Path part
    }
  }

  if(@dest_path_parts)
  {
    $data .= pack("n3", HTLC_DATA_DESTDIR,    # Atom type
                        $dest_dir_len + 2 + (3 * scalar(@dest_path_parts)),
                                              # Atom length
                        scalar(@dest_path_parts));
                                              # Num path parts

    my($path_part);

    foreach $path_part (@dest_path_parts)     # Path parts data
    {
      if(length($path_part) > HTLC_MAX_PATHLEN)
      {
        croak("Maximum path part length exceeded");
      }

      $data .= pack("nCa*", 0x0000,           # 2 null bytes
                            length $path_part,# Length
                            $path_part);      # Path part
    }
  }

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: MOVE FILE - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_FILE_MOVE, time(),
                         undef, [ $save_src_path, $save_dest_path ]);
    return($task_num);
  }
  else { return }
}

sub rename
{
  my($self, $path, $new_name) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_rename_now($path, $new_name); 
  }
  else
  {
    return $self->_rename($path, $new_name); 
  }
}

sub _rename_now
{
  my($self, $path, $new_name) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->rename($path, $new_name);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _rename
{
  my($self, $path, $new_name) = @_;

  return undef  unless(length($path) && length($new_name));
  return _change_file_info($self, $path, $new_name, undef);
}

sub comment
{
  my($self, $path, $comments) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al13_comment_now($path, $comments); 
  }
  else
  {
    return $self->_comment($path, $comments); 
  }
}

sub _al13_comment_now
{
  my($self, $path, $comments) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->comment($path, $comments);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _comment
{
  my($self, $path, $comments) = @_;

  return undef  unless(length($path));
  $comments = ""  unless(defined($comments));
  return _change_file_info($self, $path, undef, $comments);
}

sub _change_file_info
{
  my($self, $path, $name, $comments) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data, $task_num, @path_parts, $length, $save_path, $file,
     $dir_len, $num_atoms);

  $path =~ s/^$self->{'PATH_SEPARATOR'}//;
  $path =~ s/$self->{'PATH_SEPARATOR'}$//;
  $save_path = $path;
  @path_parts = split($self->{'PATH_SEPARATOR'}, $path);
  $path =~ s/$self->{'PATH_SEPARATOR'}//g;

  if(length($path) > HTLC_MAX_PATHLEN)
  {
    croak("Maximum path length exceeded");
  }

  $file = pop(@path_parts);

  # File part: 2 bytes for num atoms, 2 bytes for atom type,
  # 2 bytes for file name length
  $length = (2 + 2 + 2 + length($file));

  if(@path_parts)
  {
    $dir_len = length(join('', @path_parts));
    # Path part: 2 bytes for atom type, 2 bytes for atom len
    # 2 bytes for num path components, and 2 null bytes and
    # 1 byte path part length for each path part
    $length += (2 + 2 + 2 + (3 * @path_parts));
    $length += $dir_len;
  }

  if(length($name))
  {
    # Name part: 2 bytes for atom type, 2 bytes for
    # atom len, and the new name
    $length += (2 + 2 + length($name));
  }

  if(defined($comments))
  {
    # Comments part: 2 bytes for atom type, 2 bytes for
    # atom len, length of the new comments, else 1 null
    # byte if removing comments.
    $length += 2 + 2;
    if(length($comments)) { $length += length($comments) }
    else                  { $length += 1                 }
  }

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_FILE_SETINFO);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len($length);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  $num_atoms = (@path_parts) ? 2 : 1;
  $num_atoms++  if(length($name));
  $num_atoms++  if(defined($comments));

  $data .= pack("n3a*", $num_atoms,           # Number of atoms
                        HTLC_DATA_FILE,       # Atom type
                        length($file),        # Atom length
                        $file);               # Atom data

  if(@path_parts)
  {
    $data .= pack("n3", HTLC_DATA_DIRECTORY,  # Atom type
                        $dir_len + 2 + (3 * scalar(@path_parts)),
                                              # Atom length
                        scalar(@path_parts)); # Num path parts

    my($path_part);

    foreach $path_part (@path_parts)          # Path parts data
    {
      if(length($path_part) > HTLC_MAX_PATHLEN)
      {
        croak("Maximum path part length exceeded");
      }

      $data .= pack("nCa*", 0x0000,           # 2 null bytes
                            length $path_part,# Length
                            $path_part);      # Path part
    }
  }

  if(length($name))
  {
    $data .= pack("nna*", HTLC_DATA_FILE_RENAME,# Atom type
                          length($name),      # Length
                          $name);             # Name
  }

  if(defined($comments))
  {
    $data .= pack("n", HTLS_DATA_FILE_COMMENT);# Atom type

    if(length($comments))
    {
      $data .= pack("na*", length($comments), # Length
                           $comments);        # Comments
    }
    else # Remove comments
    {
      $data .=  pack("nx", 0x0001);           # Length + null byte
    }
  }

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: SET INFO - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_SET_INFO, time(), undef, $save_path);
    return($task_num);
  }
  else { return }
}

sub post_news
{
  my($self, @post) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al10_post_news_now(@post); 
  }
  else
  {
    return $self->_post_news(@post); 
  }
}

sub _al10_post_news_now
{
  my($self, @post) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->post_news(@post);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _post_news
{
  my($self, @post) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($post) = join('', @post);

  my($data, $task_num);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_NEWS_POST);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_SHORT_HDR + length($post));
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n3a*", 0x0001,                # Number of atoms
                       HTLS_DATA_NEWS_POST,   # Atom type
                       length($post),         # Atom length
                       $post);                # Atom data

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: POST NEWS - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_NEWS_POST, time());
  }
  else { return }

  return($task_num);
}

sub get_news
{
  my($self) = shift;

  my($task, $task_num, $packet);

  $task_num = $self->req_news();
  $task = $self->{'TASKS'}->{$task_num};

  return(undef)  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return(undef);
  }

  if(wantarray)
  {
    return @{$self->{'NEWS'}};
  }
  else
  {
    return (@{$self->{'NEWS'}}) ? join('_' x 58, @{$self->{'NEWS'}}) : "";
  }
}

sub req_news
{
  my($self) = shift;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data, $task_num);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_NEWS_GETFILE);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_TASK_FILLER);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n", 0x0000);

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: NEWS - $task_num\n");
    $self->{'TASKS'}->{$task_num} = 
      new Net::Hotline::Task($task_num, HTLC_TASK_NEWS, time());
    return($task_num);
  }
  else { return }
}

sub al04_user_by_nick
{
  my($self, $nick_match) = @_;

  my($socket, @users);

  eval { m/$nick_match/ };

  return undef  if($@ || !$self->{'USER_LIST'} || length($nick_match) == 0);

  foreach $socket (sort { $a <=> $b } keys(%{$self->{'USER_LIST'}}))
  {
    if($self->{'USER_LIST'}->{$socket}->nick() =~ /^$nick_match$/)
    {
      if(wantarray())
      {
        push(@users, $self->{'USER_LIST'}->{$socket});
      }
      else
      {
        return $self->{'USER_LIST'}->{$socket};
      }
    }
  }

  if(@users) { return @users }
  else       { return }
}

sub user_by_socket
{
  my($self, $socket) = @_;
  return $self->{'USER_LIST'}->{$socket};
}

sub icon
{
  my($self, $icon) = @_;

  return $self->{'ICON'}  unless($icon =~ /^-?\d+$/);

  return _update_user($self, $icon, $self->{'NICK'});
}

sub nick
{
  my($self, $nick) = @_;

  return $self->{'NICK'}  unless(defined($nick));

  return _update_user($self, $self->{'ICON'}, $nick);
}

sub _update_user
{
  my($self, $icon, $nick) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_USER_CHANGE);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len((SIZEOF_HL_SHORT_HDR * 2) + length($nick));
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n6a*", 0x0002,                # Num atoms

                       HTLC_DATA_ICON,        # Atom type
                       0x0002,                # Atom length
                       $icon,                 # Atom data

                       HTLC_DATA_NICKNAME,    # Atom type
                       length($nick),         # Atom length
                       $nick);                # Atom data

  $self->{'NICK'} = $nick;
  $self->{'ICON'} = $icon;

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

sub get_userlist
{
  my($self) = shift;

  my($task, $task_num, $packet);

  $task_num = $self->req_userlist();
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return $self->{'USER_LIST'};
}

sub al05_req_userlist
{
  my($self) = shift;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data, $task_num);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_USER_GETLIST);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_TASK_FILLER);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n", 0x0000);

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: GET USER LIST - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_USER_LIST, time());
    return($task_num);
  }
  else { return }
}

sub kick
{
  my($self, $user_or_socket) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_kick_now($user_or_socket); 
  }
  else
  {
    return $self->_kick($user_or_socket); 
  }
}

sub _kick_now
{
  my($self, $user_or_socket) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_kick($user_or_socket);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _kick
{
  my($self, $user_or_socket) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($socket, $task_num);

  if(ref($user_or_socket)) { $socket = $user_or_socket->socket() }
  else                     { $socket = $user_or_socket           }

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_USER_KICK);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_LONG_HDR);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n4", 0x0001,                  # Num atoms

                     HTLC_DATA_SOCKET,        # Atom type
                     0x0002,                  # Atom length
                     $socket);                # Atom data

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: KICK($socket) - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_KICK, time());
  }
  else { return }

  return ($task_num);
}

sub ban
{
  my($self, $user_or_socket) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_ban_now($user_or_socket); 
  }
  else
  {
    return $self->_ban($user_or_socket); 
  }
}

sub _ban_now
{
  my($self, $user_or_socket) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_ban($user_or_socket);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _ban
{
  my($self, $user_or_socket) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($socket, $task_num);

  if(ref($user_or_socket)) { $socket = $user_or_socket->socket() }
  else                     { $socket = $user_or_socket           }

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_USER_KICK);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_LONG_HDR + 6);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n7", 0x0002,                  # Num atoms

                     HTLC_DATA_SOCKET,        # Atom type
                     0x0002,                  # Atom length
                     $socket,                 # Atom data

                     HTLC_DATA_BAN,           # Atom type
                     0x0002,                  # Atom length
                     0x0001);                 # Atom data (always 1???)

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: BAN($socket) - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_BAN, time());
  }
  else { return }
  
  return ($task_num);
}

sub msg
{
  my($self, $user_or_socket, @message) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_msg_now($user_or_socket, @message); 
  }
  else
  {
    return $self->_msg($user_or_socket, @message); 
  }
}

sub _msg_now
{
  my($self, $user_or_socket, @message) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_msg($user_or_socket, @message);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _msg
{
  my($self, $user_or_socket, @message) = @_;

  my($message) = join('', @message);

  $message =~ s/\n/@{[HTLC_NEWLINE]}/osg;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($socket);

  if(ref($user_or_socket)) { $socket = $user_or_socket->socket() }
  else                     { $socket = $user_or_socket           }

  my($data, $task_num);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_MSG);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len((SIZEOF_HL_SHORT_HDR * 2) +
                     length($message));
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n6", 0x0002,                  # Num atoms

                     HTLC_DATA_SOCKET,        # Atom type
                     0x0002,                  # Atom length
                     $socket,                 # Atom data

                     HTLC_DATA_MSG,           # Atom type
                     length($message)) .      # Atom length
          $message;                           # Atom data

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: MSG - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_SEND_MSG, time());
  }
  else { return }

  return($task_num);
}

sub chat_action
{
  my($self, @message) = @_;

  my($message) = join('', @message);

  $message =~ s/\n/@{[HTLC_NEWLINE]}/osg;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_CHAT);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len((SIZEOF_HL_SHORT_HDR  * 2) +
                     length($message));
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n6", 0x0002,                  # Num atoms

                     HTLC_DATA_OPTION,        # Atom type
                     0x0002,                  # Atom length
                     0x0001,                  # Atom data

                     HTLC_DATA_CHAT,          # Atom type
                     length($message)) .      # Atom length
          $message;                           # Atom data

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

sub chat
{
  my($self, @message) = @_;

  my($message) = join('', @message);

  $message =~ s/\n/@{[HTLC_NEWLINE]}/osg;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_CHAT);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(SIZEOF_HL_SHORT_HDR +
                     length($message));
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header() .
          pack("n3", 0x0001,                  # Num atoms

                     HTLC_DATA_CHAT,          # Atom type
                     length($message)) .      # Atom length
          $message;                           # Atom data

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

sub send_file
{
  my($self, $task, $ref, $size, $resume) = @_;

  my($server, $port, $data, $xfer, $length, $buf_size);
  my($local_sep, $remote_sep, $filename, $src_path, $dest_path);
  my($type, $creator, $created, $modified, $finder_flags,  $comments,
     $data_fh, $rsrc_fh, $data_len, $rsrc_len, $data_pos, $rsrc_pos,
     $res_fd);

  $task->finish(undef);

  $local_sep = PATH_SEPARATOR;

  $buf_size = $self->{'HTXF_BUFSIZE'};

  if($resume)
  {
    # 52 46 4c 54  00 01 00 00  00 00 00 00  00 00 00 00  RFLT............
    # 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
    # 00 00 00 00  00 00 00 00  00 02 44 41  54 41 00 06  ..........DATA..
    # 9a cf 00 00  00 00 00 00  00 00 4d 41  43 52 00 00  ..........MACR..
    # 00 00 00 00  00 00 00 00  00 00                     ..........

    unless(substr($resume, 0, 4) eq 'RFLT')
    {
      $task->error(1);
      $task->finish(time());
      $task->error_text("Bad data from server!");
      $self->{'LAST_ERROR'} = $task->error_text();
      return;
    }

    $data_pos = unpack("N", substr($resume, 46, 4));
    $rsrc_pos = unpack("N", substr($resume, 62, 4));
  }

  $data_fh = new IO::File;
  $rsrc_fh = new IO::File;

  ($src_path, $dest_path) = @{$task->path()};

  ($filename = $src_path) =~ s/^.*?$local_sep([^$local_sep]+)$/$1/;

  ($data_len, $rsrc_len, $comments, $finder_flags, $type, $creator, $length)
    = @{$task->misc()};

  unless($data_fh->open($src_path))
  {
    $task->error(1);
    $task->finish(time());
    $task->error_text("Could not open to $src_path: $!");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  if($self->{'MACOS'})
  {
    # Protect from compile-time errors on non-Mac OS systems that don't
    # define O_RSRC in Fcntl
    eval '$res_fd = POSIX::open($src_path, O_RDONLY | O_RSRC)';

    unless($rsrc_fh->fdopen($res_fd, "r"))
    {
      $task->error(1);
      $task->finish(time());
      $task->error_text("Could not read to resource fork from $src_path: $!");
      $self->{'LAST_ERROR'} = $task->error_text();
      return;
    }
  }
  elsif($rsrc_len > 0 || ($resume && $rsrc_pos > 0))
  {
    $task->error(1);
    $task->finish(time());
    $task->error_text("Server is expecting resource fork data from a non-Mac OS client!\n" .
                      "Are you sure you're uploading the right file?");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  if($resume)
  {
    if($rsrc_pos > 0)
    {
      unless($rsrc_fh->seek($rsrc_pos, 0))
      {
        $task->error(1);
        $task->finish(time());
        $task->error_text("Could not seek to position $rsrc_pos in resource fork of $src_path: $!");
        $self->{'LAST_ERROR'} = $task->error_text();
        return;
      }
    }

    if($data_pos > 0)
    {
      unless($data_fh->seek($data_pos, 0))
      {
        $task->error(1);
        $task->finish(time());
        $task->error_text("Could not seek to position $data_pos in $src_path: $!");
        $self->{'LAST_ERROR'} = $task->error_text();
        return;
      }
    }
  }

  ($created, $modified) = (stat($src_path))[9,10];

  unless($self->{'MACOS'})
  {
    $created  += HTLC_UNIX_TO_MACOS_TIME;
    $modified += HTLC_UNIX_TO_MACOS_TIME;
  }

  $data = HTXF_MAGIC . pack("NNx4", $ref, ($length - $rsrc_pos - $data_pos));

  $server = $self->{'SERVER_ADDR'};

  # HTXF_TCPPORT only if server port is 5500
  $port = $self->{'SERVER_PORT'} + 1; 

  unless($xfer = IO::Socket::INET->new(PeerAddr =>$server,
                                       PeerPort =>$port,
                                       Timeout  =>$self->{'CONNECT_TIMEOUT'},
                                       Proto    =>'tcp'))
  {
    $task->finish(time());
    $task->error_text("Could not open file transfer connection: $@");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  _debug(_hexdump($data));

  unless(_hlc_write($self, $xfer, \$data, length($data)))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text($self->{'LAST_ERROR'});
    return;
  }

  # 46 49 4c 50  00 01 00 00  00 00 00 00  00 00 00 00  FILP............
  # 00 00 00 00  00 00 00 03  49 4e 46 4f  00 00 00 00  ........INFO....
  # 00 00 00 00  00 00 00 5c  41 4d 41 43  53 49 54 44  .......\AMACSITD
  # 53 49 54 21  00 00 00 00  00 00 21 00  00 00 00 00  SIT!......!.....
  # 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
  # 00 00 00 00  00 00 00 00  00 00 00 00  00 07 70 00  ..............p.
  # 00 b1 ce 81  92 07 70 00  00 02 df 7d  3d 00 00 00  ......p....}=...

  # 12 53 77 6f  6f 70 20 46  41 51 2e 74  65 78 74 2e  .Swoop FAQ.text.
  # 73 69 74 00  00 44 41 54  41 00 00 00  00 00 00 00  sit..DATA.......
  # 00 00 00 59  5c                                     ...Y\
  $data = pack("a4nx16na4x8Na4a4a4x6nx32nx2Nnx2NN",
               "FILP", 0x0001, 0x0003, "INFO",
               length($comments) + length($filename) + 74,
               "AMAC", $type, $creator, $finder_flags, 0x0770,
               $created, 0x0770, $modified, length($filename));

  $data .= $filename .
           pack("n", length($comments)) .
           $comments .
           pack("a4x8N", "DATA", ($data_len - $data_pos));

  _debug(_hexdump($data));

  unless(_hlc_write($self, $xfer, \$data, length($data)))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text($self->{'LAST_ERROR'});
    return;
  }

  # Upload data fork
  unless($self->_upload($xfer, $data_fh, $data_len, $buf_size))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text("Upload did not complete.");
  }

  # 4D 41 43 52  00 00 00 00  00 00 00 00  00 00 01 EC  MACR............
  $data = pack("a4x8N", "MACR", ($rsrc_len - $rsrc_pos));

  _debug(_hexdump($data));

  unless(_hlc_write($self, $xfer, \$data, length($data)))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text($self->{'LAST_ERROR'});
    return;
  }

  if($rsrc_len > 0)
  {
    # Upload resource fork
    unless($self->_upload($xfer, $rsrc_fh, $rsrc_len, $buf_size))
    {
      $xfer->close();
      $task->error(1);
      $task->finish(time());
      $task->error_text("Upload did not complete.");
      return;
    }
  }

  return(1);
}

sub recv_file
{
  my($self, $task, $ref, $size) = @_;

  my($server, $data, $xfer, $tot_length, $length, $buf_size, @ret);
  my($data_file, $rsrc_file, $type, $creator, $created, $modified,
     $finder_flags,  $comments, $comments_len, $data_fh, $data_len,
     $rsrc_fh, $rsrc_len, $name_len, $real_mac_res_fork, $res_fd,
     $finished_file, $port);

  $tot_length = $size;

  $buf_size = $self->{'HTXF_BUFSIZE'};

  $data_fh = new IO::File;
  $rsrc_fh = new IO::File;

  ($data_file, $rsrc_file) = @{$task->path()}[1, 2];

  if($self->{'MACOS'})
  {
    if(length($data_file) > MACOS_MAX_FILENAME)
    {
      for($data_file)
      {
        my($len) = MACOS_MAX_FILENAME - 6;
        
        # Try to preserve filename extension, if any
        # ("\xC9" is "..." in Mac OS)
        # Otherwise, just truncate
        s/^(.{$len}).*?\.(\w{1,4})/$1\xC9.$2/o ||
        s/^(.@{[MACOS_MAX_FILENAME]}).*/$1/;
      }
    }
  }

  unless($data_fh->open(">>$data_file"))
  {
    $task->error(1);
    $task->finish(time());
    $task->error_text("Could not write to $data_file: $!");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  if($self->{'MACOS'})
  {
    # Protect from compile-time errors on non-Mac OS systems that don't
    # define O_RSRC in Fcntl
    eval '$res_fd = POSIX::open($data_file, O_WRONLY | O_CREAT | O_RSRC)';
  }

  # If we're on Mac OS and we can write directly to the resource fork
  if(defined($res_fd) && $rsrc_fh->fdopen($res_fd, "w"))
  {
    $real_mac_res_fork = 1;
    # Temporarily set file type and creator to Hotline's "partial download"
    MacPerl::SetFileInfo(HTXF_PARTIAL_CREATOR, HTXF_PARTIAL_TYPE, $data_file);
  }
  else
  {
    unless($rsrc_fh->open(">>$rsrc_file"))
    {
      $task->error(1);
      $task->finish(time());
      $task->error_text("Could not write to $rsrc_file: $!");
      $self->{'LAST_ERROR'} = $task->error_text();
      return;
    }
  }

  $task->finish(undef);

  $server = $self->{'SERVER_ADDR'};

  $data = HTXF_MAGIC . pack("Nx8", $ref);

  # HTXF_TCPPORT only if server port is 5500
  $port = $self->{'SERVER_PORT'} + 1; 

  unless($xfer = IO::Socket::INET->new(PeerAddr =>$server,
                                       PeerPort =>$port,
                                       Timeout  =>$self->{'CONNECT_TIMEOUT'},
                                       Proto    =>'tcp'))
  {
    $task->finish(time());
    $task->error_text("Could not open file transfer connection: $@");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  unless(_hlc_write($self, $xfer, \$data, length($data)))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text($self->{'LAST_ERROR'});
    return;
  }

  # 46 49 4C 50  00 01 00 00  00 00 00 00  00 00 00 00  FILP............
  # 00 00 00 00  00 00 00 03  49 4E 46 4F  00 00 00 00  ........INFO....
  # 00 00 00 00  00 00 00 60                            .......`
  unless(_hlc_buffered_read($self, $xfer, \$data, SIZEOF_HL_FILE_XFER_HDR))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text($self->{'LAST_ERROR'});
    return;
  }

  $tot_length -= SIZEOF_HL_FILE_XFER_HDR;
  $length = (unpack("N", substr($data, 36, 4)) + SIZEOF_HL_FILE_FORK_HDR);

  unless(substr($data, 0, 4) eq 'FILP')
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text("Bad data from server!");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  #                           41 4D 41 43  54 45 58 54          AMACTEXT
  # 74 74 78 74  00 00 00 00  00 00 01 00  00 00 00 00  ttxt............
  # 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
  # 00 00 00 00  00 00 00 00  00 00 00 00  07 70 00 00  .............p..
  # AE A3 8A 18  07 70 00 00  AE A3 8C 1D  00 00 00 05  .....p..........
  # 74 65 78 74  32 00 11 66  74 70 2E 6D  69 63 72 6F  text2..ftp.micro
  # 73 6F 66 74  2E 63 6F 6D  44 41 54 41  00 00 00 00  soft.comDATA....
  # 00 00 00 00  00 00 01 00                            ........

  unless(_hlc_buffered_read($self, $xfer, \$data, $length))
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text($self->{'LAST_ERROR'});
    return;
  }

  $tot_length -= $length;

  $type     = substr($data, 4, 4);
  $creator  = substr($data, 8, 4);

  $created      = unpack("N", substr($data, 56, 4));
  $finder_flags = substr($data, 18, 2);
  $modified     = unpack("N", substr($data, 64, 4));
  $name_len     = unpack("C", substr($data, 71, 1));
  $comments_len = unpack("n", substr($data, 72 + $name_len, 2)); # 72
  $comments = substr($data, 72 + $name_len + 2, $comments_len);

  $data_len = unpack("N", substr($data, -4));
  $length = $self->_download($xfer, $data_fh, $data_len, $buf_size);
  $tot_length -= $length;
  $data_fh->close();

  unless($length == $data_len)
  {
    $xfer->close();
    $task->error(1);
    $task->finish(time());
    $task->error_text("Download incomplete.");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  # Yet another server bug: it'll tell you it's going to send a resource
  # fork header even when the file has no resource fork (i.e. $size will
  # be SIZEOF_HL_FILE_FORK_HDR bytes larger than the data the server will
  # actually send).  So we only try to read if we have more than
  # SIZEOF_HL_FILE_FORK_HDR left.
  if($tot_length > SIZEOF_HL_FILE_FORK_HDR)
  {
    # 4D 41 43 52  00 00 00 00  00 00 00 00  00 00 01 EC  MACR............
    $length = _hlc_buffered_read($self, $xfer, \$data, SIZEOF_HL_FILE_FORK_HDR);

    return  unless($length);
    $tot_length -= $length;

    $rsrc_len = unpack("N", substr($data, -4));
    $length = $self->_download($xfer, $rsrc_fh, $rsrc_len, $buf_size);
    $tot_length -= $length;
    $rsrc_fh->close();

    unless($length == $rsrc_len)
    {
      $xfer->close();
      $task->error(1);
      $task->finish(time());
      $task->error_text("Download incomplete.");
      $self->{'LAST_ERROR'} = $task->error_text();
      return;
    }
  }
  else
  {
    $tot_length = 0;
    $rsrc_len = 0;
  }

  $xfer->close();

  unless($tot_length == 0)
  {
    $task->error(1);
    $task->finish(time());
    $task->error_text("Tried to download $size bytes, got " .
                      $size - $tot_length . " bytes instead.");
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  $data_len = (stat($data_file))[7];
  $rsrc_len = (stat($rsrc_file))[7];

  unless($rsrc_len)
  {
    unlink($rsrc_file)  if(-e $rsrc_file);
    undef $rsrc_file;
    $rsrc_len = 0;
  }

  unless($data_len || $real_mac_res_fork)
  {
    unlink($data_file)  if(-e $data_file);
    undef $data_file;
    $data_len = 0;
  }

  $task->finish(time());

  # Set the rest of the Mac OS information if we're doing that sort of thing
  if(($real_mac_res_fork && -e $data_file))
  {
    utime($created, $modified, $data_file);

    my($fsspec) = MacPerl::MakeFSSpec($data_file);

    if(length($comments))
    {
      Mac::MoreFiles::FSpDTSetComment($fsspec, $comments);
    }

    my($cat)   = Mac::Files::FSpGetCatInfo($fsspec);
    my($finfo) = $cat->ioFlFndrInfo();

    $finfo->fdFlags(unpack("n", $finder_flags) & 0xFEFF);
    $finfo->fdType($type);
    $finfo->fdCreator($creator);
    $cat->ioFlFndrInfo($finfo);
    Mac::Files::FSpSetCatInfo($fsspec, $cat);

    # Rename data file to remove the .data part
    ($finished_file = $data_file) =~ s/$self->{'DATA_FORK_EXT'}$//;
    unless(CORE::rename($data_file, $finished_file))
    {
      $task->error_text(qq(Could not rename "$data_file" to "$finished_file": $!));
      $self->{'LAST_ERROR'} = $task->error_text();
      return;
    }

    # Return a sigle true value rather than an array of parameters
    # to indicate that you can't call macbinary() if we've already
    # made a Mac file.
    return(1);
  }
  elsif(! -e $rsrc_file)
  {
    ($finished_file = $data_file) =~ s/$self->{'DATA_FORK_EXT'}$//;
    CORE::rename($data_file, $finished_file);
    $data_file = $finished_file;
  }

  return [ $data_file, $data_len,
           $rsrc_file, $rsrc_len,
           $buf_size, $type, $creator, $comments,
           $created, $modified, $finder_flags ];
}

sub _download
{
  my($self, $src_fh, $dest_fh, $len, $buf_size) = @_;

  my($data, $tot_read, $read);

  $tot_read = 0;

  if($len <= $buf_size)
  {
    $read = read($src_fh, $data, $len);
    return  unless(defined($read));
    print $dest_fh $data || return;
    $tot_read += $read;
  }
  else
  {
    my($loop)     = int($len/$buf_size);
    my($leftover) = $len % $buf_size;

    for(; $loop > 0; $loop--)
    {
      $read = read($src_fh, $data, $buf_size);
      return  unless(defined($read));
      print $dest_fh $data || return;
      $tot_read += $read;
    }

    if($leftover > 0)
    {
      $read = read($src_fh, $data, $leftover);
      return  unless(defined($read));
      print $dest_fh $data || return;
      $tot_read += $read;
    }
  }

  unless($tot_read == $len)
  {
    croak("Tried to read $len bytes, actually read $tot_read.  Download may be corrupted!");
  }

  return($tot_read);
}

sub _upload
{
  my($self, $dest_fh, $src_fh, $len, $buf_size) = @_;

  my($data);

  if($len <= $buf_size)
  {
    unless(defined(read($src_fh, $data, $len))) { return }
    _hlc_write($self, $dest_fh, \$data, length($data)) || return;
  }
  else
  {
    my($loop)     = int($len/$buf_size);
    my($leftover) = $len % $buf_size;

    for(; $loop > 0; $loop--)
    {
      unless(defined(read($src_fh, $data, $buf_size))) { return }
      _hlc_write($self, $dest_fh, \$data, length($data)) || return;
    }

    if($leftover > 0)
    {
      unless(defined(read($src_fh, $data, $leftover))) { return }
      _hlc_write($self, $dest_fh, \$data, length($data)) || return;
    }
  }

  return(1);
}

sub macbinary
{
  my($self) = shift  if(ref($_[0]));

  my($macbin_file, $params) = @_;

  unless(ref($params) =~ /^ARRAY/ && @{$params} == 11)
  {
    croak("Incorrect arguments to macbinary()");
  }

  my($data_file, $data_len,
     $rsrc_file, $rsrc_len,
     $buf_size, $type, $creator, $comments,
     $created, $modified, $finder_flags) = @{$params};

  my($finished_file, $filename, $macbin_fh, $data_fh, $rsrc_fh,
     $macbin_hdr, $buf, $len, $pad);

  unless($rsrc_len > 0 || $data_len > 0)
  {
    $self->{'LAST_ERROR'} = "No resource or data fork length."  if($self);
    $! = "No resource or data fork length.";
    return;
  }

  if(defined($data_file))
  {
    ($finished_file = $data_file) =~ s/$self->{'DATA_FORK_EXT'}$//;
  }
  elsif(defined($rsrc_file))
  {
    ($finished_file = $rsrc_file) =~ s/$self->{'RSRC_FORK_EXT'}$//;
  }
  else
  {
    croak "Bad arguments to macbinary() - No rsrc or data file arguments.";
  }

  $finished_file =~ /([^@{[PATH_SEPARATOR]}]+)$/o;
  $filename = $1;

  unless(length($macbin_file))
  {
    $macbin_file .= "$finished_file.bin";
  }

  if(-e $macbin_file)
  {
    $self->{'LAST_ERROR'} = "$macbin_file: file already exists."  if($self);
    $! = "$macbin_file: file already exists.";
    return;
  }

  $buf_size = 4096  unless($buf_size =~ /^\d+$/);

  $macbin_fh = new IO::File;
  $data_fh   = new IO::File;
  $rsrc_fh   = new IO::File;

  unless($macbin_fh->open(">$macbin_file"))
  {
    $self->{'LAST_ERROR'} = $!  if($self);
    return;
  }

  $macbin_hdr = pack("x128"); # Start with empty 128 byte header

  # Offset 000-Byte, old version number, must be kept at zero for compatibility

  # Offset 001-Byte, Length of filename (must be in the range 1-63)
  substr($macbin_hdr, 1, 1) = pack("C", length($filename));

  # Offset 002-1 to 63 chars, filename (only "length" bytes are significant).
  substr($macbin_hdr, 2, length($filename)) = $filename;

  # Offset 065-Long Word, file type (normally expressed as four characters)
  substr($macbin_hdr, 65, 4) = $type;

  # Offset 069-Long Word, file creator (normally expressed as four characters)
  substr($macbin_hdr, 69, 4) = $creator;

  # Offset 073-Byte, original Finder flags
  #     Bit 7 - Locked.
  #     Bit 6 - Invisible.
  #     Bit 5 - Bundle.
  #     Bit 4 - System.
  #     Bit 3 - Bozo.
  #     Bit 2 - Busy.
  #     Bit 1 - Changed.
  #     Bit 0 - Inited.
  substr($macbin_hdr, 73, 1) =             # Clear inited bit
    pack("C", unpack("C", substr($finder_flags, 0, 1)) & 0xFE);

  # Offset 074-Byte, zero fill, must be zero for compatibility

  # Offset 075-Word, file's vertical position within its window.
  substr($macbin_hdr, 75, 2) = pack("n", 0xFFFF);

  # Offset 077-Word, file's horizontal position within its window.
  substr($macbin_hdr, 77, 2) = pack("n", 0xFFFF);

  # Offset 079-Word, file's window or folder ID.
  # Offset 081-Byte, "Protected" flag (in low order bit).
  # Offset 082-Byte, zero fill, must be zero for compatibility

  # Offset 083-Long Word, Data Fork length (bytes, zero if no Data Fork).
  substr($macbin_hdr, 83, 4) = pack("N", $data_len);

  # Offset 087-Long Word, Resource Fork length (bytes, zero if no R.F.).
  substr($macbin_hdr, 87, 4) = pack("N", $rsrc_len);

  # Offset 091-Long Word, File's creation date
  substr($macbin_hdr, 91, 4) = pack("N", $created);

  # Offset 095-Long Word, File's "last modified" date.
  substr($macbin_hdr, 95, 4) = pack("N", $modified);

  # Offset 099-Word, length of Get Info comment to be sent after the resource fork
  #            (if implemented, see below).
  # Offset 101-Byte, Finder Flags, bits 0-7. (Bits 8-15 are already in byte 73)
  # Offset 116-Long Word, Length of total files when packed files are unpacked.
  #            This is only used by programs that pack and unpack on the fly,
  #            mimicing a standalone utility such as PackIt. A program that is
  #            uploading a single file must zero this location when sending a
  #            file. Programs that do not unpack/uncompress files when
  #            downloading may ignore this value.
  substr($macbin_hdr, 116, 4) = pack("N", $data_len + $rsrc_len);

  # Offset 120-Word, Length of a secondary header. If this is non-zero,
  #            Skip this many bytes (rounded up to the next multiple of 128)
  #            This is for future expansion only, when sending files with
  #            MacBinary, this word should be zero.

  # Offset 122-Byte, Version number of Macbinary II that the uploading program
  # is written for (the version begins at 129)
  substr($macbin_hdr, 122, 1) = pack("C", 129);

  # Offset 123-Byte, Minimum MacBinary II version needed to read this file
  # (start this value at 129 129)
  substr($macbin_hdr, 123, 1) = pack("C", 129);

  # Offset 124-Word, CRC of previous 124 bytes
  substr($macbin_hdr, 124, 2) = pack("n", macbin_crc(substr($macbin_hdr, 0, 124), 0));

  # Macbinary II header
  print $macbin_fh $macbin_hdr;

  # Data fork, null padded to a multiple of 128 bytes
  if($data_len)
  {
    unless($data_fh->open($data_file))
    {
      $self->{'LAST_ERROR'} = $!  if($self);
      return;
    }

    while($len = read($data_fh, $buf, $buf_size))
    {
      croak("read() error: $!")  unless(defined($len));
      print $macbin_fh $buf;
    }
    $data_fh->close();

    if($data_len % 128)
    {
      $pad = "x" . (128 - ($data_len % 128));
      print $macbin_fh pack($pad);
    }
  }

  # Resource fork, null padded to a multiple of 128 bytes
  if($rsrc_len)
  {
    unless($rsrc_fh->open($rsrc_file))
    {
      $self->{'LAST_ERROR'} = $!  if($self);
      return;
    }

    while($len = read($rsrc_fh, $buf, $buf_size))
    {
      croak("read() error: $!")  unless(defined($len));
      print $macbin_fh $buf;      
    }
    $rsrc_fh->close();

    if($rsrc_len % 128)
    {
      $pad = "x" . (128 - ($rsrc_len % 128));
      print $macbin_fh pack($pad);
    }
  }

  $macbin_fh->close();

  return(1);
}

sub tracker
{
  $_[0]->{'TRACKER_ADDR'} = $_[1]  if(@_ == 2);
  return $_[0]->{'TRACKER_ADDR'};
}

sub tracker_list
{
  my($self, $timeout) = @_;

  my($tracker, $tracker_address, $server, $port, @servers, $data,
     $num_servers, $length, $tli_ip, $tli_port, $tli_num_users,
     $tli_name, $tli_desc, $byte1);

  $tracker_address = $self->{'TRACKER_ADDR'};

  unless($tracker_address =~ /\S/)
  {
    croak("Tracker address not set!");
  }

  if(($server = $tracker_address) =~ s/^([^ :]+)(?:[: ](\d+))?$/$1/)
  {
    $port = $2 || HTRK_TCPPORT;
  }
  else
  {
    croak("Bad server address: $tracker_address");
  }

  $timeout = $self->{'CONNECT_TIMEOUT'}  unless(defined($timeout));

  eval
  {
    $SIG{'ALRM'} = sub { die "timeout" };
    alarm($timeout);
  
    $tracker = IO::Socket::INET->new(PeerAddr =>$server,
                                     PeerPort =>$port,
                                     Timeout  =>$timeout,
                                     Proto    =>'tcp');
    alarm(0);
    $SIG{'ALRM'} = 'DEFAULT';
  };

  if($@ =~ /timeout/)
  {
    $self->{'LAST_ERROR'} = "Timed out after $timeout seconds.";
    return;
  }

  if(!$tracker || $@)
  {
    $self->{'LAST_ERROR'} = $@ || $! || 'Connection failed';
    return;
  }

  # 48 54 52 4B  00 01                                  HTRK..
  _hlc_write($self, $tracker, \HTRK_MAGIC, HTRK_MAGIC_LEN) || return;

  # 48 54 52 4B  00 01                                  HTRK..
  _hlc_buffered_read($self, $tracker, \$data, HTRK_MAGIC_LEN) || return;

  unless($data eq HTRK_MAGIC)
  {
    $self->{'LAST_ERROR'} = "Bad data from tracker.  Not a hotline tracker?";
    return;
  }

  # 00 01 1F F5  00 53 00 4A  | D1 9C 4B 86  15 7C 00 04  .....S.J..K..|..
  # ^^^^^^^^^^^  ^^^^^ ^^^^^  | ^^^^^^^^^^^  ^^^^^ ^^^^^
  # ???????????    |   ?????  | IP Address   Port  num users ...
  #              num servers  |
  _hlc_buffered_read($self, $tracker, \$data, 8) || return;

  $num_servers = unpack("n", substr($data, 4, 2));

  # Bug fixes here thanks to Les Brown <Les@hotlinecentral.com>
  while(@servers < $num_servers)
  {
    # 4 bytes for IP, 2 bytes for port, 2 bytes for num users
    unless(_hlc_buffered_read($self, $tracker, \$data, 4 + 2 + 2))
    {
      $tracker->close()  if($tracker->opened());
      return  unless(@servers);
      return (wantarray) ? @servers : \@servers;
    }

    # Skip these 8 bytes if the first byte was zero
    $byte1 = unpack("C", substr($data, 0, 1));
    next  if($byte1 == 0);

    $tli_ip        = join('.', map { unpack("C", $_) } split('', substr($data, 0, 4)));
    $tli_port      = unpack("n", substr($data, 4, 2));
    $tli_num_users = unpack("n", substr($data, 6, 2));

    # 2 null bytes, 1 byte for name len
    unless(_hlc_buffered_read($self, $tracker, \$data, 2 + 1))
    {
      $tracker->close()  if($tracker->opened());
      return  unless(@servers);
      return (wantarray) ? @servers : \@servers;
    }

    $length = unpack("C", substr($data, 2, 1));

    # $length bytes for name, 1 byte for description length
    unless(_hlc_buffered_read($self, $tracker, \$data, $length + 1))
    {
      $tracker->close()  if($tracker->opened());
      return  unless(@servers);
      return (wantarray) ? @servers : \@servers;
    }

    $length = unpack("C", chop($tli_name = $data));

    # $length bytes for description
    unless(_hlc_buffered_read($self, $tracker, \$tli_desc, $length))
    {
      $tracker->close()  if($tracker->opened());
      return  unless(@servers);
      return (wantarray) ? @servers : \@servers;
    }

    push(@servers, new Net::Hotline::TrackerListItem($tli_ip,
                                                     $tli_port,
                                                     $tli_num_users,
                                                     $tli_name,
                                                     $tli_desc));
  }

  $tracker->close()  if($tracker->opened());

  return (wantarray) ? @servers : \@servers;
}

sub pchat_invite
{
  my($self, $socket, $ref) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al11_pchat_invite_now($socket, $ref);
  }
  else
  {
    return $self->_pchat_invite($socket, $ref);
  }
}

sub _al11_pchat_invite_now
{
  my($self, $socket, $ref) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_pchat_invite($socket, $ref);
  $task = $self->{'TASKS'}->{$task_num};

  return(1)  if(defined($ref));

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _pchat_invite
{
  my($self, $socket, $ref) = @_;

  my($data, $proto_header, $length, $task_num, $create);

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened());

  $create = defined($ref);

  # 8 bytes for socket atom + 6 or 8 bytes for pchat ref atom (optional)
  $length = 8 + (defined($ref)) ? (($ref > 0xFFFF) ? 8 : 6) : 0;

  $proto_header = new Net::Hotline::Protocol::Header;

  $proto_header->type(($create) ? HTLC_HDR_PCHAT_CREATE :
                                  HTLC_HDR_PCHAT_INVITE);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len($length);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  # Socket of the user we're inviting
  $data .= pack("nnnn", ($create) ? 2 : 1,    # Num atoms
                        HTLC_DATA_SOCKET,     # Atom type
                        0x0002,               # Atom length
                        $socket);             # Atom value

  unless($create)
  {
    my($fmt) = ($ref > 0xFFFF) ? "nnN" : "nnn";

    # Private chat reference number
    $data .= pack($fmt, HTLC_DATA_PCHAT_REF,  # Atom type
                       ($ref > 0xFFFF) ? 4 :2,# Atom length
                       $ref);                 # Atom value
  }

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if($create)
  {
    if(_hlc_write($self, $server, \$data, length($data)))
    {
      _debug("NEW TASK: PCHAT INVITE/CREATE - $task_num\n");
      $self->{'TASKS'}->{$task_num} =
        new Net::Hotline::Task($task_num,HTLC_TASK_PCHAT_CREATE, time());
    }
    else { return }

    return($task_num);
  }
  else
  {
    if(_hlc_write($self, $server, \$data, length($data)))
    {
      _debug("PCHAT INVITE SOCKET($socket) TO PCHAT($ref)\n");
      return(1);
    }
    else { return }
  }
}

sub pchat_accept
{
  my($self, $ref) = @_;

  if($self->{'BLOCKING_TASKS'})
  {
    return $self->_al12_pchat_accept_now($ref);
  }
  else
  {
    return $self->_pchat_accept($ref);
  }
}

sub _al12_pchat_accept_now
{
  my($self, $ref) = @_;

  my($task, $task_num, $packet);

  $task_num = $self->_pchat_accept($ref);
  $task = $self->{'TASKS'}->{$task_num};

  return  unless($task_num);

  $packet = _blocking_task($self, $task_num);

  if($task->error())
  {
    $self->{'LAST_ERROR'} = $task->error_text();
    return;
  }

  return(1);
}

sub _pchat_accept
{
  my($self, $ref) = @_;

  my($data, $proto_header, $task_num);

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && defined($ref));

  $proto_header = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_PCHAT_ACCEPT);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(($ref > 0xFFFF) ? 10 : 8);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  my($fmt) = ($ref > 0xFFFF) ? "nnnN" : "nnnn";

  # Pchat ref number atom
  $data .= pack($fmt, 0x0001,                 # Num atoms
                      HTLC_DATA_PCHAT_REF,    # Atom type
                      ($ref > 0xFFFF) ? 4 : 2,# Atom length
                      $ref);                  # Atom value

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    _debug("NEW TASK: PCHAT ACCEPT($ref) - $task_num\n");
    $self->{'TASKS'}->{$task_num} =
      new Net::Hotline::Task($task_num, HTLC_TASK_PCHAT_ACCEPT, time(), undef, undef, $ref);
  }
  else { return }

  return($task_num);
}

sub pchat_decline
{
  my($self, $ref) = @_;

  my($data, $proto_header, $task_num, $length);

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && defined($ref));

  $proto_header = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_PCHAT_DECLINE);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(($ref > 0xFFFF) ? 10 : 8);
  $proto_header->len2($proto_header->len);

  $data = $proto_header->header();

  my($fmt) = ($ref > 0xFFFF) ? "nnnN" : "nnnn";

  # Pchat ref number atom
  $data .= pack($fmt, 0x0001,                 # Num atoms
                      HTLC_DATA_PCHAT_REF,    # Atom type
                      ($ref > 0xFFFF) ? 4 : 2,# Atom length
                      $ref);                  # Atom value

  _debug(_hexdump($data));

  $task_num = $proto_header->seq();

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

sub al07_pchat_action
{
  my($self, $ref, @message) = @_;

  my($message) = join('', @message);

  $message =~ s/\n/@{[HTLC_NEWLINE]}/osg;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && defined($ref));

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_CHAT);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len((($ref > 0xFFFF) ? 20 : 18) + length($message));
  $proto_header->len2($proto_header->len);

  my($fmt) = ($ref > 0xFFFF) ? "n6Nnn" : "n9";

  $data = $proto_header->header() .
          pack($fmt, 0x0003,                  # Num atoms

                     HTLC_DATA_OPTION,        # Atom type
                     0x0002,                  # Atom length
                     0x0001,                  # Atom data

                     HTLC_DATA_PCHAT_REF,     # Atom type
                     ($ref > 0xFFFF) ? 4 : 2, # Atom length
                     $ref,                    # Atom value

                     HTLC_DATA_CHAT,          # Atom type
                     length($message)) .      # Atom length
          $message;                           # Atom data

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

sub pchat
{
  my($self, $ref, @message) = @_;

  my($message) = join('', @message);

  $message =~ s/\n/@{[HTLC_NEWLINE]}/osg;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && defined($ref));

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_CHAT);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len((($ref > 0xFFFF) ? 14 : 12) + length($message));
  $proto_header->len2($proto_header->len);

  my($fmt) = ($ref > 0xFFFF) ? "n3Nnn" : "n6";

  $data = $proto_header->header() .
          pack($fmt, 0x0002,                  # Num atoms

                     HTLC_DATA_PCHAT_REF,     # Atom type
                     ($ref > 0xFFFF) ? 4 : 2, # Atom length
                     $ref,                    # Atom value

                     HTLC_DATA_CHAT,          # Atom type
                     length($message)) .      # Atom length
          $message;                           # Atom data

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

sub pchat_leave
{
  my($self, $ref) = @_;

  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && defined($ref));

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_PCHAT_CLOSE);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len(($ref > 0xFFFF) ? 10 : 8);
  $proto_header->len2($proto_header->len);

  my($fmt) = ($ref > 0xFFFF) ? "n3N" : "n4";

  $data = $proto_header->header() .
          pack($fmt, 0x0001,                  # Num atoms

                     HTLC_DATA_PCHAT_REF,     # Atom type
                     ($ref > 0xFFFF) ? 4 : 2, # Atom length
                     $ref);                   # Atom value

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    delete $self->{'PCHATS'}->{$ref};
    return(1);
  }
  else { return }
}

sub pchat_subject
{
  my($self, $ref, @subject) = @_;
  
  my($server) = $self->{'SERVER'} or croak "Not connected to a server";
  return  unless($server->opened() && defined($ref));

  my($subject) = join('', @subject);

  my($data);

  my($proto_header) = new Net::Hotline::Protocol::Header;

  $proto_header->type(HTLC_HDR_PCHAT_SUBJECT);
  $proto_header->seq($self->_next_seqnum());
  $proto_header->task(0x00000000);
  $proto_header->len((($ref > 0xFFFF) ? 14 : 12) + length($subject));
  $proto_header->len2($proto_header->len);

  my($fmt) = ($ref > 0xFFFF) ? "n3Nnn" : "n6";

  $data = $proto_header->header() .
          pack($fmt, 0x0002,                  # Num atoms

                     HTLC_DATA_PCHAT_REF,     # Atom type
                     ($ref > 0xFFFF) ? 4 : 2, # Atom length
                     $ref,                    # Atom value

                     HTLC_DATA_PCHAT_SUBJECT, # Atom type
                     length($subject)) .      # Atom length
          $subject;                           # Atom value

  _debug(_hexdump($data));

  if(_hlc_write($self, $server, \$data, length($data)))
  {
    return(1);
  }
  else { return }
}

1;