module Network.Xmpp.Concurrent.Message where
import Control.Applicative((<$>))
import Network.Xmpp.Concurrent.Types
import Control.Concurrent.STM
import Network.Xmpp.Types
import Network.Xmpp.Concurrent.Basic
pullMessageA :: Session -> IO (Either (Annotated MessageError) (Annotated Message))
pullMessageA session = do
(stanza, as) <- atomically . readTChan $ stanzaCh session
case stanza of
MessageS m -> return $ Right (m, as)
MessageErrorS e -> return $ Left (e, as)
_ -> pullMessageA session
pullMessage :: Session -> IO (Either MessageError Message)
pullMessage s = either (Left . fst) (Right . fst) <$> pullMessageA s
getMessageA :: Session -> IO (Annotated Message)
getMessageA = waitForMessageA (const True)
getMessage :: Session -> IO Message
getMessage s = fst <$> getMessageA s
waitForMessageA :: (Annotated Message -> Bool) -> Session -> IO (Annotated Message)
waitForMessageA f session = do
s <- pullMessageA session
case s of
Left _ -> waitForMessageA f session
Right m | f m -> return m
| otherwise -> waitForMessageA f session
waitForMessage :: (Message -> Bool) -> Session -> IO Message
waitForMessage f s = fst <$> waitForMessageA (f . fst) s
waitForMessageErrorA :: (Annotated MessageError -> Bool)
-> Session
-> IO (Annotated MessageError)
waitForMessageErrorA f session = do
s <- pullMessageA session
case s of
Right _ -> waitForMessageErrorA f session
Left m | f m -> return m
| otherwise -> waitForMessageErrorA f session
waitForMessageError :: (MessageError -> Bool) -> Session -> IO MessageError
waitForMessageError f s = fst <$> waitForMessageErrorA (f . fst) s
filterMessagesA :: (Annotated MessageError -> Bool)
-> (Annotated Message -> Bool)
-> Session -> IO (Either (Annotated MessageError)
(Annotated Message))
filterMessagesA f g session = do
s <- pullMessageA session
case s of
Left e | f e -> return $ Left e
| otherwise -> filterMessagesA f g session
Right m | g m -> return $ Right m
| otherwise -> filterMessagesA f g session
filterMessages :: (MessageError -> Bool)
-> (Message -> Bool)
-> Session
-> IO (Either MessageError Message)
filterMessages f g s = either (Left . fst) (Right . fst) <$>
filterMessagesA (f . fst) (g . fst) s
sendMessage :: Message -> Session -> IO (Either XmppFailure ())
sendMessage m session = sendStanza (MessageS m) session