]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/Main.hs
project renamed to "functorrent"
[functorrent.git] / src / Main.hs
index 7ab471258fdb0f70f9d5db6b32da77d5f69ce6c7..14656d12689135c477c5bf60a8ffef2d0b787172 100644 (file)
@@ -1,12 +1,41 @@
 module Main where
 
 import System.Environment (getArgs)
+import System.Exit
 import qualified Data.ByteString.Char8 as BC
 import qualified Bencode as Benc
+import qualified Metainfo as MInfo
+import qualified Tracker as T
+import qualified Text.ParserCombinators.Parsec as Parsec
+import qualified Peer as P
+import Data.Functor
+
+printError :: Parsec.ParseError -> IO ()
+printError e = putStrLn $ "parse error: " ++ show e
+
+genPeerId :: String
+genPeerId = "-HS0001-20150215"
+
+exit :: IO BC.ByteString
+exit = exitWith ExitSuccess
+
+usage :: IO ()
+usage = putStrLn "usage: functorrent torrent-file"
+
+parse :: [String] -> IO (BC.ByteString)
+parse [] = usage >> exit
+parse [a] = BC.readFile a
+parse _ = exit
 
 main :: IO ()
 main = do
   args <- getArgs
-  torrentStr <- BC.readFile (head args)
-  let metadata = Benc.decode torrentStr
+  torrentStr <- parse args
+  case (Benc.decode torrentStr) of
+   Right d -> case (MInfo.mkMetaInfo d) of
+               Nothing -> putStrLn "parse error"
+               Just m -> do
+                 body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d genPeerId)
+                 putStrLn (show (P.getPeers (P.getPeerResponse body)))
+   Left e -> printError e
   putStrLn "done"