summaryrefslogtreecommitdiff
path: root/XMPPToTox.hs
diff options
context:
space:
mode:
Diffstat (limited to 'XMPPToTox.hs')
-rw-r--r--XMPPToTox.hs67
1 files changed, 67 insertions, 0 deletions
diff --git a/XMPPToTox.hs b/XMPPToTox.hs
new file mode 100644
index 00000000..7ca4330e
--- /dev/null
+++ b/XMPPToTox.hs
@@ -0,0 +1,67 @@
1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE NoMonomorphismRestriction #-}
3module XMPPToTox
4 ( module XMPPToTox
5 , CryptoMessage(..)
6 , MessageID(..)
7 ) where
8
9import Control.Applicative
10import Control.Monad
11import Control.Monad.Catch
12import Data.Conduit
13import Data.Function
14import Data.Monoid
15import Data.Text (Text)
16import Data.Text.Encoding as T
17import Data.XML.Types as XML
18import Network.Tox.Crypto.Transport (CryptoMessage (..), MessageID (..))
19import Text.XML.Stream.Parse as XML
20
21-- Debugging. Not real Tox message.
22funnyMessage :: MonadThrow m => Text -> ConduitM i CryptoMessage m ()
23funnyMessage txt = yield $ UpToN Padding (T.encodeUtf8 txt)
24
25sendMsg :: MonadThrow m => Text -> ConduitM i CryptoMessage m ()
26sendMsg txt = yield $ UpToN MESSAGE (T.encodeUtf8 txt)
27
28
29eom :: MonadThrow m => ConduitM Event o m ()
30eom = many_ ignoreAnyTreeContent
31
32msgToTox :: MonadThrow m =>
33 ConduitM Event CryptoMessage m (Maybe ())
34msgToTox = tag' "{jabber:server}message"
35 (requireAttr "type" >>= \case
36 "chat" -> ignoreAttrs
37 _ -> empty)
38 $ \_ -> many_ $ choose
39 [ tagIgnoreAttrs "{jabber:server}body"
40 $ do content >>= sendMsg
41 eom
42 , tagIgnoreAttrs "{http://jabber.org/protocol/chatstates}composing"
43 $ do yield $ TwoByte TYPING 1
44 eom
45 , tagIgnoreAttrs "{http://jabber.org/protocol/chatstates}paused"
46 $ do yield $ TwoByte TYPING 0
47 eom
48 , ignoreAnyTreeContent
49 ]
50
51unknownToTox :: MonadThrow m =>
52 ConduitM Event CryptoMessage m (Maybe ())
53unknownToTox = tag anyName (\n -> ignoreAttrs >> return n) $ \n -> do
54 funnyMessage $ nameLocalName n
55 eom
56
57
58xmppToTox :: MonadThrow m => Conduit XML.Event m CryptoMessage
59xmppToTox = do
60 eventBeginDocument <- await
61 streamTag <- await
62 fix $ \loop -> do
63 got <- choose
64 [ msgToTox
65 , unknownToTox
66 ]
67 forM_ got $ \_ -> loop