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