summaryrefslogtreecommitdiff
path: root/examples/dhtd.hs
blob: fdbe77196cce4273cf7f81a23ee8e9f4beffbb48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
{-# LANGUAGE BangPatterns              #-}
{-# LANGUAGE CPP                       #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts          #-}
{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE LambdaCase                #-}
{-# LANGUAGE MultiParamTypeClasses     #-}
{-# LANGUAGE NamedFieldPuns            #-}
{-# LANGUAGE NondecreasingIndentation  #-}
{-# LANGUAGE OverloadedStrings         #-}
{-# LANGUAGE PartialTypeSignatures     #-}
{-# LANGUAGE PatternSynonyms           #-}
{-# LANGUAGE RankNTypes                #-}
{-# LANGUAGE RecordWildCards           #-}
{-# LANGUAGE ScopedTypeVariables       #-}
{-# LANGUAGE TupleSections             #-}
{-# LANGUAGE TypeFamilies              #-}
{-# LANGUAGE TypeOperators             #-}

module Main where

import Control.Arrow
import Control.Applicative
import Control.Concurrent.STM
import Control.Concurrent.STM.TMChan
import Control.Exception
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Control
import Control.Monad.Trans.Resource (runResourceT)
import Data.Bool
import Data.Char
import Data.Conduit as C
import Data.Function
import Data.Hashable
import Data.List
import Data.Word
import Data.InOrOut
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Map.Strict as Map
import Data.Maybe
import qualified Data.Set        as Set
import Data.Tuple
import Data.Time.Clock
import qualified Data.XML.Types  as XML
import GHC.Conc (threadStatus,ThreadStatus(..))
import GHC.Stats
import Network.Socket
import System.Environment
import System.IO
import System.Mem
import System.Posix.Process
import Text.PrettyPrint.HughesPJClass
import Text.Printf
import Text.Read
#ifdef THREAD_DEBUG
import Control.Concurrent.Lifted.Instrument
#else
import Control.Concurrent.Lifted
import GHC.Conc                  (labelThread)
#endif
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Vector as V
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import System.Posix.Signals


import Announcer
import Crypto.Tox -- (zeros32,SecretKey,PublicKey, generateSecretKey, toPublic, encodeSecret, decodeSecret, userKeys)
import Network.UPNP as UPNP
import Network.Address hiding (NodeId, NodeInfo(..))
import Network.QueryResponse
import Network.StreamServer
import Network.Kademlia.Search
import qualified Network.BitTorrent.MainlineDHT as Mainline
import qualified Network.Tox as Tox
import Network.Kademlia.Routing as R
import Data.Aeson as J (ToJSON, FromJSON)
import qualified Data.Aeson as J
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as B
import qualified Data.Text as T
import qualified Data.Text.Encoding as E
import qualified Data.Text.Encoding.Error as E
import Control.Concurrent.Tasks
import System.IO.Error
import qualified Data.Serialize as S
import Network.BitTorrent.DHT.ContactInfo as Peers
import qualified Data.MinMaxPSQ as MM
import Data.Wrapper.PSQ as PSQ (pattern (:->))
import qualified Data.Wrapper.PSQ as PSQ
import Data.Ord
import Data.Time.Clock.POSIX
import qualified Network.Tox.DHT.Transport as Tox
import qualified Network.Tox.DHT.Handlers as Tox
import qualified Network.Tox.Onion.Transport as Tox
import qualified Network.Tox.Onion.Handlers as Tox
import qualified Network.Tox.Crypto.Transport as Tox (CryptoMessage(..),CryptoData(..), isOFFLINE, isKillPacket)
import qualified Network.Tox.Crypto.Handlers as Tox
import Data.Typeable
import Network.Tox.ContactInfo as Tox
import OnionRouter
import PingMachine
import Data.PacketQueue

-- Presence imports.
import ConsoleWriter
import Presence
import XMPPServer
import Connection
import ToxToXMPP
import qualified Connection.Tcp as Tcp (ConnectionEvent(..))
import Control.Concurrent.Supply
import qualified Data.CyclicBuffer as CB
import DPut


showReport :: [(String,String)] -> String
showReport kvs = showColumns $ map (\(x,y)->[x,y]) kvs

showColumns :: [[String]] -> String
showColumns rows = do
    let cols = transpose rows
        ws = map (maximum . map (succ . length)) cols
    fs <- rows
    _ <- take 1 fs -- Guard against empty rows so that 'last' is safe.
    "  " ++ concat (zipWith (printf "%-*s") (init ws) (init fs)) ++ last fs ++ "\n"

pshow :: Show a => a -> B.ByteString
pshow = B.pack . show

marshalForClient :: String -> String
marshalForClient s = show (length s) ++ ":" ++ s

marshalForClientB :: B.ByteString -> B.ByteString
marshalForClientB s = B.concat [pshow (B.length s),":",s]

data ClientHandle = ClientHandle Handle (MVar Int)

-- | Writes a message and signals ready for next command.
hPutClient :: ClientHandle -> String -> IO ()
hPutClient (ClientHandle h hstate) s = do
    st <- takeMVar hstate
    hPutStr h ('.' : marshalForClient s)
    putMVar hstate 1 -- ready for input

-- | Writes a message and signals ready for next command.
hPutClientB :: ClientHandle -> B.ByteString -> IO ()
hPutClientB (ClientHandle h hstate) s = do
    st <- takeMVar hstate
    B.hPutStr h ('.' `B.cons` marshalForClientB s)
    putMVar hstate 1 -- ready for input

-- | Writes message, but signals there is more to come.
hPutClientChunk :: ClientHandle -> String -> IO ()
hPutClientChunk (ClientHandle h hstate) s = do
    st <- takeMVar hstate
    hPutStr h (' ' : marshalForClient s)
    putMVar hstate 2 -- ready for more output

data DHTQuery nid ni = forall addr r tok.
                        ( Ord addr
                        , Typeable r
                        , Typeable tok
                        , Typeable ni
                        ) => DHTQuery
    { qsearch     :: Search nid addr tok ni r
    , qhandler    :: ni -> nid -> IO ([ni], [r], Maybe tok) -- ^ Invoked on local node, when there is no query destination.
    , qshowR      :: r -> String
    , qshowTok    :: tok -> Maybe String
    }

data DHTAnnouncable nid = forall dta tok ni r.
                        ( Show r
                        , Typeable dta -- information being announced
                        , Typeable tok -- token
                        , Typeable r   -- search result
                        , Typeable ni  -- node
                        ) => DHTAnnouncable
    { announceParseData    :: String -> Either String dta
    , announceParseToken   :: dta -> String -> Either String tok
    , announceParseAddress :: String -> Either String ni
    , announceSendData     :: Either ( String {- search name -}
                                     , String -> Either String r
                                     , PublicKey {- me -} -> dta -> r -> IO ())
                                     (dta -> tok -> Maybe ni -> IO (Maybe r))
    , announceInterval     :: POSIXTime
    , announceTarget       :: dta -> nid
    }

data DHTSearch nid ni = forall addr tok r. DHTSearch
    { searchThread     :: ThreadId
    , searchState      :: SearchState nid addr tok ni r
    , searchShowTok    :: tok -> Maybe String
    , searchResults    :: TVar (Set.Set String)
    }

data DHTPing ni = forall r. DHTPing
    { pingQuery :: [String] -> ni -> IO (Maybe r)
    , pingShowResult :: r -> String
    }

data DHT = forall nid ni. ( Show     ni
                          , Read     ni
                          , ToJSON   ni
                          , FromJSON ni
                          , Ord      ni
                          , Hashable ni
                          , Show     nid
                          , Ord      nid
                          , Hashable nid
                          , Typeable ni
                          , S.Serialize nid
                          ) =>
    DHT
    { dhtBuckets       :: TVar (BucketList ni)
    , dhtSecretKey     :: STM (Maybe SecretKey)
    , dhtPing          :: Map.Map String (DHTPing ni)
    , dhtQuery         :: Map.Map String (DHTQuery nid ni)
    , dhtAnnouncables  :: Map.Map String (DHTAnnouncable nid)
    , dhtParseId       :: String -> Either String nid
    , dhtSearches      :: TVar (Map.Map (String,nid) (DHTSearch nid ni))
    , dhtFallbackNodes :: IO [ni]
    , dhtBootstrap     :: [ni] -> [ni] -> IO ()
    }

nodesFileName :: String -> String
nodesFileName netname = netname ++ "-nodes.json"

saveNodes :: String -> DHT -> IO ()
saveNodes netname DHT{dhtBuckets} = do
    bkts <- atomically $ readTVar dhtBuckets
    let ns = map fst $ concat $ R.toList bkts
        bs = J.encode ns
        fname = nodesFileName netname
    L.writeFile fname bs

loadNodes :: FromJSON ni => String -> IO [ni]
loadNodes netname = do
    let fname = nodesFileName netname
    attempt <- tryIOError $ do
        J.decode <$> L.readFile fname
         >>= maybe (ioError $ userError "Nothing") return
    either (const $ fallbackLoad fname) return attempt

fallbackLoad :: FromJSON t => FilePath -> IO [t]
fallbackLoad fname = do
    attempt <- tryIOError $ do
        J.decode <$> L.readFile fname
         >>= maybe (ioError $ userError "Nothing") return
    let go r = do
            let m = HashMap.lookup "nodes" (r :: J.Object)
                ns0 = case m of Just (J.Array v) -> V.toList v
                                Nothing        -> []
                ns1 = zip (map J.fromJSON ns0) ns0
                issuc (J.Error _,_) = False
                issuc _             = True
                (ss,fs) = partition issuc ns1
                ns = map (\(J.Success n,_) -> n) ss
            mapM_ print (map snd fs)  >> return ns
    either (const $ return []) go attempt


{-
pingNodes :: String -> DHT -> IO Bool
pingNodes netname DHT{dhtPing} | Just DHTPing{pingQuery=ping} <- Map.lookup "ping" dhtPing = do
    let fname = nodesFileName netname
    attempt <- tryIOError $ do
        J.decode <$> L.readFile fname
         >>= maybe (ioError $ userError "Nothing") return
-}

asProxyTypeOf :: a -> proxy a -> a
asProxyTypeOf = const

pingNodes :: String -> DHT -> IO (Maybe Int)
pingNodes netname dht@DHT{dhtPing} | Just DHTPing{pingQuery=ping} <- Map.lookup "ping" dhtPing = do
    let fname = nodesFileName netname
    ns <- loadNodes netname
    case ns of
        [] -> return Nothing
        _ -> do
            fork $ do
                 myThreadId >>= flip labelThread ("pinging."++fname)
                 putStrLn $ "Forked "++show fname
                 withTaskGroup ("withTaskGroup."++fname) 10 $ \g -> do
                   forM_ (ns `asTypeOf` [])
                        $ \n -> forkTask g (show n)
                                $ void
                                $ ping [] n
                 putStrLn $ "Load finished "++show fname
            return $ Just $ length ns
pingNodes _ _ = return Nothing



reportTable :: Show ni => BucketList ni -> [(String,String)]
reportTable bkts = map (show *** show . fst)
                 $ concat
                 $ zipWith map (map (,) [0::Int ..])
                 $ R.toList
                 $ bkts

reportResult ::
    String
    -> (r -> String)
    -> (tok -> Maybe String)
    -> (ni -> String)
    -> ClientHandle
    -> Either String ([ni],[r],Maybe tok)
    -> IO ()
reportResult meth showR showTok showN h (Left e)            = hPutClient h e
reportResult meth showR showTok showN h (Right (ns,rs,tok)) = do
    hPutClient h $ showReport report
 where
    report = intercalate [("","")] [ tok_r , node_r , result_r ]

    tok_r = maybe [] (pure . ("token:",)) $ showTok =<< tok

    node_r = map ( ("n",) . showN ) ns

    result_r | (meth=="node") = []
             | otherwise      = map ( (take 1 meth,) . showR ) rs

-- example:
-- * 10 peer 141d6c6ee2810f46d28bbe8373d4f454a4122535
-- - 1  peer 141d6c6ee2810f46d28bbe8373d4f454a4122535
--   22 node 141d6c6ee2810f46d28bbe8373d4f454a4122535
--
-- key: '*' in progress
--      '-' stopped
--      ' ' finished
showSearches :: ( Show nid
                , Ord nid
                , Hashable nid
                , Ord ni
                , Hashable ni
                ) => Map.Map (String,nid) (DHTSearch nid ni) -> IO String
showSearches searches = do
    tups <- forM (Map.toList searches) $ \((meth,nid),DHTSearch{..}) -> do
        (is'fin, cnt) <- atomically $
                (,) <$> searchIsFinished searchState
                    <*> (Set.size <$> readTVar searchResults)
        tstat <- threadStatus searchThread
        let stat = case tstat of
                _ | is'fin     -> ' '
                ThreadFinished -> '-'
                ThreadDied     -> '-'
                _              -> '*'
        return (stat,show cnt,meth,show nid)
    let cnt'width = maximum $ map (\(_,cnt,_,_)->length cnt) tups
        mth'width = maximum $ map (\(_,_,mth,_)->length mth) tups
    return $ do -- List monad.
        (stat,cnt,meth,nid) <- tups
        printf "  %c %-*s %-*s %s\n" stat cnt'width cnt mth'width meth nid

forkSearch ::
    ( Ord nid
    , Hashable nid
    , Ord ni
    , Hashable ni
    , Show nid
    ) =>
    String
    -> nid
    -> DHTQuery nid ni
    -> TVar (Map.Map (String,nid) (DHTSearch nid ni))
    -> TVar (BucketList ni)
    -> ThreadId
    -> TVar (Maybe (IO ()))
    -> STM ()
forkSearch method nid DHTQuery{qsearch,qshowTok,qshowR} dhtSearches dhtBuckets  tid kvar = do
    ns <- R.kclosest (searchSpace qsearch) searchK nid <$> readTVar dhtBuckets
    st <- newSearch qsearch nid ns
    results <- newTVar Set.empty
    let storeResult r = modifyTVar' results (Set.insert (qshowR r))
                        >> return True
        new = DHTSearch
            { searchThread  = tid
            , searchState   = st
            , searchShowTok = qshowTok
            , searchResults = results
            }
    modifyTVar' dhtSearches $ Map.insert (method,nid) new
    -- Finally, we write the search loop action into a tvar that will be executed in a new
    -- thread.
    writeTVar kvar $ Just $ searchLoop qsearch nid storeResult st

reportSearchResults :: (Show t, Ord t1, Ord t, Hashable t) =>
                                   String -> ClientHandle -> DHTSearch t1 t -> IO ()
reportSearchResults meth h DHTSearch{searchShowTok,searchState,searchResults} = do
    (ns,rs) <- atomically $ do
        mm <- readTVar $ searchInformant searchState
        rset <- readTVar searchResults
        let ns = map (\(MM.Binding ni tok _) -> (ni,tok))
                     $ MM.toList mm
            rs = Set.toList rset
        return (ns,rs)
    let n'width       = succ $ maximum $ map (length . show . fst) ns
        showN (n,tok) = take n'width (show n ++ repeat ' ') ++ (fromMaybe "" $ searchShowTok =<< tok)
        ns'           = map showN ns
    reportResult meth id (const Nothing) id h (Right (ns',rs, Just ()))

data ConnectionManager = forall status k. ConnectionManager { typedManager :: Connection.Manager status k }

data Session = Session
    { netname           :: String
    , selectedKey       :: Maybe PublicKey
    , dhts              :: Map.Map String DHT
    , externalAddresses :: IO [SockAddr]
    , swarms            :: Mainline.SwarmsDatabase
    , cryptosessions    :: Tox.NetCryptoSessions
    , toxkeys           :: TVar Tox.AnnouncedKeys
    , roster            :: Tox.ContactInfo
    , announceToLan     :: IO ()
    , connectionManager :: Maybe ConnectionManager
    , onionRouter       :: OnionRouter
    , announcer         :: Announcer
    , signalQuit        :: IO ()
    , mbTox             :: Maybe Tox.Tox
    }

exceptionsToClient :: ClientHandle -> IO () -> IO ()
exceptionsToClient (ClientHandle h hstate) action =
    action `catch` \(SomeException e) -> do
                        st <- takeMVar hstate
                        when (st /= 1) $ do
                            hPutStr h ('.': marshalForClient (show e))
                        putMVar hstate 1 -- ready for input

hGetClientLine :: ClientHandle -> IO String
hGetClientLine (ClientHandle h hstate) = do
    st <- takeMVar hstate
    -- st should be 1
    x <- hGetLine h
    putMVar hstate 0 -- ready for output
    return x

hCloseClient :: ClientHandle -> IO ()
hCloseClient (ClientHandle h hstate) = do
    st <- takeMVar hstate
    hClose h
    putMVar hstate 3 -- closed file handle

clientSession0 :: Session -> t1 -> t -> Handle -> IO ()
clientSession0 s sock cnum h = do
    hstate <- newMVar 1 -- ready for input
    clientSession s sock cnum (ClientHandle h hstate)
  `catch` \e -> if isEOFError e then return ()
                                else throwIO e


clientSession :: Session -> t1 -> t -> ClientHandle -> IO ()
clientSession s@Session{..} sock cnum h = do
    line <- dropWhile isSpace <$> hGetClientLine h
    let (c,args) = second (dropWhile isSpace) $ break isSpace line
        cmd0 :: IO () -> IO ()
        cmd0 action = exceptionsToClient h action >> clientSession s sock cnum h
        switchNetwork dest = do hPutClient h ("Network: "++dest)
                                clientSession s{netname=dest} sock cnum h
        switchKey key = clientSession s { selectedKey = key } sock cnum h
        twoWords str = let (word1,a1) = break isSpace (dropWhile isSpace str)
                           (word2,a2) = break isSpace (dropWhile isSpace a1)
                           in (word1,word2,drop 1 a2)
        strp = B.unpack . fst . until snd dropEnd . (,False) . B.dropWhile isSpace . B.pack
            where
                dropEnd (x,_) =
                    case B.unsnoc x of
                                 Just (str,c) | isSpace c -> (str,False)
                                 _ -> (x,True)
    let readHex :: (Read n, Integral n) => String -> Maybe n
        readHex s = readMaybe ("0x" ++ s)
        strToSession :: String -> IO (Either String Tox.NetCryptoSession)
        strToSession idStr
          = case readHex idStr of
                Nothing -> return (Left "Unable to parse session id")
                Just id -> do
                    sessions <- filter ((==id) . Tox.ncSessionId)
                              . concat
                              . Map.elems <$> (atomically $ readTVar (Tox.netCryptoSessionsByKey cryptosessions))
                    case sessions of
                        []     -> return (Left "Session not found")
                        (x:xs) -> return (Right x)
    let mkrow :: (SecretKey, PublicKey) -> (String,String)
        mkrow (a,b) | Just x <- encodeSecret a= (B.unpack x, show (Tox.key2id b))
        mkrow _ = error (concat ["Assertion fail in 'mkrow' function at ", __FILE__, ":", show __LINE__])
        sessionCommands :: [[String]]
        sessionCommands =
            [ ["ping"]   -- pinglike
            , ["cookie"] -- pinglike
            , ["stop"]
            , ["throw"]
            , ["quit"]
            , ["pid"]
            , ["external-ip"]
            , ["threads"]
            , ["mem"]
            , ["nid"]
            , ["lan"]
            , ["ls"]
            , ["k"]
            , ["roster"]
            , ["sessions"]
            , ["session"]
            , ["netcrypto"]
            , ["onion"]
            , ["g"]
            , ["p"]
            , ["a"]
            , ["s"]
            , ["x"]
            , ["save"]
            , ["load"]
            , ["swarms"]
            , ["peers"]
            , ["toxids"]
            , ["c"]
            , ["quiet"]
            , ["verbose"]
            , ["help"]
            ]
    case (map toLower c,args) of
        (n, _) | n `elem` Map.keys dhts -> switchNetwork n
        -- "ping"
        -- "cookie"
        (pinglike, s) | Just DHT{dhtPing} <- Map.lookup netname dhts
                      , Just DHTPing{ pingQuery=ping
                                    , pingShowResult=showr } <- Map.lookup pinglike dhtPing
                      , ws@(_:_) <- words s
                    -> cmd0 $ do
                        case readEither $ last ws of
                          Right addr -> do result <- ping (init ws) addr
                                           let rs = [" ", maybe "Timeout." showr result]
                                           hPutClient h $ unlines rs
                          Left er -> hPutClient h er
        (x,_) | not (null (strp x))
              , x `notElem` map head sessionCommands -> cmd0 $ do
                hPutClient h $ "error."

        ("stop", _) -> do hPutClient h "Terminating DHT Daemon."
                          hCloseClient h
                          signalQuit

        ("throw", er) -> cmd0 $ do
                            throwIO $ userError er
                            hPutClient h "The impossible happened!"

        ("quit", _) -> hPutClient h "" >> hCloseClient h

        ("pid", _) -> cmd0 $ do
                    pid <- getProcessID
                    hPutClient h (show pid)
        ("external-ip", _) -> cmd0 $ do
                    unlines . map (either show show . either4or6) <$> externalAddresses
                        >>= hPutClient h
#ifdef THREAD_DEBUG
        ("threads", _) -> cmd0 $ do
                    ts <- threadsInformation
                    tm <- getCurrentTime
                    r <- forM ts $ \(tid,PerThread{..}) -> do
                        stat <- threadStatus tid
                        let showStat (ThreadBlocked reason) = show reason
                            showStat stat                   = show stat
                        return [show lbl,show (diffUTCTime tm startTime),showStat stat]
                    hPutClient h $ showColumns r
#endif
        ("mem", s) -> cmd0 $ do
                    case s of
                        "gc" -> do hPutClient h "Performing garbage collection..."
                                   performMajorGC
                        "" -> do
                            is_enabled <- getGCStatsEnabled
                            if is_enabled
                             then do
                                GCStats{..} <- getGCStats
                                let r = [ ("bytesAllocated", show bytesAllocated)
                                        , ("numGcs", show numGcs)
                                        , ("maxBytesUsed", show maxBytesUsed)
                                        , ("numByteUsageSamples", show numByteUsageSamples)
                                        , ("cumulativeBytesUsed", show cumulativeBytesUsed)
                                        , ("bytesCopied", show bytesCopied)
                                        , ("currentBytesUsed", show currentBytesUsed)
                                        , ("currentBytesSlop", show currentBytesSlop)
                                        , ("maxBytesSlop", show maxBytesSlop)
                                        , ("peakMegabytesAllocated", show peakMegabytesAllocated)
                                        , ("mutatorCpuSeconds", show mutatorCpuSeconds)
                                        , ("mutatorWallSeconds", show mutatorWallSeconds)
                                        , ("gcCpuSeconds", show gcCpuSeconds)
                                        , ("gcWallSeconds", show gcWallSeconds)
                                        , ("cpuSeconds", show cpuSeconds)
                                        , ("wallSeconds", show wallSeconds)
                                        , ("parTotBytesCopied", show parTotBytesCopied)
                                        , ("parMaxBytesCopied", show parMaxBytesCopied)
                                        ]
                                hPutClient h $ showReport r
                            else hPutClient h "Run with +RTS -T to obtain live memory-usage information."
                        _ -> hPutClient h "error."

        ("nid", s) | Just DHT{dhtParseId} <- Map.lookup netname dhts
                    -> cmd0 $ do
                        hPutClient h $ case dhtParseId s of
                            Left e    ->
                                -- HACK: split nospam from hex toxid
                                case dhtParseId (take 64 s) of
                                    Left e -> case Tox.parseNoSpamId $ T.pack s of
                                                Left ej -> if elem '@' s
                                                            then "Error: " ++ ej
                                                            else "Error: " ++ e
                                                Right jid -> show jid
                                    Right nid -> show nid ++ " nospam:" ++ drop 64 s
                            Right nid -> show nid

        ("lan", _)  -> cmd0 $ do
                        announceToLan
                        hPutClient h "ok"

        ("ls", _) | Just DHT{dhtBuckets} <- Map.lookup netname dhts
                    -> cmd0 $ do
                        bkts <- atomically $ readTVar dhtBuckets
                        let r = reportTable bkts
                        hPutClient h $
                            showReport $
                                    r ++ [ ("buckets", show $ R.shape bkts)
                                         , ("node-id", show $ thisNode bkts)
                                         , ("network", netname) ]

        --  TODO: online documentation.
        --
        --  k - manage key-pairs
        --
        --      k                  (list keys)
        --      k gen              (generate new key and list keys)
        --      k add <secret-key> (input a specific secret key)
        --      k del <secret-key>
        --      k secrets          (list key pairs, including secret keys)

        ("k", s) | ""           <- strp s -> cmd0 $ do
                        ks <- atomically $ myKeyPairs roster
                        let spaces k | Just sel <- selectedKey, (sel == k) = " *"
                                     | otherwise                           = "  "
                        hPutClient h $ unlines $ map (\(_,k) -> mappend (spaces k) . show . Tox.key2id $ k) ks
                 | "gen"        <- strp s -> do
                        secret <- generateSecretKey
                        let pubkey = toPublic secret
                        oldks <- atomically $ do
                            ks <- myKeyPairs roster
                            Tox.addContactInfo roster secret
                            return ks
                        let asString = show . Tox.key2id
                        hPutClient h $ unlines $ map (mappend "  " . show . Tox.key2id . snd) oldks
                                                  ++ [mappend " *" . show . Tox.key2id $ pubkey]
                        switchKey $ Just pubkey
                 | "secrets"    <- strp s -> cmd0 $ do
                        ks <- atomically $ myKeyPairs roster
                        skey <- maybe (return Nothing) (atomically . dhtSecretKey)
                                      $ Map.lookup netname dhts
                        hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of
                            Just x  -> [("",""),("dht-key:",B.unpack x)]
                            Nothing -> []
                 | ("sel",_:expr) <- break isSpace s -> do
                         ks <- atomically $ map (show . Tox.key2id . snd) <$> myKeyPairs roster
                         case find (isInfixOf expr) ks of
                            Just k -> do
                                hPutClient h $ "Selected key: "++k
                                switchKey $ Just $ Tox.id2key $ read k
                            Nothing -> cmd0 $ hPutClient h "no match."
                 | ("add":secs) <- words s
                 , mbSecs <- map (decodeSecret . B.pack) secs
                 , all isJust mbSecs -> do
                        let f (Just b) = b
                            f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__])
                        let toPair x = (x,toPublic x)
                            pairs = map (toPair . f) mbSecs
                        oldks <- atomically $ do
                            oldks <- myKeyPairs roster
                            forM pairs $ \(sk,_) -> Tox.addContactInfo roster sk
                            return oldks
                        hPutClient h $ unlines $ map (mappend "  " . show . Tox.key2id . snd) oldks
                                                  ++ map (mappend " *" . show . Tox.key2id .snd) pairs
                        switchKey $ listToMaybe $ map snd pairs
                 | ("del":secs) <- words s
                 , mbSecs <- map (decodeSecret . B.pack) secs
                 , all isJust mbSecs -> do
                        let f (Just b) = b
                            f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__])
                        let toPair x = (x,toPublic x)
                            pairs = map (toPair . f) mbSecs
                        ks <- atomically $ do
                            forM pairs $ \(_,pk) -> Tox.delContactInfo roster pk
                            myKeyPairs roster
                        hPutClient h . showReport $ map mkrow ks
                        switchKey $ do
                            k <- selectedKey
                            guard $ k `notElem` map snd pairs
                            Just k

        ("roster", s) -> cmd0 $ join $ atomically $ do
            dns <- dnsPresentation roster
            fs <- HashMap.toList <$> friendRequests roster
            let showFriend (remotekey,fr) =
                    ("  " ++ show remotekey, T.unpack $ T.decodeUtf8 $ Tox.friendRequestText fr)
                showAccount (me,cs) =
                    [(show me,"")] ++ map showFriend cs
                frs = fs >>= showAccount
            return $ do
                hPutClientChunk h $ unlines [ dns, "", "Friend Requests" ]
                hPutClient h $ showReport frs

        ("quiet",s) | s' <- strp s
                    , Just (tag::DebugTag) <- readMaybe ('X':map toUpper (take 1 s') ++ drop 1 s')
                    -> cmd0 $ do
                setQuiet tag
                hPutClient h $ "Suppressing " ++ show tag ++ " messages."

        ("verbose",s) | s' <- strp s
                    , Just (tag::DebugTag) <- readMaybe ('X':map toUpper (take 1 s') ++ drop 1 s')
                    -> cmd0 $ do
                setVerbose tag
                hPutClient h $ "Showing " ++ show tag ++ " messages."

        -- list information about current netcrypto sesssions
        ("sessions", s) | "" <- strp s
            -> cmd0 $ do
                sessions <- concat . Map.elems <$> (atomically $ readTVar (Tox.netCryptoSessionsByKey cryptosessions))
                let sessionsReport = mapM showPerSession sessions
                    headers = ["SessionID", "YourKey", "TheirKey", "NextMsg", "Dropped","Handled","Unhandled"]
                    showPerSession (Tox.NCrypto
                                        { ncSessionId = id
                                        , ncMyPublicKey = yourkey
                                        , ncTheirPublicKey = theirkey
                                        , ncLastNMsgs = lastN
                                        , ncSockAddr = sockAddr
                                        }) = do
                        (num,dropped) <- atomically $ liftA2 (,) (CB.getTotal lastN) (CB.getDropped lastN)
                        as <- atomically (CB.cyclicBufferViewList lastN)
                        let (h,u) = partition (fst . snd) as
                            countHandled   = length h
                            countUnhandled = length u
                        return [ printf "%x" id           -- "SessionID"
                               , show (Tox.key2id yourkey) -- "YourKey"
                               , show (Tox.key2id theirkey)-- "TheirKey"
                               , show num                 -- "NextMsg"
                               , show dropped             -- "Dropped"
                               , show countHandled        -- "Handled"
                               , show countUnhandled      -- "Unhandled"
                               ]
                if null sessions
                 then hPutClient h "No sessions."
                 else do
                    rows <- sessionsReport
                    hPutClient h (showColumns (headers:rows))
        -- session <N> tail
        -- show context (latest lossless messages)
        ("session", s) | (idStr,tailcmd,unstripped) <- twoWords s
                        , "" <- strp unstripped
                        , tailcmd `elem` ["tail","context"]
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                     msgs <- atomically $ CB.cyclicBufferViewList (Tox.ncLastNMsgs session)
                                     hPutClientB h (B.unlines (map showMsg msgs))
        -- session <N> me
        -- display information about how you look to that session
        ("session", s) | (idStr,"me",unstripped) <- twoWords s
                        , "" <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                     view <- atomically (readTVar (Tox.ncView session) >>= Tox.viewSnapshot)
                                     hPutClientB h (vShowMe view 0)
        -- session <N> them
        -- display information about the person on the other end of the session
        ("session", s) | (idStr,them,unstripped) <- twoWords s
                        , "" <- strp unstripped
                        , them `elem` ["them","you"]
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                     view <- atomically (readTVar (Tox.ncView session) >>= Tox.viewSnapshot)
                                     hPutClientB h (vShowThem view 0)
        -- session <N> online
        -- send ONLINE packet to session N
        ("session", s) | (idStr,"online",unstripped) <- twoWords s
                        , stripped <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            Tox.sendOnline (Tox.toxCryptoKeys tox) session
                                            hPutClient h "sent ONLINE"
        -- session <N> online
        -- send OFFLINE packet to session N
        ("session", s) | (idStr,"offline",unstripped) <- twoWords s
                        , stripped <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            Tox.sendOffline (Tox.toxCryptoKeys tox) session
                                            hPutClient h "sent OFFLINE"
        -- session <N> kill
        -- send KILL packet to session N
        ("session", s) | (idStr,"kill",unstripped) <- twoWords s
                        , stripped <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            Tox.sendKill (Tox.toxCryptoKeys tox) session
                                            hPutClient h "sent KillPacket"
        -- session <N> nick <NICKNAME>
        -- send NICK packet to session N, setting nick to NICKNAME
        ("session", s) | (idStr,"nick",unstripped) <- twoWords s
                        , nick <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            Tox.setNick (Tox.toxCryptoKeys tox) session (B.pack nick)
                                            hPutClient h "sent NICKNAME"
        -- session <N> status <STATUS>
        -- send USERSTATUS packet to session N, set status to STATUS
        ("session", s) | (idStr,"status",unstripped) <- twoWords s
                        , statusStr  <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            case readMaybe statusStr of
                                                Nothing -> hPutClient h "Unable to parse status"
                                                Just status -> do
                                                    Tox.setStatus (Tox.toxCryptoKeys tox) session status
                                                    hPutClient h "sent USERSTATUS"
        -- session <N> typing <TYPINGSTATUS>
        -- send TYPING packet to session N, set typing to TYPINGSTATUS
        ("session", s) | (idStr,"typing",unstripped) <- twoWords s
                        , typingstatus <- strp unstripped
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            case readMaybe typingstatus of
                                                Nothing -> hPutClient h "Unable to parse status"
                                                Just status -> do
                                                    Tox.setTyping (Tox.toxCryptoKeys tox) session status
                                                    hPutClient h "sent TYPINGSTATUS"
        -- session <N> statusmsg <MSG>
        -- send STATUSMESSAGE packet to session N, setting status message to MSG
        ("session", s) | (idStr,"statusmsg",statusmsg) <- twoWords s
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            Tox.setStatusMsg (Tox.toxCryptoKeys tox) session (B.pack statusmsg)
                                            hPutClient h "sent STATUSMESSAGE"
        -- session <N> c <MSG>
        -- send MESSAGE packet to session N (send chat message MSG)
        ("session", s) | (idStr,msgcmd,msg) <- twoWords s
                        , msgcmd `elem` ["c","msg","send"]
                        -> cmd0 $ do
                            lrSession <- strToSession idStr
                            case lrSession of
                                Left s -> hPutClient h s
                                Right session -> do
                                    case mbTox of
                                        Nothing -> hPutClient h "Requires Tox enabled."
                                        Just tox-> do
                                            Tox.sendChatMsg (Tox.toxCryptoKeys tox) session (B.pack msg)
                                            hPutClient h "sent MESSAGE"

        ("onion", s) -> cmd0 $ join $ atomically $ do
            rm <- readTVar $ routeMap onionRouter
            ts <- readTVar $ trampolineNodes onionRouter
            rs <- mapM readTVar (pendingRoutes onionRouter)
            let showRecord :: Int -> Bool -> [String]
                showRecord n True = [show n, "pending", ""]
                showRecord n False
                    | Just RouteRecord{responseCount,timeoutCount} <- IntMap.lookup n rm
                                = [show n, show responseCount, show timeoutCount]
                    | otherwise = [show n, "error!",""]
                r = map (uncurry showRecord) $ IntMap.toAscList rs
            return $ do
                hPutClientChunk h $ "trampolines: " ++ show (IntMap.size ts) ++ "\n"
                hPutClient h $ showColumns $ ["","responses","timeouts"]:r

        -- necrypto <FRIEND-TOXID>
        -- establish a netcrypto session with specified person
        ("netcrypto", s)
                    | Just DHT{..} <- Map.lookup netname dhts
                    -> cmd0 $ do
            case selectedKey of
                Nothing -> hPutClient h "No key is selected, see k command."
                Just mypubkey -> do
                    let keystr = strp s
                        goParse = either
                                         (\_ -> either (hPutClient h . ("Bad netcrypto target: "++))
                                                 (goTarget . Left)
                                                 (readEither keystr)) -- attempt read as NodeInfo type
                                         (goTarget . Right  . Tox.id2key)
                                         $ readEither keystr -- attempt read as NodeId type
                        goTarget (Left userkey_nodeinfo) = do
                            msec <- atomically $ do
                                fmap userSecret . HashMap.lookup (Tox.key2id mypubkey) <$> readTVar (accounts roster)
                            case mbTox of
                                Nothing -> hPutClient h "Requires Tox enabled."
                                Just tox-> do
                                    case msec of
                                        Nothing -> hPutClient h "Error getting secret key"
                                        Just sec -> do
                                            let their_pub = Tox.id2key $ Tox.nodeId userkey_nodeinfo
                                                their_addr = Tox.nodeAddr userkey_nodeinfo
                                            let acsVar = accounts (Tox.toxContactInfo tox)
                                            acsmap <- atomically $ readTVar acsVar
                                            case HashMap.lookup (Tox.key2id mypubkey) acsmap of
                                                Nothing -> hPutClient h "Unable to find account for selected key"
                                                Just account -> do
                                                    atomically $ setContactAddr their_pub their_addr account
                                                    Tox.netCrypto tox sec their_pub
                                                    hPutClient h "Handshake sent"
                        goTarget (Right their_pub) = do
                            msec <- atomically $ do
                                ks <- map swap <$> myKeyPairs roster
                                return $ Data.List.lookup mypubkey ks
                            case mbTox of
                                Nothing -> hPutClient h "Requires Tox enabled."
                                Just tox-> do
                                    case msec of
                                        Nothing -> hPutClient h "Error getting secret key"
                                        Just sec -> do
                                            Tox.netCrypto tox sec their_pub
                                            hPutClient h "Handshake sent"
                    goParse
        ("g", s) | Just DHT{..} <- Map.lookup netname dhts
                    -> cmd0 $ do
                        -- arguments: method
                        --            nid
                        --            (optional dest-ni)
                        self <- atomically $ thisNode <$> readTVar dhtBuckets
                        let (method,xs) = break isSpace $ dropWhile isSpace s
                            (nidstr,ys) = break isSpace $ dropWhile isSpace xs
                            destination = dropWhile isSpace ys
                            goQuery qry = either (hPutClient h . ("Bad search target: "++))
                                                 (goTarget qry)
                                                 $ dhtParseId nidstr
                            goTarget DHTQuery{..} nid =
                                go nid >>= reportResult method qshowR qshowTok show h
                              where
                                go | null destination = fmap Right . qhandler self
                                   | otherwise        = case readEither destination of
                                        Right ni -> fmap (maybe (Left "Timeout.") Right)
                                                    . flip (searchQuery qsearch) ni
                                        Left  e  -> const $ return $ Left ("Bad destination: "++e)
                        maybe (hPutClient h ("Unsupported method: "++method))
                              goQuery
                              $ Map.lookup method dhtQuery

        -- TODO: Online help.
        --
        -- p - put/publish a single given datum on a single given node.
        --
        --         When destination address (node-addr) is optional, it's absense means to
        --         publish information in the local node's own database.
        --
        --         Bittorrent: (peer) publish yourself as peer in swarm.
        --                     (port) set your current bittorrent listen port.
        --
        --             p peer <infohash> <token> [node-addr]
        --
        --             p port <num>
        --
        --         Tox: (toxid) publish a rendezvous onion route to dht node.
        --              (friend) send a friend-request over a rendezvous point.
        --              (dhtkey) send your dht node-id over a rendezvous point.
        --
        --             p toxid <key> <token> <node-addr>
        --
        --             p friend <nospamid> <rendezvous-addr> <text>
        --
        --             p dhtkey <key> <rendezvous-addr>

        ("p", s) | Just DHT{..} <- Map.lookup netname dhts
                    -> cmd0 $ do
                        -- arguments:  Left                  Right
                        --             ----                  -----
                        --             method                method
                        --             data (jid or key)     data
                        --             dest-rendezvous(r)    token
                        --             (optional extra-text) (optional dest-ni)
                        self <- atomically $ thisNode <$> readTVar dhtBuckets
                        let (method,xs) = break isSpace $ dropWhile isSpace s
                            (dtastr,ys) = break isSpace $ dropWhile isSpace xs
                            (tokenstr,zs) = break isSpace $ dropWhile isSpace ys
                            destination = dropWhile isSpace zs
                            goTarget DHTAnnouncable{..} | Right asend <- announceSendData = do
                                let dta = announceParseData dtastr
                                    tok = dta >>= flip announceParseToken tokenstr
                                case liftA2 (,) dta tok of
                                    Left e    -> hPutClient h e
                                    Right nid -> go asend nid >>= either (hPutClient h) (hPutClient h . show)
                              where
                                go asend | null destination = fmap (maybe (Left "Timeout.") Right)
                                                              . flip (uncurry asend) Nothing
                                         | otherwise        = case announceParseAddress destination of
                                            Right ni -> fmap (maybe (Left "Timeout.") Right)
                                                        . flip (uncurry asend) (Just ni)
                                            Left  e  -> const $ return $ Left ("Bad destination: "++e)
                            goTarget DHTAnnouncable{..} | Left (searchName,parseResult,asend) <- announceSendData = do
                                either (hPutClient h) id $ do
                                    dta <- announceParseData $ unwords [dtastr,destination]
                                    r   <- parseResult tokenstr
                                    return $ case selectedKey of
                                        Nothing -> hPutClient h "Missing secret user-key."
                                        Just k  -> do
                                            asend k dta r
                                            hPutClient h "Sent."
                        maybe (hPutClient h ("Unsupported method: "++method))
                              goTarget
                              $ Map.lookup method dhtAnnouncables

        -- TODO: Online documentation.
        --
        -- a - announce, like put/publish but automatically selects nodes to publish on
        --     and periodically refreshes them.
        --
        --     The method name is preceded with a + to start or a - to stop a given
        --     recurring publication.
        --
        --     BitTorrent: (peer) Every minute, announce you are participating
        --                        in a torrent swarm.
        --
        --         a +peer <infohash> a -peer <infohash>
        --
        --     Tox: (toxid) Every 15 seconds, announce your tox identity to the
        --                  DHT so friends can find you.
        --
        --         a +toxid <key>
        --         a -toxid <key>
        --
        --         a +friend <jid> <text>
        --         a +dhtkey <key>
        ("a", "") -> cmd0 $ do
                        now <- getPOSIXTime
                        rs <- atomically $ do
                            as <- readTVar (scheduled $ announcer)
                            forM (PSQ.toList as) $ \(k,ptm,item) -> do
                                kstr <- unpackAnnounceKey announcer k
                                return [ show (ptm - now)
                                       , show (itemStatusNum item)
                                       , kstr
                                       ]
                        hPutClient h $ showColumns rs
        ("a", s) | Just DHT{..} <- Map.lookup netname dhts
                 , not (null s)
                    -> cmd0 $ do
                        let (op:method,xs) = break isSpace $ dropWhile isSpace s
                            dtastr         = dropWhile isSpace xs

                            a = Map.lookup method dhtAnnouncables
                            q = do DHTAnnouncable { announceSendData } <- a
                                   Map.lookup (either (\(search,_,_)->search)
                                                      (const method)
                                                      announceSendData)
                                              dhtQuery
                            doit :: Char -> Announcer -> AnnounceKey -> AnnounceMethod r -> r -> IO ()
                            doit '+' = schedule
                            doit '-' = cancel
                            doit _   = \_ _ _ _ -> hPutClientChunk h "Starting(+) or canceling(-)?"
                            matchingResult ::
                                ( Typeable stok
                                , Typeable ptok
                                , Typeable sni
                                , Typeable pni )
                                            => Search nid addr stok sni sr
                                            -> (pr -> ptok -> Maybe pni -> IO (Maybe pubr))
                                            -> Maybe (stok :~: ptok, sni :~: pni)
                            matchingResult _ _ = liftA2 (,) eqT eqT
                            matchingResult2 ::
                                ( Typeable sr
                                , Typeable pr )
                                            => Search nid addr stok sni sr
                                            -> (PublicKey -> pdta -> pr -> IO ())
                                            -> (pdta -> nid)
                                            -> Maybe (pr :~: sr)
                            matchingResult2 _ _ _ = eqT
                            reportit target = case op of
                                        '+' -> hPutClient h $ "Announcing at " ++ target ++ "."
                                        '-' -> hPutClient h $ "Canceling " ++ target ++ "."
                            -- mameth is for typical kademlia announce.
                            mameth = do
                                DHTAnnouncable { announceSendData
                                               , announceParseData
                                               , announceInterval
                                               , announceTarget } <- a
                                DHTQuery { qsearch } <- q
                                asend <- either (const Nothing) Just announceSendData
                                (Refl, Refl) <- matchingResult qsearch asend
                                -- return $ hPutClient h "Type matches."
                                dta <- either (const Nothing) Just $ announceParseData dtastr
                                return $ do
                                    akey <- atomically $ packAnnounceKey announcer (method ++ ":" ++ dtastr)
                                    doit op announcer
                                            akey
                                            (AnnounceMethod qsearch (Right asend) dhtBuckets
                                                            (announceTarget dta)
                                                            announceInterval)
                                            dta
                                    reportit $ show $ announceTarget dta
                            -- lmeth is for atypical announce messages such as
                            --   Tox dht-key and friend-request messages.
                            lmeth :: Maybe (IO ())
                            lmeth = do
                                DHTAnnouncable { announceSendData
                                               , announceParseData
                                               , announceInterval
                                               , announceTarget } <- a
                                DHTQuery { qsearch } <- q
                                (_,_,asend) <- either Just (const Nothing) announceSendData
                                Refl <- matchingResult2 qsearch asend announceTarget
                                dta <- either (const Nothing) Just $ announceParseData dtastr
                                pub <- selectedKey
                                return $ do
                                    akey <- atomically $ packAnnounceKey announcer (method ++ ":" ++ dtastr)
                                    doit op announcer
                                            akey
                                            (AnnounceMethod qsearch (Left $ asend pub) dhtBuckets
                                                            (announceTarget dta)
                                                            announceInterval)
                                            dta
                                    reportit $ show $ announceTarget dta
                            ptest = fromMaybe "E:NoMethod"
                                        $ fmap (\DHTAnnouncable { announceParseData
                                                                , announceTarget }
                                                  -> either ("E:"++) (show . announceTarget)
                                                          $ announceParseData dtastr)
                                               a

                        let aerror = unlines
                                [ "announce error."
                                , "method = " ++ method
                                , "query = " ++ maybe "nil" (const "ok") q
                                , "publish = " ++ maybe "nil" (const "ok") a
                                -- , "chktok = " ++ maybe "nil" (const "ok") chktok -- chktok = nil
                                -- , "chkni = " ++ maybe "nil" (const "ok") chkni
                                , "ptest = " ++ ptest
                                , "mameth = " ++ show (fmap (const ()) mameth)
                                , "lmeth  = " ++ show (fmap (const ()) lmeth)
                                , "selectedKey = " ++ show selectedKey
                                ]
                        fromMaybe (hPutClient h aerror) $ mameth <|> lmeth

        ("s", s) | Just dht@DHT{..} <- Map.lookup netname dhts
                    -> cmd0 $ do
                        let (method,xs) = break isSpace s
                            (nidstr,ys) = break isSpace $ dropWhile isSpace xs
                            presentSearches = hPutClient h
                                              =<< showSearches
                                              =<< atomically (readTVar dhtSearches)
                            goTarget qry nid = do
                                kvar <- atomically $ newTVar Nothing
                                -- Forking a thread, but it may ubruptly quit if the following
                                -- STM action decides not to add a new search.  This is so that
                                -- I can store the ThreadId into new DHTSearch structure.
                                tid <- fork $ join $ atomically (readTVar kvar >>= maybe retry return)
                                join $ atomically $ do
                                    schs <- readTVar dhtSearches
                                    case Map.lookup (method,nid) schs of
                                        Nothing  -> do forkSearch method nid qry dhtSearches dhtBuckets tid kvar
                                                       return $ presentSearches
                                        Just sch -> do writeTVar kvar (Just $ return ())
                                                       return $ reportSearchResults method h sch
                            goQuery qry = either (hPutClient h . ("Bad search target: "++))
                                                 (goTarget qry)
                                                 $ dhtParseId nidstr
                        if null method then presentSearches
                                       else maybe (hPutClient h ("Unsupported method: "++method))
                                                  goQuery
                                                  $ Map.lookup method dhtQuery

        ("x", s) | Just DHT{..} <- Map.lookup netname dhts
                    -> cmd0 $ do
                        let (method,xs) = break isSpace s
                            (nidstr,ys) = break isSpace $ dropWhile isSpace xs
                            go nid = join $ atomically $ do
                                schs <- readTVar dhtSearches
                                case Map.lookup (method,nid) schs of
                                    Nothing -> return $ hPutClient h "No match."
                                    Just DHTSearch{searchThread} -> do
                                        modifyTVar' dhtSearches (Map.delete (method,nid))
                                        return $ do
                                            killThread searchThread
                                            hPutClient h "Removed search."
                        either (hPutClient h . ("Bad search target: "++)) go $ dhtParseId nidstr

        ("save", _) | Just dht <- Map.lookup netname dhts
                    -> cmd0 $ do
                        saveNodes netname dht
                        hPutClient h $ "Saved " ++ nodesFileName netname ++ "."

        ("load", _) | Just dht <- Map.lookup netname dhts
                    -> cmd0 $ do
                        b <- pingNodes netname dht
                        case b of
                            Just num ->
                                  hPutClient h $ unwords [ "Pinging"
                                                         , show num
                                                         , "nodes from"
                                                         , nodesFileName netname ++ "."
                                                         ]
                            Nothing ->
                                  hPutClient h $ "Failed: " ++ nodesFileName netname ++ "."

        ("swarms", s) -> cmd0 $ do
                    let fltr = case s of
                                    ('-':'v':cs) | all isSpace (take 1 cs)
                                      -> const True
                                    _ -> (\(h,c,n) -> c/=0 )
                    ss <- atomically $ Peers.knownSwarms <$> readTVar (Mainline.contactInfo swarms)
                    let r = map (\(h,c,n) -> (unwords [show h,show c], maybe "" show n))
                            $ filter fltr ss
                    hPutClient h $ showReport r

        ("peers", s) -> cmd0 $ case readEither s of
                        Right ih -> do
                            ps <- atomically $ Peers.lookup ih <$> readTVar (Mainline.contactInfo swarms)
                            hPutClient h $ showReport $ map (((,) "") . show . pPrint) ps
                        Left er -> hPutClient h er
        ("toxids", s) -> cmd0 $ do
                        keydb <- atomically $ readTVar toxkeys
                        now <- getPOSIXTime
                        let entries = map mkentry $ PSQ.toList (Tox.keyByAge keydb)
                            mkentry (k :-> Down tm) = [ show cnt, show k, show (now - tm) ]
                                where Just (_,(cnt,_)) = MM.lookup' k (Tox.keyAssoc keydb)
                        hPutClient h $ showColumns entries

        ("c", s) | Just (ConnectionManager mgr) <- connectionManager
                 , "" <- strp s
                 -> cmd0 $ join $ atomically $ do
            cmap <- connections mgr
            cs <- Map.toList <$> mapM connStatus cmap
            let mkrow = Connection.showKey mgr *** Connection.showStatus mgr
                rs    = map mkrow cs
            return $ do
            hPutClient h $ showReport rs

        ("help", s) | Just DHT{..} <- Map.lookup netname dhts
                    -> cmd0 $ do
            let tolist :: a -> [a]
                tolist = (:[])

                dhtkeys, announcables, ks, allcommands :: [[String]]
                dhtkeys = map tolist $ Map.keys dhts
                queries = map (tolist . ("s "++)) $ Map.keys dhtQuery
                xs = map (tolist . ("x "++)) $ Map.keys dhtQuery
                gs = map (tolist . ("g "++)) $ Map.keys dhtQuery
                announcables = map (tolist . ("p "++)) $ Map.keys dhtAnnouncables
                ks = [["k gen"],["k public"],["k secret"]]
                allcommands = sortBy (comparing (take 1)) $ concat [sessionCommands, dhtkeys, announcables, ks, queries, gs,xs]

            hPutClient h ("Available commands:\n" ++ showColumns allcommands)

        _    -> cmd0 $ hPutClient h "error."


readExternals :: (ni -> SockAddr) -> [TVar (BucketList ni)] -> IO [SockAddr]
readExternals nodeAddr vars = do
    as <- atomically $ mapM (fmap (nodeAddr . selfNode) . readTVar) vars
    let unspecified (SockAddrInet _ 0)              = True
        unspecified (SockAddrInet6 _ _ (0,0,0,0) _) = True
        unspecified _                               = False
    -- TODO: Filter to only global addresses?
    return $ filter (not . unspecified) as

data Options = Options
    { portbt    :: String
    , porttox   :: String
    , portxmpp  :: String -- client-to-server
    , portxmppS :: String -- server-to-server
    , ip6bt     :: Bool
    , ip6tox    :: Bool
    , dhtkey    :: Maybe SecretKey
    -- | Currently only relevant to XMPP server code.
    --
    --   [ 0 ] Don't log XMPP stanzas.
    --
    --   [ 1 ] Log non-ping stanzas.
    --
    --   [ 2 ] Log all stanzas, even pings.
    , verbosity :: Int
    }
 deriving (Eq,Show)

sensibleDefaults :: Options
sensibleDefaults = Options
    { portbt    = "6881"
    , porttox   = "33445"
    , portxmpp  = "5222"
    , portxmppS = "5269"
    , ip6bt     = True
    , ip6tox    = True
    , dhtkey    = Nothing
    , verbosity = 2
    }

-- bt=<port>,tox=<port>
-- -4
parseArgs :: [String] -> Options -> Options
parseArgs []                  opts = opts
parseArgs ("--dhtkey":k:args) opts = parseArgs args opts
    { dhtkey = decodeSecret $ B.pack k }
parseArgs ("--dht-key":k:args) opts = parseArgs args opts
    { dhtkey = decodeSecret $ B.pack k }
parseArgs ("-4":args) opts         = parseArgs args opts
    { ip6bt  = False
    , ip6tox = False }
parseArgs (arg:args) opts          = parseArgs args opts
    { portbt    = fromMaybe (portbt opts)  $ Prelude.lookup "bt"  ports
    , porttox   = fromMaybe (porttox opts) $ Prelude.lookup "tox" ports
    , portxmpp  = fromMaybe (portxmpp opts) $ Prelude.lookup "xmpp" ports
    , portxmppS = fromMaybe (portxmppS opts) $ Prelude.lookup "xmpp.s2s" ports }
 where
    ports = map ( (dropWhile (==',') *** dropWhile (=='='))
                . break (=='=') )
                $ groupBy (const (/= ',')) arg

noArgPing :: (x -> IO (Maybe r)) -> [String] -> x -> IO (Maybe r)
noArgPing f [] x = f x
noArgPing _ _  _ = return Nothing

-- | Create a Conduit Source by repeatedly calling an IO action.
ioToSource :: IO (Maybe x) -> IO () -> C.Source IO x
ioToSource !action !onEOF = liftIO action >>= \case
        Nothing   -> liftIO onEOF
        Just item -> do C.yield item
                        ioToSource action onEOF

newXmmpSink :: Tox.NetCryptoSession -> C.Sink (Flush Tox.CryptoMessage) IO ()
newXmmpSink session@(Tox.NCrypto { ncOutgoingQueue = Tox.HaveHandshake outq, ncPacketQueue }) = C.awaitForever $ \flush_cyptomessage -> do
    let sendit :: Tox.NetCryptoSession -> Flush Tox.CryptoMessage -> IO ()
        sendit session (Chunk msg) = do
            extra <- readyOutGoing outq
            r <- atomically $ do
                rTry <- tryAppendQueueOutgoing extra outq msg
                case rTry of
                    OGFull -> retry
                    OGSuccess -> return OGSuccess
                    OGEncodeFail -> return OGEncodeFail
            when (r == OGEncodeFail) $
                hPutStrLn stderr ("FAILURE to Encode Outgoing: " ++ show msg)
        sendit session Flush = return ()
    liftIO $ sendit session flush_cyptomessage


toxAnnounceSendData :: Tox.Tox -> PublicKey
                               -> Nonce32
                               -> Maybe Tox.NodeInfo
                               -> IO (Maybe (Tox.Rendezvous, Tox.AnnounceResponse))
toxAnnounceSendData tox pubkey token = \case
    Just ni -> Tox.putRendezvous (Tox.onionTimeout tox)
                                 (Tox.toxCryptoKeys tox)
                                 (Tox.toxOnion tox)
                                 (pubkey :: PublicKey)
                                 (token :: Nonce32)
                                 ni
    Nothing -> return Nothing

toxQSearch :: Tox.Tox -> Search Tox.NodeId (IP, PortNumber) Nonce32 Tox.NodeInfo Tox.Rendezvous
toxQSearch tox = Tox.toxidSearch (Tox.onionTimeout tox) (Tox.toxCryptoKeys tox) (Tox.toxOnion tox)

toxAnnounceInterval :: POSIXTime
toxAnnounceInterval = 15

-- |
--
-- These hooks will be invoked in order to connect to *.tox hosts in a user's
-- XMPP roster.
toxman :: Announcer -> [(String,TVar (BucketList Tox.NodeInfo))] -> Tox.Tox -> PresenceState -> ToxManager ConnectionKey
toxman announcer toxbkts tox presence = ToxManager
    { activateAccount        = \k pubname seckey -> do
        hPutStrLn stderr $ "toxman ACTIVATE " ++ show (Tox.key2id $ toPublic seckey)
        let ContactInfo{ accounts } = Tox.toxContactInfo tox
            pub = toPublic seckey
            pubid = Tox.key2id pub
        (acnt,newlyActive) <- atomically $ do
            macnt <- HashMap.lookup pubid <$> readTVar accounts
            acnt <- maybe (newAccount seckey) return macnt
            rs <- readTVar $ clientRefs acnt
            writeTVar (clientRefs acnt) $! Set.insert k rs
            modifyTVar accounts (HashMap.insert pubid acnt)
            if not (Set.null rs)
                then return (acnt,[])
                else do
                    fmap ((,) acnt) $ forM toxbkts $ \(nm,bkts) -> do
                        akey <- packAnnounceKey announcer (nm ++ "id:" ++ show pubid)
                        return (akey,bkts)
        forM_ newlyActive $ \(akey,bkts) -> do
            -- Schedule recurring announce.
            --
            schedule announcer
                     akey
                     (AnnounceMethod (toxQSearch tox)
                                     (Right $ toxAnnounceSendData tox)
                                     bkts
                                     pubid
                                     toxAnnounceInterval)
                     pub

            forkAccountWatcher acnt tox presence
            return ()

    , deactivateAccount      = \k pubname -> do
       hPutStrLn stderr $ "toxman DECTIVATE (todo) 1 " ++ show pubname
       let ContactInfo{ accounts } = Tox.toxContactInfo tox
           mpubid = readMaybe $ T.unpack $ T.take 43 pubname
       bStopped <- fmap (fromMaybe Nothing) $ atomically $ do
        forM mpubid $ \pubid -> do
        refs <- do
            macnt <- HashMap.lookup pubid <$> readTVar accounts
            rs <- fromMaybe Set.empty <$> mapM (readTVar . clientRefs) macnt
            forM_ macnt $ \acnt -> do
                modifyTVar' (clientRefs acnt) $ Set.delete k
            return rs
        if (Set.null $ refs Set.\\ Set.singleton k) then do
            --   TODO
            --   If this is the last reference to a non-connected contact:
            --      Stop the recurring search for that contact
            --
            -- Stop recurring announce.
            fmap Just $ forM toxbkts $ \(nm,bkts) -> do
                akey <- packAnnounceKey announcer (nm ++ "id:" ++ show pubid)
                return (akey,bkts)
        else return Nothing
       forM_ bStopped $ \kbkts -> do
           hPutStrLn stderr $ "toxman DECTIVATE (todo) 3 " ++ show pubname
           let Just pubid = mpubid
               pub        = Tox.id2key pubid
           forM_ kbkts $ \(akey,bkts) -> do
                cancel announcer
                     akey
                     (AnnounceMethod (toxQSearch tox)
                                     (Right $ toxAnnounceSendData tox)
                                     bkts
                                     pubid
                                     toxAnnounceInterval)
                     pub

    , setToxConnectionPolicy = \me them p -> do
       hPutStrLn stderr $ "toxman ConnectionPolicy " ++ show (me,them,p)
       case p of
        TryingToConnect -> do
            let db@ContactInfo{ accounts } = Tox.toxContactInfo tox
            sequence_ $ do
            meid   <- readMaybe $ T.unpack $ T.take 43 me
            themid <- readMaybe $ T.unpack $ T.take 43 them
            Just $ atomically $ do
            accs <- readTVar accounts
            case HashMap.lookup meid accs of
                Nothing  -> return () -- Unknown account.
                Just acc -> setContactPolicy (Tox.id2key themid) TryingToConnect acc
            -- If unscheduled and unconnected, schedule recurring search for this contact.
        _ -> return () -- Remove contact.
    }

-- | Called upon a new Tox friend-connection session with a remote peer in
-- order to set up translating conduits that simulate a remote XMPP server.
announceToxJabberPeer :: PublicKey -- ^ Remote tox node's long-term user key.
                      -> TChan ((ConnectionKey,SockAddr), Tcp.ConnectionEvent XML.Event)
                      -> SockAddr -- ^ Local bind address for incoming Tox packets.
                      -> SockAddr -- ^ Remote address for this connection.
                      -> STM Bool
                      -> C.Source IO Tox.CryptoMessage
                      -> C.Sink (Flush Tox.CryptoMessage) IO ()
                      -> IO (Maybe (Tox.NetCryptoSession -> Tox.NetCryptoSession))
announceToxJabberPeer them echan laddr saddr pingflag tsrc tsnk
    = do
     atomically $ writeTChan echan
        ( (PeerKey saddr, laddr )
        , Tcp.Connection pingflag xsrc xsnk )
     return Nothing
 where
    xsrc = tsrc =$= toxToXmpp (T.pack $ show them ++ ".tox")
    xsnk = flushPassThrough xmppToTox =$= tsnk

vShowMe :: Tox.ViewSnapshot -> Int -> B.ByteString
vShowMe (Tox.ViewSnapshot { vNick, vStatus, vStatusMsg, vTyping }) indent
    = B.unlines
    . map doRow $  [ ["Nick: ", vNick ]
                   , ["Status: ", "(" <> pshow vStatus <> ") " <> vStatusMsg ]
                   , ["Typing: ", pshow vTyping ]
                   ]
    where (<>) = B.append
          space = B.replicate indent ' '
          doRow = B.append space . B.concat

vShowThem :: Tox.ViewSnapshot -> Int -> B.ByteString
vShowThem (Tox.ViewSnapshot { vTheirNick, vTheirStatus, vTheirStatusMsg, vTheirTyping }) indent
    = B.unlines
    . map doRow $  [ ["Nick: ", vTheirNick ]
                   , ["Status: ", "(" <> pshow vTheirStatus <> ") " <> vTheirStatusMsg ]
                   , ["Typing: ", pshow vTheirTyping ]
                   ]
    where (<>) = B.append
          space = B.replicate indent ' '
          doRow = B.append space . B.concat

showMsg ::(Word32, (Bool,(Tox.ViewSnapshot, InOrOut Tox.CryptoMessage))) -> B.ByteString
showMsg (n,(flg,(snapshot,iocm))) = B.concat [bool "  " "h " flg, showmsg' (snapshot,iocm)]
    where
        showmsg' (snapshot,In cm) = B.concat [Tox.vNick snapshot,"> ", pshow cm]
        showmsg' (snapshot,Out cm) = B.concat [utf8boldify (Tox.vNick snapshot),": ",pshow cm]
        utf8boldify s = boldify (T.decodeUtf8With E.lenientDecode s)
            where
                boldify :: T.Text -> B.ByteString
                boldify j = E.encodeUtf8 $ T.map addIt j
                    where addIt x = let o = ord x in case o of
                            _ | o <= 90 && o >= 65  -> chr (o + 119743)
                            _ | o <= 122 && o >= 97 -> chr (o + 119737)
                            _ -> x


main :: IO ()
main = runResourceT $ liftBaseWith $ \resT -> do
    args <- getArgs
    let opts = parseArgs args sensibleDefaults
    print opts

    swarms <- Mainline.newSwarmsDatabase
    -- Restore peer database before forking the listener thread.
    peerdb <- left show <$> tryIOError (L.readFile "bt-peers.dat")
    either (hPutStrLn stderr . ("bt-peers.dat: "++))
           (atomically . writeTVar (Mainline.contactInfo swarms))
           (peerdb >>= S.decodeLazy)

    announcer <- forkAnnouncer


    (quitBt,btdhts,btips,baddrs) <- case portbt opts of
        "" -> return (return (), Map.empty,return [],[])
        p  -> do
            addr <- getBindAddress p (ip6bt opts)
            (bt,btR,btBootstrap4, btBootstrap6) <- Mainline.newClient swarms addr
            quitBt <- forkListener "bt" (clientNet bt)
            mainlineSearches <- atomically $ newTVar Map.empty
            peerPort <- atomically $ newTVar 6881 -- BitTorrent client TCP port.
            let mainlineDHT bkts wantip = DHT
                    { dhtBuckets = bkts btR
                    , dhtPing    = Map.singleton "ping" $ DHTPing
                                        { pingQuery = noArgPing $ fmap (bool Nothing (Just ())) . Mainline.ping bt
                                        , pingShowResult = show
                                        }
                    , dhtQuery   = Map.fromList
                                    [ ("node", DHTQuery
                                        { qsearch = (Mainline.nodeSearch bt)
                                        , qhandler = (\ni -> fmap Mainline.unwrapNodes
                                                                . Mainline.findNodeH btR ni
                                                                . flip Mainline.FindNode (Just Want_Both))
                                        , qshowR = show
                                        , qshowTok = (const Nothing)
                                        })
                                    -- Maybe (sr :~: pr, stok :~: ptok, sni :~: pni )
                                    -- sr = InfoHash
                                    -- stok = Token
                                    -- sni = NodeInfo
                                    , ("peer", DHTQuery
                                        { qsearch = (Mainline.peerSearch bt)
                                        , qhandler = (\ni -> fmap Mainline.unwrapPeers
                                                                . Mainline.getPeersH btR swarms ni
                                                                . flip Mainline.GetPeers (Just Want_Both)
                                                                . (read . show)) -- TODO: InfoHash -> NodeId
                                        , qshowR = (show . pPrint)
                                        , qshowTok = (Just . show)
                                        })
                                    ]
                    , dhtParseId  = readEither :: String -> Either String Mainline.NodeId
                    , dhtSearches = mainlineSearches
                    , dhtFallbackNodes = Mainline.bootstrapNodes wantip
                    , dhtAnnouncables = Map.fromList
                                    -- Maybe (sr :~: pr, stok :~: ptok, sni :~: pni )
                                    -- dta = Announce
                                    -- pr = Announced
                                    -- ptok = Token
                                    -- pni = NodeInfo
                                    [ ("peer", DHTAnnouncable { announceSendData = Right $ \ih tok -> \case
                                                                    Just ni -> do
                                                                        port <- atomically $ readTVar peerPort
                                                                        let dta = Mainline.mkAnnounce port ih tok
                                                                        Mainline.announce bt dta ni
                                                                    Nothing -> return Nothing
                                                              , announceParseAddress = readEither
                                                              , announceParseData = readEither
                                                              , announceParseToken = const $ readEither
                                                              , announceInterval = 60 -- TODO: Is one minute good?
                                                              , announceTarget = (read . show) -- TODO: InfoHash -> NodeId -- peer
                                                              })
                                    , ("port", DHTAnnouncable { announceParseData = readEither
                                                              , announceParseToken = \_ _ -> return ()
                                                              , announceParseAddress = const $ Right ()
                                                              , announceSendData = Right $ \dta () -> \case
                                                                    Nothing -> do atomically $ writeTVar peerPort (dta :: PortNumber)
                                                                                  return $ Just dta
                                                                    Just _ -> return Nothing
                                                              , announceInterval = 0 -- TODO: The "port" setting should probably
                                                                                     -- be a command rather than an announcement.
                                                              , announceTarget = const $ Mainline.zeroID
                                                              })]

                    , dhtSecretKey = return Nothing
                    , dhtBootstrap = case wantip of
                                        Want_IP4 -> btBootstrap4
                                        Want_IP6 -> btBootstrap6
                    }
                dhts = Map.fromList $
                        ("bt4", mainlineDHT Mainline.routing4 Want_IP4)
                        : if ip6bt opts
                            then [ ("bt6", mainlineDHT Mainline.routing6 Want_IP6) ]
                            else []
                ips :: IO [SockAddr]
                ips = readExternals Mainline.nodeAddr
                                        [ Mainline.routing4 btR
                                        , Mainline.routing6 btR
                                        ]
            return (quitBt,dhts,ips, [addr])

    keysdb <- Tox.newKeysDatabase

    _crypto <- Tox.newCrypto
    _netCryptoSessionsState <- Tox.newSessionsState _crypto Tox.defaultUnRecHook Tox.defaultCryptoDataHooks
    (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- case porttox opts of
        "" -> return (Nothing,return (), Map.empty, return [],[])
        toxport -> do
            addrTox <- getBindAddress toxport (ip6tox opts)
            hPutStrLn stderr $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts))
            tox <- Tox.newTox keysdb
                              addrTox
                              (Just _netCryptoSessionsState)
                              (dhtkey opts)
            (quitTox, toxStrap4, toxStrap6) <- Tox.forkTox tox

            toxSearches <- atomically $ newTVar Map.empty

            let toxDHT bkts wantip = DHT
                    { dhtBuckets = bkts (Tox.toxRouting tox)
                    , dhtPing    = Map.fromList
                                    [ ("ping", DHTPing
                                            { pingQuery = noArgPing $ fmap (bool Nothing (Just ())) . Tox.ping (Tox.toxDHT tox)
                                            , pingShowResult = show
                                            })
                                    , ("cookie", DHTPing
                                            { pingQuery = \case
                                                [keystr] | Just mykey <- readMaybe keystr
                                                         -> Tox.cookieRequest (Tox.toxCryptoKeys tox)
                                                                              (Tox.toxDHT tox)
                                                                              (Tox.id2key mykey)
                                                _        -> const $ return Nothing
                                            , pingShowResult = show
                                            })]
                    , dhtQuery   = Map.fromList
                                    [ ("node", DHTQuery
                                        { qsearch = (Tox.nodeSearch $ Tox.toxDHT tox)
                                        , qhandler = (\ni -> fmap Tox.unwrapNodes
                                                                . Tox.getNodesH (Tox.toxRouting tox) ni
                                                                . Tox.GetNodes)
                                        , qshowR = show -- NodeInfo
                                        , qshowTok = (const Nothing)
                                        })
                                    , ("toxid", DHTQuery
                                        { qsearch = toxQSearch tox
                                        , qhandler = -- qhandler :: ni -> nid -> IO ([ni], [r], tok)
                                                         (\ni nid ->
                                                            Tox.unwrapAnnounceResponse Nothing
                                                                <$> clientAddress (Tox.toxDHT tox) Nothing
                                                                <*> Tox.announceH (Tox.toxRouting tox)
                                                                                  (Tox.toxTokens tox)
                                                                                  (Tox.toxAnnouncedKeys tox)
                                                                                  (Tox.OnionDestination Tox.SearchingAlias ni Nothing)
                                                                                  (Tox.AnnounceRequest zeros32 nid Tox.zeroID))
                                        , qshowR  = show -- Rendezvous
                                        , qshowTok = Just . show -- Nonce32
                                        })
                                    ]
                    , dhtParseId  = readEither :: String -> Either String Tox.NodeId
                    , dhtSearches = toxSearches
                    , dhtFallbackNodes = return []
                    , dhtAnnouncables = Map.fromList
                                    -- To announce your own tox OrjBG... identity is online:
                                    --
                                    -- > a +toxid OrjBG.GyWuQhGc1pb0KssgmYAocohFh35Vx8mREC9Nu
                                    [ ("toxid", DHTAnnouncable { announceSendData = Right (toxAnnounceSendData tox)
                                                               , announceParseAddress = readEither
                                                               , announceParseToken = const $ readEither
                                                               , announceParseData = fmap Tox.id2key . readEither
                                                               , announceTarget = Tox.key2id -- toxid

                    -- For peers we are announcing ourselves to, if we are not
                    -- announced to them toxcore tries every 3 seconds to
                    -- announce ourselves to them until they return that we
                    -- have announced ourselves to, then toxcore sends an
                    -- announce request packet every 15 seconds to see if we
                    -- are still announced and re announce ourselves at the
                    -- same time. The timeout of 15 seconds means a `ping_id`
                    -- received in the last packet will not have had time to
                    -- expire (20 second minimum timeout) before it is resent
                    -- 15 seconds later. Toxcore sends every announce packet
                    -- with the `ping_id` previously received from that peer
                    -- with the same path (if possible).
                                                               , announceInterval = toxAnnounceInterval

                                                               })
                                    -- dhtkey parameters:
                                    --
                                    -- ni  = NodeInfo
                                    -- r   = Rendezvous
                                    -- tok = Nonce32
                                    -- dta = PublicKey{-them-}
                                    --
                                    -- Using k-selected identity, to share your dht
                                    -- key with remote tox user
                                    -- "KWoEx1XQHrluIoW.3nK6BFb6XCebKWr3nDDt3V7CcoJ"
                                    -- ...
                                    --
                                    -- > a +dhtkey KWoEx1XQHrluIoW.3nK6BFb6XCebKWr3nDDt3V7CcoJ
                                    , ("dhtkey", DHTAnnouncable { announceSendData = Left ("toxid", readEither, \me them addr -> do
                                                                        dkey <- Tox.getContactInfo tox
                                                                        sendMessage
                                                                            (Tox.toxToRoute tox)
                                                                            (Tox.AnnouncedRendezvous them addr)
                                                                            (me,Tox.OnionDHTPublicKey dkey))
                                                                , announceParseAddress = \str -> do
                                                                        ni <- readEither str
                                                                        return ( ni :: Tox.NodeInfo )
                                                                , announceParseToken = \_ str -> do
                                                                        tok <- readEither str
                                                                        return ( tok :: Nonce32 )
                                                                , announceParseData = fmap Tox.id2key . readEither
                                                                , announceTarget = Tox.key2id

                    -- We send this packet every 30 seconds if there is more
                    -- than one peer (in the 8) that says they our friend is
                    -- announced on them. This packet can also be sent through
                    -- the DHT module as a DHT request packet (see DHT) if we
                    -- know the DHT public key of the friend and are looking
                    -- for them in the DHT but have not connected to them yet.
                    -- 30 second is a reasonable timeout to not flood the
                    -- network with too many packets while making sure the
                    -- other will eventually receive the packet. Since packets
                    -- are sent through every peer that knows the friend,
                    -- resending it right away without waiting has a high
                    -- likelihood of failure as the chances of packet loss
                    -- happening to all (up to to 8) packets sent is low.
                    --
                                                               , announceInterval = 30

                                                               })
                                    -- "friend" parameters
                                    --
                                    -- ni  = NodeInfo
                                    -- r   = Rendezvous
                                    -- tok = Nonce32
                                    -- dta = (NoSpamId{-them-},String)
                                    --
                                    -- Using k-selected identity, to send a
                                    -- friend-request to the JID $TESTZ300@OrjBG...:
                                    --
                                    -- > a +friend $TESTZ300@OrjBG.GyWuQhGc1pb0KssgmYAocohFh35Vx8mREC9Nu.tox Hey, add me!
                                    --
                                    , ("friend", DHTAnnouncable { announceSendData = Left ("toxid", readEither, \me them0 addr -> do
                                                                        let (Tox.NoSpamId sum them,txt) = them0
                                                                            Tox.NoSpam nospam _ = sum
                                                                            fr = Tox.FriendRequest nospam (T.encodeUtf8 $ T.pack txt)
                                                                        sendMessage
                                                                            (Tox.toxToRoute tox)
                                                                            (Tox.AnnouncedRendezvous them addr)
                                                                            (me,Tox.OnionFriendRequest fr))
                                                                , announceParseAddress = \str -> do
                                                                        ni <- readEither str
                                                                        return ( ni :: Tox.NodeInfo )
                                                                , announceParseToken = \_ str -> do
                                                                        tok <- readEither str
                                                                        return ( tok :: Nonce32 )
                                                                , announceParseData = \str -> do
                                                                        let (jidstr,txt) = break isSpace str
                                                                        jid <- readEither jidstr
                                                                        return (jid, drop 1 txt)
                                                                , announceTarget = \(Tox.NoSpamId _ pub,_) -> Tox.key2id pub

                    -- Friend requests are sent with exponentially increasing
                    -- interval of 2 seconds, 4 seconds, 8 seconds, etc... in
                    -- toxcore. This is so friend requests get resent but
                    -- eventually get resent in intervals that are so big that
                    -- they essentially expire. The sender has no way of
                    -- knowing if a peer refuses a friend requests which is why
                    -- friend requests need to expire in some way.  Note that
                    -- the interval is the minimum timeout, if toxcore cannot
                    -- send that friend request it will try again until it
                    -- manages to send it. One reason for not being able to
                    -- send the friend request would be that the onion has not
                    -- found the friend in the onion and so cannot send an
                    -- onion data packet to them.
                    --
                    -- TODO: Support exponential backoff behavior.  For now, setting
                    -- interval to 8 seconds.

                                                               , announceInterval = 8
                                                               })]
                    , dhtSecretKey = return $ Just $ transportSecret (Tox.toxCryptoKeys tox)
                    , dhtBootstrap = case wantip of
                                        Want_IP4 -> toxStrap4
                                        Want_IP6 -> toxStrap6
                    }
                dhts = Map.fromList $
                        ("tox4", toxDHT Tox.routing4 Want_IP4)
                        : if ip6tox opts
                            then [ ("tox6", toxDHT Tox.routing6 Want_IP6) ]
                            else []
                ips :: IO [SockAddr]
                ips = readExternals Tox.nodeAddr [ Tox.routing4 $ Tox.toxRouting tox
                                                 , Tox.routing6 $ Tox.toxRouting tox ]
            return (Just tox, quitTox, dhts, ips, [addrTox])

    let netCryptoSessionsState = maybe _netCryptoSessionsState Tox.toxCryptoSessions mbtox

    _ <- UPNP.requestPorts "dht-client" $ map (Datagram,) $ baddrs ++ taddrs

    (msv,mconns,mstate) <- case portxmpp opts of
        "" -> return (Nothing,Nothing,Nothing)
        p  -> do
            cport <- getBindAddress p True{-IPv6 supported-}
            -- TODO: Allow running without an XMPP server-to-server port.
            --       This should probably be default for toxmpp use.
            sport <- getBindAddress (portxmppS opts) True{-IPv6 supported-}

            -- XMPP initialization
            cw        <- newConsoleWriter
            serverVar <- atomically $ newEmptyTMVar
            let lookupBkts :: String -> Map.Map String DHT -> Maybe (String,TVar (BucketList Tox.NodeInfo))
                lookupBkts name m = case Map.lookup name m of
                    Nothing              -> Nothing
                    Just DHT{dhtBuckets} -> cast (name, dhtBuckets)
            let toxbkts = catMaybes
                            [ lookupBkts "tox4" toxdhts
                            , lookupBkts "tox6" toxdhts
                            ]

            state     <- newPresenceState cw (toxman announcer toxbkts <$> mbtox) serverVar

            sv    <- resT $ xmppServer (presenceHooks state (verbosity opts) (Just cport) (Just sport))
            -- We now have a server object but it's not ready to use until
            -- we put it into the 'server' field of our /state/ record.
            conns     <- xmppConnections  sv
            atomically $ do
                putTMVar serverVar  (sv,conns) -- Okay, now it's ready. :)
                                               -- FIXME: This is error prone.
            return (Just sv, Just conns, Just state)

    forM_ (take 1 taddrs) $ \addrTox -> do
        atomically $ Tox.addNewSessionHook netCryptoSessionsState $ \mbNoSpam netcrypto -> do
            -- allsessionsMap <- atomically $ readTVar (netCryptoSessions netCryptoSessionsState)
            let sockAddr = Tox.ncSockAddr netcrypto
                pubKey   = Tox.ncTheirPublicKey netcrypto
            tmchan <- atomically newTMChan
            let Just pingMachine = Tox.ncPingMachine netcrypto
                pingflag         = readTVar (pingFlag pingMachine)
                receiveCrypto    = atomically $ readTMChan tmchan
                onEOF            = return () -- TODO: Update toxContactInfo, not connected.
                xmppSrc          = ioToSource receiveCrypto onEOF
                xmppSink         = newXmmpSink netcrypto
            forM_ msv $ \sv -> do
                let Tox.HaveDHTKey saddr = Tox.ncSockAddr netcrypto
                announceToxJabberPeer (Tox.ncTheirPublicKey netcrypto) (xmppEventChannel sv) addrTox saddr pingflag xmppSrc xmppSink
            -- TODO: Update toxContactInfo, connected.
            atomically $ do
                supply <- readTVar (Tox.listenerIDSupply netCryptoSessionsState)
                let (listenerId,supply') = freshId supply
                writeTVar (Tox.listenerIDSupply netCryptoSessionsState) supply'
                modifyTVar' (Tox.ncListeners netcrypto) (IntMap.insert listenerId (0,tmchan))
            return Nothing

    let dhts = Map.union btdhts toxdhts

    (waitForSignal, checkQuit) <- do
        signalQuit <- atomically $ newTVar False
        let quitCommand = atomically $ writeTVar signalQuit True
        installHandler sigTERM (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing
        installHandler sigINT (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing
        let defaultToxData = do
                rster <- Tox.newContactInfo
                orouter <- newOnionRouter (hPutStrLn stderr)
                return (rster, orouter)
        (rstr,orouter) <- fromMaybe defaultToxData $ do
            tox <- mbtox
            return $ return ( Tox.toxContactInfo tox, Tox.toxOnionRoutes tox )
        let session = clientSession0 $ Session
                { netname           = concat $ take 1 $ Map.keys dhts -- initial default DHT
                , selectedKey       = Nothing
                , dhts              = dhts  -- all DHTs
                , signalQuit        = quitCommand
                , swarms            = swarms
                , cryptosessions    = netCryptoSessionsState
                , toxkeys           = keysdb
                , roster            = rstr
                , announceToLan     = fromMaybe (return ()) $ Tox.toxAnnounceToLan <$> mbtox
                , connectionManager = ConnectionManager <$> mconns
                , onionRouter       = orouter
                , externalAddresses = liftM2 (++) btips toxips
                , announcer         = announcer
                , mbTox             = mbtox
                }
        srv <- streamServer (withSession session) (SockAddrUnix "dht.sock")
        return ( do atomically $ readTVar signalQuit >>= check
                    quitListening srv
               , readTVar signalQuit >>= check
               )


    forM_ (Map.toList dhts)
          $ \(netname, dht@DHT { dhtBuckets = bkts
                               , dhtQuery = qrys
                               , dhtPing = pings
                               , dhtFallbackNodes = getBootstrapNodes
                               , dhtBootstrap = bootstrap }) -> do
        btSaved <- loadNodes netname --  :: IO [Mainline.NodeInfo]
        putStrLn $ "Loaded "++show (length btSaved)++" nodes for "++netname++"."
        fallbackNodes <- getBootstrapNodes
        let isNodesSearch :: ni :~: r -> Search nid addr tok ni r -> Search nid addr tok ni ni
            isNodesSearch Refl sch = sch
            ping = maybe (const $ return False)
                         (\DHTPing{pingQuery} -> fmap (maybe False (const True)) . pingQuery [])
                         $ Map.lookup "ping" pings
        fork $ do
            myThreadId >>= flip labelThread ("bootstrap."++netname)
            bootstrap btSaved fallbackNodes
        return ()

    forkIO $ do
        myThreadId >>= flip labelThread "XMPP.stanzas"
        let console = cwPresenceChan <$> (mstate >>= consoleWriter)
        fix $ \loop -> do
        what <- atomically
            $ orElse (do (client,stanza) <- maybe retry takeTMVar console
                         return $ forM_ mstate $ \state -> do
                                     informClientPresence0 state Nothing client stanza
                                     loop)
                     (checkQuit >> return (return ()))
        what

    forM msv $ \_ -> hPutStrLn stderr "Started XMPP server."

    -- Wait for DHT and XMPP threads to finish.
    -- Use ResourceT to clean-up XMPP server.
    waitForSignal

    stopAnnouncer announcer
    quitBt
    quitTox

    swarmsdb <- atomically $ readTVar (Mainline.contactInfo swarms)
    L.writeFile "bt-peers.dat" $ S.encodeLazy swarmsdb