]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Metainfo.hs
hspec tests for single/multi torrent, bencode etc
[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 DuplicateRecordFields #-}
21
22 module FuncTorrent.Metainfo
23     (Info(..)
24     , Metainfo(..)
25     , DynamicInfo(..)
26     , torrentToMetainfo
27     ) where
28
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)
34
35 import FuncTorrent.Bencode (BVal(..), encode, decode, bstrToString, bValToInteger)
36
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
42                  } deriving (Eq, Show)
43
44 data DynamicInfo = SingleFileInfo { name :: String
45                                   , lengthInBytes :: Integer
46                                   , md5sum :: Maybe String
47                                   }
48                  | MultiFileInfo { dname :: String
49                                  , files :: [FileInfo]
50                                  }
51                  deriving (Eq, Show)
52
53 data FileInfo = FileInfo { lengthInBytes :: Integer
54                          , md5sum :: Maybe String
55                          , path :: String
56                          } deriving (Eq, Show)
57
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
65                          } deriving (Eq, Show)
66
67 mkInfo :: BVal -> Maybe Info
68 mkInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
69                        (Bstr pieces') = m ! "pieces"
70                        private' = Nothing
71                        bdictfiles = lookup "files" m
72                    in
73                      case bdictfiles of
74                        Just fs -> let (Blist files') = fs
75                                       (Bstr name') = m ! "name"
76                                   in
77                                     Just Info { pieceLength = pieceLength'
78                                               , pieces = pieces'
79                                               , private = private'
80                                               , dyninfo = MultiFileInfo {
81                                                   dname = unpack name',
82                                                   files =
83                                                       map (\(Bdict f) ->
84                                                             let (Just len') = bValToInteger (f ! "length")
85                                                                 (Bstr s') = f ! "path"
86                                                             in
87                                                               FileInfo { lengthInBytes = len',
88                                                                          md5sum = Nothing,
89                                                                          path = unpack s'
90                                                                      })
91                                                       files' }
92                                               }
93                        Nothing -> let (Bstr name') = m ! "name"
94                                       (Bint length') = m ! "length"
95                                       md5sum' = Nothing
96                                   in
97                                     Just Info { pieceLength = pieceLength'
98                                               , pieces = pieces'
99                                               , private = private'
100                                               , dyninfo = SingleFileInfo {
101                                                   name = unpack name',
102                                                   lengthInBytes = length',
103                                                   md5sum = md5sum'}
104                                               }
105
106 mkInfo _ = Nothing
107
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
117     in Right Metainfo {
118                info         = info'
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")
126              }
127 mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"
128
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''
137                                                               _ -> ""
138                                                _ -> "") l
139 getAnnounceList (Just (Bdict _)) = []
140
141 torrentToMetainfo :: ByteString -> Either String Metainfo
142 torrentToMetainfo s =
143   case decode s of
144    Right d -> mkMetaInfo d
145    Left e -> Left $ show e