]> git.rkrishnan.org Git - functorrent.git/blob - src/Main.hs
more refactoring
[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 Data.Functor
11
12 printError :: Parsec.ParseError -> IO ()
13 printError e = putStrLn $ "parse error: " ++ show e
14
15 genPeerId :: String
16 genPeerId = "-HS0001-20150215"
17
18 exit :: IO BC.ByteString
19 exit = exitWith ExitSuccess
20
21 usage :: IO ()
22 usage = putStrLn "usage: deluge torrent-file"
23
24 parse :: [String] -> IO (BC.ByteString)
25 parse [] = usage >> exit
26 parse [a] = BC.readFile a
27 parse _ = exit
28
29 main :: IO ()
30 main = do
31   args <- getArgs
32   torrentStr <- parse args
33   case (Benc.decode torrentStr) of
34    Right d -> case (MInfo.mkMetaInfo d) of
35                Nothing -> putStrLn "parse error"
36                Just m -> do
37                  body <- (Benc.decode . BC.pack) <$> T.connect (MInfo.announce m) (T.prepareRequest d genPeerId)
38                  putStrLn (show body)
39    Left e -> printError e
40   putStrLn "done"