[elinks-users] Adding support for ed2k and torrent links with mldonkey

Grzegorz Adam Hankiewicz gradha at titanium.sabren.com
Fri Aug 12 16:25:04 PDT 2005


Hi.

The attached lua hacks filter ed2k and .torrent links to redirect
them to whatever mldonkey daemon you have on your lan. The console
version is for those sites that don't give you a .torrent url
and just make elinks show the binary data of the torrent. Just run
"torrent" in the lua console when such a torrent is being displayed,
and it will be saved on disk and put on the mldonkey queue.
-------------- next part --------------
Index: hooks.lua.in
===================================================================
RCS file: /var/cvs/elinks/elinks/contrib/lua/hooks.lua.in,v
retrieving revision 1.73
diff -u -p -r1.73 hooks.lua.in
--- hooks.lua.in	20 May 2005 05:18:13 -0000	1.73
+++ hooks.lua.in	12 Aug 2005 23:09:48 -0000
@@ -371,6 +371,48 @@ end
 table.insert(goto_url_hooks, complete_uri_prefix)
 
 
+-- MLDonkey aware functions to catch specific links. Get MLDonkey from
+-- http://savannah.nongnu.org/projects/mldonkey/ or http://mldonkey.berlios.de/
+
+-- Write here the daemon's CGI query URL
+mldonkey_host = "http://127.0.0.1:4080/submit?q="
+
+-- Custom function to substitute some characters of URLs.
+-- PD: I'm sure there a better way to do this, I'm to tired...
+function url_donkey(text)
+    text = string.gsub(text, " ", "%%20")
+    text = string.gsub(text, "%:", "%%3A")
+    text = string.gsub(text, "%/", "%%2F")
+    text = string.gsub(text, "%|", "%%7C")
+    text = string.gsub(text, "%!", "%%21")
+    text = string.gsub(text, "%?", "%%AA")
+    text = string.gsub(text, "%&", "%%26")
+    return string.gsub(text, "%#", "%%23")
+end
+
+-- Detecting ed2k links
+function detect_ed2k_links (url)
+    if string.find(url,'ed2k.*%|file%|') then
+	text = url_donkey(string.gsub(url,
+			  "(.-)ed2k.*file%|([^|]*)%|(%d*)%|(%x*).*",
+			  "dllink ed2k://|file|%2%|%3%|%4%|"))
+	return mldonkey_host..text,true
+    end
+    -- Unmatched
+    return url,nil
+end
+table.insert(follow_url_hooks, detect_ed2k_links)
+
+function detect_bittorrent_links (url)
+    if string.find(url,'.*%.torrent$') then
+	return mldonkey_host.."dllink%20"..url_donkey(url),true
+    end
+    -- Unmatched
+    return url,nil
+end
+table.insert(follow_url_hooks, detect_bittorrent_links)
+
+
 ----------------------------------------------------------------------
 --  pre_format_html_hook
 ----------------------------------------------------------------------
@@ -511,9 +553,28 @@ function mutt ()
     return "run", "mutt -a "..tmp
 end
 
+-- Save torrent contents currently being showed and tell daemon to download it.
+-- This is for those torrent sites where they redirect you thousands of times
+-- and never give you a http:*.torrent url to match against.
+function save_torrent ()
+    -- See if we can obtain the local document.
+    local doc = current_document()
+    if doc then
+        -- Create a temporary file
+        local tmp = tmpname () ..".torrent"
+        -- Write document into the temporary file.
+        writeto (tmp) write (doc) writeto()
+        -- Tell elinks to delete it later.
+        table.insert (tmp_files, tmp)
+        -- Call the mldonkey daemon on the local temporary file.
+        return "goto_url", mldonkey_host.."dllink%20"..url_donkey(tmp)
+    end
+end
+
 -- Table of expressions which are recognised by our lua_console_hook.
 console_hook_functions = {
     reload	= "reload ()",
+    torrent	= save_torrent,
     mutt	= mutt,
 }
 


More information about the elinks-users mailing list