1 #!/usr/bin/env python 2 3 """ 4 A processing framework for iMIP content. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from email import message_from_file 23 from imiptools.content import get_addresses, handle_itip_part 24 from imiptools.mail import Messenger 25 import sys 26 27 # Postfix exit codes. 28 29 EX_TEMPFAIL = 75 30 31 # Permitted iTIP content types. 32 33 itip_content_types = [ 34 "text/calendar", # from RFC 6047 35 "text/x-vcalendar", "application/ics", # other possibilities 36 ] 37 38 # Processing of incoming messages. 39 40 def get_all_values(msg, key): 41 l = [] 42 for v in msg.get_all(key) or []: 43 l += [s.strip() for s in v.split(",")] 44 return l 45 46 class Processor: 47 48 "The processing framework." 49 50 def __init__(self, handlers, messenger=None): 51 self.handlers = handlers 52 self.messenger = messenger or Messenger() 53 self.lmtp_socket = None 54 55 def process(self, f, original_recipients, recipients, outgoing_only): 56 57 """ 58 Process content from the stream 'f' accompanied by the given 59 'original_recipients' and 'recipients'. 60 """ 61 62 msg = message_from_file(f) 63 senders = get_addresses(msg.get_all("Reply-To") or msg.get_all("From")) 64 original_recipients = original_recipients or get_addresses(get_all_values(msg, "To")) 65 66 # Handle messages with iTIP parts. 67 68 all_responses = [] 69 handled = False 70 71 for part in msg.walk(): 72 if part.get_content_type() in itip_content_types and \ 73 part.get_param("method"): 74 75 all_responses += handle_itip_part(part, senders, original_recipients, self.handlers, self.messenger) 76 handled = True 77 78 # When processing outgoing messages, no replies or deliveries are 79 # performed. 80 81 if outgoing_only: 82 return 83 84 # Pack any returned parts into a single message. 85 86 if all_responses: 87 outgoing_parts = [] 88 forwarded_parts = [] 89 90 for outgoing, part in all_responses: 91 if outgoing: 92 outgoing_parts.append(part) 93 else: 94 forwarded_parts.append(part) 95 96 # Reply using any outgoing parts in a new message. 97 98 if outgoing_parts: 99 message = self.messenger.make_message(outgoing_parts, senders) 100 101 if "-d" in sys.argv: 102 print >>sys.stderr, "Outgoing parts..." 103 print message 104 else: 105 self.messenger.sendmail(senders, message.as_string()) 106 107 # Forward messages to their recipients using the existing message. 108 109 if forwarded_parts: 110 message = self.messenger.wrap_message(msg, forwarded_parts) 111 112 if "-d" in sys.argv: 113 print >>sys.stderr, "Forwarded parts..." 114 print message 115 elif self.lmtp_socket: 116 self.messenger.sendmail(original_recipients, message.as_string(), lmtp_socket=self.lmtp_socket) 117 118 # Unhandled messages are delivered as they are. 119 120 if not handled: 121 if "-d" in sys.argv: 122 print >>sys.stderr, "Unhandled parts..." 123 print msg 124 elif self.lmtp_socket: 125 self.messenger.sendmail(original_recipients, msg.as_string(), lmtp_socket=self.lmtp_socket) 126 127 def process_args(self, args, stream): 128 129 """ 130 Interpret the given program arguments 'args' and process input from the 131 given 'stream'. 132 """ 133 134 # Obtain the different kinds of recipients plus sender address. 135 136 original_recipients = [] 137 recipients = [] 138 senders = [] 139 lmtp = [] 140 outgoing_only = False 141 142 l = [] 143 144 for arg in args: 145 146 # Detect outgoing processing mode. 147 148 if arg == "-O": 149 outgoing_only = True 150 151 # Switch to collecting recipients. 152 153 if arg == "-o": 154 l = original_recipients 155 elif arg == "-r": 156 l = recipients 157 158 # Switch to collecting senders. 159 160 elif arg == "-s": 161 l = senders 162 163 # Switch to getting the LMTP socket. 164 165 elif arg == "-l": 166 l = lmtp 167 168 # Ignore debugging options. 169 170 elif arg == "-d": 171 pass 172 else: 173 l.append(arg) 174 175 self.messenger.sender = senders and senders[0] or self.messenger.sender 176 self.lmtp_socket = lmtp and lmtp[0] or None 177 self.process(stream, original_recipients, recipients, outgoing_only) 178 179 def __call__(self): 180 181 """ 182 Obtain arguments from the command line to initialise the processor 183 before invoking it. 184 """ 185 186 args = sys.argv[1:] 187 188 if "-d" in args: 189 self.process_args(args, sys.stdin) 190 else: 191 try: 192 self.process_args(args, sys.stdin) 193 except SystemExit, value: 194 sys.exit(value) 195 except Exception, exc: 196 if "-v" in args: 197 raise 198 type, value, tb = sys.exc_info() 199 print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) 200 #import traceback 201 #traceback.print_exc(file=open("/tmp/mail.log", "a")) 202 sys.exit(EX_TEMPFAIL) 203 sys.exit(0) 204 205 # vim: tabstop=4 expandtab shiftwidth=4