diff options
author | joe <joe@jerkface.net> | 2013-07-02 03:30:26 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-07-02 03:30:26 -0400 |
commit | d6d23835b19f1d804be5c5a181fd38586bb6b136 (patch) | |
tree | c7f4b32fd00d1fd905ffdd94be0f6c179cb6176d /Presence/main.hs | |
parent | 814f6cceb3486e87115aeb40f3766d60a0768f18 (diff) |
Reply to presence probes.
Diffstat (limited to 'Presence/main.hs')
-rw-r--r-- | Presence/main.hs | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/Presence/main.hs b/Presence/main.hs index e02b4348..7981f00b 100644 --- a/Presence/main.hs +++ b/Presence/main.hs | |||
@@ -78,7 +78,7 @@ data ClientSession = ClientSession { | |||
78 | presence_state :: PresenceState | 78 | presence_state :: PresenceState |
79 | } | 79 | } |
80 | 80 | ||
81 | instance XMPPSession ClientSession where | 81 | instance JabberClientSession ClientSession where |
82 | data XMPPClass ClientSession = ClientSessions PresenceState | 82 | data XMPPClass ClientSession = ClientSessions PresenceState |
83 | 83 | ||
84 | newSession (ClientSessions state) sock = do | 84 | newSession (ClientSessions state) sock = do |
@@ -119,29 +119,23 @@ instance XMPPSession ClientSession where | |||
119 | let tvar = subscriberMap (presence_state session) | 119 | let tvar = subscriberMap (presence_state session) |
120 | atomically $ subscribeToMap tvar jid | 120 | atomically $ subscribeToMap tvar jid |
121 | 121 | ||
122 | announcePresence _ _ = error "announcePresence on client session?" | ||
123 | |||
124 | 122 | ||
125 | data PeerSession = PeerSession { | 123 | data PeerSession = PeerSession { |
126 | announced :: TVar (Set JID), | 124 | announced :: TVar (Set JID), |
127 | peer_name :: Peer, | 125 | peer_name :: Peer, |
128 | peer_global :: PresenceState | 126 | peer_global :: PresenceState |
129 | } | 127 | } |
130 | instance XMPPSession PeerSession where | 128 | instance JabberPeerSession PeerSession where |
131 | data XMPPClass PeerSession = PeerSessions PresenceState | 129 | data XMPPPeerClass PeerSession = PeerSessions PresenceState |
132 | |||
133 | setResource _ _ = error "setResource on peer session?" | ||
134 | getJID _ = error "getJID on peer session?" | ||
135 | subscribe _ _ = error "subscribe on peer session?" | ||
136 | 130 | ||
137 | newSession (PeerSessions state) sock = do | 131 | newPeerSession (PeerSessions state) sock = do |
138 | me <- fmap (RemotePeer . withoutPort) (getPeerName sock) | 132 | me <- fmap (RemotePeer . withoutPort) (getPeerName sock) |
139 | L.putStrLn $ "PEER SESSION: open "<++>showPeer me | 133 | L.putStrLn $ "PEER SESSION: open "<++>showPeer me |
140 | let remotes = remoteUsers state | 134 | let remotes = remoteUsers state |
141 | jids <- atomically $ getRefFromMap remotes me (newTVar Set.empty) return | 135 | jids <- atomically $ getRefFromMap remotes me (newTVar Set.empty) return |
142 | return $ PeerSession jids me state | 136 | return $ PeerSession jids me state |
143 | 137 | ||
144 | closeSession session = do | 138 | closePeerSession session = do |
145 | L.putStrLn $ "PEER SESSION: close "<++>showPeer (peer_name session) | 139 | L.putStrLn $ "PEER SESSION: close "<++>showPeer (peer_name session) |
146 | let offline jid = Presence jid Offline | 140 | let offline jid = Presence jid Offline |
147 | unrefFromMap (remoteUsers . peer_global $ session) (peer_name session) | 141 | unrefFromMap (remoteUsers . peer_global $ session) (peer_name session) |
@@ -149,6 +143,22 @@ instance XMPPSession PeerSession where | |||
149 | js <- fmap Set.toList (readTVarIO . announced $ session) | 143 | js <- fmap Set.toList (readTVarIO . announced $ session) |
150 | forM_ js $ announcePresence session . offline | 144 | forM_ js $ announcePresence session . offline |
151 | 145 | ||
146 | peerSessionFactory session = PeerSessions (peer_global session) | ||
147 | |||
148 | peerAddress session = peer_name session | ||
149 | |||
150 | userStatus session user = do | ||
151 | let state = peer_global session | ||
152 | (tty,users) <- atomically $ do | ||
153 | tty <- readTVar $ currentTTY state | ||
154 | users <- readTVar $ activeUsers state | ||
155 | return (tty,users) | ||
156 | let jids = Set.filter (\jid->name jid==Just user) users | ||
157 | ps = map (\jid -> Presence jid (matchResource tty jid)) . Set.toList $ jids | ||
158 | if null ps | ||
159 | then return [Presence (JID { name=Just user, peer=LocalHost, resource=Nothing }) Offline] | ||
160 | else return ps | ||
161 | |||
152 | announcePresence session (Presence jid status) = do | 162 | announcePresence session (Presence jid status) = do |
153 | (greedy,subs) <- atomically $ do | 163 | (greedy,subs) <- atomically $ do |
154 | subs <- readTVar $ subscriberMap (peer_global session) | 164 | subs <- readTVar $ subscriberMap (peer_global session) |
@@ -162,6 +172,10 @@ instance XMPPSession PeerSession where | |||
162 | Offline -> Set.delete jid jids | 172 | Offline -> Set.delete jid jids |
163 | _ -> Set.insert jid jids | 173 | _ -> Set.insert jid jids |
164 | 174 | ||
175 | getBuddies _ user = ConfigFiles.getBuddies user | ||
176 | getSubscribers _ user = ConfigFiles.getSubscribers user | ||
177 | |||
178 | |||
165 | subscribeToChan tmvar = | 179 | subscribeToChan tmvar = |
166 | (do (cnt,chan) <- takeTMVar tmvar | 180 | (do (cnt,chan) <- takeTMVar tmvar |
167 | putTMVar tmvar (cnt+1,chan) | 181 | putTMVar tmvar (cnt+1,chan) |
@@ -284,12 +298,6 @@ on_chvt state vtnum = do | |||
284 | return (us,fmap snd subs,fmap snd locals_greedy) | 298 | return (us,fmap snd subs,fmap snd locals_greedy) |
285 | update_presence locals_greedy subs users $ matchResource tty | 299 | update_presence locals_greedy subs users $ matchResource tty |
286 | 300 | ||
287 | data UnixConfig = UnixConfig | ||
288 | |||
289 | instance XMPPConfig UnixConfig where | ||
290 | getBuddies _ user = ConfigFiles.getBuddies user | ||
291 | getSubscribers _ user = ConfigFiles.getSubscribers user | ||
292 | |||
293 | start :: Network.Socket.Family -> IO () | 301 | start :: Network.Socket.Family -> IO () |
294 | start ip4or6 = do | 302 | start ip4or6 = do |
295 | let host = LocalHost | 303 | let host = LocalHost |
@@ -298,7 +306,7 @@ start ip4or6 = do | |||
298 | dologin :: t -> IO () | 306 | dologin :: t -> IO () |
299 | 307 | ||
300 | chan <- atomically $ subscribeToChan (localSubscriber global_state) | 308 | chan <- atomically $ subscribeToChan (localSubscriber global_state) |
301 | remotes <- forkIO $ seekRemotePeers UnixConfig chan (outGoingConnections global_state) | 309 | remotes <- forkIO $ seekRemotePeers (PeerSessions global_state) chan (outGoingConnections global_state) |
302 | 310 | ||
303 | installHandler sigUSR1 (Catch (dologin (userError "signaled"))) Nothing | 311 | installHandler sigUSR1 (Catch (dologin (userError "signaled"))) Nothing |
304 | -- installHandler sigTERM (CatchOnce (dologin (userError "term signaled"))) Nothing | 312 | -- installHandler sigTERM (CatchOnce (dologin (userError "term signaled"))) Nothing |