diff options
author | joe <joe@jerkface.net> | 2014-02-17 11:40:10 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-02-17 11:40:10 -0500 |
commit | 38f7f68475502bc8b4ce8c6154865d52845b0c30 (patch) | |
tree | 8ef9ca5cac16af9796dc2727fb1613294f43f3d3 /Presence | |
parent | 24f0f7a50653223ea72c846a56817760a0bd63b9 (diff) |
cloneTChan unavailable in wheezy :(
added reverse-lookups for peer names
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/XMPPServer.hs | 44 |
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 #-} |
2 | module XMPPServer | 3 | module 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 |
16 | import Debug.Trace | 18 | import Debug.Trace |
@@ -39,7 +41,7 @@ import Data.Conduit.Blaze (builderToByteStringFlush) | |||
39 | import qualified Text.XML.Stream.Render as XML | 41 | import qualified Text.XML.Stream.Render as XML |
40 | import qualified Text.XML.Stream.Parse as XML | 42 | import qualified Text.XML.Stream.Parse as XML |
41 | import Data.XML.Types as XML | 43 | import Data.XML.Types as XML |
42 | import Data.Maybe (catMaybes,fromJust,isJust,isNothing) | 44 | import Data.Maybe (catMaybes,fromJust,isJust,isNothing,listToMaybe) |
43 | import Data.Monoid ( (<>) ) | 45 | import Data.Monoid ( (<>) ) |
44 | import Data.Text (Text) | 46 | import Data.Text (Text) |
45 | import qualified Data.Text as Text (pack,unpack) | 47 | import qualified Data.Text as Text (pack,unpack) |
@@ -49,7 +51,9 @@ import qualified Data.Map as Map | |||
49 | import Data.Set (Set, (\\) ) | 51 | import Data.Set (Set, (\\) ) |
50 | import qualified Data.Set as Set | 52 | import qualified Data.Set as Set |
51 | import qualified System.Random | 53 | import qualified System.Random |
54 | import qualified Network.BSD as BSD | ||
52 | 55 | ||
56 | import GetHostByAddr (getHostByAddr) | ||
53 | import qualified Control.Concurrent.STM.UpdateStream as Slotted | 57 | import qualified Control.Concurrent.STM.UpdateStream as Slotted |
54 | import ControlMaybe | 58 | import ControlMaybe |
55 | import Nesting | 59 | import 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 | ||
172 | peerKeyToText :: ConnectionKey -> Text | 176 | peerKeyToText :: ConnectionKey -> Text |
173 | peerKeyToText (PeerKey { callBackAddress=addr }) = addrToText addr | 177 | peerKeyToText (PeerKey { callBackAddress=addr }) = addrToText addr |
174 | peerKeyToText (ClientKey { localAddress=addr }) = "ErrorClIeNt" | 178 | peerKeyToText (ClientKey { localAddress=addr }) = "ErrorClIeNt0" |
179 | |||
180 | peerKeyToResolvedName :: ConnectionKey -> IO Text | ||
181 | peerKeyToResolvedName k@(ClientKey { localAddress=addr }) = return "ErrorClIeNt1" | ||
182 | peerKeyToResolvedName 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 | ||
176 | wlog s = putStrLn s | 189 | wlog 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. | ||
973 | cloneTChan :: TChan a -> STM (TChan a) | ||
974 | cloneTChan 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 | ||