From: Date: Sun, 3 Jul 2005 11:41:00 +0000 (-0400) Subject: Made a number of change and cleanup type fixes: X-Git-Url: https://projects.mako.cc/source/attachcheck/commitdiff_plain/4f860295b5c047929844843b1c46863a25f0ad66 Made a number of change and cleanup type fixes: * Added a perl script to add an override to a message passed on STDIN * Added a set of new test data * A host of general clean up and bug fix issues to the script --- diff --git a/add_override.pl b/add_override.pl new file mode 100644 index 0000000..803ccd9 --- /dev/null +++ b/add_override.pl @@ -0,0 +1,6 @@ +#!/usr/bin/perl -p -i + +if (not $edited and m/^\s*\n$/) { + s/^\s*\n$/X-AttachCheck-Override: Yes\n\n/; + $edited = 1; +} diff --git a/attachment_checker b/attachment_checker index cc3785e..e99d23d 100644 --- a/attachment_checker +++ b/attachment_checker @@ -2,26 +2,58 @@ # (c) 2004 -- Benjamin Mako Hill +# TODO: +# * check for an attachment overrides line as an alternative type of option + __copyright__ = "Copyright (c) 2004 Benjamin Mako Hill" __author__ = "Benjamin Mako Hill " -import sys -import os 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 -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() ) +## 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 ) @@ -45,9 +77,10 @@ for part in message.walk(): # 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 @@ -74,43 +107,17 @@ if attachment_expected: # 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() + elif message.get( 'X-AttachCheck-Override' ) == "Yes": + send_message() + 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 ) -## 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: diff --git a/test-data/confirmed_forgotten.msg b/test-data/confirmed_forgotten.msg new file mode 100644 index 0000000..5e47633 --- /dev/null +++ b/test-data/confirmed_forgotten.msg @@ -0,0 +1,44 @@ +From mako@atdot.cc Sat Jul 2 15:30:08 2005 +Date: Sat, 2 Jul 2005 15:30:08 +0200 +From: "Benj. Mako Hill" +To: mako@atdot.cc +Subject: CONFIRM forgotten attachment (signed) +Message-ID: <20050702133008.GC18291@yukidoke.org> +Mime-Version: 1.0 +Content-Type: multipart/signed; micalg=pgp-sha1; + protocol="application/pgp-signature"; boundary="mYYhpFXgKVw71fwr" +Content-Disposition: inline +User-Agent: Mutt/1.5.6+20040907i +Status: RO +Content-Length: 668 + +--mYYhpFXgKVw71fwr +Content-Type: text/plain; charset=us-ascii +Content-Disposition: inline +Content-Transfer-Encoding: quoted-printable + +attached + +--=20 +Benjamin Mako Hill +mako@atdot.cc +http://mako.cc/ + +Creativity can be a social contribution, but only in so +far as society is free to use the results. --RMS + +--mYYhpFXgKVw71fwr +Content-Type: application/pgp-signature; name="signature.asc" +Content-Description: Digital signature +Content-Disposition: inline + +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.2.5 (GNU/Linux) + +iD8DBQFCxpbgic1LIWB1WeYRAiqXAKCNEXlhCgDx9N5SXuDfRcmuQrZfFACgzihb +kvM5gTRPDzdPgcKAIaWGCHk= +=f0Xi +-----END PGP SIGNATURE----- + +--mYYhpFXgKVw71fwr-- + diff --git a/test-data/forgotten_attachment-signed.msg b/test-data/forgotten_attachment-signed.msg index ab41e08..6589783 100644 --- a/test-data/forgotten_attachment-signed.msg +++ b/test-data/forgotten_attachment-signed.msg @@ -2,7 +2,7 @@ From mako@atdot.cc Sat Jul 2 15:30:08 2005 Date: Sat, 2 Jul 2005 15:30:08 +0200 From: "Benj. Mako Hill" To: mako@atdot.cc -Subject: CONFIRM forgotten attachment (signed) +Subject: forgotten attachment (signed) Message-ID: <20050702133008.GC18291@yukidoke.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; diff --git a/test-data/x-overridden_forgotten.msg b/test-data/x-overridden_forgotten.msg new file mode 100644 index 0000000..a4994f5 --- /dev/null +++ b/test-data/x-overridden_forgotten.msg @@ -0,0 +1,46 @@ +From mako@atdot.cc Sat Jul 2 15:30:08 2005 +Date: Sat, 2 Jul 2005 15:30:08 +0200 +From: "Benj. Mako Hill" +To: mako@atdot.cc +Subject: forgotten attachment (signed) +Message-ID: <20050702133008.GC18291@yukidoke.org> +Mime-Version: 1.0 +X-AttachCheck-Override: Yes +Content-Type: multipart/signed; micalg=pgp-sha1; + protocol="application/pgp-signature"; boundary="mYYhpFXgKVw71fwr" +Content-Disposition: inline +User-Agent: Mutt/1.5.6+20040907i +Status: RO +Content-Length: 668 + + +--mYYhpFXgKVw71fwr +Content-Type: text/plain; charset=us-ascii +Content-Disposition: inline +Content-Transfer-Encoding: quoted-printable + +attached + +--=20 +Benjamin Mako Hill +mako@atdot.cc +http://mako.cc/ + +Creativity can be a social contribution, but only in so +far as society is free to use the results. --RMS + +--mYYhpFXgKVw71fwr +Content-Type: application/pgp-signature; name="signature.asc" +Content-Description: Digital signature +Content-Disposition: inline + +-----BEGIN PGP SIGNATURE----- +Version: GnuPG v1.2.5 (GNU/Linux) + +iD8DBQFCxpbgic1LIWB1WeYRAiqXAKCNEXlhCgDx9N5SXuDfRcmuQrZfFACgzihb +kvM5gTRPDzdPgcKAIaWGCHk= +=f0Xi +-----END PGP SIGNATURE----- + +--mYYhpFXgKVw71fwr-- +