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 handle_itip_part 24 from imiptools.data import get_addresses, get_uri 25 from imiptools.mail import Messenger 26 from imiptools.profile import Preferences 27 import sys 28 29 # Postfix exit codes. 30 31 EX_TEMPFAIL = 75 32 33 # Permitted iTIP content types. 34 35 itip_content_types = [ 36 "text/calendar", # from RFC 6047 37 "text/x-vcalendar", "application/ics", # other possibilities 38 ] 39 40 # Processing of incoming messages. 41 42 def get_all_values(msg, key): 43 l = [] 44 for v in msg.get_all(key) or []: 45 l += [s.strip() for s in v.split(",")] 46 return l 47 48 class Processor: 49 50 "The processing framework." 51 52 def __init__(self, handlers, messenger=None): 53 self.handlers = handlers 54 self.messenger = messenger or Messenger() 55 self.lmtp_socket = None 56 57 def process(self, f, original_recipients, recipients, outgoing_only): 58 59 """ 60 Process content from the stream 'f' accompanied by the given 61 'original_recipients' and 'recipients'. 62 """ 63 64 msg = message_from_file(f) 65 senders = get_addresses(msg.get_all("Reply-To") or msg.get_all("From") or []) 66 original_recipients = original_recipients or get_addresses(get_all_values(msg, "To") or []) 67 68 # Handle messages with iTIP parts. 69 70 for recipient in original_recipients: 71 self.process_for_recipient(msg, recipient, senders, outgoing_only) 72 73 def process_for_recipient(self, msg, recipient, senders, outgoing_only): 74 75 """ 76 Process the given 'msg' for a single 'recipient', having the given 77 'senders', and with the given 'outgoing_only' status. 78 79 Processing individually means that contributions to resulting messages 80 may be constructed according to individual preferences. 81 """ 82 83 all_responses = [] 84 handled = False 85 86 for part in msg.walk(): 87 if part.get_content_type() in itip_content_types and \ 88 part.get_param("method"): 89 90 all_responses += handle_itip_part(part, senders, recipient, self.handlers, self.messenger) 91 handled = True 92 93 # When processing outgoing messages, no replies or deliveries are 94 # performed. 95 96 if outgoing_only: 97 return 98 99 # Pack any returned parts into messages. 100 101 if all_responses: 102 outgoing_parts = {} 103 forwarded_parts = [] 104 105 for outgoing_recipients, part in all_responses: 106 if outgoing_recipients: 107 for outgoing_recipient in outgoing_recipients: 108 if not outgoing_parts.has_key(outgoing_recipient): 109 outgoing_parts[outgoing_recipient] = [] 110 outgoing_parts[outgoing_recipient].append(part) 111 else: 112 forwarded_parts.append(part) 113 114 # Reply using any outgoing parts in a new message. 115 116 if outgoing_parts: 117 for outgoing_recipient, parts in outgoing_parts.items(): 118 message = self.messenger.make_outgoing_message(parts, [outgoing_recipient]) 119 120 if "-d" in sys.argv: 121 print >>sys.stderr, "Outgoing parts for %s..." % outgoing_recipient 122 print message 123 else: 124 self.messenger.sendmail([outgoing_recipient], message.as_string()) 125 126 # Forward messages to their recipients either wrapping the existing 127 # message, accompanying it or replacing it. 128 129 if forwarded_parts: 130 131 # Determine whether to wrap, accompany or replace the message. 132 133 preferences = Preferences(get_uri(recipient)) 134 135 incoming = preferences.get("incoming") 136 137 if incoming == "message-only": 138 messages = [msg] 139 else: 140 summary = self.messenger.make_summary_message(msg, forwarded_parts) 141 if incoming == "summary-then-message": 142 messages = [summary, msg] 143 elif incoming == "message-then-summary": 144 messages = [msg, summary] 145 elif incoming == "summary-only": 146 messages = [summary] 147 else: # incoming == "summary-wraps-message": 148 messages = [self.messenger.wrap_message(msg, forwarded_parts)] 149 150 for message in messages: 151 if "-d" in sys.argv: 152 print >>sys.stderr, "Forwarded parts..." 153 print message 154 elif self.lmtp_socket: 155 self.messenger.sendmail(recipient, message.as_string(), lmtp_socket=self.lmtp_socket) 156 157 # Unhandled messages are delivered as they are. 158 159 if not handled: 160 if "-d" in sys.argv: 161 print >>sys.stderr, "Unhandled parts..." 162 print msg 163 elif self.lmtp_socket: 164 self.messenger.sendmail(recipient, msg.as_string(), lmtp_socket=self.lmtp_socket) 165 166 def process_args(self, args, stream): 167 168 """ 169 Interpret the given program arguments 'args' and process input from the 170 given 'stream'. 171 """ 172 173 # Obtain the different kinds of recipients plus sender address. 174 175 original_recipients = [] 176 recipients = [] 177 senders = [] 178 lmtp = [] 179 outgoing_only = False 180 181 l = [] 182 183 for arg in args: 184 185 # Detect outgoing processing mode. 186 187 if arg == "-O": 188 outgoing_only = True 189 190 # Switch to collecting recipients. 191 192 if arg == "-o": 193 l = original_recipients 194 elif arg == "-r": 195 l = recipients 196 197 # Switch to collecting senders. 198 199 elif arg == "-s": 200 l = senders 201 202 # Switch to getting the LMTP socket. 203 204 elif arg == "-l": 205 l = lmtp 206 207 # Ignore debugging options. 208 209 elif arg == "-d": 210 pass 211 else: 212 l.append(arg) 213 214 self.messenger.sender = senders and senders[0] or self.messenger.sender 215 self.lmtp_socket = lmtp and lmtp[0] or None 216 self.process(stream, original_recipients, recipients, outgoing_only) 217 218 def __call__(self): 219 220 """ 221 Obtain arguments from the command line to initialise the processor 222 before invoking it. 223 """ 224 225 args = sys.argv[1:] 226 227 if "-d" in args: 228 self.process_args(args, sys.stdin) 229 else: 230 try: 231 self.process_args(args, sys.stdin) 232 except SystemExit, value: 233 sys.exit(value) 234 except Exception, exc: 235 if "-v" in args: 236 raise 237 type, value, tb = sys.exc_info() 238 print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) 239 #import traceback 240 #traceback.print_exc(file=open("/tmp/mail.log", "a")) 241 sys.exit(EX_TEMPFAIL) 242 sys.exit(0) 243 244 # vim: tabstop=4 expandtab shiftwidth=4