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