diff options
Diffstat (limited to 'ToxToXMPP.hs')
-rw-r--r-- | ToxToXMPP.hs | 70 |
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 | ||
18 | import Data.Conduit as C | 18 | import Control.Applicative |
19 | import qualified Data.Conduit.List as CL | 19 | import Data.Conduit as C |
20 | import Data.XML.Types as XML | 20 | import qualified Data.Conduit.List as CL |
21 | import Data.Monoid | ||
22 | import Data.Text.Encoding as T | ||
23 | import Data.XML.Types as XML | ||
21 | import EventUtil | 24 | import EventUtil |
22 | import Network.Tox.Crypto.Transport as Tox | 25 | import Network.Tox.Crypto.Transport as Tox |
23 | import Network.Tox.Handshake (HandshakeParams (..)) | 26 | import Network.Tox.Handshake (HandshakeParams (..)) |
24 | import Util (unsplitJID) | 27 | import qualified Text.XML.Stream.Parse as XML |
25 | import XMPPServer as XMPP | 28 | import Util (unsplitJID) |
29 | import XMPPServer as XMPP | ||
30 | |||
26 | 31 | ||
27 | import Announcer | 32 | import Announcer |
28 | import Announcer.Tox | 33 | import Announcer.Tox |
@@ -71,31 +76,39 @@ import GHC.Conc (labelThread) | |||
71 | #endif | 76 | #endif |
72 | import DPut | 77 | import DPut |
73 | import Nesting | 78 | import Nesting |
74 | 79 | import XMPPToTox | |
75 | xmppToTox :: Conduit XML.Event IO Tox.CryptoMessage | ||
76 | xmppToTox = 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 | ||
87 | toxToXmpp :: Monad m => SockAddr -> PublicKey -> Text -> Conduit Tox.CryptoMessage m XML.Event | 81 | toxToXmpp :: Monad m => SockAddr -> PublicKey -> Text -> Conduit Tox.CryptoMessage m XML.Event |
88 | toxToXmpp laddr me theirhost = do | 82 | toxToXmpp 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" | |
97 | xmppInstantMessage :: Monad m => Text -> Maybe Text -> Maybe Text -> Text -> ConduitM i Event m () | 91 | (Just $ "root@" <> theirhost) -- /from/ |
98 | xmppInstantMessage 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 | |||
105 | xmppInstantMessage :: Monad m => Text | ||
106 | -> Maybe Text | ||
107 | -> Maybe Text | ||
108 | -> [(Name, [Content])] | ||
109 | -> Text | ||
110 | -> ConduitM i Event m () | ||
111 | xmppInstantMessage 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" |