]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Metainfo.hs
Bencode, Metainfo: remove warnings and general cleanup
[functorrent.git] / src / FuncTorrent / Metainfo.hs
1 {-
2  - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
3  -
4  - This file is part of FuncTorrent.
5  -
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.
10  -
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.
15  -
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/>
18  -}
19
20 {-# LANGUAGE OverloadedStrings #-}
21 module FuncTorrent.Metainfo
22     (Info(..),
23      Metainfo(..),
24      torrentToMetainfo
25     ) where
26
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)
33
34 import FuncTorrent.Bencode (BVal(..), encode, decode, bstrToString, bValToInteger)
35
36 data FileMeta = FileMeta { lengthInBytes :: !Integer
37                          , md5sum :: !(Maybe String)
38                          , path :: String
39                          } deriving (Eq, Show)
40
41 data Info = Info { pieceLength :: !Integer
42                  , pieces :: !ByteString
43                  , private :: !(Maybe Integer)
44                  , name :: !String
45                  , filemeta :: [FileMeta]
46                  } deriving (Eq, Show)
47
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
55                          } deriving (Eq, Show)
56
57 bvalToInfo :: BVal -> Maybe Info
58 bvalToInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
59                            (Bstr pieces') = m ! "pieces"
60                            private' = Nothing
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'
66                                                , pieces = pieces'
67                                                , private = private'
68                                                , name = unpack name'
69                                                , filemeta = []
70                                                }
71                        in
72                          case filesIfMulti of
73                            Nothing -> let (Bint length') = m ! "length"
74                                           filemeta' = FileMeta { lengthInBytes = length'
75                                                                , md5sum = Nothing
76                                                                , path = unpack name' }
77                                       in Just (partialInfo { filemeta = [filemeta'] })
78                            Just (Blist files) -> mapM toFileMeta files >>=
79                                                  \filemeta' ->
80                                                    Just partialInfo { filemeta = filemeta' }
81 bvalToInfo _ = Nothing
82
83 toFileMeta :: BVal -> Maybe FileMeta
84 toFileMeta (Bdict fm) = let (Bint length) = fm ! "length"
85                             (Blist pathElems) = fm ! "path"
86                             pathStrings = fmap bstrToString pathElems
87                         in
88                           sequence pathStrings >>=
89                           \pathList -> let path' = concat $ intersperse "/" pathList
90                                        in Just (FileMeta { lengthInBytes = length
91                                                          , md5sum = Nothing
92                                                          , path = path' })
93 toFileMeta _ = Nothing
94
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
104     in Right Metainfo {
105                info         = info'
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")
113              }
114 mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"
115
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''
124                                                               _ -> ""
125                                                _ -> "") l
126 getAnnounceList (Just (Bdict _)) = []
127
128 torrentToMetainfo :: ByteString -> Either String Metainfo
129 torrentToMetainfo s =
130   case decode s of
131    Right d -> mkMetaInfo d
132    Left e -> Left $ show e