3 # (c) 2004 -- Benjamin Mako Hill <mako@bork.hampshire.edu>
6 # * check for an attachment overrides line as an alternative type of option
8 __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill"
9 __author__ = "Benjamin Mako Hill <mako@debian.org>"
13 ### Configuration Options
14 ###########################################
16 # location of the sendmail binary
17 sendmail = "/usr/sbin/sendmail"
19 # list of mimetype which are, for the sake of this program, not
21 ignored_types = ( "applica/pgp-signat", "application/pgp-signature" )
23 # list of regular expressions which we will view as being indicative
25 attachment_regexes = [ r'\battach(ed|ment)?\b(?im)' ]
28 ### No Need To Edit Below This Line
29 ###########################################
37 mailpipe = os.popen("%s -t" % sendmail, 'w')
38 mailpipe.write( message_string )
39 sys.exit( mailpipe.close() )
41 ## SUB: print error message
44 """(Your message mentions attachments but does not include any.
48 (1) Add an attachment or add \"CONFIRM\" (in ALLCAPS to beginning of
49 the subject of the message and resend. \"CONFIRM\" will be
50 stripped from your message subject befor the message is sent.
53 (2) Add a \"X-AttachCheck-Override: Yes\" header the message.
55 Read the documentation for AttachCheck for more details."""
57 # get the mail from stdin
58 message_string = sys.stdin.read()
59 message = email.message_from_string( message_string )
61 attachment_expected = False
62 attachment_seen = False
65 for part in message.walk():
67 # check to see if this part is a plain text entry
68 if part.get_content_type() == "text/plain":
70 # if this is the second text block, we should interrate
73 attachment_seen = True
74 # otherwise, mark that we've now seen one text field
78 # search for the word "attach" or "attached" or "attachment"
79 # in the body of the message
80 for regex in attachment_regexes:
82 if re.search( regex, part.get_payload() ):
83 attachment_expected = True
85 # check to see if this mime-type is something we can ignore
86 elif ( re.match( r'message/(?m)', part.get_content_type() ) or
87 re.match( r'multipart/(?m)', part.get_content_type() ) or
88 part.get_content_type() in ignored_types ):
91 # if it's not text and it's an ignored type, it's a seen attachment
93 attachment_seen = True
95 # now check to see if we are expecting an attachment
96 if attachment_expected:
98 # if we are expecting an attachment and we've seen one, we should
99 # send the file and be done with things
103 # if we are expected have not seen it, we need to check to see if
104 # the mail has been confirmed
107 # check for the confirmation
109 if re.search( r'Subject: CONFIRM', message_string ):
110 message_string = re.sub( r'(Subject: )(CONFIRM )(.*?)\n',
111 r'\1\3\n', message_string )
114 elif message.get( 'X-AttachCheck-Override' ) == "Yes":
122 # if we are not expecting anything more, we should send the message