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@152 | 26 | from imiptools.dates import * |
paul@76 | 27 | from imiptools.period import have_conflict, insert_period, remove_period |
paul@152 | 28 | from pytz import timezone |
paul@182 | 29 | from socket import gethostname |
paul@48 | 30 | from vCalendar import parse, ParseError, to_dict |
paul@75 | 31 | from vRecurrence import get_parameters, get_rule |
paul@129 | 32 | import email.utils |
paul@48 | 33 | import imip_store |
paul@48 | 34 | |
paul@48 | 35 | try: |
paul@48 | 36 | from cStringIO import StringIO |
paul@48 | 37 | except ImportError: |
paul@48 | 38 | from StringIO import StringIO |
paul@48 | 39 | |
paul@48 | 40 | # Content interpretation. |
paul@48 | 41 | |
paul@48 | 42 | def get_items(d, name, all=True): |
paul@48 | 43 | |
paul@48 | 44 | """ |
paul@48 | 45 | Get all items from 'd' with the given 'name', returning single items if |
paul@48 | 46 | 'all' is specified and set to a false value and if only one value is |
paul@169 | 47 | present for the name. Return None if no items are found for the name or if |
paul@169 | 48 | many items are found but 'all' is set to a false value. |
paul@48 | 49 | """ |
paul@48 | 50 | |
paul@48 | 51 | if d.has_key(name): |
paul@48 | 52 | values = d[name] |
paul@169 | 53 | if all: |
paul@169 | 54 | return values |
paul@169 | 55 | elif len(values) == 1: |
paul@48 | 56 | return values[0] |
paul@48 | 57 | else: |
paul@169 | 58 | return None |
paul@48 | 59 | else: |
paul@48 | 60 | return None |
paul@48 | 61 | |
paul@48 | 62 | def get_item(d, name): |
paul@48 | 63 | return get_items(d, name, False) |
paul@48 | 64 | |
paul@48 | 65 | def get_value_map(d, name): |
paul@48 | 66 | |
paul@48 | 67 | """ |
paul@48 | 68 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@48 | 69 | dictionary will map values for the name to any attributes or qualifiers |
paul@48 | 70 | that may have been present. |
paul@48 | 71 | """ |
paul@48 | 72 | |
paul@48 | 73 | items = get_items(d, name) |
paul@48 | 74 | if items: |
paul@48 | 75 | return dict(items) |
paul@48 | 76 | else: |
paul@48 | 77 | return {} |
paul@48 | 78 | |
paul@48 | 79 | def get_values(d, name, all=True): |
paul@48 | 80 | if d.has_key(name): |
paul@48 | 81 | values = d[name] |
paul@48 | 82 | if not all and len(values) == 1: |
paul@48 | 83 | return values[0][0] |
paul@48 | 84 | else: |
paul@48 | 85 | return map(lambda x: x[0], values) |
paul@48 | 86 | else: |
paul@48 | 87 | return None |
paul@48 | 88 | |
paul@48 | 89 | def get_value(d, name): |
paul@48 | 90 | return get_values(d, name, False) |
paul@48 | 91 | |
paul@48 | 92 | def get_utc_datetime(d, name): |
paul@48 | 93 | value, attr = get_item(d, name) |
paul@48 | 94 | dt = get_datetime(value, attr) |
paul@48 | 95 | return to_utc_datetime(dt) |
paul@48 | 96 | |
paul@129 | 97 | def get_addresses(values): |
paul@129 | 98 | return [address for name, address in email.utils.getaddresses(values)] |
paul@129 | 99 | |
paul@48 | 100 | def get_address(value): |
paul@134 | 101 | return value.lower().startswith("mailto:") and value.lower()[7:] or value |
paul@48 | 102 | |
paul@48 | 103 | def get_uri(value): |
paul@137 | 104 | return value.lower().startswith("mailto:") and value.lower() or ":" in value and value or "mailto:%s" % value.lower() |
paul@48 | 105 | |
paul@129 | 106 | def uri_dict(d): |
paul@129 | 107 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@129 | 108 | |
paul@129 | 109 | def uri_item(item): |
paul@129 | 110 | return get_uri(item[0]), item[1] |
paul@129 | 111 | |
paul@129 | 112 | def uri_items(items): |
paul@129 | 113 | return [(get_uri(value), attr) for value, attr in items] |
paul@129 | 114 | |
paul@159 | 115 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, partstat_set): |
paul@159 | 116 | |
paul@159 | 117 | """ |
paul@159 | 118 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@159 | 119 | 'new_dtstamp', and the 'partstat_set' indication, whether the object |
paul@159 | 120 | providing the new information is really newer than the object providing the |
paul@159 | 121 | old information. |
paul@159 | 122 | """ |
paul@159 | 123 | |
paul@159 | 124 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@159 | 125 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@159 | 126 | |
paul@159 | 127 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@159 | 128 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@159 | 129 | |
paul@159 | 130 | is_old_sequence = have_sequence and ( |
paul@159 | 131 | int(new_sequence) < int(old_sequence) or |
paul@159 | 132 | is_same_sequence and is_old_dtstamp |
paul@159 | 133 | ) |
paul@159 | 134 | |
paul@159 | 135 | return is_same_sequence and partstat_set or not is_old_sequence |
paul@159 | 136 | |
paul@75 | 137 | # NOTE: Need to expose the 100 day window for recurring events in the |
paul@75 | 138 | # NOTE: configuration. |
paul@75 | 139 | |
paul@75 | 140 | def get_periods(obj, window_size=100): |
paul@75 | 141 | |
paul@76 | 142 | """ |
paul@76 | 143 | Return periods for the given object 'obj', confining materialised periods |
paul@76 | 144 | to the given 'window_size' in days starting from the present moment. |
paul@76 | 145 | """ |
paul@76 | 146 | |
paul@75 | 147 | dtstart = get_utc_datetime(obj, "DTSTART") |
paul@75 | 148 | dtend = get_utc_datetime(obj, "DTEND") |
paul@75 | 149 | |
paul@75 | 150 | # NOTE: Need also DURATION support. |
paul@75 | 151 | |
paul@75 | 152 | duration = dtend - dtstart |
paul@75 | 153 | |
paul@75 | 154 | # Recurrence rules create multiple instances to be checked. |
paul@75 | 155 | # Conflicts may only be assessed within a period defined by policy |
paul@75 | 156 | # for the agent, with instances outside that period being considered |
paul@75 | 157 | # unchecked. |
paul@75 | 158 | |
paul@75 | 159 | window_end = datetime.now() + timedelta(window_size) |
paul@75 | 160 | |
paul@75 | 161 | # NOTE: Need also RDATE and EXDATE support. |
paul@75 | 162 | |
paul@75 | 163 | rrule = get_value(obj, "RRULE") |
paul@75 | 164 | |
paul@75 | 165 | if rrule: |
paul@75 | 166 | selector = get_rule(dtstart, rrule) |
paul@75 | 167 | parameters = get_parameters(rrule) |
paul@75 | 168 | periods = [] |
paul@75 | 169 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): |
paul@75 | 170 | start = datetime(*start, tzinfo=timezone("UTC")) |
paul@75 | 171 | end = start + duration |
paul@75 | 172 | periods.append((format_datetime(start), format_datetime(end))) |
paul@75 | 173 | else: |
paul@75 | 174 | periods = [(format_datetime(dtstart), format_datetime(dtend))] |
paul@75 | 175 | |
paul@75 | 176 | return periods |
paul@75 | 177 | |
paul@122 | 178 | def remove_from_freebusy(freebusy, attendee, uid, store): |
paul@122 | 179 | |
paul@122 | 180 | """ |
paul@122 | 181 | For the given 'attendee', remove periods from 'freebusy' that are associated |
paul@122 | 182 | with 'uid' in the 'store'. |
paul@122 | 183 | """ |
paul@122 | 184 | |
paul@122 | 185 | remove_period(freebusy, uid) |
paul@122 | 186 | store.set_freebusy(attendee, freebusy) |
paul@122 | 187 | |
paul@168 | 188 | def remove_from_freebusy_for_other(freebusy, user, other, uid, store): |
paul@168 | 189 | |
paul@168 | 190 | """ |
paul@168 | 191 | For the given 'user', remove for the 'other' party periods from 'freebusy' |
paul@168 | 192 | that are associated with 'uid' in the 'store'. |
paul@168 | 193 | """ |
paul@168 | 194 | |
paul@168 | 195 | remove_period(freebusy, uid) |
paul@168 | 196 | store.set_freebusy_for_other(user, freebusy, other) |
paul@168 | 197 | |
paul@168 | 198 | def _update_freebusy(freebusy, periods, transp, uid): |
paul@168 | 199 | |
paul@168 | 200 | """ |
paul@168 | 201 | Update the free/busy details with the given 'periods', 'transp' setting and |
paul@168 | 202 | 'uid'. |
paul@168 | 203 | """ |
paul@168 | 204 | |
paul@168 | 205 | remove_period(freebusy, uid) |
paul@168 | 206 | |
paul@168 | 207 | for start, end in periods: |
paul@168 | 208 | insert_period(freebusy, (start, end, uid, transp)) |
paul@168 | 209 | |
paul@77 | 210 | def update_freebusy(freebusy, attendee, periods, transp, uid, store): |
paul@76 | 211 | |
paul@76 | 212 | """ |
paul@76 | 213 | For the given 'attendee', update the free/busy details with the given |
paul@77 | 214 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@76 | 215 | """ |
paul@76 | 216 | |
paul@168 | 217 | _update_freebusy(freebusy, periods, transp, uid) |
paul@168 | 218 | store.set_freebusy(attendee, freebusy) |
paul@168 | 219 | |
paul@168 | 220 | def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): |
paul@77 | 221 | |
paul@168 | 222 | """ |
paul@168 | 223 | For the given 'user', update the free/busy details of 'other' with the given |
paul@168 | 224 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@168 | 225 | """ |
paul@76 | 226 | |
paul@168 | 227 | _update_freebusy(freebusy, periods, transp, uid) |
paul@168 | 228 | store.set_freebusy_for_other(user, freebusy, other) |
paul@76 | 229 | |
paul@77 | 230 | def can_schedule(freebusy, periods, uid): |
paul@76 | 231 | |
paul@77 | 232 | """ |
paul@77 | 233 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@77 | 234 | employing the specified 'uid'. |
paul@77 | 235 | """ |
paul@76 | 236 | |
paul@77 | 237 | for conflict in have_conflict(freebusy, periods, True): |
paul@112 | 238 | start, end, found_uid, found_transp = conflict |
paul@77 | 239 | if found_uid != uid: |
paul@77 | 240 | return False |
paul@76 | 241 | |
paul@77 | 242 | return True |
paul@76 | 243 | |
paul@48 | 244 | # Handler mechanism objects. |
paul@48 | 245 | |
paul@179 | 246 | def handle_itip_part(part, senders, recipient, handlers, messenger): |
paul@48 | 247 | |
paul@48 | 248 | """ |
paul@89 | 249 | Handle the given iTIP 'part' from the given 'senders' for the given |
paul@179 | 250 | 'recipient' using the given 'handlers' and information provided by the |
paul@89 | 251 | given 'messenger'. Return a list of responses, each response being a tuple |
paul@89 | 252 | of the form (is-outgoing, message-part). |
paul@48 | 253 | """ |
paul@48 | 254 | |
paul@48 | 255 | method = part.get_param("method") |
paul@48 | 256 | |
paul@48 | 257 | # Decode the data and parse it. |
paul@48 | 258 | |
paul@48 | 259 | f = StringIO(part.get_payload(decode=True)) |
paul@48 | 260 | |
paul@48 | 261 | itip = parse_object(f, part.get_content_charset(), "VCALENDAR") |
paul@48 | 262 | |
paul@48 | 263 | # Ignore the part if not a calendar object. |
paul@48 | 264 | |
paul@48 | 265 | if not itip: |
paul@48 | 266 | return [] |
paul@48 | 267 | |
paul@48 | 268 | # Require consistency between declared and employed methods. |
paul@48 | 269 | |
paul@48 | 270 | if get_value(itip, "METHOD") == method: |
paul@48 | 271 | |
paul@48 | 272 | # Look for different kinds of sections. |
paul@48 | 273 | |
paul@60 | 274 | all_results = [] |
paul@48 | 275 | |
paul@48 | 276 | for name, cls in handlers: |
paul@48 | 277 | for details in get_values(itip, name) or []: |
paul@48 | 278 | |
paul@48 | 279 | # Dispatch to a handler and obtain any response. |
paul@48 | 280 | |
paul@179 | 281 | handler = cls(details, senders, recipient, messenger) |
paul@182 | 282 | results = methods[method](handler)() |
paul@48 | 283 | |
paul@89 | 284 | # Aggregate responses for a single message. |
paul@48 | 285 | |
paul@182 | 286 | if results: |
paul@182 | 287 | for result in results: |
paul@182 | 288 | outgoing, part = result |
paul@182 | 289 | all_results.append((outgoing, part)) |
paul@48 | 290 | |
paul@60 | 291 | return all_results |
paul@48 | 292 | |
paul@60 | 293 | return [] |
paul@48 | 294 | |
paul@70 | 295 | def parse_object(f, encoding, objtype=None): |
paul@48 | 296 | |
paul@48 | 297 | """ |
paul@70 | 298 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@78 | 299 | given, only objects of that type will be returned. Otherwise, the root of |
paul@78 | 300 | the content will be returned as a dictionary with a single key indicating |
paul@78 | 301 | the object type. |
paul@70 | 302 | |
paul@70 | 303 | Return None if the content was not readable or suitable. |
paul@48 | 304 | """ |
paul@48 | 305 | |
paul@48 | 306 | try: |
paul@48 | 307 | try: |
paul@48 | 308 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@70 | 309 | if objtype and doctype == objtype: |
paul@48 | 310 | return to_dict(obj)[objtype][0] |
paul@70 | 311 | elif not objtype: |
paul@78 | 312 | return to_dict(obj) |
paul@48 | 313 | finally: |
paul@48 | 314 | f.close() |
paul@108 | 315 | |
paul@108 | 316 | # NOTE: Handle parse errors properly. |
paul@108 | 317 | |
paul@48 | 318 | except (ParseError, ValueError): |
paul@48 | 319 | pass |
paul@48 | 320 | |
paul@48 | 321 | return None |
paul@48 | 322 | |
paul@48 | 323 | def to_part(method, calendar): |
paul@48 | 324 | |
paul@48 | 325 | """ |
paul@48 | 326 | Write using the given 'method', the 'calendar' details to a MIME |
paul@48 | 327 | text/calendar part. |
paul@48 | 328 | """ |
paul@48 | 329 | |
paul@48 | 330 | encoding = "utf-8" |
paul@48 | 331 | out = StringIO() |
paul@48 | 332 | try: |
paul@48 | 333 | imip_store.to_stream(out, imip_store.make_calendar(calendar, method), encoding) |
paul@48 | 334 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@48 | 335 | part.set_param("method", method) |
paul@48 | 336 | return part |
paul@48 | 337 | |
paul@48 | 338 | finally: |
paul@48 | 339 | out.close() |
paul@48 | 340 | |
paul@182 | 341 | # References to the Web interface. |
paul@182 | 342 | |
paul@182 | 343 | def get_manager_url(): |
paul@182 | 344 | url_base = MANAGER_URL or "http://%s/" % gethostname() |
paul@182 | 345 | return "%s/%s" % (url_base.rstrip("/"), MANAGER_PATH.lstrip("/")) |
paul@182 | 346 | |
paul@182 | 347 | def get_object_url(uid): |
paul@182 | 348 | return "%s/%s" % (get_manager_url().rstrip("/"), uid) |
paul@182 | 349 | |
paul@48 | 350 | class Handler: |
paul@48 | 351 | |
paul@48 | 352 | "General handler support." |
paul@48 | 353 | |
paul@179 | 354 | def __init__(self, details, senders=None, recipient=None, messenger=None): |
paul@48 | 355 | |
paul@48 | 356 | """ |
paul@48 | 357 | Initialise the handler with the 'details' of a calendar object and the |
paul@179 | 358 | 'senders' and 'recipient' of the object (if specifically indicated). |
paul@48 | 359 | """ |
paul@48 | 360 | |
paul@48 | 361 | self.details = details |
paul@129 | 362 | self.senders = senders and set(map(get_address, senders)) |
paul@179 | 363 | self.recipient = recipient and get_address(recipient) |
paul@89 | 364 | self.messenger = messenger |
paul@48 | 365 | |
paul@205 | 366 | self.uid = self.get_value("UID") |
paul@205 | 367 | self.sequence = self.get_value("SEQUENCE") |
paul@205 | 368 | self.dtstamp = self.get_value("DTSTAMP") |
paul@48 | 369 | |
paul@48 | 370 | self.store = imip_store.FileStore() |
paul@48 | 371 | |
paul@48 | 372 | try: |
paul@48 | 373 | self.publisher = imip_store.FilePublisher() |
paul@48 | 374 | except OSError: |
paul@48 | 375 | self.publisher = None |
paul@48 | 376 | |
paul@182 | 377 | def wrap(self, text, link=True): |
paul@182 | 378 | |
paul@182 | 379 | "Wrap any valid message for passing to the recipient." |
paul@182 | 380 | |
paul@182 | 381 | texts = [] |
paul@182 | 382 | texts.append(text) |
paul@182 | 383 | if link: |
paul@182 | 384 | texts.append("If your mail program cannot handle this " |
paul@182 | 385 | "message, you may view the details here:\n\n%s" % |
paul@182 | 386 | get_object_url(self.uid)) |
paul@182 | 387 | |
paul@182 | 388 | return [(False, MIMEText("\n".join(texts)))] |
paul@182 | 389 | |
paul@70 | 390 | # Access to calendar structures and other data. |
paul@70 | 391 | |
paul@48 | 392 | def get_items(self, name, all=True): |
paul@48 | 393 | return get_items(self.details, name, all) |
paul@48 | 394 | |
paul@48 | 395 | def get_item(self, name): |
paul@48 | 396 | return get_item(self.details, name) |
paul@48 | 397 | |
paul@48 | 398 | def get_value_map(self, name): |
paul@48 | 399 | return get_value_map(self.details, name) |
paul@48 | 400 | |
paul@48 | 401 | def get_values(self, name, all=True): |
paul@48 | 402 | return get_values(self.details, name, all) |
paul@48 | 403 | |
paul@48 | 404 | def get_value(self, name): |
paul@48 | 405 | return get_value(self.details, name) |
paul@48 | 406 | |
paul@48 | 407 | def get_utc_datetime(self, name): |
paul@48 | 408 | return get_utc_datetime(self.details, name) |
paul@48 | 409 | |
paul@75 | 410 | def get_periods(self): |
paul@75 | 411 | return get_periods(self.details) |
paul@75 | 412 | |
paul@122 | 413 | def remove_from_freebusy(self, freebusy, attendee): |
paul@122 | 414 | remove_from_freebusy(freebusy, attendee, self.uid, self.store) |
paul@122 | 415 | |
paul@168 | 416 | def remove_from_freebusy_for_other(self, freebusy, user, other): |
paul@168 | 417 | remove_from_freebusy_for_other(freebusy, user, other, self.uid, self.store) |
paul@168 | 418 | |
paul@77 | 419 | def update_freebusy(self, freebusy, attendee, periods): |
paul@77 | 420 | return update_freebusy(freebusy, attendee, periods, self.get_value("TRANSP"), self.uid, self.store) |
paul@77 | 421 | |
paul@168 | 422 | def update_freebusy_for_other(self, freebusy, user, other, periods): |
paul@168 | 423 | return update_freebusy_for_other(freebusy, user, other, periods, self.get_value("TRANSP"), self.uid, self.store) |
paul@168 | 424 | |
paul@77 | 425 | def can_schedule(self, freebusy, periods): |
paul@77 | 426 | return can_schedule(freebusy, periods, self.uid) |
paul@76 | 427 | |
paul@168 | 428 | def filter_by_senders(self, mapping): |
paul@168 | 429 | |
paul@168 | 430 | """ |
paul@168 | 431 | Return a list of items from 'mapping' filtered using sender information. |
paul@168 | 432 | """ |
paul@168 | 433 | |
paul@89 | 434 | if self.senders: |
paul@168 | 435 | |
paul@168 | 436 | # Get a mapping from senders to identities. |
paul@168 | 437 | |
paul@168 | 438 | identities = self.get_sender_identities(mapping) |
paul@168 | 439 | |
paul@168 | 440 | # Find the senders that are valid. |
paul@168 | 441 | |
paul@168 | 442 | senders = map(get_address, identities) |
paul@168 | 443 | valid = self.senders.intersection(senders) |
paul@89 | 444 | |
paul@168 | 445 | # Return the true identities. |
paul@168 | 446 | |
paul@168 | 447 | return [identities[get_uri(address)] for address in valid] |
paul@168 | 448 | else: |
paul@168 | 449 | return mapping |
paul@168 | 450 | |
paul@179 | 451 | def filter_by_recipient(self, mapping): |
paul@168 | 452 | |
paul@168 | 453 | """ |
paul@168 | 454 | Return a list of items from 'mapping' filtered using recipient |
paul@168 | 455 | information. |
paul@168 | 456 | """ |
paul@168 | 457 | |
paul@179 | 458 | if self.recipient: |
paul@179 | 459 | addresses = set(map(get_address, mapping)) |
paul@179 | 460 | return map(get_uri, addresses.intersection([self.recipient])) |
paul@89 | 461 | else: |
paul@168 | 462 | return mapping |
paul@48 | 463 | |
paul@100 | 464 | def require_organiser_and_attendees(self, from_organiser=True): |
paul@51 | 465 | |
paul@51 | 466 | """ |
paul@179 | 467 | Return the organiser and attendees for the current object, filtered for |
paul@179 | 468 | the recipient of interest. Return None if no identities are eligible. |
paul@129 | 469 | |
paul@129 | 470 | Organiser and attendee identities are provided as lower case values. |
paul@51 | 471 | """ |
paul@51 | 472 | |
paul@129 | 473 | attendee_map = uri_dict(self.get_value_map("ATTENDEE")) |
paul@168 | 474 | organiser_item = uri_item(self.get_item("ORGANIZER")) |
paul@48 | 475 | |
paul@168 | 476 | # Only provide details for attendees who sent/receive the message. |
paul@48 | 477 | |
paul@179 | 478 | attendee_filter_fn = from_organiser and self.filter_by_recipient or self.filter_by_senders |
paul@100 | 479 | |
paul@48 | 480 | attendees = {} |
paul@168 | 481 | for attendee in attendee_filter_fn(attendee_map): |
paul@48 | 482 | attendees[attendee] = attendee_map[attendee] |
paul@48 | 483 | |
paul@168 | 484 | if not attendees or not organiser_item: |
paul@168 | 485 | return None |
paul@168 | 486 | |
paul@168 | 487 | # Only provide details for an organiser who sent/receives the message. |
paul@168 | 488 | |
paul@179 | 489 | organiser_filter_fn = from_organiser and self.filter_by_senders or self.filter_by_recipient |
paul@168 | 490 | |
paul@168 | 491 | if not organiser_filter_fn(dict([organiser_item])): |
paul@89 | 492 | return None |
paul@89 | 493 | |
paul@168 | 494 | return organiser_item, attendees |
paul@94 | 495 | |
paul@168 | 496 | def get_sender_identities(self, mapping): |
paul@94 | 497 | |
paul@94 | 498 | """ |
paul@168 | 499 | Return a mapping from actual senders to the identities for which they |
paul@168 | 500 | have provided data, extracting this information from the given |
paul@168 | 501 | 'mapping'. |
paul@94 | 502 | """ |
paul@94 | 503 | |
paul@168 | 504 | senders = {} |
paul@89 | 505 | |
paul@168 | 506 | for value, attr in mapping.items(): |
paul@94 | 507 | sent_by = attr.get("SENT-BY") |
paul@94 | 508 | if sent_by: |
paul@168 | 509 | senders[get_uri(sent_by)] = value |
paul@168 | 510 | else: |
paul@168 | 511 | senders[value] = value |
paul@48 | 512 | |
paul@168 | 513 | return senders |
paul@48 | 514 | |
paul@100 | 515 | def get_object(self, user, objtype): |
paul@100 | 516 | |
paul@100 | 517 | """ |
paul@100 | 518 | Return the stored object to which the current object refers for the |
paul@100 | 519 | given 'user' and for the given 'objtype'. |
paul@100 | 520 | """ |
paul@100 | 521 | |
paul@100 | 522 | f = self.store.get_event(user, self.uid) |
paul@100 | 523 | obj = f and parse_object(f, "utf-8", objtype) |
paul@100 | 524 | return obj |
paul@100 | 525 | |
paul@100 | 526 | def have_new_object(self, attendee, objtype, obj=None): |
paul@51 | 527 | |
paul@51 | 528 | """ |
paul@51 | 529 | Return whether the current object is new to the 'attendee' for the |
paul@51 | 530 | given 'objtype'. |
paul@51 | 531 | """ |
paul@51 | 532 | |
paul@100 | 533 | obj = obj or self.get_object(attendee, objtype) |
paul@51 | 534 | |
paul@51 | 535 | # If found, compare SEQUENCE and potentially DTSTAMP. |
paul@51 | 536 | |
paul@100 | 537 | if obj: |
paul@100 | 538 | sequence = get_value(obj, "SEQUENCE") |
paul@100 | 539 | dtstamp = get_value(obj, "DTSTAMP") |
paul@51 | 540 | |
paul@100 | 541 | # If the request refers to an older version of the object, ignore |
paul@51 | 542 | # it. |
paul@51 | 543 | |
paul@172 | 544 | return is_new_object(sequence, self.sequence, dtstamp, self.dtstamp, |
paul@172 | 545 | self.is_partstat_updated(obj)) |
paul@51 | 546 | |
paul@51 | 547 | return True |
paul@51 | 548 | |
paul@172 | 549 | def is_partstat_updated(self, obj): |
paul@174 | 550 | |
paul@174 | 551 | """ |
paul@174 | 552 | Return whether the participant status has been updated in the current |
paul@174 | 553 | object in comparison to the given 'obj'. |
paul@174 | 554 | |
paul@174 | 555 | NOTE: Some clients like Claws Mail erase time information from DTSTAMP |
paul@174 | 556 | NOTE: and make it invalid. Thus, such attendance information may also be |
paul@174 | 557 | NOTE: incorporated into any new object assessment. |
paul@174 | 558 | """ |
paul@174 | 559 | |
paul@172 | 560 | old_attendees = get_value_map(obj, "ATTENDEE") |
paul@172 | 561 | new_attendees = self.get_value_map("ATTENDEE") |
paul@172 | 562 | |
paul@172 | 563 | for attendee, attr in old_attendees.items(): |
paul@172 | 564 | old_partstat = attr.get("PARTSTAT") |
paul@172 | 565 | new_attr = new_attendees.get(attendee) |
paul@172 | 566 | new_partstat = new_attr and new_attr.get("PARTSTAT") |
paul@172 | 567 | |
paul@172 | 568 | if old_partstat == "NEEDS-ACTION" and new_partstat and \ |
paul@172 | 569 | new_partstat != old_partstat: |
paul@172 | 570 | |
paul@172 | 571 | return True |
paul@172 | 572 | |
paul@172 | 573 | return False |
paul@172 | 574 | |
paul@158 | 575 | def update_dtstamp(self): |
paul@158 | 576 | |
paul@158 | 577 | "Update the DTSTAMP in the current object." |
paul@158 | 578 | |
paul@158 | 579 | dtstamp = self.get_utc_datetime("DTSTAMP") |
paul@158 | 580 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@158 | 581 | self.details["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@158 | 582 | |
paul@48 | 583 | # Handler registry. |
paul@48 | 584 | |
paul@48 | 585 | methods = { |
paul@48 | 586 | "ADD" : lambda handler: handler.add, |
paul@48 | 587 | "CANCEL" : lambda handler: handler.cancel, |
paul@48 | 588 | "COUNTER" : lambda handler: handler.counter, |
paul@48 | 589 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@48 | 590 | "PUBLISH" : lambda handler: handler.publish, |
paul@48 | 591 | "REFRESH" : lambda handler: handler.refresh, |
paul@48 | 592 | "REPLY" : lambda handler: handler.reply, |
paul@48 | 593 | "REQUEST" : lambda handler: handler.request, |
paul@48 | 594 | } |
paul@48 | 595 | |
paul@48 | 596 | # vim: tabstop=4 expandtab shiftwidth=4 |