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