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