paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@48 | 3 | """ |
paul@48 | 4 | Interpretation and preparation of iMIP content, together with a content handling |
paul@48 | 5 | mechanism employed by specific recipients. |
paul@146 | 6 | |
paul@146 | 7 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 8 | |
paul@146 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 12 | version. |
paul@146 | 13 | |
paul@146 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 17 | details. |
paul@146 | 18 | |
paul@146 | 19 | You should have received a copy of the GNU General Public License along with |
paul@146 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@48 | 21 | """ |
paul@48 | 22 | |
paul@152 | 23 | from datetime import datetime, timedelta |
paul@48 | 24 | from email.mime.text import MIMEText |
paul@182 | 25 | from imiptools.config import MANAGER_PATH, MANAGER_URL |
paul@213 | 26 | from imiptools.data import Object, parse_object, \ |
paul@360 | 27 | get_address, get_uri, get_value, get_window_end, \ |
paul@371 | 28 | is_new_object, uri_dict, uri_item, uri_values |
paul@361 | 29 | from imiptools.dates import format_datetime, get_default_timezone, to_timezone |
paul@250 | 30 | from imiptools.period import can_schedule, insert_period, remove_period, \ |
paul@361 | 31 | remove_affected_period, update_freebusy |
paul@361 | 32 | from imiptools.profile import Preferences |
paul@182 | 33 | from socket import gethostname |
paul@48 | 34 | import imip_store |
paul@48 | 35 | |
paul@48 | 36 | try: |
paul@48 | 37 | from cStringIO import StringIO |
paul@48 | 38 | except ImportError: |
paul@48 | 39 | from StringIO import StringIO |
paul@48 | 40 | |
paul@48 | 41 | # Handler mechanism objects. |
paul@48 | 42 | |
paul@224 | 43 | def handle_itip_part(part, handlers): |
paul@48 | 44 | |
paul@48 | 45 | """ |
paul@227 | 46 | Handle the given iTIP 'part' using the given 'handlers' dictionary. |
paul@224 | 47 | |
paul@224 | 48 | Return a list of responses, each response being a tuple of the form |
paul@224 | 49 | (outgoing-recipients, message-part). |
paul@48 | 50 | """ |
paul@48 | 51 | |
paul@48 | 52 | method = part.get_param("method") |
paul@48 | 53 | |
paul@48 | 54 | # Decode the data and parse it. |
paul@48 | 55 | |
paul@48 | 56 | f = StringIO(part.get_payload(decode=True)) |
paul@48 | 57 | |
paul@48 | 58 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@48 | 59 | |
paul@48 | 60 | # Ignore the part if not a calendar object. |
paul@48 | 61 | |
paul@48 | 62 | if not itip: |
paul@228 | 63 | return |
paul@48 | 64 | |
paul@48 | 65 | # Require consistency between declared and employed methods. |
paul@48 | 66 | |
paul@48 | 67 | if get_value(itip, "METHOD") == method: |
paul@48 | 68 | |
paul@48 | 69 | # Look for different kinds of sections. |
paul@48 | 70 | |
paul@60 | 71 | all_results = [] |
paul@48 | 72 | |
paul@226 | 73 | for name, items in itip.items(): |
paul@226 | 74 | |
paul@226 | 75 | # Get a handler for the given section. |
paul@226 | 76 | |
paul@226 | 77 | handler = handlers.get(name) |
paul@226 | 78 | if not handler: |
paul@226 | 79 | continue |
paul@226 | 80 | |
paul@226 | 81 | for item in items: |
paul@48 | 82 | |
paul@48 | 83 | # Dispatch to a handler and obtain any response. |
paul@48 | 84 | |
paul@226 | 85 | handler.set_object(Object({name : item})) |
paul@228 | 86 | methods[method](handler)() |
paul@48 | 87 | |
paul@182 | 88 | # References to the Web interface. |
paul@182 | 89 | |
paul@182 | 90 | def get_manager_url(): |
paul@182 | 91 | url_base = MANAGER_URL or "http://%s/" % gethostname() |
paul@182 | 92 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@182 | 93 | |
paul@182 | 94 | def get_object_url(uid): |
paul@182 | 95 | return "%s/%s" % (get_manager_url().rstrip("/"), uid) |
paul@182 | 96 | |
paul@48 | 97 | class Handler: |
paul@48 | 98 | |
paul@48 | 99 | "General handler support." |
paul@48 | 100 | |
paul@224 | 101 | def __init__(self, senders=None, recipient=None, messenger=None): |
paul@48 | 102 | |
paul@48 | 103 | """ |
paul@213 | 104 | Initialise the handler with the calendar 'obj' and the 'senders' and |
paul@213 | 105 | 'recipient' of the object (if specifically indicated). |
paul@48 | 106 | """ |
paul@48 | 107 | |
paul@129 | 108 | self.senders = senders and set(map(get_address, senders)) |
paul@179 | 109 | self.recipient = recipient and get_address(recipient) |
paul@89 | 110 | self.messenger = messenger |
paul@48 | 111 | |
paul@228 | 112 | self.results = [] |
paul@228 | 113 | self.outgoing_methods = set() |
paul@228 | 114 | |
paul@226 | 115 | self.obj = None |
paul@226 | 116 | self.uid = None |
paul@343 | 117 | self.recurrenceid = None |
paul@226 | 118 | self.sequence = None |
paul@226 | 119 | self.dtstamp = None |
paul@48 | 120 | |
paul@48 | 121 | self.store = imip_store.FileStore() |
paul@48 | 122 | |
paul@48 | 123 | try: |
paul@48 | 124 | self.publisher = imip_store.FilePublisher() |
paul@48 | 125 | except OSError: |
paul@48 | 126 | self.publisher = None |
paul@48 | 127 | |
paul@224 | 128 | def set_object(self, obj): |
paul@224 | 129 | self.obj = obj |
paul@226 | 130 | self.uid = self.obj.get_value("UID") |
paul@346 | 131 | self.recurrenceid = format_datetime(self.obj.get_utc_datetime("RECURRENCE-ID")) |
paul@226 | 132 | self.sequence = self.obj.get_value("SEQUENCE") |
paul@226 | 133 | self.dtstamp = self.obj.get_value("DTSTAMP") |
paul@224 | 134 | |
paul@182 | 135 | def wrap(self, text, link=True): |
paul@182 | 136 | |
paul@182 | 137 | "Wrap any valid message for passing to the recipient." |
paul@182 | 138 | |
paul@182 | 139 | texts = [] |
paul@182 | 140 | texts.append(text) |
paul@182 | 141 | if link: |
paul@182 | 142 | texts.append("If your mail program cannot handle this " |
paul@182 | 143 | "message, you may view the details here:\n\n%s" % |
paul@182 | 144 | get_object_url(self.uid)) |
paul@182 | 145 | |
paul@228 | 146 | return self.add_result(None, None, MIMEText("\n".join(texts))) |
paul@228 | 147 | |
paul@228 | 148 | # Result registration. |
paul@228 | 149 | |
paul@228 | 150 | def add_result(self, method, outgoing_recipients, part): |
paul@228 | 151 | |
paul@228 | 152 | """ |
paul@228 | 153 | Record a result having the given 'method', 'outgoing_recipients' and |
paul@228 | 154 | message part. |
paul@228 | 155 | """ |
paul@228 | 156 | |
paul@228 | 157 | if outgoing_recipients: |
paul@228 | 158 | self.outgoing_methods.add(method) |
paul@228 | 159 | self.results.append((outgoing_recipients, part)) |
paul@228 | 160 | |
paul@228 | 161 | def get_results(self): |
paul@228 | 162 | return self.results |
paul@228 | 163 | |
paul@228 | 164 | def get_outgoing_methods(self): |
paul@228 | 165 | return self.outgoing_methods |
paul@182 | 166 | |
paul@361 | 167 | # Convenience methods for modifying free/busy collections. |
paul@361 | 168 | |
paul@361 | 169 | def remove_from_freebusy(self, freebusy): |
paul@70 | 170 | |
paul@361 | 171 | "Remove this event from the given 'freebusy' collection." |
paul@361 | 172 | |
paul@361 | 173 | remove_period(freebusy, self.uid, self.recurrenceid) |
paul@361 | 174 | |
paul@371 | 175 | def remove_freebusy_for_original_recurrence(self, freebusy): |
paul@371 | 176 | |
paul@371 | 177 | """ |
paul@371 | 178 | Remove from 'freebusy' any specific recurrence from parent free/busy |
paul@371 | 179 | details for the current object. |
paul@371 | 180 | """ |
paul@371 | 181 | |
paul@371 | 182 | if self.recurrenceid: |
paul@371 | 183 | remove_affected_period(freebusy, self.uid, self.recurrenceid) |
paul@371 | 184 | |
paul@371 | 185 | def _update_freebusy(self, freebusy, periods, recurrenceid, transp=None): |
paul@122 | 186 | |
paul@361 | 187 | """ |
paul@361 | 188 | Update the 'freebusy' collection with the given 'periods', indicating an |
paul@361 | 189 | explicit 'recurrenceid' to affect either a recurrence or the parent |
paul@361 | 190 | event. |
paul@361 | 191 | """ |
paul@361 | 192 | |
paul@371 | 193 | update_freebusy(freebusy, periods, transp or self.obj.get_value("TRANSP"), |
paul@361 | 194 | self.uid, recurrenceid) |
paul@168 | 195 | |
paul@371 | 196 | def update_freebusy(self, freebusy, periods, transp=None): |
paul@360 | 197 | |
paul@361 | 198 | """ |
paul@361 | 199 | Update the 'freebusy' collection for this event with the given |
paul@361 | 200 | 'periods'. |
paul@361 | 201 | """ |
paul@361 | 202 | |
paul@371 | 203 | self._update_freebusy(freebusy, periods, self.recurrenceid, transp) |
paul@371 | 204 | |
paul@371 | 205 | def update_freebusy_for_participant(self, freebusy, periods, attr, for_organiser=False): |
paul@371 | 206 | |
paul@371 | 207 | """ |
paul@371 | 208 | Update the 'freebusy' collection using the given 'periods', subject to |
paul@371 | 209 | the 'attr' provided for the participant, indicating whether this is |
paul@371 | 210 | being generated 'for_organiser' or not. |
paul@371 | 211 | """ |
paul@371 | 212 | |
paul@371 | 213 | # Organisers employ a special transparency. |
paul@371 | 214 | |
paul@371 | 215 | if for_organiser or attr.get("PARTSTAT") != "DECLINED": |
paul@371 | 216 | self.update_freebusy(freebusy, periods, transp=(for_organiser and "ORG" or None)) |
paul@371 | 217 | else: |
paul@371 | 218 | self.remove_from_freebusy(freebusy) |
paul@361 | 219 | |
paul@361 | 220 | # Convenience methods for updating stored free/busy information. |
paul@77 | 221 | |
paul@371 | 222 | def update_freebusy_from_participant(self, user, participant_item, for_organiser): |
paul@304 | 223 | |
paul@304 | 224 | """ |
paul@304 | 225 | For the given 'user', record the free/busy information for the |
paul@371 | 226 | 'participant_item' (a value plus attributes) representing a different |
paul@371 | 227 | identity, thus maintaining a separate record of their free/busy details. |
paul@304 | 228 | """ |
paul@304 | 229 | |
paul@304 | 230 | participant, participant_attr = participant_item |
paul@304 | 231 | |
paul@361 | 232 | if participant == user: |
paul@361 | 233 | return |
paul@304 | 234 | |
paul@361 | 235 | freebusy = self.store.get_freebusy_for_other(user, participant) |
paul@361 | 236 | tzid = self.get_tzid(user) |
paul@361 | 237 | window_end = get_window_end(tzid) |
paul@371 | 238 | periods = self.obj.get_periods_for_freebusy(tzid, window_end) |
paul@360 | 239 | |
paul@371 | 240 | # Record in the free/busy details unless a non-participating attendee. |
paul@361 | 241 | |
paul@371 | 242 | self.update_freebusy_for_participant(freebusy, periods, participant_attr, |
paul@371 | 243 | for_organiser and self.is_not_attendee(participant, self.obj)) |
paul@361 | 244 | |
paul@380 | 245 | # Subtract any recurrences from the free/busy details of a parent |
paul@380 | 246 | # object. |
paul@380 | 247 | |
paul@380 | 248 | for recurrenceid in self.store.get_recurrences(user, self.uid): |
paul@380 | 249 | remove_affected_period(freebusy, self.uid, recurrenceid) |
paul@380 | 250 | |
paul@361 | 251 | self.store.set_freebusy_for_other(user, freebusy, participant) |
paul@304 | 252 | |
paul@306 | 253 | def update_freebusy_from_organiser(self, attendee, organiser_item): |
paul@268 | 254 | |
paul@268 | 255 | """ |
paul@268 | 256 | For the 'attendee', record free/busy information from the |
paul@268 | 257 | 'organiser_item' (a value plus attributes). |
paul@268 | 258 | """ |
paul@268 | 259 | |
paul@371 | 260 | self.update_freebusy_from_participant(attendee, organiser_item, True) |
paul@268 | 261 | |
paul@306 | 262 | def update_freebusy_from_attendees(self, organiser, attendees): |
paul@268 | 263 | |
paul@268 | 264 | "For the 'organiser', record free/busy information from 'attendees'." |
paul@268 | 265 | |
paul@304 | 266 | for attendee_item in attendees.items(): |
paul@371 | 267 | self.update_freebusy_from_participant(organiser, attendee_item, False) |
paul@168 | 268 | |
paul@361 | 269 | # Logic, filtering and access to calendar structures and other data. |
paul@361 | 270 | |
paul@371 | 271 | def is_not_attendee(self, identity, obj): |
paul@371 | 272 | |
paul@371 | 273 | "Return whether 'identity' is not an attendee in 'obj'." |
paul@371 | 274 | |
paul@371 | 275 | return identity not in uri_values(obj.get_values("ATTENDEE")) |
paul@371 | 276 | |
paul@77 | 277 | def can_schedule(self, freebusy, periods): |
paul@343 | 278 | return can_schedule(freebusy, periods, self.uid, self.recurrenceid) |
paul@76 | 279 | |
paul@168 | 280 | def filter_by_senders(self, mapping): |
paul@168 | 281 | |
paul@168 | 282 | """ |
paul@168 | 283 | Return a list of items from 'mapping' filtered using sender information. |
paul@168 | 284 | """ |
paul@168 | 285 | |
paul@89 | 286 | if self.senders: |
paul@168 | 287 | |
paul@168 | 288 | # Get a mapping from senders to identities. |
paul@168 | 289 | |
paul@168 | 290 | identities = self.get_sender_identities(mapping) |
paul@168 | 291 | |
paul@168 | 292 | # Find the senders that are valid. |
paul@168 | 293 | |
paul@168 | 294 | senders = map(get_address, identities) |
paul@168 | 295 | valid = self.senders.intersection(senders) |
paul@89 | 296 | |
paul@168 | 297 | # Return the true identities. |
paul@168 | 298 | |
paul@168 | 299 | return [identities[get_uri(address)] for address in valid] |
paul@168 | 300 | else: |
paul@168 | 301 | return mapping |
paul@168 | 302 | |
paul@179 | 303 | def filter_by_recipient(self, mapping): |
paul@168 | 304 | |
paul@168 | 305 | """ |
paul@168 | 306 | Return a list of items from 'mapping' filtered using recipient |
paul@168 | 307 | information. |
paul@168 | 308 | """ |
paul@168 | 309 | |
paul@179 | 310 | if self.recipient: |
paul@179 | 311 | addresses = set(map(get_address, mapping)) |
paul@179 | 312 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@89 | 313 | else: |
paul@168 | 314 | return mapping |
paul@48 | 315 | |
paul@292 | 316 | def require_organiser(self, from_organiser=True): |
paul@51 | 317 | |
paul@51 | 318 | """ |
paul@292 | 319 | Return the organiser for the current object, filtered for the sender or |
paul@292 | 320 | recipient of interest. Return None if no identities are eligible. |
paul@292 | 321 | |
paul@292 | 322 | The organiser identity is normalized. |
paul@292 | 323 | """ |
paul@292 | 324 | |
paul@292 | 325 | organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@292 | 326 | |
paul@292 | 327 | # Only provide details for an organiser who sent/receives the message. |
paul@292 | 328 | |
paul@292 | 329 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@129 | 330 | |
paul@292 | 331 | if not organiser_filter_fn(dict([organiser_item])): |
paul@292 | 332 | return None |
paul@292 | 333 | |
paul@292 | 334 | return organiser_item |
paul@292 | 335 | |
paul@292 | 336 | def require_attendees(self, from_organiser=True): |
paul@292 | 337 | |
paul@292 | 338 | """ |
paul@292 | 339 | Return the attendees for the current object, filtered for the sender or |
paul@292 | 340 | recipient of interest. Return None if no identities are eligible. |
paul@292 | 341 | |
paul@292 | 342 | The attendee identities are normalized. |
paul@51 | 343 | """ |
paul@51 | 344 | |
paul@213 | 345 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@48 | 346 | |
paul@168 | 347 | # Only provide details for attendees who sent/receive the message. |
paul@48 | 348 | |
paul@179 | 349 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@100 | 350 | |
paul@48 | 351 | attendees = {} |
paul@168 | 352 | for attendee in attendee_filter_fn(attendee_map): |
paul@48 | 353 | attendees[attendee] = attendee_map[attendee] |
paul@48 | 354 | |
paul@292 | 355 | return attendees |
paul@292 | 356 | |
paul@292 | 357 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@292 | 358 | |
paul@292 | 359 | """ |
paul@292 | 360 | Return the organiser and attendees for the current object, filtered for |
paul@292 | 361 | the recipient of interest. Return None if no identities are eligible. |
paul@168 | 362 | |
paul@292 | 363 | Organiser and attendee identities are normalized. |
paul@292 | 364 | """ |
paul@168 | 365 | |
paul@292 | 366 | organiser_item = self.require_organiser(from_organiser) |
paul@292 | 367 | attendees = self.require_attendees(from_organiser) |
paul@168 | 368 | |
paul@292 | 369 | if not attendees or not organiser_item: |
paul@89 | 370 | return None |
paul@89 | 371 | |
paul@168 | 372 | return organiser_item, attendees |
paul@94 | 373 | |
paul@168 | 374 | def get_sender_identities(self, mapping): |
paul@94 | 375 | |
paul@94 | 376 | """ |
paul@168 | 377 | Return a mapping from actual senders to the identities for which they |
paul@168 | 378 | have provided data, extracting this information from the given |
paul@168 | 379 | 'mapping'. |
paul@94 | 380 | """ |
paul@94 | 381 | |
paul@168 | 382 | senders = {} |
paul@89 | 383 | |
paul@168 | 384 | for value, attr in mapping.items(): |
paul@94 | 385 | sent_by = attr.get("SENT-BY") |
paul@94 | 386 | if sent_by: |
paul@168 | 387 | senders[get_uri(sent_by)] = value |
paul@168 | 388 | else: |
paul@168 | 389 | senders[value] = value |
paul@48 | 390 | |
paul@168 | 391 | return senders |
paul@48 | 392 | |
paul@360 | 393 | def _get_object(self, user, uid, recurrenceid): |
paul@360 | 394 | |
paul@360 | 395 | """ |
paul@360 | 396 | Return the stored object for the given 'user', 'uid' and 'recurrenceid'. |
paul@360 | 397 | """ |
paul@360 | 398 | |
paul@360 | 399 | fragment = self.store.get_event(user, uid, recurrenceid) |
paul@360 | 400 | return fragment and Object(fragment) |
paul@360 | 401 | |
paul@213 | 402 | def get_object(self, user): |
paul@100 | 403 | |
paul@100 | 404 | """ |
paul@100 | 405 | Return the stored object to which the current object refers for the |
paul@360 | 406 | given 'user'. |
paul@100 | 407 | """ |
paul@100 | 408 | |
paul@360 | 409 | return self._get_object(user, self.uid, self.recurrenceid) |
paul@360 | 410 | |
paul@360 | 411 | def get_parent_object(self, user): |
paul@360 | 412 | |
paul@360 | 413 | """ |
paul@360 | 414 | Return the parent object to which the current object refers for the |
paul@360 | 415 | given 'user'. |
paul@360 | 416 | """ |
paul@360 | 417 | |
paul@361 | 418 | return self.recurrenceid and self._get_object(user, self.uid, None) or None |
paul@100 | 419 | |
paul@213 | 420 | def have_new_object(self, attendee, obj=None): |
paul@51 | 421 | |
paul@51 | 422 | """ |
paul@213 | 423 | Return whether the current object is new to the 'attendee' (or if the |
paul@213 | 424 | given 'obj' is new). |
paul@51 | 425 | """ |
paul@51 | 426 | |
paul@213 | 427 | obj = obj or self.get_object(attendee) |
paul@51 | 428 | |
paul@51 | 429 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@51 | 430 | |
paul@100 | 431 | if obj: |
paul@213 | 432 | sequence = obj.get_value("SEQUENCE") |
paul@213 | 433 | dtstamp = obj.get_value("DTSTAMP") |
paul@51 | 434 | |
paul@100 | 435 | # If the request refers to an older version of the object, ignore |
paul@51 | 436 | # it. |
paul@51 | 437 | |
paul@172 | 438 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@172 | 439 | self.is_partstat_updated(obj)) |
paul@51 | 440 | |
paul@51 | 441 | return True |
paul@51 | 442 | |
paul@172 | 443 | def is_partstat_updated(self, obj): |
paul@174 | 444 | |
paul@174 | 445 | """ |
paul@174 | 446 | Return whether the participant status has been updated in the current |
paul@174 | 447 | object in comparison to the given 'obj'. |
paul@174 | 448 | |
paul@174 | 449 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@174 | 450 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@174 | 451 | NOTE: incorporated into any new object assessment. |
paul@174 | 452 | """ |
paul@174 | 453 | |
paul@309 | 454 | old_attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@309 | 455 | new_attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@172 | 456 | |
paul@172 | 457 | for attendee, attr in old_attendees.items(): |
paul@172 | 458 | old_partstat = attr.get("PARTSTAT") |
paul@172 | 459 | new_attr = new_attendees.get(attendee) |
paul@172 | 460 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@172 | 461 | |
paul@172 | 462 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@172 | 463 | new_partstat != old_partstat: |
paul@172 | 464 | |
paul@172 | 465 | return True |
paul@172 | 466 | |
paul@172 | 467 | return False |
paul@172 | 468 | |
paul@268 | 469 | def merge_attendance(self, attendees, identity): |
paul@268 | 470 | |
paul@268 | 471 | """ |
paul@268 | 472 | Merge attendance from the current object's 'attendees' into the version |
paul@268 | 473 | stored for the given 'identity'. |
paul@268 | 474 | """ |
paul@268 | 475 | |
paul@268 | 476 | obj = self.get_object(identity) |
paul@268 | 477 | |
paul@268 | 478 | if not obj or not self.have_new_object(identity, obj=obj): |
paul@268 | 479 | return False |
paul@268 | 480 | |
paul@268 | 481 | # Get attendee details in a usable form. |
paul@268 | 482 | |
paul@268 | 483 | attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@268 | 484 | |
paul@268 | 485 | for attendee, attendee_attr in attendees.items(): |
paul@268 | 486 | |
paul@268 | 487 | # Update attendance in the loaded object. |
paul@268 | 488 | |
paul@268 | 489 | attendee_map[attendee] = attendee_attr |
paul@268 | 490 | |
paul@268 | 491 | # Set the new details and store the object. |
paul@268 | 492 | |
paul@268 | 493 | obj["ATTENDEE"] = attendee_map.items() |
paul@268 | 494 | |
paul@334 | 495 | # Set the complete event if not an additional occurrence. |
paul@334 | 496 | |
paul@334 | 497 | event = obj.to_node() |
paul@346 | 498 | recurrenceid = format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) |
paul@334 | 499 | |
paul@343 | 500 | self.store.set_event(identity, self.uid, self.recurrenceid, event) |
paul@268 | 501 | |
paul@268 | 502 | return True |
paul@268 | 503 | |
paul@158 | 504 | def update_dtstamp(self): |
paul@158 | 505 | |
paul@158 | 506 | "Update the DTSTAMP in the current object." |
paul@158 | 507 | |
paul@213 | 508 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@158 | 509 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@213 | 510 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@158 | 511 | |
paul@273 | 512 | def set_sequence(self, increment=False): |
paul@273 | 513 | |
paul@273 | 514 | "Update the SEQUENCE in the current object." |
paul@273 | 515 | |
paul@273 | 516 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@273 | 517 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@273 | 518 | |
paul@361 | 519 | def get_tzid(self, identity): |
paul@361 | 520 | |
paul@361 | 521 | "Return the time regime applicable for the given 'identity'." |
paul@361 | 522 | |
paul@361 | 523 | preferences = Preferences(identity) |
paul@361 | 524 | return preferences.get("TZID") or get_default_timezone() |
paul@361 | 525 | |
paul@48 | 526 | # Handler registry. |
paul@48 | 527 | |
paul@48 | 528 | methods = { |
paul@48 | 529 | "ADD" : lambda handler: handler.add, |
paul@48 | 530 | "CANCEL" : lambda handler: handler.cancel, |
paul@48 | 531 | "COUNTER" : lambda handler: handler.counter, |
paul@48 | 532 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@48 | 533 | "PUBLISH" : lambda handler: handler.publish, |
paul@48 | 534 | "REFRESH" : lambda handler: handler.refresh, |
paul@48 | 535 | "REPLY" : lambda handler: handler.reply, |
paul@48 | 536 | "REQUEST" : lambda handler: handler.request, |
paul@48 | 537 | } |
paul@48 | 538 | |
paul@48 | 539 | # vim: tabstop=4 expandtab shiftwidth=4 |