paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@48 | 3 | """ |
paul@48 | 4 | Interpretation and preparation of iMIP content, together with a content handling |
paul@48 | 5 | mechanism employed by specific recipients. |
paul@146 | 6 | |
paul@146 | 7 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 8 | |
paul@146 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 12 | version. |
paul@146 | 13 | |
paul@146 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 17 | details. |
paul@146 | 18 | |
paul@146 | 19 | You should have received a copy of the GNU General Public License along with |
paul@146 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@48 | 21 | """ |
paul@48 | 22 | |
paul@152 | 23 | from datetime import datetime, timedelta |
paul@48 | 24 | from email.mime.text import MIMEText |
paul@182 | 25 | from imiptools.config import MANAGER_PATH, MANAGER_URL |
paul@213 | 26 | from imiptools.data import Object, parse_object, \ |
paul@226 | 27 | get_address, get_uri, get_value, \ |
paul@220 | 28 | is_new_object, uri_dict, uri_item |
paul@250 | 29 | from imiptools.dates import format_datetime, to_timezone |
paul@250 | 30 | from imiptools.period import can_schedule, insert_period, remove_period, \ |
paul@256 | 31 | remove_from_freebusy, \ |
paul@250 | 32 | remove_from_freebusy_for_other, \ |
paul@250 | 33 | update_freebusy, update_freebusy_for_other |
paul@152 | 34 | from pytz import timezone |
paul@182 | 35 | from socket import gethostname |
paul@48 | 36 | import imip_store |
paul@48 | 37 | |
paul@48 | 38 | try: |
paul@48 | 39 | from cStringIO import StringIO |
paul@48 | 40 | except ImportError: |
paul@48 | 41 | from StringIO import StringIO |
paul@48 | 42 | |
paul@48 | 43 | # Handler mechanism objects. |
paul@48 | 44 | |
paul@224 | 45 | def handle_itip_part(part, handlers): |
paul@48 | 46 | |
paul@48 | 47 | """ |
paul@227 | 48 | Handle the given iTIP 'part' using the given 'handlers' dictionary. |
paul@224 | 49 | |
paul@224 | 50 | Return a list of responses, each response being a tuple of the form |
paul@224 | 51 | (outgoing-recipients, message-part). |
paul@48 | 52 | """ |
paul@48 | 53 | |
paul@48 | 54 | method = part.get_param("method") |
paul@48 | 55 | |
paul@48 | 56 | # Decode the data and parse it. |
paul@48 | 57 | |
paul@48 | 58 | f = StringIO(part.get_payload(decode=True)) |
paul@48 | 59 | |
paul@48 | 60 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@48 | 61 | |
paul@48 | 62 | # Ignore the part if not a calendar object. |
paul@48 | 63 | |
paul@48 | 64 | if not itip: |
paul@228 | 65 | return |
paul@48 | 66 | |
paul@48 | 67 | # Require consistency between declared and employed methods. |
paul@48 | 68 | |
paul@48 | 69 | if get_value(itip, "METHOD") == method: |
paul@48 | 70 | |
paul@48 | 71 | # Look for different kinds of sections. |
paul@48 | 72 | |
paul@60 | 73 | all_results = [] |
paul@48 | 74 | |
paul@226 | 75 | for name, items in itip.items(): |
paul@226 | 76 | |
paul@226 | 77 | # Get a handler for the given section. |
paul@226 | 78 | |
paul@226 | 79 | handler = handlers.get(name) |
paul@226 | 80 | if not handler: |
paul@226 | 81 | continue |
paul@226 | 82 | |
paul@226 | 83 | for item in items: |
paul@48 | 84 | |
paul@48 | 85 | # Dispatch to a handler and obtain any response. |
paul@48 | 86 | |
paul@226 | 87 | handler.set_object(Object({name : item})) |
paul@228 | 88 | methods[method](handler)() |
paul@48 | 89 | |
paul@182 | 90 | # References to the Web interface. |
paul@182 | 91 | |
paul@182 | 92 | def get_manager_url(): |
paul@182 | 93 | url_base = MANAGER_URL or "http://%s/" % gethostname() |
paul@182 | 94 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@182 | 95 | |
paul@182 | 96 | def get_object_url(uid): |
paul@182 | 97 | return "%s/%s" % (get_manager_url().rstrip("/"), uid) |
paul@182 | 98 | |
paul@48 | 99 | class Handler: |
paul@48 | 100 | |
paul@48 | 101 | "General handler support." |
paul@48 | 102 | |
paul@224 | 103 | def __init__(self, senders=None, recipient=None, messenger=None): |
paul@48 | 104 | |
paul@48 | 105 | """ |
paul@213 | 106 | Initialise the handler with the calendar 'obj' and the 'senders' and |
paul@213 | 107 | 'recipient' of the object (if specifically indicated). |
paul@48 | 108 | """ |
paul@48 | 109 | |
paul@129 | 110 | self.senders = senders and set(map(get_address, senders)) |
paul@179 | 111 | self.recipient = recipient and get_address(recipient) |
paul@89 | 112 | self.messenger = messenger |
paul@48 | 113 | |
paul@228 | 114 | self.results = [] |
paul@228 | 115 | self.outgoing_methods = set() |
paul@228 | 116 | |
paul@226 | 117 | self.obj = None |
paul@226 | 118 | self.uid = None |
paul@226 | 119 | self.sequence = None |
paul@226 | 120 | self.dtstamp = None |
paul@48 | 121 | |
paul@48 | 122 | self.store = imip_store.FileStore() |
paul@48 | 123 | |
paul@48 | 124 | try: |
paul@48 | 125 | self.publisher = imip_store.FilePublisher() |
paul@48 | 126 | except OSError: |
paul@48 | 127 | self.publisher = None |
paul@48 | 128 | |
paul@224 | 129 | def set_object(self, obj): |
paul@224 | 130 | self.obj = obj |
paul@226 | 131 | self.uid = self.obj.get_value("UID") |
paul@226 | 132 | self.sequence = self.obj.get_value("SEQUENCE") |
paul@226 | 133 | self.dtstamp = self.obj.get_value("DTSTAMP") |
paul@224 | 134 | |
paul@182 | 135 | def wrap(self, text, link=True): |
paul@182 | 136 | |
paul@182 | 137 | "Wrap any valid message for passing to the recipient." |
paul@182 | 138 | |
paul@182 | 139 | texts = [] |
paul@182 | 140 | texts.append(text) |
paul@182 | 141 | if link: |
paul@182 | 142 | texts.append("If your mail program cannot handle this " |
paul@182 | 143 | "message, you may view the details here:\n\n%s" % |
paul@182 | 144 | get_object_url(self.uid)) |
paul@182 | 145 | |
paul@228 | 146 | return self.add_result(None, None, MIMEText("\n".join(texts))) |
paul@228 | 147 | |
paul@228 | 148 | # Result registration. |
paul@228 | 149 | |
paul@228 | 150 | def add_result(self, method, outgoing_recipients, part): |
paul@228 | 151 | |
paul@228 | 152 | """ |
paul@228 | 153 | Record a result having the given 'method', 'outgoing_recipients' and |
paul@228 | 154 | message part. |
paul@228 | 155 | """ |
paul@228 | 156 | |
paul@228 | 157 | if outgoing_recipients: |
paul@228 | 158 | self.outgoing_methods.add(method) |
paul@228 | 159 | self.results.append((outgoing_recipients, part)) |
paul@228 | 160 | |
paul@228 | 161 | def get_results(self): |
paul@228 | 162 | return self.results |
paul@228 | 163 | |
paul@228 | 164 | def get_outgoing_methods(self): |
paul@228 | 165 | return self.outgoing_methods |
paul@182 | 166 | |
paul@70 | 167 | # Access to calendar structures and other data. |
paul@70 | 168 | |
paul@122 | 169 | def remove_from_freebusy(self, freebusy, attendee): |
paul@122 | 170 | remove_from_freebusy(freebusy, attendee, self.uid, self.store) |
paul@122 | 171 | |
paul@168 | 172 | def remove_from_freebusy_for_other(self, freebusy, user, other): |
paul@168 | 173 | remove_from_freebusy_for_other(freebusy, user, other, self.uid, self.store) |
paul@168 | 174 | |
paul@77 | 175 | def update_freebusy(self, freebusy, attendee, periods): |
paul@305 | 176 | update_freebusy(freebusy, attendee, periods, self.obj.get_value("TRANSP"), |
paul@305 | 177 | self.uid, self.store) |
paul@77 | 178 | |
paul@304 | 179 | def update_freebusy_from_participant(self, user, participant_item, tzid): |
paul@304 | 180 | |
paul@304 | 181 | """ |
paul@304 | 182 | For the given 'user', record the free/busy information for the |
paul@304 | 183 | 'participant_item' (a value plus attributes), using the 'tzid' to define |
paul@304 | 184 | period information. |
paul@304 | 185 | """ |
paul@304 | 186 | |
paul@304 | 187 | participant, participant_attr = participant_item |
paul@304 | 188 | |
paul@304 | 189 | if participant != user: |
paul@304 | 190 | freebusy = self.store.get_freebusy_for_other(user, participant) |
paul@304 | 191 | |
paul@304 | 192 | if participant_attr.get("PARTSTAT") != "DECLINED": |
paul@304 | 193 | update_freebusy_for_other(freebusy, user, participant, |
paul@304 | 194 | self.obj.get_periods_for_freebusy(tzid), |
paul@304 | 195 | self.obj.get_value("TRANSP"), |
paul@304 | 196 | self.uid, self.store) |
paul@304 | 197 | else: |
paul@304 | 198 | self.remove_from_freebusy_for_other(freebusy, user, participant) |
paul@304 | 199 | |
paul@291 | 200 | def update_freebusy_from_organiser(self, attendee, organiser_item, tzid=None): |
paul@268 | 201 | |
paul@268 | 202 | """ |
paul@268 | 203 | For the 'attendee', record free/busy information from the |
paul@268 | 204 | 'organiser_item' (a value plus attributes). |
paul@268 | 205 | """ |
paul@268 | 206 | |
paul@304 | 207 | self.update_freebusy_from_participant(attendee, organiser_item, tzid) |
paul@268 | 208 | |
paul@291 | 209 | def update_freebusy_from_attendees(self, organiser, attendees, tzid=None): |
paul@268 | 210 | |
paul@268 | 211 | "For the 'organiser', record free/busy information from 'attendees'." |
paul@268 | 212 | |
paul@304 | 213 | for attendee_item in attendees.items(): |
paul@304 | 214 | self.update_freebusy_from_participant(organiser, attendee_item, tzid) |
paul@168 | 215 | |
paul@77 | 216 | def can_schedule(self, freebusy, periods): |
paul@77 | 217 | return can_schedule(freebusy, periods, self.uid) |
paul@76 | 218 | |
paul@168 | 219 | def filter_by_senders(self, mapping): |
paul@168 | 220 | |
paul@168 | 221 | """ |
paul@168 | 222 | Return a list of items from 'mapping' filtered using sender information. |
paul@168 | 223 | """ |
paul@168 | 224 | |
paul@89 | 225 | if self.senders: |
paul@168 | 226 | |
paul@168 | 227 | # Get a mapping from senders to identities. |
paul@168 | 228 | |
paul@168 | 229 | identities = self.get_sender_identities(mapping) |
paul@168 | 230 | |
paul@168 | 231 | # Find the senders that are valid. |
paul@168 | 232 | |
paul@168 | 233 | senders = map(get_address, identities) |
paul@168 | 234 | valid = self.senders.intersection(senders) |
paul@89 | 235 | |
paul@168 | 236 | # Return the true identities. |
paul@168 | 237 | |
paul@168 | 238 | return [identities[get_uri(address)] for address in valid] |
paul@168 | 239 | else: |
paul@168 | 240 | return mapping |
paul@168 | 241 | |
paul@179 | 242 | def filter_by_recipient(self, mapping): |
paul@168 | 243 | |
paul@168 | 244 | """ |
paul@168 | 245 | Return a list of items from 'mapping' filtered using recipient |
paul@168 | 246 | information. |
paul@168 | 247 | """ |
paul@168 | 248 | |
paul@179 | 249 | if self.recipient: |
paul@179 | 250 | addresses = set(map(get_address, mapping)) |
paul@179 | 251 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@89 | 252 | else: |
paul@168 | 253 | return mapping |
paul@48 | 254 | |
paul@292 | 255 | def require_organiser(self, from_organiser=True): |
paul@51 | 256 | |
paul@51 | 257 | """ |
paul@292 | 258 | Return the organiser for the current object, filtered for the sender or |
paul@292 | 259 | recipient of interest. Return None if no identities are eligible. |
paul@292 | 260 | |
paul@292 | 261 | The organiser identity is normalized. |
paul@292 | 262 | """ |
paul@292 | 263 | |
paul@292 | 264 | organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@292 | 265 | |
paul@292 | 266 | # Only provide details for an organiser who sent/receives the message. |
paul@292 | 267 | |
paul@292 | 268 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@129 | 269 | |
paul@292 | 270 | if not organiser_filter_fn(dict([organiser_item])): |
paul@292 | 271 | return None |
paul@292 | 272 | |
paul@292 | 273 | return organiser_item |
paul@292 | 274 | |
paul@292 | 275 | def require_attendees(self, from_organiser=True): |
paul@292 | 276 | |
paul@292 | 277 | """ |
paul@292 | 278 | Return the attendees for the current object, filtered for the sender or |
paul@292 | 279 | recipient of interest. Return None if no identities are eligible. |
paul@292 | 280 | |
paul@292 | 281 | The attendee identities are normalized. |
paul@51 | 282 | """ |
paul@51 | 283 | |
paul@213 | 284 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@48 | 285 | |
paul@168 | 286 | # Only provide details for attendees who sent/receive the message. |
paul@48 | 287 | |
paul@179 | 288 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@100 | 289 | |
paul@48 | 290 | attendees = {} |
paul@168 | 291 | for attendee in attendee_filter_fn(attendee_map): |
paul@48 | 292 | attendees[attendee] = attendee_map[attendee] |
paul@48 | 293 | |
paul@292 | 294 | return attendees |
paul@292 | 295 | |
paul@292 | 296 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@292 | 297 | |
paul@292 | 298 | """ |
paul@292 | 299 | Return the organiser and attendees for the current object, filtered for |
paul@292 | 300 | the recipient of interest. Return None if no identities are eligible. |
paul@168 | 301 | |
paul@292 | 302 | Organiser and attendee identities are normalized. |
paul@292 | 303 | """ |
paul@168 | 304 | |
paul@292 | 305 | organiser_item = self.require_organiser(from_organiser) |
paul@292 | 306 | attendees = self.require_attendees(from_organiser) |
paul@168 | 307 | |
paul@292 | 308 | if not attendees or not organiser_item: |
paul@89 | 309 | return None |
paul@89 | 310 | |
paul@168 | 311 | return organiser_item, attendees |
paul@94 | 312 | |
paul@168 | 313 | def get_sender_identities(self, mapping): |
paul@94 | 314 | |
paul@94 | 315 | """ |
paul@168 | 316 | Return a mapping from actual senders to the identities for which they |
paul@168 | 317 | have provided data, extracting this information from the given |
paul@168 | 318 | 'mapping'. |
paul@94 | 319 | """ |
paul@94 | 320 | |
paul@168 | 321 | senders = {} |
paul@89 | 322 | |
paul@168 | 323 | for value, attr in mapping.items(): |
paul@94 | 324 | sent_by = attr.get("SENT-BY") |
paul@94 | 325 | if sent_by: |
paul@168 | 326 | senders[get_uri(sent_by)] = value |
paul@168 | 327 | else: |
paul@168 | 328 | senders[value] = value |
paul@48 | 329 | |
paul@168 | 330 | return senders |
paul@48 | 331 | |
paul@213 | 332 | def get_object(self, user): |
paul@100 | 333 | |
paul@100 | 334 | """ |
paul@100 | 335 | Return the stored object to which the current object refers for the |
paul@100 | 336 | given 'user' and for the given 'objtype'. |
paul@100 | 337 | """ |
paul@100 | 338 | |
paul@301 | 339 | fragment = self.store.get_event(user, self.uid) |
paul@213 | 340 | return fragment and Object(fragment) |
paul@100 | 341 | |
paul@213 | 342 | def have_new_object(self, attendee, obj=None): |
paul@51 | 343 | |
paul@51 | 344 | """ |
paul@213 | 345 | Return whether the current object is new to the 'attendee' (or if the |
paul@213 | 346 | given 'obj' is new). |
paul@51 | 347 | """ |
paul@51 | 348 | |
paul@213 | 349 | obj = obj or self.get_object(attendee) |
paul@51 | 350 | |
paul@51 | 351 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@51 | 352 | |
paul@100 | 353 | if obj: |
paul@213 | 354 | sequence = obj.get_value("SEQUENCE") |
paul@213 | 355 | dtstamp = obj.get_value("DTSTAMP") |
paul@51 | 356 | |
paul@100 | 357 | # If the request refers to an older version of the object, ignore |
paul@51 | 358 | # it. |
paul@51 | 359 | |
paul@172 | 360 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@172 | 361 | self.is_partstat_updated(obj)) |
paul@51 | 362 | |
paul@51 | 363 | return True |
paul@51 | 364 | |
paul@172 | 365 | def is_partstat_updated(self, obj): |
paul@174 | 366 | |
paul@174 | 367 | """ |
paul@174 | 368 | Return whether the participant status has been updated in the current |
paul@174 | 369 | object in comparison to the given 'obj'. |
paul@174 | 370 | |
paul@174 | 371 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@174 | 372 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@174 | 373 | NOTE: incorporated into any new object assessment. |
paul@174 | 374 | """ |
paul@174 | 375 | |
paul@213 | 376 | old_attendees = obj.get_value_map("ATTENDEE") |
paul@213 | 377 | new_attendees = self.obj.get_value_map("ATTENDEE") |
paul@172 | 378 | |
paul@172 | 379 | for attendee, attr in old_attendees.items(): |
paul@172 | 380 | old_partstat = attr.get("PARTSTAT") |
paul@172 | 381 | new_attr = new_attendees.get(attendee) |
paul@172 | 382 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@172 | 383 | |
paul@172 | 384 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@172 | 385 | new_partstat != old_partstat: |
paul@172 | 386 | |
paul@172 | 387 | return True |
paul@172 | 388 | |
paul@172 | 389 | return False |
paul@172 | 390 | |
paul@268 | 391 | def merge_attendance(self, attendees, identity): |
paul@268 | 392 | |
paul@268 | 393 | """ |
paul@268 | 394 | Merge attendance from the current object's 'attendees' into the version |
paul@268 | 395 | stored for the given 'identity'. |
paul@268 | 396 | """ |
paul@268 | 397 | |
paul@268 | 398 | obj = self.get_object(identity) |
paul@268 | 399 | |
paul@268 | 400 | if not obj or not self.have_new_object(identity, obj=obj): |
paul@268 | 401 | return False |
paul@268 | 402 | |
paul@268 | 403 | # Get attendee details in a usable form. |
paul@268 | 404 | |
paul@268 | 405 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@268 | 406 | |
paul@268 | 407 | for attendee, attendee_attr in attendees.items(): |
paul@268 | 408 | |
paul@268 | 409 | # Update attendance in the loaded object. |
paul@268 | 410 | |
paul@268 | 411 | attendee_map[attendee] = attendee_attr |
paul@268 | 412 | |
paul@268 | 413 | # Set the new details and store the object. |
paul@268 | 414 | |
paul@268 | 415 | obj["ATTENDEE"] = attendee_map.items() |
paul@268 | 416 | |
paul@268 | 417 | self.store.set_event(identity, self.uid, obj.to_node()) |
paul@268 | 418 | |
paul@268 | 419 | return True |
paul@268 | 420 | |
paul@158 | 421 | def update_dtstamp(self): |
paul@158 | 422 | |
paul@158 | 423 | "Update the DTSTAMP in the current object." |
paul@158 | 424 | |
paul@213 | 425 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@158 | 426 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@213 | 427 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@158 | 428 | |
paul@273 | 429 | def set_sequence(self, increment=False): |
paul@273 | 430 | |
paul@273 | 431 | "Update the SEQUENCE in the current object." |
paul@273 | 432 | |
paul@273 | 433 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@273 | 434 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@273 | 435 | |
paul@48 | 436 | # Handler registry. |
paul@48 | 437 | |
paul@48 | 438 | methods = { |
paul@48 | 439 | "ADD" : lambda handler: handler.add, |
paul@48 | 440 | "CANCEL" : lambda handler: handler.cancel, |
paul@48 | 441 | "COUNTER" : lambda handler: handler.counter, |
paul@48 | 442 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@48 | 443 | "PUBLISH" : lambda handler: handler.publish, |
paul@48 | 444 | "REFRESH" : lambda handler: handler.refresh, |
paul@48 | 445 | "REPLY" : lambda handler: handler.reply, |
paul@48 | 446 | "REQUEST" : lambda handler: handler.request, |
paul@48 | 447 | } |
paul@48 | 448 | |
paul@48 | 449 | # vim: tabstop=4 expandtab shiftwidth=4 |