@@ -73,7 +73,7 @@ class node. Any parts of other types are each stored in separate files
7373an exception, the original message is bounced back to the sender with the
7474explanatory message given in the exception.
7575
76- $Id: mailgw.py,v 1.86 2002-09-10 12:44:42 richard Exp $
76+ $Id: mailgw.py,v 1.87 2002-09-11 01:19:16 richard Exp $
7777'''
7878
7979import string , re , os , mimetools , cStringIO , smtplib , socket , binascii , quopri
@@ -143,6 +143,79 @@ def __init__(self, instance, db):
143143 # (for testing)
144144 self .trapExceptions = 1
145145
146+ def do_pipe (self ):
147+ ''' Read a message from standard input and pass it to the mail handler.
148+ '''
149+ self .main (sys .stdin )
150+ return 0
151+
152+ def do_mailbox (self , filename ):
153+ ''' Read a series of messages from the specified unix mailbox file and
154+ pass each to the mail handler.
155+ '''
156+ # open the spool file and lock it
157+ import fcntl , FCNTL
158+ f = open (filename , 'r+' )
159+ fcntl .flock (f .fileno (), FCNTL .LOCK_EX )
160+
161+ # handle and clear the mailbox
162+ try :
163+ from mailbox import UnixMailbox
164+ mailbox = UnixMailbox (f , factory = Message )
165+ # grab one message
166+ message = mailbox .next ()
167+ while message :
168+ # handle this message
169+ self .handle_Message (message )
170+ message = mailbox .next ()
171+ # nuke the file contents
172+ os .ftruncate (f .fileno (), 0 )
173+ except :
174+ import traceback
175+ traceback .print_exc ()
176+ return 1
177+ fcntl .flock (f .fileno (), FCNTL .LOCK_UN )
178+ return 0
179+
180+ def do_pop (self , server , user = '' , password = '' ):
181+ '''Read a series of messages from the specified POP server.
182+ '''
183+ import getpass , poplib , socket
184+ try :
185+ if not user :
186+ user = raw_input (_ ('User: ' ))
187+ if not password :
188+ password = getpass .getpass ()
189+ except (KeyboardInterrupt , EOFError ):
190+ # Ctrl C or D maybe also Ctrl Z under Windows.
191+ print "\n Aborted by user."
192+ return 1
193+
194+ # open a connection to the server and retrieve all messages
195+ try :
196+ server = poplib .POP3 (server )
197+ except socket .error , message :
198+ print "POP server error:" , message
199+ return 1
200+ server .user (user )
201+ server .pass_ (password )
202+ numMessages = len (server .list ()[1 ])
203+ for i in range (1 , numMessages + 1 ):
204+ # retr: returns
205+ # [ pop response e.g. '+OK 459 octets',
206+ # [ array of message lines ],
207+ # number of octets ]
208+ lines = server .retr (i )[1 ]
209+ s = cStringIO .StringIO ('\n ' .join (lines ))
210+ s .seek (0 )
211+ self .handle_Message (Message (s ))
212+ # delete the message
213+ server .dele (i )
214+
215+ # quit the server to commit changes.
216+ server .quit ()
217+ return 0
218+
146219 def main (self , fp ):
147220 ''' fp - the file from which to read the Message.
148221 '''
@@ -795,9 +868,13 @@ def parseContent(content, keep_citations, keep_body,
795868 signature = re .compile (r'^[>|\s]*[-_]+\s*$' ),
796869 original_message = re .compile (r'^[>|\s]*-----Original Message-----$' )):
797870 ''' The message body is divided into sections by blank lines.
798- Sections where the second and all subsequent lines begin with a ">" or "|"
799- character are considered "quoting sections". The first line of the first
800- non-quoting section becomes the summary of the message.
871+ Sections where the second and all subsequent lines begin with a ">"
872+ or "|" character are considered "quoting sections". The first line of
873+ the first non-quoting section becomes the summary of the message.
874+
875+ If keep_citations is true, then we keep the "quoting sections" in the
876+ content.
877+ If keep_body is true, we even keep the signature sections.
801878 '''
802879 # strip off leading carriage-returns / newlines
803880 i = 0
@@ -850,10 +927,12 @@ def parseContent(content, keep_citations, keep_body,
850927
851928 # and add the section to the output
852929 l .append (section )
853- # we only set content for those who want to delete cruft from the
854- # message body, otherwise the body is left untouched.
930+
931+ # Now reconstitute the message content minus the bits we don't care
932+ # about.
855933 if not keep_body :
856934 content = '\n \n ' .join (l )
935+
857936 return summary , content
858937
859938# vim: set filetype=python ts=4 sw=4 et si
0 commit comments