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@152 | 25 | from imiptools.dates import * |
paul@76 | 26 | from imiptools.period import have_conflict, insert_period, remove_period |
paul@152 | 27 | from pytz import timezone |
paul@48 | 28 | from vCalendar import parse, ParseError, to_dict |
paul@75 | 29 | from vRecurrence import get_parameters, get_rule |
paul@129 | 30 | import email.utils |
paul@48 | 31 | import imip_store |
paul@48 | 32 | |
paul@48 | 33 | try: |
paul@48 | 34 | from cStringIO import StringIO |
paul@48 | 35 | except ImportError: |
paul@48 | 36 | from StringIO import StringIO |
paul@48 | 37 | |
paul@48 | 38 | # Content interpretation. |
paul@48 | 39 | |
paul@48 | 40 | def get_items(d, name, all=True): |
paul@48 | 41 | |
paul@48 | 42 | """ |
paul@48 | 43 | Get all items from 'd' with the given 'name', returning single items if |
paul@48 | 44 | 'all' is specified and set to a false value and if only one value is |
paul@48 | 45 | present for the name. Return None if no items are found for the name. |
paul@48 | 46 | """ |
paul@48 | 47 | |
paul@48 | 48 | if d.has_key(name): |
paul@48 | 49 | values = d[name] |
paul@48 | 50 | if not all and len(values) == 1: |
paul@48 | 51 | return values[0] |
paul@48 | 52 | else: |
paul@48 | 53 | return values |
paul@48 | 54 | else: |
paul@48 | 55 | return None |
paul@48 | 56 | |
paul@48 | 57 | def get_item(d, name): |
paul@48 | 58 | return get_items(d, name, False) |
paul@48 | 59 | |
paul@48 | 60 | def get_value_map(d, name): |
paul@48 | 61 | |
paul@48 | 62 | """ |
paul@48 | 63 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@48 | 64 | dictionary will map values for the name to any attributes or qualifiers |
paul@48 | 65 | that may have been present. |
paul@48 | 66 | """ |
paul@48 | 67 | |
paul@48 | 68 | items = get_items(d, name) |
paul@48 | 69 | if items: |
paul@48 | 70 | return dict(items) |
paul@48 | 71 | else: |
paul@48 | 72 | return {} |
paul@48 | 73 | |
paul@48 | 74 | def get_values(d, name, all=True): |
paul@48 | 75 | if d.has_key(name): |
paul@48 | 76 | values = d[name] |
paul@48 | 77 | if not all and len(values) == 1: |
paul@48 | 78 | return values[0][0] |
paul@48 | 79 | else: |
paul@48 | 80 | return map(lambda x: x[0], values) |
paul@48 | 81 | else: |
paul@48 | 82 | return None |
paul@48 | 83 | |
paul@48 | 84 | def get_value(d, name): |
paul@48 | 85 | return get_values(d, name, False) |
paul@48 | 86 | |
paul@48 | 87 | def get_utc_datetime(d, name): |
paul@48 | 88 | value, attr = get_item(d, name) |
paul@48 | 89 | dt = get_datetime(value, attr) |
paul@48 | 90 | return to_utc_datetime(dt) |
paul@48 | 91 | |
paul@129 | 92 | def get_addresses(values): |
paul@129 | 93 | return [address for name, address in email.utils.getaddresses(values)] |
paul@129 | 94 | |
paul@48 | 95 | def get_address(value): |
paul@134 | 96 | return value.lower().startswith("mailto:") and value.lower()[7:] or value |
paul@48 | 97 | |
paul@48 | 98 | def get_uri(value): |
paul@137 | 99 | return value.lower().startswith("mailto:") and value.lower() or ":" in value and value or "mailto:%s" % value.lower() |
paul@48 | 100 | |
paul@129 | 101 | def uri_dict(d): |
paul@129 | 102 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@129 | 103 | |
paul@129 | 104 | def uri_item(item): |
paul@129 | 105 | return get_uri(item[0]), item[1] |
paul@129 | 106 | |
paul@129 | 107 | def uri_items(items): |
paul@129 | 108 | return [(get_uri(value), attr) for value, attr in items] |
paul@129 | 109 | |
paul@159 | 110 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, partstat_set): |
paul@159 | 111 | |
paul@159 | 112 | """ |
paul@159 | 113 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@159 | 114 | 'new_dtstamp', and the 'partstat_set' indication, whether the object |
paul@159 | 115 | providing the new information is really newer than the object providing the |
paul@159 | 116 | old information. |
paul@159 | 117 | """ |
paul@159 | 118 | |
paul@159 | 119 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@159 | 120 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@159 | 121 | |
paul@159 | 122 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@159 | 123 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@159 | 124 | |
paul@159 | 125 | is_old_sequence = have_sequence and ( |
paul@159 | 126 | int(new_sequence) < int(old_sequence) or |
paul@159 | 127 | is_same_sequence and is_old_dtstamp |
paul@159 | 128 | ) |
paul@159 | 129 | |
paul@159 | 130 | return is_same_sequence and partstat_set or not is_old_sequence |
paul@159 | 131 | |
paul@75 | 132 | # NOTE: Need to expose the 100 day window for recurring events in the |
paul@75 | 133 | # NOTE: configuration. |
paul@75 | 134 | |
paul@75 | 135 | def get_periods(obj, window_size=100): |
paul@75 | 136 | |
paul@76 | 137 | """ |
paul@76 | 138 | Return periods for the given object 'obj', confining materialised periods |
paul@76 | 139 | to the given 'window_size' in days starting from the present moment. |
paul@76 | 140 | """ |
paul@76 | 141 | |
paul@75 | 142 | dtstart = get_utc_datetime(obj, "DTSTART") |
paul@75 | 143 | dtend = get_utc_datetime(obj, "DTEND") |
paul@75 | 144 | |
paul@75 | 145 | # NOTE: Need also DURATION support. |
paul@75 | 146 | |
paul@75 | 147 | duration = dtend - dtstart |
paul@75 | 148 | |
paul@75 | 149 | # Recurrence rules create multiple instances to be checked. |
paul@75 | 150 | # Conflicts may only be assessed within a period defined by policy |
paul@75 | 151 | # for the agent, with instances outside that period being considered |
paul@75 | 152 | # unchecked. |
paul@75 | 153 | |
paul@75 | 154 | window_end = datetime.now() + timedelta(window_size) |
paul@75 | 155 | |
paul@75 | 156 | # NOTE: Need also RDATE and EXDATE support. |
paul@75 | 157 | |
paul@75 | 158 | rrule = get_value(obj, "RRULE") |
paul@75 | 159 | |
paul@75 | 160 | if rrule: |
paul@75 | 161 | selector = get_rule(dtstart, rrule) |
paul@75 | 162 | parameters = get_parameters(rrule) |
paul@75 | 163 | periods = [] |
paul@75 | 164 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): |
paul@75 | 165 | start = datetime(*start, tzinfo=timezone("UTC")) |
paul@75 | 166 | end = start + duration |
paul@75 | 167 | periods.append((format_datetime(start), format_datetime(end))) |
paul@75 | 168 | else: |
paul@75 | 169 | periods = [(format_datetime(dtstart), format_datetime(dtend))] |
paul@75 | 170 | |
paul@75 | 171 | return periods |
paul@75 | 172 | |
paul@122 | 173 | def remove_from_freebusy(freebusy, attendee, uid, store): |
paul@122 | 174 | |
paul@122 | 175 | """ |
paul@122 | 176 | For the given 'attendee', remove periods from 'freebusy' that are associated |
paul@122 | 177 | with 'uid' in the 'store'. |
paul@122 | 178 | """ |
paul@122 | 179 | |
paul@122 | 180 | remove_period(freebusy, uid) |
paul@122 | 181 | store.set_freebusy(attendee, freebusy) |
paul@122 | 182 | |
paul@168 | 183 | def remove_from_freebusy_for_other(freebusy, user, other, uid, store): |
paul@168 | 184 | |
paul@168 | 185 | """ |
paul@168 | 186 | For the given 'user', remove for the 'other' party periods from 'freebusy' |
paul@168 | 187 | that are associated with 'uid' in the 'store'. |
paul@168 | 188 | """ |
paul@168 | 189 | |
paul@168 | 190 | remove_period(freebusy, uid) |
paul@168 | 191 | store.set_freebusy_for_other(user, freebusy, other) |
paul@168 | 192 | |
paul@168 | 193 | def _update_freebusy(freebusy, periods, transp, uid): |
paul@168 | 194 | |
paul@168 | 195 | """ |
paul@168 | 196 | Update the free/busy details with the given 'periods', 'transp' setting and |
paul@168 | 197 | 'uid'. |
paul@168 | 198 | """ |
paul@168 | 199 | |
paul@168 | 200 | remove_period(freebusy, uid) |
paul@168 | 201 | |
paul@168 | 202 | for start, end in periods: |
paul@168 | 203 | insert_period(freebusy, (start, end, uid, transp)) |
paul@168 | 204 | |
paul@77 | 205 | def update_freebusy(freebusy, attendee, periods, transp, uid, store): |
paul@76 | 206 | |
paul@76 | 207 | """ |
paul@76 | 208 | For the given 'attendee', update the free/busy details with the given |
paul@77 | 209 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@76 | 210 | """ |
paul@76 | 211 | |
paul@168 | 212 | _update_freebusy(freebusy, periods, transp, uid) |
paul@168 | 213 | store.set_freebusy(attendee, freebusy) |
paul@168 | 214 | |
paul@168 | 215 | def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): |
paul@77 | 216 | |
paul@168 | 217 | """ |
paul@168 | 218 | For the given 'user', update the free/busy details of 'other' with the given |
paul@168 | 219 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@168 | 220 | """ |
paul@76 | 221 | |
paul@168 | 222 | _update_freebusy(freebusy, periods, transp, uid) |
paul@168 | 223 | store.set_freebusy_for_other(user, freebusy, other) |
paul@76 | 224 | |
paul@77 | 225 | def can_schedule(freebusy, periods, uid): |
paul@76 | 226 | |
paul@77 | 227 | """ |
paul@77 | 228 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@77 | 229 | employing the specified 'uid'. |
paul@77 | 230 | """ |
paul@76 | 231 | |
paul@77 | 232 | for conflict in have_conflict(freebusy, periods, True): |
paul@112 | 233 | start, end, found_uid, found_transp = conflict |
paul@77 | 234 | if found_uid != uid: |
paul@77 | 235 | return False |
paul@76 | 236 | |
paul@77 | 237 | return True |
paul@76 | 238 | |
paul@48 | 239 | # Handler mechanism objects. |
paul@48 | 240 | |
paul@89 | 241 | def handle_itip_part(part, senders, recipients, handlers, messenger): |
paul@48 | 242 | |
paul@48 | 243 | """ |
paul@89 | 244 | Handle the given iTIP 'part' from the given 'senders' for the given |
paul@89 | 245 | 'recipients' using the given 'handlers' and information provided by the |
paul@89 | 246 | given 'messenger'. Return a list of responses, each response being a tuple |
paul@89 | 247 | of the form (is-outgoing, message-part). |
paul@48 | 248 | """ |
paul@48 | 249 | |
paul@48 | 250 | method = part.get_param("method") |
paul@48 | 251 | |
paul@48 | 252 | # Decode the data and parse it. |
paul@48 | 253 | |
paul@48 | 254 | f = StringIO(part.get_payload(decode=True)) |
paul@48 | 255 | |
paul@48 | 256 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@48 | 257 | |
paul@48 | 258 | # Ignore the part if not a calendar object. |
paul@48 | 259 | |
paul@48 | 260 | if not itip: |
paul@48 | 261 | return [] |
paul@48 | 262 | |
paul@48 | 263 | # Require consistency between declared and employed methods. |
paul@48 | 264 | |
paul@48 | 265 | if get_value(itip, "METHOD") == method: |
paul@48 | 266 | |
paul@48 | 267 | # Look for different kinds of sections. |
paul@48 | 268 | |
paul@60 | 269 | all_results = [] |
paul@48 | 270 | |
paul@48 | 271 | for name, cls in handlers: |
paul@48 | 272 | for details in get_values(itip, name) or []: |
paul@48 | 273 | |
paul@48 | 274 | # Dispatch to a handler and obtain any response. |
paul@48 | 275 | |
paul@89 | 276 | handler = cls(details, senders, recipients, messenger) |
paul@60 | 277 | result = methods[method](handler)() |
paul@48 | 278 | |
paul@89 | 279 | # Aggregate responses for a single message. |
paul@48 | 280 | |
paul@60 | 281 | if result: |
paul@60 | 282 | response_method, part = result |
paul@60 | 283 | outgoing = method != response_method |
paul@60 | 284 | all_results.append((outgoing, part)) |
paul@48 | 285 | |
paul@60 | 286 | return all_results |
paul@48 | 287 | |
paul@60 | 288 | return [] |
paul@48 | 289 | |
paul@70 | 290 | def parse_object(f, encoding, objtype=None): |
paul@48 | 291 | |
paul@48 | 292 | """ |
paul@70 | 293 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@78 | 294 | given, only objects of that type will be returned. Otherwise, the root of |
paul@78 | 295 | the content will be returned as a dictionary with a single key indicating |
paul@78 | 296 | the object type. |
paul@70 | 297 | |
paul@70 | 298 | Return None if the content was not readable or suitable. |
paul@48 | 299 | """ |
paul@48 | 300 | |
paul@48 | 301 | try: |
paul@48 | 302 | try: |
paul@48 | 303 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@70 | 304 | if objtype and doctype == objtype: |
paul@48 | 305 | return to_dict(obj)[objtype][0] |
paul@70 | 306 | elif not objtype: |
paul@78 | 307 | return to_dict(obj) |
paul@48 | 308 | finally: |
paul@48 | 309 | f.close() |
paul@108 | 310 | |
paul@108 | 311 | # NOTE: Handle parse errors properly. |
paul@108 | 312 | |
paul@48 | 313 | except (ParseError, ValueError): |
paul@48 | 314 | pass |
paul@48 | 315 | |
paul@48 | 316 | return None |
paul@48 | 317 | |
paul@48 | 318 | def to_part(method, calendar): |
paul@48 | 319 | |
paul@48 | 320 | """ |
paul@48 | 321 | Write using the given 'method', the 'calendar' details to a MIME |
paul@48 | 322 | text/calendar part. |
paul@48 | 323 | """ |
paul@48 | 324 | |
paul@48 | 325 | encoding = "utf-8" |
paul@48 | 326 | out = StringIO() |
paul@48 | 327 | try: |
paul@48 | 328 | imip_store.to_stream(out, imip_store.make_calendar(calendar, method), encoding) |
paul@48 | 329 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@48 | 330 | part.set_param("method", method) |
paul@48 | 331 | return part |
paul@48 | 332 | |
paul@48 | 333 | finally: |
paul@48 | 334 | out.close() |
paul@48 | 335 | |
paul@48 | 336 | class Handler: |
paul@48 | 337 | |
paul@48 | 338 | "General handler support." |
paul@48 | 339 | |
paul@89 | 340 | def __init__(self, details, senders=None, recipients=None, messenger=None): |
paul@48 | 341 | |
paul@48 | 342 | """ |
paul@48 | 343 | Initialise the handler with the 'details' of a calendar object and the |
paul@89 | 344 | 'senders' and 'recipients' of the object (if specifically indicated). |
paul@48 | 345 | """ |
paul@48 | 346 | |
paul@48 | 347 | self.details = details |
paul@129 | 348 | self.senders = senders and set(map(get_address, senders)) |
paul@129 | 349 | self.recipients = recipients and set(map(get_address, recipients)) |
paul@89 | 350 | self.messenger = messenger |
paul@48 | 351 | |
paul@48 | 352 | self.uid = get_value(details, "UID") |
paul@48 | 353 | self.sequence = get_value(details, "SEQUENCE") |
paul@48 | 354 | self.dtstamp = get_value(details, "DTSTAMP") |
paul@48 | 355 | |
paul@48 | 356 | self.store = imip_store.FileStore() |
paul@48 | 357 | |
paul@48 | 358 | try: |
paul@48 | 359 | self.publisher = imip_store.FilePublisher() |
paul@48 | 360 | except OSError: |
paul@48 | 361 | self.publisher = None |
paul@48 | 362 | |
paul@70 | 363 | # Access to calendar structures and other data. |
paul@70 | 364 | |
paul@48 | 365 | def get_items(self, name, all=True): |
paul@48 | 366 | return get_items(self.details, name, all) |
paul@48 | 367 | |
paul@48 | 368 | def get_item(self, name): |
paul@48 | 369 | return get_item(self.details, name) |
paul@48 | 370 | |
paul@48 | 371 | def get_value_map(self, name): |
paul@48 | 372 | return get_value_map(self.details, name) |
paul@48 | 373 | |
paul@48 | 374 | def get_values(self, name, all=True): |
paul@48 | 375 | return get_values(self.details, name, all) |
paul@48 | 376 | |
paul@48 | 377 | def get_value(self, name): |
paul@48 | 378 | return get_value(self.details, name) |
paul@48 | 379 | |
paul@48 | 380 | def get_utc_datetime(self, name): |
paul@48 | 381 | return get_utc_datetime(self.details, name) |
paul@48 | 382 | |
paul@75 | 383 | def get_periods(self): |
paul@75 | 384 | return get_periods(self.details) |
paul@75 | 385 | |
paul@122 | 386 | def remove_from_freebusy(self, freebusy, attendee): |
paul@122 | 387 | remove_from_freebusy(freebusy, attendee, self.uid, self.store) |
paul@122 | 388 | |
paul@168 | 389 | def remove_from_freebusy_for_other(self, freebusy, user, other): |
paul@168 | 390 | remove_from_freebusy_for_other(freebusy, user, other, self.uid, self.store) |
paul@168 | 391 | |
paul@77 | 392 | def update_freebusy(self, freebusy, attendee, periods): |
paul@77 | 393 | return update_freebusy(freebusy, attendee, periods, self.get_value("TRANSP"), self.uid, self.store) |
paul@77 | 394 | |
paul@168 | 395 | def update_freebusy_for_other(self, freebusy, user, other, periods): |
paul@168 | 396 | return update_freebusy_for_other(freebusy, user, other, periods, self.get_value("TRANSP"), self.uid, self.store) |
paul@168 | 397 | |
paul@77 | 398 | def can_schedule(self, freebusy, periods): |
paul@77 | 399 | return can_schedule(freebusy, periods, self.uid) |
paul@76 | 400 | |
paul@168 | 401 | def filter_by_senders(self, mapping): |
paul@168 | 402 | |
paul@168 | 403 | """ |
paul@168 | 404 | Return a list of items from 'mapping' filtered using sender information. |
paul@168 | 405 | """ |
paul@168 | 406 | |
paul@89 | 407 | if self.senders: |
paul@168 | 408 | |
paul@168 | 409 | # Get a mapping from senders to identities. |
paul@168 | 410 | |
paul@168 | 411 | identities = self.get_sender_identities(mapping) |
paul@168 | 412 | |
paul@168 | 413 | # Find the senders that are valid. |
paul@168 | 414 | |
paul@168 | 415 | senders = map(get_address, identities) |
paul@168 | 416 | valid = self.senders.intersection(senders) |
paul@89 | 417 | |
paul@168 | 418 | # Return the true identities. |
paul@168 | 419 | |
paul@168 | 420 | return [identities[get_uri(address)] for address in valid] |
paul@168 | 421 | else: |
paul@168 | 422 | return mapping |
paul@168 | 423 | |
paul@168 | 424 | def filter_by_recipients(self, mapping): |
paul@168 | 425 | |
paul@168 | 426 | """ |
paul@168 | 427 | Return a list of items from 'mapping' filtered using recipient |
paul@168 | 428 | information. |
paul@168 | 429 | """ |
paul@168 | 430 | |
paul@89 | 431 | if self.recipients: |
paul@168 | 432 | addresses = map(get_address, mapping) |
paul@168 | 433 | return map(get_uri, self.recipients.intersection(addresses)) |
paul@89 | 434 | else: |
paul@168 | 435 | return mapping |
paul@48 | 436 | |
paul@100 | 437 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@51 | 438 | |
paul@51 | 439 | """ |
paul@51 | 440 | Return the organiser and attendees for the current object, filtered by |
paul@51 | 441 | the recipients of interest. Return None if no identities are eligible. |
paul@129 | 442 | |
paul@129 | 443 | Organiser and attendee identities are provided as lower case values. |
paul@51 | 444 | """ |
paul@51 | 445 | |
paul@129 | 446 | attendee_map = uri_dict(self.get_value_map("ATTENDEE")) |
paul@168 | 447 | organiser_item = uri_item(self.get_item("ORGANIZER")) |
paul@48 | 448 | |
paul@168 | 449 | # Only provide details for attendees who sent/receive the message. |
paul@48 | 450 | |
paul@168 | 451 | attendee_filter_fn = from_organiser and self.filter_by_recipients or self.filter_by_senders |
paul@100 | 452 | |
paul@48 | 453 | attendees = {} |
paul@168 | 454 | for attendee in attendee_filter_fn(attendee_map): |
paul@48 | 455 | attendees[attendee] = attendee_map[attendee] |
paul@48 | 456 | |
paul@168 | 457 | if not attendees or not organiser_item: |
paul@168 | 458 | return None |
paul@168 | 459 | |
paul@168 | 460 | # Only provide details for an organiser who sent/receives the message. |
paul@168 | 461 | |
paul@168 | 462 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipients |
paul@168 | 463 | |
paul@168 | 464 | if not organiser_filter_fn(dict([organiser_item])): |
paul@89 | 465 | return None |
paul@89 | 466 | |
paul@168 | 467 | return organiser_item, attendees |
paul@94 | 468 | |
paul@168 | 469 | def get_sender_identities(self, mapping): |
paul@94 | 470 | |
paul@94 | 471 | """ |
paul@168 | 472 | Return a mapping from actual senders to the identities for which they |
paul@168 | 473 | have provided data, extracting this information from the given |
paul@168 | 474 | 'mapping'. |
paul@94 | 475 | """ |
paul@94 | 476 | |
paul@168 | 477 | senders = {} |
paul@89 | 478 | |
paul@168 | 479 | for value, attr in mapping.items(): |
paul@94 | 480 | sent_by = attr.get("SENT-BY") |
paul@94 | 481 | if sent_by: |
paul@168 | 482 | senders[get_uri(sent_by)] = value |
paul@168 | 483 | else: |
paul@168 | 484 | senders[value] = value |
paul@48 | 485 | |
paul@168 | 486 | return senders |
paul@48 | 487 | |
paul@100 | 488 | def get_object(self, user, objtype): |
paul@100 | 489 | |
paul@100 | 490 | """ |
paul@100 | 491 | Return the stored object to which the current object refers for the |
paul@100 | 492 | given 'user' and for the given 'objtype'. |
paul@100 | 493 | """ |
paul@100 | 494 | |
paul@100 | 495 | f = self.store.get_event(user, self.uid) |
paul@100 | 496 | obj = f and parse_object(f, "utf-8", objtype) |
paul@100 | 497 | return obj |
paul@100 | 498 | |
paul@100 | 499 | def have_new_object(self, attendee, objtype, obj=None): |
paul@51 | 500 | |
paul@51 | 501 | """ |
paul@51 | 502 | Return whether the current object is new to the 'attendee' for the |
paul@51 | 503 | given 'objtype'. |
paul@51 | 504 | """ |
paul@51 | 505 | |
paul@100 | 506 | obj = obj or self.get_object(attendee, objtype) |
paul@51 | 507 | |
paul@51 | 508 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@51 | 509 | |
paul@100 | 510 | if obj: |
paul@100 | 511 | sequence = get_value(obj, "SEQUENCE") |
paul@100 | 512 | dtstamp = get_value(obj, "DTSTAMP") |
paul@51 | 513 | |
paul@159 | 514 | # NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@159 | 515 | # NOTE: and make it invalid. Thus, attendance information may also be |
paul@159 | 516 | # NOTE: checked. |
paul@159 | 517 | |
paul@168 | 518 | for _attendee, old_attr in get_items(obj, "ATTENDEE"): |
paul@168 | 519 | if _attendee == attendee: |
paul@168 | 520 | break |
paul@168 | 521 | else: |
paul@168 | 522 | return False |
paul@168 | 523 | |
paul@168 | 524 | for _attendee, new_attr in self.get_items("ATTENDEE"): |
paul@168 | 525 | if _attendee == attendee: |
paul@168 | 526 | break |
paul@168 | 527 | else: |
paul@168 | 528 | return False |
paul@168 | 529 | |
paul@159 | 530 | old_partstat = old_attr.get("PARTSTAT") |
paul@159 | 531 | new_partstat = new_attr.get("PARTSTAT") |
paul@159 | 532 | |
paul@159 | 533 | partstat_set = old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@159 | 534 | new_partstat != old_partstat |
paul@159 | 535 | |
paul@100 | 536 | # If the request refers to an older version of the object, ignore |
paul@51 | 537 | # it. |
paul@51 | 538 | |
paul@159 | 539 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, partstat_set) |
paul@51 | 540 | |
paul@51 | 541 | return True |
paul@51 | 542 | |
paul@158 | 543 | def update_dtstamp(self): |
paul@158 | 544 | |
paul@158 | 545 | "Update the DTSTAMP in the current object." |
paul@158 | 546 | |
paul@158 | 547 | dtstamp = self.get_utc_datetime("DTSTAMP") |
paul@158 | 548 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@158 | 549 | self.details["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@158 | 550 | |
paul@48 | 551 | # Handler registry. |
paul@48 | 552 | |
paul@48 | 553 | methods = { |
paul@48 | 554 | "ADD" : lambda handler: handler.add, |
paul@48 | 555 | "CANCEL" : lambda handler: handler.cancel, |
paul@48 | 556 | "COUNTER" : lambda handler: handler.counter, |
paul@48 | 557 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@48 | 558 | "PUBLISH" : lambda handler: handler.publish, |
paul@48 | 559 | "REFRESH" : lambda handler: handler.refresh, |
paul@48 | 560 | "REPLY" : lambda handler: handler.reply, |
paul@48 | 561 | "REQUEST" : lambda handler: handler.request, |
paul@48 | 562 | } |
paul@48 | 563 | |
paul@48 | 564 | # vim: tabstop=4 expandtab shiftwidth=4 |