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