From 1d6984b0ba5e8112ac3fa841efe60bd723ddbf2c Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Sun, 17 Jun 2007 04:59:24 +0000 Subject: [PATCH] adds basic text blocklist support - tarka/steve --- plugins/BlocklistImport/__init__.py | 32 ++++++++++++++---- plugins/BlocklistImport/peerguardian.py | 4 +++ plugins/BlocklistImport/text.py | 43 +++++++++++++++++++++++++ plugins/BlocklistImport/ui.py | 34 +++++++++++++++---- 4 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 plugins/BlocklistImport/text.py diff --git a/plugins/BlocklistImport/__init__.py b/plugins/BlocklistImport/__init__.py index 8a04268f8..ee4ad5060 100644 --- a/plugins/BlocklistImport/__init__.py +++ b/plugins/BlocklistImport/__init__.py @@ -1,9 +1,18 @@ - +## +# Copyright 2007 Steve 'Tarka' Smith (tarka@internode.on.net) +# Distributed under the same terms as Deluge +## plugin_name = "Blocklist Importer" plugin_author = "Steve 'Tarka' Smith" -plugin_version = "0.1" -plugin_description = "Downloads and import PeerGuardian blocklists" +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. +""" def deluge_init(deluge_path): global path @@ -17,8 +26,14 @@ def enable(core, interface): import urllib, deluge.common, deluge.pref from peerguardian import PGReader, PGException +from text import TextReader from ui import GTKConfig, GTKProgress +# List of formats supported. This is used to generate the UI list and +# specify the reader class +readers = {'p2bgz':("PeerGuardian P2B (GZip)", PGReader), + 'text':("PeerGuardian Text", TextReader)} + class BlocklistImport: def __init__(self, path, core, interface): @@ -30,7 +45,7 @@ class BlocklistImport: self.gtkconf = GTKConfig(self) self.gtkprog = GTKProgress(self) - self.blockfile = deluge.common.CONFIG_DIR + "/blocklist.p2b.gzip" + self.blockfile = deluge.common.CONFIG_DIR + "/blocklist.cache" conffile = deluge.common.CONFIG_DIR + "/blocklist.conf" self.config = deluge.pref.Preferences(filename=conffile, @@ -53,6 +68,7 @@ class BlocklistImport: # Attempt initial import if fetch: + print "Fetching",self.config.get('url') self.gtkprog.start_download() filename, headers = urllib.urlretrieve(self.config.get('url'), filename=self.blockfile, @@ -61,7 +77,10 @@ class BlocklistImport: self.gtkprog.start_import() self.core.reset_ip_filter() - reader = PGReader(self.blockfile) + #reader = PGReader(self.blockfile) + ltype = self.config.get('listtype') + print "importing with",ltype + reader = readers[ltype][1](self.blockfile) ips = reader.next() while ips: @@ -77,9 +96,10 @@ class BlocklistImport: def configure(self): self.gtkconf.start() - def setconfig(self, url, load_on_start): + def setconfig(self, url, load_on_start, listtype): self.config.set('url', url) self.config.set('load_on_start', load_on_start) + self.config.set('listtype', listtype) self.config.save() self.loadlist(fetch=True) diff --git a/plugins/BlocklistImport/peerguardian.py b/plugins/BlocklistImport/peerguardian.py index 98d2c70d1..464d0149f 100644 --- a/plugins/BlocklistImport/peerguardian.py +++ b/plugins/BlocklistImport/peerguardian.py @@ -1,3 +1,7 @@ +## +# Copyright 2007 Steve 'Tarka' Smith (tarka@internode.on.net) +# Distributed under the same terms as Deluge +## from exceptions import Exception from struct import unpack diff --git a/plugins/BlocklistImport/text.py b/plugins/BlocklistImport/text.py new file mode 100644 index 000000000..70f6dbc55 --- /dev/null +++ b/plugins/BlocklistImport/text.py @@ -0,0 +1,43 @@ +## +# Copyright 2007 Steve 'Tarka' Smith (tarka@internode.on.net) +# Distributed under the same terms as Deluge +## + + +from exceptions import Exception +import re + +class TextException(Exception): + pass + +class FormatException(TextException): + pass + +# This reads text-formatted block-lists +class TextReader: + + def __init__(self, filename): + print "TextReader loading",filename + self.count = 0 + + # FIXME: Catch or just leave? + self.fd = open(filename, 'r') + + self.re = re.compile(':(\d+\.\d+\.\d+\.\d+)-(\d+\.\d+\.\d+\.\d+)\s*$') + + def next(self): + self.count += 1 + + txt = self.fd.readline() + if txt == "": + return False + + match = self.re.search(txt) + if not match: + raise FormatException("Couldn't match on line %d"%self.count) + + return match.groups() + + def close(self): + self.fd.close() + diff --git a/plugins/BlocklistImport/ui.py b/plugins/BlocklistImport/ui.py index ae2358525..6495aa293 100644 --- a/plugins/BlocklistImport/ui.py +++ b/plugins/BlocklistImport/ui.py @@ -1,5 +1,10 @@ +## +# Copyright 2007 Steve 'Tarka' Smith (tarka@internode.on.net) +# Distributed under the same terms as Deluge +## -import gtk +import gobject, gtk +import BlocklistImport class GTKConfig(gtk.Dialog): def __init__(self, plugin): @@ -16,19 +21,29 @@ class GTKConfig(gtk.Dialog): label = gtk.Label() label.set_markup('Blocklist URL') self.url = gtk.Entry() - self.listtype = gtk.combo_box_new_text() - self.listtype.append_text("PeerGuardian (GZip)") + + ls = gtk.ListStore(gobject.TYPE_STRING, # Long name + gobject.TYPE_STRING) # Short name + for k in BlocklistImport.readers.keys(): + ls.append([BlocklistImport.readers[k][0], k]) + + cell = gtk.CellRendererText() + cell.set_property('xpad', 5) # padding for status text + + self.listtype = gtk.ComboBox(model=ls) + self.listtype.pack_start(cell, False) + self.listtype.add_attribute(cell, 'text', 0) self.listtype.set_active(0) hbox = gtk.HBox(False, 6) hbox.pack_start(label) hbox.pack_start(self.url) - hbox.pack_start(self.listtype) + self.vbox.pack_start(self.listtype) self.vbox.pack_start(hbox) # Load on start - self.load_on_start = gtk.CheckButton("Load on start") + self.load_on_start = gtk.CheckButton("Download on start") self.vbox.pack_start(self.load_on_start) self.connect('response', self.ok) @@ -46,8 +61,13 @@ class GTKConfig(gtk.Dialog): self.cancel(dialog) return - self.plugin.setconfig(self.url.get_text(), - self.load_on_start.get_active()) + ls = self.listtype.get_model() + ltype = ls[self.listtype.get_active()][1] + url = self.url.get_text() + los = self.load_on_start.get_active() + + self.plugin.setconfig(url, los, ltype) + def cancel(self, dialog, response): self.hide_all()