encode (Bint i) = "i" ++ show i ++ "e"
encode (Blist xs) = "l" ++ encodeList xs ++ "e"
where encodeList [] = ""
- encodeList (x:xs) = encode x ++ encodeList xs
+ encodeList (x:xs') = encode x ++ encodeList xs'
encode (Bdict d) = "d" ++ encodeDict d ++ "e"
where encodeDict m = concat [encode k ++ encode (m M.! k) | k <- M.keys m]
module Main where
import System.Environment (getArgs)
+import System.Exit
import qualified Data.ByteString.Char8 as BC
import qualified Bencode as Benc
import qualified Metainfo as MInfo
genPeerId :: String
genPeerId = "-HS0001-20150215"
+exit :: IO BC.ByteString
+exit = exitWith ExitSuccess
+
+usage :: IO ()
+usage = putStrLn "usage: deluge torrent-file"
+
+parse :: [String] -> IO (BC.ByteString)
+parse [] = usage >> exit
+parse [a] = BC.readFile a
+parse _ = exit
+
main :: IO ()
main = do
args <- getArgs
- torrentStr <- BC.readFile (head args)
+ torrentStr <- parse args
case (Benc.decode torrentStr) of
Right d -> case (MInfo.mkMetaInfo d) of
Nothing -> putStrLn "parse error"
import qualified Bencode as Benc
import qualified Data.ByteString.Char8 as BC
import qualified Data.Map as M
-import qualified Crypto.Hash as H
-import qualified Crypto.Hash.SHA1 as SHA1
-import Data.Time.Clock
-- only single file mode supported for the time being.
data Info = Info { pieceLength :: !Integer