3 # (c) 2004 -- Benjamin Mako Hill <mako@bork.hampshire.edu>
5 __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill"
6 __author__ = "Benjamin Mako Hill <mako@debian.org>"
13 # location of the sendmail binary
14 sendmail = "/usr/sbin/sendmail"
16 # list of mimetype which are, for the sake of this program, not
18 ignored_types = ( "applica/pgp-signat", "application/pgp-signature" )
21 mailpipe = os.popen("%s -t" % sendmail, 'w')
22 mailpipe.write( message_string )
23 sys.exit( mailpipe.close() )
25 # get the mail from stdin
26 message_string = sys.stdin.read()
27 message = email.message_from_string( message_string )
29 attachment_expected = False
30 attachment_seen = False
33 for part in message.walk():
35 # check to see if this part is a plain text entry
36 if part.get_content_type() == "text/plain":
38 # if this is the second text block, we should interrate
41 attachment_seen = True
42 # otherwise, mark that we've now seen one text field
46 # search for the word "attach" or "attached" or "attachment"
47 # in the body of the message
48 if re.search( r'\battach(ed|ment)?\b(?im)',
50 attachment_expected = True
52 # check to see if this mime-type is something we can ignore
53 elif ( re.match( r'message/(?m)', part.get_content_type() ) or
54 re.match( r'multipart/(?m)', part.get_content_type() ) or
55 part.get_content_type() in ignored_types ):
58 # if it's not text and it's an ignored type, it's a seen attachment
60 attachment_seen = True
62 # now check to see if we are expecting an attachment
63 if attachment_expected:
65 # if we are expecting an attachment and we've seen one, we should
66 # send the file and be done with things
70 # if we are expected have not seen it, we need to check to see if
71 # the mail has been confirmed
74 # check for the confirmation
76 if re.search( r'Subject: CONFIRM', message_string ):
78 message_string = re.sub( r'(Subject: )(CONFIRM )(.*?)\n',
79 r'\1\3\n', message_string )
83 print >>sys.stderr, "Your message mentions attachments but does not include any."
85 print >>sys.stderr, "Either add an attachment or add \"CONFIRM\" (in ALLCAPS)"
86 print >>sys.stderr, "to beginning of the subject of the message and resend."
88 print >>sys.stderr, "\"CONFIRM\" will be stripped from your message subject before"
89 print >>sys.stderr, "the message is sent."
97 ## answer = raw_input( "No attachment detected.\n"
98 ## "Are you sure you want to send this?"
102 ## # send the mail without the attachment
106 ## # go back and let us reattach the message
107 ## elif answer == "n":
110 ## # no good answer ask again
115 # if we are not expecting anything more, we should send the message