summaryrefslogtreecommitdiff
path: root/Presence/main.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-07-11 21:25:40 -0400
committerjoe <joe@jerkface.net>2013-07-11 21:25:40 -0400
commit87b0599200ae29ac766925eea8f1d639b46cb114 (patch)
treeb64ee753e53a3c1a6cf7ab31a85c15e966e744ad /Presence/main.hs
parent563125c8dc8c73b6382708637a12c72110cd0662 (diff)
Handle rejection case.
Diffstat (limited to 'Presence/main.hs')
-rw-r--r--Presence/main.hs83
1 files changed, 56 insertions, 27 deletions
diff --git a/Presence/main.hs b/Presence/main.hs
index 702b98d5..781b87fa 100644
--- a/Presence/main.hs
+++ b/Presence/main.hs
@@ -87,6 +87,14 @@ data PresenceState = PresenceState
87 } 87 }
88 88
89 89
90rosterPush msg state = do
91 let rchan = rosterChannel state
92 atomically $ do
93 isempty <- isEmptyTMVar rchan
94 when (not isempty) $ do
95 (_,ch) <- readTMVar rchan
96 writeTChan ch msg
97
90getJabberUserForId muid = 98getJabberUserForId muid =
91 maybe (return "nobody") 99 maybe (return "nobody")
92 (\(uid,_) -> 100 (\(uid,_) ->
@@ -157,7 +165,7 @@ instance JabberClientSession ClientSession where
157 165
158 rsc <- readIORef (unix_resource s) 166 rsc <- readIORef (unix_resource s)
159 -- let jid = user <++> "@" <++> host <++?> "/" <++$> rsc 167 -- let jid = user <++> "@" <++> host <++?> "/" <++$> rsc
160 L.putStrLn $ "CLIENT SESSION: jid " <++> L.show (JID (Just user) host rsc) 168 -- L.putStrLn $ "CLIENT SESSION: jid " <++> L.show (JID (Just user) host rsc)
161 return (JID (Just user) host rsc) 169 return (JID (Just user) host rsc)
162 170
163 closeSession session = do 171 closeSession session = do
@@ -228,12 +236,9 @@ instance JabberClientSession ClientSession where
228 me <- getJID s 236 me <- getJID s
229 withJust (name me) $ \user -> do 237 withJust (name me) $ \user -> do
230 addRawJid ConfigFiles.modifySolicited user jid_str 238 addRawJid ConfigFiles.modifySolicited user jid_str
231 let rchan = rosterChannel . presence_state $ s 239
232 atomically $ do 240 rosterPush (RequestedSubscription user jid_str) (presence_state s)
233 isempty <- isEmptyTMVar rchan 241
234 when (not isempty) $ do
235 (_,ch) <- readTMVar rchan
236 writeTChan ch (RequestedSubscription user jid_str)
237 sendMessage (outGoingConnections . presence_state $ s) 242 sendMessage (outGoingConnections . presence_state $ s)
238 (Solicitation me jid) 243 (Solicitation me jid)
239 (peer jid) 244 (peer jid)
@@ -278,17 +283,14 @@ instance JabberClientSession ClientSession where
278 user <- readIORef (unix_uid s) >>= getJabberUserForId 283 user <- readIORef (unix_uid s) >>= getJabberUserForId
279 cjid <- parseHostNameJID contact 284 cjid <- parseHostNameJID contact
280 let rmjid modify user buddy = modify user (cmpJID buddy) Nothing 285 let rmjid modify user buddy = modify user (cmpJID buddy) Nothing
286
287 -- update config files
281 addRawJid ConfigFiles.modifySubscribers user contact -- add subscribers 288 addRawJid ConfigFiles.modifySubscribers user contact -- add subscribers
282 rmjid ConfigFiles.modifyPending user cjid 289 rmjid ConfigFiles.modifyPending user cjid
283 rmjid ConfigFiles.modifyOthers user cjid 290 rmjid ConfigFiles.modifyOthers user cjid
284 291
285 -- roster push 292 -- roster push
286 let rchan = rosterChannel . presence_state $ s 293 rosterPush (NewSubscriber user contact) (presence_state s)
287 atomically $ do
288 isempty <- isEmptyTMVar rchan
289 when (not isempty) $ do
290 (_,ch) <- readTMVar rchan
291 writeTChan ch (NewSubscriber user contact)
292 294
293 -- notify peer 295 -- notify peer
294 sendMessage (outGoingConnections . presence_state $ s) 296 sendMessage (outGoingConnections . presence_state $ s)
@@ -300,6 +302,33 @@ instance JabberClientSession ClientSession where
300 forM_ presence $ \p -> sendMessage cons (OutBoundPresence p) (peer cjid) 302 forM_ presence $ \p -> sendMessage cons (OutBoundPresence p) (peer cjid)
301 return () 303 return ()
302 304
305 rejectSubscriber s contact = do
306 user <- readIORef (unix_uid s) >>= getJabberUserForId
307 cjid <- parseHostNameJID contact
308 let rmjid modify user buddy = modify user (cmpJID buddy) Nothing
309
310 -- update config files
311 was_pending <- rmjid ConfigFiles.modifyPending user cjid
312 was_subscribed <- rmjid ConfigFiles.modifySubscribers user cjid
313 addRawJid ConfigFiles.modifyOthers user contact
314
315 -- roster push
316 rosterPush (RejectSubscriber user contact) (presence_state s)
317
318 -- notify peer
319 when (was_pending || was_subscribed) $ do
320 let cons = outGoingConnections . presence_state $ s
321 isonline (Presence _ Offline) = False
322 isonline _ = True
323 presence <- fmap (filter isonline) $ getUserStatus (presence_state s) user
324 me <- getJID s
325 when (not (null presence)) $
326 sendMessage cons (OutBoundPresence . Presence me $ Offline) (peer cjid)
327 sendMessage (outGoingConnections . presence_state $ s)
328 (Rejection (JID (Just user) LocalHost Nothing)
329 cjid)
330 (peer cjid)
331 return ()
303 332
304tupleToJID (user,tty,pid) = jid user LocalHost tty 333tupleToJID (user,tty,pid) = jid user LocalHost tty
305 334
@@ -375,19 +404,24 @@ instance JabberPeerSession PeerSession where
375 solicited <- ConfigFiles.getSolicited user 404 solicited <- ConfigFiles.getSolicited user
376 let rmjid modify user buddy = modify user (cmpJID buddy) Nothing 405 let rmjid modify user buddy = modify user (cmpJID buddy) Nothing
377 was_sol <- rmjid ConfigFiles.modifySolicited user buddy 406 was_sol <- rmjid ConfigFiles.modifySolicited user buddy
378 putStrLn $ "was_sol = "<++>bshow was_sol
379 when was_sol $ do -- if buddy ∈ solicited: 407 when was_sol $ do -- if buddy ∈ solicited:
380 addJid ConfigFiles.modifyBuddies user buddy -- add buddies 408 addJid ConfigFiles.modifyBuddies user buddy -- add buddies
381 rmjid ConfigFiles.modifyOthers user buddy -- remove others 409 rmjid ConfigFiles.modifyOthers user buddy -- remove others
382 let rchan = rosterChannel . peer_global $ session
383 mbuddy <- asHostNameJID buddy 410 mbuddy <- asHostNameJID buddy
384 withJust mbuddy $ \buddy -> do 411 withJust mbuddy $ \buddy -> do
385 atomically $ do 412 rosterPush (NewBuddy user buddy) (peer_global session)
386 isempty <- isEmptyTMVar rchan 413
387 when (not isempty) $ do 414 processRejection session user buddy = do
388 (_,ch) <- readTMVar rchan 415 solicited <- ConfigFiles.getSolicited user
389 writeTChan ch (NewBuddy user buddy) 416 let rmjid modify user buddy = modify user (cmpJID buddy) Nothing
390 return () 417 was_sol <- rmjid ConfigFiles.modifySolicited user buddy
418 when was_sol $ do -- if buddy ∈ solicited:
419 rmjid ConfigFiles.modifyBuddies user buddy -- remove buddies
420 addJid ConfigFiles.modifyOthers user buddy -- add others
421 mbuddy <- asHostNameJID buddy
422 withJust mbuddy $ \buddy -> do
423 rosterPush (RemovedBuddy user buddy) (peer_global session)
424
391 processRequest session user buddy = do 425 processRequest session user buddy = do
392 let addjid modify user buddy = do 426 let addjid modify user buddy = do
393 hbuddy <- asHostNameJID buddy 427 hbuddy <- asHostNameJID buddy
@@ -403,14 +437,9 @@ instance JabberPeerSession PeerSession where
403 -- contact ∉ subscribers & contact ∈ pending --> SHOULD NOT 437 -- contact ∉ subscribers & contact ∈ pending --> SHOULD NOT
404 -- contact ∉ subscribers & contact ∉ pending --> MUST 438 -- contact ∉ subscribers & contact ∉ pending --> MUST
405 when (not was_pending) $ do 439 when (not was_pending) $ do
406 let rchan = rosterChannel . peer_global $ session
407 mbuddy <- asHostNameJID buddy 440 mbuddy <- asHostNameJID buddy
408 withJust mbuddy $ \buddy -> do 441 withJust mbuddy $ \buddy -> do
409 atomically $ do 442 rosterPush (PendingSubscriber user buddy) (peer_global session)
410 isempty <- isEmptyTMVar rchan
411 when (not isempty) $ do
412 (_,ch) <- readTMVar rchan
413 writeTChan ch (PendingSubscriber user buddy)
414 443
415 444
416data RefCountedChan = forall a. RefCountedChan (TMVar (RefCount,TChan a)) 445data RefCountedChan = forall a. RefCountedChan (TMVar (RefCount,TChan a))