diff options
Diffstat (limited to 'XMPPToTox.hs')
-rw-r--r-- | XMPPToTox.hs | 67 |
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 #-} | ||
3 | module XMPPToTox | ||
4 | ( module XMPPToTox | ||
5 | , CryptoMessage(..) | ||
6 | , MessageID(..) | ||
7 | ) where | ||
8 | |||
9 | import Control.Applicative | ||
10 | import Control.Monad | ||
11 | import Control.Monad.Catch | ||
12 | import Data.Conduit | ||
13 | import Data.Function | ||
14 | import Data.Monoid | ||
15 | import Data.Text (Text) | ||
16 | import Data.Text.Encoding as T | ||
17 | import Data.XML.Types as XML | ||
18 | import Network.Tox.Crypto.Transport (CryptoMessage (..), MessageID (..)) | ||
19 | import Text.XML.Stream.Parse as XML | ||
20 | |||
21 | -- Debugging. Not real Tox message. | ||
22 | funnyMessage :: MonadThrow m => Text -> ConduitM i CryptoMessage m () | ||
23 | funnyMessage txt = yield $ UpToN Padding (T.encodeUtf8 txt) | ||
24 | |||
25 | sendMsg :: MonadThrow m => Text -> ConduitM i CryptoMessage m () | ||
26 | sendMsg txt = yield $ UpToN MESSAGE (T.encodeUtf8 txt) | ||
27 | |||
28 | |||
29 | eom :: MonadThrow m => ConduitM Event o m () | ||
30 | eom = many_ ignoreAnyTreeContent | ||
31 | |||
32 | msgToTox :: MonadThrow m => | ||
33 | ConduitM Event CryptoMessage m (Maybe ()) | ||
34 | msgToTox = 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 | |||
51 | unknownToTox :: MonadThrow m => | ||
52 | ConduitM Event CryptoMessage m (Maybe ()) | ||
53 | unknownToTox = tag anyName (\n -> ignoreAttrs >> return n) $ \n -> do | ||
54 | funnyMessage $ nameLocalName n | ||
55 | eom | ||
56 | |||
57 | |||
58 | xmppToTox :: MonadThrow m => Conduit XML.Event m CryptoMessage | ||
59 | xmppToTox = do | ||
60 | eventBeginDocument <- await | ||
61 | streamTag <- await | ||
62 | fix $ \loop -> do | ||
63 | got <- choose | ||
64 | [ msgToTox | ||
65 | , unknownToTox | ||
66 | ] | ||
67 | forM_ got $ \_ -> loop | ||