paul@441 | 1 | #!/usr/bin/env python |
paul@441 | 2 | |
paul@441 | 3 | """ |
paul@441 | 4 | Common calendar client utilities. |
paul@441 | 5 | |
paul@1212 | 6 | Copyright (C) 2014, 2015, 2016, 2017 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@1302 | 22 | from collections import OrderedDict |
paul@740 | 23 | from datetime import datetime, timedelta |
paul@1212 | 24 | from imiptools.config import settings |
paul@1332 | 25 | from imiptools.data import check_delegation, get_address, \ |
paul@1274 | 26 | get_recurrence_periods, \ |
paul@1325 | 27 | get_sender_identities, get_uri, \ |
paul@1204 | 28 | get_window_end, is_new_object, make_freebusy, \ |
paul@1332 | 29 | make_uid, new_object, to_part, \ |
paul@1332 | 30 | update_attendees_with_delegates, \ |
paul@1326 | 31 | uri_dict, uri_item, uri_items, uri_parts, uri_values |
paul@1238 | 32 | from imiptools.dates import check_permitted_values, format_datetime, \ |
paul@1238 | 33 | get_datetime, get_default_timezone, \ |
paul@1238 | 34 | get_duration, get_time, get_timestamp, \ |
paul@1238 | 35 | to_datetime |
paul@1005 | 36 | from imiptools.i18n import get_translator |
paul@1230 | 37 | from imiptools.freebusy import SupportAttendee, SupportExpires |
paul@443 | 38 | from imiptools.profile import Preferences |
paul@1088 | 39 | from imiptools.stores import get_store, get_publisher, get_journal |
paul@441 | 40 | |
paul@443 | 41 | class Client: |
paul@443 | 42 | |
paul@443 | 43 | "Common handler and manager methods." |
paul@443 | 44 | |
paul@467 | 45 | default_window_size = 100 |
paul@729 | 46 | organiser_methods = "ADD", "CANCEL", "DECLINECOUNTER", "PUBLISH", "REQUEST" |
paul@467 | 47 | |
paul@1039 | 48 | def __init__(self, user, messenger=None, store=None, publisher=None, journal=None, |
paul@1039 | 49 | preferences_dir=None): |
paul@728 | 50 | |
paul@728 | 51 | """ |
paul@728 | 52 | Initialise a calendar client with the current 'user', plus any |
paul@1039 | 53 | 'messenger', 'store', 'publisher' and 'journal' objects, indicating any |
paul@1039 | 54 | specific 'preferences_dir'. |
paul@728 | 55 | """ |
paul@728 | 56 | |
paul@443 | 57 | self.user = user |
paul@601 | 58 | self.messenger = messenger |
paul@1212 | 59 | self.store = store or get_store(settings["STORE_TYPE"], settings["STORE_DIR"]) |
paul@1212 | 60 | self.journal = journal or get_journal(settings["STORE_TYPE"], settings["JOURNAL_DIR"]) |
paul@604 | 61 | |
paul@604 | 62 | try: |
paul@1212 | 63 | self.publisher = publisher or get_publisher(settings["PUBLISH_DIR"]) |
paul@604 | 64 | except OSError: |
paul@604 | 65 | self.publisher = None |
paul@604 | 66 | |
paul@639 | 67 | self.preferences_dir = preferences_dir |
paul@443 | 68 | self.preferences = None |
paul@443 | 69 | |
paul@1005 | 70 | # Localise the messenger. |
paul@1005 | 71 | |
paul@1005 | 72 | if self.messenger: |
paul@1005 | 73 | self.messenger.gettext = self.get_translator() |
paul@1005 | 74 | |
paul@1012 | 75 | def get_store(self): |
paul@1012 | 76 | return self.store |
paul@1012 | 77 | |
paul@1012 | 78 | def get_publisher(self): |
paul@1012 | 79 | return self.publisher |
paul@1012 | 80 | |
paul@1039 | 81 | def get_journal(self): |
paul@1039 | 82 | return self.journal |
paul@1039 | 83 | |
paul@730 | 84 | # Store-related methods. |
paul@730 | 85 | |
paul@730 | 86 | def acquire_lock(self): |
paul@730 | 87 | self.store.acquire_lock(self.user) |
paul@730 | 88 | |
paul@730 | 89 | def release_lock(self): |
paul@730 | 90 | self.store.release_lock(self.user) |
paul@730 | 91 | |
paul@730 | 92 | # Preferences-related methods. |
paul@730 | 93 | |
paul@443 | 94 | def get_preferences(self): |
paul@467 | 95 | if not self.preferences and self.user: |
paul@639 | 96 | self.preferences = Preferences(self.user, self.preferences_dir) |
paul@443 | 97 | return self.preferences |
paul@443 | 98 | |
paul@1005 | 99 | def get_locale(self): |
paul@1012 | 100 | prefs = self.get_preferences() |
paul@1012 | 101 | return prefs and prefs.get("LANG", "en", True) or "en" |
paul@1005 | 102 | |
paul@1005 | 103 | def get_translator(self): |
paul@1005 | 104 | return get_translator([self.get_locale()]) |
paul@1005 | 105 | |
paul@791 | 106 | def get_user_attributes(self): |
paul@791 | 107 | prefs = self.get_preferences() |
paul@791 | 108 | return prefs and prefs.get_all(["CN"]) or {} |
paul@791 | 109 | |
paul@443 | 110 | def get_tzid(self): |
paul@443 | 111 | prefs = self.get_preferences() |
paul@467 | 112 | return prefs and prefs.get("TZID") or get_default_timezone() |
paul@443 | 113 | |
paul@443 | 114 | def get_window_size(self): |
paul@1238 | 115 | |
paul@1238 | 116 | "Return the period window size as an integer." |
paul@1238 | 117 | |
paul@443 | 118 | prefs = self.get_preferences() |
paul@443 | 119 | try: |
paul@467 | 120 | return prefs and int(prefs.get("window_size")) or self.default_window_size |
paul@443 | 121 | except (TypeError, ValueError): |
paul@467 | 122 | return self.default_window_size |
paul@443 | 123 | |
paul@1238 | 124 | def get_window_start(self): |
paul@1238 | 125 | |
paul@1238 | 126 | "Return the period window start as a datetime." |
paul@1238 | 127 | |
paul@1238 | 128 | prefs = self.get_preferences() |
paul@1276 | 129 | tzid = self.get_tzid() |
paul@1276 | 130 | start = prefs and get_datetime(prefs.get("window_start"), {"TZID" : tzid}) |
paul@1276 | 131 | return isinstance(start, datetime) and start or start and to_datetime(start, tzid) |
paul@1238 | 132 | |
paul@1242 | 133 | def get_window_end(self, size=None, start=None): |
paul@1238 | 134 | |
paul@1238 | 135 | "Return the period window end as a datetime." |
paul@1238 | 136 | |
paul@1276 | 137 | tzid = self.get_tzid() |
paul@1276 | 138 | return get_window_end(tzid, size or self.get_window_size(), |
paul@1276 | 139 | start or self.get_window_start()) |
paul@443 | 140 | |
paul@667 | 141 | def is_participating(self): |
paul@748 | 142 | |
paul@748 | 143 | "Return participation in the calendar system." |
paul@748 | 144 | |
paul@667 | 145 | prefs = self.get_preferences() |
paul@1212 | 146 | return prefs and prefs.get("participating", settings["PARTICIPATING_DEFAULT"]) != "no" or False |
paul@667 | 147 | |
paul@443 | 148 | def is_sharing(self): |
paul@748 | 149 | |
paul@748 | 150 | "Return whether free/busy information is being generally shared." |
paul@748 | 151 | |
paul@467 | 152 | prefs = self.get_preferences() |
paul@1212 | 153 | return prefs and prefs.get("freebusy_sharing", settings["SHARING_DEFAULT"]) == "share" or False |
paul@443 | 154 | |
paul@443 | 155 | def is_bundling(self): |
paul@748 | 156 | |
paul@748 | 157 | "Return whether free/busy information is being bundled in messages." |
paul@748 | 158 | |
paul@467 | 159 | prefs = self.get_preferences() |
paul@1212 | 160 | return prefs and prefs.get("freebusy_bundling", settings["BUNDLING_DEFAULT"]) == "always" or False |
paul@467 | 161 | |
paul@467 | 162 | def is_notifying(self): |
paul@748 | 163 | |
paul@748 | 164 | "Return whether recipients are notified about free/busy payloads." |
paul@748 | 165 | |
paul@467 | 166 | prefs = self.get_preferences() |
paul@1212 | 167 | return prefs and prefs.get("freebusy_messages", settings["NOTIFYING_DEFAULT"]) == "notify" or False |
paul@443 | 168 | |
paul@748 | 169 | def is_publishing(self): |
paul@748 | 170 | |
paul@748 | 171 | "Return whether free/busy information is being published as Web resources." |
paul@748 | 172 | |
paul@748 | 173 | prefs = self.get_preferences() |
paul@1212 | 174 | return prefs and prefs.get("freebusy_publishing", settings["PUBLISHING_DEFAULT"]) == "publish" or False |
paul@748 | 175 | |
paul@688 | 176 | def is_refreshing(self): |
paul@748 | 177 | |
paul@748 | 178 | "Return whether a recipient supports requests to refresh event details." |
paul@748 | 179 | |
paul@688 | 180 | prefs = self.get_preferences() |
paul@1212 | 181 | return prefs and prefs.get("event_refreshing", settings["REFRESHING_DEFAULT"]) == "always" or False |
paul@688 | 182 | |
paul@734 | 183 | def allow_add(self): |
paul@734 | 184 | return self.get_add_method_response() in ("add", "refresh") |
paul@734 | 185 | |
paul@734 | 186 | def get_add_method_response(self): |
paul@728 | 187 | prefs = self.get_preferences() |
paul@1212 | 188 | return prefs and prefs.get("add_method_response", settings["ADD_RESPONSE_DEFAULT"]) or "refresh" |
paul@734 | 189 | |
paul@740 | 190 | def get_offer_period(self): |
paul@740 | 191 | |
paul@759 | 192 | "Decode a specification in the iCalendar duration format." |
paul@740 | 193 | |
paul@740 | 194 | prefs = self.get_preferences() |
paul@1212 | 195 | duration = prefs and prefs.get("freebusy_offers", settings["FREEBUSY_OFFER_DEFAULT"]) |
paul@740 | 196 | |
paul@759 | 197 | # NOTE: Should probably report an error somehow if None. |
paul@740 | 198 | |
paul@759 | 199 | return duration and get_duration(duration) or None |
paul@740 | 200 | |
paul@734 | 201 | def get_organiser_replacement(self): |
paul@734 | 202 | prefs = self.get_preferences() |
paul@1212 | 203 | return prefs and prefs.get("organiser_replacement", settings["ORGANISER_REPLACEMENT_DEFAULT"]) or "attendee" |
paul@728 | 204 | |
paul@668 | 205 | def have_manager(self): |
paul@1212 | 206 | return settings["MANAGER_INTERFACE"] |
paul@668 | 207 | |
paul@669 | 208 | def get_permitted_values(self): |
paul@655 | 209 | |
paul@655 | 210 | """ |
paul@655 | 211 | Decode a specification of one of the following forms... |
paul@655 | 212 | |
paul@655 | 213 | <minute values> |
paul@655 | 214 | <hour values>:<minute values> |
paul@655 | 215 | <hour values>:<minute values>:<second values> |
paul@655 | 216 | |
paul@655 | 217 | ...with each list of values being comma-separated. |
paul@655 | 218 | """ |
paul@655 | 219 | |
paul@655 | 220 | prefs = self.get_preferences() |
paul@669 | 221 | permitted_values = prefs and prefs.get("permitted_times") |
paul@669 | 222 | if permitted_values: |
paul@655 | 223 | try: |
paul@655 | 224 | l = [] |
paul@669 | 225 | for component in permitted_values.split(":")[:3]: |
paul@655 | 226 | if component: |
paul@655 | 227 | l.append(map(int, component.split(","))) |
paul@655 | 228 | else: |
paul@655 | 229 | l.append(None) |
paul@655 | 230 | |
paul@655 | 231 | # NOTE: Should probably report an error somehow. |
paul@655 | 232 | |
paul@655 | 233 | except ValueError: |
paul@655 | 234 | return None |
paul@655 | 235 | else: |
paul@655 | 236 | l = (len(l) < 2 and [None] or []) + l + (len(l) < 3 and [None] or []) |
paul@655 | 237 | return l |
paul@655 | 238 | else: |
paul@655 | 239 | return None |
paul@655 | 240 | |
paul@581 | 241 | # Common operations on calendar data. |
paul@581 | 242 | |
paul@1281 | 243 | def update_sender_attr(self, attr): |
paul@584 | 244 | |
paul@584 | 245 | "Update the SENT-BY attribute of the 'attr' sender metadata." |
paul@584 | 246 | |
paul@1292 | 247 | if self.messenger: |
paul@1292 | 248 | if self.messenger.sender != get_address(self.user): |
paul@1292 | 249 | attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@1294 | 250 | elif attr.has_key("SENT-BY"): |
paul@1292 | 251 | del attr["SENT-BY"] |
paul@584 | 252 | |
paul@1238 | 253 | def get_periods(self, obj, explicit_only=False, future_only=False): |
paul@606 | 254 | |
paul@606 | 255 | """ |
paul@1276 | 256 | Return periods for the given 'obj'. |
paul@1238 | 257 | |
paul@1238 | 258 | If 'explicit_only' is set to a true value, only explicit periods will be |
paul@941 | 259 | returned, not rule-based periods. |
paul@1238 | 260 | |
paul@1238 | 261 | If 'future_only' is set to a true value, only future periods will be |
paul@1238 | 262 | returned, not all periods defined by an event starting in the past. |
paul@606 | 263 | """ |
paul@606 | 264 | |
paul@1276 | 265 | return obj.get_periods( |
paul@1238 | 266 | start=(future_only and self.get_window_start() or None), |
paul@1238 | 267 | end=(not explicit_only and self.get_window_end() or None)) |
paul@606 | 268 | |
paul@1322 | 269 | def get_updated_periods(self, obj, explicit_only=False, future_only=False): |
paul@1302 | 270 | |
paul@1302 | 271 | """ |
paul@1302 | 272 | Return the periods provided by 'obj' and associated recurrence |
paul@1302 | 273 | instances. Each original period is returned in a tuple with a |
paul@1302 | 274 | corresponding updated period which may be the same or which may be None |
paul@1302 | 275 | if the period is cancelled. A list of these tuples is returned. |
paul@1322 | 276 | |
paul@1322 | 277 | If 'explicit_only' is set to a true value, only explicit periods will be |
paul@1322 | 278 | returned, not rule-based periods. |
paul@1322 | 279 | |
paul@1322 | 280 | If 'future_only' is set to a true value, only future periods will be |
paul@1322 | 281 | returned, not all periods defined by an event starting in the past. |
paul@1302 | 282 | """ |
paul@1302 | 283 | |
paul@1330 | 284 | self.store.update_event_from_recurrences(self.user, obj) |
paul@1302 | 285 | |
paul@1322 | 286 | return obj.get_updated_periods( |
paul@1322 | 287 | start=(future_only and self.get_window_start() or None), |
paul@1322 | 288 | end=(not explicit_only and self.get_window_end() or None)) |
paul@1302 | 289 | |
paul@606 | 290 | # Store operations. |
paul@606 | 291 | |
paul@766 | 292 | def get_stored_object(self, uid, recurrenceid, section=None, username=None): |
paul@606 | 293 | |
paul@606 | 294 | """ |
paul@606 | 295 | Return the stored object for the current user, with the given 'uid' and |
paul@766 | 296 | 'recurrenceid' from the given 'section' and for the given 'username' (if |
paul@766 | 297 | specified), or from the standard object collection otherwise. |
paul@606 | 298 | """ |
paul@606 | 299 | |
paul@755 | 300 | if section == "counters": |
paul@1276 | 301 | obj = self.store.get_counter(self.user, username, uid, recurrenceid) |
paul@755 | 302 | else: |
paul@1276 | 303 | obj = self.store.get_event(self.user, uid, recurrenceid, section) |
paul@1276 | 304 | |
paul@1276 | 305 | # Set the fallback time zone. |
paul@1276 | 306 | |
paul@1276 | 307 | if obj: |
paul@1276 | 308 | obj.set_tzid(self.get_tzid()) |
paul@1276 | 309 | |
paul@1276 | 310 | return obj |
paul@606 | 311 | |
paul@604 | 312 | # Free/busy operations. |
paul@604 | 313 | |
paul@606 | 314 | def get_freebusy_part(self, freebusy=None): |
paul@604 | 315 | |
paul@604 | 316 | """ |
paul@606 | 317 | Return a message part containing free/busy information for the user, |
paul@606 | 318 | either specified as 'freebusy' or obtained from the store directly. |
paul@604 | 319 | """ |
paul@604 | 320 | |
paul@604 | 321 | if self.is_sharing() and self.is_bundling(): |
paul@604 | 322 | |
paul@604 | 323 | # Invent a unique identifier. |
paul@604 | 324 | |
paul@1204 | 325 | uid = make_uid(self.user) |
paul@604 | 326 | |
paul@606 | 327 | freebusy = freebusy or self.store.get_freebusy(self.user) |
paul@604 | 328 | |
paul@604 | 329 | user_attr = {} |
paul@1281 | 330 | self.update_sender_attr(user_attr) |
paul@1174 | 331 | return self.to_part("PUBLISH", [make_freebusy(freebusy, uid, self.user, user_attr)]) |
paul@604 | 332 | |
paul@604 | 333 | return None |
paul@604 | 334 | |
paul@740 | 335 | def update_freebusy(self, freebusy, periods, transp, uid, recurrenceid, summary, organiser, expires=None): |
paul@606 | 336 | |
paul@606 | 337 | """ |
paul@606 | 338 | Update the 'freebusy' collection with the given 'periods', indicating a |
paul@606 | 339 | 'transp' status, explicit 'uid' and 'recurrenceid' to indicate either a |
paul@606 | 340 | recurrence or the parent event. The 'summary' and 'organiser' must also |
paul@606 | 341 | be provided. |
paul@740 | 342 | |
paul@740 | 343 | An optional 'expires' datetime string can be provided to tag a free/busy |
paul@740 | 344 | offer. |
paul@606 | 345 | """ |
paul@606 | 346 | |
paul@1171 | 347 | # Add specific attendee information for certain collections. |
paul@1171 | 348 | |
paul@1171 | 349 | if isinstance(freebusy, SupportAttendee): |
paul@1171 | 350 | freebusy.update_freebusy(periods, transp, uid, recurrenceid, summary, organiser, self.user) |
paul@1171 | 351 | |
paul@1171 | 352 | # Add expiry datetime for certain collections. |
paul@1171 | 353 | |
paul@1171 | 354 | elif isinstance(freebusy, SupportExpires): |
paul@1171 | 355 | freebusy.update_freebusy(periods, transp, uid, recurrenceid, summary, organiser, expires) |
paul@1171 | 356 | |
paul@1171 | 357 | # Provide only the essential attributes for other collections. |
paul@1171 | 358 | |
paul@1171 | 359 | else: |
paul@1171 | 360 | freebusy.update_freebusy(periods, transp, uid, recurrenceid, summary, organiser) |
paul@606 | 361 | |
paul@1174 | 362 | # Preparation of content. |
paul@1174 | 363 | |
paul@1174 | 364 | def to_part(self, method, fragments): |
paul@1174 | 365 | |
paul@1174 | 366 | "Return an encoded MIME part for the given 'method' and 'fragments'." |
paul@1174 | 367 | |
paul@1212 | 368 | return to_part(method, fragments, line_length=settings["CALENDAR_LINE_LENGTH"]) |
paul@1174 | 369 | |
paul@1174 | 370 | def object_to_part(self, method, obj): |
paul@1174 | 371 | |
paul@1174 | 372 | "Return an encoded MIME part for the given 'method' and 'obj'." |
paul@1174 | 373 | |
paul@1212 | 374 | return obj.to_part(method, line_length=settings["CALENDAR_LINE_LENGTH"]) |
paul@1174 | 375 | |
paul@864 | 376 | # Preparation of messages communicating the state of events. |
paul@864 | 377 | |
paul@864 | 378 | def get_message_parts(self, obj, method, attendee=None): |
paul@864 | 379 | |
paul@864 | 380 | """ |
paul@864 | 381 | Return a tuple containing a list of methods and a list of message parts, |
paul@864 | 382 | with the parts collectively describing the given object 'obj' and its |
paul@864 | 383 | recurrences, using 'method' as the means of publishing details (with |
paul@864 | 384 | CANCEL being used to retract or remove details). |
paul@864 | 385 | |
paul@864 | 386 | If 'attendee' is indicated, the attendee's participation will be taken |
paul@864 | 387 | into account when generating the description. |
paul@864 | 388 | """ |
paul@864 | 389 | |
paul@864 | 390 | # Assume that the outcome will be composed of requests and |
paul@864 | 391 | # cancellations. It would not seem completely bizarre to produce |
paul@864 | 392 | # publishing messages if a refresh message was unprovoked. |
paul@864 | 393 | |
paul@864 | 394 | responses = [] |
paul@864 | 395 | methods = set() |
paul@864 | 396 | |
paul@864 | 397 | # Get the parent event, add SENT-BY details to the organiser. |
paul@864 | 398 | |
paul@864 | 399 | if not attendee or self.is_participating(attendee, obj=obj): |
paul@1281 | 400 | self.update_sender(obj) |
paul@1174 | 401 | responses.append(self.object_to_part(method, obj)) |
paul@864 | 402 | methods.add(method) |
paul@864 | 403 | |
paul@864 | 404 | # Get recurrences for parent events. |
paul@864 | 405 | |
paul@864 | 406 | if not self.recurrenceid: |
paul@864 | 407 | |
paul@864 | 408 | # Collect active and cancelled recurrences. |
paul@864 | 409 | |
paul@864 | 410 | for rl, section, rmethod in [ |
paul@864 | 411 | (self.store.get_active_recurrences(self.user, self.uid), None, method), |
paul@864 | 412 | (self.store.get_cancelled_recurrences(self.user, self.uid), "cancellations", "CANCEL"), |
paul@864 | 413 | ]: |
paul@864 | 414 | |
paul@864 | 415 | for recurrenceid in rl: |
paul@864 | 416 | |
paul@864 | 417 | # Get the recurrence, add SENT-BY details to the organiser. |
paul@864 | 418 | |
paul@864 | 419 | obj = self.get_stored_object(self.uid, recurrenceid, section) |
paul@864 | 420 | |
paul@864 | 421 | if not attendee or self.is_participating(attendee, obj=obj): |
paul@1281 | 422 | self.update_sender(obj) |
paul@1174 | 423 | responses.append(self.object_to_part(rmethod, obj)) |
paul@864 | 424 | methods.add(rmethod) |
paul@864 | 425 | |
paul@864 | 426 | return methods, responses |
paul@864 | 427 | |
paul@601 | 428 | class ClientForObject(Client): |
paul@601 | 429 | |
paul@601 | 430 | "A client maintaining a specific object." |
paul@601 | 431 | |
paul@1039 | 432 | def __init__(self, obj, user, messenger=None, store=None, publisher=None, |
paul@1039 | 433 | journal=None, preferences_dir=None): |
paul@1039 | 434 | Client.__init__(self, user, messenger, store, publisher, journal, preferences_dir) |
paul@601 | 435 | self.set_object(obj) |
paul@601 | 436 | |
paul@601 | 437 | def set_object(self, obj): |
paul@606 | 438 | |
paul@606 | 439 | "Set the current object to 'obj', obtaining metadata details." |
paul@606 | 440 | |
paul@601 | 441 | self.obj = obj |
paul@601 | 442 | self.uid = obj and self.obj.get_uid() |
paul@601 | 443 | self.recurrenceid = obj and self.obj.get_recurrenceid() |
paul@601 | 444 | self.sequence = obj and self.obj.get_value("SEQUENCE") |
paul@601 | 445 | self.dtstamp = obj and self.obj.get_value("DTSTAMP") |
paul@601 | 446 | |
paul@1276 | 447 | # Set the fallback time zone. |
paul@1276 | 448 | |
paul@1276 | 449 | if obj: |
paul@1276 | 450 | self.obj.set_tzid(self.get_tzid()) |
paul@1276 | 451 | |
paul@1302 | 452 | def new_object(self, objtype): |
paul@1302 | 453 | |
paul@1302 | 454 | "Initialise a new object for the client with the given 'objtype'." |
paul@1302 | 455 | |
paul@1321 | 456 | self.set_object(new_object(objtype, self.user, self.get_user_attributes(), self.get_tzid())) |
paul@1302 | 457 | return self.obj |
paul@1302 | 458 | |
paul@1272 | 459 | def load_object(self, uid, recurrenceid): |
paul@1272 | 460 | |
paul@1272 | 461 | "Load the object with the given 'uid' and 'recurrenceid'." |
paul@1272 | 462 | |
paul@1272 | 463 | self.set_object(self.get_stored_object(uid, recurrenceid)) |
paul@1272 | 464 | |
paul@729 | 465 | def set_identity(self, method): |
paul@729 | 466 | |
paul@729 | 467 | """ |
paul@729 | 468 | Set the current user for the current object in the context of the given |
paul@729 | 469 | 'method'. It is usually set when initialising the handler, using the |
paul@729 | 470 | recipient details, but outgoing messages do not reference the recipient |
paul@729 | 471 | in this way. |
paul@729 | 472 | """ |
paul@729 | 473 | |
paul@729 | 474 | pass |
paul@729 | 475 | |
paul@727 | 476 | def is_usable(self, method=None): |
paul@720 | 477 | |
paul@727 | 478 | "Return whether the current object is usable with the given 'method'." |
paul@720 | 479 | |
paul@720 | 480 | return True |
paul@720 | 481 | |
paul@1302 | 482 | def is_attendee(self): |
paul@1302 | 483 | |
paul@1302 | 484 | "Return whether the current user is an attendee in the current object." |
paul@1302 | 485 | |
paul@1302 | 486 | return self.obj.get_value_map("ATTENDEE").has_key(self.user) |
paul@1302 | 487 | |
paul@818 | 488 | def is_organiser(self): |
paul@818 | 489 | |
paul@818 | 490 | """ |
paul@818 | 491 | Return whether the current user is the organiser in the current object. |
paul@818 | 492 | """ |
paul@818 | 493 | |
paul@818 | 494 | return get_uri(self.obj.get_value("ORGANIZER")) == self.user |
paul@818 | 495 | |
paul@1123 | 496 | def is_recurrence(self): |
paul@1123 | 497 | |
paul@1123 | 498 | "Return whether the current object is a recurrence of its parent." |
paul@1123 | 499 | |
paul@1123 | 500 | parent = self.get_parent_object() |
paul@1276 | 501 | return parent and parent.has_recurrence(self.obj.get_recurrenceid()) |
paul@1123 | 502 | |
paul@1302 | 503 | def get_recurrences(self, uid=None): |
paul@1268 | 504 | |
paul@1268 | 505 | "Return the current object's recurrence identifiers." |
paul@1268 | 506 | |
paul@1302 | 507 | return self.store.get_recurrences(self.user, uid or self.uid) |
paul@1268 | 508 | |
paul@1268 | 509 | def get_periods(self, obj=None, explicit_only=False, future_only=False): |
paul@1268 | 510 | |
paul@1268 | 511 | "Return the periods provided by the current object." |
paul@1268 | 512 | |
paul@1268 | 513 | return Client.get_periods(self, obj or self.obj, explicit_only, future_only) |
paul@1268 | 514 | |
paul@1302 | 515 | def get_updated_periods(self, obj=None): |
paul@1268 | 516 | |
paul@1268 | 517 | """ |
paul@1268 | 518 | Return the periods provided by the current object and associated |
paul@1302 | 519 | recurrence instances. |
paul@1268 | 520 | """ |
paul@1268 | 521 | |
paul@1302 | 522 | return Client.get_updated_periods(self, obj or self.obj) |
paul@1268 | 523 | |
paul@1039 | 524 | # Common operations on calendar data. |
paul@1039 | 525 | |
paul@1281 | 526 | def update_sender(self, obj=None): |
paul@1281 | 527 | |
paul@1281 | 528 | """ |
paul@1281 | 529 | Update sender details in 'obj', or the current object if not indicated, |
paul@1281 | 530 | modifying the organiser attributes. |
paul@1281 | 531 | """ |
paul@1281 | 532 | |
paul@1281 | 533 | obj = obj or self.obj |
paul@1281 | 534 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@1281 | 535 | self.update_sender_attr(organiser_attr) |
paul@1281 | 536 | |
paul@1039 | 537 | def update_senders(self, obj=None): |
paul@1039 | 538 | |
paul@1039 | 539 | """ |
paul@1039 | 540 | Update sender details in 'obj', or the current object if not indicated, |
paul@1039 | 541 | removing SENT-BY attributes for attendees other than the current user if |
paul@1039 | 542 | those attributes give the URI of the calendar system. |
paul@1039 | 543 | """ |
paul@1039 | 544 | |
paul@1039 | 545 | obj = obj or self.obj |
paul@1039 | 546 | calendar_uri = self.messenger and get_uri(self.messenger.sender) |
paul@1281 | 547 | |
paul@1302 | 548 | for attendee, attendee_attr in uri_items(obj.get_items("ATTENDEE") or []): |
paul@1302 | 549 | |
paul@1302 | 550 | # Fix up the SENT-BY attribute for this user. |
paul@1302 | 551 | |
paul@1302 | 552 | if attendee == self.user: |
paul@1302 | 553 | self.update_sender_attr(attendee_attr) |
paul@1302 | 554 | |
paul@1302 | 555 | # Remove any conflicting SENT-BY attributes for other users. |
paul@1302 | 556 | |
paul@1302 | 557 | elif attendee_attr.get("SENT-BY") == calendar_uri: |
paul@1302 | 558 | del attendee_attr["SENT-BY"] |
paul@1039 | 559 | |
paul@1039 | 560 | def get_sending_attendee(self): |
paul@1039 | 561 | |
paul@1039 | 562 | "Return the attendee who sent the current object." |
paul@1039 | 563 | |
paul@1039 | 564 | # Search for the sender of the message or the calendar system address. |
paul@1039 | 565 | |
paul@1325 | 566 | senders = set(uri_values(self.senders or self.messenger and [self.messenger.sender] or [])) |
paul@1325 | 567 | |
paul@1325 | 568 | if senders: |
paul@1325 | 569 | |
paul@1325 | 570 | # Obtain a mapping from sender URI to attendee URI, where the sender |
paul@1325 | 571 | # is taken from the SENT-BY attribute if present, or from the |
paul@1325 | 572 | # attendee value otherwise. |
paul@1039 | 573 | |
paul@1325 | 574 | sent_by = get_sender_identities(uri_dict(self.obj.get_value_map("ATTENDEE"))) |
paul@1325 | 575 | |
paul@1325 | 576 | # Obtain the attendee for the first sender matching the SENT-BY or |
paul@1325 | 577 | # attendee value. |
paul@1325 | 578 | |
paul@1325 | 579 | for sender in senders.intersection(sent_by.keys()): |
paul@1325 | 580 | return sent_by[sender][0] |
paul@1039 | 581 | |
paul@1039 | 582 | return None |
paul@1039 | 583 | |
paul@604 | 584 | # Object update methods. |
paul@601 | 585 | |
paul@676 | 586 | def update_recurrenceid(self): |
paul@676 | 587 | |
paul@676 | 588 | """ |
paul@676 | 589 | Update the RECURRENCE-ID in the current object, initialising it from |
paul@676 | 590 | DTSTART. |
paul@676 | 591 | """ |
paul@676 | 592 | |
paul@680 | 593 | self.obj["RECURRENCE-ID"] = [self.obj.get_item("DTSTART")] |
paul@676 | 594 | self.recurrenceid = self.obj.get_recurrenceid() |
paul@676 | 595 | |
paul@809 | 596 | def update_dtstamp(self, obj=None): |
paul@601 | 597 | |
paul@809 | 598 | "Update the DTSTAMP in the current object or any given object 'obj'." |
paul@809 | 599 | |
paul@809 | 600 | obj = obj or self.obj |
paul@809 | 601 | self.dtstamp = obj.update_dtstamp() |
paul@601 | 602 | |
paul@1281 | 603 | def update_sequence(self, obj=None): |
paul@601 | 604 | |
paul@809 | 605 | "Update the SEQUENCE in the current object or any given object 'obj'." |
paul@601 | 606 | |
paul@809 | 607 | obj = obj or self.obj |
paul@1281 | 608 | obj.update_sequence(self.is_organiser()) |
paul@601 | 609 | |
paul@606 | 610 | def merge_attendance(self, attendees): |
paul@606 | 611 | |
paul@606 | 612 | """ |
paul@606 | 613 | Merge attendance from the current object's 'attendees' into the version |
paul@606 | 614 | stored for the current user. |
paul@606 | 615 | """ |
paul@606 | 616 | |
paul@606 | 617 | obj = self.get_stored_object_version() |
paul@606 | 618 | |
paul@739 | 619 | if not obj or not self.have_new_object(): |
paul@606 | 620 | return False |
paul@606 | 621 | |
paul@606 | 622 | # Get attendee details in a usable form. |
paul@606 | 623 | |
paul@1326 | 624 | stored_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@606 | 625 | |
paul@606 | 626 | for attendee, attendee_attr in attendees.items(): |
paul@606 | 627 | |
paul@836 | 628 | # Update attendance in the loaded object for any recognised |
paul@836 | 629 | # attendees. |
paul@606 | 630 | |
paul@1326 | 631 | if stored_attendees.has_key(attendee): |
paul@1326 | 632 | stored_attendees[attendee] = attendee_attr |
paul@1176 | 633 | |
paul@1326 | 634 | update_attendees_with_delegates(stored_attendees, attendees) |
paul@1176 | 635 | |
paul@606 | 636 | # Set the new details and store the object. |
paul@606 | 637 | |
paul@1326 | 638 | obj["ATTENDEE"] = stored_attendees.items() |
paul@606 | 639 | |
paul@744 | 640 | # Set a specific recurrence or the complete event if not an additional |
paul@744 | 641 | # occurrence. |
paul@606 | 642 | |
paul@829 | 643 | return self.store.set_event(self.user, self.uid, self.recurrenceid, obj.to_node()) |
paul@606 | 644 | |
paul@1302 | 645 | def update_attendees(self, to_invite, to_cancel, to_modify): |
paul@818 | 646 | |
paul@818 | 647 | """ |
paul@1302 | 648 | Update the attendees in the current object with the given 'to_invite', |
paul@1302 | 649 | 'to_cancel' and 'to_modify' attendee mappings. |
paul@818 | 650 | """ |
paul@818 | 651 | |
paul@1302 | 652 | attendees = uri_items(self.obj.get_items("ATTENDEE") or []) |
paul@1302 | 653 | attendee_map = OrderedDict(attendees) |
paul@818 | 654 | |
paul@1302 | 655 | # Normalise the identities. |
paul@818 | 656 | |
paul@1302 | 657 | to_invite = uri_dict(to_invite) |
paul@1302 | 658 | to_cancel = uri_dict(to_cancel) |
paul@1302 | 659 | to_modify = uri_dict(to_modify) |
paul@818 | 660 | |
paul@1302 | 661 | if self.is_organiser(): |
paul@818 | 662 | |
paul@1302 | 663 | # Remove uninvited attendees. |
paul@818 | 664 | |
paul@1302 | 665 | for attendee in to_cancel.keys(): |
paul@1302 | 666 | if attendee_map.has_key(attendee): |
paul@1302 | 667 | del attendee_map[attendee] |
paul@818 | 668 | |
paul@1302 | 669 | # Attendees (when countering) must only include the current user and |
paul@1302 | 670 | # any added attendees. |
paul@818 | 671 | |
paul@1302 | 672 | else: |
paul@1302 | 673 | attr = attendee_map.get(self.user) or self.get_user_attributes() |
paul@1302 | 674 | attendee_map = {self.user : attr} |
paul@818 | 675 | |
paul@1302 | 676 | # Update modified attendees. |
paul@818 | 677 | |
paul@1302 | 678 | for attendee, attr in to_modify.items(): |
paul@1302 | 679 | existing_attr = attendee_map.get(attendee) |
paul@1302 | 680 | if existing_attr: |
paul@1302 | 681 | existing_attr.update(attr) |
paul@818 | 682 | |
paul@1302 | 683 | # Add newly-invited attendees, applicable for organisers and attendees |
paul@1302 | 684 | # (when countering). |
paul@818 | 685 | |
paul@1302 | 686 | for attendee, attr in to_invite.items(): |
paul@1302 | 687 | if not attendee_map.has_key(attendee): |
paul@818 | 688 | |
paul@1302 | 689 | # Only the organiser can reset the participation attributes. |
paul@818 | 690 | |
paul@1316 | 691 | if self.is_organiser() and attendee != self.user: |
paul@1302 | 692 | attr.update({"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"}) |
paul@818 | 693 | |
paul@1302 | 694 | attendee_map[attendee] = attr |
paul@818 | 695 | |
paul@1302 | 696 | self.obj["ATTENDEE"] = attendee_map.items() |
paul@818 | 697 | |
paul@818 | 698 | def update_participation(self, partstat=None): |
paul@818 | 699 | |
paul@818 | 700 | """ |
paul@818 | 701 | Update the participation in the current object of the user with the |
paul@818 | 702 | given 'partstat'. |
paul@818 | 703 | """ |
paul@818 | 704 | |
paul@818 | 705 | attendee_attr = uri_dict(self.obj.get_value_map("ATTENDEE")).get(self.user) |
paul@1284 | 706 | |
paul@818 | 707 | if not attendee_attr: |
paul@818 | 708 | return None |
paul@1284 | 709 | |
paul@1284 | 710 | # Set the participation and remove any request for response indicator. |
paul@1284 | 711 | |
paul@818 | 712 | if partstat: |
paul@818 | 713 | attendee_attr["PARTSTAT"] = partstat |
paul@818 | 714 | if attendee_attr.has_key("RSVP"): |
paul@818 | 715 | del attendee_attr["RSVP"] |
paul@1284 | 716 | |
paul@818 | 717 | return attendee_attr |
paul@818 | 718 | |
paul@1302 | 719 | def update_event_version(self, changed=False): |
paul@909 | 720 | |
paul@909 | 721 | """ |
paul@1284 | 722 | Update the event version information and details for sending. Where |
paul@1284 | 723 | 'changed' is set to a true value for the attendee, the attendee list |
paul@1284 | 724 | will be processed and preserved; otherwise, the attendee replying will |
paul@1284 | 725 | be retained and the others removed from the object. |
paul@909 | 726 | """ |
paul@909 | 727 | |
paul@1284 | 728 | if self.is_organiser(): |
paul@1284 | 729 | self.update_sender() |
paul@1284 | 730 | |
paul@1284 | 731 | # Process attendee SENT-BY usage, timestamp and sequence details |
paul@1284 | 732 | # appropriately for the sender's role. |
paul@1284 | 733 | |
paul@1284 | 734 | self.update_senders() |
paul@1284 | 735 | self.update_dtstamp() |
paul@1284 | 736 | self.update_sequence() |
paul@1284 | 737 | |
paul@1284 | 738 | return True |
paul@1284 | 739 | |
paul@1302 | 740 | def update_event_from_periods(self, to_set, to_exclude): |
paul@909 | 741 | |
paul@909 | 742 | """ |
paul@1302 | 743 | Set the periods in any redefined event from the 'to_set' list, excluding |
paul@1302 | 744 | the main period if it appears in 'to_exclude'. |
paul@909 | 745 | """ |
paul@909 | 746 | |
paul@1302 | 747 | if to_set: |
paul@1302 | 748 | self.obj.set_periods(to_set) |
paul@1302 | 749 | |
paul@1318 | 750 | if to_exclude: |
paul@1318 | 751 | self.obj.update_exceptions(to_exclude, to_set or []) |
paul@1302 | 752 | |
paul@1281 | 753 | # General message generation methods. |
paul@909 | 754 | |
paul@1281 | 755 | def get_recipients(self, obj=None): |
paul@909 | 756 | |
paul@909 | 757 | """ |
paul@1281 | 758 | Return recipients for 'obj' (or the current object) dependent on the |
paul@1281 | 759 | current user's role. |
paul@909 | 760 | """ |
paul@909 | 761 | |
paul@1281 | 762 | obj = obj or self.obj |
paul@1281 | 763 | |
paul@1281 | 764 | organiser = get_uri(obj.get_value("ORGANIZER")) |
paul@1281 | 765 | attendees = uri_values(obj.get_values("ATTENDEE")) |
paul@1281 | 766 | |
paul@909 | 767 | # As organiser, send an invitation to attendees, excluding oneself if |
paul@909 | 768 | # also attending. The updated event will be saved by the outgoing |
paul@909 | 769 | # handler. |
paul@909 | 770 | |
paul@1281 | 771 | if self.is_organiser(): |
paul@1281 | 772 | return [get_address(attendee) for attendee in attendees if attendee != self.user] |
paul@909 | 773 | else: |
paul@1281 | 774 | return [get_address(organiser)] |
paul@1281 | 775 | |
paul@1281 | 776 | def attach_freebusy(self, parts): |
paul@909 | 777 | |
paul@1281 | 778 | """ |
paul@1281 | 779 | Since the outgoing handler updates this user's free/busy details, the |
paul@1281 | 780 | stored details will probably not have the updated details straight away, |
paul@1281 | 781 | so we update our copy for serialisation as the bundled free/busy object. |
paul@1281 | 782 | """ |
paul@909 | 783 | |
paul@1097 | 784 | freebusy = self.store.get_freebusy(self.user).copy() |
paul@1281 | 785 | self.update_freebusy(freebusy, self.user, self.is_organiser()) |
paul@909 | 786 | |
paul@909 | 787 | # Bundle free/busy information if appropriate. |
paul@909 | 788 | |
paul@909 | 789 | part = self.get_freebusy_part(freebusy) |
paul@909 | 790 | if part: |
paul@909 | 791 | parts.append(part) |
paul@909 | 792 | |
paul@1281 | 793 | def make_message(self, parts, recipients, bcc_sender=False): |
paul@909 | 794 | |
paul@909 | 795 | """ |
paul@1281 | 796 | Send the given 'parts' to the appropriate 'recipients', also sending a |
paul@1281 | 797 | copy to the sender. |
paul@909 | 798 | """ |
paul@909 | 799 | |
paul@1005 | 800 | if not self.messenger: |
paul@1281 | 801 | return None |
paul@909 | 802 | |
paul@1281 | 803 | # Update and attach bundled free/busy details. |
paul@1281 | 804 | |
paul@1281 | 805 | self.attach_freebusy(parts) |
paul@1281 | 806 | |
paul@1281 | 807 | if not bcc_sender: |
paul@1281 | 808 | return self.messenger.make_outgoing_message(parts, recipients) |
paul@1281 | 809 | else: |
paul@1281 | 810 | sender = get_address(self.user) |
paul@1281 | 811 | return self.messenger.make_outgoing_message(parts, recipients, outgoing_bcc=sender) |
paul@1281 | 812 | |
paul@1281 | 813 | def send_message(self, message, recipients, bcc_sender=False): |
paul@909 | 814 | |
paul@909 | 815 | """ |
paul@1281 | 816 | Send 'message' to 'recipients', explicitly specifying the sender as an |
paul@1281 | 817 | outgoing BCC recipient if 'bcc_sender' is set, since the generic |
paul@1281 | 818 | calendar user will be the actual sender. |
paul@909 | 819 | """ |
paul@909 | 820 | |
paul@1281 | 821 | if not recipients and not bcc_sender or not self.messenger: |
paul@1005 | 822 | return |
paul@1005 | 823 | |
paul@909 | 824 | if not bcc_sender: |
paul@909 | 825 | self.messenger.sendmail(recipients, message.as_string()) |
paul@909 | 826 | else: |
paul@1283 | 827 | sender = get_address(self.user) |
paul@909 | 828 | self.messenger.sendmail(recipients, message.as_string(), outgoing_bcc=sender) |
paul@909 | 829 | |
paul@1281 | 830 | def make_message_for_self(self, parts): |
paul@1281 | 831 | |
paul@1281 | 832 | "Send 'message' to the current user." |
paul@1281 | 833 | |
paul@1281 | 834 | if not self.messenger: |
paul@1281 | 835 | return None |
paul@909 | 836 | |
paul@1281 | 837 | sender = get_address(self.user) |
paul@1281 | 838 | return self.messenger.make_outgoing_message(parts, [sender]) |
paul@1281 | 839 | |
paul@1281 | 840 | def send_message_to_self(self, message): |
paul@1281 | 841 | |
paul@1281 | 842 | "Send 'message' to the current user." |
paul@909 | 843 | |
paul@1005 | 844 | if not self.messenger: |
paul@1005 | 845 | return |
paul@1005 | 846 | |
paul@909 | 847 | sender = get_address(self.user) |
paul@909 | 848 | self.messenger.sendmail([sender], message.as_string()) |
paul@909 | 849 | |
paul@1281 | 850 | # Specific message generation methods. |
paul@1281 | 851 | |
paul@1281 | 852 | def get_rescheduled_parts(self, periods, method): |
paul@1281 | 853 | |
paul@1281 | 854 | """ |
paul@1281 | 855 | Return message parts describing rescheduled 'periods' affected by 'method'. |
paul@1281 | 856 | """ |
paul@1281 | 857 | |
paul@1281 | 858 | rescheduled_parts = [] |
paul@1281 | 859 | |
paul@1281 | 860 | if periods: |
paul@1281 | 861 | |
paul@1281 | 862 | # Duplicate the core of the object without any period information. |
paul@1281 | 863 | |
paul@1281 | 864 | obj = self.obj.copy() |
paul@1281 | 865 | obj.remove_all(["RRULE", "RDATE", "DTSTART", "DTEND", "DURATION"]) |
paul@1281 | 866 | |
paul@1281 | 867 | for p in periods: |
paul@1281 | 868 | if not p.origin: |
paul@1281 | 869 | continue |
paul@1281 | 870 | |
paul@1281 | 871 | # Set specific recurrence information. |
paul@909 | 872 | |
paul@1281 | 873 | obj.set_datetime("DTSTART", p.get_start()) |
paul@1281 | 874 | obj.set_datetime("DTEND", p.get_end()) |
paul@1281 | 875 | |
paul@1281 | 876 | # Acquire the original recurrence identifier associated with |
paul@1281 | 877 | # this period. This may differ where the start of the period has |
paul@1281 | 878 | # changed. |
paul@1281 | 879 | |
paul@1281 | 880 | dt, attr = p.get_recurrenceid_item() |
paul@1281 | 881 | obj["RECURRENCE-ID"] = [(format_datetime(dt), attr)] |
paul@1281 | 882 | |
paul@1281 | 883 | rescheduled_parts.append(self.object_to_part(method, obj)) |
paul@1281 | 884 | |
paul@1281 | 885 | return rescheduled_parts |
paul@1281 | 886 | |
paul@1302 | 887 | def make_update_message(self, recipients, update_parent=False, |
paul@1302 | 888 | to_unschedule=None, to_reschedule=None, |
paul@1302 | 889 | all_unscheduled=None, all_rescheduled=None, |
paul@1302 | 890 | to_add=None): |
paul@1281 | 891 | |
paul@1281 | 892 | """ |
paul@1281 | 893 | Prepare event updates from the organiser of an event for the given |
paul@1302 | 894 | 'recipients', including the parent event if 'update_parent' is set to a |
paul@1302 | 895 | true value. |
paul@1302 | 896 | |
paul@1302 | 897 | Additional parts are provided by the 'to_unschedule' and 'to_reschedule' |
paul@1302 | 898 | collections. Alternatively, where the parent event is being updated, the |
paul@1302 | 899 | 'all_unscheduled' and 'all_rescheduled' period collections are included. |
paul@1302 | 900 | |
paul@1302 | 901 | The 'to_add' period collection augments the existing periods. |
paul@1281 | 902 | """ |
paul@1281 | 903 | |
paul@1302 | 904 | parts = [] |
paul@909 | 905 | |
paul@1302 | 906 | if update_parent: |
paul@1302 | 907 | parts.append(self.object_to_part("REQUEST", self.obj)) |
paul@1302 | 908 | unscheduled = all_unscheduled |
paul@1302 | 909 | rescheduled = all_rescheduled |
paul@1302 | 910 | else: |
paul@1302 | 911 | unscheduled = to_unschedule |
paul@1302 | 912 | rescheduled = to_reschedule |
paul@1281 | 913 | |
paul@1302 | 914 | parts += self.get_rescheduled_parts(unscheduled, "CANCEL") |
paul@1302 | 915 | parts += self.get_rescheduled_parts(rescheduled, "REQUEST") |
paul@1302 | 916 | parts += self.get_rescheduled_parts(to_add, "ADD") |
paul@1302 | 917 | return self.make_message(parts, recipients) |
paul@1281 | 918 | |
paul@1302 | 919 | def make_self_update_message(self, all_unscheduled=None, all_rescheduled=None, |
paul@1302 | 920 | to_add=None): |
paul@1281 | 921 | |
paul@1281 | 922 | """ |
paul@1281 | 923 | Prepare event updates to be sent from the organiser of an event to |
paul@1281 | 924 | themself. |
paul@1281 | 925 | """ |
paul@1281 | 926 | |
paul@1281 | 927 | parts = [self.object_to_part("PUBLISH", self.obj)] |
paul@1302 | 928 | parts += self.get_rescheduled_parts(all_unscheduled, "CANCEL") |
paul@1302 | 929 | parts += self.get_rescheduled_parts(all_rescheduled, "PUBLISH") |
paul@1302 | 930 | parts += self.get_rescheduled_parts(to_add, "ADD") |
paul@1302 | 931 | return self.make_message_for_self(parts) |
paul@1281 | 932 | |
paul@1302 | 933 | def make_response_message(self, recipients, update_parent=False, |
paul@1302 | 934 | all_rescheduled=None, to_reschedule=None): |
paul@909 | 935 | |
paul@1281 | 936 | """ |
paul@1302 | 937 | Prepare a response to 'recipients', including the parent event if |
paul@1302 | 938 | 'update_parent' is set to a true value, incorporating 'all_rescheduled' |
paul@1302 | 939 | periods, of which there may be indicated periods 'to_reschedule'. |
paul@1281 | 940 | """ |
paul@1281 | 941 | |
paul@1302 | 942 | parts = [self.object_to_part(update_parent and "COUNTER" or "REPLY", self.obj)] |
paul@1302 | 943 | |
paul@1302 | 944 | # Determine existing replaced periods that are not newly rescheduled. |
paul@1302 | 945 | |
paul@1302 | 946 | rescheduled_unmodified = set(all_rescheduled or []).difference(to_reschedule or []) |
paul@1302 | 947 | |
paul@1302 | 948 | if rescheduled_unmodified: |
paul@1302 | 949 | parts += self.get_rescheduled_parts(rescheduled_unmodified, update_parent and "COUNTER" or "REPLY") |
paul@1281 | 950 | |
paul@1302 | 951 | # Suggest details for newly rescheduled periods. |
paul@1281 | 952 | |
paul@1302 | 953 | if to_reschedule: |
paul@1302 | 954 | parts += self.get_rescheduled_parts(to_reschedule, "COUNTER") |
paul@1302 | 955 | |
paul@1302 | 956 | return self.make_message(parts, recipients, bcc_sender=True) |
paul@1302 | 957 | |
paul@1302 | 958 | def make_cancel_message(self, to_cancel=None): |
paul@1005 | 959 | |
paul@1281 | 960 | """ |
paul@1302 | 961 | Prepare an event cancellation message involving the participants in the |
paul@1302 | 962 | 'to_cancel' mapping. |
paul@1281 | 963 | """ |
paul@1281 | 964 | |
paul@1302 | 965 | if not to_cancel: |
paul@1302 | 966 | return None |
paul@1302 | 967 | |
paul@1302 | 968 | obj = self.obj.copy() |
paul@1302 | 969 | obj["ATTENDEE"] = to_cancel.items() |
paul@1302 | 970 | |
paul@1281 | 971 | parts = [self.object_to_part("CANCEL", obj)] |
paul@1302 | 972 | return self.make_message(parts, to_cancel.keys()) |
paul@1281 | 973 | |
paul@1281 | 974 | def make_cancel_message_for_self(self, obj): |
paul@1281 | 975 | |
paul@1281 | 976 | "Prepare an event cancellation for the current user." |
paul@1281 | 977 | |
paul@1281 | 978 | parts = [self.object_to_part("CANCEL", obj)] |
paul@1281 | 979 | return self.make_message_for_self(parts) |
paul@909 | 980 | |
paul@909 | 981 | # Action methods. |
paul@909 | 982 | |
paul@1302 | 983 | def send_declined_counter_to_attendee(self, attendee): |
paul@909 | 984 | |
paul@1302 | 985 | "Send a declined counter-proposal to 'attendee'." |
paul@909 | 986 | |
paul@909 | 987 | # Obtain the counter-proposal for the attendee. |
paul@909 | 988 | |
paul@909 | 989 | obj = self.get_stored_object(self.uid, self.recurrenceid, "counters", attendee) |
paul@909 | 990 | if not obj: |
paul@1302 | 991 | return |
paul@909 | 992 | |
paul@1281 | 993 | self.update_senders(obj) |
paul@909 | 994 | obj.update_dtstamp() |
paul@1281 | 995 | obj.update_sequence() |
paul@1283 | 996 | |
paul@1302 | 997 | parts = [self.object_to_part("DECLINECOUNTER", obj)] |
paul@1283 | 998 | |
paul@1283 | 999 | # Create and send the response. |
paul@909 | 1000 | |
paul@1283 | 1001 | recipients = self.get_recipients(obj) |
paul@1283 | 1002 | message = self.make_message(parts, recipients, bcc_sender=True) |
paul@1283 | 1003 | self.send_message(message, recipients, bcc_sender=True) |
paul@1283 | 1004 | |
paul@1302 | 1005 | def send_response_to_organiser(self, all_rescheduled=None, to_reschedule=None, |
paul@1302 | 1006 | changed=False): |
paul@909 | 1007 | |
paul@909 | 1008 | """ |
paul@1302 | 1009 | Send a response to the organiser describing attendance and proposed |
paul@1302 | 1010 | amendments to the event. |
paul@909 | 1011 | |
paul@1302 | 1012 | If 'all_rescheduled' is specified, it provides details of separate |
paul@1302 | 1013 | recurrence instances for which a response needs to be generated. |
paul@909 | 1014 | |
paul@1302 | 1015 | If 'to_reschedule' provides rescheduled periods, these will be sent as |
paul@1302 | 1016 | counter-proposals. |
paul@909 | 1017 | |
paul@1302 | 1018 | If 'changed' is set to a true value, a counter-proposal will be sent for |
paul@1302 | 1019 | the entire event instead of a reply. |
paul@909 | 1020 | """ |
paul@909 | 1021 | |
paul@1281 | 1022 | recipients = self.get_recipients() |
paul@1302 | 1023 | message = self.make_response_message(recipients, all_rescheduled, |
paul@1302 | 1024 | to_reschedule, changed) |
paul@1281 | 1025 | self.send_message(message, recipients, bcc_sender=True) |
paul@909 | 1026 | |
paul@1302 | 1027 | def send_update_to_recipients(self, to_unschedule=None, to_reschedule=None): |
paul@909 | 1028 | |
paul@909 | 1029 | """ |
paul@1302 | 1030 | Send cancellations for each of the recurrences 'to_unschedule' along |
paul@1302 | 1031 | with modifications for each of the recurrences 'to_reschedule'. |
paul@1302 | 1032 | """ |
paul@909 | 1033 | |
paul@1302 | 1034 | recipients = self.get_recipients() |
paul@1302 | 1035 | message = self.make_update_message(recipients, to_unschedule, to_reschedule) |
paul@1302 | 1036 | self.send_message(message, recipients) |
paul@909 | 1037 | |
paul@1302 | 1038 | def send_publish_to_self(self, all_unscheduled=None, all_rescheduled=None): |
paul@909 | 1039 | |
paul@1302 | 1040 | """ |
paul@1302 | 1041 | Send published event details incorporating 'all_unscheduled' and |
paul@1302 | 1042 | 'all_rescheduled' periods. |
paul@909 | 1043 | """ |
paul@909 | 1044 | |
paul@1302 | 1045 | # Since the organiser can update the SEQUENCE but this can leave any |
paul@1302 | 1046 | # mail/calendar client lagging, issue a PUBLISH message to the |
paul@1302 | 1047 | # user's address. |
paul@1268 | 1048 | |
paul@1302 | 1049 | recipients = self.get_recipients() |
paul@1302 | 1050 | message = self.make_self_update_message(all_unscheduled, all_rescheduled) |
paul@1302 | 1051 | self.send_message_to_self(message) |
paul@909 | 1052 | |
paul@1302 | 1053 | def send_cancel_to_recipients(self, to_cancel=None): |
paul@909 | 1054 | |
paul@1302 | 1055 | "Send a cancellation to all uninvited attendees in 'to_cancel'." |
paul@909 | 1056 | |
paul@1302 | 1057 | message = self.make_cancel_message(to_cancel) |
paul@1302 | 1058 | self.send_message(message, to_cancel.keys()) |
paul@909 | 1059 | |
paul@1302 | 1060 | def send_cancel_to_self(self): |
paul@909 | 1061 | |
paul@1302 | 1062 | "Issue a CANCEL message to the user's address." |
paul@909 | 1063 | |
paul@1302 | 1064 | message = self.make_cancel_message_for_self(self.obj) |
paul@1302 | 1065 | self.send_message_to_self(message) |
paul@909 | 1066 | |
paul@606 | 1067 | # Object-related tests. |
paul@606 | 1068 | |
paul@728 | 1069 | def is_recognised_organiser(self, organiser): |
paul@728 | 1070 | |
paul@728 | 1071 | """ |
paul@728 | 1072 | Return whether the given 'organiser' is recognised from |
paul@728 | 1073 | previously-received details. If no stored details exist, True is |
paul@728 | 1074 | returned. |
paul@728 | 1075 | """ |
paul@728 | 1076 | |
paul@728 | 1077 | obj = self.get_stored_object_version() |
paul@728 | 1078 | if obj: |
paul@728 | 1079 | stored_organiser = get_uri(obj.get_value("ORGANIZER")) |
paul@728 | 1080 | return stored_organiser == organiser |
paul@728 | 1081 | else: |
paul@728 | 1082 | return True |
paul@728 | 1083 | |
paul@728 | 1084 | def is_recognised_attendee(self, attendee): |
paul@728 | 1085 | |
paul@728 | 1086 | """ |
paul@728 | 1087 | Return whether the given 'attendee' is recognised from |
paul@728 | 1088 | previously-received details. If no stored details exist, True is |
paul@728 | 1089 | returned. |
paul@728 | 1090 | """ |
paul@728 | 1091 | |
paul@728 | 1092 | obj = self.get_stored_object_version() |
paul@728 | 1093 | if obj: |
paul@728 | 1094 | stored_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@728 | 1095 | return stored_attendees.has_key(attendee) |
paul@728 | 1096 | else: |
paul@728 | 1097 | return True |
paul@728 | 1098 | |
paul@694 | 1099 | def get_attendance(self, user=None, obj=None): |
paul@606 | 1100 | |
paul@606 | 1101 | """ |
paul@606 | 1102 | Return the attendance attributes for 'user', or the current user if |
paul@606 | 1103 | 'user' is not specified. |
paul@606 | 1104 | """ |
paul@606 | 1105 | |
paul@694 | 1106 | attendees = uri_dict((obj or self.obj).get_value_map("ATTENDEE")) |
paul@697 | 1107 | return attendees.get(user or self.user) |
paul@606 | 1108 | |
paul@694 | 1109 | def is_participating(self, user, as_organiser=False, obj=None): |
paul@609 | 1110 | |
paul@609 | 1111 | """ |
paul@609 | 1112 | Return whether, subject to the 'user' indicating an identity and the |
paul@609 | 1113 | 'as_organiser' status of that identity, the user concerned is actually |
paul@609 | 1114 | participating in the current object event. |
paul@609 | 1115 | """ |
paul@609 | 1116 | |
paul@697 | 1117 | # Use any attendee property information for an organiser, not the |
paul@697 | 1118 | # organiser property attributes. |
paul@697 | 1119 | |
paul@917 | 1120 | attr = self.get_attendance(user, obj) |
paul@1176 | 1121 | return as_organiser or attr is not None and not attr or \ |
paul@1176 | 1122 | attr and attr.get("PARTSTAT") not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") |
paul@917 | 1123 | |
paul@917 | 1124 | def has_indicated_attendance(self, user=None, obj=None): |
paul@917 | 1125 | |
paul@917 | 1126 | """ |
paul@917 | 1127 | Return whether the given 'user' (or the current user if not specified) |
paul@917 | 1128 | has indicated attendance in the given 'obj' (or the current object if |
paul@917 | 1129 | not specified). |
paul@917 | 1130 | """ |
paul@917 | 1131 | |
paul@917 | 1132 | attr = self.get_attendance(user, obj) |
paul@917 | 1133 | return attr and attr.get("PARTSTAT") not in (None, "NEEDS-ACTION") |
paul@609 | 1134 | |
paul@609 | 1135 | def get_overriding_transparency(self, user, as_organiser=False): |
paul@609 | 1136 | |
paul@609 | 1137 | """ |
paul@609 | 1138 | Return the overriding transparency to be associated with the free/busy |
paul@609 | 1139 | records for an event, subject to the 'user' indicating an identity and |
paul@609 | 1140 | the 'as_organiser' status of that identity. |
paul@609 | 1141 | |
paul@609 | 1142 | Where an identity is only an organiser and not attending, "ORG" is |
paul@609 | 1143 | returned. Otherwise, no overriding transparency is defined and None is |
paul@609 | 1144 | returned. |
paul@609 | 1145 | """ |
paul@609 | 1146 | |
paul@609 | 1147 | attr = self.get_attendance(user) |
paul@609 | 1148 | return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None |
paul@609 | 1149 | |
paul@606 | 1150 | def can_schedule(self, freebusy, periods): |
paul@606 | 1151 | |
paul@606 | 1152 | """ |
paul@606 | 1153 | Indicate whether within 'freebusy' the given 'periods' can be scheduled. |
paul@606 | 1154 | """ |
paul@606 | 1155 | |
paul@1062 | 1156 | return freebusy.can_schedule(periods, self.uid, self.recurrenceid) |
paul@606 | 1157 | |
paul@739 | 1158 | def have_new_object(self, strict=True): |
paul@606 | 1159 | |
paul@606 | 1160 | """ |
paul@739 | 1161 | Return whether the current object is new to the current user. |
paul@739 | 1162 | |
paul@739 | 1163 | If 'strict' is specified and is a false value, the DTSTAMP test will be |
paul@739 | 1164 | ignored. This is useful in handling responses from attendees from |
paul@739 | 1165 | clients (like Claws Mail) that erase time information from DTSTAMP and |
paul@739 | 1166 | make it invalid. |
paul@606 | 1167 | """ |
paul@606 | 1168 | |
paul@739 | 1169 | obj = self.get_stored_object_version() |
paul@606 | 1170 | |
paul@606 | 1171 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@606 | 1172 | |
paul@606 | 1173 | if obj: |
paul@606 | 1174 | sequence = obj.get_value("SEQUENCE") |
paul@606 | 1175 | dtstamp = obj.get_value("DTSTAMP") |
paul@606 | 1176 | |
paul@606 | 1177 | # If the request refers to an older version of the object, ignore |
paul@606 | 1178 | # it. |
paul@606 | 1179 | |
paul@682 | 1180 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, not strict) |
paul@606 | 1181 | |
paul@606 | 1182 | return True |
paul@606 | 1183 | |
paul@672 | 1184 | def possibly_recurring_indefinitely(self): |
paul@672 | 1185 | |
paul@672 | 1186 | "Return whether the object recurs indefinitely." |
paul@672 | 1187 | |
paul@672 | 1188 | # Obtain the stored object to make sure that recurrence information |
paul@672 | 1189 | # is not being ignored. This might happen if a client sends a |
paul@672 | 1190 | # cancellation without the complete set of properties, for instance. |
paul@672 | 1191 | |
paul@672 | 1192 | return self.obj.possibly_recurring_indefinitely() or \ |
paul@672 | 1193 | self.get_stored_object_version() and \ |
paul@672 | 1194 | self.get_stored_object_version().possibly_recurring_indefinitely() |
paul@672 | 1195 | |
paul@655 | 1196 | # Constraint application on event periods. |
paul@655 | 1197 | |
paul@655 | 1198 | def check_object(self): |
paul@655 | 1199 | |
paul@655 | 1200 | "Check the object against any scheduling constraints." |
paul@655 | 1201 | |
paul@669 | 1202 | permitted_values = self.get_permitted_values() |
paul@669 | 1203 | if not permitted_values: |
paul@655 | 1204 | return None |
paul@655 | 1205 | |
paul@655 | 1206 | invalid = [] |
paul@655 | 1207 | |
paul@1276 | 1208 | for period in self.obj.get_periods(): |
paul@941 | 1209 | errors = period.check_permitted(permitted_values) |
paul@941 | 1210 | if errors: |
paul@941 | 1211 | start_errors, end_errors = errors |
paul@656 | 1212 | invalid.append((period.origin, start_errors, end_errors)) |
paul@655 | 1213 | |
paul@655 | 1214 | return invalid |
paul@655 | 1215 | |
paul@660 | 1216 | def correct_object(self): |
paul@655 | 1217 | |
paul@660 | 1218 | "Correct the object according to any scheduling constraints." |
paul@655 | 1219 | |
paul@669 | 1220 | permitted_values = self.get_permitted_values() |
paul@1276 | 1221 | return permitted_values and self.obj.correct_object(permitted_values) |
paul@655 | 1222 | |
paul@941 | 1223 | def correct_period(self, period): |
paul@941 | 1224 | |
paul@941 | 1225 | "Correct 'period' according to any scheduling constraints." |
paul@941 | 1226 | |
paul@941 | 1227 | permitted_values = self.get_permitted_values() |
paul@941 | 1228 | if not permitted_values: |
paul@941 | 1229 | return period |
paul@941 | 1230 | else: |
paul@941 | 1231 | return period.get_corrected(permitted_values) |
paul@941 | 1232 | |
paul@606 | 1233 | # Object retrieval. |
paul@606 | 1234 | |
paul@606 | 1235 | def get_stored_object_version(self): |
paul@606 | 1236 | |
paul@606 | 1237 | """ |
paul@606 | 1238 | Return the stored object to which the current object refers for the |
paul@606 | 1239 | current user. |
paul@606 | 1240 | """ |
paul@606 | 1241 | |
paul@606 | 1242 | return self.get_stored_object(self.uid, self.recurrenceid) |
paul@606 | 1243 | |
paul@704 | 1244 | def get_definitive_object(self, as_organiser): |
paul@606 | 1245 | |
paul@606 | 1246 | """ |
paul@606 | 1247 | Return an object considered definitive for the current transaction, |
paul@704 | 1248 | using 'as_organiser' to select the current transaction's object if |
paul@704 | 1249 | false, or selecting a stored object if true. |
paul@606 | 1250 | """ |
paul@606 | 1251 | |
paul@704 | 1252 | return not as_organiser and self.obj or self.get_stored_object_version() |
paul@606 | 1253 | |
paul@606 | 1254 | def get_parent_object(self): |
paul@606 | 1255 | |
paul@606 | 1256 | """ |
paul@606 | 1257 | Return the parent object to which the current object refers for the |
paul@606 | 1258 | current user. |
paul@606 | 1259 | """ |
paul@606 | 1260 | |
paul@606 | 1261 | return self.recurrenceid and self.get_stored_object(self.uid, None) or None |
paul@606 | 1262 | |
paul@606 | 1263 | # Convenience methods for modifying free/busy collections. |
paul@606 | 1264 | |
paul@606 | 1265 | def get_recurrence_start_point(self, recurrenceid): |
paul@606 | 1266 | |
paul@606 | 1267 | "Get 'recurrenceid' in a form suitable for matching free/busy entries." |
paul@606 | 1268 | |
paul@1276 | 1269 | return self.obj.get_recurrence_start_point(recurrenceid) |
paul@606 | 1270 | |
paul@1231 | 1271 | def remove_from_freebusy(self, freebusy, participant=None): |
paul@606 | 1272 | |
paul@1231 | 1273 | """ |
paul@1231 | 1274 | Remove this event from the given 'freebusy' collection. If 'participant' |
paul@1231 | 1275 | is specified, only remove this event if the participant is attending. |
paul@1231 | 1276 | """ |
paul@1231 | 1277 | |
paul@1231 | 1278 | removed = freebusy.remove_event_periods(self.uid, self.recurrenceid, participant) |
paul@1043 | 1279 | if not removed and self.recurrenceid: |
paul@1231 | 1280 | return freebusy.remove_affected_period(self.uid, self.get_recurrence_start_point(self.recurrenceid), participant) |
paul@1043 | 1281 | else: |
paul@1043 | 1282 | return removed |
paul@606 | 1283 | |
paul@606 | 1284 | def remove_freebusy_for_recurrences(self, freebusy, recurrenceids=None): |
paul@606 | 1285 | |
paul@606 | 1286 | """ |
paul@606 | 1287 | Remove from 'freebusy' any original recurrence from parent free/busy |
paul@606 | 1288 | details for the current object, if the current object is a specific |
paul@606 | 1289 | additional recurrence. Otherwise, remove all additional recurrence |
paul@606 | 1290 | information corresponding to 'recurrenceids', or if omitted, all |
paul@606 | 1291 | recurrences. |
paul@606 | 1292 | """ |
paul@606 | 1293 | |
paul@606 | 1294 | if self.recurrenceid: |
paul@606 | 1295 | recurrenceid = self.get_recurrence_start_point(self.recurrenceid) |
paul@1062 | 1296 | freebusy.remove_affected_period(self.uid, recurrenceid) |
paul@606 | 1297 | else: |
paul@606 | 1298 | # Remove obsolete recurrence periods. |
paul@606 | 1299 | |
paul@1062 | 1300 | freebusy.remove_additional_periods(self.uid, recurrenceids) |
paul@606 | 1301 | |
paul@606 | 1302 | # Remove original periods affected by additional recurrences. |
paul@606 | 1303 | |
paul@606 | 1304 | if recurrenceids: |
paul@606 | 1305 | for recurrenceid in recurrenceids: |
paul@606 | 1306 | recurrenceid = self.get_recurrence_start_point(recurrenceid) |
paul@1062 | 1307 | freebusy.remove_affected_period(self.uid, recurrenceid) |
paul@606 | 1308 | |
paul@740 | 1309 | def update_freebusy(self, freebusy, user, as_organiser, offer=False): |
paul@606 | 1310 | |
paul@606 | 1311 | """ |
paul@606 | 1312 | Update the 'freebusy' collection for this event with the periods and |
paul@606 | 1313 | transparency associated with the current object, subject to the 'user' |
paul@606 | 1314 | identity and the attendance details provided for them, indicating |
paul@704 | 1315 | whether the update is being done 'as_organiser' (for the organiser of |
paul@676 | 1316 | an event) or not. |
paul@740 | 1317 | |
paul@740 | 1318 | If 'offer' is set to a true value, any free/busy updates will be tagged |
paul@740 | 1319 | with an expiry time. |
paul@606 | 1320 | """ |
paul@606 | 1321 | |
paul@606 | 1322 | # Obtain the stored object if the current object is not issued by the |
paul@606 | 1323 | # organiser. Attendees do not have the opportunity to redefine the |
paul@606 | 1324 | # periods. |
paul@606 | 1325 | |
paul@704 | 1326 | obj = self.get_definitive_object(as_organiser) |
paul@606 | 1327 | if not obj: |
paul@606 | 1328 | return |
paul@606 | 1329 | |
paul@606 | 1330 | # Obtain the affected periods. |
paul@606 | 1331 | |
paul@1238 | 1332 | periods = self.get_periods(obj, future_only=True) |
paul@606 | 1333 | |
paul@606 | 1334 | # Define an overriding transparency, the indicated event transparency, |
paul@606 | 1335 | # or the default transparency for the free/busy entry. |
paul@606 | 1336 | |
paul@704 | 1337 | transp = self.get_overriding_transparency(user, as_organiser) or \ |
paul@606 | 1338 | obj.get_value("TRANSP") or \ |
paul@606 | 1339 | "OPAQUE" |
paul@606 | 1340 | |
paul@740 | 1341 | # Calculate any expiry time. If no offer period is defined, do not |
paul@740 | 1342 | # record the offer periods. |
paul@740 | 1343 | |
paul@740 | 1344 | if offer: |
paul@740 | 1345 | offer_period = self.get_offer_period() |
paul@740 | 1346 | if offer_period: |
paul@759 | 1347 | expires = get_timestamp(offer_period) |
paul@740 | 1348 | else: |
paul@740 | 1349 | return |
paul@740 | 1350 | else: |
paul@740 | 1351 | expires = None |
paul@740 | 1352 | |
paul@606 | 1353 | # Perform the low-level update. |
paul@606 | 1354 | |
paul@606 | 1355 | Client.update_freebusy(self, freebusy, periods, transp, |
paul@606 | 1356 | self.uid, self.recurrenceid, |
paul@606 | 1357 | obj.get_value("SUMMARY"), |
paul@915 | 1358 | get_uri(obj.get_value("ORGANIZER")), |
paul@740 | 1359 | expires) |
paul@606 | 1360 | |
paul@606 | 1361 | def update_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@740 | 1362 | updating_other=False, offer=False): |
paul@606 | 1363 | |
paul@606 | 1364 | """ |
paul@695 | 1365 | Update the 'freebusy' collection for the given 'user', indicating |
paul@695 | 1366 | whether the update is 'for_organiser' (being done for the organiser of |
paul@695 | 1367 | an event) or not, and whether it is 'updating_other' (meaning another |
paul@695 | 1368 | user's details). |
paul@740 | 1369 | |
paul@740 | 1370 | If 'offer' is set to a true value, any free/busy updates will be tagged |
paul@740 | 1371 | with an expiry time. |
paul@606 | 1372 | """ |
paul@606 | 1373 | |
paul@606 | 1374 | # Record in the free/busy details unless a non-participating attendee. |
paul@697 | 1375 | # Remove periods for non-participating attendees. |
paul@606 | 1376 | |
paul@744 | 1377 | if offer or self.is_participating(user, for_organiser and not updating_other): |
paul@704 | 1378 | self.update_freebusy(freebusy, user, |
paul@704 | 1379 | for_organiser and not updating_other or |
paul@740 | 1380 | not for_organiser and updating_other, |
paul@740 | 1381 | offer |
paul@704 | 1382 | ) |
paul@606 | 1383 | else: |
paul@606 | 1384 | self.remove_from_freebusy(freebusy) |
paul@606 | 1385 | |
paul@697 | 1386 | def remove_freebusy_for_participant(self, freebusy, user, for_organiser=False, |
paul@697 | 1387 | updating_other=False): |
paul@697 | 1388 | |
paul@697 | 1389 | """ |
paul@697 | 1390 | Remove details from the 'freebusy' collection for the given 'user', |
paul@697 | 1391 | indicating whether the modification is 'for_organiser' (being done for |
paul@697 | 1392 | the organiser of an event) or not, and whether it is 'updating_other' |
paul@697 | 1393 | (meaning another user's details). |
paul@697 | 1394 | """ |
paul@697 | 1395 | |
paul@697 | 1396 | # Remove from the free/busy details if a specified attendee. |
paul@697 | 1397 | |
paul@697 | 1398 | if self.is_participating(user, for_organiser and not updating_other): |
paul@697 | 1399 | self.remove_from_freebusy(freebusy) |
paul@697 | 1400 | |
paul@606 | 1401 | # Convenience methods for updating stored free/busy information received |
paul@606 | 1402 | # from other users. |
paul@606 | 1403 | |
paul@697 | 1404 | def update_freebusy_from_participant(self, user, for_organiser, fn=None): |
paul@606 | 1405 | |
paul@606 | 1406 | """ |
paul@606 | 1407 | For the current user, record the free/busy information for another |
paul@606 | 1408 | 'user', indicating whether the update is 'for_organiser' or not, thus |
paul@606 | 1409 | maintaining a separate record of their free/busy details. |
paul@606 | 1410 | """ |
paul@606 | 1411 | |
paul@697 | 1412 | fn = fn or self.update_freebusy_for_participant |
paul@697 | 1413 | |
paul@606 | 1414 | # A user does not store free/busy information for themself as another |
paul@606 | 1415 | # party. |
paul@606 | 1416 | |
paul@606 | 1417 | if user == self.user: |
paul@606 | 1418 | return |
paul@606 | 1419 | |
paul@730 | 1420 | self.acquire_lock() |
paul@702 | 1421 | try: |
paul@1071 | 1422 | freebusy = self.store.get_freebusy_for_other_for_update(self.user, user) |
paul@702 | 1423 | fn(freebusy, user, for_organiser, True) |
paul@702 | 1424 | |
paul@702 | 1425 | # Tidy up any obsolete recurrences. |
paul@606 | 1426 | |
paul@1268 | 1427 | self.remove_freebusy_for_recurrences(freebusy, self.get_recurrences()) |
paul@730 | 1428 | self.store.set_freebusy_for_other(self.user, freebusy, user) |
paul@606 | 1429 | |
paul@702 | 1430 | finally: |
paul@730 | 1431 | self.release_lock() |
paul@606 | 1432 | |
paul@606 | 1433 | def update_freebusy_from_organiser(self, organiser): |
paul@606 | 1434 | |
paul@606 | 1435 | "For the current user, record free/busy information from 'organiser'." |
paul@606 | 1436 | |
paul@606 | 1437 | self.update_freebusy_from_participant(organiser, True) |
paul@606 | 1438 | |
paul@606 | 1439 | def update_freebusy_from_attendees(self, attendees): |
paul@606 | 1440 | |
paul@606 | 1441 | "For the current user, record free/busy information from 'attendees'." |
paul@606 | 1442 | |
paul@836 | 1443 | obj = self.get_stored_object_version() |
paul@836 | 1444 | |
paul@836 | 1445 | if not obj or not self.have_new_object(): |
paul@1175 | 1446 | return False |
paul@836 | 1447 | |
paul@836 | 1448 | # Filter out unrecognised attendees. |
paul@836 | 1449 | |
paul@836 | 1450 | attendees = set(attendees).intersection(uri_values(obj.get_values("ATTENDEE"))) |
paul@836 | 1451 | |
paul@836 | 1452 | for attendee in attendees: |
paul@606 | 1453 | self.update_freebusy_from_participant(attendee, False) |
paul@606 | 1454 | |
paul@1175 | 1455 | return True |
paul@1175 | 1456 | |
paul@697 | 1457 | def remove_freebusy_from_organiser(self, organiser): |
paul@697 | 1458 | |
paul@697 | 1459 | "For the current user, remove free/busy information from 'organiser'." |
paul@697 | 1460 | |
paul@697 | 1461 | self.update_freebusy_from_participant(organiser, True, self.remove_freebusy_for_participant) |
paul@697 | 1462 | |
paul@697 | 1463 | def remove_freebusy_from_attendees(self, attendees): |
paul@697 | 1464 | |
paul@697 | 1465 | "For the current user, remove free/busy information from 'attendees'." |
paul@697 | 1466 | |
paul@697 | 1467 | for attendee in attendees.keys(): |
paul@697 | 1468 | self.update_freebusy_from_participant(attendee, False, self.remove_freebusy_for_participant) |
paul@697 | 1469 | |
paul@756 | 1470 | # Convenience methods for updating free/busy details at the event level. |
paul@756 | 1471 | |
paul@756 | 1472 | def update_event_in_freebusy(self, for_organiser=True): |
paul@756 | 1473 | |
paul@756 | 1474 | """ |
paul@756 | 1475 | Update free/busy information when handling an object, doing so for the |
paul@756 | 1476 | organiser of an event if 'for_organiser' is set to a true value. |
paul@756 | 1477 | """ |
paul@756 | 1478 | |
paul@1071 | 1479 | freebusy = self.store.get_freebusy_for_update(self.user) |
paul@756 | 1480 | |
paul@756 | 1481 | # Obtain the attendance attributes for this user, if available. |
paul@756 | 1482 | |
paul@756 | 1483 | self.update_freebusy_for_participant(freebusy, self.user, for_organiser) |
paul@756 | 1484 | |
paul@756 | 1485 | # Remove original recurrence details replaced by additional |
paul@756 | 1486 | # recurrences, as well as obsolete additional recurrences. |
paul@756 | 1487 | |
paul@1268 | 1488 | self.remove_freebusy_for_recurrences(freebusy, self.get_recurrences()) |
paul@756 | 1489 | self.store.set_freebusy(self.user, freebusy) |
paul@756 | 1490 | |
paul@756 | 1491 | if self.publisher and self.is_sharing() and self.is_publishing(): |
paul@756 | 1492 | self.publisher.set_freebusy(self.user, freebusy) |
paul@756 | 1493 | |
paul@756 | 1494 | # Update free/busy provider information if the event may recur |
paul@756 | 1495 | # indefinitely. |
paul@756 | 1496 | |
paul@756 | 1497 | if self.possibly_recurring_indefinitely(): |
paul@756 | 1498 | self.store.append_freebusy_provider(self.user, self.obj) |
paul@756 | 1499 | |
paul@756 | 1500 | return True |
paul@756 | 1501 | |
paul@756 | 1502 | def remove_event_from_freebusy(self): |
paul@756 | 1503 | |
paul@756 | 1504 | "Remove free/busy information when handling an object." |
paul@756 | 1505 | |
paul@1071 | 1506 | freebusy = self.store.get_freebusy_for_update(self.user) |
paul@756 | 1507 | |
paul@756 | 1508 | self.remove_from_freebusy(freebusy) |
paul@756 | 1509 | self.remove_freebusy_for_recurrences(freebusy) |
paul@756 | 1510 | self.store.set_freebusy(self.user, freebusy) |
paul@756 | 1511 | |
paul@756 | 1512 | if self.publisher and self.is_sharing() and self.is_publishing(): |
paul@756 | 1513 | self.publisher.set_freebusy(self.user, freebusy) |
paul@756 | 1514 | |
paul@756 | 1515 | # Update free/busy provider information if the event may recur |
paul@756 | 1516 | # indefinitely. |
paul@756 | 1517 | |
paul@756 | 1518 | if self.possibly_recurring_indefinitely(): |
paul@756 | 1519 | self.store.remove_freebusy_provider(self.user, self.obj) |
paul@756 | 1520 | |
paul@756 | 1521 | def update_event_in_freebusy_offers(self): |
paul@756 | 1522 | |
paul@756 | 1523 | "Update free/busy offers when handling an object." |
paul@756 | 1524 | |
paul@1071 | 1525 | freebusy = self.store.get_freebusy_offers_for_update(self.user) |
paul@756 | 1526 | |
paul@756 | 1527 | # Obtain the attendance attributes for this user, if available. |
paul@756 | 1528 | |
paul@756 | 1529 | self.update_freebusy_for_participant(freebusy, self.user, offer=True) |
paul@756 | 1530 | |
paul@756 | 1531 | # Remove original recurrence details replaced by additional |
paul@756 | 1532 | # recurrences, as well as obsolete additional recurrences. |
paul@756 | 1533 | |
paul@1268 | 1534 | self.remove_freebusy_for_recurrences(freebusy, self.get_recurrences()) |
paul@756 | 1535 | self.store.set_freebusy_offers(self.user, freebusy) |
paul@756 | 1536 | |
paul@756 | 1537 | return True |
paul@756 | 1538 | |
paul@756 | 1539 | def remove_event_from_freebusy_offers(self): |
paul@756 | 1540 | |
paul@756 | 1541 | "Remove free/busy offers when handling an object." |
paul@756 | 1542 | |
paul@1071 | 1543 | freebusy = self.store.get_freebusy_offers_for_update(self.user) |
paul@756 | 1544 | |
paul@756 | 1545 | self.remove_from_freebusy(freebusy) |
paul@756 | 1546 | self.remove_freebusy_for_recurrences(freebusy) |
paul@756 | 1547 | self.store.set_freebusy_offers(self.user, freebusy) |
paul@756 | 1548 | |
paul@756 | 1549 | return True |
paul@756 | 1550 | |
paul@809 | 1551 | # Convenience methods for removing counter-proposals and updating the |
paul@809 | 1552 | # request queue. |
paul@809 | 1553 | |
paul@813 | 1554 | def remove_request(self): |
paul@813 | 1555 | return self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@813 | 1556 | |
paul@813 | 1557 | def remove_event(self): |
paul@813 | 1558 | return self.store.remove_event(self.user, self.uid, self.recurrenceid) |
paul@813 | 1559 | |
paul@809 | 1560 | def remove_counter(self, attendee): |
paul@809 | 1561 | self.remove_counters([attendee]) |
paul@809 | 1562 | |
paul@809 | 1563 | def remove_counters(self, attendees): |
paul@809 | 1564 | for attendee in attendees: |
paul@809 | 1565 | self.store.remove_counter(self.user, attendee, self.uid, self.recurrenceid) |
paul@809 | 1566 | |
paul@809 | 1567 | if not self.store.get_counters(self.user, self.uid, self.recurrenceid): |
paul@809 | 1568 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@809 | 1569 | |
paul@441 | 1570 | # vim: tabstop=4 expandtab shiftwidth=4 |