#!/usr/bin/env python
"""
tmda_purgedups.py	2005-01-06

Copyright (c) 2005 'Diesel' Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
Licence: GPL2, Copyright notice may not be altered.

"""

import sys, os

VERSION = '20050106'

class MsgBase:
	MinFrom	= 5
	MinSubject = 5
	def __init__(self):
		self.subdict = {}
		self.frmdict = {}
		self.MinFrom = MsgBase.MinFrom
		self.MinSubject = MsgBase.MinSubject
		self.DupFromMsgs = []
		self.DupSubjectMsgs = []
		self.DupMsgs = []

	def add (self,msg,frm,sub):
		if frm != '':
			if frm not in self.frmdict:
				self.frmdict[frm] = []
			self.frmdict[frm].append(msg)
		if sub != '':
			if sub not in self.subdict:
				self.subdict[sub] = []
			self.subdict[sub].append(msg)
	def read(self,f):
		for line in f:
			msg,frm,sub = line[:-1].split('\t')
			self.add(msg,frm,sub)
	def write(self,f=sys.stdout):
		for msg in self.DupMsgs:
			f.write(msg + '\n')
	def calc (self):
		from sets import Set
		self.calcfrm()
		self.calcsub()
		self.DupMsgs = list(Set(self.DupFromMsgs) | Set(self.DupSubjectMsgs))
		
	def calcfrm (self):
		l = []
		for v in self.frmdict.values():
			if len(v) >= self.MinFrom:
				l += v
		self.DupFromMsgs = l
	def calcsub (self):
		l = []
		for v in self.subdict.values():
			if len(v) >= self.MinSubject:
				l += v
		self.DupSubjectMsgs = l

def getoptions ():
	import optparse
	
	global optd
	usage = 'Usage: %prog [options] file|dir ...'
	version = '%prog '+ VERSION

	parser = optparse.OptionParser(version=version, usage=usage)

	parser.add_option("-v", "--verbose", default=False, action="store_true",
		help='Verbose output enabled.')
 
	parser.add_option("-s", "--summary", default=False, action="store_true",
		help='Print summary at completion to stderr.')

	parser.add_option("-F", "--from-min", default=5, action="store", type='int',
		help='Minmum "From" duplicates required to count.')

	parser.add_option("-S", "--subject-min", default=5, action="store", type='int',
		help='Minmum "Subject" duplicates required to count.')
		
	parser.add_option("-T", "--tmda-delete", default=False, action="store_true",
		help='Call tmda pending directly to delete messages instead of printing them.')
	
	parser.add_option("-B", "--blacklist", default=False, action="store_true",
		help='If deleting directly, also Blacklist message sender.')

	parser.add_option("-p", "--pretend", default=False, action="store_true",
		help='Do not actually delete messages.')

	(options, args) = parser.parse_args()
	optd = options.__dict__
	
	optd['args'] = args


def main ():
	import subprocess
	getoptions()
	mb = MsgBase()
	
	mb.MinFrom = optd['from_min']
	mb.MinSubject = optd['subject_min']

	cmd = 'tmda-pending -b -T'	
	tmdap = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, close_fds=True)

	if optd['verbose']:
		print >> sys.stderr, 'Calculating duplicate pending messages.'
	mb.read(tmdap.stdout)
	mb.calc()

	if optd['tmda_delete']:
		if optd['verbose']:
			print >> sys.stderr, 'Deleting duplicate pending messages.'
		cmd = 'tmda-pending -b '
		if optd['pretend']:
			cmd += '--pretend '
		if optd['blacklist']:
			cmd += '--blacklist '
		cmd += '--delete -'
	
		tmdap = subprocess.Popen(cmd.split(), stdin=subprocess.PIPE, close_fds=optd['verbose'])
	
		mb.write(tmdap.stdin)
		tmdap.stdin.close()
		tmdap.wait()
	else:
		mb.write()

	if optd['summary'] is True:
		print >> sys.stderr, 'Duplicate Froms: %s' % (len(mb.DupFromMsgs))
		print >> sys.stderr,'Duplicate Subjects: %s' % (len(mb.DupSubjectMsgs))
		print >> sys.stderr,'Total Unique Duplicates: %s' % (len(mb.DupMsgs))
	

if __name__ == '__main__':
	main()
