1 # PopDel.py Copyright Richard Harris 2002
3 # author: Richard Harris (rover@emptydog.com)
4 # The author releases this file under the GPL
5 # license. See COPYING for specifics.
7 # See PopDel.manual for the use of this Python class.
10 # last modified: 27 Apr 2006
13 # Matthias Andree, April 2006:
14 # Bump version to 0.1+jc2 and mark Joshua Crawford
15 # as additional author.
16 # Reformat ESR's change log entry
17 # Note: emptydog.com is currently not registered.
18 # Joshua Crawford, April 2006:
19 # Display From: address.
20 # List every email, even if it has no Subject: header.
21 # this also avoids indexing errors (that caused
22 # deleting the wrong message)
23 # Joshua Crawford, November 2004:
24 # Out of range error fixed.
25 # Allow for all caps "SUBJECT:".
26 # Display user and server name in messages.
27 # Don't prompt for save if no changes.
28 # Don't clear the screen until we're displaying a menu.
29 # Check for invalid choice.
30 # Check all arguments exist.
31 # Check for errors in POP.
32 # Return 1 on errors, 0 otherwise.
33 # Eric S. Raymond, January 2003:
34 # Hacked to support message ranges.
36 import os, poplib, re, string, sys
39 HDR = "\nPopDel - Delete messages from popmail - Ver. 0.1+jc2"
40 BYE = "\n PopDel Ver.0.1+jc2 by Richard Harris and Joshua Crawford\n" +\
41 # " site - http://emptydog.com/\n" +\
42 " email - Richard Harris <rover@emptydog.com>\n" +\
43 " email - Joshua Crawford <jgcrawford@gmail.com>\n"
44 PROMPT1 = "Choose message number to delete or 'q' to quit: "
45 PROMPT2 = "Quit or abort: "
46 CHOICES = ["Save changes and quit.",
47 "Abort and make no deletions."]
54 # get user to choose an element from thing
55 def query(self, thing, prompt):
58 for i in range(0, length):
59 print '(' + `i + 1` + ') ' + thing[i]
60 while filter(lambda x: x > length, choice):
61 choice = raw_input(prompt)
67 choice = map(int, string.split(choice, "-"))
71 choice = range(choice[0], choice[1]+1)
80 if (len(sys.argv) < 4):
81 print 'Usage: ' + sys.argv[0] + ' pop3.host.name username password'
85 M = poplib.POP3(sys.argv[1])
87 print 'Could not reach ' + sys.argv[1]
92 print 'Bad username ' + sys.argv[2] + '@' + sys.argv[1]
98 print 'Bad password for ' + sys.argv[2] + '@' + sys.argv[1]
101 # M.set_debuglevel(1)
105 print 'Error reading listing for ' + sys.argv[2] + '@' + sys.argv[1]
112 print '\nNo messages for ' + sys.argv[2] + '@' + sys.argv[1]
115 tokens = string.split(entry)
116 subject = '(no subject)'
117 address = '(no address)'
119 head = M.top(int(tokens[0]), 32)
121 print 'Error issuing TOP command for ' + sys.argv[2] + '@' + sys.argv[1]
127 if (string.find(string.upper(line), 'SUBJECT:') == 0):
129 if (string.find(string.upper(line), 'FROM:') == 0):
131 result = re.search(r'([^\t <>]*@[^\t <>]*)', line)
133 address = result.expand(r'\1')
134 subj = address[:40] + ' [' + tokens[1] + 'o] ' + subject
135 subjects.append(subj)
140 print '\nMessages for ' + sys.argv[2] + '@' + sys.argv[1] + ':'
141 msglist = self.query(subjects, self.PROMPT1)
142 print "Choice:", msglist
148 print 'Error deleting message #' + `msg`
154 subjects[msg-1] = subjects[msg-1] + ' -X-'
159 print '\nExit Options:'
160 choice = self.query(self.CHOICES, self.PROMPT2)
161 print "Choice:", choice
162 if (choice == [1]): # commit changes and quit
164 else: # reset and quit
171 #-----------------main