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 peerId = "-HS0001-*-*-20150215"
19 exit :: IO BC.ByteString
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
36 case MInfo.mkMetaInfo d of
37 Nothing -> putStrLn "parse error"
39 let len = MInfo.lengthInBytes (MInfo.info m)
40 body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d peerId len)
41 print (P.getPeers (P.getPeerResponse body))
42 Left e -> printError e