3 # AttachCheck -- A MTA wrapper to help check outgoing email for
4 # forgotten attachments.
6 # (c) 2005 -- Benjamin Mako Hill <mako@bork.hampshire.edu>
7 # Author/Software Homepage at: http://mako.cc
9 # This software comes with ABSOLUTELY NO WARRANTY.
10 # This is free software and is licensed under the GNU GPL.
12 __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill"
13 __author__ = "Benjamin Mako Hill <mako@debian.org>"
15 ### Configuration Options
16 ###########################################
18 # location of the sendmail binary
19 sendmail = "/usr/sbin/sendmail"
21 # list of mimetype which are, for the sake of this program, not
23 ignored_types = ( "applica/pgp-signat", "application/pgp-signature" )
25 # list of regular expressions which we will view as being indicative
27 attachment_regexes = [ r'\battach(ed|ment|ing)?\b(?im)',
28 r'\balleg(o|at[oaie]|ando)' ]
31 ### No Edit Below This Line
32 ###########################################
41 mailpipe = os.popen("%s -t" % sendmail, 'w')
42 mailpipe.write( message_string )
43 sys.exit( mailpipe.close() )
45 ## SUB: print error message
48 """(Your message mentions attachments but does not include any.
52 (1) Add an attachment or add \"CONFIRM\" (in ALLCAPS to beginning of
53 the subject of the message and resend. \"CONFIRM\" will be
54 stripped from your message subject befor the message is sent.
57 (2) Add a \"X-AttachCheck-Override: Yes\" header the message.
59 Read the documentation for AttachCheck for more details."""
61 # get the mail from stdin
62 message_string = sys.stdin.read()
63 message = email.message_from_string( message_string )
65 attachment_expected = False
66 attachment_seen = False
69 for part in message.walk():
71 # check to see if this part is a plain text entry
72 if part.get_content_type() == "text/plain":
74 # if this is the second text block, we should interrate
77 attachment_seen = True
78 # otherwise, mark that we've now seen one text field
82 # search for the word "attach" or "attached" or "attachment"
83 # in the body of the message
84 for regex in attachment_regexes:
86 if re.search( regex, part.get_payload() ):
87 attachment_expected = True
89 # check to see if this mime-type is something we can ignore
90 elif ( re.match( r'message/(?m)', part.get_content_type() ) or
91 re.match( r'multipart/(?m)', part.get_content_type() ) or
92 part.get_content_type() in ignored_types ):
95 # if it's not text and it's an ignored type, it's a seen attachment
97 attachment_seen = True
99 # now check to see if we are expecting an attachment
100 if attachment_expected:
102 # if we are expecting an attachment and we've seen one, we should
103 # send the file and be done with things
107 # if we are expected have not seen it, we need to check to see if
108 # the mail has been confirmed
111 # check for the confirmation
113 if re.search( r'Subject: CONFIRM', message_string ):
114 message_string = re.sub( r'(Subject: )(CONFIRM )(.*?)\n',
115 r'\1\3\n', message_string )
118 elif message.get( 'X-AttachCheck-Override' ) == "Yes":
126 # if we are not expecting anything more, we should send the message