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