2 - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
4 - This file is part of FuncTorrent.
6 - FuncTorrent is free software; you can redistribute it and/or modify
7 - it under the terms of the GNU General Public License as published by
8 - the Free Software Foundation; either version 3 of the License, or
9 - (at your option) any later version.
11 - FuncTorrent is distributed in the hope that it will be useful,
12 - but WITHOUT ANY WARRANTY; without even the implied warranty of
13 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 - GNU General Public License for more details.
16 - You should have received a copy of the GNU General Public License
17 - along with FuncTorrent; if not, see <http://www.gnu.org/licenses/>
20 {-# LANGUAGE OverloadedStrings #-}
21 module FuncTorrent.Metainfo
27 import Prelude hiding (lookup)
28 import Data.ByteString.Char8 (ByteString, unpack)
29 import Data.Map as M ((!), lookup)
30 import Data.List (intersperse)
31 import Crypto.Hash.SHA1 (hash)
32 import Data.Maybe (maybeToList)
34 import FuncTorrent.Bencode (BVal(..), encode, decode, bstrToString, bValToInteger)
36 data FileMeta = FileMeta { lengthInBytes :: !Integer
37 , md5sum :: !(Maybe String)
41 data Info = Info { pieceLength :: !Integer
42 , pieces :: !ByteString
43 , private :: !(Maybe Integer)
45 , filemeta :: [FileMeta]
48 data Metainfo = Metainfo { info :: !(Maybe Info)
49 , announceList :: ![String]
50 , creationDate :: !(Maybe Integer)
51 , comment :: !(Maybe String)
52 , createdBy :: !(Maybe String)
53 , encoding :: !(Maybe String)
54 , infoHash :: !ByteString
57 bvalToInfo :: BVal -> Maybe Info
58 bvalToInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
59 (Bstr pieces') = m ! "pieces"
61 (Bstr name') = m ! "name"
62 -- is the key "files" present? If so, it is a multi-file torrent
63 -- if not, it is a single file torrent.
64 filesIfMulti = lookup "files" m
65 partialInfo = Info { pieceLength = pieceLength'
73 Nothing -> let (Bint length') = m ! "length"
74 filemeta' = FileMeta { lengthInBytes = length'
76 , path = unpack name' }
77 in Just (partialInfo { filemeta = [filemeta'] })
78 Just (Blist files) -> mapM toFileMeta files >>=
80 Just partialInfo { filemeta = filemeta' }
81 bvalToInfo _ = Nothing
83 toFileMeta :: BVal -> Maybe FileMeta
84 toFileMeta (Bdict fm) = let (Bint length) = fm ! "length"
85 (Blist pathElems) = fm ! "path"
86 pathStrings = fmap bstrToString pathElems
88 sequence pathStrings >>=
89 \pathList -> let path' = concat $ intersperse "/" pathList
90 in Just (FileMeta { lengthInBytes = length
93 toFileMeta _ = Nothing
95 mkMetaInfo :: BVal -> Either String Metainfo
96 mkMetaInfo (Bdict m) =
97 let info' = bvalToInfo $ m ! "info"
98 announce' = lookup "announce" m
99 announceList' = lookup "announce-list" m
100 creationDate' = lookup "creation date" m
101 comment' = lookup "comment" m
102 createdBy' = lookup "created by" m
103 encoding' = lookup "encoding" m
106 , announceList = maybeToList (announce' >>= bstrToString)
107 ++ getAnnounceList announceList'
108 , creationDate = bValToInteger =<< creationDate'
109 , comment = bstrToString =<< comment'
110 , createdBy = bstrToString =<< createdBy'
111 , encoding = bstrToString =<< encoding'
112 , infoHash = hash . encode $ (m ! "info")
114 mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"
116 getAnnounceList :: Maybe BVal -> [String]
117 getAnnounceList Nothing = []
118 getAnnounceList (Just (Bint _)) = []
119 getAnnounceList (Just (Bstr _)) = []
120 getAnnounceList (Just (Blist l)) = map (\s -> case s of
121 (Bstr s') -> unpack s'
122 (Blist s') -> case s' of
123 [Bstr s''] -> unpack s''
126 getAnnounceList (Just (Bdict _)) = []
128 torrentToMetainfo :: ByteString -> Either String Metainfo
129 torrentToMetainfo s =
131 Right d -> mkMetaInfo d
132 Left e -> Left $ show e