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