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@182 | 25 | from imiptools.config import MANAGER_PATH, MANAGER_URL |
paul@213 | 26 | from imiptools.data import Object, parse_object, \ |
paul@226 | 27 | get_address, get_uri, get_value, \ |
paul@220 | 28 | is_new_object, uri_dict, uri_item |
paul@152 | 29 | from imiptools.dates import * |
paul@221 | 30 | from imiptools.period import can_schedule, insert_period, remove_period |
paul@152 | 31 | from pytz import timezone |
paul@182 | 32 | from socket import gethostname |
paul@75 | 33 | from vRecurrence import get_parameters, get_rule |
paul@48 | 34 | import imip_store |
paul@48 | 35 | |
paul@48 | 36 | try: |
paul@48 | 37 | from cStringIO import StringIO |
paul@48 | 38 | except ImportError: |
paul@48 | 39 | from StringIO import StringIO |
paul@48 | 40 | |
paul@75 | 41 | # NOTE: Need to expose the 100 day window for recurring events in the |
paul@75 | 42 | # NOTE: configuration. |
paul@75 | 43 | |
paul@75 | 44 | def get_periods(obj, window_size=100): |
paul@75 | 45 | |
paul@76 | 46 | """ |
paul@76 | 47 | Return periods for the given object 'obj', confining materialised periods |
paul@76 | 48 | to the given 'window_size' in days starting from the present moment. |
paul@76 | 49 | """ |
paul@76 | 50 | |
paul@213 | 51 | dtstart = obj.get_utc_datetime("DTSTART") |
paul@213 | 52 | dtend = obj.get_utc_datetime("DTEND") |
paul@75 | 53 | |
paul@75 | 54 | # NOTE: Need also DURATION support. |
paul@75 | 55 | |
paul@75 | 56 | duration = dtend - dtstart |
paul@75 | 57 | |
paul@75 | 58 | # Recurrence rules create multiple instances to be checked. |
paul@75 | 59 | # Conflicts may only be assessed within a period defined by policy |
paul@75 | 60 | # for the agent, with instances outside that period being considered |
paul@75 | 61 | # unchecked. |
paul@75 | 62 | |
paul@75 | 63 | window_end = datetime.now() + timedelta(window_size) |
paul@75 | 64 | |
paul@75 | 65 | # NOTE: Need also RDATE and EXDATE support. |
paul@75 | 66 | |
paul@213 | 67 | rrule = obj.get_value("RRULE") |
paul@75 | 68 | |
paul@75 | 69 | if rrule: |
paul@75 | 70 | selector = get_rule(dtstart, rrule) |
paul@75 | 71 | parameters = get_parameters(rrule) |
paul@75 | 72 | periods = [] |
paul@75 | 73 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): |
paul@75 | 74 | start = datetime(*start, tzinfo=timezone("UTC")) |
paul@75 | 75 | end = start + duration |
paul@75 | 76 | periods.append((format_datetime(start), format_datetime(end))) |
paul@75 | 77 | else: |
paul@75 | 78 | periods = [(format_datetime(dtstart), format_datetime(dtend))] |
paul@75 | 79 | |
paul@75 | 80 | return periods |
paul@75 | 81 | |
paul@122 | 82 | def remove_from_freebusy(freebusy, attendee, uid, store): |
paul@122 | 83 | |
paul@122 | 84 | """ |
paul@122 | 85 | For the given 'attendee', remove periods from 'freebusy' that are associated |
paul@122 | 86 | with 'uid' in the 'store'. |
paul@122 | 87 | """ |
paul@122 | 88 | |
paul@122 | 89 | remove_period(freebusy, uid) |
paul@122 | 90 | store.set_freebusy(attendee, freebusy) |
paul@122 | 91 | |
paul@168 | 92 | def remove_from_freebusy_for_other(freebusy, user, other, uid, store): |
paul@168 | 93 | |
paul@168 | 94 | """ |
paul@168 | 95 | For the given 'user', remove for the 'other' party periods from 'freebusy' |
paul@168 | 96 | that are associated with 'uid' in the 'store'. |
paul@168 | 97 | """ |
paul@168 | 98 | |
paul@168 | 99 | remove_period(freebusy, uid) |
paul@168 | 100 | store.set_freebusy_for_other(user, freebusy, other) |
paul@168 | 101 | |
paul@168 | 102 | def _update_freebusy(freebusy, periods, transp, uid): |
paul@168 | 103 | |
paul@168 | 104 | """ |
paul@168 | 105 | Update the free/busy details with the given 'periods', 'transp' setting and |
paul@168 | 106 | 'uid'. |
paul@168 | 107 | """ |
paul@168 | 108 | |
paul@168 | 109 | remove_period(freebusy, uid) |
paul@168 | 110 | |
paul@168 | 111 | for start, end in periods: |
paul@168 | 112 | insert_period(freebusy, (start, end, uid, transp)) |
paul@168 | 113 | |
paul@77 | 114 | def update_freebusy(freebusy, attendee, periods, transp, uid, store): |
paul@76 | 115 | |
paul@76 | 116 | """ |
paul@76 | 117 | For the given 'attendee', update the free/busy details with the given |
paul@77 | 118 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@76 | 119 | """ |
paul@76 | 120 | |
paul@168 | 121 | _update_freebusy(freebusy, periods, transp, uid) |
paul@168 | 122 | store.set_freebusy(attendee, freebusy) |
paul@168 | 123 | |
paul@168 | 124 | def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): |
paul@77 | 125 | |
paul@168 | 126 | """ |
paul@168 | 127 | For the given 'user', update the free/busy details of 'other' with the given |
paul@168 | 128 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@168 | 129 | """ |
paul@76 | 130 | |
paul@168 | 131 | _update_freebusy(freebusy, periods, transp, uid) |
paul@168 | 132 | store.set_freebusy_for_other(user, freebusy, other) |
paul@76 | 133 | |
paul@48 | 134 | # Handler mechanism objects. |
paul@48 | 135 | |
paul@224 | 136 | def handle_itip_part(part, handlers): |
paul@48 | 137 | |
paul@48 | 138 | """ |
paul@224 | 139 | Handle the given iTIP 'part' using the given 'handlers'. |
paul@224 | 140 | |
paul@224 | 141 | Return a list of responses, each response being a tuple of the form |
paul@224 | 142 | (outgoing-recipients, message-part). |
paul@48 | 143 | """ |
paul@48 | 144 | |
paul@226 | 145 | handlers = dict(handlers) |
paul@48 | 146 | method = part.get_param("method") |
paul@48 | 147 | |
paul@48 | 148 | # Decode the data and parse it. |
paul@48 | 149 | |
paul@48 | 150 | f = StringIO(part.get_payload(decode=True)) |
paul@48 | 151 | |
paul@48 | 152 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@48 | 153 | |
paul@48 | 154 | # Ignore the part if not a calendar object. |
paul@48 | 155 | |
paul@48 | 156 | if not itip: |
paul@48 | 157 | return [] |
paul@48 | 158 | |
paul@48 | 159 | # Require consistency between declared and employed methods. |
paul@48 | 160 | |
paul@48 | 161 | if get_value(itip, "METHOD") == method: |
paul@48 | 162 | |
paul@48 | 163 | # Look for different kinds of sections. |
paul@48 | 164 | |
paul@60 | 165 | all_results = [] |
paul@48 | 166 | |
paul@226 | 167 | for name, items in itip.items(): |
paul@226 | 168 | |
paul@226 | 169 | # Get a handler for the given section. |
paul@226 | 170 | |
paul@226 | 171 | handler = handlers.get(name) |
paul@226 | 172 | if not handler: |
paul@226 | 173 | continue |
paul@226 | 174 | |
paul@226 | 175 | for item in items: |
paul@48 | 176 | |
paul@48 | 177 | # Dispatch to a handler and obtain any response. |
paul@48 | 178 | |
paul@226 | 179 | handler.set_object(Object({name : item})) |
paul@182 | 180 | results = methods[method](handler)() |
paul@48 | 181 | |
paul@89 | 182 | # Aggregate responses for a single message. |
paul@48 | 183 | |
paul@182 | 184 | if results: |
paul@182 | 185 | for result in results: |
paul@215 | 186 | outgoing_recipients, part = result |
paul@215 | 187 | all_results.append((outgoing_recipients, part)) |
paul@48 | 188 | |
paul@60 | 189 | return all_results |
paul@48 | 190 | |
paul@60 | 191 | return [] |
paul@48 | 192 | |
paul@182 | 193 | # References to the Web interface. |
paul@182 | 194 | |
paul@182 | 195 | def get_manager_url(): |
paul@182 | 196 | url_base = MANAGER_URL or "http://%s/" % gethostname() |
paul@182 | 197 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@182 | 198 | |
paul@182 | 199 | def get_object_url(uid): |
paul@182 | 200 | return "%s/%s" % (get_manager_url().rstrip("/"), uid) |
paul@182 | 201 | |
paul@48 | 202 | class Handler: |
paul@48 | 203 | |
paul@48 | 204 | "General handler support." |
paul@48 | 205 | |
paul@224 | 206 | def __init__(self, senders=None, recipient=None, messenger=None): |
paul@48 | 207 | |
paul@48 | 208 | """ |
paul@213 | 209 | Initialise the handler with the calendar 'obj' and the 'senders' and |
paul@213 | 210 | 'recipient' of the object (if specifically indicated). |
paul@48 | 211 | """ |
paul@48 | 212 | |
paul@129 | 213 | self.senders = senders and set(map(get_address, senders)) |
paul@179 | 214 | self.recipient = recipient and get_address(recipient) |
paul@89 | 215 | self.messenger = messenger |
paul@48 | 216 | |
paul@226 | 217 | self.obj = None |
paul@226 | 218 | self.uid = None |
paul@226 | 219 | self.sequence = None |
paul@226 | 220 | self.dtstamp = None |
paul@48 | 221 | |
paul@48 | 222 | self.store = imip_store.FileStore() |
paul@48 | 223 | |
paul@48 | 224 | try: |
paul@48 | 225 | self.publisher = imip_store.FilePublisher() |
paul@48 | 226 | except OSError: |
paul@48 | 227 | self.publisher = None |
paul@48 | 228 | |
paul@224 | 229 | def set_object(self, obj): |
paul@224 | 230 | self.obj = obj |
paul@226 | 231 | self.uid = self.obj.get_value("UID") |
paul@226 | 232 | self.sequence = self.obj.get_value("SEQUENCE") |
paul@226 | 233 | self.dtstamp = self.obj.get_value("DTSTAMP") |
paul@224 | 234 | |
paul@182 | 235 | def wrap(self, text, link=True): |
paul@182 | 236 | |
paul@182 | 237 | "Wrap any valid message for passing to the recipient." |
paul@182 | 238 | |
paul@182 | 239 | texts = [] |
paul@182 | 240 | texts.append(text) |
paul@182 | 241 | if link: |
paul@182 | 242 | texts.append("If your mail program cannot handle this " |
paul@182 | 243 | "message, you may view the details here:\n\n%s" % |
paul@182 | 244 | get_object_url(self.uid)) |
paul@182 | 245 | |
paul@215 | 246 | return [(None, MIMEText("\n".join(texts)))] |
paul@182 | 247 | |
paul@70 | 248 | # Access to calendar structures and other data. |
paul@70 | 249 | |
paul@75 | 250 | def get_periods(self): |
paul@213 | 251 | return get_periods(self.obj) |
paul@75 | 252 | |
paul@122 | 253 | def remove_from_freebusy(self, freebusy, attendee): |
paul@122 | 254 | remove_from_freebusy(freebusy, attendee, self.uid, self.store) |
paul@122 | 255 | |
paul@168 | 256 | def remove_from_freebusy_for_other(self, freebusy, user, other): |
paul@168 | 257 | remove_from_freebusy_for_other(freebusy, user, other, self.uid, self.store) |
paul@168 | 258 | |
paul@77 | 259 | def update_freebusy(self, freebusy, attendee, periods): |
paul@213 | 260 | return update_freebusy(freebusy, attendee, periods, self.obj.get_value("TRANSP"), self.uid, self.store) |
paul@77 | 261 | |
paul@168 | 262 | def update_freebusy_for_other(self, freebusy, user, other, periods): |
paul@213 | 263 | return update_freebusy_for_other(freebusy, user, other, periods, self.obj.get_value("TRANSP"), self.uid, self.store) |
paul@168 | 264 | |
paul@77 | 265 | def can_schedule(self, freebusy, periods): |
paul@77 | 266 | return can_schedule(freebusy, periods, self.uid) |
paul@76 | 267 | |
paul@168 | 268 | def filter_by_senders(self, mapping): |
paul@168 | 269 | |
paul@168 | 270 | """ |
paul@168 | 271 | Return a list of items from 'mapping' filtered using sender information. |
paul@168 | 272 | """ |
paul@168 | 273 | |
paul@89 | 274 | if self.senders: |
paul@168 | 275 | |
paul@168 | 276 | # Get a mapping from senders to identities. |
paul@168 | 277 | |
paul@168 | 278 | identities = self.get_sender_identities(mapping) |
paul@168 | 279 | |
paul@168 | 280 | # Find the senders that are valid. |
paul@168 | 281 | |
paul@168 | 282 | senders = map(get_address, identities) |
paul@168 | 283 | valid = self.senders.intersection(senders) |
paul@89 | 284 | |
paul@168 | 285 | # Return the true identities. |
paul@168 | 286 | |
paul@168 | 287 | return [identities[get_uri(address)] for address in valid] |
paul@168 | 288 | else: |
paul@168 | 289 | return mapping |
paul@168 | 290 | |
paul@179 | 291 | def filter_by_recipient(self, mapping): |
paul@168 | 292 | |
paul@168 | 293 | """ |
paul@168 | 294 | Return a list of items from 'mapping' filtered using recipient |
paul@168 | 295 | information. |
paul@168 | 296 | """ |
paul@168 | 297 | |
paul@179 | 298 | if self.recipient: |
paul@179 | 299 | addresses = set(map(get_address, mapping)) |
paul@179 | 300 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@89 | 301 | else: |
paul@168 | 302 | return mapping |
paul@48 | 303 | |
paul@100 | 304 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@51 | 305 | |
paul@51 | 306 | """ |
paul@179 | 307 | Return the organiser and attendees for the current object, filtered for |
paul@179 | 308 | the recipient of interest. Return None if no identities are eligible. |
paul@129 | 309 | |
paul@129 | 310 | Organiser and attendee identities are provided as lower case values. |
paul@51 | 311 | """ |
paul@51 | 312 | |
paul@213 | 313 | attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@213 | 314 | organiser_item = uri_item(self.obj.get_item("ORGANIZER")) |
paul@48 | 315 | |
paul@168 | 316 | # Only provide details for attendees who sent/receive the message. |
paul@48 | 317 | |
paul@179 | 318 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@100 | 319 | |
paul@48 | 320 | attendees = {} |
paul@168 | 321 | for attendee in attendee_filter_fn(attendee_map): |
paul@48 | 322 | attendees[attendee] = attendee_map[attendee] |
paul@48 | 323 | |
paul@168 | 324 | if not attendees or not organiser_item: |
paul@168 | 325 | return None |
paul@168 | 326 | |
paul@168 | 327 | # Only provide details for an organiser who sent/receives the message. |
paul@168 | 328 | |
paul@179 | 329 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@168 | 330 | |
paul@168 | 331 | if not organiser_filter_fn(dict([organiser_item])): |
paul@89 | 332 | return None |
paul@89 | 333 | |
paul@168 | 334 | return organiser_item, attendees |
paul@94 | 335 | |
paul@168 | 336 | def get_sender_identities(self, mapping): |
paul@94 | 337 | |
paul@94 | 338 | """ |
paul@168 | 339 | Return a mapping from actual senders to the identities for which they |
paul@168 | 340 | have provided data, extracting this information from the given |
paul@168 | 341 | 'mapping'. |
paul@94 | 342 | """ |
paul@94 | 343 | |
paul@168 | 344 | senders = {} |
paul@89 | 345 | |
paul@168 | 346 | for value, attr in mapping.items(): |
paul@94 | 347 | sent_by = attr.get("SENT-BY") |
paul@94 | 348 | if sent_by: |
paul@168 | 349 | senders[get_uri(sent_by)] = value |
paul@168 | 350 | else: |
paul@168 | 351 | senders[value] = value |
paul@48 | 352 | |
paul@168 | 353 | return senders |
paul@48 | 354 | |
paul@213 | 355 | def get_object(self, user): |
paul@100 | 356 | |
paul@100 | 357 | """ |
paul@100 | 358 | Return the stored object to which the current object refers for the |
paul@100 | 359 | given 'user' and for the given 'objtype'. |
paul@100 | 360 | """ |
paul@100 | 361 | |
paul@100 | 362 | f = self.store.get_event(user, self.uid) |
paul@213 | 363 | fragment = f and parse_object(f, "utf-8") |
paul@213 | 364 | return fragment and Object(fragment) |
paul@100 | 365 | |
paul@213 | 366 | def have_new_object(self, attendee, obj=None): |
paul@51 | 367 | |
paul@51 | 368 | """ |
paul@213 | 369 | Return whether the current object is new to the 'attendee' (or if the |
paul@213 | 370 | given 'obj' is new). |
paul@51 | 371 | """ |
paul@51 | 372 | |
paul@213 | 373 | obj = obj or self.get_object(attendee) |
paul@51 | 374 | |
paul@51 | 375 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@51 | 376 | |
paul@100 | 377 | if obj: |
paul@213 | 378 | sequence = obj.get_value("SEQUENCE") |
paul@213 | 379 | dtstamp = obj.get_value("DTSTAMP") |
paul@51 | 380 | |
paul@100 | 381 | # If the request refers to an older version of the object, ignore |
paul@51 | 382 | # it. |
paul@51 | 383 | |
paul@172 | 384 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@172 | 385 | self.is_partstat_updated(obj)) |
paul@51 | 386 | |
paul@51 | 387 | return True |
paul@51 | 388 | |
paul@172 | 389 | def is_partstat_updated(self, obj): |
paul@174 | 390 | |
paul@174 | 391 | """ |
paul@174 | 392 | Return whether the participant status has been updated in the current |
paul@174 | 393 | object in comparison to the given 'obj'. |
paul@174 | 394 | |
paul@174 | 395 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@174 | 396 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@174 | 397 | NOTE: incorporated into any new object assessment. |
paul@174 | 398 | """ |
paul@174 | 399 | |
paul@213 | 400 | old_attendees = obj.get_value_map("ATTENDEE") |
paul@213 | 401 | new_attendees = self.obj.get_value_map("ATTENDEE") |
paul@172 | 402 | |
paul@172 | 403 | for attendee, attr in old_attendees.items(): |
paul@172 | 404 | old_partstat = attr.get("PARTSTAT") |
paul@172 | 405 | new_attr = new_attendees.get(attendee) |
paul@172 | 406 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@172 | 407 | |
paul@172 | 408 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@172 | 409 | new_partstat != old_partstat: |
paul@172 | 410 | |
paul@172 | 411 | return True |
paul@172 | 412 | |
paul@172 | 413 | return False |
paul@172 | 414 | |
paul@158 | 415 | def update_dtstamp(self): |
paul@158 | 416 | |
paul@158 | 417 | "Update the DTSTAMP in the current object." |
paul@158 | 418 | |
paul@213 | 419 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@158 | 420 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@213 | 421 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@158 | 422 | |
paul@48 | 423 | # Handler registry. |
paul@48 | 424 | |
paul@48 | 425 | methods = { |
paul@48 | 426 | "ADD" : lambda handler: handler.add, |
paul@48 | 427 | "CANCEL" : lambda handler: handler.cancel, |
paul@48 | 428 | "COUNTER" : lambda handler: handler.counter, |
paul@48 | 429 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@48 | 430 | "PUBLISH" : lambda handler: handler.publish, |
paul@48 | 431 | "REFRESH" : lambda handler: handler.refresh, |
paul@48 | 432 | "REPLY" : lambda handler: handler.reply, |
paul@48 | 433 | "REQUEST" : lambda handler: handler.request, |
paul@48 | 434 | } |
paul@48 | 435 | |
paul@48 | 436 | # vim: tabstop=4 expandtab shiftwidth=4 |