]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Metainfo.hs
Clean up tracker network code
[functorrent.git] / src / FuncTorrent / Metainfo.hs
index 4a95a70a9f980209cd7e5bf9300e0d900cc3a939..a3313d18cbb5c6338de4a38466719af25ad47258 100644 (file)
@@ -1,16 +1,17 @@
 module FuncTorrent.Metainfo
     (Info(..),
      Metainfo(..),
-     mkMetaInfo,
-     mkInfo
+     mkInfo,
+     mkMetaInfo
     ) where
 
 import Prelude hiding (lookup)
 import Data.ByteString.Char8 (ByteString, unpack)
 import Data.Map as M ((!), lookup)
+import Crypto.Hash.SHA1 (hash)
 import Data.Maybe (maybeToList)
 
-import FuncTorrent.Bencode (BVal(..), bstrToString)
+import FuncTorrent.Bencode (BVal(..), encode, bstrToString, bValToInteger)
 
 -- only single file mode supported for the time being.
 data Info = Info { pieceLength :: !Integer
@@ -23,10 +24,11 @@ data Info = Info { pieceLength :: !Integer
 
 data Metainfo = Metainfo { info :: !Info
                          , announceList :: ![String]
-                         , creationDate :: !(Maybe String)
+                         , creationDate :: !(Maybe Integer)
                          , comment :: !(Maybe String)
                          , createdBy :: !(Maybe String)
                          , encoding :: !(Maybe String)
+                         , infoHash :: !ByteString
                          } deriving (Eq, Show)
 
 mkInfo :: BVal -> Maybe Info
@@ -44,27 +46,26 @@ mkInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
                                 , md5sum = md5sum'}
 mkInfo _ = Nothing
 
-maybeBstrToString :: Maybe BVal -> Maybe String
-maybeBstrToString (Just (Bstr bs)) = Just $ unpack bs
-maybeBstrToString _ = Nothing
+mkMetaInfo :: BVal   -> Maybe Metainfo
+mkMetaInfo (Bdict m)  =
+    let (Just info')  = mkInfo $ m ! "info"
+        announce'     = lookup "announce" m
+        announceList' = lookup "announce-list" m
+        creationDate' = lookup "creation date" m
+        comment'      = lookup "comment" m
+        createdBy'    = lookup "created by" m
+        encoding'     = lookup "encoding" m
+    in Just Metainfo {
+             info         = info'
+           , announceList = maybeToList (announce' >>= bstrToString)
+                            ++ getAnnounceList announceList'
+           , creationDate = bValToInteger =<< creationDate'
+           , comment      = bstrToString  =<< comment'
+           , createdBy    = bstrToString  =<< createdBy'
+           , encoding     = bstrToString  =<< encoding'
+           , infoHash     = hash . encode $ (m ! "info")
+        }
 
-mkMetaInfo :: BVal -> Maybe Metainfo
-mkMetaInfo (Bdict m) = let (Just info') = mkInfo $ m ! "info"
-                           announce' = lookup "announce" m
-                           announceList' = lookup "announce-list" m
-                           -- creationDate = lookup (Bstr (pack "creation date")) m
-                           creationDate' = Nothing
-                           comment' = lookup "comment" m
-                           createdBy' = lookup "created by" m
-                           encoding' = lookup "encoding" m
-                       in Just Metainfo { info = info'
-                                        , announceList = maybeToList (announce' >>= bstrToString)
-                                                         ++ getAnnounceList announceList'
-                                        , creationDate = creationDate'
-                                        , comment = maybeBstrToString comment'
-                                        , createdBy = maybeBstrToString createdBy'
-                                        , encoding = maybeBstrToString encoding'
-                                        }
 mkMetaInfo _ = Nothing
 
 getAnnounceList :: Maybe BVal -> [String]