summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Avahi.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/Avahi.hs')
-rw-r--r--src/Network/Tox/Avahi.hs39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/Network/Tox/Avahi.hs b/src/Network/Tox/Avahi.hs
index 7fe87db2..559e5fd7 100644
--- a/src/Network/Tox/Avahi.hs
+++ b/src/Network/Tox/Avahi.hs
@@ -1,16 +1,18 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# LANGUAGE RecordWildCards #-} 2{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
3module Network.Tox.Avahi 4module Network.Tox.Avahi
4 ( module Network.Tox.Avahi 5 ( module Network.Tox.Avahi
5 , NodeInfo(..) 6 , NodeInfo(..)
6 , NodeId(..) 7 , NodeId
7 ) where 8 ) where
8 9
9import Data.Foldable 10import Control.Applicative
10import Network.Address 11import Data.Foldable
11import Network.Avahi 12import Network.Address
12import Network.Tox.NodeId 13import Network.Avahi
13import Text.Read 14import Network.Tox.NodeId
15import Text.Read
14 16
15toxServiceName :: String 17toxServiceName :: String
16toxServiceName = "_tox_dht._udp" 18toxServiceName = "_tox_dht._udp"
@@ -21,8 +23,8 @@ toxServiceDomain = "local"
21(<.>) :: String -> String -> String 23(<.>) :: String -> String -> String
22a <.> b = a ++ "." ++ b 24a <.> b = a ++ "." ++ b
23 25
24toxService :: String -> PortNumber -> NodeId -> Service 26toxService :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> Service
25toxService hostname (fromIntegral -> port) (show -> extra) = 27toxService hostname (fromIntegral -> port) dhtkey toxid =
26 Service { 28 Service {
27 serviceProtocol = PROTO_UNSPEC, 29 serviceProtocol = PROTO_UNSPEC,
28 serviceName = "Tox DHT @ " ++ hostname, 30 serviceName = "Tox DHT @ " ++ hostname,
@@ -31,16 +33,17 @@ toxService hostname (fromIntegral -> port) (show -> extra) =
31 serviceHost = if null hostname then "" else hostname <.> toxServiceDomain, 33 serviceHost = if null hostname then "" else hostname <.> toxServiceDomain,
32 serviceAddress = Nothing, 34 serviceAddress = Nothing,
33 servicePort = port, 35 servicePort = port,
34 serviceText = extra 36 serviceText = maybe (show dhtkey) (show . ((,) dhtkey)) toxid
35 } 37 }
36 38
37announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> IO () 39announceToxServiceWithHostname :: String -> PortNumber -> NodeId -> (Maybe NodeId) -> IO ()
38announceToxServiceWithHostname = ((.).(.).(.)) announce toxService 40announceToxServiceWithHostname = (boobs.boobs) announce toxService
41 where boobs = ((.).(.))
39 42
40announceToxService :: PortNumber -> NodeId -> IO () 43announceToxService :: PortNumber -> NodeId -> (Maybe NodeId) -> IO ()
41announceToxService = announceToxServiceWithHostname "" 44announceToxService = announceToxServiceWithHostname ""
42 45
43queryToxService :: (NodeInfo -> IO ()) -> IO () 46queryToxService :: (NodeInfo -> Maybe NodeId -> IO ()) -> IO ()
44queryToxService cb = 47queryToxService cb =
45 browse $ 48 browse $
46 BrowseQuery 49 BrowseQuery
@@ -50,8 +53,10 @@ queryToxService cb =
50 , lookupCallback = runCallback 53 , lookupCallback = runCallback
51 } 54 }
52 where 55 where
53 runCallback x@Service {..} = do 56 runCallback Service {..} = do
54 let nid = readMaybe serviceText 57 let both :: Maybe (NodeId, NodeId)
58 both = readMaybe serviceText
59 nid = (fst <$> both) <|> readMaybe serviceText
55 addr = readMaybe =<< serviceAddress 60 addr = readMaybe =<< serviceAddress
56 p = fromIntegral servicePort 61 p = fromIntegral servicePort
57 forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) 62 forM_ nid $ \n -> forM_ addr $ \a -> cb (NodeInfo n a p) (snd <$> both)