summaryrefslogtreecommitdiff
path: root/ToxToXMPP.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ToxToXMPP.hs')
-rw-r--r--ToxToXMPP.hs70
1 files changed, 41 insertions, 29 deletions
diff --git a/ToxToXMPP.hs b/ToxToXMPP.hs
index b75ada6a..9979526a 100644
--- a/ToxToXMPP.hs
+++ b/ToxToXMPP.hs
@@ -15,14 +15,19 @@ module ToxToXMPP
15 , interweave 15 , interweave
16 ) where 16 ) where
17 17
18import Data.Conduit as C 18import Control.Applicative
19import qualified Data.Conduit.List as CL 19import Data.Conduit as C
20import Data.XML.Types as XML 20import qualified Data.Conduit.List as CL
21import Data.Monoid
22import Data.Text.Encoding as T
23import Data.XML.Types as XML
21import EventUtil 24import EventUtil
22import Network.Tox.Crypto.Transport as Tox 25import Network.Tox.Crypto.Transport as Tox
23import Network.Tox.Handshake (HandshakeParams (..)) 26import Network.Tox.Handshake (HandshakeParams (..))
24import Util (unsplitJID) 27import qualified Text.XML.Stream.Parse as XML
25import XMPPServer as XMPP 28import Util (unsplitJID)
29import XMPPServer as XMPP
30
26 31
27import Announcer 32import Announcer
28import Announcer.Tox 33import Announcer.Tox
@@ -71,31 +76,39 @@ import GHC.Conc (labelThread)
71#endif 76#endif
72import DPut 77import DPut
73import Nesting 78import Nesting
74 79import XMPPToTox
75xmppToTox :: Conduit XML.Event IO Tox.CryptoMessage
76xmppToTox = doNestingXML $ do
77 eventBeginDocument <- await
78 streamTag <- await
79 fix $ \loop -> do
80 e <- nextElement
81 -- dput DPut.XMan $ "xmppToTox: " ++ show e
82 --
83 -- (yield e >> awaitForever yield) $$ prettyPrint "xmpp->Tox"
84 -- prettyPrint
85 loop
86 80
87toxToXmpp :: Monad m => SockAddr -> PublicKey -> Text -> Conduit Tox.CryptoMessage m XML.Event 81toxToXmpp :: Monad m => SockAddr -> PublicKey -> Text -> Conduit Tox.CryptoMessage m XML.Event
88toxToXmpp laddr me theirhost = do 82toxToXmpp laddr me theirhost = do
89 CL.sourceList $ XMPP.greet' "jabber:server" theirhost 83 CL.sourceList $ XMPP.greet' "jabber:server" theirhost
90 let me_u = T.pack $ show (key2id me) 84 let me_u = T.pack $ show (key2id me)
91 awaitForever $ \toxmsg -> do 85 awaitForever $ \case
92 xmppInstantMessage "jabber:server" 86
93 (Just theirhost) -- /from/ 87 UpToN { msgID = MESSAGE
94 (Just $ unsplitJID (Just me_u,T.pack (show laddr),Nothing)) -- /to/ should match local address of this node. 88 , msgBytes = bs }
95 (T.pack $ show $ msgID toxmsg) 89 -> do
96 90 xmppInstantMessage "jabber:server"
97xmppInstantMessage :: Monad m => Text -> Maybe Text -> Maybe Text -> Text -> ConduitM i Event m () 91 (Just $ "root@" <> theirhost) -- /from/
98xmppInstantMessage namespace mfrom mto text = do 92 (Just $ unsplitJID (Just me_u,T.pack (show laddr),Nothing)) -- /to/ should match local address of this node.
93 []
94 (T.decodeUtf8 bs)
95
96 toxmsg | msgID toxmsg == PacketRequest -> return ()
97
98 toxmsg -> do
99 xmppInstantMessage "jabber:server"
100 (Just theirhost) -- /from/
101 (Just $ unsplitJID (Just me_u,T.pack (show laddr),Nothing)) -- /to/ should match local address of this node.
102 [ attr "style" "font-weight:bold; color:red" ]
103 (T.pack $ show $ msgID toxmsg)
104
105xmppInstantMessage :: Monad m => Text
106 -> Maybe Text
107 -> Maybe Text
108 -> [(Name, [Content])]
109 -> Text
110 -> ConduitM i Event m ()
111xmppInstantMessage namespace mfrom mto style text = do
99 let ns n = n { nameNamespace = Just namespace } 112 let ns n = n { nameNamespace = Just namespace }
100 C.yield $ EventBeginElement (ns "message") 113 C.yield $ EventBeginElement (ns "message")
101 ( maybe id (\t->(attr "from" t:)) mfrom 114 ( maybe id (\t->(attr "from" t:)) mfrom
@@ -106,8 +119,7 @@ xmppInstantMessage namespace mfrom mto text = do
106 C.yield $ EventEndElement (ns "body") 119 C.yield $ EventEndElement (ns "body")
107 C.yield $ EventBeginElement "{http://jabber.org/protocol/xhtml-im}html" [] 120 C.yield $ EventBeginElement "{http://jabber.org/protocol/xhtml-im}html" []
108 C.yield $ EventBeginElement "{http://www.w3.org/1999/xhtml}body" [] 121 C.yield $ EventBeginElement "{http://www.w3.org/1999/xhtml}body" []
109 C.yield $ EventBeginElement "{http://www.w3.org/1999/xhtml}p" 122 C.yield $ EventBeginElement "{http://www.w3.org/1999/xhtml}p" style
110 [ attr "style" "font-weight:bold; color:red" ]
111 C.yield $ EventContent $ ContentText text 123 C.yield $ EventContent $ ContentText text
112 C.yield $ EventEndElement "{http://www.w3.org/1999/xhtml}p" 124 C.yield $ EventEndElement "{http://www.w3.org/1999/xhtml}p"
113 C.yield $ EventEndElement "{http://www.w3.org/1999/xhtml}body" 125 C.yield $ EventEndElement "{http://www.w3.org/1999/xhtml}body"