- move the new system to python3
- rework the subject munging so that it doesn't involve working with the full
message as string
- pass message object around explicitly
# AttachCheck -- A MTA wrapper to help check outgoing email for
# forgotten attachments.
# AttachCheck -- A MTA wrapper to help check outgoing email for
# forgotten attachments.
import re
## SUB: send message
import re
## SUB: send message
+def send_message(message):
from subprocess import Popen, PIPE
process = Popen(cmd, stdin=PIPE)
mailpipe = process.stdin
from subprocess import Popen, PIPE
process = Popen(cmd, stdin=PIPE)
mailpipe = process.stdin
- mailpipe.write( message_string )
+ mailpipe.write( message.as_string().encode() )
mailpipe.close()
sys.exit( process.wait() )
## SUB: print error message
def print_error():
mailpipe.close()
sys.exit( process.wait() )
## SUB: print error message
def print_error():
"""(Your message mentions attachments but does not include any.
Either:
"""(Your message mentions attachments but does not include any.
Either:
(2) Add a \"X-AttachCheck-Override: Yes\" header the message.
(2) Add a \"X-AttachCheck-Override: Yes\" header the message.
-Read the documentation for AttachCheck for more details."""
+Read the documentation for AttachCheck for more details.""", file=sys.stderr)
# get the mail from stdin
# get the mail from stdin
-message_string = sys.stdin.read()
-message = email.message_from_string( message_string )
+message_bytes = sys.stdin.buffer.read()
+message = email.message_from_bytes( message_bytes )
attachment_expected = False
attachment_seen = False
attachment_expected = False
attachment_seen = False
# if we are expecting an attachment and we've seen one, we should
# send the file and be done with things
if attachment_seen:
# if we are expecting an attachment and we've seen one, we should
# send the file and be done with things
if attachment_seen:
# if we are expected have not seen it, we need to check to see if
# the mail has been confirmed
else:
# check for the confirmation
# if we are expected have not seen it, we need to check to see if
# the mail has been confirmed
else:
# 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()
+ subject_string = message['Subject']
+ print(subject_string)
+ if re.search( r'^\s*CONFIRM', subject_string ):
+ subject_string = re.sub( r'(CONFIRM )(.*?)', r'\2', subject_string )
+ message['Subject'] = subject_string
+ send_message(message)
+
+ elif message['X-AttachCheck-Override'] == "Yes":
+ send_message(message)
# 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: