1. Martin Tournoij
  2. spamdb-curses

Commits

Martin Tournoij  committed 840c531

Python 3 compat

  • Participants
  • Parent commits 4630e0e
  • Branches default
  • Tags version_1.2

Comments (0)

Files changed (2)

File README.md

View file
  • Ignore whitespace
+Human friendly curses interface for `spamdb(8)` to manage addresses for the
+whitelist/greylist. 
+
+Note: This is for OpenBSD's `spamd(8)`, which has absolutely nothing to do
+with SpamAssasin's `spamd`. 
+
+It's simple, and "just works"™ ... What more is there to say? 
+
+ChangeLog
+=========
+* Version 1.2 - 2012-11-01
+	* Now works with both Python 2 & Python 3
+
+* Version 1.1 - 2011-10-25
+	* Handle reverse DNS lookups better 
+	* Don't crash if the detail window is too large to fit on the screen 
+
+* Version 1.0 - 2011-09-28
+	* Hello, world

File spamdb-curses

View file
  • Ignore whitespace
 #
 # spamdb-curses: A human-friendly interface around spamdb(8)
 #
-# Martin Tournoij <martin@arp242.net>
-# http://code.google.com/p/spamdb-curses/
-# Free for any use. There are no restrictions.
+# Copyright © 2011-2012 Martin Tournoij <martin@arp242.net>
+# MIT License applies: http://opensource.org/licenses/MIT
+# http://code.arp242.net/spamdb-curses/
 #
 
 import curses
 import sys
 import time
 
-try:
-	import thread
-except ImportError:
-	import dummy_thread
+if sys.version_info[0] == 2:
+	try:
+		import thread as _thread
+	except ImportError:
+		import _dummy_thread as dummy_thread
+else:
+	try:
+		import _thread
+	except ImportError:
+		import _dummy_thread
 
 if sys.version_info.major == 2 and sys.version_info.minor < 7:
 	from spamdbcurses import OrderedDict as collections
 	curses.use_default_colors()
 
 	# Don't display cursor
-	curses.curs_set(0)
+	try:
+		curses.curs_set(0)
+	except:
+		pass
 
 	(maxy, maxx) = screen.getmaxyx()
 
 	if maxy < 19 or maxx < 60:
-		print 'Your terminal needs to be at least 19 lines in height'
-		print 'and 60 characters in width.'
+		print('Your terminal needs to be at least 19 lines in height')
+		print('and 60 characters in width.')
 		sys.exit(1)
 
 	_spamdb = GetSpamdb()
 	UpdateCursor(mainwin, 0)
 
 	statuswin = screen.subwin(6, maxx, maxy - 6, 0)
-	thread.start_new_thread(DnsLookups, (statuswin,))
+	_thread.start_new_thread(DnsLookups, (statuswin,))
 
 	# Check if we can write the spamdb and acually make changes
 	# XXX This is not the most reliable manner, for example the spamdb binary
 
 			(maxy, maxx) = screen.getmaxyx()
 			if maxy < 19 or maxx < 60:
-				print 'Your terminal needs to be at least 19 lines in height'
-				print 'and 60 characters in width.'
+				print('Your terminal needs to be at least 19 lines in height')
+				print('and 60 characters in width.')
 				sys.exit(1)
 
 			mainwin = screen.subwin(maxy - 6, maxx, 0, 0)
 				mainwin.refresh()
 				UpdateStatus(statuswin, 'Done!')
 				statuswin.refresh()
-				thread.start_new_thread(DnsLookups, (statuswin,mainwin))
+				_thread.start_new_thread(DnsLookups, (statuswin,mainwin))
 		# Run spamd-setup
 		elif c == ord('s'):
 			if _readonly:
 				else:
 					UpdateStatus(statuswin, 'Starting spamd-setup in the background...')
 					statuswin.refresh()
-					thread.start_new_thread(SpamdSetup, (statuswin,))
+					_thread.start_new_thread(SpamdSetup, (statuswin,))
 
 
 ###
 	# Get longest string
 	keylen = 0
 	vlen = 0
