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