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