paul@49 | 1 | #!/usr/bin/env python |
paul@49 | 2 | |
paul@49 | 3 | from email import message_from_file |
paul@49 | 4 | from email.mime.multipart import MIMEMultipart |
paul@49 | 5 | from email.mime.text import MIMEText |
paul@49 | 6 | from smtplib import SMTP |
paul@49 | 7 | from imiptools.content import handle_itip_part |
paul@49 | 8 | import sys |
paul@49 | 9 | |
paul@49 | 10 | MESSAGE_SENDER = "resources+agent@example.com" |
paul@49 | 11 | |
paul@49 | 12 | MESSAGE_SUBJECT = "Calendar system message" |
paul@49 | 13 | |
paul@49 | 14 | MESSAGE_TEXT = """\ |
paul@49 | 15 | This is a response to a calendar message sent by your calendar program. |
paul@49 | 16 | """ |
paul@49 | 17 | |
paul@49 | 18 | # Postfix exit codes. |
paul@49 | 19 | |
paul@49 | 20 | EX_TEMPFAIL = 75 |
paul@49 | 21 | |
paul@49 | 22 | # Permitted iTIP content types. |
paul@49 | 23 | |
paul@49 | 24 | itip_content_types = [ |
paul@49 | 25 | "text/calendar", # from RFC 6047 |
paul@49 | 26 | "text/x-vcalendar", "application/ics", # other possibilities |
paul@49 | 27 | ] |
paul@49 | 28 | |
paul@49 | 29 | # Sending of outgoing messages. |
paul@49 | 30 | |
paul@49 | 31 | def sendmail(sender, recipients, data): |
paul@49 | 32 | smtp = SMTP("localhost") |
paul@49 | 33 | smtp.sendmail(sender, recipients, data) |
paul@49 | 34 | smtp.quit() |
paul@49 | 35 | |
paul@49 | 36 | # Processing of incoming messages. |
paul@49 | 37 | |
paul@49 | 38 | def get_all_values(msg, key): |
paul@49 | 39 | l = [] |
paul@49 | 40 | for v in msg.get_all(key) or []: |
paul@49 | 41 | l += [s.strip() for s in v.split(",")] |
paul@49 | 42 | return l |
paul@49 | 43 | |
paul@49 | 44 | class Processor: |
paul@49 | 45 | |
paul@49 | 46 | "The processing framework." |
paul@49 | 47 | |
paul@49 | 48 | def __init__(self, handlers, sender=None, subject=None, body_text=None): |
paul@49 | 49 | self.handlers = handlers |
paul@49 | 50 | self.sender = sender or MESSAGE_SENDER |
paul@49 | 51 | self.subject = subject or MESSAGE_SUBJECT |
paul@49 | 52 | self.body_text = body_text or MESSAGE_TEXT |
paul@49 | 53 | |
paul@49 | 54 | def process(self, f, original_recipients, recipients): |
paul@49 | 55 | |
paul@49 | 56 | """ |
paul@49 | 57 | Process content from the stream 'f' accompanied by the given |
paul@49 | 58 | 'original_recipients' and 'recipients'. |
paul@49 | 59 | """ |
paul@49 | 60 | |
paul@49 | 61 | msg = message_from_file(f) |
paul@49 | 62 | senders = msg.get_all("Reply-To") or msg.get_all("From") |
paul@49 | 63 | original_recipients = original_recipients or get_all_values(msg, "To") |
paul@49 | 64 | |
paul@49 | 65 | # Handle messages with iTIP parts. |
paul@49 | 66 | |
paul@49 | 67 | all_parts = [] |
paul@49 | 68 | |
paul@49 | 69 | for part in msg.walk(): |
paul@49 | 70 | if part.get_content_type() in itip_content_types and \ |
paul@49 | 71 | part.get_param("method"): |
paul@49 | 72 | |
paul@49 | 73 | all_parts += handle_itip_part(part, original_recipients, self.handlers) |
paul@49 | 74 | |
paul@59 | 75 | # Pack any returned parts into a single message. |
paul@49 | 76 | |
paul@49 | 77 | if all_parts: |
paul@49 | 78 | message = self.make_message(all_parts, senders) |
paul@49 | 79 | |
paul@49 | 80 | if "-d" in sys.argv: |
paul@49 | 81 | print message |
paul@49 | 82 | else: |
paul@49 | 83 | sendmail(self.sender, senders, message.as_string()) |
paul@49 | 84 | |
paul@59 | 85 | # Forward messages to their recipients. |
paul@59 | 86 | |
paul@49 | 87 | def make_message(self, parts, recipients): |
paul@49 | 88 | |
paul@49 | 89 | "Make a message from the given 'parts' for the given 'recipients'." |
paul@49 | 90 | |
paul@49 | 91 | message = MIMEMultipart("alternative", _subparts=parts) |
paul@49 | 92 | message.preamble = self.body_text |
paul@49 | 93 | payload = message.get_payload() |
paul@49 | 94 | payload.insert(0, MIMEText(self.body_text)) |
paul@49 | 95 | |
paul@49 | 96 | message["From"] = self.sender |
paul@49 | 97 | for recipient in recipients: |
paul@49 | 98 | message["To"] = recipient |
paul@49 | 99 | message["Subject"] = self.subject |
paul@49 | 100 | |
paul@49 | 101 | return message |
paul@49 | 102 | |
paul@49 | 103 | def process_args(self, args, stream): |
paul@49 | 104 | |
paul@49 | 105 | """ |
paul@49 | 106 | Interpret the given program arguments 'args' and process input from the |
paul@49 | 107 | given 'stream'. |
paul@49 | 108 | """ |
paul@49 | 109 | |
paul@49 | 110 | # Obtain the different kinds of recipients plus sender address. |
paul@49 | 111 | |
paul@49 | 112 | original_recipients = [] |
paul@49 | 113 | recipients = [] |
paul@49 | 114 | senders = [] |
paul@49 | 115 | |
paul@49 | 116 | l = [] |
paul@49 | 117 | |
paul@49 | 118 | for arg in args: |
paul@49 | 119 | |
paul@49 | 120 | # Switch to collecting recipients. |
paul@49 | 121 | |
paul@49 | 122 | if arg == "-o": |
paul@49 | 123 | l = original_recipients |
paul@49 | 124 | elif arg == "-r": |
paul@49 | 125 | l = recipients |
paul@49 | 126 | |
paul@49 | 127 | # Switch to collecting senders. |
paul@49 | 128 | |
paul@49 | 129 | elif arg == "-s": |
paul@49 | 130 | l = senders |
paul@49 | 131 | |
paul@49 | 132 | # Ignore debugging options. |
paul@49 | 133 | |
paul@49 | 134 | elif arg == "-d": |
paul@49 | 135 | pass |
paul@49 | 136 | else: |
paul@49 | 137 | l.append(arg) |
paul@49 | 138 | |
paul@49 | 139 | self.sender = senders and senders[0] or self.sender |
paul@49 | 140 | self.process(stream, original_recipients, recipients) |
paul@49 | 141 | |
paul@49 | 142 | def __call__(self): |
paul@49 | 143 | |
paul@49 | 144 | """ |
paul@49 | 145 | Obtain arguments from the command line to initialise the processor |
paul@49 | 146 | before invoking it. |
paul@49 | 147 | """ |
paul@49 | 148 | |
paul@49 | 149 | args = sys.argv[1:] |
paul@49 | 150 | |
paul@49 | 151 | if "-d" in args: |
paul@49 | 152 | self.process_args(args, sys.stdin) |
paul@49 | 153 | else: |
paul@49 | 154 | try: |
paul@49 | 155 | self.process_args(args, sys.stdin) |
paul@49 | 156 | except SystemExit, value: |
paul@49 | 157 | sys.exit(value) |
paul@49 | 158 | except Exception, exc: |
paul@49 | 159 | type, value, tb = sys.exc_info() |
paul@49 | 160 | print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) |
paul@49 | 161 | sys.exit(EX_TEMPFAIL) |
paul@49 | 162 | sys.exit(0) |
paul@49 | 163 | |
paul@49 | 164 | # vim: tabstop=4 expandtab shiftwidth=4 |