1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
{-# OPTIONS -fno-warn-orphans #-}
{-# LANGUAGE OverloadedStrings #-}
-- | This module provides torrent metainfo serialization.
module Data.Torrent
( module Data.Torrent.InfoHash
, Torrent(..), TorrentInfo(..), TorrentFile(..)
, fromFile
) where
import Control.Applicative
import Control.Monad
import qualified Data.Map as M
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC (pack, unpack)
import Data.Text (Text)
import Data.BEncode
import Data.Torrent.InfoHash
import Network.URI
type Time = Text
-- TODO comment fields
-- TODO more convenient form of torrent info.
data Torrent = Torrent {
tInfoHash :: InfoHash
, tAnnounce :: URI
, tAnnounceList :: Maybe [[URI]]
, tComment :: Maybe Text
, tCreatedBy :: Maybe ByteString
, tCreationDate :: Maybe Time
, tEncoding :: Maybe ByteString
, tInfo :: TorrentInfo
, tPublisher :: Maybe URI
, tPublisherURL :: Maybe URI
} deriving Show
data TorrentInfo =
SingleFile {
tLength :: Int
, tMD5sum :: Maybe ByteString
, tName :: ByteString
, tPieceLength :: Int
, tPieces :: ByteString -- Vector ByteString?
, tPrivate :: Maybe Bool
}
| MultiFile {
tFiles :: [TorrentFile]
, tName :: ByteString
, tPieceLength :: Int
, tPieces :: ByteString -- Vector ByteString?
, tPrivate :: Maybe Bool
} deriving (Show, Read, Eq)
data TorrentFile = TorrentFile {
tfLength :: Int
, tfMD5sum :: Maybe ByteString
, tfPath :: [ByteString]
} deriving (Show, Read, Eq)
instance BEncodable URI where
toBEncode uri = toBEncode (BC.pack (uriToString id uri ""))
{-# INLINE toBEncode #-}
fromBEncode (BString s) | Just url <- parseURI (BC.unpack s) = return url
fromBEncode b = decodingError $ "url <" ++ show b ++ ">"
{-# INLINE fromBEncode #-}
instance BEncodable Torrent where
toBEncode t = fromAscAssocs
[ "announce" --> tAnnounce t
, "announce-list" -->? tAnnounceList t
, "comment" -->? tComment t
, "created by" -->? tCreatedBy t
, "creation date" -->? tCreationDate t
, "encoding" -->? tEncoding t
, "info" --> tInfo t
, "publisher" -->? tPublisher t
, "publisher-url" -->? tPublisherURL t
]
fromBEncode (BDict d) | Just info <- M.lookup "info" d =
Torrent <$> pure (hashlazy (encode info))
<*> d >-- "announce"
<*> d >--? "announce-list"
<*> d >--? "comment"
<*> d >--? "created by"
<*> d >--? "creation date"
<*> d >--? "encoding"
<*> d >-- "info"
<*> d >--? "publisher"
<*> d >--? "publisher-url"
fromBEncode _ = decodingError "Torrent"
instance BEncodable TorrentInfo where
toBEncode ti@(SingleFile { }) = fromAscAssocs
[ "length" --> tLength ti
, "md5sum" -->? tMD5sum ti
, "name" --> tName ti
, "piece length" --> tPieceLength ti
, "pieces" --> tPieces ti
, "private" -->? tPrivate ti
]
toBEncode ti@(MultiFile {}) = fromAscAssocs
[ "files" --> tFiles ti
, "name" --> tName ti
, "piece length" --> tPieceLength ti
, "pieces" --> tPieces ti
, "private" -->? tPrivate ti
]
fromBEncode (BDict d)
| Just (BList fs) <- M.lookup "files" d =
MultiFile <$> mapM fromBEncode fs
<*> d >-- "name"
<*> d >-- "piece length"
<*> d >-- "pieces"
<*> d >--? "private"
| otherwise =
SingleFile <$> d >-- "length"
<*> d >--? "md5sum"
<*> d >-- "name"
<*> d >-- "piece length"
<*> d >-- "pieces"
<*> d >--? "private"
fromBEncode _ = decodingError "TorrentInfo"
instance BEncodable TorrentFile where
toBEncode tf = fromAssocs
[ "length" --> tfLength tf
, "md5sum" -->? tfMD5sum tf
, "path" --> tfPath tf
]
fromBEncode (BDict d) =
TorrentFile <$> d >-- "length"
<*> d >--? "md5sum"
<*> d >-- "path"
fromBEncode _ = decodingError "TorrentFile"
fromFile :: FilePath -> IO (Result Torrent)
fromFile filepath = (fromBEncode <=< decode) <$> B.readFile filepath
|