diff --git a/deluge/common.py b/deluge/common.py index 7411ae995..38d1016a8 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -560,6 +560,20 @@ def is_url(url): return url.partition('://')[0] in ("http", "https", "ftp", "udp") +def is_infohash(infohash): + """ + A check to determine if a string is a valid infohash. + + Args: + infohash (str): The string to check. + + Returns: + bool: True if valid infohash, False otherwise. + + """ + return len(infohash) == 40 and infohash.isalnum() + + def is_magnet(uri): """ A check to determine if a uri is a valid bittorrent magnet uri @@ -616,7 +630,7 @@ def get_magnet_info(uri): except TypeError as ex: log.debug("Invalid base32 magnet hash: %s, %s", xt_hash, ex) break - elif len(xt_hash) == 40: + elif is_infohash(xt_hash): info_hash = xt_hash.lower() else: break diff --git a/deluge/tests/test_common.py b/deluge/tests/test_common.py index 7201a7e4c..4b913e380 100644 --- a/deluge/tests/test_common.py +++ b/deluge/tests/test_common.py @@ -2,8 +2,8 @@ import os from twisted.trial import unittest -from deluge.common import (VersionSplit, fdate, fpcnt, fpeer, fsize, fspeed, ftime, get_path_size, is_ip, is_magnet, - is_url) +from deluge.common import (VersionSplit, fdate, fpcnt, fpeer, fsize, fspeed, ftime, get_path_size, is_infohash, is_ip, + is_magnet, is_url) from deluge.ui.util import lang @@ -48,6 +48,9 @@ class CommonTestCase(unittest.TestCase): def test_is_magnet(self): self.failUnless(is_magnet("magnet:?xt=urn:btih:SU5225URMTUEQLDXQWRB2EQWN6KLTYKN")) + def test_is_infohash(self): + self.failUnless(is_infohash("2dc5d0e71a66fe69649a640d39cb00a259704973")) + def test_get_path_size(self): self.failUnless(get_path_size(os.devnull) == 0) self.failUnless(get_path_size("non-existant.file") == -1) diff --git a/deluge/ui/gtkui/addtorrentdialog.py b/deluge/ui/gtkui/addtorrentdialog.py index d75f0f65b..871e2b791 100644 --- a/deluge/ui/gtkui/addtorrentdialog.py +++ b/deluge/ui/gtkui/addtorrentdialog.py @@ -663,10 +663,16 @@ class AddTorrentDialog(component.Component): dialog.set_default_response(gtk.RESPONSE_OK) dialog.set_transient_for(self.dialog) entry.grab_focus() + + text = (gtk.clipboard_get(selection='PRIMARY').wait_for_text() or + gtk.clipboard_get().wait_for_text()).strip() + if deluge.common.is_infohash(text): + entry.set_text(text) + dialog.show_all() response = dialog.run() infohash = entry.get_text().strip() - if response == gtk.RESPONSE_OK and len(infohash) == 40: + if response == gtk.RESPONSE_OK and deluge.common.is_infohash(infohash): trackers = [] b = textview.get_buffer() lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split("\n")