3 import System.Environment (getArgs)
5 import qualified Data.ByteString.Char8 as BC
6 import qualified Bencode as Benc
7 import qualified Metainfo as MInfo
8 import qualified Tracker as T
9 import qualified Text.ParserCombinators.Parsec as Parsec
10 import qualified Peer as P
13 printError :: Parsec.ParseError -> IO ()
14 printError e = putStrLn $ "parse error: " ++ show e
17 genPeerId = "-HS0001-20150215"
19 exit :: IO BC.ByteString
20 exit = exitWith ExitSuccess
23 usage = putStrLn "usage: functorrent torrent-file"
25 parse :: [String] -> IO (BC.ByteString)
26 parse [] = usage >> exit
27 parse [a] = BC.readFile a
33 torrentStr <- parse args
34 case (Benc.decode torrentStr) of
35 Right d -> case (MInfo.mkMetaInfo d) of
36 Nothing -> putStrLn "parse error"
38 body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d genPeerId)
39 putStrLn (show (P.getPeers (P.getPeerResponse body)))
40 Left e -> printError e