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 DuplicateRecordFields #-}
22 module FuncTorrent.Metainfo
29 import Prelude hiding (lookup)
30 import Data.ByteString.Char8 (ByteString, unpack)
31 import Data.Map as M ((!), lookup)
32 import Crypto.Hash.SHA1 (hash)
33 import Data.Maybe (maybeToList)
35 import FuncTorrent.Bencode (BVal(..), encode, decode, bstrToString, bValToInteger)
37 -- only single file mode supported for the time being.
38 data Info = Info { pieceLength :: !Integer
39 , pieces :: !ByteString
40 , private :: !(Maybe Integer)
41 , dyninfo :: !DynamicInfo
44 data DynamicInfo = SingleFileInfo { name :: String
45 , lengthInBytes :: Integer
46 , md5sum :: Maybe String
48 | MultiFileInfo { dname :: String
53 data FileInfo = FileInfo { lengthInBytes :: Integer
54 , md5sum :: Maybe String
58 data Metainfo = Metainfo { info :: !Info
59 , announceList :: ![String]
60 , creationDate :: !(Maybe Integer)
61 , comment :: !(Maybe String)
62 , createdBy :: !(Maybe String)
63 , encoding :: !(Maybe String)
64 , infoHash :: !ByteString
67 mkInfo :: BVal -> Maybe Info
68 mkInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
69 (Bstr pieces') = m ! "pieces"
71 bdictfiles = lookup "files" m
74 Just fs -> let (Blist files') = fs
75 (Bstr name') = m ! "name"
77 Just Info { pieceLength = pieceLength'
80 , dyninfo = MultiFileInfo {
84 let (Just len') = bValToInteger (f ! "length")
85 (Bstr s') = f ! "path"
87 FileInfo { lengthInBytes = len',
93 Nothing -> let (Bstr name') = m ! "name"
94 (Bint length') = m ! "length"
97 Just Info { pieceLength = pieceLength'
100 , dyninfo = SingleFileInfo {
102 lengthInBytes = length',
108 mkMetaInfo :: BVal -> Either String Metainfo
109 mkMetaInfo (Bdict m) =
110 let (Just info') = mkInfo $ m ! "info"
111 announce' = lookup "announce" m
112 announceList' = lookup "announce-list" m
113 creationDate' = lookup "creation date" m
114 comment' = lookup "comment" m
115 createdBy' = lookup "created by" m
116 encoding' = lookup "encoding" m
119 , announceList = maybeToList (announce' >>= bstrToString)
120 ++ getAnnounceList announceList'
121 , creationDate = bValToInteger =<< creationDate'
122 , comment = bstrToString =<< comment'
123 , createdBy = bstrToString =<< createdBy'
124 , encoding = bstrToString =<< encoding'
125 , infoHash = hash . encode $ (m ! "info")
127 mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"
129 getAnnounceList :: Maybe BVal -> [String]
130 getAnnounceList Nothing = []
131 getAnnounceList (Just (Bint _)) = []
132 getAnnounceList (Just (Bstr _)) = []
133 getAnnounceList (Just (Blist l)) = map (\s -> case s of
134 (Bstr s') -> unpack s'
135 (Blist s') -> case s' of
136 [Bstr s''] -> unpack s''
139 getAnnounceList (Just (Bdict _)) = []
141 torrentToMetainfo :: ByteString -> Either String Metainfo
142 torrentToMetainfo s =
144 Right d -> mkMetaInfo d
145 Left e -> Left $ show e