Made a number of change and cleanup type fixes:
[attachcheck] / attachment_checker
index cc3785e8a436f33b588f1a195927873b97eab2cd..e99d23df8132b6e6f30cdb0254a01b3bc03fc25d 100644 (file)
@@ -2,26 +2,58 @@
 
 # (c) 2004 -- Benjamin Mako Hill <mako@bork.hampshire.edu>
 
 
 # (c) 2004 -- Benjamin Mako Hill <mako@bork.hampshire.edu>
 
+# TODO:
+# * check for an attachment overrides line as an alternative type of option
+
 __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill"
 __author__ = "Benjamin Mako Hill <mako@debian.org>" 
 
 __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill"
 __author__ = "Benjamin Mako Hill <mako@debian.org>" 
 
-import sys
-import os
 import re
 import re
-import email
+
+### Configuration Options
+###########################################
 
 # location of the sendmail binary
 sendmail = "/usr/sbin/sendmail"
 
 # list of mimetype which are, for the sake of this program, not
 # attachments
 
 # location of the sendmail binary
 sendmail = "/usr/sbin/sendmail"
 
 # list of mimetype which are, for the sake of this program, not
 # attachments
-ignored_types = ( "applica/pgp-signat", "application/pgp-signature" ) 
+ignored_types = ( "applica/pgp-signat", "application/pgp-signature" )
+
+# list of regular expressions which we will view as being indicative
+# of an attachment
+attachment_regexes = [ r'\battach(ed|ment)?\b(?im)' ]
+
 
 
+### No Need To Edit Below This Line
+###########################################
+
+import sys
+import os
+import email
+
+## SUB: send message
 def send_message(): 
     mailpipe = os.popen("%s -t" % sendmail, 'w')
     mailpipe.write( message_string )
     sys.exit( mailpipe.close() )
 
 def send_message(): 
     mailpipe = os.popen("%s -t" % sendmail, 'w')
     mailpipe.write( message_string )
     sys.exit( mailpipe.close() )
 
+## SUB: print error message
+def print_error():
+    print >>sys.stderr, \
+"""(Your message mentions attachments but does not include any.
+
+Either:
+
+ (1) Add an attachment or add \"CONFIRM\" (in ALLCAPS to beginning of
+     the subject of the message and resend.  \"CONFIRM\" will be
+     stripped from your message subject befor the message is sent.
+
+
+ (2) Add a \"X-AttachCheck-Override: Yes\" header the message.
+
+Read the documentation for AttachCheck for more details."""
+    
 # get the mail from stdin
 message_string = sys.stdin.read()
 message = email.message_from_string( message_string )
 # get the mail from stdin
 message_string = sys.stdin.read()
 message = email.message_from_string( message_string )
@@ -45,9 +77,10 @@ for part in message.walk():
 
         # search for the word "attach" or "attached" or "attachment"
         # in the body of the message
 
         # search for the word "attach" or "attached" or "attachment"
         # in the body of the message
-        if re.search( r'\battach(ed|ment)?\b(?im)',
-                      part.get_payload() ):
-            attachment_expected = True
+        for regex in attachment_regexes:
+
+            if re.search( regex, part.get_payload() ):
+                attachment_expected = True
 
     # check to see if this mime-type is something we can ignore
     elif ( re.match( r'message/(?m)', part.get_content_type() ) or
 
     # check to see if this mime-type is something we can ignore
     elif ( re.match( r'message/(?m)', part.get_content_type() ) or
@@ -74,43 +107,17 @@ if attachment_expected:
         # check for the confirmation
 
         if re.search( r'Subject: CONFIRM', message_string ):
         # check for the confirmation
 
         if re.search( r'Subject: CONFIRM', message_string ):
-
             message_string = re.sub( r'(Subject: )(CONFIRM )(.*?)\n',
                                      r'\1\3\n', message_string )
             send_message()
 
             message_string = re.sub( r'(Subject: )(CONFIRM )(.*?)\n',
                                      r'\1\3\n', message_string )
             send_message()
 
+        elif message.get( 'X-AttachCheck-Override' ) == "Yes":
+            send_message()
+            
         else:
         else:
-            print >>sys.stderr, "Your message mentions attachments but does not include any."
-            print >>sys.stderr
-            print >>sys.stderr, "Either add an attachment or add \"CONFIRM\" (in ALLCAPS)"
-            print >>sys.stderr, "to beginning of the subject of the message and resend."
-            print >>sys.stderr
-            print >>sys.stderr, "\"CONFIRM\" will be stripped from your message subject before"
-            print >>sys.stderr, "the message is sent."
+            print_error()
             sys.exit( 1 )
 
             sys.exit( 1 )
 
-##    else:
-##        answer = ''
-##        while not answer:
-
-##            answer = 'n'
-##            answer = raw_input( "No attachment detected.\n"
-##                                "Are you sure you want to send this?"
-##                                " [Y/N]: " )
-
-                
-##            # send the mail without the attachment
-##             if answer == "y":
-##                 send_message()
-
-##             # go back and let us reattach the message
-##             elif answer == "n":
-##                 sys.exit( 1 )
-
-##             # no good answer ask again
-##             else:
-##                 answer = ''
-
 
 # if we are not expecting anything more, we should send the message
 else:
 
 # if we are not expecting anything more, we should send the message
 else:

Benjamin Mako Hill || Want to submit a patch?