summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-05-18 23:16:46 -0400
committerjoe <joe@jerkface.net>2018-05-18 23:16:46 -0400
commitea3c97cea6cb2a690afca743fa8fecfbb533d69b (patch)
tree463c0ad5176f9978c8f1732069dfdb82f610e937
parent6eefc1e5112753a9f01396e6ba54dca0b7f56c04 (diff)
Daemon options to enable or disable XMPP.
-rw-r--r--Presence/Presence.hs7
-rw-r--r--Presence/XMPPServer.hs7
-rw-r--r--examples/dhtd.hs56
-rw-r--r--todo.txt4
4 files changed, 46 insertions, 28 deletions
diff --git a/Presence/Presence.hs b/Presence/Presence.hs
index c556a170..97b9d5b8 100644
--- a/Presence/Presence.hs
+++ b/Presence/Presence.hs
@@ -13,7 +13,7 @@ import Control.Concurrent.STM.TMVar
13import Control.Monad.Trans.Resource (runResourceT) 13import Control.Monad.Trans.Resource (runResourceT)
14import Control.Monad.Trans 14import Control.Monad.Trans
15import Control.Monad.IO.Class (MonadIO, liftIO) 15import Control.Monad.IO.Class (MonadIO, liftIO)
16import Network.Socket ( SockAddr(..) ) 16import Network.Socket ( SockAddr(..), PortNumber )
17import System.Endian (fromBE32) 17import System.Endian (fromBE32)
18import Data.List (nub, (\\), intersect, groupBy, sort, sortBy ) 18import Data.List (nub, (\\), intersect, groupBy, sort, sortBy )
19import Data.Ord (comparing ) 19import Data.Ord (comparing )
@@ -124,8 +124,8 @@ newPresenceState cw toxman xmpp = atomically $ do
124 } 124 }
125 125
126 126
127presenceHooks :: PresenceState -> Int -> XMPPServerParameters 127presenceHooks :: PresenceState -> Int -> Maybe PortNumber -> XMPPServerParameters
128presenceHooks state verbosity = XMPPServerParameters 128presenceHooks state verbosity mport = XMPPServerParameters
129 { xmppChooseResourceName = chooseResourceName state 129 { xmppChooseResourceName = chooseResourceName state
130 , xmppTellClientHisName = tellClientHisName state 130 , xmppTellClientHisName = tellClientHisName state
131 , xmppTellMyNameToClient = textHostName 131 , xmppTellMyNameToClient = textHostName
@@ -148,6 +148,7 @@ presenceHooks state verbosity = XMPPServerParameters
148 , xmppClientInformSubscription = clientInformSubscription state 148 , xmppClientInformSubscription = clientInformSubscription state
149 , xmppPeerInformSubscription = peerInformSubscription state 149 , xmppPeerInformSubscription = peerInformSubscription state
150 , xmppVerbosity = return verbosity 150 , xmppVerbosity = return verbosity
151 , xmppClientPort = fromMaybe 5222 mport
151 } 152 }
152 153
153 154
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index ea1500d4..c9132d0f 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -92,8 +92,8 @@ import qualified Connection
92 92
93peerport :: PortNumber 93peerport :: PortNumber
94peerport = 5269 94peerport = 5269
95clientport :: PortNumber 95-- clientport :: PortNumber
96clientport = 5222 96-- clientport = 5222
97 97
98my_uuid :: Text 98my_uuid :: Text
99my_uuid = "154ae29f-98f2-4af4-826d-a40c8a188574" 99my_uuid = "154ae29f-98f2-4af4-826d-a40c8a188574"
@@ -213,6 +213,7 @@ data XMPPServerParameters =
213 , -- | Called when a remote peer informs us of our subscription status. 213 , -- | Called when a remote peer informs us of our subscription status.
214 xmppPeerInformSubscription :: IO () -> ConnectionKey -> Stanza -> IO () 214 xmppPeerInformSubscription :: IO () -> ConnectionKey -> Stanza -> IO ()
215 , xmppVerbosity :: IO Int 215 , xmppVerbosity :: IO Int
216 , xmppClientPort :: PortNumber -- 5222
216 } 217 }
217 218
218 219
@@ -1841,7 +1842,7 @@ xmppServer xmpp = do
1841 hPutStrLn stderr $ "Starting peer listen" 1842 hPutStrLn stderr $ "Starting peer listen"
1842 control sv (Listen peerport peer_params) 1843 control sv (Listen peerport peer_params)
1843 hPutStrLn stderr $ "Starting client listen" 1844 hPutStrLn stderr $ "Starting client listen"
1844 control sv (Listen clientport client_params) 1845 control sv (Listen (xmppClientPort xmpp) client_params)
1845 return XMPPServer { _xmpp_sv = sv, _xmpp_peer_params = peer_params } 1846 return XMPPServer { _xmpp_sv = sv, _xmpp_peer_params = peer_params }
1846 1847
1847#if MIN_VERSION_stm(2,4,0) 1848#if MIN_VERSION_stm(2,4,0)
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index a921fea6..fcb02ace 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -393,7 +393,7 @@ data Session = Session
393 , toxkeys :: TVar Tox.AnnouncedKeys 393 , toxkeys :: TVar Tox.AnnouncedKeys
394 , userkeys :: TVar [(SecretKey,PublicKey)] 394 , userkeys :: TVar [(SecretKey,PublicKey)]
395 , roster :: Tox.ContactInfo 395 , roster :: Tox.ContactInfo
396 , connectionManager :: ConnectionManager 396 , connectionManager :: Maybe ConnectionManager
397 , onionRouter :: OnionRouter 397 , onionRouter :: OnionRouter
398 , announcer :: Announcer 398 , announcer :: Announcer
399 , signalQuit :: IO () 399 , signalQuit :: IO ()
@@ -935,8 +935,9 @@ clientSession s@Session{..} sock cnum h = do
935 where Just (_,(cnt,_)) = MM.lookup' k (Tox.keyAssoc keydb) 935 where Just (_,(cnt,_)) = MM.lookup' k (Tox.keyAssoc keydb)
936 hPutClient h $ showColumns entries 936 hPutClient h $ showColumns entries
937 937
938 ("c", s) | "" <- strp s -> cmd0 $ join $ atomically $ do 938 ("c", s) | Just (ConnectionManager mgr) <- connectionManager
939 ConnectionManager mgr <- return connectionManager 939 , "" <- strp s
940 -> cmd0 $ join $ atomically $ do
940 cmap <- connections mgr 941 cmap <- connections mgr
941 cs <- Map.toList <$> mapM connStatus cmap 942 cs <- Map.toList <$> mapM connStatus cmap
942 let mkrow = Connection.showKey mgr *** Connection.showStatus mgr 943 let mkrow = Connection.showKey mgr *** Connection.showStatus mgr
@@ -975,6 +976,7 @@ readExternals nodeAddr vars = do
975data Options = Options 976data Options = Options
976 { portbt :: String 977 { portbt :: String
977 , porttox :: String 978 , porttox :: String
979 , portxmpp :: String
978 , ip6bt :: Bool 980 , ip6bt :: Bool
979 , ip6tox :: Bool 981 , ip6tox :: Bool
980 , dhtkey :: Maybe SecretKey 982 , dhtkey :: Maybe SecretKey
@@ -993,6 +995,7 @@ sensibleDefaults :: Options
993sensibleDefaults = Options 995sensibleDefaults = Options
994 { portbt = "6881" 996 { portbt = "6881"
995 , porttox = "33445" 997 , porttox = "33445"
998 , portxmpp = "5222"
996 , ip6bt = True 999 , ip6bt = True
997 , ip6tox = True 1000 , ip6tox = True
998 , dhtkey = Nothing 1001 , dhtkey = Nothing
@@ -1009,8 +1012,9 @@ parseArgs ("-4":args) opts = parseArgs args opts
1009 { ip6bt = False 1012 { ip6bt = False
1010 , ip6tox = False } 1013 , ip6tox = False }
1011parseArgs (arg:args) opts = parseArgs args opts 1014parseArgs (arg:args) opts = parseArgs args opts
1012 { portbt = fromMaybe (portbt opts) $ Prelude.lookup "bt" ports 1015 { portbt = fromMaybe (portbt opts) $ Prelude.lookup "bt" ports
1013 , porttox = fromMaybe (porttox opts) $ Prelude.lookup "tox" ports } 1016 , porttox = fromMaybe (porttox opts) $ Prelude.lookup "tox" ports
1017 , portxmpp = fromMaybe (portxmpp opts) $ Prelude.lookup "xmpp" ports }
1014 where 1018 where
1015 ports = map ( (dropWhile (==',') *** dropWhile (=='=')) 1019 ports = map ( (dropWhile (==',') *** dropWhile (=='='))
1016 . break (=='=') ) 1020 . break (=='=') )
@@ -1414,18 +1418,24 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1414 1418
1415 _ <- UPNP.requestPorts "dht-client" $ map (Datagram,) $ baddrs ++ taddrs 1419 _ <- UPNP.requestPorts "dht-client" $ map (Datagram,) $ baddrs ++ taddrs
1416 1420
1417 -- XMPP initialization 1421 (msv,mconns,mstate) <- case portxmpp opts of
1418 cw <- newConsoleWriter 1422 "" -> return (Nothing,Nothing,Nothing)
1419 serverVar <- atomically $ newEmptyTMVar 1423 p -> do
1420 state <- newPresenceState cw (toxman <$> mbtox) serverVar 1424 mxmppPort <- sockAddrPort <$> getBindAddress p True{-IPv6 supported-}
1421 1425
1422 sv <- resT $ xmppServer (presenceHooks state (verbosity opts)) 1426 -- XMPP initialization
1423 -- We now have a server object but it's not ready to use until 1427 cw <- newConsoleWriter
1424 -- we put it into the 'server' field of our /state/ record. 1428 serverVar <- atomically $ newEmptyTMVar
1425 conns <- xmppConnections sv 1429 state <- newPresenceState cw (toxman <$> mbtox) serverVar
1426 atomically $ do 1430
1427 putTMVar serverVar (sv,conns) -- Okay, now it's ready. :) 1431 sv <- resT $ xmppServer (presenceHooks state (verbosity opts) Nothing)
1428 -- FIXME: This is error prone. 1432 -- We now have a server object but it's not ready to use until
1433 -- we put it into the 'server' field of our /state/ record.
1434 conns <- xmppConnections sv
1435 atomically $ do
1436 putTMVar serverVar (sv,conns) -- Okay, now it's ready. :)
1437 -- FIXME: This is error prone.
1438 return (Just sv, Just conns, Just state)
1429 1439
1430 forM_ (take 1 taddrs) $ \addrTox -> do 1440 forM_ (take 1 taddrs) $ \addrTox -> do
1431 atomically $ Tox.addNewSessionHook netCryptoSessionsState $ \mbNoSpam netcrypto -> do 1441 atomically $ Tox.addNewSessionHook netCryptoSessionsState $ \mbNoSpam netcrypto -> do
@@ -1437,7 +1447,8 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1437 onEOF = return () -- TODO: Update toxContactInfo, not connected. 1447 onEOF = return () -- TODO: Update toxContactInfo, not connected.
1438 xmppSrc = ioToSource receiveCrypto onEOF 1448 xmppSrc = ioToSource receiveCrypto onEOF
1439 xmppSink = newXmmpSink netcrypto 1449 xmppSink = newXmmpSink netcrypto
1440 announceToxJabberPeer (xmppEventChannel sv) addrTox (Tox.ncSockAddr netcrypto) pingflag xmppSrc xmppSink 1450 forM_ msv $ \sv -> do
1451 announceToxJabberPeer (xmppEventChannel sv) addrTox (Tox.ncSockAddr netcrypto) pingflag xmppSrc xmppSink
1441 -- TODO: Update toxContactInfo, connected. 1452 -- TODO: Update toxContactInfo, connected.
1442#endif 1453#endif
1443 let handleIncoming typ session cd | any ($ typ) [Tox.isKillPacket, Tox.isOFFLINE] = atomically $ do 1454 let handleIncoming typ session cd | any ($ typ) [Tox.isKillPacket, Tox.isOFFLINE] = atomically $ do
@@ -1475,7 +1486,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1475 , toxkeys = keysdb 1486 , toxkeys = keysdb
1476 , userkeys = toxids 1487 , userkeys = toxids
1477 , roster = rstr 1488 , roster = rstr
1478 , connectionManager = ConnectionManager conns 1489 , connectionManager = ConnectionManager <$> mconns
1479 , onionRouter = orouter 1490 , onionRouter = orouter
1480 , externalAddresses = liftM2 (++) btips toxips 1491 , externalAddresses = liftM2 (++) btips toxips
1481 , announcer = announcer 1492 , announcer = announcer
@@ -1508,16 +1519,17 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1508 1519
1509 forkIO $ do 1520 forkIO $ do
1510 myThreadId >>= flip labelThread "XMPP.stanzas" 1521 myThreadId >>= flip labelThread "XMPP.stanzas"
1511 let console = cwPresenceChan <$> consoleWriter state 1522 let console = cwPresenceChan <$> (mstate >>= consoleWriter)
1512 fix $ \loop -> do 1523 fix $ \loop -> do
1513 what <- atomically 1524 what <- atomically
1514 $ orElse (do (client,stanza) <- maybe retry takeTMVar console 1525 $ orElse (do (client,stanza) <- maybe retry takeTMVar console
1515 return $ do informClientPresence0 state Nothing client stanza 1526 return $ forM_ mstate $ \state -> do
1527 informClientPresence0 state Nothing client stanza
1516 loop) 1528 loop)
1517 (checkQuit >> return (return ())) 1529 (checkQuit >> return (return ()))
1518 what 1530 what
1519 1531
1520 hPutStrLn stderr "Started XMPP server." 1532 forM msv $ \_ -> hPutStrLn stderr "Started XMPP server."
1521 1533
1522 -- Wait for DHT and XMPP threads to finish. 1534 -- Wait for DHT and XMPP threads to finish.
1523 -- Use ResourceT to clean-up XMPP server. 1535 -- Use ResourceT to clean-up XMPP server.
diff --git a/todo.txt b/todo.txt
index 75c046e0..70af1142 100644
--- a/todo.txt
+++ b/todo.txt
@@ -1,3 +1,7 @@
1xmpp: handle tox-friends in roster.
2
3xmpp: load tox user key from ~/.presence/<pubkey>.tox/secret
4
1tox: Add fallback trials to cookie response in case response is from another address than request. 5tox: Add fallback trials to cookie response in case response is from another address than request.
2 6
3ui: Online help. 7ui: Online help.