From fa2428e1c93e5795d60693f7c2734f924b8df80f Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sun, 16 Jul 2017 16:37:04 +0530
Subject: [PATCH] MagnetURI: New module to handle magnet uris

Magnet URIs have much less information than the .torrent files.
The client talk via extension protocols to the peers to get the
info dict.

Just the shell for the module. Nothing in there yet.
---
 functorrent.cabal            |  1 +
 src/FuncTorrent/MagnetURI.hs | 43 ++++++++++++++++++++++++++++++++++++
 src/main/Main.hs             |  5 +++--
 stack.yaml                   | 37 -------------------------------
 4 files changed, 47 insertions(+), 39 deletions(-)
 create mode 100644 src/FuncTorrent/MagnetURI.hs
 delete mode 100644 stack.yaml

diff --git a/functorrent.cabal b/functorrent.cabal
index 3de1ebf..cec4735 100644
--- a/functorrent.cabal
+++ b/functorrent.cabal
@@ -29,6 +29,7 @@ library
                        FuncTorrent.Tracker.Http,
                        FuncTorrent.Tracker.Types,
                        FuncTorrent.Tracker.Udp,
+                       FuncTorrent.MagnetURI,
                        FuncTorrent.Utils
 
   other-extensions:    OverloadedStrings
diff --git a/src/FuncTorrent/MagnetURI.hs b/src/FuncTorrent/MagnetURI.hs
new file mode 100644
index 0000000..4c695b5
--- /dev/null
+++ b/src/FuncTorrent/MagnetURI.hs
@@ -0,0 +1,43 @@
+{-
+Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
+
+This file is part of FuncTorrent.
+
+FuncTorrent is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+FuncTorrent is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with FuncTorrent; if not,  see <http://www.gnu.org/licenses/>
+-}
+
+-- | Parse magnet URI
+-- The spec for magnetURI: https://en.wikipedia.org/wiki/Magnet_URI_scheme
+-- An example from tpb:
+--   magnet:?xt=urn:btih:1f8a4ee3c3f57e81f8f0b4e658177201fc2a3118&dn=Honey+Bee+2+%5B2017%5D+Malayalam+DVDRiP+x264+AAC+700MB+ZippyMovieZ+E&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Fzer0day.ch%3A1337&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969
+-- xt   - extra topic - urn containing filehash
+-- btih - bittorrent infohash
+-- dn   - display name (for the user)
+-- tr   - tracker URL
+-- xl   - exact length
+-- mt   - link to a meta file (manifest topic) that contains a list of magnet links
+-- urn  - uniform resource name
+
+-- How does a client join the swarm with only the magnet uri?
+-- This is detailed in http://www.bittorrent.org/beps/bep_0009.html
+-- The protocol depends on the extensions protocol specified in BEP 0010.
+-- http://www.bittorrent.org/beps/bep_0009.html
+
+-- 1. First we parse the magnet uri and get a list of trackers
+-- 2. We then use the usual tracker protocol to get a list of peers.
+-- 3. Then we talk to the peers to create the metadata via the BEP 0009 protocol
+
+module FuncTorrent.MagnetURI where
+
+
diff --git a/src/main/Main.hs b/src/main/Main.hs
index 8fccebe..c5acb40 100644
--- a/src/main/Main.hs
+++ b/src/main/Main.hs
@@ -23,20 +23,21 @@ module Main where
 import           Prelude hiding (log, length, readFile, getContents)
 
 import           Control.Concurrent (forkIO, killThread)
-import           Control.Concurrent.MVar (readMVar)
+
 import           Data.ByteString.Char8 (ByteString, getContents, readFile)
 import qualified FuncTorrent.FileSystem as FS (createMsgChannel, pieceMapFromFile, run)
 import           FuncTorrent.Logger (initLogger, logMessage, logStop)
+import           FuncTorrent.MagnetURI
 import           FuncTorrent.Metainfo (Info(..), Metainfo(..), torrentToMetainfo)
 import           FuncTorrent.Peer (handlePeerMsgs)
 import           FuncTorrent.PieceManager (initPieceMap)
 import qualified FuncTorrent.Server as Server
 import           FuncTorrent.Tracker (runTracker, getConnectedPeers, newTracker)
 import           Network (PortID (PortNumber))
-import           System.IO (withFile, IOMode (ReadWriteMode))
 import           System.Directory (doesFileExist)
 import           System.Environment (getArgs)
 import           System.Exit (exitSuccess)
+import           System.IO (withFile, IOMode (ReadWriteMode))
 import           System.Random (getStdGen, randomRs)
 
 logError :: String -> (String -> IO ()) -> IO ()
diff --git a/stack.yaml b/stack.yaml
deleted file mode 100644
index 5b69a9a..0000000
--- a/stack.yaml
+++ /dev/null
@@ -1,37 +0,0 @@
-# For more information, see: https://github.com/commercialhaskell/stack/blob/release/doc/yaml_configuration.md
-
-# Override default flag values for local packages and extra-deps
-flags: {}
-
-# Local packages, usually specified by relative directory name
-packages:
-
-# Local packages, usually specified by relative directory name
-- '.'
-
-# Packages to be pulled from upstream that are not in the resolver (e.g., acme-missiles-0.3)
-extra-deps: []
-
-# Specifies the GHC version and set of packages available (e.g., lts-3.5, nightly-2015-09-21, ghc-7.10.2)
-resolver: nightly-2016-06-19
-
-rebuild-ghc-options: true
-ghc-options:
-    # All packages
-    "*": -Wall # -fwarn-incomplete-uni-patterns -fwarn-incomplete-record-updates -fwarn-monomorphism-restriction -fwarn-auto-orphans -fwarn-implicit-prelude -fwarn-missing-local-sigs -fwarn-missing-exported-sigs -fwarn-missing-import-lists -fwarn-identities
-    some-package: -DSOME_CPP_FLAG
-
-# Control whether we use the GHC we find on the path
-# system-ghc: true
-
-# Require a specific version of stack, using version ranges
-# require-stack-version: -any # Default
-# require-stack-version: >= 1.0.0
-
-# Override the architecture used by stack, especially useful on Windows
-# arch: i386
-# arch: x86_64
-
-# Extra directories used by stack for building
-# extra-include-dirs: [/path/to/dir]
-# extra-lib-dirs: [/path/to/dir]
-- 
2.45.2