cc3785e8a436f33b588f1a195927873b97eab2cd
[attachcheck] / attachment_checker
1 #!/usr/bin/env python
2
3 # (c) 2004 -- Benjamin Mako Hill <mako@bork.hampshire.edu>
4
5 __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill"
6 __author__ = "Benjamin Mako Hill <mako@debian.org>" 
7
8 import sys
9 import os
10 import re
11 import email
12
13 # location of the sendmail binary
14 sendmail = "/usr/sbin/sendmail"
15
16 # list of mimetype which are, for the sake of this program, not
17 # attachments
18 ignored_types = ( "applica/pgp-signat", "application/pgp-signature" ) 
19
20 def send_message(): 
21     mailpipe = os.popen("%s -t" % sendmail, 'w')
22     mailpipe.write( message_string )
23     sys.exit( mailpipe.close() )
24
25 # get the mail from stdin
26 message_string = sys.stdin.read()
27 message = email.message_from_string( message_string )
28
29 attachment_expected = False
30 attachment_seen = False
31 text_seen = False
32
33 for part in message.walk():
34
35     # check to see if this part is a plain text entry
36     if part.get_content_type() == "text/plain":
37
38         # if this is the second text block, we should interrate
39         # through things
40         if text_seen:
41             attachment_seen = True
42         # otherwise, mark that we've now seen one text field
43         else:
44             text_seen = True
45
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)',
49                       part.get_payload() ):
50             attachment_expected = True
51
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 ):
56         continue
57
58     # if it's not text and it's an ignored type, it's a seen attachment
59     else:
60         attachment_seen = True
61
62 # now check to see if we are expecting an attachment
63 if attachment_expected:
64
65     # if we are expecting an attachment and we've seen one, we should
66     # send the file and be done with things
67     if attachment_seen:
68         send_message()
69
70     # if we are expected have not seen it, we need to check to see if
71     # the mail has been confirmed
72
73     else:
74         # check for the confirmation
75
76         if re.search( r'Subject: CONFIRM', message_string ):
77
78             message_string = re.sub( r'(Subject: )(CONFIRM )(.*?)\n',
79                                      r'\1\3\n', message_string )
80             send_message()
81
82         else:
83             print >>sys.stderr, "Your message mentions attachments but does not include any."
84             print >>sys.stderr
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."
87             print >>sys.stderr
88             print >>sys.stderr, "\"CONFIRM\" will be stripped from your message subject before"
89             print >>sys.stderr, "the message is sent."
90             sys.exit( 1 )
91
92 ##    else:
93 ##        answer = ''
94 ##        while not answer:
95
96 ##            answer = 'n'
97 ##            answer = raw_input( "No attachment detected.\n"
98 ##                                "Are you sure you want to send this?"
99 ##                                " [Y/N]: " )
100
101                 
102 ##            # send the mail without the attachment
103 ##             if answer == "y":
104 ##                 send_message()
105
106 ##             # go back and let us reattach the message
107 ##             elif answer == "n":
108 ##                 sys.exit( 1 )
109
110 ##             # no good answer ask again
111 ##             else:
112 ##                 answer = ''
113
114
115 # if we are not expecting anything more, we should send the message
116 else:
117     send_message()

Benjamin Mako Hill || Want to submit a patch?