paul@418 | 1 | #!/usr/bin/env python |
paul@418 | 2 | |
paul@418 | 3 | """ |
paul@418 | 4 | General handler support for incoming calendar objects. |
paul@418 | 5 | |
paul@418 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@418 | 7 | |
paul@418 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@418 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@418 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@418 | 11 | version. |
paul@418 | 12 | |
paul@418 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@418 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@418 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@418 | 16 | details. |
paul@418 | 17 | |
paul@418 | 18 | You should have received a copy of the GNU General Public License along with |
paul@418 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@418 | 20 | """ |
paul@418 | 21 | |
paul@418 | 22 | from email.mime.text import MIMEText |
paul@601 | 23 | from imiptools.client import ClientForObject |
paul@418 | 24 | from imiptools.config import MANAGER_PATH, MANAGER_URL |
paul@601 | 25 | from imiptools.data import Object, get_address, get_uri, \ |
paul@418 | 26 | is_new_object, uri_dict, uri_item, uri_values |
paul@563 | 27 | from imiptools.dates import format_datetime, get_recurrence_start_point, \ |
paul@561 | 28 | to_timezone |
paul@418 | 29 | from imiptools.period import can_schedule, remove_period, \ |
paul@601 | 30 | remove_additional_periods, remove_affected_period |
paul@418 | 31 | from imiptools.profile import Preferences |
paul@418 | 32 | from socket import gethostname |
paul@418 | 33 | import imip_store |
paul@418 | 34 | |
paul@418 | 35 | # References to the Web interface. |
paul@418 | 36 | |
paul@418 | 37 | def get_manager_url(): |
paul@418 | 38 | url_base = MANAGER_URL or "http://%s/" % gethostname() |
paul@418 | 39 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@418 | 40 | |
paul@418 | 41 | def get_object_url(uid, recurrenceid=None): |
paul@418 | 42 | return "%s/%s%s" % ( |
paul@418 | 43 | get_manager_url().rstrip("/"), uid, |
paul@418 | 44 | recurrenceid and "/%s" % recurrenceid or "" |
paul@418 | 45 | ) |
paul@418 | 46 | |
paul@601 | 47 | class Handler(ClientForObject): |
paul@418 | 48 | |
paul@418 | 49 | "General handler support." |
paul@418 | 50 | |
paul@563 | 51 | def __init__(self, senders=None, recipient=None, messenger=None, store=None, |
paul@563 | 52 | publisher=None): |
paul@418 | 53 | |
paul@418 | 54 | """ |
paul@601 | 55 | Initialise the handler with any specifically indicated 'senders' and |
paul@601 | 56 | 'recipient' of a calendar object. The object is initially undefined. |
paul@601 | 57 | |
paul@601 | 58 | The optional 'messenger' provides a means of interacting with the mail |
paul@601 | 59 | system. |
paul@563 | 60 | |
paul@563 | 61 | The optional 'store' and 'publisher' can be specified to override the |
paul@563 | 62 | default store and publisher objects. |
paul@418 | 63 | """ |
paul@418 | 64 | |
paul@601 | 65 | ClientForObject.__init__(self, None, recipient and get_uri(recipient), messenger) |
paul@468 | 66 | |
paul@418 | 67 | self.senders = senders and set(map(get_address, senders)) |
paul@418 | 68 | self.recipient = recipient and get_address(recipient) |
paul@418 | 69 | |
paul@418 | 70 | self.results = [] |
paul@418 | 71 | self.outgoing_methods = set() |
paul@418 | 72 | |
paul@563 | 73 | self.store = store or imip_store.FileStore() |
paul@418 | 74 | |
paul@418 | 75 | try: |
paul@563 | 76 | self.publisher = publisher or imip_store.FilePublisher() |
paul@418 | 77 | except OSError: |
paul@418 | 78 | self.publisher = None |
paul@418 | 79 | |
paul@418 | 80 | def wrap(self, text, link=True): |
paul@418 | 81 | |
paul@418 | 82 | "Wrap any valid message for passing to the recipient." |
paul@418 | 83 | |
paul@418 | 84 | texts = [] |
paul@418 | 85 | texts.append(text) |
paul@418 | 86 | if link: |
paul@418 | 87 | texts.append("If your mail program cannot handle this " |
paul@418 | 88 | "message, you may view the details here:\n\n%s" % |
paul@418 | 89 | get_object_url(self.uid, self.recurrenceid)) |
paul@418 | 90 | |
paul@418 | 91 | return self.add_result(None, None, MIMEText("\n".join(texts))) |
paul@418 | 92 | |
paul@418 | 93 | # Result registration. |
paul@418 | 94 | |
paul@418 | 95 | def add_result(self, method, outgoing_recipients, part): |
paul@418 | 96 | |
paul@418 | 97 | """ |
paul@418 | 98 | Record a result having the given 'method', 'outgoing_recipients' and |
paul@418 | 99 | message part. |
paul@418 | 100 | """ |
paul@418 | 101 | |
paul@418 | 102 | if outgoing_recipients: |
paul@418 | 103 | self.outgoing_methods.add(method) |
paul@418 | 104 | self.results.append((outgoing_recipients, part)) |
paul@418 | 105 | |
paul@418 | 106 | def get_results(self): |
paul@418 | 107 | return self.results |
paul@418 | 108 | |
paul@418 | 109 | def get_outgoing_methods(self): |
paul@418 | 110 | return self.outgoing_methods |
paul@418 | 111 | |
paul@418 | 112 | # Convenience methods for modifying free/busy collections. |
paul@418 | 113 | |
paul@563 | 114 | def get_recurrence_start_point(self, recurrenceid): |
paul@511 | 115 | |
paul@511 | 116 | "Get 'recurrenceid' in a form suitable for matching free/busy entries." |
paul@511 | 117 | |
paul@561 | 118 | tzid = self.obj.get_tzid() or self.get_tzid() |
paul@563 | 119 | return get_recurrence_start_point(recurrenceid, tzid) |
paul@511 | 120 | |
paul@418 | 121 | def remove_from_freebusy(self, freebusy): |
paul@418 | 122 | |
paul@418 | 123 | "Remove this event from the given 'freebusy' collection." |
paul@418 | 124 | |
paul@523 | 125 | if not remove_period(freebusy, self.uid, self.recurrenceid) and self.recurrenceid: |
paul@563 | 126 | remove_affected_period(freebusy, self.uid, self.get_recurrence_start_point(self.recurrenceid)) |
paul@418 | 127 | |
paul@418 | 128 | def remove_freebusy_for_recurrences(self, freebusy, recurrenceids=None): |
paul@418 | 129 | |
paul@418 | 130 | """ |
paul@418 | 131 | Remove from 'freebusy' any original recurrence from parent free/busy |
paul@418 | 132 | details for the current object, if the current object is a specific |
paul@418 | 133 | additional recurrence. Otherwise, remove all additional recurrence |
paul@418 | 134 | information corresponding to 'recurrenceids', or if omitted, all |
paul@418 | 135 | recurrences. |
paul@418 | 136 | """ |
paul@418 | 137 | |
paul@418 | 138 | if self.recurrenceid: |
paul@563 | 139 | recurrenceid = self.get_recurrence_start_point(self.recurrenceid) |
paul@511 | 140 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@418 | 141 | else: |
paul@418 | 142 | # Remove obsolete recurrence periods. |
paul@418 | 143 | |
paul@418 | 144 | remove_additional_periods(freebusy, self.uid, recurrenceids) |
paul@418 | 145 | |
paul@418 | 146 | # Remove original periods affected by additional recurrences. |
paul@418 | 147 | |
paul@418 | 148 | if recurrenceids: |
paul@418 | 149 | for recurrenceid in recurrenceids: |
paul@563 | 150 | recurrenceid = self.get_recurrence_start_point(recurrenceid) |
paul@418 | 151 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@418 | 152 | |
paul@418 | 153 | # Convenience methods for updating stored free/busy information. |
paul@418 | 154 | |
paul@468 | 155 | def update_freebusy_from_participant(self, participant_item, for_organiser): |
paul@418 | 156 | |
paul@418 | 157 | """ |
paul@468 | 158 | For the calendar user, record the free/busy information for the |
paul@418 | 159 | 'participant_item' (a value plus attributes) representing a different |
paul@418 | 160 | identity, thus maintaining a separate record of their free/busy details. |
paul@418 | 161 | """ |
paul@418 | 162 | |
paul@418 | 163 | participant, participant_attr = participant_item |
paul@418 | 164 | |
paul@580 | 165 | # A user does not store free/busy information for themself as another |
paul@580 | 166 | # party. |
paul@580 | 167 | |
paul@468 | 168 | if participant == self.user: |
paul@418 | 169 | return |
paul@418 | 170 | |
paul@468 | 171 | freebusy = self.store.get_freebusy_for_other(self.user, participant) |
paul@419 | 172 | |
paul@419 | 173 | # Obtain the stored object if the current object is not issued by the |
paul@419 | 174 | # organiser. |
paul@419 | 175 | |
paul@580 | 176 | obj = self.get_definitive_object(for_organiser) |
paul@419 | 177 | if not obj: |
paul@419 | 178 | return |
paul@419 | 179 | |
paul@419 | 180 | # Obtain the affected periods. |
paul@419 | 181 | |
paul@557 | 182 | periods = obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@418 | 183 | |
paul@418 | 184 | # Record in the free/busy details unless a non-participating attendee. |
paul@505 | 185 | # Use any attendee information for an organiser, not the organiser's own |
paul@505 | 186 | # attributes. |
paul@505 | 187 | |
paul@505 | 188 | if for_organiser: |
paul@505 | 189 | participant_attr = obj.get_value_map("ATTENDEE").get(participant) |
paul@418 | 190 | |
paul@418 | 191 | self.update_freebusy_for_participant(freebusy, periods, participant_attr, |
paul@468 | 192 | for_organiser and not self.is_attendee(participant)) |
paul@418 | 193 | |
paul@505 | 194 | # Tidy up any obsolete recurrences. |
paul@505 | 195 | |
paul@468 | 196 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@468 | 197 | self.store.set_freebusy_for_other(self.user, freebusy, participant) |
paul@418 | 198 | |
paul@468 | 199 | def update_freebusy_from_organiser(self, organiser_item): |
paul@418 | 200 | |
paul@418 | 201 | """ |
paul@468 | 202 | For the current user, record free/busy information from the |
paul@418 | 203 | 'organiser_item' (a value plus attributes). |
paul@418 | 204 | """ |
paul@418 | 205 | |
paul@468 | 206 | self.update_freebusy_from_participant(organiser_item, True) |
paul@418 | 207 | |
paul@468 | 208 | def update_freebusy_from_attendees(self, attendees): |
paul@418 | 209 | |
paul@468 | 210 | "For the current user, record free/busy information from 'attendees'." |
paul@418 | 211 | |
paul@418 | 212 | for attendee_item in attendees.items(): |
paul@468 | 213 | self.update_freebusy_from_participant(attendee_item, False) |
paul@418 | 214 | |
paul@418 | 215 | # Logic, filtering and access to calendar structures and other data. |
paul@418 | 216 | |
paul@468 | 217 | def is_attendee(self, identity, obj=None): |
paul@418 | 218 | |
paul@468 | 219 | """ |
paul@468 | 220 | Return whether 'identity' is an attendee in the current object, or in |
paul@468 | 221 | 'obj' if specified. |
paul@468 | 222 | """ |
paul@418 | 223 | |
paul@468 | 224 | return identity in uri_values((obj or self.obj).get_values("ATTENDEE")) |
paul@418 | 225 | |
paul@418 | 226 | def can_schedule(self, freebusy, periods): |
paul@582 | 227 | |
paul@582 | 228 | """ |
paul@582 | 229 | Indicate whether within 'freebusy' the given 'periods' can be scheduled. |
paul@582 | 230 | """ |
paul@582 | 231 | |
paul@418 | 232 | return can_schedule(freebusy, periods, self.uid, self.recurrenceid) |
paul@418 | 233 | |
paul@418 | 234 | def filter_by_senders(self, mapping): |
paul@418 | 235 | |
paul@418 | 236 | """ |
paul@418 | 237 | Return a list of items from 'mapping' filtered using sender information. |
paul@418 | 238 | """ |
paul@418 | 239 | |
paul@418 | 240 | if self.senders: |
paul@418 | 241 | |
paul@418 | 242 | # Get a mapping from senders to identities. |
paul@418 | 243 | |
paul@418 | 244 | identities = self.get_sender_identities(mapping) |
paul@418 | 245 | |
paul@418 | 246 | # Find the senders that are valid. |
paul@418 | 247 | |
paul@418 | 248 | senders = map(get_address, identities) |
paul@418 | 249 | valid = self.senders.intersection(senders) |
paul@418 | 250 | |
paul@418 | 251 | # Return the true identities. |
paul@418 | 252 | |
paul@418 | 253 | return [identities[get_uri(address)] for address in valid] |
paul@418 | 254 | else: |
paul@418 | 255 | return mapping |
paul@418 | 256 | |
paul@418 | 257 | def filter_by_recipient(self, mapping): |
paul@418 | 258 | |
paul@418 | 259 | """ |
paul@418 | 260 | Return a list of items from 'mapping' filtered using recipient |
paul@418 | 261 | information. |
paul@418 | 262 | """ |
paul@418 | 263 | |
paul@418 | 264 | if self.recipient: |
paul@418 | 265 | addresses = set(map(get_address, mapping)) |
paul@418 | 266 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@418 | 267 | else: |
paul@418 | 268 | return mapping |
paul@418 | 269 | |
paul@418 | 270 | def require_organiser(self, from_organiser=True): |
paul@418 | 271 | |
paul@418 | 272 | """ |
paul@418 | 273 | Return the organiser for the current object, filtered for the sender or |
paul@418 | 274 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 275 | |
paul@418 | 276 | The organiser identity is normalized. |
paul@418 | 277 | """ |
paul@418 | 278 | |
paul@418 | 279 | organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@418 | 280 | |
paul@418 | 281 | # Only provide details for an organiser who sent/receives the message. |
paul@418 | 282 | |
paul@418 | 283 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@418 | 284 | |
paul@418 | 285 | if not organiser_filter_fn(dict([organiser_item])): |
paul@418 | 286 | return None |
paul@418 | 287 | |
paul@418 | 288 | return organiser_item |
paul@418 | 289 | |
paul@418 | 290 | def require_attendees(self, from_organiser=True): |
paul@418 | 291 | |
paul@418 | 292 | """ |
paul@418 | 293 | Return the attendees for the current object, filtered for the sender or |
paul@418 | 294 | recipient of interest. Return None if no identities are eligible. |
paul@418 | 295 | |
paul@418 | 296 | The attendee identities are normalized. |
paul@418 | 297 | """ |
paul@418 | 298 | |
paul@418 | 299 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@418 | 300 | |
paul@418 | 301 | # Only provide details for attendees who sent/receive the message. |
paul@418 | 302 | |
paul@418 | 303 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@418 | 304 | |
paul@418 | 305 | attendees = {} |
paul@418 | 306 | for attendee in attendee_filter_fn(attendee_map): |
paul@418 | 307 | attendees[attendee] = attendee_map[attendee] |
paul@418 | 308 | |
paul@418 | 309 | return attendees |
paul@418 | 310 | |
paul@418 | 311 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@418 | 312 | |
paul@418 | 313 | """ |
paul@418 | 314 | Return the organiser and attendees for the current object, filtered for |
paul@418 | 315 | the recipient of interest. Return None if no identities are eligible. |
paul@418 | 316 | |
paul@418 | 317 | Organiser and attendee identities are normalized. |
paul@418 | 318 | """ |
paul@418 | 319 | |
paul@418 | 320 | organiser_item = self.require_organiser(from_organiser) |
paul@418 | 321 | attendees = self.require_attendees(from_organiser) |
paul@418 | 322 | |
paul@418 | 323 | if not attendees or not organiser_item: |
paul@418 | 324 | return None |
paul@418 | 325 | |
paul@418 | 326 | return organiser_item, attendees |
paul@418 | 327 | |
paul@418 | 328 | def get_sender_identities(self, mapping): |
paul@418 | 329 | |
paul@418 | 330 | """ |
paul@418 | 331 | Return a mapping from actual senders to the identities for which they |
paul@418 | 332 | have provided data, extracting this information from the given |
paul@418 | 333 | 'mapping'. |
paul@418 | 334 | """ |
paul@418 | 335 | |
paul@418 | 336 | senders = {} |
paul@418 | 337 | |
paul@418 | 338 | for value, attr in mapping.items(): |
paul@418 | 339 | sent_by = attr.get("SENT-BY") |
paul@418 | 340 | if sent_by: |
paul@418 | 341 | senders[get_uri(sent_by)] = value |
paul@418 | 342 | else: |
paul@418 | 343 | senders[value] = value |
paul@418 | 344 | |
paul@418 | 345 | return senders |
paul@418 | 346 | |
paul@468 | 347 | def _get_object(self, uid, recurrenceid): |
paul@418 | 348 | |
paul@418 | 349 | """ |
paul@468 | 350 | Return the stored object for the current user, with the given 'uid' and |
paul@468 | 351 | 'recurrenceid'. |
paul@418 | 352 | """ |
paul@418 | 353 | |
paul@468 | 354 | fragment = self.store.get_event(self.user, uid, recurrenceid) |
paul@418 | 355 | return fragment and Object(fragment) |
paul@418 | 356 | |
paul@468 | 357 | def get_object(self): |
paul@418 | 358 | |
paul@418 | 359 | """ |
paul@418 | 360 | Return the stored object to which the current object refers for the |
paul@468 | 361 | current user. |
paul@418 | 362 | """ |
paul@418 | 363 | |
paul@468 | 364 | return self._get_object(self.uid, self.recurrenceid) |
paul@418 | 365 | |
paul@582 | 366 | def get_definitive_object(self, from_organiser): |
paul@582 | 367 | |
paul@582 | 368 | """ |
paul@582 | 369 | Return an object considered definitive for the current transaction, |
paul@582 | 370 | using 'from_organiser' to select the current transaction's object if |
paul@582 | 371 | true, or selecting a stored object if false. |
paul@582 | 372 | """ |
paul@582 | 373 | |
paul@582 | 374 | return from_organiser and self.obj or self.get_object() |
paul@582 | 375 | |
paul@468 | 376 | def get_parent_object(self): |
paul@418 | 377 | |
paul@418 | 378 | """ |
paul@418 | 379 | Return the parent object to which the current object refers for the |
paul@468 | 380 | current user. |
paul@418 | 381 | """ |
paul@418 | 382 | |
paul@468 | 383 | return self.recurrenceid and self._get_object(self.uid, None) or None |
paul@418 | 384 | |
paul@468 | 385 | def have_new_object(self, obj=None): |
paul@418 | 386 | |
paul@418 | 387 | """ |
paul@468 | 388 | Return whether the current object is new to the current user (or if the |
paul@418 | 389 | given 'obj' is new). |
paul@418 | 390 | """ |
paul@418 | 391 | |
paul@468 | 392 | obj = obj or self.get_object() |
paul@418 | 393 | |
paul@418 | 394 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@418 | 395 | |
paul@418 | 396 | if obj: |
paul@418 | 397 | sequence = obj.get_value("SEQUENCE") |
paul@418 | 398 | dtstamp = obj.get_value("DTSTAMP") |
paul@418 | 399 | |
paul@418 | 400 | # If the request refers to an older version of the object, ignore |
paul@418 | 401 | # it. |
paul@418 | 402 | |
paul@418 | 403 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@418 | 404 | self.is_partstat_updated(obj)) |
paul@418 | 405 | |
paul@418 | 406 | return True |
paul@418 | 407 | |
paul@418 | 408 | def is_partstat_updated(self, obj): |
paul@418 | 409 | |
paul@418 | 410 | """ |
paul@418 | 411 | Return whether the participant status has been updated in the current |
paul@418 | 412 | object in comparison to the given 'obj'. |
paul@418 | 413 | |
paul@418 | 414 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@418 | 415 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@418 | 416 | NOTE: incorporated into any new object assessment. |
paul@418 | 417 | """ |
paul@418 | 418 | |
paul@418 | 419 | old_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@418 | 420 | new_attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@418 | 421 | |
paul@418 | 422 | for attendee, attr in old_attendees.items(): |
paul@418 | 423 | old_partstat = attr.get("PARTSTAT") |
paul@418 | 424 | new_attr = new_attendees.get(attendee) |
paul@418 | 425 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@418 | 426 | |
paul@418 | 427 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@418 | 428 | new_partstat != old_partstat: |
paul@418 | 429 | |
paul@418 | 430 | return True |
paul@418 | 431 | |
paul@418 | 432 | return False |
paul@418 | 433 | |
paul@468 | 434 | def merge_attendance(self, attendees): |
paul@418 | 435 | |
paul@418 | 436 | """ |
paul@418 | 437 | Merge attendance from the current object's 'attendees' into the version |
paul@468 | 438 | stored for the current user. |
paul@418 | 439 | """ |
paul@418 | 440 | |
paul@468 | 441 | obj = self.get_object() |
paul@418 | 442 | |
paul@468 | 443 | if not obj or not self.have_new_object(obj): |
paul@418 | 444 | return False |
paul@418 | 445 | |
paul@418 | 446 | # Get attendee details in a usable form. |
paul@418 | 447 | |
paul@418 | 448 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@418 | 449 | |
paul@418 | 450 | for attendee, attendee_attr in attendees.items(): |
paul@418 | 451 | |
paul@418 | 452 | # Update attendance in the loaded object. |
paul@418 | 453 | |
paul@418 | 454 | attendee_map[attendee] = attendee_attr |
paul@418 | 455 | |
paul@418 | 456 | # Set the new details and store the object. |
paul@418 | 457 | |
paul@418 | 458 | obj["ATTENDEE"] = attendee_map.items() |
paul@418 | 459 | |
paul@418 | 460 | # Set the complete event if not an additional occurrence. |
paul@418 | 461 | |
paul@418 | 462 | event = obj.to_node() |
paul@468 | 463 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@418 | 464 | |
paul@418 | 465 | return True |
paul@418 | 466 | |
paul@418 | 467 | # vim: tabstop=4 expandtab shiftwidth=4 |