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