1 {-# LANGUAGE OverloadedStrings #-}
4 import Prelude hiding (length, readFile, writeFile)
5 import Data.ByteString.Char8 (ByteString, readFile, writeFile, length)
6 import System.Environment (getArgs)
7 import System.Exit (exitSuccess)
8 import Text.ParserCombinators.Parsec (ParseError)
10 import FuncTorrent.Bencode (decode, BVal(..))
11 import FuncTorrent.Logger (initLogger, logMessage, logStop)
12 import FuncTorrent.Metainfo (announce, lengthInBytes, mkMetaInfo, info, name)
13 import FuncTorrent.Peer (getPeers, getPeerResponse, handShakeMsg)
14 import FuncTorrent.Tracker (connect, prepareRequest)
16 logError :: ParseError -> (String -> IO ()) -> IO ()
17 logError e logMsg = logMsg $ "parse error: \n" ++ show e
20 peerId = "-HS0001-*-*-20150215"
26 usage = putStrLn "usage: functorrent torrent-file"
28 parse :: [String] -> IO ByteString
29 parse [] = usage >> exit
30 parse [a] = readFile a
37 let logMsg = logMessage logR
38 logMsg $ "Parsing input file: " ++ concat args
39 torrentStr <- parse args
40 case decode torrentStr of
43 Nothing -> logMsg "parse error"
45 logMsg "Input File OK"
47 let len = lengthInBytes $ info m
50 logMsg "Trying to fetch peers: "
51 body <- connect (announce m) (prepareRequest d' peerId len)
53 -- TODO: Write to ~/.functorrent/caches
54 writeFile (name (info m) ++ ".cache") body
56 let peerResponse = show $ getPeers $ getPeerResponse body
57 logMsg $ "Peers List : " ++ peerResponse
59 let hsMsgLen = show $ length $ handShakeMsg d' peerId
60 logMsg $ "Hand-shake message length : " ++ hsMsgLen
62 Left e -> logError e logMsg