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@601 | 22 | from datetime import datetime |
paul@668 | 23 | from imiptools.config import MANAGER_INTERFACE |
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@627 | 28 | get_timestamp, to_timezone |
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@443 | 77 | def get_tzid(self): |
paul@443 | 78 | prefs = self.get_preferences() |
paul@467 | 79 | return prefs and prefs.get("TZID") or get_default_timezone() |
paul@443 | 80 | |
paul@443 | 81 | def get_window_size(self): |
paul@443 | 82 | prefs = self.get_preferences() |
paul@443 | 83 | try: |
paul@467 | 84 | return prefs and int(prefs.get("window_size")) or self.default_window_size |
paul@443 | 85 | except (TypeError, ValueError): |
paul@467 | 86 | return self.default_window_size |
paul@443 | 87 | |
paul@443 | 88 | def get_window_end(self): |
paul@443 | 89 | return get_window_end(self.get_tzid(), self.get_window_size()) |
paul@443 | 90 | |
paul@667 | 91 | def is_participating(self): |
paul@667 | 92 | prefs = self.get_preferences() |
paul@667 | 93 | return prefs and prefs.get("participating", "participate") != "no" or False |
paul@667 | 94 | |
paul@443 | 95 | def is_sharing(self): |
paul@467 | 96 | prefs = self.get_preferences() |
paul@467 | 97 | return prefs and prefs.get("freebusy_sharing") == "share" or False |
paul@443 | 98 | |
paul@443 | 99 | def is_bundling(self): |
paul@467 | 100 | prefs = self.get_preferences() |
paul@467 | 101 | return prefs and prefs.get("freebusy_bundling") == "always" or False |
paul@467 | 102 | |
paul@467 | 103 | def is_notifying(self): |
paul@467 | 104 | prefs = self.get_preferences() |
paul@467 | 105 | return prefs and prefs.get("freebusy_messages") == "notify" or False |
paul@443 | 106 | |
paul@688 | 107 | def is_refreshing(self): |
paul@688 | 108 | prefs = self.get_preferences() |
paul@688 | 109 | return prefs and prefs.get("event_refreshing") == "always" or False |
paul@688 | 110 | |
paul@734 | 111 | def allow_add(self): |
paul@734 | 112 | return self.get_add_method_response() in ("add", "refresh") |
paul@734 | 113 | |
paul@734 | 114 | def get_add_method_response(self): |
paul@728 | 115 | prefs = self.get_preferences() |
paul@734 | 116 | return prefs and prefs.get("add_method_response", "refresh") or "refresh" |
paul@734 | 117 | |
paul@734 | 118 | def get_organiser_replacement(self): |
paul@734 | 119 | prefs = self.get_preferences() |
paul@734 | 120 | return prefs and prefs.get("organiser_replacement", "attendee") or "attendee" |
paul@728 | 121 | |
paul@668 | 122 | def have_manager(self): |
paul@668 | 123 | return MANAGER_INTERFACE |
paul@668 | 124 | |
paul@669 | 125 | def get_permitted_values(self): |
paul@655 | 126 | |
paul@655 | 127 | """ |
paul@655 | 128 | Decode a specification of one of the following forms... |
paul@655 | 129 | |
paul@655 | 130 | <minute values> |
paul@655 | 131 | <hour values>:<minute values> |
paul@655 | 132 | <hour values>:<minute values>:<second values> |
paul@655 | 133 | |
paul@655 | 134 | ...with each list of values being comma-separated. |
paul@655 | 135 | """ |
paul@655 | 136 | |
paul@655 | 137 | prefs = self.get_preferences() |
paul@669 | 138 | permitted_values = prefs and prefs.get("permitted_times") |
paul@669 | 139 | if permitted_values: |
paul@655 | 140 | try: |
paul@655 | 141 | l = [] |
paul@669 | 142 | for component in permitted_values.split(":")[:3]: |
paul@655 | 143 | if component: |
paul@655 | 144 | l.append(map(int, component.split(","))) |
paul@655 | 145 | else: |
paul@655 | 146 | l.append(None) |
paul@655 | 147 | |
paul@655 | 148 | # NOTE: Should probably report an error somehow. |
paul@655 | 149 | |
paul@655 | 150 | except ValueError: |
paul@655 | 151 | return None |
paul@655 | 152 | else: |
paul@655 | 153 | l = (len(l) < 2 and [None] or []) + l + (len(l) < 3 and [None] or []) |
paul@655 | 154 | return l |
paul@655 | 155 | else: |
paul@655 | 156 | return None |
paul@655 | 157 | |
paul@581 | 158 | # Common operations on calendar data. |
paul@581 | 159 | |
paul@619 | 160 | def update_attendees(self, obj, attendees, removed): |
paul@619 | 161 | |
paul@619 | 162 | """ |
paul@619 | 163 | Update the attendees in 'obj' with the given 'attendees' and 'removed' |
paul@619 | 164 | attendee lists. A list is returned containing the attendees whose |
paul@619 | 165 | attendance should be cancelled. |
paul@619 | 166 | """ |
paul@619 | 167 | |
paul@619 | 168 | to_cancel = [] |
paul@619 | 169 | |
paul@619 | 170 | existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) |
paul@619 | 171 | added = set(attendees).difference(existing_attendees) |
paul@619 | 172 | |
paul@619 | 173 | if added or removed: |
paul@619 | 174 | attendees = uri_items(obj.get_items("ATTENDEE") or []) |
paul@619 | 175 | sequence = obj.get_value("SEQUENCE") |
paul@619 | 176 | |
paul@619 | 177 | if removed: |
paul@619 | 178 | remaining = [] |
paul@619 | 179 | |
paul@619 | 180 | for attendee, attendee_attr in attendees: |
paul@619 | 181 | if attendee in removed: |
paul@619 | 182 | |
paul@619 | 183 | # Without a sequence number, assume that the event has not |
paul@619 | 184 | # been published and that attendees can be silently removed. |
paul@619 | 185 | |
paul@619 | 186 | if sequence is not None: |
paul@619 | 187 | to_cancel.append((attendee, attendee_attr)) |
paul@619 | 188 | else: |
paul@619 | 189 | remaining.append((attendee, attendee_attr)) |
paul@619 | 190 | |
paul@619 | 191 | attendees = remaining |
paul@619 | 192 | |
paul@619 | 193 | if added: |
paul@619 | 194 | for attendee in added: |
paul@619 | 195 | attendee = attendee.strip() |
paul@619 | 196 | if attendee: |
paul@619 | 197 | attendees.append((get_uri(attendee), {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) |
paul@619 | 198 | |
paul@619 | 199 | obj["ATTENDEE"] = attendees |
paul@619 | 200 | |
paul@619 | 201 | return to_cancel |
paul@619 | 202 | |
paul@582 | 203 | def update_participation(self, obj, partstat=None): |
paul@581 | 204 | |
paul@581 | 205 | """ |
paul@581 | 206 | Update the participation in 'obj' of the user with the given 'partstat'. |
paul@581 | 207 | """ |
paul@581 | 208 | |
paul@581 | 209 | attendee_attr = uri_dict(obj.get_value_map("ATTENDEE")).get(self.user) |
paul@582 | 210 | if not attendee_attr: |
paul@582 | 211 | return None |
paul@582 | 212 | if partstat: |
paul@582 | 213 | attendee_attr["PARTSTAT"] = partstat |
paul@581 | 214 | if attendee_attr.has_key("RSVP"): |
paul@581 | 215 | del attendee_attr["RSVP"] |
paul@584 | 216 | self.update_sender(attendee_attr) |
paul@581 | 217 | return attendee_attr |
paul@581 | 218 | |
paul@584 | 219 | def update_sender(self, attr): |
paul@584 | 220 | |
paul@584 | 221 | "Update the SENT-BY attribute of the 'attr' sender metadata." |
paul@584 | 222 | |
paul@584 | 223 | if self.messenger and self.messenger.sender != get_address(self.user): |
paul@584 | 224 | attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@584 | 225 | |
paul@606 | 226 | def get_periods(self, obj): |
paul@606 | 227 | |
paul@606 | 228 | """ |
paul@606 | 229 | Return periods for the given 'obj'. Interpretation of periods can depend |
paul@606 | 230 | on the time zone, which is obtained for the current user. |
paul@606 | 231 | """ |
paul@606 | 232 | |
paul@606 | 233 | return obj.get_periods(self.get_tzid(), self.get_window_end()) |
paul@606 | 234 | |
paul@606 | 235 | # Store operations. |
paul@606 | 236 | |
paul@606 | 237 | def get_stored_object(self, uid, recurrenceid): |
paul@606 | 238 | |
paul@606 | 239 | """ |
paul@606 | 240 | Return the stored object for the current user, with the given 'uid' and |
paul@606 | 241 | 'recurrenceid'. |
paul@606 | 242 | """ |
paul@606 | 243 | |
paul@606 | 244 | fragment = self.store.get_event(self.user, uid, recurrenceid) |
paul@606 | 245 | return fragment and Object(fragment) |
paul@606 | 246 | |
paul@604 | 247 | # Free/busy operations. |
paul@604 | 248 | |
paul@606 | 249 | def get_freebusy_part(self, freebusy=None): |
paul@604 | 250 | |
paul@604 | 251 | """ |
paul@606 | 252 | Return a message part containing free/busy information for the user, |
paul@606 | 253 | either specified as 'freebusy' or obtained from the store directly. |
paul@604 | 254 | """ |
paul@604 | 255 | |
paul@604 | 256 | if self.is_sharing() and self.is_bundling(): |
paul@604 | 257 | |
paul@604 | 258 | # Invent a unique identifier. |
paul@604 | 259 | |
paul@604 | 260 | utcnow = get_timestamp() |
paul@604 | 261 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@604 | 262 | |
paul@606 | 263 | freebusy = freebusy or self.store.get_freebusy(self.user) |
paul@604 | 264 | |
paul@604 | 265 | user_attr = {} |
paul@604 | 266 | self.update_sender(user_attr) |
paul@604 | 267 | return to_part("PUBLISH", [make_freebusy(freebusy, uid, self.user, user_attr)]) |
paul@604 | 268 | |
paul@604 | 269 | return None |
paul@604 | 270 | |
paul@606 | 271 | def update_freebusy(self, freebusy, periods, transp, uid, recurrenceid, summary, organiser): |
paul@606 | 272 | |
paul@606 | 273 | """ |
paul@606 | 274 | Update the 'freebusy' collection with the given 'periods', indicating a |
paul@606 | 275 | 'transp' status, explicit 'uid' and 'recurrenceid' to indicate either a |
paul@606 | 276 | recurrence or the parent event. The 'summary' and 'organiser' must also |
paul@606 | 277 | be provided. |
paul@606 | 278 | """ |
paul@606 | 279 | |
paul@612 | 280 | update_freebusy(freebusy, periods, transp, uid, recurrenceid, summary, organiser) |
paul@606 | 281 | |
paul@601 | 282 | class ClientForObject(Client): |
paul@601 | 283 | |
paul@601 | 284 | "A client maintaining a specific object." |
paul@601 | 285 | |
paul@639 | 286 | def __init__(self, obj, user, messenger=None, store=None, publisher=None, preferences_dir=None): |
paul@639 | 287 | Client.__init__(self, user, messenger, store, publisher, preferences_dir) |
paul@601 | 288 | self.set_object(obj) |
paul@601 | 289 | |
paul@601 | 290 | def set_object(self, obj): |
paul@606 | 291 | |
paul@606 | 292 | "Set the current object to 'obj', obtaining metadata details." |
paul@606 | 293 | |
paul@601 | 294 | self.obj = obj |
paul@601 | 295 | self.uid = obj and self.obj.get_uid() |
paul@601 | 296 | self.recurrenceid = obj and self.obj.get_recurrenceid() |
paul@601 | 297 | self.sequence = obj and self.obj.get_value("SEQUENCE") |
paul@601 | 298 | self.dtstamp = obj and self.obj.get_value("DTSTAMP") |
paul@601 | 299 | |
paul@729 | 300 | def set_identity(self, method): |
paul@729 | 301 | |
paul@729 | 302 | """ |
paul@729 | 303 | Set the current user for the current object in the context of the given |
paul@729 | 304 | 'method'. It is usually set when initialising the handler, using the |
paul@729 | 305 | recipient details, but outgoing messages do not reference the recipient |
paul@729 | 306 | in this way. |
paul@729 | 307 | """ |
paul@729 | 308 | |
paul@729 | 309 | pass |
paul@729 | 310 | |
paul@727 | 311 | def is_usable(self, method=None): |
paul@720 | 312 | |
paul@727 | 313 | "Return whether the current object is usable with the given 'method'." |
paul@720 | 314 | |
paul@720 | 315 | return True |
paul@720 | 316 | |
paul@604 | 317 | # Object update methods. |
paul@601 | 318 | |
paul@676 | 319 | def update_recurrenceid(self): |
paul@676 | 320 | |
paul@676 | 321 | """ |
paul@676 | 322 | Update the RECURRENCE-ID in the current object, initialising it from |
paul@676 | 323 | DTSTART. |
paul@676 | 324 | """ |
paul@676 | 325 | |
paul@680 | 326 | self.obj["RECURRENCE-ID"] = [self.obj.get_item("DTSTART")] |
paul@676 | 327 | self.recurrenceid = self.obj.get_recurrenceid() |
paul@676 | 328 | |
paul@601 | 329 | def update_dtstamp(self): |
paul@601 | 330 | |
paul@601 | 331 | "Update the DTSTAMP in the current object." |
paul@601 | 332 | |
paul@601 | 333 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@601 | 334 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@720 | 335 | self.dtstamp = format_datetime(dtstamp and dtstamp > utcnow and dtstamp or utcnow) |
paul@676 | 336 | self.obj["DTSTAMP"] = [(self.dtstamp, {})] |
paul@601 | 337 | |
paul@601 | 338 | def set_sequence(self, increment=False): |
paul@601 | 339 | |
paul@601 | 340 | "Update the SEQUENCE in the current object." |
paul@601 | 341 | |
paul@601 | 342 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@601 | 343 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@601 | 344 | |
paul@606 | 345 | def merge_attendance(self, attendees): |
paul@606 | 346 | |
paul@606 | 347 | """ |
paul@606 | 348 | Merge attendance from the current object's 'attendees' into the version |
paul@606 | 349 | stored for the current user. |
paul@606 | 350 | """ |
paul@606 | 351 | |
paul@606 | 352 | obj = self.get_stored_object_version() |
paul@606 | 353 | |
paul@606 | 354 | if not obj or not self.have_new_object(obj): |
paul@606 | 355 | return False |
paul@606 | 356 | |
paul@606 | 357 | # Get attendee details in a usable form. |
paul@606 | 358 | |
paul@606 | 359 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@606 | 360 | |
paul@606 | 361 | for attendee, attendee_attr in attendees.items(): |
paul@606 | 362 | |
paul@606 | 363 | # Update attendance in the loaded object. |
paul@606 | 364 | |
paul@606 | 365 | attendee_map[attendee] = attendee_attr |
paul@606 | 366 | |
paul@606 | 367 | # Set the new details and store the object. |
paul@606 | 368 | |
paul@606 | 369 | obj["ATTENDEE"] = attendee_map.items() |
paul@606 | 370 | |
paul@606 | 371 | # Set the complete event if not an additional occurrence. |
paul@606 | 372 | |
paul@606 | 373 | event = obj.to_node() |
paul@606 | 374 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@606 | 375 | |
paul@606 | 376 | return True |
paul@606 | 377 | |
paul@606 | 378 | # Object-related tests. |
paul@606 | 379 | |
paul@728 | 380 | def is_recognised_organiser(self, organiser): |
paul@728 | 381 | |
paul@728 | 382 | """ |
paul@728 | 383 | Return whether the given 'organiser' is recognised from |
paul@728 | 384 | previously-received details. If no stored details exist, True is |
paul@728 | 385 | returned. |
paul@728 | 386 | """ |
paul@728 | 387 | |
paul@728 | 388 | obj = self.get_stored_object_version() |
paul@728 | 389 | if obj: |
paul@728 | 390 | stored_organiser = get_uri(obj.get_value("ORGANIZER")) |
paul@728 | 391 | return stored_organiser == organiser |
paul@728 | 392 | else: |
paul@728 | 393 | return True |
paul@728 | 394 | |
paul@728 | 395 | def is_recognised_attendee(self, attendee): |
paul@728 | 396 | |
paul@728 | 397 | """ |
paul@728 | 398 | Return whether the given 'attendee' is recognised from |
paul@728 | 399 | previously-received details. If no stored details exist, True is |
paul@728 | 400 | returned. |
paul@728 | 401 | """ |
paul@728 | 402 | |
paul@728 | 403 | obj = self.get_stored_object_version() |
paul@728 | 404 | if obj: |
paul@728 | 405 | stored_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@728 | 406 | return stored_attendees.has_key(attendee) |
paul@728 | 407 | else: |
paul@728 | 408 | return True |
paul@728 | 409 | |
paul@694 | 410 | def get_attendance(self, user=None, obj=None): |
paul@606 | 411 | |
paul@606 | 412 | """ |
paul@606 | 413 | Return the attendance attributes for 'user', or the current user if |
paul@606 | 414 | 'user' is not specified. |
paul@606 | 415 | """ |
paul@606 | 416 | |
paul@694 | 417 | attendees = uri_dict((obj or self.obj).get_value_map("ATTENDEE")) |
paul@697 | 418 | return attendees.get(user or self.user) |
paul@606 | 419 | |
paul@694 | 420 | def is_participating(self, user, as_organiser=False, obj=None): |
paul@609 | 421 | |
paul@609 | 422 | """ |
paul@609 | 423 | Return whether, subject to the 'user' indicating an identity and the |
paul@609 | 424 | 'as_organiser' status of that identity, the user concerned is actually |
paul@609 | 425 | participating in the current object event. |
paul@609 | 426 | """ |
paul@609 | 427 | |
paul@697 | 428 | # Use any attendee property information for an organiser, not the |
paul@697 | 429 | # organiser property attributes. |
paul@697 | 430 | |
paul@694 | 431 | attr = self.get_attendance(user, obj=obj) |
paul@697 | 432 | return as_organiser or attr is not None and not attr or attr and attr.get("PARTSTAT") != "DECLINED" |
paul@609 | 433 | |
paul@609 | 434 | def get_overriding_transparency(self, user, as_organiser=False): |
paul@609 | 435 | |
paul@609 | 436 | """ |
paul@609 | 437 | Return the overriding transparency to be associated with the free/busy |
paul@609 | 438 | records for an event, subject to the 'user' indicating an identity and |
paul@609 | 439 | the 'as_organiser' status of that identity. |
paul@609 | 440 | |
paul@609 | 441 | Where an identity is only an organiser and not attending, "ORG" is |
paul@609 | 442 | returned. Otherwise, no overriding transparency is defined and None is |
paul@609 | 443 | returned. |
paul@609 | 444 | """ |
paul@609 | 445 | |
paul@609 | 446 | attr = self.get_attendance(user) |
paul@609 | 447 | return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None |
paul@609 | 448 | |
paul@606 | 449 | def is_attendee(self, identity, obj=None): |
paul@606 | 450 | |
paul@606 | 451 | """ |
paul@606 | 452 | Return whether 'identity' is an attendee in the current object, or in |
paul@606 | 453 | 'obj' if specified. |
paul@606 | 454 | """ |
paul@606 | 455 | |
paul@606 | 456 | return identity in uri_values((obj or self.obj).get_values("ATTENDEE")) |
paul@606 | 457 | |
paul@606 | 458 | def can_schedule(self, freebusy, periods): |
paul@606 | 459 | |
paul@606 | 460 | """ |
paul@606 | 461 | Indicate whether within 'freebusy' the given 'periods' can be scheduled. |
paul@606 | 462 | """ |
paul@606 | 463 | |
paul@606 | 464 | return can_schedule(freebusy, periods, self.uid, self.recurrenceid) |
paul@606 | 465 | |
paul@682 | 466 | def have_new_object(self, obj=None, strict=True): |
paul@606 | 467 | |
paul@606 | 468 | """ |
paul@606 | 469 | Return whether the current object is new to the current user (or if the |
paul@682 | 470 | given 'obj' is new). If 'strict' is specified and is a false value, the |
paul@682 | 471 | DTSTAMP test will be ignored. This is useful in handling responses from |
paul@682 | 472 | attendees from clients (like Claws Mail) that erase time information |
paul@682 | 473 | from DTSTAMP and make it invalid. |
paul@606 | 474 | """ |
paul@606 | 475 | |
paul@606 | 476 | obj = obj or self.get_stored_object_version() |
paul@606 | 477 | |
paul@606 | 478 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@606 | 479 | |
paul@606 | 480 | if obj: |
paul@606 | 481 | sequence = obj.get_value("SEQUENCE") |
paul@606 | 482 | dtstamp = obj.get_value("DTSTAMP") |
paul@606 | 483 | |
paul@606 | 484 | # If the request refers to an older version of the object, ignore |
paul@606 | 485 | # it. |
paul@606 | 486 | |
paul@682 | 487 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, not strict) |
paul@606 | 488 | |
paul@606 | 489 | return True |
paul@606 | 490 | |
paul@672 | 491 | def possibly_recurring_indefinitely(self): |
paul@672 | 492 | |
paul@672 | 493 | "Return whether the object recurs indefinitely." |
paul@672 | 494 | |
paul@672 | 495 | # Obtain the stored object to make sure that recurrence information |
paul@672 | 496 | # is not being ignored. This might happen if a client sends a |
paul@672 | 497 | # cancellation without the complete set of properties, for instance. |
paul@672 | 498 | |
paul@672 | 499 | return self.obj.possibly_recurring_indefinitely() or \ |
paul@672 | 500 | self.get_stored_object_version() and \ |
paul@672 | 501 | self.get_stored_object_version().possibly_recurring_indefinitely() |
paul@672 | 502 | |
paul@655 | 503 | # Constraint application on event periods. |
paul@655 | 504 | |
paul@655 | 505 | def check_object(self): |
paul@655 | 506 | |
paul@655 | 507 | "Check the object against any scheduling constraints." |
paul@655 | 508 | |
paul@669 | 509 | permitted_values = self.get_permitted_values() |
paul@669 | 510 | if not permitted_values: |
paul@655 | 511 | return None |
paul@655 | 512 | |
paul@655 | 513 | invalid = [] |
paul@655 | 514 | |
paul@660 | 515 | for period in self.obj.get_periods(self.get_tzid()): |
paul@655 | 516 | start = period.get_start() |
paul@655 | 517 | end = period.get_end() |
paul@669 | 518 | start_errors = check_permitted_values(start, permitted_values) |
paul@669 | 519 | end_errors = check_permitted_values(end, permitted_values) |
paul@656 | 520 | if start_errors or end_errors: |
paul@656 | 521 | invalid.append((period.origin, start_errors, end_errors)) |
paul@655 | 522 | |
paul@655 | 523 | return invalid |
paul@655 | 524 | |
paul@660 | 525 | def correct_object(self): |
paul@655 | 526 | |
paul@660 | 527 | "Correct the object according to any scheduling constraints." |
paul@655 | 528 | |
paul@669 | 529 | permitted_values = self.get_permitted_values() |
paul@669 | 530 | return permitted_values and self.obj.correct_object(self.get_tzid(), permitted_values) |
paul@655 | 531 | |
paul@606 | 532 | # Object retrieval. |
paul@606 | 533 | |
paul@606 | 534 | def get_stored_object_version(self): |
paul@606 | 535 | |
paul@606 | 536 | """ |
paul@606 | 537 | Return the stored object to which the current object refers for the |
paul@606 | 538 | current user. |
paul@606 | 539 | """ |
paul@606 | 540 | |
paul@606 | 541 | return self.get_stored_object(self.uid, self.recurrenceid) |
paul@606 | 542 | |
paul@704 | 543 | def get_definitive_object(self, as_organiser): |
paul@606 | 544 | |
paul@606 | 545 | """ |
paul@606 | 546 | Return an object considered definitive for the current transaction, |
paul@704 | 547 | using 'as_organiser' to select the current transaction's object if |
paul@704 | 548 | false, or selecting a stored object if true. |
paul@606 | 549 | """ |
paul@606 | 550 | |
paul@704 | 551 | return not as_organiser and self.obj or self.get_stored_object_version() |
paul@606 | 552 | |
paul@606 | 553 | def get_parent_object(self): |
paul@606 | 554 | |
paul@606 | 555 | """ |
paul@606 | 556 | Return the parent object to which the current object refers for the |
paul@606 | 557 | current user. |
paul@606 | 558 | """ |
paul@606 | 559 | |
paul@606 | 560 | return self.recurrenceid and self.get_stored_object(self.uid, None) or None |
paul@606 | 561 | |
paul@606 | 562 | # Convenience methods for modifying free/busy collections. |
paul@606 | 563 | |
paul@606 | 564 | def get_recurrence_start_point(self, recurrenceid): |
paul@606 | 565 | |
paul@606 | 566 | "Get 'recurrenceid' in a form suitable for matching free/busy entries." |
paul@606 | 567 | |
paul@627 | 568 | return self.obj.get_recurrence_start_point(recurrenceid, self.get_tzid()) |
paul@606 | 569 | |
paul@606 | 570 | def remove_from_freebusy(self, freebusy): |
paul@606 | 571 | |
paul@606 | 572 | "Remove this event from the given 'freebusy' collection." |
paul@606 | 573 | |
paul@606 | 574 | if not remove_period(freebusy, self.uid, self.recurrenceid) and self.recurrenceid: |
paul@606 | 575 | remove_affected_period(freebusy, self.uid, self.get_recurrence_start_point(self.recurrenceid)) |
paul@606 | 576 | |
paul@606 | 577 | def remove_freebusy_for_recurrences(self, freebusy, recurrenceids=None): |
paul@606 | 578 | |
paul@606 | 579 | """ |
paul@606 | 580 | Remove from 'freebusy' any original recurrence from parent free/busy |
paul@606 | 581 | details for the current object, if the current object is a specific |
paul@606 | 582 | additional recurrence. Otherwise, remove all additional recurrence |
paul@606 | 583 | information corresponding to 'recurrenceids', or if omitted, all |
paul@606 | 584 | recurrences. |
paul@606 | 585 | """ |
paul@606 | 586 | |
paul@606 | 587 | if self.recurrenceid: |
paul@606 | 588 | recurrenceid = self.get_recurrence_start_point(self.recurrenceid) |
paul@606 | 589 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@606 | 590 | else: |
paul@606 | 591 | # Remove obsolete recurrence periods. |
paul@606 | 592 | |
paul@606 | 593 | remove_additional_periods(freebusy, self.uid, recurrenceids) |
paul@606 | 594 | |
paul@606 | 595 | # Remove original periods affected by additional recurrences. |
paul@606 | 596 | |
paul@606 | 597 | if recurrenceids: |
paul@606 | 598 | for recurrenceid in recurrenceids: |
paul@606 | 599 | recurrenceid = self.get_recurrence_start_point(recurrenceid) |
paul@606 | 600 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@606 | 601 | |
paul@704 | 602 | def update_freebusy(self, freebusy, user, as_organiser): |
paul@606 | 603 | |
paul@606 | 604 | """ |
paul@606 | 605 | Update the 'freebusy' collection for this event with the periods and |
paul@606 | 606 | transparency associated with the current object, subject to the 'user' |
paul@606 | 607 | identity and the attendance details provided for them, indicating |
paul@704 | 608 | whether the update is being done 'as_organiser' (for the organiser of |
paul@676 | 609 | an event) or not. |
paul@606 | 610 | """ |
paul@606 | 611 | |
paul@606 | 612 | # Obtain the stored object if the current object is not issued by the |
paul@606 | 613 | # organiser. Attendees do not have the opportunity to redefine the |
paul@606 | 614 | # periods. |
paul@606 | 615 | |
paul@704 | 616 | obj = self.get_definitive_object(as_organiser) |
paul@606 | 617 | if not obj: |
paul@606 | 618 | return |
paul@606 | 619 | |
paul@606 | 620 | # Obtain the affected periods. |
paul@606 | 621 | |
paul@606 | 622 | periods = self.get_periods(obj) |
paul@606 | 623 | |
paul@606 | 624 | # Define an overriding transparency, the indicated event transparency, |
paul@606 | 625 | # or the default transparency for the free/busy entry. |
paul@606 | 626 | |
paul@704 | 627 | transp = self.get_overriding_transparency(user, as_organiser) or \ |
paul@606 | 628 | obj.get_value("TRANSP") or \ |
paul@606 | 629 | "OPAQUE" |
paul@606 | 630 | |
paul@606 | 631 | # Perform the low-level update. |
paul@606 | 632 | |
paul@606 | 633 | Client.update_freebusy(self, freebusy, periods, transp, |
paul@606 | 634 | self.uid, self.recurrenceid, |
paul@606 | 635 | obj.get_value("SUMMARY"), |
paul@606 | 636 | obj.get_value("ORGANIZER")) |
paul@606 | 637 | |
paul@606 | 638 | def update_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@606 | 639 | updating_other=False): |
paul@606 | 640 | |
paul@606 | 641 | """ |
paul@695 | 642 | Update the 'freebusy' collection for the given 'user', indicating |
paul@695 | 643 | whether the update is 'for_organiser' (being done for the organiser of |
paul@695 | 644 | an event) or not, and whether it is 'updating_other' (meaning another |
paul@695 | 645 | user's details). |
paul@606 | 646 | """ |
paul@606 | 647 | |
paul@606 | 648 | # Record in the free/busy details unless a non-participating attendee. |
paul@697 | 649 | # Remove periods for non-participating attendees. |
paul@606 | 650 | |
paul@606 | 651 | if self.is_participating(user, for_organiser and not updating_other): |
paul@704 | 652 | self.update_freebusy(freebusy, user, |
paul@704 | 653 | for_organiser and not updating_other or |
paul@704 | 654 | not for_organiser and updating_other |
paul@704 | 655 | ) |
paul@606 | 656 | else: |
paul@606 | 657 | self.remove_from_freebusy(freebusy) |
paul@606 | 658 | |
paul@697 | 659 | def remove_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@697 | 660 | updating_other=False): |
paul@697 | 661 | |
paul@697 | 662 | """ |
paul@697 | 663 | Remove details from the 'freebusy' collection for the given 'user', |
paul@697 | 664 | indicating whether the modification is 'for_organiser' (being done for |
paul@697 | 665 | the organiser of an event) or not, and whether it is 'updating_other' |
paul@697 | 666 | (meaning another user's details). |
paul@697 | 667 | """ |
paul@697 | 668 | |
paul@697 | 669 | # Remove from the free/busy details if a specified attendee. |
paul@697 | 670 | |
paul@697 | 671 | if self.is_participating(user, for_organiser and not updating_other): |
paul@697 | 672 | self.remove_from_freebusy(freebusy) |
paul@697 | 673 | |
paul@606 | 674 | # Convenience methods for updating stored free/busy information received |
paul@606 | 675 | # from other users. |
paul@606 | 676 | |
paul@697 | 677 | def update_freebusy_from_participant(self, user, for_organiser, fn=None): |
paul@606 | 678 | |
paul@606 | 679 | """ |
paul@606 | 680 | For the current user, record the free/busy information for another |
paul@606 | 681 | 'user', indicating whether the update is 'for_organiser' or not, thus |
paul@606 | 682 | maintaining a separate record of their free/busy details. |
paul@606 | 683 | """ |
paul@606 | 684 | |
paul@697 | 685 | fn = fn or self.update_freebusy_for_participant |
paul@697 | 686 | |
paul@606 | 687 | # A user does not store free/busy information for themself as another |
paul@606 | 688 | # party. |
paul@606 | 689 | |
paul@606 | 690 | if user == self.user: |
paul@606 | 691 | return |
paul@606 | 692 | |
paul@730 | 693 | self.acquire_lock() |
paul@702 | 694 | try: |
paul@730 | 695 | freebusy = self.store.get_freebusy_for_other(self.user, user) |
paul@702 | 696 | fn(freebusy, user, for_organiser, True) |
paul@702 | 697 | |
paul@702 | 698 | # Tidy up any obsolete recurrences. |
paul@606 | 699 | |
paul@702 | 700 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@730 | 701 | self.store.set_freebusy_for_other(self.user, freebusy, user) |
paul@606 | 702 | |
paul@702 | 703 | finally: |
paul@730 | 704 | self.release_lock() |
paul@606 | 705 | |
paul@606 | 706 | def update_freebusy_from_organiser(self, organiser): |
paul@606 | 707 | |
paul@606 | 708 | "For the current user, record free/busy information from 'organiser'." |
paul@606 | 709 | |
paul@606 | 710 | self.update_freebusy_from_participant(organiser, True) |
paul@606 | 711 | |
paul@606 | 712 | def update_freebusy_from_attendees(self, attendees): |
paul@606 | 713 | |
paul@606 | 714 | "For the current user, record free/busy information from 'attendees'." |
paul@606 | 715 | |
paul@606 | 716 | for attendee in attendees.keys(): |
paul@606 | 717 | self.update_freebusy_from_participant(attendee, False) |
paul@606 | 718 | |
paul@697 | 719 | def remove_freebusy_from_organiser(self, organiser): |
paul@697 | 720 | |
paul@697 | 721 | "For the current user, remove free/busy information from 'organiser'." |
paul@697 | 722 | |
paul@697 | 723 | self.update_freebusy_from_participant(organiser, True, self.remove_freebusy_for_participant) |
paul@697 | 724 | |
paul@697 | 725 | def remove_freebusy_from_attendees(self, attendees): |
paul@697 | 726 | |
paul@697 | 727 | "For the current user, remove free/busy information from 'attendees'." |
paul@697 | 728 | |
paul@697 | 729 | for attendee in attendees.keys(): |
paul@697 | 730 | self.update_freebusy_from_participant(attendee, False, self.remove_freebusy_for_participant) |
paul@697 | 731 | |
paul@441 | 732 | # vim: tabstop=4 expandtab shiftwidth=4 |