1 module FuncTorrent.Metainfo
9 import Prelude hiding (lookup)
10 import Data.ByteString.Char8 (ByteString, unpack)
11 import Data.Map as M ((!), lookup)
12 import Crypto.Hash.SHA1 (hash)
13 import Data.Maybe (maybeToList)
15 import FuncTorrent.Bencode (BVal(..), InfoDict, encode, bstrToString)
17 -- only single file mode supported for the time being.
18 data Info = Info { pieceLength :: !Integer
19 , pieces :: !ByteString
20 , private :: !(Maybe Integer)
22 , lengthInBytes :: !Integer
23 , md5sum :: !(Maybe String)
26 data Metainfo = Metainfo { info :: !Info
27 , announceList :: ![String]
28 , creationDate :: !(Maybe String)
29 , comment :: !(Maybe String)
30 , createdBy :: !(Maybe String)
31 , encoding :: !(Maybe String)
34 mkInfo :: BVal -> Maybe Info
35 mkInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
36 (Bstr pieces') = m ! "pieces"
38 (Bstr name') = m ! "name"
39 (Bint length') = m ! "length"
41 in Just Info { pieceLength = pieceLength'
45 , lengthInBytes = length'
49 maybeBstrToString :: Maybe BVal -> Maybe String
50 maybeBstrToString (Just (Bstr bs)) = Just $ unpack bs
51 maybeBstrToString _ = Nothing
53 mkMetaInfo :: BVal -> Maybe Metainfo
54 mkMetaInfo (Bdict m) = let (Just info') = mkInfo $ m ! "info"
55 announce' = lookup "announce" m
56 announceList' = lookup "announce-list" m
57 -- creationDate = lookup (Bstr (pack "creation date")) m
58 creationDate' = Nothing
59 comment' = lookup "comment" m
60 createdBy' = lookup "created by" m
61 encoding' = lookup "encoding" m
62 in Just Metainfo { info = info'
63 , announceList = maybeToList (announce' >>= bstrToString)
64 ++ getAnnounceList announceList'
65 , creationDate = creationDate'
66 , comment = maybeBstrToString comment'
67 , createdBy = maybeBstrToString createdBy'
68 , encoding = maybeBstrToString encoding'
70 mkMetaInfo _ = Nothing
72 getAnnounceList :: Maybe BVal -> [String]
73 getAnnounceList Nothing = []
74 getAnnounceList (Just (Bint _)) = []
75 getAnnounceList (Just (Bstr _)) = []
76 getAnnounceList (Just (Blist l)) = map (\s -> case s of
77 (Bstr s') -> unpack s'
78 (Blist s') -> case s' of
79 [Bstr s''] -> unpack s''
83 getAnnounceList (Just (Bdict _)) = []
85 -- | Info hash is urlencoded 20 byte SHA1 hash of the value of the info key from
86 -- the Metainfo file. Note that the value will be a bencoded dictionary, given
87 -- the definition of the info key above. TODO: `Metainfo -> ByteString`
88 infoHash :: InfoDict -> ByteString
89 infoHash m = hash . encode $ (m ! "info")