2007-06-17 04:59:24 +00:00
|
|
|
##
|
|
|
|
# Copyright 2007 Steve 'Tarka' Smith (tarka@internode.on.net)
|
|
|
|
# Distributed under the same terms as Deluge
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
from exceptions import Exception
|
2007-06-25 07:53:58 +00:00
|
|
|
import re, gzip, os
|
2007-06-24 05:45:01 +00:00
|
|
|
from socket import inet_aton
|
|
|
|
from struct import unpack
|
2007-06-25 07:53:58 +00:00
|
|
|
from zipfile import ZipFile
|
2007-11-04 22:33:55 +00:00
|
|
|
import ui
|
2007-06-17 04:59:24 +00:00
|
|
|
|
|
|
|
class TextException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FormatException(TextException):
|
|
|
|
pass
|
|
|
|
|
2007-06-24 05:45:01 +00:00
|
|
|
class TextBase:
|
2007-06-17 04:59:24 +00:00
|
|
|
|
2007-06-24 05:45:01 +00:00
|
|
|
def __init__(self, fd, regexp):
|
|
|
|
print "TextBase loading"
|
2007-06-17 04:59:24 +00:00
|
|
|
self.count = 0
|
2007-06-24 05:45:01 +00:00
|
|
|
self.fd = fd
|
|
|
|
self.re = re.compile(regexp)
|
2007-06-17 04:59:24 +00:00
|
|
|
|
|
|
|
def next(self):
|
|
|
|
self.count += 1
|
|
|
|
|
|
|
|
txt = self.fd.readline()
|
|
|
|
if txt == "":
|
|
|
|
return False
|
|
|
|
|
|
|
|
match = self.re.search(txt)
|
|
|
|
if not match:
|
2007-11-04 22:33:55 +00:00
|
|
|
ui.GTKError(_("Wrong file type or corrupted blocklist file."))
|
2007-06-24 05:45:01 +00:00
|
|
|
|
2007-11-04 22:33:55 +00:00
|
|
|
try:
|
|
|
|
g = match.groups()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
start = ".".join(g[0:4])
|
|
|
|
end = ".".join(g[4:8])
|
2007-06-17 04:59:24 +00:00
|
|
|
|
2007-11-04 22:33:55 +00:00
|
|
|
return (start, end)
|
2007-06-17 04:59:24 +00:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.fd.close()
|
|
|
|
|
2007-06-24 05:45:01 +00:00
|
|
|
|
|
|
|
# This reads PeerGuardian text-formatted block-lists
|
|
|
|
class PGTextReader(TextBase):
|
|
|
|
|
|
|
|
def __init__(self, fd):
|
|
|
|
print "PGTextReader loading"
|
|
|
|
regexp = ':(\d+)\.(\d+)\.(\d+)\.(\d+)-(\d+)\.(\d+)\.(\d+)\.(\d+)\s*$'
|
|
|
|
TextBase.__init__(self, fd, regexp)
|
|
|
|
|
|
|
|
|
|
|
|
# This reads uncompressed PG text list
|
|
|
|
class TextReader(PGTextReader):
|
|
|
|
|
|
|
|
def __init__(self, filename):
|
|
|
|
print "TextReader loading",filename
|
2007-11-04 22:33:55 +00:00
|
|
|
try:
|
|
|
|
PGTextReader.__init__(self, open(filename, 'r'))
|
|
|
|
except:
|
|
|
|
ui.GTKError(_("Wrong file type or corrupted blocklist file."))
|
2007-06-24 05:45:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Reads Emule style blocklists (aka nipfilter)
|
|
|
|
class MuleReader(TextBase):
|
|
|
|
|
|
|
|
def __init__(self, fd):
|
|
|
|
print "MuleReader loading"
|
|
|
|
regexp = '0*(\d+)\.0*(\d+)\.0*(\d+)\.0*(\d+)\s*-\s*0*(\d+)\.0*(\d+)\.0*(\d+)\.0*(\d+)\s*,'
|
|
|
|
TextBase.__init__(self, fd, regexp)
|
|
|
|
|
|
|
|
class GZMuleReader(MuleReader):
|
|
|
|
|
|
|
|
def __init__(self, filename):
|
|
|
|
print "GZMuleReader loading",filename
|
2007-11-04 22:33:55 +00:00
|
|
|
try:
|
|
|
|
MuleReader.__init__(self, gzip.open(filename, "r"))
|
|
|
|
except:
|
|
|
|
ui.GTKError(_("Wrong file type or corrupted blocklist file."))
|
2007-06-24 05:45:01 +00:00
|
|
|
|
|
|
|
|
2007-06-25 07:53:58 +00:00
|
|
|
# Reads zip files from SafePeer style files
|
|
|
|
class PGZip(TextBase):
|
|
|
|
|
|
|
|
def __init__(self, filename):
|
|
|
|
# Open zip and extract first file
|
2007-11-04 22:33:55 +00:00
|
|
|
try:
|
|
|
|
self.zfd = ZipFile(filename, 'r')
|
|
|
|
except:
|
|
|
|
ui.GTKError(_("Wrong file type or corrupted blocklist file."))
|
|
|
|
else:
|
|
|
|
self.files = self.zfd.namelist()
|
|
|
|
self.opennext()
|
2007-06-25 07:53:58 +00:00
|
|
|
|
|
|
|
def opennext(self):
|
|
|
|
self.tmp = os.tmpfile()
|
|
|
|
f = self.files.pop()
|
|
|
|
print "Loading file",f
|
|
|
|
self.tmp.write(self.zfd.read(f))
|
|
|
|
self.tmp.seek(0)
|
|
|
|
self.reader = PGTextReader(self.tmp)
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
try:
|
|
|
|
ret = self.reader.next()
|
|
|
|
if ret == False:
|
|
|
|
# This bit is repeated below and could be moved into a
|
|
|
|
# new procedure. However I'm not clear on how this
|
|
|
|
# would effect tail recursion, so it remains
|
|
|
|
# broken-out for now.
|
|
|
|
if len(self.files) > 0:
|
|
|
|
self.opennext()
|
|
|
|
return self.next()
|
|
|
|
else:
|
|
|
|
# End of zip
|
|
|
|
return False
|
|
|
|
return ret
|
|
|
|
|
2007-11-04 22:33:55 +00:00
|
|
|
except FormatException, e:
|
|
|
|
ui.GTKError(_("Got format exception for zipfile:",e))
|
2007-06-25 07:53:58 +00:00
|
|
|
# Just skip
|
|
|
|
if len(self.files) > 0:
|
|
|
|
self.opennext()
|
|
|
|
return self.next()
|
|
|
|
else:
|
|
|
|
return False
|
2007-11-04 22:33:55 +00:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2007-06-25 07:53:58 +00:00
|
|
|
|
|
|
|
def close(self):
|
2007-11-04 22:33:55 +00:00
|
|
|
try:
|
|
|
|
self.zfd.close()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|