summaryrefslogtreecommitdiff
path: root/Presence/XMPPServer.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-02-17 11:40:10 -0500
committerjoe <joe@jerkface.net>2014-02-17 11:40:10 -0500
commit38f7f68475502bc8b4ce8c6154865d52845b0c30 (patch)
tree8ef9ca5cac16af9796dc2727fb1613294f43f3d3 /Presence/XMPPServer.hs
parent24f0f7a50653223ea72c846a56817760a0bd63b9 (diff)
cloneTChan unavailable in wheezy :(
added reverse-lookups for peer names
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r--Presence/XMPPServer.hs44
1 files changed, 40 insertions, 4 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index aab689ad..c0bede90 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE OverloadedStrings #-} 2{-# LANGUAGE OverloadedStrings #-}
2module XMPPServer 3module XMPPServer
3 ( xmppServer 4 ( xmppServer
@@ -11,6 +12,7 @@ module XMPPServer
11 , cloneStanza 12 , cloneStanza
12 , LangSpecificMessage(..) 13 , LangSpecificMessage(..)
13 , peerKeyToText 14 , peerKeyToText
15 , peerKeyToResolvedName
14 , addrToText 16 , addrToText
15 ) where 17 ) where
16import Debug.Trace 18import Debug.Trace
@@ -39,7 +41,7 @@ import Data.Conduit.Blaze (builderToByteStringFlush)
39import qualified Text.XML.Stream.Render as XML 41import qualified Text.XML.Stream.Render as XML
40import qualified Text.XML.Stream.Parse as XML 42import qualified Text.XML.Stream.Parse as XML
41import Data.XML.Types as XML 43import Data.XML.Types as XML
42import Data.Maybe (catMaybes,fromJust,isJust,isNothing) 44import Data.Maybe (catMaybes,fromJust,isJust,isNothing,listToMaybe)
43import Data.Monoid ( (<>) ) 45import Data.Monoid ( (<>) )
44import Data.Text (Text) 46import Data.Text (Text)
45import qualified Data.Text as Text (pack,unpack) 47import qualified Data.Text as Text (pack,unpack)
@@ -49,7 +51,9 @@ import qualified Data.Map as Map
49import Data.Set (Set, (\\) ) 51import Data.Set (Set, (\\) )
50import qualified Data.Set as Set 52import qualified Data.Set as Set
51import qualified System.Random 53import qualified System.Random
54import qualified Network.BSD as BSD
52 55
56import GetHostByAddr (getHostByAddr)
53import qualified Control.Concurrent.STM.UpdateStream as Slotted 57import qualified Control.Concurrent.STM.UpdateStream as Slotted
54import ControlMaybe 58import ControlMaybe
55import Nesting 59import Nesting
@@ -150,7 +154,7 @@ data XMPPServerParameters =
150 , xmppRosterOthers :: ConnectionKey -> IO [Text] 154 , xmppRosterOthers :: ConnectionKey -> IO [Text]
151 , xmppSubscribeToRoster :: ConnectionKey -> IO () 155 , xmppSubscribeToRoster :: ConnectionKey -> IO ()
152 -- , xmppLookupClientJID :: ConnectionKey -> IO Text 156 -- , xmppLookupClientJID :: ConnectionKey -> IO Text
153 , xmppLookupPeerName :: ConnectionKey -> IO Text 157 , xmppTellClientNameOfPeer :: ConnectionKey -> IO Text
154 , xmppDeliverMessage :: (IO ()) -> Stanza -> IO () 158 , xmppDeliverMessage :: (IO ()) -> Stanza -> IO ()
155 , xmppInformClientPresence :: ConnectionKey -> Stanza -> IO () 159 , xmppInformClientPresence :: ConnectionKey -> Stanza -> IO ()
156 } 160 }
@@ -171,7 +175,16 @@ addrToText (addr@(SockAddrInet6 _ _ _ _)) = Text.pack $ stripColon (show addr)
171 175
172peerKeyToText :: ConnectionKey -> Text 176peerKeyToText :: ConnectionKey -> Text
173peerKeyToText (PeerKey { callBackAddress=addr }) = addrToText addr 177peerKeyToText (PeerKey { callBackAddress=addr }) = addrToText addr
174peerKeyToText (ClientKey { localAddress=addr }) = "ErrorClIeNt" 178peerKeyToText (ClientKey { localAddress=addr }) = "ErrorClIeNt0"
179
180peerKeyToResolvedName :: ConnectionKey -> IO Text
181peerKeyToResolvedName k@(ClientKey { localAddress=addr }) = return "ErrorClIeNt1"
182peerKeyToResolvedName k@(PeerKey { callBackAddress=addr }) = do
183 ent <- getHostByAddr addr -- AF_UNSPEC addr
184 let names = BSD.hostName ent : BSD.hostAliases ent
185 mname = listToMaybe names
186 return $ maybe (peerKeyToText k) Text.pack mname
187
175 188
176wlog s = putStrLn s 189wlog s = putStrLn s
177 where _ = s :: String 190 where _ = s :: String
@@ -796,7 +809,7 @@ sendRoster query xmpp replyto = do
796 flip (maybe $ return ()) k $ \k -> do 809 flip (maybe $ return ()) k $ \k -> do
797 jid <- case k of 810 jid <- case k of
798 ClientKey {} -> xmppTellClientHisName xmpp k -- LookupClientJID xmpp k 811 ClientKey {} -> xmppTellClientHisName xmpp k -- LookupClientJID xmpp k
799 PeerKey {} -> xmppLookupPeerName xmpp k 812 PeerKey {} -> xmppTellClientNameOfPeer xmpp k
800 let getlist f = do 813 let getlist f = do
801 bs <- f xmpp k 814 bs <- f xmpp k
802 -- js <- mapM parseHostNameJID bs 815 -- js <- mapM parseHostNameJID bs
@@ -936,6 +949,7 @@ xmppServer xmpp = do
936 gen <- System.Random.getStdGen 949 gen <- System.Random.getStdGen
937 let (r,gen') = System.Random.next gen 950 let (r,gen') = System.Random.next gen
938 return $ r `mod` 2000 -- maximum 2 seconds of fuzz 951 return $ r `mod` 2000 -- maximum 2 seconds of fuzz
952 liftIO . wlog $ "pingfuzz = " ++ show pingfuzz
939 let peer_params = (connectionDefaults peerKey) 953 let peer_params = (connectionDefaults peerKey)
940 { pingInterval = 15000 + pingfuzz 954 { pingInterval = 15000 + pingfuzz
941 , timeout = 2000 955 , timeout = 2000
@@ -949,3 +963,25 @@ xmppServer xmpp = do
949 control sv (Listen peerport peer_params) 963 control sv (Listen peerport peer_params)
950 control sv (Listen clientport client_params) 964 control sv (Listen clientport client_params)
951 return XMPPServer { _xmpp_sv = sv, _xmpp_peer_params = peer_params } 965 return XMPPServer { _xmpp_sv = sv, _xmpp_peer_params = peer_params }
966
967#if MIN_VERSION_stm(2,4,0)
968#else
969-- |Clone a 'TChan': similar to dupTChan, but the cloned channel starts with the
970-- same content available as the original channel.
971--
972-- Terrible inefficient implementation provided to build against older libraries.
973cloneTChan :: TChan a -> STM (TChan a)
974cloneTChan chan = do
975 contents <- chanContents' chan
976 chan2 <- dupTChan chan
977 mapM_ (writeTChan chan) contents
978 mapM_ (writeTChan chan2) contents
979 return chan2
980 where
981 chanContents' chan = do
982 b <- isEmptyTChan chan
983 if b then return [] else do
984 x <- readTChan chan
985 xs <- chanContents' chan
986 return (x:xs)
987#endif