2007-06-17 04:59:24 +00:00
|
|
|
##
|
|
|
|
# Copyright 2007 Steve 'Tarka' Smith (tarka@internode.on.net)
|
|
|
|
# Distributed under the same terms as Deluge
|
|
|
|
##
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-11-22 00:02:06 +00:00
|
|
|
plugin_name = "Blocklist Importer"
|
2008-01-24 08:46:17 +00:00
|
|
|
plugin_author = "Steve 'Tarka' Smith, updated by Mark Stahler"
|
|
|
|
plugin_version = "0.45"
|
2007-07-20 22:08:48 +00:00
|
|
|
plugin_description = _("""
|
2007-06-26 05:58:52 +00:00
|
|
|
Download and import various IP blocklists.
|
2007-06-17 04:59:24 +00:00
|
|
|
|
2007-06-26 05:58:52 +00:00
|
|
|
Currently this plugin can handle PeerGuardian (binary and text),
|
|
|
|
SafePeer and Emule lists. PeerGuardian 7zip format files are not
|
|
|
|
supported. Files may be specified as URLs or locations on the local
|
|
|
|
filesystem.
|
|
|
|
|
|
|
|
A page with pointer to blocklist download sites is available on the
|
|
|
|
wiki:
|
|
|
|
|
|
|
|
http://dev.deluge-torrent.org/wiki/BlocklistPlugin
|
2007-07-20 22:08:48 +00:00
|
|
|
""")
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
def deluge_init(deluge_path):
|
|
|
|
global path
|
|
|
|
path = deluge_path
|
|
|
|
|
|
|
|
def enable(core, interface):
|
|
|
|
global path
|
|
|
|
return BlocklistImport(path, core, interface)
|
|
|
|
|
|
|
|
#################### The plugin itself ####################
|
|
|
|
|
|
|
|
import urllib, deluge.common, deluge.pref
|
|
|
|
from peerguardian import PGReader, PGException
|
2007-06-25 07:53:58 +00:00
|
|
|
from text import TextReader, GZMuleReader, PGZip
|
2007-06-12 08:13:16 +00:00
|
|
|
from ui import GTKConfig, GTKProgress
|
2008-01-24 08:46:17 +00:00
|
|
|
import os.path, os, time
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-17 04:59:24 +00:00
|
|
|
# List of formats supported. This is used to generate the UI list and
|
2007-06-26 04:31:55 +00:00
|
|
|
# specify the reader class. The last entry is for storage by the UI.
|
2007-07-20 22:08:48 +00:00
|
|
|
readers = {'p2bgz':[_("PeerGuardian P2B (GZip)"), PGReader, None],
|
|
|
|
'pgtext':[_("PeerGuardian Text (Uncompressed)"), TextReader, None],
|
|
|
|
'gzmule':[_("Emule IP list (GZip)"), GZMuleReader, None],
|
|
|
|
'spzip':[_("SafePeer Text (Zipped)"), PGZip, None]}
|
2007-06-17 04:59:24 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
class BlocklistImport:
|
|
|
|
|
|
|
|
def __init__(self, path, core, interface):
|
2008-01-06 19:53:47 +00:00
|
|
|
print "Found blocklist plugin ..."
|
2007-06-12 08:13:16 +00:00
|
|
|
# Save the path, interface, and core so they can be used later
|
|
|
|
self.path = path
|
|
|
|
self.core = core
|
|
|
|
self.interface = interface
|
2007-06-26 04:31:55 +00:00
|
|
|
self.cancelled = False
|
2007-06-12 08:13:16 +00:00
|
|
|
self.gtkconf = GTKConfig(self)
|
|
|
|
self.gtkprog = GTKProgress(self)
|
2007-06-25 23:31:26 +00:00
|
|
|
self.nimported = 0
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-12-14 01:19:39 +00:00
|
|
|
self.blockfile = os.path.join(deluge.common.CONFIG_DIR, "blocklist.cache")
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-12-14 01:19:39 +00:00
|
|
|
conffile = os.path.join(deluge.common.CONFIG_DIR, "blocklist.conf")
|
2007-06-12 08:13:16 +00:00
|
|
|
self.config = deluge.pref.Preferences(filename=conffile,
|
|
|
|
global_defaults=False)
|
|
|
|
self.config.load()
|
|
|
|
|
2008-01-28 07:09:33 +00:00
|
|
|
try:
|
|
|
|
load_after_days = self.config.get("load_after_days")
|
|
|
|
except:
|
|
|
|
self.config.set("load_after_days", -1)
|
|
|
|
|
2007-06-28 00:51:14 +00:00
|
|
|
if self.config.has_key('url'):
|
2008-01-24 08:46:17 +00:00
|
|
|
self.loadlist(fetch=self.config.get('load_after_days'))
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _download_update(self, curr, chunksize, size):
|
|
|
|
incs = float(size) / float(chunksize)
|
|
|
|
self.gtkprog.download_prog(curr/incs)
|
|
|
|
|
2008-01-24 08:46:17 +00:00
|
|
|
def loadlist(self, fetch=-1):
|
2007-09-17 03:56:42 +00:00
|
|
|
# Stop all torrents
|
|
|
|
self.paused_or_not = {}
|
|
|
|
for unique_ID in self.core.unique_IDs:
|
|
|
|
self.paused_or_not[unique_ID] = self.core.is_user_paused(unique_ID)
|
|
|
|
if not self.paused_or_not[unique_ID]:
|
|
|
|
self.core.set_user_pause(unique_ID, True, enforce_queue=False)
|
|
|
|
|
2007-06-16 05:45:22 +00:00
|
|
|
self.gtkprog.start()
|
2008-01-24 08:46:17 +00:00
|
|
|
|
|
|
|
# Compare modified time with load_after_days
|
|
|
|
try:
|
|
|
|
liststats = os.stat(self.blockfile)
|
|
|
|
list_timestamp = liststats.st_mtime
|
|
|
|
now_timestamp = time.time()
|
|
|
|
days_update = self.config.get('load_after_days')
|
2008-04-19 15:30:40 +00:00
|
|
|
if days_update == None:
|
|
|
|
days_update = 0
|
2008-01-24 08:46:17 +00:00
|
|
|
|
|
|
|
# Seconds in a day = 86400
|
|
|
|
# Check to see if todays timestamp is older than the list plus days in config
|
|
|
|
|
|
|
|
if now_timestamp >= (list_timestamp + (86400 * days_update)):
|
|
|
|
fetch = -1
|
|
|
|
print 'New Blocklist required...'
|
|
|
|
|
|
|
|
# If blocklist doesnt exist
|
|
|
|
except OSError, e:
|
|
|
|
pass
|
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
# Attempt initial import
|
2008-01-24 08:46:17 +00:00
|
|
|
if fetch == -1:
|
2007-06-17 04:59:24 +00:00
|
|
|
print "Fetching",self.config.get('url')
|
2007-06-16 05:45:22 +00:00
|
|
|
self.gtkprog.start_download()
|
2007-06-24 10:32:51 +00:00
|
|
|
try:
|
2007-06-26 04:31:55 +00:00
|
|
|
filename, headers = urllib.urlretrieve(self.config.get('url'),
|
|
|
|
filename=self.blockfile,
|
|
|
|
reporthook=self._download_update)
|
2007-11-04 22:33:55 +00:00
|
|
|
except IOError, e:
|
|
|
|
err = ui.GTKError(_("Couldn't download URL") + ": %s"%e)
|
2007-06-24 10:32:51 +00:00
|
|
|
self.gtkprog.stop()
|
|
|
|
return
|
2007-06-16 05:45:22 +00:00
|
|
|
|
|
|
|
self.gtkprog.start_import()
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
self.core.reset_ip_filter()
|
2007-06-17 04:59:24 +00:00
|
|
|
ltype = self.config.get('listtype')
|
|
|
|
print "importing with",ltype
|
2007-06-24 10:32:51 +00:00
|
|
|
|
|
|
|
try:
|
2007-06-26 04:31:55 +00:00
|
|
|
reader = readers[ltype][1](self.blockfile)
|
2007-11-04 22:33:55 +00:00
|
|
|
except IOError, e:
|
|
|
|
err = ui.GTKError(_("Couldn't open blocklist file") + ": %s"%e)
|
2007-06-24 10:32:51 +00:00
|
|
|
self.gtkprog.stop()
|
|
|
|
return
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-24 10:32:51 +00:00
|
|
|
print "Starting import"
|
2007-11-04 22:33:55 +00:00
|
|
|
try:
|
|
|
|
ips = reader.next()
|
|
|
|
except:
|
|
|
|
ui.GTKError(_("Wrong file type or corrupted blocklist file."))
|
2007-06-24 05:45:01 +00:00
|
|
|
curr = 0
|
2007-06-25 07:53:58 +00:00
|
|
|
try:
|
|
|
|
while ips and not self.cancelled:
|
|
|
|
self.core.add_range_to_ip_filter(*ips)
|
|
|
|
ips = reader.next()
|
|
|
|
curr += 1
|
|
|
|
if curr % 100 == 0:
|
2007-07-21 20:17:20 +00:00
|
|
|
self.gtkprog.import_prog(text=(_("Imported") + " %s " + _("IPs"))%curr)
|
2007-06-25 07:53:58 +00:00
|
|
|
else:
|
2007-06-24 18:55:47 +00:00
|
|
|
self.gtkprog.import_prog()
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-11-04 22:33:55 +00:00
|
|
|
except:
|
2007-06-25 07:53:58 +00:00
|
|
|
self.gtkprog.stop()
|
|
|
|
reader.close()
|
|
|
|
return
|
|
|
|
|
2007-08-02 18:20:24 +00:00
|
|
|
self.core.set_ip_filter()
|
2007-06-12 08:13:16 +00:00
|
|
|
reader.close()
|
2007-06-25 23:31:26 +00:00
|
|
|
self.nimported = curr
|
2007-06-16 05:45:22 +00:00
|
|
|
self.gtkprog.end_import()
|
2007-06-26 04:31:55 +00:00
|
|
|
print "Import finished"
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-16 05:45:22 +00:00
|
|
|
self.gtkprog.stop()
|
2007-06-26 04:31:55 +00:00
|
|
|
self.cancelled = False
|
2007-09-11 21:55:49 +00:00
|
|
|
#restart torrents that werent paused by us
|
|
|
|
for unique_ID in self.core.unique_IDs:
|
|
|
|
if not self.paused_or_not[unique_ID]:
|
|
|
|
self.core.set_user_pause(unique_ID, False, enforce_queue=False)
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-08-06 00:40:18 +00:00
|
|
|
def configure(self, window):
|
2007-06-26 04:31:55 +00:00
|
|
|
self.gtkconf.start(self.config.get('listtype'),
|
|
|
|
self.config.get('url'),
|
2008-01-24 08:46:17 +00:00
|
|
|
self.config.get('load_after_days'),
|
2007-08-06 00:40:18 +00:00
|
|
|
window)
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2008-01-24 08:46:17 +00:00
|
|
|
def setconfig(self, url, load_after_days, listtype):
|
2007-06-12 08:13:16 +00:00
|
|
|
self.config.set('url', url)
|
2008-01-24 08:46:17 +00:00
|
|
|
self.config.set('load_after_days', load_after_days)
|
2007-06-17 04:59:24 +00:00
|
|
|
self.config.set('listtype', listtype)
|
2007-06-12 08:13:16 +00:00
|
|
|
self.config.save()
|
|
|
|
|
2008-01-24 08:46:17 +00:00
|
|
|
self.loadlist()
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
self.core.reset_ip_filter()
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self.core.reset_ip_filter()
|
|
|
|
|
|
|
|
def update(self):
|
2007-07-21 20:17:20 +00:00
|
|
|
msg = ("[" + _("Blocklist") + ": %s " + _("entries") + "]") % self.nimported
|
2007-06-25 23:37:44 +00:00
|
|
|
self.interface.statusbar_temp_msg += ' ' + msg
|