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