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
|
|
|
|
|
|
|
plugin_name = "Blocklist Importer"
|
|
|
|
plugin_author = "Steve 'Tarka' Smith"
|
2007-06-17 04:59:24 +00:00
|
|
|
plugin_version = "0.3"
|
|
|
|
plugin_description = """
|
|
|
|
Downloads and import PeerGuardian blocklists.
|
|
|
|
|
|
|
|
It can parse uncompressed text-format list, and Gzip P2B version 1 and
|
|
|
|
2. It does not currently support 7zip encoded lists unfortunately.
|
|
|
|
It is suggested these are downloaded an unpacked via a cron script.
|
|
|
|
"""
|
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
|
|
|
|
|
2007-06-17 04:59:24 +00:00
|
|
|
# List of formats supported. This is used to generate the UI list and
|
|
|
|
# specify the reader class
|
|
|
|
readers = {'p2bgz':("PeerGuardian P2B (GZip)", PGReader),
|
2007-06-24 05:45:01 +00:00
|
|
|
'pgtext':("PeerGuardian Text (Uncompressed)", TextReader),
|
2007-06-25 07:53:58 +00:00
|
|
|
'gzmule':("Emule IP list (GZip)", GZMuleReader),
|
|
|
|
'spzip':("SafePeer Text (Zipped)", PGZip)}
|
2007-06-17 04:59:24 +00:00
|
|
|
|
2007-06-12 08:13:16 +00:00
|
|
|
class BlocklistImport:
|
|
|
|
|
|
|
|
def __init__(self, path, core, interface):
|
|
|
|
print "Loading blocklist plugin ..."
|
|
|
|
# Save the path, interface, and core so they can be used later
|
|
|
|
self.path = path
|
|
|
|
self.core = core
|
|
|
|
self.interface = interface
|
|
|
|
self.gtkconf = GTKConfig(self)
|
|
|
|
self.gtkprog = GTKProgress(self)
|
2007-06-17 14:02:17 +00:00
|
|
|
self.cancelled = False
|
2007-06-25 23:31:26 +00:00
|
|
|
self.nimported = 0
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-17 04:59:24 +00:00
|
|
|
self.blockfile = deluge.common.CONFIG_DIR + "/blocklist.cache"
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
conffile = deluge.common.CONFIG_DIR + "/blocklist.conf"
|
|
|
|
self.config = deluge.pref.Preferences(filename=conffile,
|
|
|
|
global_defaults=False)
|
|
|
|
self.config.load()
|
|
|
|
|
|
|
|
if not self.config.has_key('url'):
|
|
|
|
self.configure()
|
|
|
|
else:
|
|
|
|
self.loadlist(fetch=self.config.get('load_on_start'))
|
|
|
|
|
|
|
|
|
|
|
|
def _download_update(self, curr, chunksize, size):
|
|
|
|
incs = float(size) / float(chunksize)
|
|
|
|
self.gtkprog.download_prog(curr/incs)
|
|
|
|
|
|
|
|
def loadlist(self, fetch=False):
|
2007-06-16 05:45:22 +00:00
|
|
|
self.gtkprog.start()
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
# Attempt initial import
|
|
|
|
if fetch:
|
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-24 17:11:41 +00:00
|
|
|
filename, headers = urllib.urlretrieve(self.config.get('url'),
|
2007-06-12 08:13:16 +00:00
|
|
|
filename=self.blockfile,
|
|
|
|
reporthook=self._download_update)
|
2007-06-24 10:32:51 +00:00
|
|
|
except IOError, (errno, strerr):
|
|
|
|
err = ui.GTKError("Couldn't download URL: %s"%strerr)
|
|
|
|
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-24 18:55:47 +00:00
|
|
|
reader = readers[ltype][1](self.blockfile)
|
2007-06-24 10:32:51 +00:00
|
|
|
except IOError, (errno, strerr):
|
|
|
|
err = ui.GTKError("Couldn't open blocklist file: %s"%strerr)
|
|
|
|
self.gtkprog.stop()
|
|
|
|
return
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-24 10:32:51 +00:00
|
|
|
print "Starting import"
|
2007-06-12 08:13:16 +00:00
|
|
|
ips = reader.next()
|
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:
|
|
|
|
self.gtkprog.import_prog(text="Imported %s IPs"%curr)
|
|
|
|
else:
|
2007-06-24 18:55:47 +00:00
|
|
|
self.gtkprog.import_prog()
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-25 07:53:58 +00:00
|
|
|
except FormatException, (ex):
|
|
|
|
err = ui.GTKError("Format error in blocklist: %s"%ex)
|
|
|
|
self.gtkprog.stop()
|
|
|
|
reader.close()
|
|
|
|
return
|
|
|
|
|
|
|
|
|
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-24 10:32:51 +00:00
|
|
|
print "Import complete"
|
2007-06-12 08:13:16 +00:00
|
|
|
|
2007-06-16 05:45:22 +00:00
|
|
|
self.gtkprog.stop()
|
2007-06-12 08:13:16 +00:00
|
|
|
|
|
|
|
def configure(self):
|
|
|
|
self.gtkconf.start()
|
|
|
|
|
2007-06-17 04:59:24 +00:00
|
|
|
def setconfig(self, url, load_on_start, listtype):
|
2007-06-12 08:13:16 +00:00
|
|
|
self.config.set('url', url)
|
|
|
|
self.config.set('load_on_start', load_on_start)
|
2007-06-17 04:59:24 +00:00
|
|
|
self.config.set('listtype', listtype)
|
2007-06-12 08:13:16 +00:00
|
|
|
self.config.save()
|
|
|
|
|
|
|
|
self.loadlist(fetch=True)
|
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
self.core.reset_ip_filter()
|
|
|
|
|
|
|
|
def unload(self):
|
|
|
|
self.core.reset_ip_filter()
|
|
|
|
|
|
|
|
def update(self):
|
2007-06-25 23:31:26 +00:00
|
|
|
msg = "[Blocklist: %s entries]" % self.nimported
|
2007-06-25 23:37:44 +00:00
|
|
|
self.interface.statusbar_temp_msg += ' ' + msg
|
2007-06-25 23:31:26 +00:00
|
|
|
|