diff options
Diffstat (limited to 'ToxToXMPP.hs')
-rw-r--r-- | ToxToXMPP.hs | 59 |
1 files changed, 49 insertions, 10 deletions
diff --git a/ToxToXMPP.hs b/ToxToXMPP.hs index f23f964c..477cfc51 100644 --- a/ToxToXMPP.hs +++ b/ToxToXMPP.hs | |||
@@ -19,6 +19,7 @@ import qualified Data.Conduit.List as CL | |||
19 | import Data.XML.Types as XML | 19 | import Data.XML.Types as XML |
20 | import EventUtil | 20 | import EventUtil |
21 | import Network.Tox.Crypto.Transport as Tox | 21 | import Network.Tox.Crypto.Transport as Tox |
22 | import Util (unsplitJID) | ||
22 | import XMPPServer as XMPP | 23 | import XMPPServer as XMPP |
23 | 24 | ||
24 | import Announcer | 25 | import Announcer |
@@ -135,6 +136,9 @@ data ToxToXMPP = ToxToXMPP | |||
135 | , txTox :: Tox JabberClients | 136 | , txTox :: Tox JabberClients |
136 | } | 137 | } |
137 | 138 | ||
139 | default_nospam :: Word32 | ||
140 | default_nospam = 0x6a7a27fc -- big-endian base64: anon/A== | ||
141 | |||
138 | dispatch :: ToxToXMPP -> ContactEvent -> IO () | 142 | dispatch :: ToxToXMPP -> ContactEvent -> IO () |
139 | dispatch tx (SessionEstablished theirKey) = stopConnecting tx theirKey | 143 | dispatch tx (SessionEstablished theirKey) = stopConnecting tx theirKey |
140 | dispatch tx (SessionTerminated theirKey) = startConnecting tx theirKey | 144 | dispatch tx (SessionTerminated theirKey) = startConnecting tx theirKey |
@@ -155,8 +159,7 @@ dispatch tx (OnionRouted theirkey (OnionFriendRequest fr) ) = do | |||
155 | -- This isn't the right thing, but we don't know their user-id. Perhaps | 159 | -- This isn't the right thing, but we don't know their user-id. Perhaps |
156 | -- there should be a way to parse it out of the friend request text. Maybe | 160 | -- there should be a way to parse it out of the friend request text. Maybe |
157 | -- after a zero-termination, or as visible text (nospam:...). | 161 | -- after a zero-termination, or as visible text (nospam:...). |
158 | let default_nospam = 0x6a7a27fc -- big-endian base64: anon/A== | 162 | let theirjid = key2jid default_nospam theirkey |
159 | theirjid = key2jid default_nospam theirkey | ||
160 | forM_ k2c $ \((PerClient{pcDeliveredFRs},conn),client) -> do | 163 | forM_ k2c $ \((PerClient{pcDeliveredFRs},conn),client) -> do |
161 | alreadyDelivered <- atomically $ do | 164 | alreadyDelivered <- atomically $ do |
162 | frs <- readTVar pcDeliveredFRs | 165 | frs <- readTVar pcDeliveredFRs |
@@ -181,8 +184,27 @@ akeyConnect :: Announcer -> NodeId -> PublicKey -> IO AnnounceKey | |||
181 | akeyConnect announcer me them = atomically $ do | 184 | akeyConnect announcer me them = atomically $ do |
182 | packAnnounceKey announcer $ "connect:" ++ (take 8 $ show me) ++ ":" ++ show (key2id them) | 185 | packAnnounceKey announcer $ "connect:" ++ (take 8 $ show me) ++ ":" ++ show (key2id them) |
183 | 186 | ||
187 | -- | Returns a list of nospam values to use for friend requests to send to a | ||
188 | -- remote peer. This list is non-empty only when it is desirable to send | ||
189 | -- friend requests. | ||
190 | checkSoliciting :: PresenceState -> PublicKey -> PublicKey -> Contact -> IO [NoSpam] | ||
191 | checkSoliciting presence me them contact = do | ||
192 | xs <- getBuddiesAndSolicited presence $ \h -> do | ||
193 | -- TODO: /h/ matches hostname? | ||
194 | return False | ||
195 | return $ do | ||
196 | (is_buddy,their_u,my_uid,xmpp_client_profile) <- xs | ||
197 | guard $ xmpp_client_profile == "tox" | ||
198 | let theirhost = T.pack $ show (key2id them) ++ ".tox" | ||
199 | NoSpamId nospam _ <- case fmap T.unpack $ their_u of | ||
200 | Just ('$':_) -> maybeToList $ readMaybe $ T.unpack $ unsplitJID (their_u,theirhost,Nothing) | ||
201 | Just ('0':'x':_) -> maybeToList $ readMaybe $ T.unpack $ unsplitJID (their_u,theirhost,Nothing) | ||
202 | _ -> maybeToList $ readMaybe $ T.unpack $ key2jid default_nospam them | ||
203 | return nospam | ||
204 | |||
184 | startConnecting0 :: ToxToXMPP -> PublicKey -> Contact -> IO () | 205 | startConnecting0 :: ToxToXMPP -> PublicKey -> Contact -> IO () |
185 | startConnecting0 tx them contact = do | 206 | startConnecting0 tx them contact = do |
207 | dput XMan $ "START CONNECTING " ++ show (key2id them) | ||
186 | let ToxToXMPP { txTox = tox | 208 | let ToxToXMPP { txTox = tox |
187 | , txAnnouncer = announcer | 209 | , txAnnouncer = announcer |
188 | , txAccount = acnt } = tx | 210 | , txAccount = acnt } = tx |
@@ -193,10 +215,10 @@ startConnecting0 tx them contact = do | |||
193 | [bkts4,bkts6] | 215 | [bkts4,bkts6] |
194 | return $ foldr interweave [] nss | 216 | return $ foldr interweave [] nss |
195 | wanted <- atomically $ (==Just TryingToConnect) <$> readTVar (contactPolicy contact) | 217 | wanted <- atomically $ (==Just TryingToConnect) <$> readTVar (contactPolicy contact) |
196 | soliciting <- return False -- TODO: read subscribers file to answer this question. | 218 | let mypub = toPublic $ userSecret acnt |
219 | me = key2id mypub | ||
220 | soliciting <- checkSoliciting (txPresence tx) mypub them contact | ||
197 | when wanted $ do | 221 | when wanted $ do |
198 | let pub = toPublic $ userSecret acnt | ||
199 | me = key2id pub | ||
200 | akey <- akeyDHTKeyShare announcer me them | 222 | akey <- akeyDHTKeyShare announcer me them |
201 | -- We send this packet every 30 seconds if there is more | 223 | -- We send this packet every 30 seconds if there is more |
202 | -- than one peer (in the 8) that says they our friend is | 224 | -- than one peer (in the 8) that says they our friend is |
@@ -218,13 +240,30 @@ startConnecting0 tx them contact = do | |||
218 | dkey <- Tox.getContactInfo tox | 240 | dkey <- Tox.getContactInfo tox |
219 | let tr = Tox.toxToRoute tox | 241 | let tr = Tox.toxToRoute tox |
220 | route = Tox.AnnouncedRendezvous theirkey rendezvous | 242 | route = Tox.AnnouncedRendezvous theirkey rendezvous |
221 | sendMessage tr route (pub,Tox.OnionDHTPublicKey dkey) | 243 | dput XMan $ unwords [ take 8 (show $ key2id mypub) ++ ":" |
222 | when soliciting $ do | 244 | , "Sending my DHT-key" |
245 | , show (key2id $ Tox.dhtpk dkey) | ||
246 | , "to" | ||
247 | , show (key2id theirkey) | ||
248 | , "via" | ||
249 | , show (Tox.rendezvousNode rendezvous) | ||
250 | ] | ||
251 | sendMessage tr route (mypub,Tox.OnionDHTPublicKey dkey) | ||
252 | forM_ soliciting $ \cksum@(NoSpam nospam _)-> do | ||
253 | dput XMan $ unwords [ take 8 (show $ key2id mypub) ++ ":" | ||
254 | , "Sending friend-request" | ||
255 | , "with nospam" | ||
256 | , "(" ++ nospam64 cksum ++ "," ++nospam16 cksum ++ ")" | ||
257 | , "to" | ||
258 | , show (key2id theirkey) | ||
259 | , "via" | ||
260 | , show (Tox.rendezvousNode rendezvous) | ||
261 | ] | ||
223 | let fr = FriendRequest | 262 | let fr = FriendRequest |
224 | { friendNoSpam = _todo | 263 | { friendNoSpam = nospam |
225 | , friendRequestText = mempty | 264 | , friendRequestText = "XMPP friend request" |
226 | } | 265 | } |
227 | sendMessage tr route (pub,Tox.OnionFriendRequest fr) | 266 | sendMessage tr route (mypub,Tox.OnionFriendRequest fr) |
228 | scheduleSearch announcer akey meth them | 267 | scheduleSearch announcer akey meth them |
229 | 268 | ||
230 | startConnecting :: ToxToXMPP -> PublicKey -> IO () | 269 | startConnecting :: ToxToXMPP -> PublicKey -> IO () |