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@75 | 110 | # NOTE: Need to expose the 100 day window for recurring events in the |
paul@75 | 111 | # NOTE: configuration. |
paul@75 | 112 | |
paul@75 | 113 | def get_periods(obj, window_size=100): |
paul@75 | 114 | |
paul@76 | 115 | """ |
paul@76 | 116 | Return periods for the given object 'obj', confining materialised periods |
paul@76 | 117 | to the given 'window_size' in days starting from the present moment. |
paul@76 | 118 | """ |
paul@76 | 119 | |
paul@75 | 120 | dtstart = get_utc_datetime(obj, "DTSTART") |
paul@75 | 121 | dtend = get_utc_datetime(obj, "DTEND") |
paul@75 | 122 | |
paul@75 | 123 | # NOTE: Need also DURATION support. |
paul@75 | 124 | |
paul@75 | 125 | duration = dtend - dtstart |
paul@75 | 126 | |
paul@75 | 127 | # Recurrence rules create multiple instances to be checked. |
paul@75 | 128 | # Conflicts may only be assessed within a period defined by policy |
paul@75 | 129 | # for the agent, with instances outside that period being considered |
paul@75 | 130 | # unchecked. |
paul@75 | 131 | |
paul@75 | 132 | window_end = datetime.now() + timedelta(window_size) |
paul@75 | 133 | |
paul@75 | 134 | # NOTE: Need also RDATE and EXDATE support. |
paul@75 | 135 | |
paul@75 | 136 | rrule = get_value(obj, "RRULE") |
paul@75 | 137 | |
paul@75 | 138 | if rrule: |
paul@75 | 139 | selector = get_rule(dtstart, rrule) |
paul@75 | 140 | parameters = get_parameters(rrule) |
paul@75 | 141 | periods = [] |
paul@75 | 142 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): |
paul@75 | 143 | start = datetime(*start, tzinfo=timezone("UTC")) |
paul@75 | 144 | end = start + duration |
paul@75 | 145 | periods.append((format_datetime(start), format_datetime(end))) |
paul@75 | 146 | else: |
paul@75 | 147 | periods = [(format_datetime(dtstart), format_datetime(dtend))] |
paul@75 | 148 | |
paul@75 | 149 | return periods |
paul@75 | 150 | |
paul@122 | 151 | def remove_from_freebusy(freebusy, attendee, uid, store): |
paul@122 | 152 | |
paul@122 | 153 | """ |
paul@122 | 154 | For the given 'attendee', remove periods from 'freebusy' that are associated |
paul@122 | 155 | with 'uid' in the 'store'. |
paul@122 | 156 | """ |
paul@122 | 157 | |
paul@122 | 158 | remove_period(freebusy, uid) |
paul@122 | 159 | store.set_freebusy(attendee, freebusy) |
paul@122 | 160 | |
paul@77 | 161 | def update_freebusy(freebusy, attendee, periods, transp, uid, store): |
paul@76 | 162 | |
paul@76 | 163 | """ |
paul@76 | 164 | For the given 'attendee', update the free/busy details with the given |
paul@77 | 165 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@76 | 166 | """ |
paul@76 | 167 | |
paul@77 | 168 | remove_period(freebusy, uid) |
paul@77 | 169 | |
paul@77 | 170 | for start, end in periods: |
paul@112 | 171 | insert_period(freebusy, (start, end, uid, transp)) |
paul@76 | 172 | |
paul@112 | 173 | store.set_freebusy(attendee, freebusy) |
paul@76 | 174 | |
paul@77 | 175 | def can_schedule(freebusy, periods, uid): |
paul@76 | 176 | |
paul@77 | 177 | """ |
paul@77 | 178 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@77 | 179 | employing the specified 'uid'. |
paul@77 | 180 | """ |
paul@76 | 181 | |
paul@77 | 182 | for conflict in have_conflict(freebusy, periods, True): |
paul@112 | 183 | start, end, found_uid, found_transp = conflict |
paul@77 | 184 | if found_uid != uid: |
paul@77 | 185 | return False |
paul@76 | 186 | |
paul@77 | 187 | return True |
paul@76 | 188 | |
paul@48 | 189 | # Handler mechanism objects. |
paul@48 | 190 | |
paul@89 | 191 | def handle_itip_part(part, senders, recipients, handlers, messenger): |
paul@48 | 192 | |
paul@48 | 193 | """ |
paul@89 | 194 | Handle the given iTIP 'part' from the given 'senders' for the given |
paul@89 | 195 | 'recipients' using the given 'handlers' and information provided by the |
paul@89 | 196 | given 'messenger'. Return a list of responses, each response being a tuple |
paul@89 | 197 | of the form (is-outgoing, message-part). |
paul@48 | 198 | """ |
paul@48 | 199 | |
paul@48 | 200 | method = part.get_param("method") |
paul@48 | 201 | |
paul@48 | 202 | # Decode the data and parse it. |
paul@48 | 203 | |
paul@48 | 204 | f = StringIO(part.get_payload(decode=True)) |
paul@48 | 205 | |
paul@48 | 206 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@48 | 207 | |
paul@48 | 208 | # Ignore the part if not a calendar object. |
paul@48 | 209 | |
paul@48 | 210 | if not itip: |
paul@48 | 211 | return [] |
paul@48 | 212 | |
paul@48 | 213 | # Require consistency between declared and employed methods. |
paul@48 | 214 | |
paul@48 | 215 | if get_value(itip, "METHOD") == method: |
paul@48 | 216 | |
paul@48 | 217 | # Look for different kinds of sections. |
paul@48 | 218 | |
paul@60 | 219 | all_results = [] |
paul@48 | 220 | |
paul@48 | 221 | for name, cls in handlers: |
paul@48 | 222 | for details in get_values(itip, name) or []: |
paul@48 | 223 | |
paul@48 | 224 | # Dispatch to a handler and obtain any response. |
paul@48 | 225 | |
paul@89 | 226 | handler = cls(details, senders, recipients, messenger) |
paul@60 | 227 | result = methods[method](handler)() |
paul@48 | 228 | |
paul@89 | 229 | # Aggregate responses for a single message. |
paul@48 | 230 | |
paul@60 | 231 | if result: |
paul@60 | 232 | response_method, part = result |
paul@60 | 233 | outgoing = method != response_method |
paul@60 | 234 | all_results.append((outgoing, part)) |
paul@48 | 235 | |
paul@60 | 236 | return all_results |
paul@48 | 237 | |
paul@60 | 238 | return [] |
paul@48 | 239 | |
paul@70 | 240 | def parse_object(f, encoding, objtype=None): |
paul@48 | 241 | |
paul@48 | 242 | """ |
paul@70 | 243 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@78 | 244 | given, only objects of that type will be returned. Otherwise, the root of |
paul@78 | 245 | the content will be returned as a dictionary with a single key indicating |
paul@78 | 246 | the object type. |
paul@70 | 247 | |
paul@70 | 248 | Return None if the content was not readable or suitable. |
paul@48 | 249 | """ |
paul@48 | 250 | |
paul@48 | 251 | try: |
paul@48 | 252 | try: |
paul@48 | 253 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@70 | 254 | if objtype and doctype == objtype: |
paul@48 | 255 | return to_dict(obj)[objtype][0] |
paul@70 | 256 | elif not objtype: |
paul@78 | 257 | return to_dict(obj) |
paul@48 | 258 | finally: |
paul@48 | 259 | f.close() |
paul@108 | 260 | |
paul@108 | 261 | # NOTE: Handle parse errors properly. |
paul@108 | 262 | |
paul@48 | 263 | except (ParseError, ValueError): |
paul@48 | 264 | pass |
paul@48 | 265 | |
paul@48 | 266 | return None |
paul@48 | 267 | |
paul@48 | 268 | def to_part(method, calendar): |
paul@48 | 269 | |
paul@48 | 270 | """ |
paul@48 | 271 | Write using the given 'method', the 'calendar' details to a MIME |
paul@48 | 272 | text/calendar part. |
paul@48 | 273 | """ |
paul@48 | 274 | |
paul@48 | 275 | encoding = "utf-8" |
paul@48 | 276 | out = StringIO() |
paul@48 | 277 | try: |
paul@48 | 278 | imip_store.to_stream(out, imip_store.make_calendar(calendar, method), encoding) |
paul@48 | 279 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@48 | 280 | part.set_param("method", method) |
paul@48 | 281 | return part |
paul@48 | 282 | |
paul@48 | 283 | finally: |
paul@48 | 284 | out.close() |
paul@48 | 285 | |
paul@48 | 286 | class Handler: |
paul@48 | 287 | |
paul@48 | 288 | "General handler support." |
paul@48 | 289 | |
paul@89 | 290 | def __init__(self, details, senders=None, recipients=None, messenger=None): |
paul@48 | 291 | |
paul@48 | 292 | """ |
paul@48 | 293 | Initialise the handler with the 'details' of a calendar object and the |
paul@89 | 294 | 'senders' and 'recipients' of the object (if specifically indicated). |
paul@48 | 295 | """ |
paul@48 | 296 | |
paul@48 | 297 | self.details = details |
paul@129 | 298 | self.senders = senders and set(map(get_address, senders)) |
paul@129 | 299 | self.recipients = recipients and set(map(get_address, recipients)) |
paul@89 | 300 | self.messenger = messenger |
paul@48 | 301 | |
paul@48 | 302 | self.uid = get_value(details, "UID") |
paul@48 | 303 | self.sequence = get_value(details, "SEQUENCE") |
paul@48 | 304 | self.dtstamp = get_value(details, "DTSTAMP") |
paul@48 | 305 | |
paul@48 | 306 | self.store = imip_store.FileStore() |
paul@48 | 307 | |
paul@48 | 308 | try: |
paul@48 | 309 | self.publisher = imip_store.FilePublisher() |
paul@48 | 310 | except OSError: |
paul@48 | 311 | self.publisher = None |
paul@48 | 312 | |
paul@70 | 313 | # Access to calendar structures and other data. |
paul@70 | 314 | |
paul@48 | 315 | def get_items(self, name, all=True): |
paul@48 | 316 | return get_items(self.details, name, all) |
paul@48 | 317 | |
paul@48 | 318 | def get_item(self, name): |
paul@48 | 319 | return get_item(self.details, name) |
paul@48 | 320 | |
paul@48 | 321 | def get_value_map(self, name): |
paul@48 | 322 | return get_value_map(self.details, name) |
paul@48 | 323 | |
paul@48 | 324 | def get_values(self, name, all=True): |
paul@48 | 325 | return get_values(self.details, name, all) |
paul@48 | 326 | |
paul@48 | 327 | def get_value(self, name): |
paul@48 | 328 | return get_value(self.details, name) |
paul@48 | 329 | |
paul@48 | 330 | def get_utc_datetime(self, name): |
paul@48 | 331 | return get_utc_datetime(self.details, name) |
paul@48 | 332 | |
paul@75 | 333 | def get_periods(self): |
paul@75 | 334 | return get_periods(self.details) |
paul@75 | 335 | |
paul@122 | 336 | def remove_from_freebusy(self, freebusy, attendee): |
paul@122 | 337 | remove_from_freebusy(freebusy, attendee, self.uid, self.store) |
paul@122 | 338 | |
paul@77 | 339 | def update_freebusy(self, freebusy, attendee, periods): |
paul@77 | 340 | return update_freebusy(freebusy, attendee, periods, self.get_value("TRANSP"), self.uid, self.store) |
paul@77 | 341 | |
paul@77 | 342 | def can_schedule(self, freebusy, periods): |
paul@77 | 343 | return can_schedule(freebusy, periods, self.uid) |
paul@76 | 344 | |
paul@89 | 345 | def filter_by_senders(self, values): |
paul@89 | 346 | addresses = map(get_address, values) |
paul@89 | 347 | if self.senders: |
paul@89 | 348 | return self.senders.intersection(addresses) |
paul@89 | 349 | else: |
paul@89 | 350 | return addresses |
paul@89 | 351 | |
paul@48 | 352 | def filter_by_recipients(self, values): |
paul@79 | 353 | addresses = map(get_address, values) |
paul@89 | 354 | if self.recipients: |
paul@89 | 355 | return self.recipients.intersection(addresses) |
paul@89 | 356 | else: |
paul@89 | 357 | return addresses |
paul@48 | 358 | |
paul@100 | 359 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@51 | 360 | |
paul@51 | 361 | """ |
paul@51 | 362 | Return the organiser and attendees for the current object, filtered by |
paul@51 | 363 | the recipients of interest. Return None if no identities are eligible. |
paul@129 | 364 | |
paul@129 | 365 | Organiser and attendee identities are provided as lower case values. |
paul@51 | 366 | """ |
paul@51 | 367 | |
paul@129 | 368 | attendee_map = uri_dict(self.get_value_map("ATTENDEE")) |
paul@129 | 369 | organiser = uri_item(self.get_item("ORGANIZER")) |
paul@48 | 370 | |
paul@48 | 371 | # Only provide details for recipients who are also attendees. |
paul@48 | 372 | |
paul@100 | 373 | filter_fn = from_organiser and self.filter_by_recipients or self.filter_by_senders |
paul@100 | 374 | |
paul@48 | 375 | attendees = {} |
paul@100 | 376 | for attendee in map(get_uri, filter_fn(attendee_map)): |
paul@48 | 377 | attendees[attendee] = attendee_map[attendee] |
paul@48 | 378 | |
paul@89 | 379 | if not attendees or not organiser: |
paul@89 | 380 | return None |
paul@89 | 381 | |
paul@94 | 382 | return organiser, attendees |
paul@94 | 383 | |
paul@94 | 384 | def validate_identities(self, items): |
paul@94 | 385 | |
paul@94 | 386 | """ |
paul@94 | 387 | Validate the 'items' against the known senders, obtaining sent-by |
paul@94 | 388 | addresses from attributes provided by the items. |
paul@94 | 389 | """ |
paul@94 | 390 | |
paul@89 | 391 | # Reject organisers that do not match any senders. |
paul@89 | 392 | |
paul@94 | 393 | identities = [] |
paul@89 | 394 | |
paul@94 | 395 | for value, attr in items: |
paul@94 | 396 | identities.append(value) |
paul@94 | 397 | sent_by = attr.get("SENT-BY") |
paul@94 | 398 | if sent_by: |
paul@130 | 399 | identities.append(get_uri(sent_by)) |
paul@48 | 400 | |
paul@94 | 401 | return self.filter_by_senders(identities) |
paul@48 | 402 | |
paul@100 | 403 | def get_object(self, user, objtype): |
paul@100 | 404 | |
paul@100 | 405 | """ |
paul@100 | 406 | Return the stored object to which the current object refers for the |
paul@100 | 407 | given 'user' and for the given 'objtype'. |
paul@100 | 408 | """ |
paul@100 | 409 | |
paul@100 | 410 | f = self.store.get_event(user, self.uid) |
paul@100 | 411 | obj = f and parse_object(f, "utf-8", objtype) |
paul@100 | 412 | return obj |
paul@100 | 413 | |
paul@100 | 414 | def have_new_object(self, attendee, objtype, obj=None): |
paul@51 | 415 | |
paul@51 | 416 | """ |
paul@51 | 417 | Return whether the current object is new to the 'attendee' for the |
paul@51 | 418 | given 'objtype'. |
paul@51 | 419 | """ |
paul@51 | 420 | |
paul@100 | 421 | obj = obj or self.get_object(attendee, objtype) |
paul@51 | 422 | |
paul@51 | 423 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@51 | 424 | |
paul@100 | 425 | if obj: |
paul@100 | 426 | sequence = get_value(obj, "SEQUENCE") |
paul@100 | 427 | dtstamp = get_value(obj, "DTSTAMP") |
paul@51 | 428 | |
paul@100 | 429 | # If the request refers to an older version of the object, ignore |
paul@51 | 430 | # it. |
paul@51 | 431 | |
paul@51 | 432 | old_dtstamp = self.dtstamp < dtstamp |
paul@51 | 433 | |
paul@141 | 434 | have_sequence = sequence is not None and self.sequence is not None |
paul@141 | 435 | |
paul@141 | 436 | if have_sequence and ( |
paul@51 | 437 | int(self.sequence) < int(sequence) or |
paul@51 | 438 | int(self.sequence) == int(sequence) and old_dtstamp |
paul@141 | 439 | ) or not have_sequence and old_dtstamp: |
paul@51 | 440 | |
paul@51 | 441 | return False |
paul@51 | 442 | |
paul@51 | 443 | return True |
paul@51 | 444 | |
paul@48 | 445 | # Handler registry. |
paul@48 | 446 | |
paul@48 | 447 | methods = { |
paul@48 | 448 | "ADD" : lambda handler: handler.add, |
paul@48 | 449 | "CANCEL" : lambda handler: handler.cancel, |
paul@48 | 450 | "COUNTER" : lambda handler: handler.counter, |
paul@48 | 451 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@48 | 452 | "PUBLISH" : lambda handler: handler.publish, |
paul@48 | 453 | "REFRESH" : lambda handler: handler.refresh, |
paul@48 | 454 | "REPLY" : lambda handler: handler.reply, |
paul@48 | 455 | "REQUEST" : lambda handler: handler.request, |
paul@48 | 456 | } |
paul@48 | 457 | |
paul@48 | 458 | # vim: tabstop=4 expandtab shiftwidth=4 |