]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Metainfo.hs
dfedb59dbdd3d57cf3643348d9e70e287f2192d0
[functorrent.git] / src / FuncTorrent / Metainfo.hs
1 module FuncTorrent.Metainfo
2     (Info(..),
3      Metainfo(..),
4      infoHash,
5      mkInfo,
6      mkMetaInfo
7     ) where
8
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)
14
15 import FuncTorrent.Bencode (BVal(..), InfoDict, encode, bstrToString)
16
17 -- only single file mode supported for the time being.
18 data Info = Info { pieceLength :: !Integer
19                  , pieces :: !ByteString
20                  , private :: !(Maybe Integer)
21                  , name :: !String
22                  , lengthInBytes :: !Integer
23                  , md5sum :: !(Maybe String)
24                  } deriving (Eq, Show)
25
26 data Metainfo = Metainfo { info :: !Info
27                          , announceList :: ![String]
28                          , creationDate :: !(Maybe Integer)
29                          , comment :: !(Maybe String)
30                          , createdBy :: !(Maybe String)
31                          , encoding :: !(Maybe String)
32                          } deriving (Eq, Show)
33
34 mkInfo :: BVal -> Maybe Info
35 mkInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
36                        (Bstr pieces') = m ! "pieces"
37                        private' = Nothing
38                        (Bstr name') = m ! "name"
39                        (Bint length') = m ! "length"
40                        md5sum' = Nothing
41                    in Just Info { pieceLength = pieceLength'
42                                 , pieces = pieces'
43                                 , private = private'
44                                 , name = unpack name'
45                                 , lengthInBytes = length'
46                                 , md5sum = md5sum'}
47 mkInfo _ = Nothing
48
49 maybeBstrToString :: Maybe BVal -> Maybe String
50 maybeBstrToString (Just (Bstr bs)) = Just $ unpack bs
51 maybeBstrToString _ = Nothing
52
53 maybeBstrToInteger :: Maybe BVal -> Maybe Integer
54 maybeBstrToInteger (Just (Bint bs)) = Just  bs
55 maybeBstrToInteger _ = Nothing
56
57 mkMetaInfo :: BVal -> Maybe Metainfo
58 mkMetaInfo (Bdict m) = let (Just info') = mkInfo $ m ! "info"
59                            announce' = lookup "announce" m
60                            announceList' = lookup "announce-list" m
61                            creationDate' = lookup "creation date" m
62                            comment' = lookup "comment" m
63                            createdBy' = lookup "created by" m
64                            encoding' = lookup "encoding" m
65                        in Just Metainfo { info = info'
66                                         , announceList = maybeToList (announce' >>= bstrToString)
67                                                          ++ getAnnounceList announceList'
68                                         , creationDate = maybeBstrToInteger creationDate'
69                                         , comment = maybeBstrToString comment'
70                                         , createdBy = maybeBstrToString createdBy'
71                                         , encoding = maybeBstrToString encoding'
72                                         }
73 mkMetaInfo _ = Nothing
74
75 getAnnounceList :: Maybe BVal -> [String]
76 getAnnounceList Nothing = []
77 getAnnounceList (Just (Bint _)) = []
78 getAnnounceList (Just (Bstr _)) = []
79 getAnnounceList (Just (Blist l)) = map (\s -> case s of
80                                                (Bstr s') ->  unpack s'
81                                                (Blist s') -> case s' of
82                                                               [Bstr s''] -> unpack s''
83                                                               _ -> ""
84                                                _ -> "") l
85
86 getAnnounceList (Just (Bdict _)) = []
87
88 -- | Info hash is urlencoded 20 byte SHA1 hash of the value of the info key from
89 -- the Metainfo file. Note that the value will be a bencoded dictionary, given
90 -- the definition of the info key above. TODO: `Metainfo -> ByteString`
91 infoHash :: InfoDict -> ByteString
92 infoHash m = hash . encode $ (m ! "info")