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