1 #!/usr/bin/env python 2 3 """ 4 A processing framework for iMIP content. 5 6 Copyright (C) 2014, 2015, 2016, 2017 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.config import settings 24 from imiptools.client import Client 25 from imiptools.content import handle_itip_part 26 from imiptools.data import get_address, get_addresses, get_uri 27 from imiptools.mail import Messenger 28 from imiptools.stores import get_store, get_publisher, get_journal 29 import sys, os 30 31 # Postfix exit codes. 32 33 EX_TEMPFAIL = 75 34 35 # Permitted iTIP content types. 36 37 itip_content_types = [ 38 "text/calendar", # from RFC 6047 39 "text/x-vcalendar", "application/ics", # other possibilities 40 ] 41 42 # Processing of incoming messages. 43 44 def get_all_values(msg, key): 45 l = [] 46 for v in msg.get_all(key) or []: 47 l += [s.strip() for s in v.split(",")] 48 return l 49 50 class Processor: 51 52 "The processing framework." 53 54 def __init__(self, handlers, outgoing_only=False): 55 self.handlers = handlers 56 self.outgoing_only = outgoing_only 57 self.messenger = None 58 self.lmtp_socket = None 59 self.store_type = None 60 self.store_dir = None 61 self.publishing_dir = None 62 self.journal_dir = None 63 self.preferences_dir = None 64 self.debug = False 65 66 def get_store(self): 67 return get_store(self.store_type, self.store_dir) 68 69 def get_publisher(self): 70 return self.publishing_dir and get_publisher(self.publishing_dir) or None 71 72 def get_journal(self): 73 return get_journal(self.store_type, self.journal_dir) 74 75 def process(self, f, original_recipients): 76 77 """ 78 Process content from the stream 'f' accompanied by the given 79 'original_recipients'. 80 """ 81 82 msg = message_from_file(f) 83 84 messenger = self.messenger 85 store = self.get_store() 86 publisher = self.get_publisher() 87 journal = self.get_journal() 88 preferences_dir = self.preferences_dir 89 90 # Handle messages with iTIP parts. 91 # Typically, the details of recipients are of interest in handling 92 # messages. 93 94 if not self.outgoing_only: 95 senders = get_addresses(get_all_values(msg, "Reply-To") or get_all_values(msg, "From") or []) 96 original_recipients = original_recipients or get_addresses(get_all_values(msg, "To") or []) 97 users = [] 98 99 for recipient in original_recipients: 100 users.append(get_uri(recipient)) 101 102 # However, outgoing messages do not usually presume anything about the 103 # eventual recipients and focus on the sender instead. If possible, the 104 # sender is identified, but since this may be the calendar system (and 105 # the actual sender is defined in the object), and since the recipient 106 # may be in a Bcc header that is not available here, it may be left as 107 # None and deduced from the object content later. 108 109 else: 110 senders = [] 111 112 for sender in get_addresses(get_all_values(msg, "From") or []): 113 if sender != settings["MESSAGE_SENDER"]: 114 senders.append(sender) 115 116 users = [senders and get_uri(senders[0]) or None] 117 118 # Process the message for each recipient. 119 120 for user in users: 121 Recipient(user, messenger, store, publisher, journal, 122 preferences_dir, self.handlers, self.outgoing_only, 123 self.debug).process(msg, senders) 124 125 def process_args(self, args, stream): 126 127 """ 128 Interpret the given program arguments 'args' and process input from the 129 given 'stream'. 130 """ 131 132 # Obtain the different kinds of recipients plus sender address. 133 134 original_recipients = [] 135 recipients = [] 136 senders = [] 137 lmtp = [] 138 store_type = [] 139 store_dir = [] 140 publishing_dir = [] 141 preferences_dir = [] 142 journal_dir = [] 143 local_smtp = False 144 145 l = [] 146 147 for arg in args: 148 149 # Switch to collecting recipients. 150 151 if arg == "-o": 152 l = original_recipients 153 154 # Switch to collecting senders. 155 156 elif arg == "-s": 157 l = senders 158 159 # Switch to getting the LMTP socket. 160 161 elif arg == "-l": 162 l = lmtp 163 164 # Detect sending to local users via SMTP. 165 166 elif arg == "-L": 167 local_smtp = True 168 169 # Switch to getting the store type. 170 171 elif arg == "-T": 172 l = store_type 173 174 # Switch to getting the store directory. 175 176 elif arg == "-S": 177 l = store_dir 178 179 # Switch to getting the publishing directory. 180 181 elif arg == "-P": 182 l = publishing_dir 183 184 # Switch to getting the preferences directory. 185 186 elif arg == "-p": 187 l = preferences_dir 188 189 # Switch to getting the journal directory. 190 191 elif arg == "-j": 192 l = journal_dir 193 194 # Ignore debugging options. 195 196 elif arg == "-d": 197 self.debug = True 198 else: 199 l.append(arg) 200 201 getvalue = lambda value, default=None: value and value[0] or default 202 203 self.messenger = Messenger(lmtp_socket=getvalue(lmtp), local_smtp=local_smtp, sender=getvalue(senders)) 204 self.store_type = getvalue(store_type, settings["STORE_TYPE"]) 205 self.store_dir = getvalue(store_dir) 206 self.publishing_dir = getvalue(publishing_dir) 207 self.preferences_dir = getvalue(preferences_dir) 208 self.journal_dir = getvalue(journal_dir) 209 210 # If debug mode is set, extend the line length for convenience. 211 212 if self.debug: 213 settings["CALENDAR_LINE_LENGTH"] = 1000 214 215 # Process the input. 216 217 self.process(stream, original_recipients) 218 219 def __call__(self): 220 221 """ 222 Obtain arguments from the command line to initialise the processor 223 before invoking it. 224 """ 225 226 args = sys.argv[1:] 227 228 if "--help" in args: 229 print >>sys.stderr, """\ 230 Usage: %s [ -o <recipient> ... ] [-s <sender> ... ] [ -l <socket> | -L ] \\ 231 [ -T <store type ] \\ 232 [ -S <store directory> ] [ -P <publishing directory> ] \\ 233 [ -p <preferences directory> ] [ -j <journal directory> ] [ -d ] 234 235 Address options: 236 237 -o Indicate the original recipients of the message, overriding any found in 238 the message headers 239 -s Indicate the senders of the message, overriding any found in the message 240 headers 241 242 Delivery options: 243 244 -l The socket filename for LMTP communication with a mailbox solution, 245 selecting the LMTP delivery method 246 -L Selects the local SMTP delivery method, requiring a suitable mail system 247 configuration 248 249 (Where a program needs to deliver messages, one of the above options must be 250 specified.) 251 252 Configuration options: 253 254 -j Indicates the location of quota-related journal information 255 -P Indicates the location of published free/busy resources 256 -p Indicates the location of user preference directories 257 -S Indicates the location of the calendar data store containing user storage 258 directories 259 -T Indicates the store and journal type (the configured value if omitted) 260 261 Output options: 262 263 -d Run in debug mode, producing informative output describing the behaviour 264 of the program 265 """ % os.path.split(sys.argv[0])[-1] 266 elif "-d" in args: 267 self.process_args(args, sys.stdin) 268 else: 269 try: 270 self.process_args(args, sys.stdin) 271 except SystemExit, value: 272 sys.exit(value) 273 except Exception, exc: 274 if "-v" in args: 275 raise 276 type, value, tb = sys.exc_info() 277 while tb.tb_next: 278 tb = tb.tb_next 279 f = tb.tb_frame 280 co = f and f.f_code 281 filename = co and co.co_filename 282 print >>sys.stderr, "Exception %s at %d in %s" % (exc, tb.tb_lineno, filename) 283 #import traceback 284 #traceback.print_exc(file=open("/tmp/mail.log", "a")) 285 sys.exit(EX_TEMPFAIL) 286 sys.exit(0) 287 288 class Recipient(Client): 289 290 "A processor acting as a client on behalf of a recipient." 291 292 def __init__(self, user, messenger, store, publisher, journal, preferences_dir, 293 handlers, outgoing_only, debug): 294 295 """ 296 Initialise the recipient with the given 'user' identity, 'messenger', 297 'store', 'publisher', 'journal', 'preferences_dir', 'handlers', 298 'outgoing_only' and 'debug' status. 299 """ 300 301 Client.__init__(self, user, messenger, store, publisher, journal, preferences_dir) 302 self.handlers = handlers 303 self.outgoing_only = outgoing_only 304 self.debug = debug 305 306 def process(self, msg, senders): 307 308 """ 309 Process the given 'msg' for a single recipient, having the given 310 'senders'. 311 312 Processing individually means that contributions to resulting messages 313 may be constructed according to individual preferences. 314 """ 315 316 handlers = dict([(name, cls(senders, self.user and get_address(self.user), 317 self.messenger, self.store, self.publisher, 318 self.journal, self.preferences_dir)) 319 for name, cls in self.handlers]) 320 handled = False 321 322 # Check for participating recipients. Non-participating recipients will 323 # have their messages left as being unhandled. 324 325 if self.outgoing_only or self.is_participating(): 326 327 # Check for returned messages. 328 329 for part in msg.walk(): 330 if part.get_content_type() == "message/delivery-status": 331 break 332 else: 333 for part in msg.walk(): 334 if part.get_content_type() in itip_content_types and \ 335 part.get_param("method"): 336 337 handle_itip_part(part, handlers) 338 handled = True 339 340 # When processing outgoing messages, no replies or deliveries are 341 # performed. 342 343 if self.outgoing_only: 344 return 345 346 # Get responses from the handlers. 347 348 all_responses = [] 349 for handler in handlers.values(): 350 all_responses += handler.get_results() 351 352 # Pack any returned parts into messages. 353 354 if all_responses: 355 outgoing_parts = {} 356 forwarded_parts = [] 357 358 for outgoing_recipients, part in all_responses: 359 if outgoing_recipients: 360 for outgoing_recipient in outgoing_recipients: 361 if not outgoing_parts.has_key(outgoing_recipient): 362 outgoing_parts[outgoing_recipient] = [] 363 outgoing_parts[outgoing_recipient].append(part) 364 else: 365 forwarded_parts.append(part) 366 367 # Reply using any outgoing parts in a new message. 368 369 if outgoing_parts: 370 371 # Obtain free/busy details, if configured to do so. 372 373 fb = self.can_provide_freebusy(handlers) and self.get_freebusy_part() 374 375 for outgoing_recipient, parts in outgoing_parts.items(): 376 377 # Bundle free/busy messages, if configured to do so. 378 379 if fb: parts.append(fb) 380 message = self.messenger.make_outgoing_message(parts, [outgoing_recipient]) 381 382 if self.debug: 383 print >>sys.stderr, "Outgoing parts for %s..." % outgoing_recipient 384 print message 385 else: 386 self.messenger.sendmail([outgoing_recipient], message.as_string()) 387 388 # Forward messages to their recipients either wrapping the existing 389 # message, accompanying it or replacing it. 390 391 if forwarded_parts: 392 393 # Determine whether to wrap, accompany or replace the message. 394 395 prefs = self.get_preferences() 396 incoming = prefs.get("incoming", settings["INCOMING_DEFAULT"]) 397 398 if incoming == "message-only": 399 messages = [msg] 400 else: 401 summary = self.messenger.make_summary_message(msg, forwarded_parts) 402 if incoming == "summary-then-message": 403 messages = [summary, msg] 404 elif incoming == "message-then-summary": 405 messages = [msg, summary] 406 elif incoming == "summary-only": 407 messages = [summary] 408 else: # incoming == "summary-wraps-message": 409 messages = [self.messenger.wrap_message(msg, forwarded_parts)] 410 411 for message in messages: 412 if self.debug: 413 print >>sys.stderr, "Forwarded parts..." 414 print message 415 elif self.messenger.local_delivery(): 416 self.messenger.sendmail([get_address(self.user)], message.as_string()) 417 418 # Unhandled messages are delivered as they are. 419 420 if not handled: 421 if self.debug: 422 print >>sys.stderr, "Unhandled parts..." 423 print msg 424 elif self.messenger.local_delivery(): 425 self.messenger.sendmail([get_address(self.user)], msg.as_string()) 426 427 def can_provide_freebusy(self, handlers): 428 429 "Test for any free/busy information produced by 'handlers'." 430 431 fbhandler = handlers.get("VFREEBUSY") 432 if fbhandler: 433 fbmethods = fbhandler.get_outgoing_methods() 434 return not "REPLY" in fbmethods and not "PUBLISH" in fbmethods 435 else: 436 return False 437 438 # vim: tabstop=4 expandtab shiftwidth=4