-	for k, v in GetCursor().iteritems():
+	for k, v in GetCursor().items():
 		if len(repr(k)) > keylen:
 			keylen = len(repr(k))
 		if len(repr(v)) > vlen:
 	win.addstr(0, 2, ' DETAILS ', curses.A_BOLD)
 
 	i = 1
-	for k, v in GetCursor().iteritems():
+	for k, v in GetCursor().items():
 		v = v[:vlen]
 		win.addstr(i, 2, ' %s %s' % (k.ljust(keylen + 5), v))
 		i += 1
 	win.border()
 	winsize = win.getmaxyx()
 
-	win.addstr(0, 2, ' ' + _curtype.upper() + ' - spamdb-curses 1.1 ', curses.A_BOLD)
+	win.addstr(0, 2, ' ' + _curtype.upper() + ' - spamdb-curses 1.2 ', curses.A_BOLD)
 	header = '         IP                Expire             From'
 	win.addstr(1, 1, '%s%s' % (header, ' ' * (winsize[1] - len(header) - 2)),
 		curses.A_REVERSE)
 	winsize = win.getmaxyx()
 
 	text = '''
-spamdb-curses 1.1, Martin Tournoij <martin@arp242.net>
-http://code.google.com/p/spamdb-curses/
-Free for any use. There are no restrictions.
+spamdb-curses 1.2, Martin Tournoij <martin@arp242.net>
+http://code.arp242.net/spamdb-curses/
+MIT License applies: http://opensource.org/licenses/MIT
 
 Keybinds:
 	?\t\tThis help
 	backport for Python >= 2.4: http://code.activestate.com/recipes/576693/
 	"""
 
-	proc = subprocess.Popen(['spamdb'], stdout=subprocess.PIPE)
-	output = proc.communicate()[0].split('\n')
+	try:
+		proc = subprocess.Popen(['spamdb'], stdout=subprocess.PIPE)
+	except OSError:
+		print('\n\nError running spamd:\n%s' % sys.exc_info()[1])
+		sys.exit(1)
+	output = proc.communicate()[0].decode().split('\n')
 
 	spamdb = {
 		'white': [],
 
 	try:
 		UpdateStatus(statuswin, 'Looking up reverse DNS in the background...')
-		for t, lst in _spamdb.iteritems():
+		for t, lst in _spamdb.items():
 			i = 0
 			for row in _spamdb[t]:
 				_spamdb[t][i]['DNS lookup'] = ReverseLookup(row['IP'])
 		if mainwin:
 			MainWindow(mainwin)
 
-		thread.exit()
+		_thread.exit()
 	finally:
 		if _locks.get('refresh'):
 			del _locks['refresh']
 		try:
 			e = subprocess.check_call(['spamdb', '-a', GetCursor()['IP']])
 		except subprocess.CalledProcessError:
-			print 'Error calling spamdb -a %s' % GetCursor()['IP']
-			print 'Exit %s' % e
-			print sys.exc_info()[0]
+			print('Error calling spamdb -a %s' % GetCursor()['IP'])
+			print('Exit %s' % e)
+			print(sys.exc_info()[0])
 			sys.exit(1)
 
 		_spamdb['white'].append(GetCursor())
 		try:
 			e = subprocess.check_call(['spamdb', '-d', GetCursor()['IP']])
 		except subprocess.CalledProcessError:
-			print 'Error calling spamdb -d %s' % GetCursor()['IP']
+			print('Error calling spamdb -d %s' % GetCursor()['IP'])
 			try:
-				print 'Exit %s' % e
+				print('Exit %s' % e)
 			except:
 				pass
-			print sys.exc_info()[0]
+			print(sys.exc_info()[0])
 			sys.exit(1)
 
 		del _spamdb[_curtype][_cursel]
 		e = subprocess.check_call(['spamd-setup'])
 		UpdateStatus(statuswin, 'spamd-setup complete')
 		statuswin.refresh()
-		thread.exit()
+		_thread.exit()
 	except subprocess.CalledProcessError:
-		print 'Error calling spamd-setup'
-		print 'Exit %s' % e
-		print sys.exc_info()[0]
+		print('Error calling spamd-setup')
+		print('Exit %s' % e)
+		print(sys.exc_info()[0])
 		sys.exit(1)
 	finally:
 		if _locks.get('spamdsetup'):