module FuncTorrent.Metainfo
- (Info,
- Metainfo,
- announceList,
- mkMetaInfo,
+ (Info(..),
+ Metainfo(..),
+ infoHash,
mkInfo,
- lengthInBytes,
- info,
- name,
+ mkMetaInfo
) where
import Prelude hiding (lookup)
import Data.ByteString.Char8 (ByteString, unpack)
import Data.Map as M ((!), lookup)
+import Crypto.Hash.SHA1 (hash)
+import Data.Maybe (maybeToList)
-import FuncTorrent.Bencode (BVal(..), bstrToString)
+import FuncTorrent.Bencode (BVal(..), InfoDict, encode, bstrToString)
-- only single file mode supported for the time being.
data Info = Info { pieceLength :: !Integer
data Metainfo = Metainfo { info :: !Info
, announceList :: ![String]
- , creationDate :: !(Maybe String)
+ , creationDate :: !(Maybe Integer)
, comment :: !(Maybe String)
, createdBy :: !(Maybe String)
, encoding :: !(Maybe String)
mkInfo _ = Nothing
maybeBstrToString :: Maybe BVal -> Maybe String
-maybeBstrToString Nothing = Nothing
-maybeBstrToString (Just s) = let (Bstr bs) = s
- in Just (unpack bs)
+maybeBstrToString (Just (Bstr bs)) = Just $ unpack bs
+maybeBstrToString _ = Nothing
+
+maybeBstrToInteger :: Maybe BVal -> Maybe Integer
+maybeBstrToInteger (Just (Bint bs)) = Just bs
+maybeBstrToInteger _ = Nothing
mkMetaInfo :: BVal -> Maybe Metainfo
mkMetaInfo (Bdict m) = let (Just info') = mkInfo $ m ! "info"
announce' = lookup "announce" m
announceList' = lookup "announce-list" m
- -- creationDate = lookup (Bstr (pack "creation date")) m
- creationDate' = Nothing
+ creationDate' = lookup "creation date" m
comment' = lookup "comment" m
createdBy' = lookup "created by" m
encoding' = lookup "encoding" m
in Just Metainfo { info = info'
, announceList = maybeToList (announce' >>= bstrToString)
++ getAnnounceList announceList'
- , creationDate = creationDate'
+ , creationDate = maybeBstrToInteger creationDate'
, comment = maybeBstrToString comment'
, createdBy = maybeBstrToString createdBy'
, encoding = maybeBstrToString encoding'
}
mkMetaInfo _ = Nothing
-maybeToList :: Maybe a -> [a]
-maybeToList Nothing = []
-maybeToList (Just x) = [x]
-
getAnnounceList :: Maybe BVal -> [String]
getAnnounceList Nothing = []
getAnnounceList (Just (Bint _)) = []
_ -> "") l
getAnnounceList (Just (Bdict _)) = []
+
+-- | Info hash is urlencoded 20 byte SHA1 hash of the value of the info key from
+-- the Metainfo file. Note that the value will be a bencoded dictionary, given
+-- the definition of the info key above. TODO: `Metainfo -> ByteString`
+infoHash :: InfoDict -> ByteString
+infoHash m = hash . encode $ (m ! "info")