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@146 | 6 | Copyright (C) 2014, 2015 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@213 | 23 | from imiptools.content import handle_itip_part |
paul@223 | 24 | from imiptools.data import get_addresses, get_uri, make_freebusy, to_part |
paul@223 | 25 | from imiptools.dates import get_timestamp |
paul@83 | 26 | from imiptools.mail import Messenger |
paul@178 | 27 | from imiptools.profile import Preferences |
paul@223 | 28 | import imip_store |
paul@49 | 29 | import sys |
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@82 | 54 | def __init__(self, handlers, messenger=None): |
paul@49 | 55 | self.handlers = handlers |
paul@82 | 56 | self.messenger = messenger or Messenger() |
paul@60 | 57 | self.lmtp_socket = None |
paul@563 | 58 | self.store_dir = None |
paul@563 | 59 | self.publishing_dir = None |
paul@563 | 60 | self.debug = False |
paul@49 | 61 | |
paul@569 | 62 | def get_store(self): |
paul@569 | 63 | return self.store_dir and imip_store.FileStore(self.store_dir) or None |
paul@569 | 64 | |
paul@569 | 65 | def get_publisher(self): |
paul@569 | 66 | return self.publishing_dir and imip_store.FilePublisher(self.publishing_dir) or None |
paul@569 | 67 | |
paul@515 | 68 | def process(self, f, original_recipients, outgoing_only): |
paul@49 | 69 | |
paul@49 | 70 | """ |
paul@49 | 71 | Process content from the stream 'f' accompanied by the given |
paul@515 | 72 | 'original_recipients'. |
paul@49 | 73 | """ |
paul@49 | 74 | |
paul@49 | 75 | msg = message_from_file(f) |
paul@178 | 76 | senders = get_addresses(msg.get_all("Reply-To") or msg.get_all("From") or []) |
paul@49 | 77 | |
paul@49 | 78 | # Handle messages with iTIP parts. |
paul@438 | 79 | # Typically, the details of recipients are of interest in handling |
paul@438 | 80 | # messages. |
paul@49 | 81 | |
paul@438 | 82 | if not outgoing_only: |
paul@438 | 83 | original_recipients = original_recipients or get_addresses(get_all_values(msg, "To") or []) |
paul@438 | 84 | for recipient in original_recipients: |
paul@438 | 85 | self.process_for_recipient(msg, recipient, senders, outgoing_only) |
paul@438 | 86 | |
paul@438 | 87 | # However, outgoing messages do not usually presume anything about the |
paul@438 | 88 | # eventual recipients. |
paul@438 | 89 | |
paul@438 | 90 | else: |
paul@438 | 91 | self.process_for_recipient(msg, None, senders, outgoing_only) |
paul@179 | 92 | |
paul@179 | 93 | def process_for_recipient(self, msg, recipient, senders, outgoing_only): |
paul@179 | 94 | |
paul@179 | 95 | """ |
paul@179 | 96 | Process the given 'msg' for a single 'recipient', having the given |
paul@179 | 97 | 'senders', and with the given 'outgoing_only' status. |
paul@179 | 98 | |
paul@179 | 99 | Processing individually means that contributions to resulting messages |
paul@179 | 100 | may be constructed according to individual preferences. |
paul@179 | 101 | """ |
paul@179 | 102 | |
paul@569 | 103 | store = self.get_store() |
paul@569 | 104 | publisher = self.get_publisher() |
paul@563 | 105 | |
paul@563 | 106 | handlers = dict([(name, cls(senders, recipient, self.messenger, store, publisher)) |
paul@563 | 107 | for name, cls in self.handlers]) |
paul@60 | 108 | handled = False |
paul@49 | 109 | |
paul@49 | 110 | for part in msg.walk(): |
paul@49 | 111 | if part.get_content_type() in itip_content_types and \ |
paul@228 | 112 | part.get_param("method"): |
paul@49 | 113 | |
paul@228 | 114 | handle_itip_part(part, handlers) |
paul@60 | 115 | handled = True |
paul@49 | 116 | |
paul@96 | 117 | # When processing outgoing messages, no replies or deliveries are |
paul@96 | 118 | # performed. |
paul@96 | 119 | |
paul@96 | 120 | if outgoing_only: |
paul@96 | 121 | return |
paul@96 | 122 | |
paul@228 | 123 | # Get responses from the handlers. |
paul@228 | 124 | |
paul@228 | 125 | all_responses = [] |
paul@228 | 126 | for handler in handlers.values(): |
paul@228 | 127 | all_responses += handler.get_results() |
paul@228 | 128 | |
paul@178 | 129 | # Pack any returned parts into messages. |
paul@49 | 130 | |
paul@60 | 131 | if all_responses: |
paul@215 | 132 | outgoing_parts = {} |
paul@60 | 133 | forwarded_parts = [] |
paul@60 | 134 | |
paul@215 | 135 | for outgoing_recipients, part in all_responses: |
paul@215 | 136 | if outgoing_recipients: |
paul@215 | 137 | for outgoing_recipient in outgoing_recipients: |
paul@215 | 138 | if not outgoing_parts.has_key(outgoing_recipient): |
paul@215 | 139 | outgoing_parts[outgoing_recipient] = [] |
paul@215 | 140 | outgoing_parts[outgoing_recipient].append(part) |
paul@60 | 141 | else: |
paul@60 | 142 | forwarded_parts.append(part) |
paul@60 | 143 | |
paul@60 | 144 | # Reply using any outgoing parts in a new message. |
paul@60 | 145 | |
paul@60 | 146 | if outgoing_parts: |
paul@223 | 147 | |
paul@223 | 148 | # Obtain free/busy details, if configured to do so. |
paul@223 | 149 | |
paul@229 | 150 | fb = self.can_provide_freebusy(handlers) and self.get_freebusy_for_recipient(recipient) |
paul@223 | 151 | |
paul@215 | 152 | for outgoing_recipient, parts in outgoing_parts.items(): |
paul@223 | 153 | |
paul@223 | 154 | # Bundle free/busy messages, if configured to do so. |
paul@223 | 155 | |
paul@223 | 156 | if fb: parts.append(fb) |
paul@215 | 157 | message = self.messenger.make_outgoing_message(parts, [outgoing_recipient]) |
paul@49 | 158 | |
paul@563 | 159 | if self.debug: |
paul@215 | 160 | print >>sys.stderr, "Outgoing parts for %s..." % outgoing_recipient |
paul@215 | 161 | print message |
paul@215 | 162 | else: |
paul@215 | 163 | self.messenger.sendmail([outgoing_recipient], message.as_string()) |
paul@60 | 164 | |
paul@178 | 165 | # Forward messages to their recipients either wrapping the existing |
paul@178 | 166 | # message, accompanying it or replacing it. |
paul@60 | 167 | |
paul@60 | 168 | if forwarded_parts: |
paul@178 | 169 | |
paul@178 | 170 | # Determine whether to wrap, accompany or replace the message. |
paul@178 | 171 | |
paul@179 | 172 | preferences = Preferences(get_uri(recipient)) |
paul@178 | 173 | |
paul@179 | 174 | incoming = preferences.get("incoming") |
paul@60 | 175 | |
paul@179 | 176 | if incoming == "message-only": |
paul@179 | 177 | messages = [msg] |
paul@179 | 178 | else: |
paul@179 | 179 | summary = self.messenger.make_summary_message(msg, forwarded_parts) |
paul@179 | 180 | if incoming == "summary-then-message": |
paul@179 | 181 | messages = [summary, msg] |
paul@179 | 182 | elif incoming == "message-then-summary": |
paul@179 | 183 | messages = [msg, summary] |
paul@179 | 184 | elif incoming == "summary-only": |
paul@179 | 185 | messages = [summary] |
paul@179 | 186 | else: # incoming == "summary-wraps-message": |
paul@179 | 187 | messages = [self.messenger.wrap_message(msg, forwarded_parts)] |
paul@178 | 188 | |
paul@179 | 189 | for message in messages: |
paul@563 | 190 | if self.debug: |
paul@179 | 191 | print >>sys.stderr, "Forwarded parts..." |
paul@179 | 192 | print message |
paul@179 | 193 | elif self.lmtp_socket: |
paul@179 | 194 | self.messenger.sendmail(recipient, message.as_string(), lmtp_socket=self.lmtp_socket) |
paul@60 | 195 | |
paul@60 | 196 | # Unhandled messages are delivered as they are. |
paul@60 | 197 | |
paul@60 | 198 | if not handled: |
paul@563 | 199 | if self.debug: |
paul@106 | 200 | print >>sys.stderr, "Unhandled parts..." |
paul@60 | 201 | print msg |
paul@60 | 202 | elif self.lmtp_socket: |
paul@179 | 203 | self.messenger.sendmail(recipient, msg.as_string(), lmtp_socket=self.lmtp_socket) |
paul@64 | 204 | |
paul@229 | 205 | def can_provide_freebusy(self, handlers): |
paul@229 | 206 | |
paul@229 | 207 | "Test for any free/busy information produced by 'handlers'." |
paul@229 | 208 | |
paul@229 | 209 | fbhandler = handlers.get("VFREEBUSY") |
paul@229 | 210 | if fbhandler: |
paul@229 | 211 | fbmethods = fbhandler.get_outgoing_methods() |
paul@229 | 212 | return not "REPLY" in fbmethods and not "PUBLISH" in fbmethods |
paul@229 | 213 | else: |
paul@229 | 214 | return False |
paul@229 | 215 | |
paul@223 | 216 | def get_freebusy_for_recipient(self, recipient): |
paul@223 | 217 | |
paul@223 | 218 | """ |
paul@223 | 219 | Return a list of responses containing free/busy information for the |
paul@223 | 220 | given 'recipient'. |
paul@223 | 221 | """ |
paul@223 | 222 | |
paul@223 | 223 | organiser = get_uri(recipient) |
paul@223 | 224 | preferences = Preferences(organiser) |
paul@223 | 225 | |
paul@292 | 226 | organiser_attr = self.messenger and {"SENT-BY" : get_uri(self.messenger.sender)} or {} |
paul@292 | 227 | |
paul@223 | 228 | if preferences.get("freebusy_sharing") == "share" and \ |
paul@223 | 229 | preferences.get("freebusy_bundling") == "always": |
paul@223 | 230 | |
paul@223 | 231 | # Invent a unique identifier. |
paul@223 | 232 | |
paul@223 | 233 | utcnow = get_timestamp() |
paul@223 | 234 | uid = "imip-agent-%s-%s" % (utcnow, recipient) |
paul@223 | 235 | |
paul@569 | 236 | freebusy = (self.get_store() or imip_store.FileStore()).get_freebusy(organiser) |
paul@292 | 237 | return to_part("PUBLISH", [make_freebusy(freebusy, uid, organiser, organiser_attr)]) |
paul@223 | 238 | |
paul@229 | 239 | return None |
paul@229 | 240 | |
paul@49 | 241 | def process_args(self, args, stream): |
paul@49 | 242 | |
paul@49 | 243 | """ |
paul@49 | 244 | Interpret the given program arguments 'args' and process input from the |
paul@49 | 245 | given 'stream'. |
paul@49 | 246 | """ |
paul@49 | 247 | |
paul@49 | 248 | # Obtain the different kinds of recipients plus sender address. |
paul@49 | 249 | |
paul@49 | 250 | original_recipients = [] |
paul@49 | 251 | recipients = [] |
paul@49 | 252 | senders = [] |
paul@60 | 253 | lmtp = [] |
paul@563 | 254 | store_dir = [] |
paul@563 | 255 | publishing_dir = [] |
paul@96 | 256 | outgoing_only = False |
paul@49 | 257 | |
paul@49 | 258 | l = [] |
paul@49 | 259 | |
paul@49 | 260 | for arg in args: |
paul@49 | 261 | |
paul@96 | 262 | # Detect outgoing processing mode. |
paul@96 | 263 | |
paul@96 | 264 | if arg == "-O": |
paul@96 | 265 | outgoing_only = True |
paul@96 | 266 | |
paul@49 | 267 | # Switch to collecting recipients. |
paul@49 | 268 | |
paul@49 | 269 | if arg == "-o": |
paul@49 | 270 | l = original_recipients |
paul@49 | 271 | |
paul@49 | 272 | # Switch to collecting senders. |
paul@49 | 273 | |
paul@49 | 274 | elif arg == "-s": |
paul@49 | 275 | l = senders |
paul@49 | 276 | |
paul@60 | 277 | # Switch to getting the LMTP socket. |
paul@60 | 278 | |
paul@60 | 279 | elif arg == "-l": |
paul@60 | 280 | l = lmtp |
paul@60 | 281 | |
paul@563 | 282 | # Switch to getting the store directory. |
paul@563 | 283 | |
paul@563 | 284 | elif arg == "-S": |
paul@563 | 285 | l = store_dir |
paul@563 | 286 | |
paul@563 | 287 | # Switch to getting the publishing directory. |
paul@563 | 288 | |
paul@563 | 289 | elif arg == "-P": |
paul@563 | 290 | l = publishing_dir |
paul@563 | 291 | |
paul@49 | 292 | # Ignore debugging options. |
paul@49 | 293 | |
paul@49 | 294 | elif arg == "-d": |
paul@563 | 295 | self.debug = True |
paul@49 | 296 | else: |
paul@49 | 297 | l.append(arg) |
paul@49 | 298 | |
paul@82 | 299 | self.messenger.sender = senders and senders[0] or self.messenger.sender |
paul@60 | 300 | self.lmtp_socket = lmtp and lmtp[0] or None |
paul@563 | 301 | self.store_dir = store_dir and store_dir[0] or None |
paul@563 | 302 | self.publishing_dir = publishing_dir and publishing_dir[0] or None |
paul@515 | 303 | self.process(stream, original_recipients, outgoing_only) |
paul@49 | 304 | |
paul@49 | 305 | def __call__(self): |
paul@49 | 306 | |
paul@49 | 307 | """ |
paul@49 | 308 | Obtain arguments from the command line to initialise the processor |
paul@49 | 309 | before invoking it. |
paul@49 | 310 | """ |
paul@49 | 311 | |
paul@49 | 312 | args = sys.argv[1:] |
paul@49 | 313 | |
paul@49 | 314 | if "-d" in args: |
paul@49 | 315 | self.process_args(args, sys.stdin) |
paul@49 | 316 | else: |
paul@49 | 317 | try: |
paul@49 | 318 | self.process_args(args, sys.stdin) |
paul@49 | 319 | except SystemExit, value: |
paul@49 | 320 | sys.exit(value) |
paul@49 | 321 | except Exception, exc: |
paul@60 | 322 | if "-v" in args: |
paul@60 | 323 | raise |
paul@49 | 324 | type, value, tb = sys.exc_info() |
paul@310 | 325 | while tb.tb_next: |
paul@310 | 326 | tb = tb.tb_next |
paul@310 | 327 | f = tb.tb_frame |
paul@310 | 328 | co = f and f.f_code |
paul@310 | 329 | filename = co and co.co_filename |
paul@310 | 330 | print >>sys.stderr, "Exception %s at %d in %s" % (exc, tb.tb_lineno, filename) |
paul@82 | 331 | #import traceback |
paul@82 | 332 | #traceback.print_exc(file=open("/tmp/mail.log", "a")) |
paul@49 | 333 | sys.exit(EX_TEMPFAIL) |
paul@49 | 334 | sys.exit(0) |
paul@49 | 335 | |
paul@49 | 336 | # vim: tabstop=4 expandtab shiftwidth=4 |