summaryrefslogtreecommitdiff
path: root/Presence/XMPPServer.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-21 01:26:58 -0400
committerjoe <joe@jerkface.net>2018-06-21 02:56:33 -0400
commit8cdc2de72ebe8945ce4b9f7fe8890970c34135a1 (patch)
tree99d8cbbaa46f089716f101058d0442b28f762bd8 /Presence/XMPPServer.hs
parent458c7a99e07300cde99826f825c3d0d6a7eab298 (diff)
Avoid awkward "flip (maybe ...)" pattern.
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r--Presence/XMPPServer.hs60
1 files changed, 30 insertions, 30 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index 0efde53b..19e721b0 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -32,6 +32,7 @@ module XMPPServer
32 , Server 32 , Server
33 , flushPassThrough 33 , flushPassThrough
34 , greet' 34 , greet'
35 , (<&>)
35 ) where 36 ) where
36 37
37import ConnectionKey 38import ConnectionKey
@@ -504,7 +505,7 @@ sendReply donevar stype reply replychan = do
504 isInternal (InternalEnableHack {}) = True 505 isInternal (InternalEnableHack {}) = True
505 isInternal (InternalCacheId {}) = True 506 isInternal (InternalCacheId {}) = True
506 isInternal _ = False 507 isInternal _ = False
507 flip (maybe $ return ()) 508 forM_
508 (fmap (const ()) stanzaTag `mplus` guard (isInternal stype)) 509 (fmap (const ()) stanzaTag `mplus` guard (isInternal stype))
509 . const $ do 510 . const $ do
510 replyStanza <- liftIO . atomically $ do 511 replyStanza <- liftIO . atomically $ do
@@ -558,11 +559,11 @@ stanzaFromList stype reply = do
558grokStanzaIQGet :: Monad m => XML.Event -> NestingXML o m (Maybe StanzaType) 559grokStanzaIQGet :: Monad m => XML.Event -> NestingXML o m (Maybe StanzaType)
559grokStanzaIQGet stanza = do 560grokStanzaIQGet stanza = do
560 mtag <- nextElement 561 mtag <- nextElement
561 flip (maybe $ return Nothing) mtag $ \tag -> do 562 forM mtag $ \tag -> do
562 case tagName tag of 563 case tagName tag of
563 "{urn:xmpp:ping}ping" -> return $ Just Ping 564 "{urn:xmpp:ping}ping" -> return Ping
564 "{jabber:iq:roster}query" -> return $ Just RequestRoster 565 "{jabber:iq:roster}query" -> return RequestRoster
565 name -> return . Just $ UnrecognizedQuery name 566 name -> return $ UnrecognizedQuery name
566 567
567parseClientVersion :: NestingXML o IO (Maybe StanzaType) 568parseClientVersion :: NestingXML o IO (Maybe StanzaType)
568parseClientVersion = parseit Nothing Nothing 569parseClientVersion = parseit Nothing Nothing
@@ -574,7 +575,7 @@ parseClientVersion = parseit Nothing Nothing
574 parseit :: Maybe Text -> Maybe Text -> NestingXML o IO (Maybe StanzaType) 575 parseit :: Maybe Text -> Maybe Text -> NestingXML o IO (Maybe StanzaType)
575 parseit mname mver = do 576 parseit mname mver = do
576 mtag <- nextElement 577 mtag <- nextElement
577 flip (maybe $ reportit mname mver) mtag $ \tag -> do 578 fromMaybe (reportit mname mver) $ mtag <&> \tag -> do
578 case tagName tag of 579 case tagName tag of
579 "{jabber:iq:version}name" -> do 580 "{jabber:iq:version}name" -> do
580 x <- XML.content 581 x <- XML.content
@@ -588,7 +589,7 @@ parseClientVersion = parseit Nothing Nothing
588grokStanzaIQResult :: XML.Event -> NestingXML o IO (Maybe StanzaType) 589grokStanzaIQResult :: XML.Event -> NestingXML o IO (Maybe StanzaType)
589grokStanzaIQResult stanza = do 590grokStanzaIQResult stanza = do
590 mtag <- nextElement 591 mtag <- nextElement
591 flip (maybe $ return (Just Pong)) mtag $ \tag -> do 592 fromMaybe (return $ Just Pong) $ mtag <&> \tag -> do
592 case tagName tag of 593 case tagName tag of
593 "{jabber:iq:version}query" | nameNamespace (tagName stanza)==Just "jabber:client" 594 "{jabber:iq:version}query" | nameNamespace (tagName stanza)==Just "jabber:client"
594 -> parseClientVersion 595 -> parseClientVersion
@@ -597,19 +598,18 @@ grokStanzaIQResult stanza = do
597grokStanzaIQSet :: XML.Event -> NestingXML o IO (Maybe StanzaType) 598grokStanzaIQSet :: XML.Event -> NestingXML o IO (Maybe StanzaType)
598grokStanzaIQSet stanza = do 599grokStanzaIQSet stanza = do
599 mtag <- nextElement 600 mtag <- nextElement
600 flip (maybe $ return Nothing) mtag $ \tag -> do 601 case tagName <$> mtag of
601 case tagName tag of 602 Just "{urn:ietf:params:xml:ns:xmpp-bind}bind"
602 "{urn:ietf:params:xml:ns:xmpp-bind}bind" -> do 603 -> do mchild <- nextElement
603 mchild <- nextElement 604 case tagName <$> mchild of
604 case fmap tagName mchild of 605 Just "{urn:ietf:params:xml:ns:xmpp-bind}resource"
605 Just "{urn:ietf:params:xml:ns:xmpp-bind}resource" -> do 606 -> do rsc <- XML.content -- TODO: MonadThrow???
606 rsc <- XML.content -- TODO: MonadThrow??? 607 return . Just $ RequestResource Nothing (Just rsc)
607 return . Just $ RequestResource Nothing (Just rsc) 608 Just _ -> return Nothing
608 Just _ -> return Nothing 609 Nothing -> return . Just $ RequestResource Nothing Nothing
609 Nothing -> return . Just $ RequestResource Nothing Nothing 610 Just "{urn:ietf:params:xml:ns:xmpp-session}session"
610 "{urn:ietf:params:xml:ns:xmpp-session}session" -> do 611 -> return $ Just SessionRequest
611 return $ Just SessionRequest 612 _ -> return Nothing
612 _ -> return Nothing
613 613
614 614
615{- 615{-
@@ -650,7 +650,7 @@ parsePresenceStatus ns stanzaTag = do
650 statusv <- liftIO . atomically $ newTChan 650 statusv <- liftIO . atomically $ newTChan
651 fix $ \loop -> do 651 fix $ \loop -> do
652 mtag <- nextElement 652 mtag <- nextElement
653 flip (maybe $ return ()) mtag $ \tag -> do 653 forM_ mtag $ \tag -> do
654 when (nameNamespace (tagName tag) == Just ns) $ do 654 when (nameNamespace (tagName tag) == Just ns) $ do
655 case nameLocalName (tagName tag) of 655 case nameLocalName (tagName tag) of
656 "show" -> do t <- XML.content 656 "show" -> do t <- XML.content
@@ -749,11 +749,11 @@ parseMessage ns stanza = do
749 749
750findConditionTag :: Monad m => NestingXML o m (Maybe XML.Event) 750findConditionTag :: Monad m => NestingXML o m (Maybe XML.Event)
751findConditionTag = do 751findConditionTag = do
752 x <- nextElement 752 mx <- nextElement
753 flip (maybe $ return Nothing) x $ \x -> do 753 fmap join $ forM mx $ \x -> do
754 case nameNamespace (tagName x) of 754 case nameNamespace (tagName x) of
755 Just "urn:ietf:params:xml:ns:xmpp-stanzas" -> return (Just x) 755 Just "urn:ietf:params:xml:ns:xmpp-stanzas" -> return (Just x)
756 _ -> findConditionTag 756 _ -> findConditionTag
757 757
758conditionFromText :: Text -> Maybe StanzaError 758conditionFromText :: Text -> Maybe StanzaError
759conditionFromText t = fmap fst $ listToMaybe ss 759conditionFromText t = fmap fst $ listToMaybe ss
@@ -765,8 +765,8 @@ conditionFromText t = fmap fst $ listToMaybe ss
765findErrorTag :: Monad m => Text -> NestingXML o m (Maybe StanzaError) 765findErrorTag :: Monad m => Text -> NestingXML o m (Maybe StanzaError)
766findErrorTag ns = do 766findErrorTag ns = do
767 x <- nextElement 767 x <- nextElement
768 flip (maybe $ return Nothing) x $ \x -> do 768 fmap join $ forM x $ \x ->
769 case tagName x of 769 case tagName x of
770 n | nameNamespace n==Just ns && nameLocalName n=="error" 770 n | nameNamespace n==Just ns && nameLocalName n=="error"
771 -> do 771 -> do
772 mtag <- findConditionTag 772 mtag <- findConditionTag
@@ -958,7 +958,7 @@ xmppInbound sv xmpp k laddr pingflag stanzas output donevar = doNestingXML $ do
958 } 958 }
959 ioWriteChan stanzas s 959 ioWriteChan stanzas s
960 you <- liftIO tellyourname 960 you <- liftIO tellyourname
961 flip (maybe $ unrecog) dispatch $ \dispatch -> 961 fromMaybe unrecog $ dispatch <&> \dispatch ->
962 case dispatch of 962 case dispatch of
963 -- Checking that the to-address matches this server. 963 -- Checking that the to-address matches this server.
964 -- Otherwise it could be a client-to-client ping or a 964 -- Otherwise it could be a client-to-client ping or a
@@ -1150,7 +1150,7 @@ wrapStanzaList xs = do
1150wrapStanzaConduit :: Monad m => StanzaWrap a -> ConduitM Event (Either (StanzaWrap Event) Event) m () 1150wrapStanzaConduit :: Monad m => StanzaWrap a -> ConduitM Event (Either (StanzaWrap Event) Event) m ()
1151wrapStanzaConduit stanza = do 1151wrapStanzaConduit stanza = do
1152 mfirst <- await 1152 mfirst <- await
1153 flip (maybe $ return ()) mfirst $ \first -> do 1153 forM_ mfirst $ \first -> do
1154 yield . Left $ stanza { stanzaChan = first } 1154 yield . Left $ stanza { stanzaChan = first }
1155 awaitForever $ yield . Right 1155 awaitForever $ yield . Right
1156 1156
@@ -1349,7 +1349,7 @@ forkConnection sv xmpp k laddr pingflag src snk stanzas = do
1349 wlog $ "handling Error hacks" 1349 wlog $ "handling Error hacks"
1350 b <- atomically $ do m <- readTVar hacks 1350 b <- atomically $ do m <- readTVar hacks
1351 cached <- readTVar msgids 1351 cached <- readTVar msgids
1352 flip (maybe $ return False) (stanzaId stanza) $ \id' -> do 1352 fromMaybe (return False) $ stanzaId stanza <&> \id' -> do
1353 return $ Map.member SimulatedChatErrors m 1353 return $ Map.member SimulatedChatErrors m
1354 && elem id' cached 1354 && elem id' cached
1355 ids <- atomically $ readTVar msgids 1355 ids <- atomically $ readTVar msgids
@@ -1442,7 +1442,7 @@ sendRoster query xmpp clientKey replyto = do
1442 let k = case stanzaOrigin query of 1442 let k = case stanzaOrigin query of
1443 NetworkOrigin k _ -> Just k 1443 NetworkOrigin k _ -> Just k
1444 LocalPeer -> Nothing -- local peer requested roster? 1444 LocalPeer -> Nothing -- local peer requested roster?
1445 flip (maybe $ return ()) k $ \k -> do 1445 forM_ k $ \k -> do
1446 hostname <- xmppTellMyNameToClient xmpp clientKey 1446 hostname <- xmppTellMyNameToClient xmpp clientKey
1447 let getlist f = do 1447 let getlist f = do
1448 bs <- f xmpp k 1448 bs <- f xmpp k