From 50d3bb1fc90c4d83d390fa2c5b328935d0ffed1d Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 26 Oct 2017 21:18:26 -0400 Subject: Publish method to send Tox friend-request. --- src/Network/Tox/DHT/Transport.hs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/Network/Tox/DHT') diff --git a/src/Network/Tox/DHT/Transport.hs b/src/Network/Tox/DHT/Transport.hs index 47505a21..b7982a1a 100644 --- a/src/Network/Tox/DHT/Transport.hs +++ b/src/Network/Tox/DHT/Transport.hs @@ -18,6 +18,8 @@ module Network.Tox.DHT.Transport , SendNodes(..) , DHTPublicKey(..) , FriendRequest(..) + , NoSpam(..) + , verifyChecksum , CookieRequest , Cookie , DHTRequest @@ -36,6 +38,7 @@ import Network.QueryResponse import Control.Arrow import Control.Monad import Data.Bool +import qualified Data.ByteString.Char8 as B8 import qualified Data.ByteString as B ;import Data.ByteString (ByteString) import Data.Functor.Contravariant @@ -45,6 +48,9 @@ import Data.Tuple import Data.Word import Foreign.C (CTime(..)) import Network.Socket +import qualified Data.ByteString.Base64 as Base64 +import qualified Data.ByteString.Base16 as Base16 +import Data.Char (isSpace) type DHTTransport = Transport String NodeInfo (DHTMessage Encrypted8) type HandleHi a = Maybe (Either String (DHTMessage Encrypted8, NodeInfo)) -> IO a @@ -221,6 +227,32 @@ data FriendRequest = FriendRequest } deriving (Eq, Show) +data NoSpam = NoSpam !Word32 !(Maybe Word16) + +instance Read NoSpam where + readsPrec d s = case break isSpace s of + (ws,rs) | (length ws == 6) -> base64decode rs (flip NoSpam Nothing <$> get) ws + (ws,rs) | (length ws == 8) -> base64decode rs (NoSpam <$> get <*> (Just <$> get)) ws + (ws,rs) | (length ws == 12) -> base16decode rs (NoSpam <$> get <*> (Just <$> get)) ws + _ -> [] + +base64decode :: Monad m => t1 -> Get t -> String -> m (t, t1) +base64decode rs getter s = + either fail (\a -> return (a,rs)) + $ runGet getter + =<< Base64.decode (B8.pack s) + +base16decode :: Monad m => t1 -> Get t -> String -> m (t, t1) +base16decode rs getter s = + either fail (\a -> return (a,rs)) + $ runGet getter + $ fst + $ Base16.decode (B8.pack s) + +verifyChecksum :: PublicKey -> Word16 -> Either String () +verifyChecksum _ _ = return () -- TODO + + -- When sent as a DHT request packet (this is the data sent in the DHT request -- packet): -- -- cgit v1.2.3