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