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