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