[fetchmail]PopDel.py patch

Joshua Crawford mortarn_lists@yahoo.com.au
Tue, 16 Nov 2004 02:39:37 +1100


--XsQoSWH+UP9D9v3l
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

G'day,

Below is a patch for PopDel.py.

I tried sending this patch 4 days ago, before I'd subscribed to the list. I
received an automated message saying it was being held for review by the
moderator. As it hasn't appeared here yet, and 4 days is close enough to
forever in the online world :), perhaps it was lost in a spam filter or
something. My apologies to the moderator if he's just busy or something.

--- /usr/doc/fetchmail-6.2.5/contrib/PopDel.py	2003-07-17 11:03:20.00000000=
0 +1000
+++ PopDel.py	2004-11-12 08:06:52.000000000 +1100
@@ -7,7 +7,18 @@
 # See PopDel.manual for the use of this Python class.
 #
 # created: 01 May 02
+#
 # change log:
+# Joshua Crawford, November 2004:
+# 	Out of range error fixed
+#	Allow for all caps SUBJECT:
+#	Display email address
+#	Don't prompt for save if no changes
+#	Don't clear the screen until we're displaying a menu
+#	Check for invalid choice
+#	Check all arguments exist
+#	Check for errors in POP
+#	Return 1 on errors, 0 otherwise
 # Hacked to support message ranges by ESR, January 2003.
 #
 import os, poplib, string, sys
@@ -24,12 +35,13 @@
 =09
 	def __init__(self):
 		self.done =3D 0
+		self.dirty =3D 0
 		return
=20
 	# get user to choose an element from thing
 	def query(self, thing, prompt):
 		length =3D len(thing)
-		choice =3D [length]
+		choice =3D [length+1]
 		for i in range(0, length):
 			print '(' + `i +  1` + ') ' + thing[i]
 		while filter(lambda x: x > length, choice):
@@ -38,36 +50,68 @@
 				self.done =3D 1
 				choice =3D [-1]
 			else:
-				choice =3D map(int, string.split(choice, "-"))
+				try:
+					choice =3D map(int, string.split(choice, "-"))
+				except:
+					choice =3D [length + 1]
 				if len(choice) > 1:
 					choice =3D range(choice[0], choice[1]+1)
 		return choice
=20
 	def run(self):
 		#log in
-		os.system('clear')
 		print self.HDR
=20
 		subjects =3D []
=20
-		M =3D poplib.POP3(sys.argv[1])
-		M.user(sys.argv[2])
-		M.pass_(sys.argv[3])
-		M.set_debuglevel(1)
-
-		messages =3D M.list()
+		if (len(sys.argv) < 4):
+			print 'Usage: ' + sys.argv[0] + ' pop3.host.name userame password'
+			return 1
+
+		try:
+			M =3D poplib.POP3(sys.argv[1])
+		except:
+			print 'Could not reach ' + sys.argv[1]
+			return 1
+		try:
+			M.user(sys.argv[2])
+		except:
+			print 'Bad username ' + sys.argv[2] + '@' + sys.argv[1]
+			M.quit()
+			return 1
+		try:
+			M.pass_(sys.argv[3])
+		except:
+			print 'Bad password for ' + sys.argv[2] + '@' + sys.argv[1]
+			M.quit()
+			return 1
+#		M.set_debuglevel(1)
+		try:
+			messages =3D M.list()
+		except:
+			print 'Error reading listing for ' + sys.argv[2] + '@' + sys.argv[1]
+			M.quit()
+			return 1
=20
 		list =3D messages[1]
 		if (len(list) =3D=3D 0):
 			M.quit()
-			print '\nNo messages on server.'
+			print '\nNo messages for ' + sys.argv[2] + '@' + sys.argv[1]
 		else:
 			for entry in list:
 				tokens =3D string.split(entry)
-				head =3D M.top(int(tokens[0]), 32)
+				try:
+					head =3D M.top(int(tokens[0]), 32)
+				except:
+					print 'Error issuing TOP command for ' + sys.argv[2] + '@' + sys.argv=
[1]
+					if self.dirty:
+						M.rset()
+					M.quit()
+					return 1
 				for line in head[1]:
-					if (string.find(line, 'Subject:') =3D=3D 0):
+					if (string.find(string.upper(line), 'SUBJECT:') =3D=3D 0):
 						subject =3D string.replace(line, 'Subject:','')
+						subject =3D string.replace(subject, 'SUBJECT:','')
 						subject =3D subject + ' - ' + tokens[1] + ' octets'
 						subjects.append(subject)
 						break
@@ -75,22 +119,33 @@
 			while not self.done:
 				os.system('clear')
 				print self.HDR
-				print '\nMessages on server:'
+				print '\nMessages for ' + sys.argv[2] + '@' + sys.argv[1] + ':'
 				msglist =3D self.query(subjects, self.PROMPT1)
 				print "Choice:", msglist
 				for msg in msglist:
-					if (msg > -1):
-						M.dele(msg+1)
-						subjects[msg] =3D subjects[msg] + ' -X-'
-
-			print '\nExit Options:'
-			choice =3D self.query(self.CHOICES, self.PROMPT2)
-			print "Choice:", choice
-			if (choice =3D=3D [1]):			# commit changes and quit
-				M.quit()
-			else:						# reset and quit
-				M.rset()
+					if (msg > 0):
+						try:
+							M.dele(msg)
+						except:
+							print 'Error deleting message #' + `msg`
+							if self.dirty:
+								M.rset()
+							M.quit()
+							return 1
+						self.dirty =3D 1
+						subjects[msg-1] =3D subjects[msg-1] + ' -X-'
+
+			if not self.dirty:
 				M.quit()
+			else:
+				print '\nExit Options:'
+				choice =3D self.query(self.CHOICES, self.PROMPT2)
+				print "Choice:", choice
+				if (choice =3D=3D [1]):		# commit changes and quit
+					M.quit()
+				else:				# reset and quit
+					M.rset()
+					M.quit()
=20
=20
 		print self.BYE
@@ -99,4 +154,4 @@
=20
 #-----------------main
 obj =3D PopDel()
-obj.run()
+sys.exit(obj.run())

--=20
Joshua 'bruce' Crawford ... http://www.geocities.com/mortarn

"Whenever I hear anyone arguing for slavery, I feel a strong impulse to see
it tried on him personally."
		-- Abraham Lincoln

--XsQoSWH+UP9D9v3l
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBmM24fNeFlNwRVjIRAtNIAJ0Vqugf96gnL8fOgApvlMpwG0SHggCePtxk
WqXf9XKdD7tpFfkeMw9RClM=
=P/sF
-----END PGP SIGNATURE-----

--XsQoSWH+UP9D9v3l--