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, part in all_responses: 106 if outgoing: 107 outgoing_parts.append(part) 108 else: 109 forwarded_parts.append(part) 110 111 # Reply using any outgoing parts in a new message. 112 113 if outgoing_parts: 114 message = self.messenger.make_outgoing_message(outgoing_parts, senders) 115 116 if "-d" in sys.argv: 117 print >>sys.stderr, "Outgoing parts..." 118 print message 119 else: 120 self.messenger.sendmail(senders, message.as_string()) 121 122 # Forward messages to their recipients either wrapping the existing 123 # message, accompanying it or replacing it. 124 125 if forwarded_parts: 126 127 # Determine whether to wrap, accompany or replace the message. 128 129 preferences = Preferences(get_uri(recipient)) 130 131 incoming = preferences.get("incoming") 132 133 if incoming == "message-only": 134 messages = [msg] 135 else: 136 summary = self.messenger.make_summary_message(msg, forwarded_parts) 137 if incoming == "summary-then-message": 138 messages = [summary, msg] 139 elif incoming == "message-then-summary": 140 messages = [msg, summary] 141 elif incoming == "summary-only": 142 messages = [summary] 143 else: # incoming == "summary-wraps-message": 144 messages = [self.messenger.wrap_message(msg, forwarded_parts)] 145 146 for message in messages: 147 if "-d" in sys.argv: 148 print >>sys.stderr, "Forwarded parts..." 149 print message 150 elif self.lmtp_socket: 151 self.messenger.sendmail(recipient, message.as_string(), lmtp_socket=self.lmtp_socket) 152 153 # Unhandled messages are delivered as they are. 154 155 if not handled: 156 if "-d" in sys.argv: 157 print >>sys.stderr, "Unhandled parts..." 158 print msg 159 elif self.lmtp_socket: 160 self.messenger.sendmail(recipient, msg.as_string(), lmtp_socket=self.lmtp_socket) 161 162 def process_args(self, args, stream): 163 164 """ 165 Interpret the given program arguments 'args' and process input from the 166 given 'stream'. 167 """ 168 169 # Obtain the different kinds of recipients plus sender address. 170 171 original_recipients = [] 172 recipients = [] 173 senders = [] 174 lmtp = [] 175 outgoing_only = False 176 177 l = [] 178 179 for arg in args: 180 181 # Detect outgoing processing mode. 182 183 if arg == "-O": 184 outgoing_only = True 185 186 # Switch to collecting recipients. 187 188 if arg == "-o": 189 l = original_recipients 190 elif arg == "-r": 191 l = recipients 192 193 # Switch to collecting senders. 194 195 elif arg == "-s": 196 l = senders 197 198 # Switch to getting the LMTP socket. 199 200 elif arg == "-l": 201 l = lmtp 202 203 # Ignore debugging options. 204 205 elif arg == "-d": 206 pass 207 else: 208 l.append(arg) 209 210 self.messenger.sender = senders and senders[0] or self.messenger.sender 211 self.lmtp_socket = lmtp and lmtp[0] or None 212 self.process(stream, original_recipients, recipients, outgoing_only) 213 214 def __call__(self): 215 216 """ 217 Obtain arguments from the command line to initialise the processor 218 before invoking it. 219 """ 220 221 args = sys.argv[1:] 222 223 if "-d" in args: 224 self.process_args(args, sys.stdin) 225 else: 226 try: 227 self.process_args(args, sys.stdin) 228 except SystemExit, value: 229 sys.exit(value) 230 except Exception, exc: 231 if "-v" in args: 232 raise 233 type, value, tb = sys.exc_info() 234 print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) 235 #import traceback 236 #traceback.print_exc(file=open("/tmp/mail.log", "a")) 237 sys.exit(EX_TEMPFAIL) 238 sys.exit(0) 239 240 # vim: tabstop=4 expandtab shiftwidth=4