]> git.rkrishnan.org Git - functorrent.git/blob - src/Main.hs
Absolute import everywhere, cleanup
[functorrent.git] / src / Main.hs
1 module Main where
2
3 import Prelude hiding (length, readFile)
4
5 import Bencode (decode, BVal(..))
6 import Data.ByteString.Char8 as BC (ByteString, pack, length, readFile, length)
7 import Data.Functor ((<$>))
8 import Metainfo (announce, lengthInBytes, mkMetaInfo, info)
9 import Peer (getPeers, getPeerResponse, handShakeMsg)
10 import System.Environment (getArgs)
11 import System.Exit (exitSuccess)
12 import Tracker (connect, prepareRequest)
13 import Text.ParserCombinators.Parsec (ParseError)
14
15 printError :: ParseError -> IO ()
16 printError e = putStrLn $ "parse error: " ++ show e
17
18 peerId :: String
19 peerId = "-HS0001-*-*-20150215"
20
21 exit :: IO ByteString
22 exit = exitSuccess
23
24 usage :: IO ()
25 usage = putStrLn "usage: functorrent torrent-file"
26
27 parse :: [String] -> IO ByteString
28 parse [] = usage >> exit
29 parse [a] = readFile a
30 parse _ = exit
31
32 main :: IO ()
33 main = do
34     args <- getArgs
35     torrentStr <- parse args
36     case decode torrentStr of
37       Right d ->
38           case mkMetaInfo d of
39             Nothing -> putStrLn "parse error"
40             Just m -> do
41               let len = lengthInBytes $ info m
42                   (Bdict d') = d
43               body <- pack <$> connect (announce m) (prepareRequest d' peerId len)
44               print $ getPeers $ getPeerResponse body
45               print $ length $ handShakeMsg d' peerId
46       Left e -> printError e
47     putStrLn "done"