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