]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/Main.hs
refactor PeerID and associated functions.
[functorrent.git] / src / Main.hs
index 24bd9a99f88236470f9e17ad5e18267b31194552..bb50a8be38f4116ea46d67c976c6711dca086e6f 100644 (file)
@@ -13,29 +13,31 @@ import Data.Functor
 printError :: Parsec.ParseError -> IO ()
 printError e = putStrLn $ "parse error: " ++ show e
 
-genPeerId :: String
-genPeerId = "-HS0001-20150215"
+peerId :: String
+peerId = "-HS0001-*-*-20150215"
 
 exit :: IO BC.ByteString
-exit = exitWith ExitSuccess
+exit = exitSuccess
 
 usage :: IO ()
-usage = putStrLn "usage: deluge torrent-file"
+usage = putStrLn "usage: functorrent torrent-file"
 
-parse :: [String] -> IO (BC.ByteString)
+parse :: [String] -> IO BC.ByteString
 parse [] = usage >> exit
 parse [a] = BC.readFile a
 parse _ = exit
 
 main :: IO ()
 main = do
-  args <- getArgs
-  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 body))
-   Left e -> printError e
-  putStrLn "done"
+    args <- getArgs
+    torrentStr <- parse args
+    case Benc.decode torrentStr of
+      Right d ->
+          case MInfo.mkMetaInfo d of
+            Nothing -> putStrLn "parse error"
+            Just m -> do
+              let len = MInfo.lengthInBytes (MInfo.info m)
+              body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d peerId len)
+              print (P.getPeers (P.getPeerResponse body))
+      Left e -> printError e
+    putStrLn "done"