paul@441 | 1 | #!/usr/bin/env python |
paul@441 | 2 | |
paul@441 | 3 | """ |
paul@441 | 4 | Common calendar client utilities. |
paul@441 | 5 | |
paul@441 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@441 | 7 | |
paul@441 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@441 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@441 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@441 | 11 | version. |
paul@441 | 12 | |
paul@441 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@441 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@441 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@441 | 16 | details. |
paul@441 | 17 | |
paul@441 | 18 | You should have received a copy of the GNU General Public License along with |
paul@441 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@441 | 20 | """ |
paul@441 | 21 | |
paul@740 | 22 | from datetime import datetime, timedelta |
paul@749 | 23 | from imiptools import config |
paul@606 | 24 | from imiptools.data import Object, get_address, get_uri, get_window_end, \ |
paul@606 | 25 | is_new_object, make_freebusy, to_part, \ |
paul@604 | 26 | uri_dict, uri_items, uri_values |
paul@669 | 27 | from imiptools.dates import check_permitted_values, format_datetime, get_default_timezone, \ |
paul@809 | 28 | get_duration, get_timestamp |
paul@606 | 29 | from imiptools.period import can_schedule, remove_period, \ |
paul@606 | 30 | remove_additional_periods, remove_affected_period, \ |
paul@606 | 31 | update_freebusy |
paul@443 | 32 | from imiptools.profile import Preferences |
paul@604 | 33 | import imip_store |
paul@441 | 34 | |
paul@443 | 35 | class Client: |
paul@443 | 36 | |
paul@443 | 37 | "Common handler and manager methods." |
paul@443 | 38 | |
paul@467 | 39 | default_window_size = 100 |
paul@729 | 40 | organiser_methods = "ADD", "CANCEL", "DECLINECOUNTER", "PUBLISH", "REQUEST" |
paul@467 | 41 | |
paul@639 | 42 | def __init__(self, user, messenger=None, store=None, publisher=None, preferences_dir=None): |
paul@728 | 43 | |
paul@728 | 44 | """ |
paul@728 | 45 | Initialise a calendar client with the current 'user', plus any |
paul@728 | 46 | 'messenger', 'store' and 'publisher' objects, indicating any specific |
paul@728 | 47 | 'preferences_dir'. |
paul@728 | 48 | """ |
paul@728 | 49 | |
paul@443 | 50 | self.user = user |
paul@601 | 51 | self.messenger = messenger |
paul@604 | 52 | self.store = store or imip_store.FileStore() |
paul@604 | 53 | |
paul@604 | 54 | try: |
paul@604 | 55 | self.publisher = publisher or imip_store.FilePublisher() |
paul@604 | 56 | except OSError: |
paul@604 | 57 | self.publisher = None |
paul@604 | 58 | |
paul@639 | 59 | self.preferences_dir = preferences_dir |
paul@443 | 60 | self.preferences = None |
paul@443 | 61 | |
paul@730 | 62 | # Store-related methods. |
paul@730 | 63 | |
paul@730 | 64 | def acquire_lock(self): |
paul@730 | 65 | self.store.acquire_lock(self.user) |
paul@730 | 66 | |
paul@730 | 67 | def release_lock(self): |
paul@730 | 68 | self.store.release_lock(self.user) |
paul@730 | 69 | |
paul@730 | 70 | # Preferences-related methods. |
paul@730 | 71 | |
paul@443 | 72 | def get_preferences(self): |
paul@467 | 73 | if not self.preferences and self.user: |
paul@639 | 74 | self.preferences = Preferences(self.user, self.preferences_dir) |
paul@443 | 75 | return self.preferences |
paul@443 | 76 | |
paul@791 | 77 | def get_user_attributes(self): |
paul@791 | 78 | prefs = self.get_preferences() |
paul@791 | 79 | return prefs and prefs.get_all(["CN"]) or {} |
paul@791 | 80 | |
paul@443 | 81 | def get_tzid(self): |
paul@443 | 82 | prefs = self.get_preferences() |
paul@467 | 83 | return prefs and prefs.get("TZID") or get_default_timezone() |
paul@443 | 84 | |
paul@443 | 85 | def get_window_size(self): |
paul@443 | 86 | prefs = self.get_preferences() |
paul@443 | 87 | try: |
paul@467 | 88 | return prefs and int(prefs.get("window_size")) or self.default_window_size |
paul@443 | 89 | except (TypeError, ValueError): |
paul@467 | 90 | return self.default_window_size |
paul@443 | 91 | |
paul@443 | 92 | def get_window_end(self): |
paul@443 | 93 | return get_window_end(self.get_tzid(), self.get_window_size()) |
paul@443 | 94 | |
paul@667 | 95 | def is_participating(self): |
paul@748 | 96 | |
paul@748 | 97 | "Return participation in the calendar system." |
paul@748 | 98 | |
paul@667 | 99 | prefs = self.get_preferences() |
paul@749 | 100 | return prefs and prefs.get("participating", config.PARTICIPATING_DEFAULT) != "no" or False |
paul@667 | 101 | |
paul@443 | 102 | def is_sharing(self): |
paul@748 | 103 | |
paul@748 | 104 | "Return whether free/busy information is being generally shared." |
paul@748 | 105 | |
paul@467 | 106 | prefs = self.get_preferences() |
paul@749 | 107 | return prefs and prefs.get("freebusy_sharing", config.SHARING_DEFAULT) == "share" or False |
paul@443 | 108 | |
paul@443 | 109 | def is_bundling(self): |
paul@748 | 110 | |
paul@748 | 111 | "Return whether free/busy information is being bundled in messages." |
paul@748 | 112 | |
paul@467 | 113 | prefs = self.get_preferences() |
paul@749 | 114 | return prefs and prefs.get("freebusy_bundling", config.BUNDLING_DEFAULT) == "always" or False |
paul@467 | 115 | |
paul@467 | 116 | def is_notifying(self): |
paul@748 | 117 | |
paul@748 | 118 | "Return whether recipients are notified about free/busy payloads." |
paul@748 | 119 | |
paul@467 | 120 | prefs = self.get_preferences() |
paul@749 | 121 | return prefs and prefs.get("freebusy_messages", config.NOTIFYING_DEFAULT) == "notify" or False |
paul@443 | 122 | |
paul@748 | 123 | def is_publishing(self): |
paul@748 | 124 | |
paul@748 | 125 | "Return whether free/busy information is being published as Web resources." |
paul@748 | 126 | |
paul@748 | 127 | prefs = self.get_preferences() |
paul@749 | 128 | return prefs and prefs.get("freebusy_publishing", config.PUBLISHING_DEFAULT) == "publish" or False |
paul@748 | 129 | |
paul@688 | 130 | def is_refreshing(self): |
paul@748 | 131 | |
paul@748 | 132 | "Return whether a recipient supports requests to refresh event details." |
paul@748 | 133 | |
paul@688 | 134 | prefs = self.get_preferences() |
paul@749 | 135 | return prefs and prefs.get("event_refreshing", config.REFRESHING_DEFAULT) == "always" or False |
paul@688 | 136 | |
paul@734 | 137 | def allow_add(self): |
paul@734 | 138 | return self.get_add_method_response() in ("add", "refresh") |
paul@734 | 139 | |
paul@734 | 140 | def get_add_method_response(self): |
paul@728 | 141 | prefs = self.get_preferences() |
paul@749 | 142 | return prefs and prefs.get("add_method_response", config.ADD_RESPONSE_DEFAULT) or "refresh" |
paul@734 | 143 | |
paul@740 | 144 | def get_offer_period(self): |
paul@740 | 145 | |
paul@759 | 146 | "Decode a specification in the iCalendar duration format." |
paul@740 | 147 | |
paul@740 | 148 | prefs = self.get_preferences() |
paul@749 | 149 | duration = prefs and prefs.get("freebusy_offers", config.FREEBUSY_OFFER_DEFAULT) |
paul@740 | 150 | |
paul@759 | 151 | # NOTE: Should probably report an error somehow if None. |
paul@740 | 152 | |
paul@759 | 153 | return duration and get_duration(duration) or None |
paul@740 | 154 | |
paul@734 | 155 | def get_organiser_replacement(self): |
paul@734 | 156 | prefs = self.get_preferences() |
paul@749 | 157 | return prefs and prefs.get("organiser_replacement", config.ORGANISER_REPLACEMENT_DEFAULT) or "attendee" |
paul@728 | 158 | |
paul@668 | 159 | def have_manager(self): |
paul@749 | 160 | return config.MANAGER_INTERFACE |
paul@668 | 161 | |
paul@669 | 162 | def get_permitted_values(self): |
paul@655 | 163 | |
paul@655 | 164 | """ |
paul@655 | 165 | Decode a specification of one of the following forms... |
paul@655 | 166 | |
paul@655 | 167 | <minute values> |
paul@655 | 168 | <hour values>:<minute values> |
paul@655 | 169 | <hour values>:<minute values>:<second values> |
paul@655 | 170 | |
paul@655 | 171 | ...with each list of values being comma-separated. |
paul@655 | 172 | """ |
paul@655 | 173 | |
paul@655 | 174 | prefs = self.get_preferences() |
paul@669 | 175 | permitted_values = prefs and prefs.get("permitted_times") |
paul@669 | 176 | if permitted_values: |
paul@655 | 177 | try: |
paul@655 | 178 | l = [] |
paul@669 | 179 | for component in permitted_values.split(":")[:3]: |
paul@655 | 180 | if component: |
paul@655 | 181 | l.append(map(int, component.split(","))) |
paul@655 | 182 | else: |
paul@655 | 183 | l.append(None) |
paul@655 | 184 | |
paul@655 | 185 | # NOTE: Should probably report an error somehow. |
paul@655 | 186 | |
paul@655 | 187 | except ValueError: |
paul@655 | 188 | return None |
paul@655 | 189 | else: |
paul@655 | 190 | l = (len(l) < 2 and [None] or []) + l + (len(l) < 3 and [None] or []) |
paul@655 | 191 | return l |
paul@655 | 192 | else: |
paul@655 | 193 | return None |
paul@655 | 194 | |
paul@581 | 195 | # Common operations on calendar data. |
paul@581 | 196 | |
paul@619 | 197 | def update_attendees(self, obj, attendees, removed): |
paul@619 | 198 | |
paul@619 | 199 | """ |
paul@619 | 200 | Update the attendees in 'obj' with the given 'attendees' and 'removed' |
paul@619 | 201 | attendee lists. A list is returned containing the attendees whose |
paul@619 | 202 | attendance should be cancelled. |
paul@619 | 203 | """ |
paul@619 | 204 | |
paul@619 | 205 | to_cancel = [] |
paul@619 | 206 | |
paul@619 | 207 | existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) |
paul@619 | 208 | added = set(attendees).difference(existing_attendees) |
paul@619 | 209 | |
paul@619 | 210 | if added or removed: |
paul@619 | 211 | attendees = uri_items(obj.get_items("ATTENDEE") or []) |
paul@619 | 212 | sequence = obj.get_value("SEQUENCE") |
paul@619 | 213 | |
paul@619 | 214 | if removed: |
paul@619 | 215 | remaining = [] |
paul@619 | 216 | |
paul@619 | 217 | for attendee, attendee_attr in attendees: |
paul@619 | 218 | if attendee in removed: |
paul@619 | 219 | |
paul@619 | 220 | # Without a sequence number, assume that the event has not |
paul@619 | 221 | # been published and that attendees can be silently removed. |
paul@619 | 222 | |
paul@619 | 223 | if sequence is not None: |
paul@619 | 224 | to_cancel.append((attendee, attendee_attr)) |
paul@619 | 225 | else: |
paul@619 | 226 | remaining.append((attendee, attendee_attr)) |
paul@619 | 227 | |
paul@619 | 228 | attendees = remaining |
paul@619 | 229 | |
paul@619 | 230 | if added: |
paul@619 | 231 | for attendee in added: |
paul@619 | 232 | attendee = attendee.strip() |
paul@619 | 233 | if attendee: |
paul@619 | 234 | attendees.append((get_uri(attendee), {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) |
paul@619 | 235 | |
paul@619 | 236 | obj["ATTENDEE"] = attendees |
paul@619 | 237 | |
paul@619 | 238 | return to_cancel |
paul@619 | 239 | |
paul@582 | 240 | def update_participation(self, obj, partstat=None): |
paul@581 | 241 | |
paul@581 | 242 | """ |
paul@581 | 243 | Update the participation in 'obj' of the user with the given 'partstat'. |
paul@581 | 244 | """ |
paul@581 | 245 | |
paul@581 | 246 | attendee_attr = uri_dict(obj.get_value_map("ATTENDEE")).get(self.user) |
paul@582 | 247 | if not attendee_attr: |
paul@582 | 248 | return None |
paul@582 | 249 | if partstat: |
paul@582 | 250 | attendee_attr["PARTSTAT"] = partstat |
paul@581 | 251 | if attendee_attr.has_key("RSVP"): |
paul@581 | 252 | del attendee_attr["RSVP"] |
paul@584 | 253 | self.update_sender(attendee_attr) |
paul@581 | 254 | return attendee_attr |
paul@581 | 255 | |
paul@584 | 256 | def update_sender(self, attr): |
paul@584 | 257 | |
paul@584 | 258 | "Update the SENT-BY attribute of the 'attr' sender metadata." |
paul@584 | 259 | |
paul@584 | 260 | if self.messenger and self.messenger.sender != get_address(self.user): |
paul@584 | 261 | attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@584 | 262 | |
paul@606 | 263 | def get_periods(self, obj): |
paul@606 | 264 | |
paul@606 | 265 | """ |
paul@606 | 266 | Return periods for the given 'obj'. Interpretation of periods can depend |
paul@606 | 267 | on the time zone, which is obtained for the current user. |
paul@606 | 268 | """ |
paul@606 | 269 | |
paul@606 | 270 | return obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@606 | 271 | |
paul@606 | 272 | # Store operations. |
paul@606 | 273 | |
paul@766 | 274 | def get_stored_object(self, uid, recurrenceid, section=None, username=None): |
paul@606 | 275 | |
paul@606 | 276 | """ |
paul@606 | 277 | Return the stored object for the current user, with the given 'uid' and |
paul@766 | 278 | 'recurrenceid' from the given 'section' and for the given 'username' (if |
paul@766 | 279 | specified), or from the standard object collection otherwise. |
paul@606 | 280 | """ |
paul@606 | 281 | |
paul@755 | 282 | if section == "counters": |
paul@766 | 283 | fragment = self.store.get_counter(self.user, username, uid, recurrenceid) |
paul@755 | 284 | else: |
paul@755 | 285 | fragment = self.store.get_event(self.user, uid, recurrenceid) |
paul@606 | 286 | return fragment and Object(fragment) |
paul@606 | 287 | |
paul@604 | 288 | # Free/busy operations. |
paul@604 | 289 | |
paul@606 | 290 | def get_freebusy_part(self, freebusy=None): |
paul@604 | 291 | |
paul@604 | 292 | """ |
paul@606 | 293 | Return a message part containing free/busy information for the user, |
paul@606 | 294 | either specified as 'freebusy' or obtained from the store directly. |
paul@604 | 295 | """ |
paul@604 | 296 | |
paul@604 | 297 | if self.is_sharing() and self.is_bundling(): |
paul@604 | 298 | |
paul@604 | 299 | # Invent a unique identifier. |
paul@604 | 300 | |
paul@604 | 301 | utcnow = get_timestamp() |
paul@604 | 302 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@604 | 303 | |
paul@606 | 304 | freebusy = freebusy or self.store.get_freebusy(self.user) |
paul@604 | 305 | |
paul@604 | 306 | user_attr = {} |
paul@604 | 307 | self.update_sender(user_attr) |
paul@604 | 308 | return to_part("PUBLISH", [make_freebusy(freebusy, uid, self.user, user_attr)]) |
paul@604 | 309 | |
paul@604 | 310 | return None |
paul@604 | 311 | |
paul@740 | 312 | def update_freebusy(self, freebusy, periods, transp, uid, recurrenceid, summary, organiser, expires=None): |
paul@606 | 313 | |
paul@606 | 314 | """ |
paul@606 | 315 | Update the 'freebusy' collection with the given 'periods', indicating a |
paul@606 | 316 | 'transp' status, explicit 'uid' and 'recurrenceid' to indicate either a |
paul@606 | 317 | recurrence or the parent event. The 'summary' and 'organiser' must also |
paul@606 | 318 | be provided. |
paul@740 | 319 | |
paul@740 | 320 | An optional 'expires' datetime string can be provided to tag a free/busy |
paul@740 | 321 | offer. |
paul@606 | 322 | """ |
paul@606 | 323 | |
paul@740 | 324 | update_freebusy(freebusy, periods, transp, uid, recurrenceid, summary, organiser, expires) |
paul@606 | 325 | |
paul@601 | 326 | class ClientForObject(Client): |
paul@601 | 327 | |
paul@601 | 328 | "A client maintaining a specific object." |
paul@601 | 329 | |
paul@639 | 330 | def __init__(self, obj, user, messenger=None, store=None, publisher=None, preferences_dir=None): |
paul@639 | 331 | Client.__init__(self, user, messenger, store, publisher, preferences_dir) |
paul@601 | 332 | self.set_object(obj) |
paul@601 | 333 | |
paul@601 | 334 | def set_object(self, obj): |
paul@606 | 335 | |
paul@606 | 336 | "Set the current object to 'obj', obtaining metadata details." |
paul@606 | 337 | |
paul@601 | 338 | self.obj = obj |
paul@601 | 339 | self.uid = obj and self.obj.get_uid() |
paul@601 | 340 | self.recurrenceid = obj and self.obj.get_recurrenceid() |
paul@601 | 341 | self.sequence = obj and self.obj.get_value("SEQUENCE") |
paul@601 | 342 | self.dtstamp = obj and self.obj.get_value("DTSTAMP") |
paul@601 | 343 | |
paul@729 | 344 | def set_identity(self, method): |
paul@729 | 345 | |
paul@729 | 346 | """ |
paul@729 | 347 | Set the current user for the current object in the context of the given |
paul@729 | 348 | 'method'. It is usually set when initialising the handler, using the |
paul@729 | 349 | recipient details, but outgoing messages do not reference the recipient |
paul@729 | 350 | in this way. |
paul@729 | 351 | """ |
paul@729 | 352 | |
paul@729 | 353 | pass |
paul@729 | 354 | |
paul@727 | 355 | def is_usable(self, method=None): |
paul@720 | 356 | |
paul@727 | 357 | "Return whether the current object is usable with the given 'method'." |
paul@720 | 358 | |
paul@720 | 359 | return True |
paul@720 | 360 | |
paul@604 | 361 | # Object update methods. |
paul@601 | 362 | |
paul@676 | 363 | def update_recurrenceid(self): |
paul@676 | 364 | |
paul@676 | 365 | """ |
paul@676 | 366 | Update the RECURRENCE-ID in the current object, initialising it from |
paul@676 | 367 | DTSTART. |
paul@676 | 368 | """ |
paul@676 | 369 | |
paul@680 | 370 | self.obj["RECURRENCE-ID"] = [self.obj.get_item("DTSTART")] |
paul@676 | 371 | self.recurrenceid = self.obj.get_recurrenceid() |
paul@676 | 372 | |
paul@809 | 373 | def update_dtstamp(self, obj=None): |
paul@601 | 374 | |
paul@809 | 375 | "Update the DTSTAMP in the current object or any given object 'obj'." |
paul@809 | 376 | |
paul@809 | 377 | obj = obj or self.obj |
paul@809 | 378 | self.dtstamp = obj.update_dtstamp() |
paul@601 | 379 | |
paul@809 | 380 | def update_sequence(self, increment=False, obj=None): |
paul@601 | 381 | |
paul@809 | 382 | "Update the SEQUENCE in the current object or any given object 'obj'." |
paul@601 | 383 | |
paul@809 | 384 | obj = obj or self.obj |
paul@809 | 385 | obj.update_sequence(increment) |
paul@601 | 386 | |
paul@606 | 387 | def merge_attendance(self, attendees): |
paul@606 | 388 | |
paul@606 | 389 | """ |
paul@606 | 390 | Merge attendance from the current object's 'attendees' into the version |
paul@606 | 391 | stored for the current user. |
paul@606 | 392 | """ |
paul@606 | 393 | |
paul@606 | 394 | obj = self.get_stored_object_version() |
paul@606 | 395 | |
paul@739 | 396 | if not obj or not self.have_new_object(): |
paul@606 | 397 | return False |
paul@606 | 398 | |
paul@606 | 399 | # Get attendee details in a usable form. |
paul@606 | 400 | |
paul@606 | 401 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@606 | 402 | |
paul@606 | 403 | for attendee, attendee_attr in attendees.items(): |
paul@606 | 404 | |
paul@606 | 405 | # Update attendance in the loaded object. |
paul@606 | 406 | |
paul@606 | 407 | attendee_map[attendee] = attendee_attr |
paul@606 | 408 | |
paul@606 | 409 | # Set the new details and store the object. |
paul@606 | 410 | |
paul@606 | 411 | obj["ATTENDEE"] = attendee_map.items() |
paul@606 | 412 | |
paul@744 | 413 | # Set a specific recurrence or the complete event if not an additional |
paul@744 | 414 | # occurrence. |
paul@606 | 415 | |
paul@736 | 416 | self.store.set_event(self.user, self.uid, self.recurrenceid, obj.to_node()) |
paul@606 | 417 | |
paul@606 | 418 | return True |
paul@606 | 419 | |
paul@606 | 420 | # Object-related tests. |
paul@606 | 421 | |
paul@728 | 422 | def is_recognised_organiser(self, organiser): |
paul@728 | 423 | |
paul@728 | 424 | """ |
paul@728 | 425 | Return whether the given 'organiser' is recognised from |
paul@728 | 426 | previously-received details. If no stored details exist, True is |
paul@728 | 427 | returned. |
paul@728 | 428 | """ |
paul@728 | 429 | |
paul@728 | 430 | obj = self.get_stored_object_version() |
paul@728 | 431 | if obj: |
paul@728 | 432 | stored_organiser = get_uri(obj.get_value("ORGANIZER")) |
paul@728 | 433 | return stored_organiser == organiser |
paul@728 | 434 | else: |
paul@728 | 435 | return True |
paul@728 | 436 | |
paul@728 | 437 | def is_recognised_attendee(self, attendee): |
paul@728 | 438 | |
paul@728 | 439 | """ |
paul@728 | 440 | Return whether the given 'attendee' is recognised from |
paul@728 | 441 | previously-received details. If no stored details exist, True is |
paul@728 | 442 | returned. |
paul@728 | 443 | """ |
paul@728 | 444 | |
paul@728 | 445 | obj = self.get_stored_object_version() |
paul@728 | 446 | if obj: |
paul@728 | 447 | stored_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@728 | 448 | return stored_attendees.has_key(attendee) |
paul@728 | 449 | else: |
paul@728 | 450 | return True |
paul@728 | 451 | |
paul@694 | 452 | def get_attendance(self, user=None, obj=None): |
paul@606 | 453 | |
paul@606 | 454 | """ |
paul@606 | 455 | Return the attendance attributes for 'user', or the current user if |
paul@606 | 456 | 'user' is not specified. |
paul@606 | 457 | """ |
paul@606 | 458 | |
paul@694 | 459 | attendees = uri_dict((obj or self.obj).get_value_map("ATTENDEE")) |
paul@697 | 460 | return attendees.get(user or self.user) |
paul@606 | 461 | |
paul@694 | 462 | def is_participating(self, user, as_organiser=False, obj=None): |
paul@609 | 463 | |
paul@609 | 464 | """ |
paul@609 | 465 | Return whether, subject to the 'user' indicating an identity and the |
paul@609 | 466 | 'as_organiser' status of that identity, the user concerned is actually |
paul@609 | 467 | participating in the current object event. |
paul@609 | 468 | """ |
paul@609 | 469 | |
paul@697 | 470 | # Use any attendee property information for an organiser, not the |
paul@697 | 471 | # organiser property attributes. |
paul@697 | 472 | |
paul@694 | 473 | attr = self.get_attendance(user, obj=obj) |
paul@697 | 474 | return as_organiser or attr is not None and not attr or attr and attr.get("PARTSTAT") != "DECLINED" |
paul@609 | 475 | |
paul@609 | 476 | def get_overriding_transparency(self, user, as_organiser=False): |
paul@609 | 477 | |
paul@609 | 478 | """ |
paul@609 | 479 | Return the overriding transparency to be associated with the free/busy |
paul@609 | 480 | records for an event, subject to the 'user' indicating an identity and |
paul@609 | 481 | the 'as_organiser' status of that identity. |
paul@609 | 482 | |
paul@609 | 483 | Where an identity is only an organiser and not attending, "ORG" is |
paul@609 | 484 | returned. Otherwise, no overriding transparency is defined and None is |
paul@609 | 485 | returned. |
paul@609 | 486 | """ |
paul@609 | 487 | |
paul@609 | 488 | attr = self.get_attendance(user) |
paul@609 | 489 | return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None |
paul@609 | 490 | |
paul@606 | 491 | def can_schedule(self, freebusy, periods): |
paul@606 | 492 | |
paul@606 | 493 | """ |
paul@606 | 494 | Indicate whether within 'freebusy' the given 'periods' can be scheduled. |
paul@606 | 495 | """ |
paul@606 | 496 | |
paul@606 | 497 | return can_schedule(freebusy, periods, self.uid, self.recurrenceid) |
paul@606 | 498 | |
paul@739 | 499 | def have_new_object(self, strict=True): |
paul@606 | 500 | |
paul@606 | 501 | """ |
paul@739 | 502 | Return whether the current object is new to the current user. |
paul@739 | 503 | |
paul@739 | 504 | If 'strict' is specified and is a false value, the DTSTAMP test will be |
paul@739 | 505 | ignored. This is useful in handling responses from attendees from |
paul@739 | 506 | clients (like Claws Mail) that erase time information from DTSTAMP and |
paul@739 | 507 | make it invalid. |
paul@606 | 508 | """ |
paul@606 | 509 | |
paul@739 | 510 | obj = self.get_stored_object_version() |
paul@606 | 511 | |
paul@606 | 512 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@606 | 513 | |
paul@606 | 514 | if obj: |
paul@606 | 515 | sequence = obj.get_value("SEQUENCE") |
paul@606 | 516 | dtstamp = obj.get_value("DTSTAMP") |
paul@606 | 517 | |
paul@606 | 518 | # If the request refers to an older version of the object, ignore |
paul@606 | 519 | # it. |
paul@606 | 520 | |
paul@682 | 521 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, not strict) |
paul@606 | 522 | |
paul@606 | 523 | return True |
paul@606 | 524 | |
paul@672 | 525 | def possibly_recurring_indefinitely(self): |
paul@672 | 526 | |
paul@672 | 527 | "Return whether the object recurs indefinitely." |
paul@672 | 528 | |
paul@672 | 529 | # Obtain the stored object to make sure that recurrence information |
paul@672 | 530 | # is not being ignored. This might happen if a client sends a |
paul@672 | 531 | # cancellation without the complete set of properties, for instance. |
paul@672 | 532 | |
paul@672 | 533 | return self.obj.possibly_recurring_indefinitely() or \ |
paul@672 | 534 | self.get_stored_object_version() and \ |
paul@672 | 535 | self.get_stored_object_version().possibly_recurring_indefinitely() |
paul@672 | 536 | |
paul@655 | 537 | # Constraint application on event periods. |
paul@655 | 538 | |
paul@655 | 539 | def check_object(self): |
paul@655 | 540 | |
paul@655 | 541 | "Check the object against any scheduling constraints." |
paul@655 | 542 | |
paul@669 | 543 | permitted_values = self.get_permitted_values() |
paul@669 | 544 | if not permitted_values: |
paul@655 | 545 | return None |
paul@655 | 546 | |
paul@655 | 547 | invalid = [] |
paul@655 | 548 | |
paul@660 | 549 | for period in self.obj.get_periods(self.get_tzid()): |
paul@655 | 550 | start = period.get_start() |
paul@655 | 551 | end = period.get_end() |
paul@669 | 552 | start_errors = check_permitted_values(start, permitted_values) |
paul@669 | 553 | end_errors = check_permitted_values(end, permitted_values) |
paul@656 | 554 | if start_errors or end_errors: |
paul@656 | 555 | invalid.append((period.origin, start_errors, end_errors)) |
paul@655 | 556 | |
paul@655 | 557 | return invalid |
paul@655 | 558 | |
paul@660 | 559 | def correct_object(self): |
paul@655 | 560 | |
paul@660 | 561 | "Correct the object according to any scheduling constraints." |
paul@655 | 562 | |
paul@669 | 563 | permitted_values = self.get_permitted_values() |
paul@669 | 564 | return permitted_values and self.obj.correct_object(self.get_tzid(), permitted_values) |
paul@655 | 565 | |
paul@606 | 566 | # Object retrieval. |
paul@606 | 567 | |
paul@606 | 568 | def get_stored_object_version(self): |
paul@606 | 569 | |
paul@606 | 570 | """ |
paul@606 | 571 | Return the stored object to which the current object refers for the |
paul@606 | 572 | current user. |
paul@606 | 573 | """ |
paul@606 | 574 | |
paul@606 | 575 | return self.get_stored_object(self.uid, self.recurrenceid) |
paul@606 | 576 | |
paul@704 | 577 | def get_definitive_object(self, as_organiser): |
paul@606 | 578 | |
paul@606 | 579 | """ |
paul@606 | 580 | Return an object considered definitive for the current transaction, |
paul@704 | 581 | using 'as_organiser' to select the current transaction's object if |
paul@704 | 582 | false, or selecting a stored object if true. |
paul@606 | 583 | """ |
paul@606 | 584 | |
paul@704 | 585 | return not as_organiser and self.obj or self.get_stored_object_version() |
paul@606 | 586 | |
paul@606 | 587 | def get_parent_object(self): |
paul@606 | 588 | |
paul@606 | 589 | """ |
paul@606 | 590 | Return the parent object to which the current object refers for the |
paul@606 | 591 | current user. |
paul@606 | 592 | """ |
paul@606 | 593 | |
paul@606 | 594 | return self.recurrenceid and self.get_stored_object(self.uid, None) or None |
paul@606 | 595 | |
paul@606 | 596 | # Convenience methods for modifying free/busy collections. |
paul@606 | 597 | |
paul@606 | 598 | def get_recurrence_start_point(self, recurrenceid): |
paul@606 | 599 | |
paul@606 | 600 | "Get 'recurrenceid' in a form suitable for matching free/busy entries." |
paul@606 | 601 | |
paul@627 | 602 | return self.obj.get_recurrence_start_point(recurrenceid, self.get_tzid()) |
paul@606 | 603 | |
paul@606 | 604 | def remove_from_freebusy(self, freebusy): |
paul@606 | 605 | |
paul@606 | 606 | "Remove this event from the given 'freebusy' collection." |
paul@606 | 607 | |
paul@606 | 608 | if not remove_period(freebusy, self.uid, self.recurrenceid) and self.recurrenceid: |
paul@606 | 609 | remove_affected_period(freebusy, self.uid, self.get_recurrence_start_point(self.recurrenceid)) |
paul@606 | 610 | |
paul@606 | 611 | def remove_freebusy_for_recurrences(self, freebusy, recurrenceids=None): |
paul@606 | 612 | |
paul@606 | 613 | """ |
paul@606 | 614 | Remove from 'freebusy' any original recurrence from parent free/busy |
paul@606 | 615 | details for the current object, if the current object is a specific |
paul@606 | 616 | additional recurrence. Otherwise, remove all additional recurrence |
paul@606 | 617 | information corresponding to 'recurrenceids', or if omitted, all |
paul@606 | 618 | recurrences. |
paul@606 | 619 | """ |
paul@606 | 620 | |
paul@606 | 621 | if self.recurrenceid: |
paul@606 | 622 | recurrenceid = self.get_recurrence_start_point(self.recurrenceid) |
paul@606 | 623 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@606 | 624 | else: |
paul@606 | 625 | # Remove obsolete recurrence periods. |
paul@606 | 626 | |
paul@606 | 627 | remove_additional_periods(freebusy, self.uid, recurrenceids) |
paul@606 | 628 | |
paul@606 | 629 | # Remove original periods affected by additional recurrences. |
paul@606 | 630 | |
paul@606 | 631 | if recurrenceids: |
paul@606 | 632 | for recurrenceid in recurrenceids: |
paul@606 | 633 | recurrenceid = self.get_recurrence_start_point(recurrenceid) |
paul@606 | 634 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@606 | 635 | |
paul@740 | 636 | def update_freebusy(self, freebusy, user, as_organiser, offer=False): |
paul@606 | 637 | |
paul@606 | 638 | """ |
paul@606 | 639 | Update the 'freebusy' collection for this event with the periods and |
paul@606 | 640 | transparency associated with the current object, subject to the 'user' |
paul@606 | 641 | identity and the attendance details provided for them, indicating |
paul@704 | 642 | whether the update is being done 'as_organiser' (for the organiser of |
paul@676 | 643 | an event) or not. |
paul@740 | 644 | |
paul@740 | 645 | If 'offer' is set to a true value, any free/busy updates will be tagged |
paul@740 | 646 | with an expiry time. |
paul@606 | 647 | """ |
paul@606 | 648 | |
paul@606 | 649 | # Obtain the stored object if the current object is not issued by the |
paul@606 | 650 | # organiser. Attendees do not have the opportunity to redefine the |
paul@606 | 651 | # periods. |
paul@606 | 652 | |
paul@704 | 653 | obj = self.get_definitive_object(as_organiser) |
paul@606 | 654 | if not obj: |
paul@606 | 655 | return |
paul@606 | 656 | |
paul@606 | 657 | # Obtain the affected periods. |
paul@606 | 658 | |
paul@606 | 659 | periods = self.get_periods(obj) |
paul@606 | 660 | |
paul@606 | 661 | # Define an overriding transparency, the indicated event transparency, |
paul@606 | 662 | # or the default transparency for the free/busy entry. |
paul@606 | 663 | |
paul@704 | 664 | transp = self.get_overriding_transparency(user, as_organiser) or \ |
paul@606 | 665 | obj.get_value("TRANSP") or \ |
paul@606 | 666 | "OPAQUE" |
paul@606 | 667 | |
paul@740 | 668 | # Calculate any expiry time. If no offer period is defined, do not |
paul@740 | 669 | # record the offer periods. |
paul@740 | 670 | |
paul@740 | 671 | if offer: |
paul@740 | 672 | offer_period = self.get_offer_period() |
paul@740 | 673 | if offer_period: |
paul@759 | 674 | expires = get_timestamp(offer_period) |
paul@740 | 675 | else: |
paul@740 | 676 | return |
paul@740 | 677 | else: |
paul@740 | 678 | expires = None |
paul@740 | 679 | |
paul@606 | 680 | # Perform the low-level update. |
paul@606 | 681 | |
paul@606 | 682 | Client.update_freebusy(self, freebusy, periods, transp, |
paul@606 | 683 | self.uid, self.recurrenceid, |
paul@606 | 684 | obj.get_value("SUMMARY"), |
paul@740 | 685 | obj.get_value("ORGANIZER"), |
paul@740 | 686 | expires) |
paul@606 | 687 | |
paul@606 | 688 | def update_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@740 | 689 | updating_other=False, offer=False): |
paul@606 | 690 | |
paul@606 | 691 | """ |
paul@695 | 692 | Update the 'freebusy' collection for the given 'user', indicating |
paul@695 | 693 | whether the update is 'for_organiser' (being done for the organiser of |
paul@695 | 694 | an event) or not, and whether it is 'updating_other' (meaning another |
paul@695 | 695 | user's details). |
paul@740 | 696 | |
paul@740 | 697 | If 'offer' is set to a true value, any free/busy updates will be tagged |
paul@740 | 698 | with an expiry time. |
paul@606 | 699 | """ |
paul@606 | 700 | |
paul@606 | 701 | # Record in the free/busy details unless a non-participating attendee. |
paul@697 | 702 | # Remove periods for non-participating attendees. |
paul@606 | 703 | |
paul@744 | 704 | if offer or self.is_participating(user, for_organiser and not updating_other): |
paul@704 | 705 | self.update_freebusy(freebusy, user, |
paul@704 | 706 | for_organiser and not updating_other or |
paul@740 | 707 | not for_organiser and updating_other, |
paul@740 | 708 | offer |
paul@704 | 709 | ) |
paul@606 | 710 | else: |
paul@606 | 711 | self.remove_from_freebusy(freebusy) |
paul@606 | 712 | |
paul@697 | 713 | def remove_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@697 | 714 | updating_other=False): |
paul@697 | 715 | |
paul@697 | 716 | """ |
paul@697 | 717 | Remove details from the 'freebusy' collection for the given 'user', |
paul@697 | 718 | indicating whether the modification is 'for_organiser' (being done for |
paul@697 | 719 | the organiser of an event) or not, and whether it is 'updating_other' |
paul@697 | 720 | (meaning another user's details). |
paul@697 | 721 | """ |
paul@697 | 722 | |
paul@697 | 723 | # Remove from the free/busy details if a specified attendee. |
paul@697 | 724 | |
paul@697 | 725 | if self.is_participating(user, for_organiser and not updating_other): |
paul@697 | 726 | self.remove_from_freebusy(freebusy) |
paul@697 | 727 | |
paul@606 | 728 | # Convenience methods for updating stored free/busy information received |
paul@606 | 729 | # from other users. |
paul@606 | 730 | |
paul@697 | 731 | def update_freebusy_from_participant(self, user, for_organiser, fn=None): |
paul@606 | 732 | |
paul@606 | 733 | """ |
paul@606 | 734 | For the current user, record the free/busy information for another |
paul@606 | 735 | 'user', indicating whether the update is 'for_organiser' or not, thus |
paul@606 | 736 | maintaining a separate record of their free/busy details. |
paul@606 | 737 | """ |
paul@606 | 738 | |
paul@697 | 739 | fn = fn or self.update_freebusy_for_participant |
paul@697 | 740 | |
paul@606 | 741 | # A user does not store free/busy information for themself as another |
paul@606 | 742 | # party. |
paul@606 | 743 | |
paul@606 | 744 | if user == self.user: |
paul@606 | 745 | return |
paul@606 | 746 | |
paul@730 | 747 | self.acquire_lock() |
paul@702 | 748 | try: |
paul@730 | 749 | freebusy = self.store.get_freebusy_for_other(self.user, user) |
paul@702 | 750 | fn(freebusy, user, for_organiser, True) |
paul@702 | 751 | |
paul@702 | 752 | # Tidy up any obsolete recurrences. |
paul@606 | 753 | |
paul@702 | 754 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@730 | 755 | self.store.set_freebusy_for_other(self.user, freebusy, user) |
paul@606 | 756 | |
paul@702 | 757 | finally: |
paul@730 | 758 | self.release_lock() |
paul@606 | 759 | |
paul@606 | 760 | def update_freebusy_from_organiser(self, organiser): |
paul@606 | 761 | |
paul@606 | 762 | "For the current user, record free/busy information from 'organiser'." |
paul@606 | 763 | |
paul@606 | 764 | self.update_freebusy_from_participant(organiser, True) |
paul@606 | 765 | |
paul@606 | 766 | def update_freebusy_from_attendees(self, attendees): |
paul@606 | 767 | |
paul@606 | 768 | "For the current user, record free/busy information from 'attendees'." |
paul@606 | 769 | |
paul@606 | 770 | for attendee in attendees.keys(): |
paul@606 | 771 | self.update_freebusy_from_participant(attendee, False) |
paul@606 | 772 | |
paul@697 | 773 | def remove_freebusy_from_organiser(self, organiser): |
paul@697 | 774 | |
paul@697 | 775 | "For the current user, remove free/busy information from 'organiser'." |
paul@697 | 776 | |
paul@697 | 777 | self.update_freebusy_from_participant(organiser, True, self.remove_freebusy_for_participant) |
paul@697 | 778 | |
paul@697 | 779 | def remove_freebusy_from_attendees(self, attendees): |
paul@697 | 780 | |
paul@697 | 781 | "For the current user, remove free/busy information from 'attendees'." |
paul@697 | 782 | |
paul@697 | 783 | for attendee in attendees.keys(): |
paul@697 | 784 | self.update_freebusy_from_participant(attendee, False, self.remove_freebusy_for_participant) |
paul@697 | 785 | |
paul@756 | 786 | # Convenience methods for updating free/busy details at the event level. |
paul@756 | 787 | |
paul@756 | 788 | def update_event_in_freebusy(self, for_organiser=True): |
paul@756 | 789 | |
paul@756 | 790 | """ |
paul@756 | 791 | Update free/busy information when handling an object, doing so for the |
paul@756 | 792 | organiser of an event if 'for_organiser' is set to a true value. |
paul@756 | 793 | """ |
paul@756 | 794 | |
paul@756 | 795 | freebusy = self.store.get_freebusy(self.user) |
paul@756 | 796 | |
paul@756 | 797 | # Obtain the attendance attributes for this user, if available. |
paul@756 | 798 | |
paul@756 | 799 | self.update_freebusy_for_participant(freebusy, self.user, for_organiser) |
paul@756 | 800 | |
paul@756 | 801 | # Remove original recurrence details replaced by additional |
paul@756 | 802 | # recurrences, as well as obsolete additional recurrences. |
paul@756 | 803 | |
paul@756 | 804 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@756 | 805 | self.store.set_freebusy(self.user, freebusy) |
paul@756 | 806 | |
paul@756 | 807 | if self.publisher and self.is_sharing() and self.is_publishing(): |
paul@756 | 808 | self.publisher.set_freebusy(self.user, freebusy) |
paul@756 | 809 | |
paul@756 | 810 | # Update free/busy provider information if the event may recur |
paul@756 | 811 | # indefinitely. |
paul@756 | 812 | |
paul@756 | 813 | if self.possibly_recurring_indefinitely(): |
paul@756 | 814 | self.store.append_freebusy_provider(self.user, self.obj) |
paul@756 | 815 | |
paul@756 | 816 | return True |
paul@756 | 817 | |
paul@756 | 818 | def remove_event_from_freebusy(self): |
paul@756 | 819 | |
paul@756 | 820 | "Remove free/busy information when handling an object." |
paul@756 | 821 | |
paul@756 | 822 | freebusy = self.store.get_freebusy(self.user) |
paul@756 | 823 | |
paul@756 | 824 | self.remove_from_freebusy(freebusy) |
paul@756 | 825 | self.remove_freebusy_for_recurrences(freebusy) |
paul@756 | 826 | self.store.set_freebusy(self.user, freebusy) |
paul@756 | 827 | |
paul@756 | 828 | if self.publisher and self.is_sharing() and self.is_publishing(): |
paul@756 | 829 | self.publisher.set_freebusy(self.user, freebusy) |
paul@756 | 830 | |
paul@756 | 831 | # Update free/busy provider information if the event may recur |
paul@756 | 832 | # indefinitely. |
paul@756 | 833 | |
paul@756 | 834 | if self.possibly_recurring_indefinitely(): |
paul@756 | 835 | self.store.remove_freebusy_provider(self.user, self.obj) |
paul@756 | 836 | |
paul@756 | 837 | def update_event_in_freebusy_offers(self): |
paul@756 | 838 | |
paul@756 | 839 | "Update free/busy offers when handling an object." |
paul@756 | 840 | |
paul@756 | 841 | freebusy = self.store.get_freebusy_offers(self.user) |
paul@756 | 842 | |
paul@756 | 843 | # Obtain the attendance attributes for this user, if available. |
paul@756 | 844 | |
paul@756 | 845 | self.update_freebusy_for_participant(freebusy, self.user, offer=True) |
paul@756 | 846 | |
paul@756 | 847 | # Remove original recurrence details replaced by additional |
paul@756 | 848 | # recurrences, as well as obsolete additional recurrences. |
paul@756 | 849 | |
paul@756 | 850 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@756 | 851 | self.store.set_freebusy_offers(self.user, freebusy) |
paul@756 | 852 | |
paul@756 | 853 | return True |
paul@756 | 854 | |
paul@756 | 855 | def remove_event_from_freebusy_offers(self): |
paul@756 | 856 | |
paul@756 | 857 | "Remove free/busy offers when handling an object." |
paul@756 | 858 | |
paul@756 | 859 | freebusy = self.store.get_freebusy_offers(self.user) |
paul@756 | 860 | |
paul@756 | 861 | self.remove_from_freebusy(freebusy) |
paul@756 | 862 | self.remove_freebusy_for_recurrences(freebusy) |
paul@756 | 863 | self.store.set_freebusy_offers(self.user, freebusy) |
paul@756 | 864 | |
paul@756 | 865 | return True |
paul@756 | 866 | |
paul@809 | 867 | # Convenience methods for removing counter-proposals and updating the |
paul@809 | 868 | # request queue. |
paul@809 | 869 | |
paul@809 | 870 | def remove_counter(self, attendee): |
paul@809 | 871 | self.remove_counters([attendee]) |
paul@809 | 872 | |
paul@809 | 873 | def remove_counters(self, attendees): |
paul@809 | 874 | for attendee in attendees: |
paul@809 | 875 | self.store.remove_counter(self.user, attendee, self.uid, self.recurrenceid) |
paul@809 | 876 | |
paul@809 | 877 | if not self.store.get_counters(self.user, self.uid, self.recurrenceid): |
paul@809 | 878 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@809 | 879 | |
paul@441 | 880 | # vim: tabstop=4 expandtab shiftwidth=4 |