# -*- Mode: Python; tab-width: 4 -*-

# An automatically-generated list of spammers
# (compiled via spam-list@spam-archive.org)
# is available from:
#   ftp://ftp.hiss.org/pub/sendmail/filter.cf.data

import regex
import rfc822
import string

def strip_eol (line):
	while line and line[-1] in "\r\n":
		line = line[:-1]
	return line

class blacklist_filter:
	def __init__ (self, filename):
		d = {}
		f = open (filename, 'r')
		print 'reading blacklist...'
		while 1:
			line = f.readline()
			if not line:
				break
			elif line[0] == '#':
				continue
			else:
				line = strip_eol (line)
				d[line] = 0
		print 'done.'
		self.bad_guys = d

	def scan (self, headers):
		for h in headers:
			if h[:6] == 'From: ':
				name, addr = rfc822.parseaddr (h[6:])
				if self.bad_guys.has_key (addr):
					self.bad_guys[addr] = self.bad_guys[addr] + 1
					return 1
		return 0

	def report (self, channel):
		list = map (lambda x: (x[1],x[0]), self.bad_guys.items())
		list.sort()
		ll = len(list)
		for i in range(10):
			elem = list[ll-i-1]
			print elem
			channel.push (
				' <%s>: %d hits\r\n' % (
					elem[1],
					elem[0]
					)
				)
