diff options
-rw-r--r-- | src/Data/Torrent.hs | 79 |
1 files changed, 75 insertions, 4 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 57831acf..83f4094c 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -20,44 +20,113 @@ import Network.URI | |||
20 | 20 | ||
21 | type Time = Text | 21 | type Time = Text |
22 | 22 | ||
23 | -- TODO comment fields | ||
24 | -- TODO more convenient form of torrent info. | 23 | -- TODO more convenient form of torrent info. |
24 | -- | Metainfo about particular torrent. | ||
25 | data Torrent = Torrent { | 25 | data Torrent = Torrent { |
26 | tInfoHash :: InfoHash | 26 | tInfoHash :: InfoHash |
27 | -- ^ SHA1 hash of the 'TorrentInfo' of the 'Torrent'. | ||
28 | |||
27 | , tAnnounce :: URI | 29 | , tAnnounce :: URI |
30 | -- ^ The URL of the tracker. | ||
31 | |||
28 | , tAnnounceList :: Maybe [[URI]] | 32 | , tAnnounceList :: Maybe [[URI]] |
33 | -- ^ Announce list add multiple tracker support. | ||
34 | -- | ||
35 | -- BEP 12: http://www.bittorrent.org/beps/bep_0012.html | ||
36 | |||
29 | , tComment :: Maybe Text | 37 | , tComment :: Maybe Text |
38 | -- ^ Free-form comments of the author. | ||
39 | |||
30 | , tCreatedBy :: Maybe ByteString | 40 | , tCreatedBy :: Maybe ByteString |
41 | -- ^ Name and version of the program used to create the .torrent. | ||
42 | |||
31 | , tCreationDate :: Maybe Time | 43 | , tCreationDate :: Maybe Time |
44 | -- ^ Creation time of the torrent, in standard UNIX epoch. | ||
45 | |||
32 | , tEncoding :: Maybe ByteString | 46 | , tEncoding :: Maybe ByteString |
47 | -- ^ String encoding format used to generate the pieces part of | ||
48 | -- the info dictionary in the .torrent metafile. | ||
49 | |||
50 | -- ^ Info about each content file. | ||
33 | , tInfo :: TorrentInfo | 51 | , tInfo :: TorrentInfo |
52 | |||
53 | |||
34 | , tPublisher :: Maybe URI | 54 | , tPublisher :: Maybe URI |
55 | -- ^ Containing the RSA public key of the publisher of the torrent. | ||
56 | -- Private counterpart of this key that has the authority to allow | ||
57 | -- new peers onto the swarm. | ||
58 | |||
35 | , tPublisherURL :: Maybe URI | 59 | , tPublisherURL :: Maybe URI |
60 | , tSignature :: Maybe ByteString | ||
61 | -- ^ The RSA signature of the info dictionary (specifically, | ||
62 | -- the encrypted SHA-1 hash of the info dictionary). | ||
36 | } deriving Show | 63 | } deriving Show |
37 | 64 | ||
65 | -- | Info part of the .torrent file contain info about each content file. | ||
38 | data TorrentInfo = | 66 | data TorrentInfo = |
39 | SingleFile { | 67 | SingleFile { |
40 | tLength :: Int | 68 | tLength :: Int |
69 | -- ^ Length of the file in bytes. | ||
70 | |||
41 | , tMD5sum :: Maybe ByteString | 71 | , tMD5sum :: Maybe ByteString |
72 | -- | 32 character long MD5 sum of the file. | ||
73 | -- Used by third-party tools, not by bittorrent protocol itself. | ||
74 | |||
42 | , tName :: ByteString | 75 | , tName :: ByteString |
76 | -- ^ Suggested name of the file single file. | ||
77 | |||
78 | |||
43 | 79 | ||
44 | , tPieceLength :: Int | 80 | , tPieceLength :: Int |
45 | , tPieces :: ByteString -- Vector ByteString? | 81 | -- ^ Number of bytes in each piece. |
82 | |||
83 | , tPieces :: ByteString | ||
84 | -- ^ Concatenation of all 20-byte SHA1 hash values. | ||
85 | |||
46 | , tPrivate :: Maybe Bool | 86 | , tPrivate :: Maybe Bool |
87 | -- ^ If set the client MUST publish its presence to get other peers ONLY | ||
88 | -- via the trackers explicity described in the metainfo file. | ||
89 | -- | ||
90 | -- BEP 27: http://www.bittorrent.org/beps/bep_0027.html | ||
47 | } | 91 | } |
92 | |||
48 | | MultiFile { | 93 | | MultiFile { |
49 | tFiles :: [TorrentFile] | 94 | tFiles :: [TorrentFile] |
95 | -- ^ List of the all files that torrent contains. | ||
96 | |||
50 | , tName :: ByteString | 97 | , tName :: ByteString |
98 | -- | The file path of the directory in which to store all the files. | ||
99 | |||
100 | |||
51 | 101 | ||
52 | , tPieceLength :: Int | 102 | , tPieceLength :: Int |
53 | , tPieces :: ByteString -- Vector ByteString? | 103 | |
104 | -- ^ Number of bytes in each piece. | ||
105 | , tPieces :: ByteString | ||
106 | -- ^ Concatenation of all 20-byte SHA1 hash values. | ||
107 | |||
54 | , tPrivate :: Maybe Bool | 108 | , tPrivate :: Maybe Bool |
109 | -- ^ If set the client MUST publish its presence to get other peers ONLY | ||
110 | -- via the trackers explicity described in the metainfo file, | ||
111 | -- | ||
112 | -- BEP 27: http://www.bittorrent.org/beps/bep_0027.html | ||
55 | } deriving (Show, Read, Eq) | 113 | } deriving (Show, Read, Eq) |
56 | 114 | ||
115 | -- | Contain info about one single file. | ||
57 | data TorrentFile = TorrentFile { | 116 | data TorrentFile = TorrentFile { |
58 | tfLength :: Int | 117 | tfLength :: Int |
118 | -- ^ Length of the file in bytes. | ||
119 | |||
59 | , tfMD5sum :: Maybe ByteString | 120 | , tfMD5sum :: Maybe ByteString |
121 | -- | 32 character long MD5 sum of the file. | ||
122 | -- Used by third-party tools, not by bittorrent protocol itself. | ||
123 | |||
60 | , tfPath :: [ByteString] | 124 | , tfPath :: [ByteString] |
125 | -- ^ One or more string elements that together represent the path and | ||
126 | -- filename. Each element in the list corresponds to either a directory | ||
127 | -- name or (in the case of the last element) the filename. | ||
128 | -- For example, the file "dir1/dir2/file.ext" would consist of three | ||
129 | -- string elements ["dir1", "dir2", "file.ext"] | ||
61 | } deriving (Show, Read, Eq) | 130 | } deriving (Show, Read, Eq) |
62 | 131 | ||
63 | instance BEncodable URI where | 132 | instance BEncodable URI where |
@@ -79,10 +148,11 @@ instance BEncodable Torrent where | |||
79 | , "info" --> tInfo t | 148 | , "info" --> tInfo t |
80 | , "publisher" -->? tPublisher t | 149 | , "publisher" -->? tPublisher t |
81 | , "publisher-url" -->? tPublisherURL t | 150 | , "publisher-url" -->? tPublisherURL t |
151 | , "signature" -->? tSignature t | ||
82 | ] | 152 | ] |
83 | 153 | ||
84 | fromBEncode (BDict d) | Just info <- M.lookup "info" d = | 154 | fromBEncode (BDict d) | Just info <- M.lookup "info" d = |
85 | Torrent <$> pure (hashlazy (encode info)) | 155 | Torrent <$> pure (hashlazy (encode info)) -- WARN |
86 | <*> d >-- "announce" | 156 | <*> d >-- "announce" |
87 | <*> d >--? "announce-list" | 157 | <*> d >--? "announce-list" |
88 | <*> d >--? "comment" | 158 | <*> d >--? "comment" |
@@ -92,6 +162,7 @@ instance BEncodable Torrent where | |||
92 | <*> d >-- "info" | 162 | <*> d >-- "info" |
93 | <*> d >--? "publisher" | 163 | <*> d >--? "publisher" |
94 | <*> d >--? "publisher-url" | 164 | <*> d >--? "publisher-url" |
165 | <*> d >--? "singature" | ||
95 | 166 | ||
96 | fromBEncode _ = decodingError "Torrent" | 167 | fromBEncode _ = decodingError "Torrent" |
97 | 168 | ||