adds basic text blocklist support - tarka/steve

This commit is contained in:
Marcos Pinto 2007-06-17 04:59:24 +00:00
parent 82d2fb08c0
commit 1d6984b0ba
4 changed files with 100 additions and 13 deletions

View File

@ -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_name = "Blocklist Importer"
plugin_author = "Steve 'Tarka' Smith" plugin_author = "Steve 'Tarka' Smith"
plugin_version = "0.1" plugin_version = "0.3"
plugin_description = "Downloads and import PeerGuardian blocklists" 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): def deluge_init(deluge_path):
global path global path
@ -17,8 +26,14 @@ def enable(core, interface):
import urllib, deluge.common, deluge.pref import urllib, deluge.common, deluge.pref
from peerguardian import PGReader, PGException from peerguardian import PGReader, PGException
from text import TextReader
from ui import GTKConfig, GTKProgress 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: class BlocklistImport:
def __init__(self, path, core, interface): def __init__(self, path, core, interface):
@ -30,7 +45,7 @@ class BlocklistImport:
self.gtkconf = GTKConfig(self) self.gtkconf = GTKConfig(self)
self.gtkprog = GTKProgress(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" conffile = deluge.common.CONFIG_DIR + "/blocklist.conf"
self.config = deluge.pref.Preferences(filename=conffile, self.config = deluge.pref.Preferences(filename=conffile,
@ -53,6 +68,7 @@ class BlocklistImport:
# Attempt initial import # Attempt initial import
if fetch: if fetch:
print "Fetching",self.config.get('url')
self.gtkprog.start_download() self.gtkprog.start_download()
filename, headers = urllib.urlretrieve(self.config.get('url'), filename, headers = urllib.urlretrieve(self.config.get('url'),
filename=self.blockfile, filename=self.blockfile,
@ -61,7 +77,10 @@ class BlocklistImport:
self.gtkprog.start_import() self.gtkprog.start_import()
self.core.reset_ip_filter() 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() ips = reader.next()
while ips: while ips:
@ -77,9 +96,10 @@ class BlocklistImport:
def configure(self): def configure(self):
self.gtkconf.start() 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('url', url)
self.config.set('load_on_start', load_on_start) self.config.set('load_on_start', load_on_start)
self.config.set('listtype', listtype)
self.config.save() self.config.save()
self.loadlist(fetch=True) self.loadlist(fetch=True)

View File

@ -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 exceptions import Exception
from struct import unpack from struct import unpack

View File

@ -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()

View File

@ -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): class GTKConfig(gtk.Dialog):
def __init__(self, plugin): def __init__(self, plugin):
@ -16,19 +21,29 @@ class GTKConfig(gtk.Dialog):
label = gtk.Label() label = gtk.Label()
label.set_markup('<b>Blocklist URL</b>') label.set_markup('<b>Blocklist URL</b>')
self.url = gtk.Entry() 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) self.listtype.set_active(0)
hbox = gtk.HBox(False, 6) hbox = gtk.HBox(False, 6)
hbox.pack_start(label) hbox.pack_start(label)
hbox.pack_start(self.url) hbox.pack_start(self.url)
hbox.pack_start(self.listtype)
self.vbox.pack_start(self.listtype)
self.vbox.pack_start(hbox) self.vbox.pack_start(hbox)
# Load on start # 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.vbox.pack_start(self.load_on_start)
self.connect('response', self.ok) self.connect('response', self.ok)
@ -46,8 +61,13 @@ class GTKConfig(gtk.Dialog):
self.cancel(dialog) self.cancel(dialog)
return return
self.plugin.setconfig(self.url.get_text(), ls = self.listtype.get_model()
self.load_on_start.get_active()) 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): def cancel(self, dialog, response):
self.hide_all() self.hide_all()