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