]> git.rkrishnan.org Git - functorrent.git/blob - src/Main.hs
project renamed to "functorrent"
[functorrent.git] / src / Main.hs
1 module Main where
2
3 import System.Environment (getArgs)
4 import System.Exit
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
11 import Data.Functor
12
13 printError :: Parsec.ParseError -> IO ()
14 printError e = putStrLn $ "parse error: " ++ show e
15
16 genPeerId :: String
17 genPeerId = "-HS0001-20150215"
18
19 exit :: IO BC.ByteString
20 exit = exitWith ExitSuccess
21
22 usage :: IO ()
23 usage = putStrLn "usage: functorrent torrent-file"
24
25 parse :: [String] -> IO (BC.ByteString)
26 parse [] = usage >> exit
27 parse [a] = BC.readFile a
28 parse _ = exit
29
30 main :: IO ()
31 main = do
32   args <- getArgs
33   torrentStr <- parse args
34   case (Benc.decode torrentStr) of
35    Right d -> case (MInfo.mkMetaInfo d) of
36                Nothing -> putStrLn "parse error"
37                Just m -> do
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
41   putStrLn "done"