paul@213 | 1 | #!/usr/bin/env python |
paul@213 | 2 | |
paul@213 | 3 | """ |
paul@213 | 4 | Interpretation of vCalendar content. |
paul@213 | 5 | |
paul@213 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@213 | 7 | |
paul@213 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@213 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@213 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@213 | 11 | version. |
paul@213 | 12 | |
paul@213 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@213 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@213 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@213 | 16 | details. |
paul@213 | 17 | |
paul@213 | 18 | You should have received a copy of the GNU General Public License along with |
paul@213 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@213 | 20 | """ |
paul@213 | 21 | |
paul@424 | 22 | from bisect import bisect_left |
paul@560 | 23 | from datetime import date, datetime, timedelta |
paul@213 | 24 | from email.mime.text import MIMEText |
paul@669 | 25 | from imiptools.dates import check_permitted_values, correct_datetime, \ |
paul@661 | 26 | format_datetime, get_datetime, \ |
paul@625 | 27 | get_datetime_item as get_item_from_datetime, \ |
paul@625 | 28 | get_datetime_tzid, \ |
paul@628 | 29 | get_duration, get_period, get_period_item, \ |
paul@627 | 30 | get_recurrence_start_point, \ |
paul@627 | 31 | get_tzid, to_datetime, to_timezone, to_utc_datetime |
paul@648 | 32 | from imiptools.period import FreeBusyPeriod, Period, RecurringPeriod, period_overlaps |
paul@213 | 33 | from vCalendar import iterwrite, parse, ParseError, to_dict, to_node |
paul@256 | 34 | from vRecurrence import get_parameters, get_rule |
paul@213 | 35 | import email.utils |
paul@213 | 36 | |
paul@213 | 37 | try: |
paul@213 | 38 | from cStringIO import StringIO |
paul@213 | 39 | except ImportError: |
paul@213 | 40 | from StringIO import StringIO |
paul@213 | 41 | |
paul@213 | 42 | class Object: |
paul@213 | 43 | |
paul@213 | 44 | "Access to calendar structures." |
paul@213 | 45 | |
paul@213 | 46 | def __init__(self, fragment): |
paul@213 | 47 | self.objtype, (self.details, self.attr) = fragment.items()[0] |
paul@213 | 48 | |
paul@535 | 49 | def get_uid(self): |
paul@535 | 50 | return self.get_value("UID") |
paul@535 | 51 | |
paul@535 | 52 | def get_recurrenceid(self): |
paul@563 | 53 | |
paul@563 | 54 | """ |
paul@563 | 55 | Return the recurrence identifier, normalised to a UTC datetime if |
paul@627 | 56 | specified as a datetime or date with accompanying time zone information, |
paul@627 | 57 | maintained as a date or floating datetime otherwise. If no recurrence |
paul@627 | 58 | identifier is present, None is returned. |
paul@627 | 59 | |
paul@627 | 60 | Note that this normalised form of the identifier may well not be the |
paul@627 | 61 | same as the originally-specified identifier because that could have been |
paul@627 | 62 | specified using an accompanying TZID attribute, whereas the normalised |
paul@627 | 63 | form is effectively a converted datetime value. |
paul@563 | 64 | """ |
paul@563 | 65 | |
paul@627 | 66 | if not self.has_key("RECURRENCE-ID"): |
paul@627 | 67 | return None |
paul@627 | 68 | dt, attr = self.get_datetime_item("RECURRENCE-ID") |
paul@628 | 69 | |
paul@628 | 70 | # Coerce any date to a UTC datetime if TZID was specified. |
paul@628 | 71 | |
paul@627 | 72 | tzid = attr.get("TZID") |
paul@627 | 73 | if tzid: |
paul@627 | 74 | dt = to_timezone(to_datetime(dt, tzid), "UTC") |
paul@627 | 75 | return format_datetime(dt) |
paul@627 | 76 | |
paul@627 | 77 | def get_recurrence_start_point(self, recurrenceid, tzid): |
paul@627 | 78 | |
paul@627 | 79 | """ |
paul@627 | 80 | Return the start point corresponding to the given 'recurrenceid', using |
paul@627 | 81 | the fallback 'tzid' to define the specific point in time referenced by |
paul@627 | 82 | the recurrence identifier if the identifier has a date representation. |
paul@627 | 83 | |
paul@627 | 84 | If 'recurrenceid' is given as None, this object's recurrence identifier |
paul@627 | 85 | is used to obtain a start point, but if this object does not provide a |
paul@627 | 86 | recurrence, None is returned. |
paul@627 | 87 | |
paul@627 | 88 | A start point is typically used to match free/busy periods which are |
paul@627 | 89 | themselves defined in terms of UTC datetimes. |
paul@627 | 90 | """ |
paul@627 | 91 | |
paul@627 | 92 | recurrenceid = recurrenceid or self.get_recurrenceid() |
paul@627 | 93 | if recurrenceid: |
paul@627 | 94 | return get_recurrence_start_point(recurrenceid, tzid) |
paul@627 | 95 | else: |
paul@627 | 96 | return None |
paul@535 | 97 | |
paul@679 | 98 | def get_recurrence_start_points(self, recurrenceids, tzid): |
paul@679 | 99 | return [self.get_recurrence_start_point(recurrenceid, tzid) for recurrenceid in recurrenceids] |
paul@679 | 100 | |
paul@535 | 101 | # Structure access. |
paul@535 | 102 | |
paul@524 | 103 | def copy(self): |
paul@524 | 104 | return Object(to_dict(self.to_node())) |
paul@524 | 105 | |
paul@213 | 106 | def get_items(self, name, all=True): |
paul@213 | 107 | return get_items(self.details, name, all) |
paul@213 | 108 | |
paul@213 | 109 | def get_item(self, name): |
paul@213 | 110 | return get_item(self.details, name) |
paul@213 | 111 | |
paul@213 | 112 | def get_value_map(self, name): |
paul@213 | 113 | return get_value_map(self.details, name) |
paul@213 | 114 | |
paul@213 | 115 | def get_values(self, name, all=True): |
paul@213 | 116 | return get_values(self.details, name, all) |
paul@213 | 117 | |
paul@213 | 118 | def get_value(self, name): |
paul@213 | 119 | return get_value(self.details, name) |
paul@213 | 120 | |
paul@506 | 121 | def get_utc_datetime(self, name, date_tzid=None): |
paul@506 | 122 | return get_utc_datetime(self.details, name, date_tzid) |
paul@213 | 123 | |
paul@417 | 124 | def get_date_values(self, name, tzid=None): |
paul@417 | 125 | items = get_date_value_items(self.details, name, tzid) |
paul@389 | 126 | return items and [value for value, attr in items] |
paul@352 | 127 | |
paul@417 | 128 | def get_date_value_items(self, name, tzid=None): |
paul@417 | 129 | return get_date_value_items(self.details, name, tzid) |
paul@352 | 130 | |
paul@646 | 131 | def get_period_values(self, name, tzid=None): |
paul@646 | 132 | return get_period_values(self.details, name, tzid) |
paul@630 | 133 | |
paul@318 | 134 | def get_datetime(self, name): |
paul@567 | 135 | t = get_datetime_item(self.details, name) |
paul@567 | 136 | if not t: return None |
paul@567 | 137 | dt, attr = t |
paul@318 | 138 | return dt |
paul@318 | 139 | |
paul@289 | 140 | def get_datetime_item(self, name): |
paul@289 | 141 | return get_datetime_item(self.details, name) |
paul@289 | 142 | |
paul@392 | 143 | def get_duration(self, name): |
paul@392 | 144 | return get_duration(self.get_value(name)) |
paul@392 | 145 | |
paul@213 | 146 | def to_node(self): |
paul@213 | 147 | return to_node({self.objtype : [(self.details, self.attr)]}) |
paul@213 | 148 | |
paul@213 | 149 | def to_part(self, method): |
paul@213 | 150 | return to_part(method, [self.to_node()]) |
paul@213 | 151 | |
paul@213 | 152 | # Direct access to the structure. |
paul@213 | 153 | |
paul@392 | 154 | def has_key(self, name): |
paul@392 | 155 | return self.details.has_key(name) |
paul@392 | 156 | |
paul@524 | 157 | def get(self, name): |
paul@524 | 158 | return self.details.get(name) |
paul@524 | 159 | |
paul@734 | 160 | def keys(self): |
paul@734 | 161 | return self.details.keys() |
paul@734 | 162 | |
paul@213 | 163 | def __getitem__(self, name): |
paul@213 | 164 | return self.details[name] |
paul@213 | 165 | |
paul@213 | 166 | def __setitem__(self, name, value): |
paul@213 | 167 | self.details[name] = value |
paul@213 | 168 | |
paul@213 | 169 | def __delitem__(self, name): |
paul@213 | 170 | del self.details[name] |
paul@213 | 171 | |
paul@524 | 172 | def remove(self, name): |
paul@524 | 173 | try: |
paul@524 | 174 | del self[name] |
paul@524 | 175 | except KeyError: |
paul@524 | 176 | pass |
paul@524 | 177 | |
paul@524 | 178 | def remove_all(self, names): |
paul@524 | 179 | for name in names: |
paul@524 | 180 | self.remove(name) |
paul@524 | 181 | |
paul@734 | 182 | def preserve(self, names): |
paul@734 | 183 | for name in self.keys(): |
paul@734 | 184 | if not name in names: |
paul@734 | 185 | self.remove(name) |
paul@734 | 186 | |
paul@256 | 187 | # Computed results. |
paul@256 | 188 | |
paul@797 | 189 | def get_main_period(self, tzid): |
paul@797 | 190 | |
paul@797 | 191 | """ |
paul@797 | 192 | Return a period object corresponding to the main start-end period for |
paul@797 | 193 | the object. |
paul@797 | 194 | """ |
paul@797 | 195 | |
paul@797 | 196 | (dtstart, dtstart_attr), (dtend, dtend_attr) = self.get_main_period_items(tzid) |
paul@797 | 197 | return RecurringPeriod(dtstart, dtend, tzid, "DTSTART", dtstart_attr, dtend_attr) |
paul@797 | 198 | |
paul@650 | 199 | def get_main_period_items(self, tzid): |
paul@650 | 200 | |
paul@650 | 201 | """ |
paul@650 | 202 | Return two (value, attributes) items corresponding to the main start-end |
paul@650 | 203 | period for the object. |
paul@650 | 204 | """ |
paul@650 | 205 | |
paul@650 | 206 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@650 | 207 | |
paul@650 | 208 | if self.has_key("DTEND"): |
paul@650 | 209 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@650 | 210 | elif self.has_key("DURATION"): |
paul@650 | 211 | duration = self.get_duration("DURATION") |
paul@650 | 212 | dtend = dtstart + duration |
paul@650 | 213 | dtend_attr = dtstart_attr |
paul@650 | 214 | else: |
paul@650 | 215 | dtend, dtend_attr = dtstart, dtstart_attr |
paul@650 | 216 | |
paul@650 | 217 | return (dtstart, dtstart_attr), (dtend, dtend_attr) |
paul@650 | 218 | |
paul@630 | 219 | def get_periods(self, tzid, end=None): |
paul@620 | 220 | |
paul@620 | 221 | """ |
paul@620 | 222 | Return periods defined by this object, employing the given 'tzid' where |
paul@620 | 223 | no time zone information is defined, and limiting the collection to a |
paul@620 | 224 | window of time with the given 'end'. |
paul@630 | 225 | |
paul@630 | 226 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 227 | explicitly-terminated rules will be returned. |
paul@620 | 228 | """ |
paul@620 | 229 | |
paul@458 | 230 | return get_periods(self, tzid, end) |
paul@360 | 231 | |
paul@630 | 232 | def get_active_periods(self, recurrenceids, tzid, end=None): |
paul@630 | 233 | |
paul@630 | 234 | """ |
paul@630 | 235 | Return all periods specified by this object that are not replaced by |
paul@630 | 236 | those defined by 'recurrenceids', using 'tzid' as a fallback time zone |
paul@630 | 237 | to convert floating dates and datetimes, and using 'end' to indicate the |
paul@630 | 238 | end of the time window within which periods are considered. |
paul@630 | 239 | """ |
paul@630 | 240 | |
paul@630 | 241 | # Specific recurrences yield all specified periods. |
paul@630 | 242 | |
paul@630 | 243 | periods = self.get_periods(tzid, end) |
paul@630 | 244 | |
paul@630 | 245 | if self.get_recurrenceid(): |
paul@630 | 246 | return periods |
paul@630 | 247 | |
paul@630 | 248 | # Parent objects need to have their periods tested against redefined |
paul@630 | 249 | # recurrences. |
paul@630 | 250 | |
paul@630 | 251 | active = [] |
paul@630 | 252 | |
paul@630 | 253 | for p in periods: |
paul@630 | 254 | |
paul@630 | 255 | # Subtract any recurrences from the free/busy details of a |
paul@630 | 256 | # parent object. |
paul@630 | 257 | |
paul@648 | 258 | if not p.is_replaced(recurrenceids): |
paul@630 | 259 | active.append(p) |
paul@630 | 260 | |
paul@630 | 261 | return active |
paul@630 | 262 | |
paul@648 | 263 | def get_freebusy_period(self, period, only_organiser=False): |
paul@648 | 264 | |
paul@648 | 265 | """ |
paul@648 | 266 | Return a free/busy period for the given 'period' provided by this |
paul@648 | 267 | object, using the 'only_organiser' status to produce a suitable |
paul@648 | 268 | transparency value. |
paul@648 | 269 | """ |
paul@648 | 270 | |
paul@648 | 271 | return FreeBusyPeriod( |
paul@648 | 272 | period.get_start_point(), |
paul@648 | 273 | period.get_end_point(), |
paul@648 | 274 | self.get_value("UID"), |
paul@648 | 275 | only_organiser and "ORG" or self.get_value("TRANSP") or "OPAQUE", |
paul@648 | 276 | self.get_recurrenceid(), |
paul@648 | 277 | self.get_value("SUMMARY"), |
paul@648 | 278 | self.get_value("ORGANIZER") |
paul@648 | 279 | ) |
paul@648 | 280 | |
paul@648 | 281 | def get_participation_status(self, participant): |
paul@648 | 282 | |
paul@648 | 283 | """ |
paul@648 | 284 | Return the participation status of the given 'participant', with the |
paul@648 | 285 | special value "ORG" indicating organiser-only participation. |
paul@648 | 286 | """ |
paul@648 | 287 | |
paul@648 | 288 | attendees = self.get_value_map("ATTENDEE") |
paul@648 | 289 | organiser = self.get_value("ORGANIZER") |
paul@648 | 290 | |
paul@692 | 291 | attendee_attr = attendees.get(participant) |
paul@692 | 292 | if attendee_attr: |
paul@692 | 293 | return attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@692 | 294 | elif organiser == participant: |
paul@692 | 295 | return "ORG" |
paul@648 | 296 | |
paul@648 | 297 | return None |
paul@648 | 298 | |
paul@648 | 299 | def get_participation(self, partstat, include_needs_action=False): |
paul@648 | 300 | |
paul@648 | 301 | """ |
paul@648 | 302 | Return whether 'partstat' indicates some kind of participation in an |
paul@648 | 303 | event. If 'include_needs_action' is specified as a true value, events |
paul@648 | 304 | not yet responded to will be treated as events with tentative |
paul@648 | 305 | participation. |
paul@648 | 306 | """ |
paul@648 | 307 | |
paul@648 | 308 | return not partstat in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@648 | 309 | include_needs_action and partstat == "NEEDS-ACTION" or \ |
paul@648 | 310 | partstat == "ORG" |
paul@648 | 311 | |
paul@422 | 312 | def get_tzid(self): |
paul@562 | 313 | |
paul@562 | 314 | """ |
paul@562 | 315 | Return a time zone identifier used by the start or end datetimes, |
paul@562 | 316 | potentially suitable for converting dates to datetimes. |
paul@562 | 317 | """ |
paul@562 | 318 | |
paul@560 | 319 | if not self.has_key("DTSTART"): |
paul@560 | 320 | return None |
paul@422 | 321 | dtstart, dtstart_attr = self.get_datetime_item("DTSTART") |
paul@630 | 322 | if self.has_key("DTEND"): |
paul@630 | 323 | dtend, dtend_attr = self.get_datetime_item("DTEND") |
paul@630 | 324 | else: |
paul@630 | 325 | dtend_attr = None |
paul@422 | 326 | return get_tzid(dtstart_attr, dtend_attr) |
paul@422 | 327 | |
paul@619 | 328 | def is_shared(self): |
paul@619 | 329 | |
paul@619 | 330 | """ |
paul@619 | 331 | Return whether this object is shared based on the presence of a SEQUENCE |
paul@619 | 332 | property. |
paul@619 | 333 | """ |
paul@619 | 334 | |
paul@619 | 335 | return self.get_value("SEQUENCE") is not None |
paul@619 | 336 | |
paul@650 | 337 | def possibly_active_from(self, dt, tzid): |
paul@650 | 338 | |
paul@650 | 339 | """ |
paul@650 | 340 | Return whether the object is possibly active from or after the given |
paul@650 | 341 | datetime 'dt' using 'tzid' to convert any dates or floating datetimes. |
paul@650 | 342 | """ |
paul@650 | 343 | |
paul@650 | 344 | dt = to_datetime(dt, tzid) |
paul@650 | 345 | periods = self.get_periods(tzid) |
paul@650 | 346 | |
paul@650 | 347 | for p in periods: |
paul@650 | 348 | if p.get_end_point() > dt: |
paul@650 | 349 | return True |
paul@650 | 350 | |
paul@672 | 351 | return self.possibly_recurring_indefinitely() |
paul@672 | 352 | |
paul@672 | 353 | def possibly_recurring_indefinitely(self): |
paul@672 | 354 | |
paul@672 | 355 | "Return whether this object may recur indefinitely." |
paul@672 | 356 | |
paul@650 | 357 | rrule = self.get_value("RRULE") |
paul@650 | 358 | parameters = rrule and get_parameters(rrule) |
paul@650 | 359 | until = parameters and parameters.get("UNTIL") |
paul@651 | 360 | count = parameters and parameters.get("COUNT") |
paul@650 | 361 | |
paul@672 | 362 | # Non-recurring periods or constrained recurrences. |
paul@651 | 363 | |
paul@651 | 364 | if not rrule or until or count: |
paul@650 | 365 | return False |
paul@651 | 366 | |
paul@672 | 367 | # Unconstrained recurring periods will always lie beyond any specified |
paul@651 | 368 | # datetime. |
paul@651 | 369 | |
paul@651 | 370 | else: |
paul@650 | 371 | return True |
paul@650 | 372 | |
paul@627 | 373 | # Modification methods. |
paul@627 | 374 | |
paul@627 | 375 | def set_datetime(self, name, dt, tzid=None): |
paul@627 | 376 | |
paul@627 | 377 | """ |
paul@627 | 378 | Set a datetime for property 'name' using 'dt' and the optional fallback |
paul@627 | 379 | 'tzid', returning whether an update has occurred. |
paul@627 | 380 | """ |
paul@627 | 381 | |
paul@627 | 382 | if dt: |
paul@627 | 383 | old_value = self.get_value(name) |
paul@627 | 384 | self[name] = [get_item_from_datetime(dt, tzid)] |
paul@627 | 385 | return format_datetime(dt) != old_value |
paul@627 | 386 | |
paul@627 | 387 | return False |
paul@627 | 388 | |
paul@627 | 389 | def set_period(self, period): |
paul@627 | 390 | |
paul@627 | 391 | "Set the given 'period' as the main start and end." |
paul@627 | 392 | |
paul@627 | 393 | result = self.set_datetime("DTSTART", period.get_start()) |
paul@627 | 394 | result = self.set_datetime("DTEND", period.get_end()) or result |
paul@661 | 395 | if self.has_key("DURATION"): |
paul@661 | 396 | del self["DURATION"] |
paul@661 | 397 | |
paul@627 | 398 | return result |
paul@627 | 399 | |
paul@627 | 400 | def set_periods(self, periods): |
paul@627 | 401 | |
paul@627 | 402 | """ |
paul@627 | 403 | Set the given 'periods' as recurrence date properties, replacing the |
paul@627 | 404 | previous RDATE properties and ignoring any RRULE properties. |
paul@627 | 405 | """ |
paul@627 | 406 | |
paul@753 | 407 | old_values = set(self.get_date_values("RDATE") or []) |
paul@627 | 408 | new_rdates = [] |
paul@627 | 409 | |
paul@627 | 410 | if self.has_key("RDATE"): |
paul@627 | 411 | del self["RDATE"] |
paul@627 | 412 | |
paul@627 | 413 | for p in periods: |
paul@627 | 414 | if p.origin != "RRULE": |
paul@627 | 415 | new_rdates.append(get_period_item(p.get_start(), p.get_end())) |
paul@627 | 416 | |
paul@661 | 417 | if new_rdates: |
paul@661 | 418 | self["RDATE"] = new_rdates |
paul@661 | 419 | |
paul@753 | 420 | return old_values != set(self.get_date_values("RDATE") or []) |
paul@661 | 421 | |
paul@784 | 422 | def update_exceptions(self, excluded): |
paul@784 | 423 | |
paul@784 | 424 | """ |
paul@784 | 425 | Update the exceptions to any rule by applying the list of 'excluded' |
paul@784 | 426 | periods. |
paul@784 | 427 | """ |
paul@784 | 428 | |
paul@784 | 429 | to_exclude = set(excluded).difference(self.get_date_values("EXDATE") or []) |
paul@784 | 430 | if not to_exclude: |
paul@784 | 431 | return False |
paul@784 | 432 | |
paul@784 | 433 | if not self.has_key("EXDATE"): |
paul@784 | 434 | self["EXDATE"] = [] |
paul@784 | 435 | |
paul@784 | 436 | for p in to_exclude: |
paul@784 | 437 | self["EXDATE"].append(get_period_item(p.get_start(), p.get_end())) |
paul@784 | 438 | |
paul@784 | 439 | return True |
paul@784 | 440 | |
paul@669 | 441 | def correct_object(self, tzid, permitted_values): |
paul@661 | 442 | |
paul@661 | 443 | "Correct the object's period details." |
paul@661 | 444 | |
paul@661 | 445 | corrected = set() |
paul@661 | 446 | rdates = [] |
paul@661 | 447 | |
paul@661 | 448 | for period in self.get_periods(tzid): |
paul@661 | 449 | start = period.get_start() |
paul@661 | 450 | end = period.get_end() |
paul@669 | 451 | start_errors = check_permitted_values(start, permitted_values) |
paul@669 | 452 | end_errors = check_permitted_values(end, permitted_values) |
paul@627 | 453 | |
paul@661 | 454 | if not (start_errors or end_errors): |
paul@661 | 455 | if period.origin == "RDATE": |
paul@661 | 456 | rdates.append(period) |
paul@661 | 457 | continue |
paul@661 | 458 | |
paul@661 | 459 | if start_errors: |
paul@669 | 460 | start = correct_datetime(start, permitted_values) |
paul@661 | 461 | if end_errors: |
paul@669 | 462 | end = correct_datetime(end, permitted_values) |
paul@661 | 463 | period = RecurringPeriod(start, end, period.tzid, period.origin, period.get_start_attr(), period.get_end_attr()) |
paul@661 | 464 | |
paul@661 | 465 | if period.origin == "DTSTART": |
paul@661 | 466 | self.set_period(period) |
paul@661 | 467 | corrected.add("DTSTART") |
paul@661 | 468 | elif period.origin == "RDATE": |
paul@661 | 469 | rdates.append(period) |
paul@661 | 470 | corrected.add("RDATE") |
paul@661 | 471 | |
paul@661 | 472 | if "RDATE" in corrected: |
paul@661 | 473 | self.set_periods(rdates) |
paul@661 | 474 | |
paul@661 | 475 | return corrected |
paul@627 | 476 | |
paul@213 | 477 | # Construction and serialisation. |
paul@213 | 478 | |
paul@213 | 479 | def make_calendar(nodes, method=None): |
paul@213 | 480 | |
paul@213 | 481 | """ |
paul@213 | 482 | Return a complete calendar node wrapping the given 'nodes' and employing the |
paul@213 | 483 | given 'method', if indicated. |
paul@213 | 484 | """ |
paul@213 | 485 | |
paul@213 | 486 | return ("VCALENDAR", {}, |
paul@213 | 487 | (method and [("METHOD", {}, method)] or []) + |
paul@213 | 488 | [("VERSION", {}, "2.0")] + |
paul@213 | 489 | nodes |
paul@213 | 490 | ) |
paul@213 | 491 | |
paul@327 | 492 | def make_freebusy(freebusy, uid, organiser, organiser_attr=None, attendee=None, |
paul@562 | 493 | attendee_attr=None, period=None): |
paul@222 | 494 | |
paul@222 | 495 | """ |
paul@222 | 496 | Return a calendar node defining the free/busy details described in the given |
paul@292 | 497 | 'freebusy' list, employing the given 'uid', for the given 'organiser' and |
paul@292 | 498 | optional 'organiser_attr', with the optional 'attendee' providing recipient |
paul@292 | 499 | details together with the optional 'attendee_attr'. |
paul@327 | 500 | |
paul@562 | 501 | The result will be constrained to the 'period' if specified. |
paul@222 | 502 | """ |
paul@222 | 503 | |
paul@222 | 504 | record = [] |
paul@222 | 505 | rwrite = record.append |
paul@222 | 506 | |
paul@292 | 507 | rwrite(("ORGANIZER", organiser_attr or {}, organiser)) |
paul@222 | 508 | |
paul@222 | 509 | if attendee: |
paul@292 | 510 | rwrite(("ATTENDEE", attendee_attr or {}, attendee)) |
paul@222 | 511 | |
paul@222 | 512 | rwrite(("UID", {}, uid)) |
paul@222 | 513 | |
paul@222 | 514 | if freebusy: |
paul@327 | 515 | |
paul@327 | 516 | # Get a constrained view if start and end limits are specified. |
paul@327 | 517 | |
paul@563 | 518 | if period: |
paul@563 | 519 | periods = period_overlaps(freebusy, period, True) |
paul@563 | 520 | else: |
paul@563 | 521 | periods = freebusy |
paul@327 | 522 | |
paul@327 | 523 | # Write the limits of the resource. |
paul@327 | 524 | |
paul@563 | 525 | if periods: |
paul@563 | 526 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(periods[0].get_start_point()))) |
paul@563 | 527 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(periods[-1].get_end_point()))) |
paul@563 | 528 | else: |
paul@563 | 529 | rwrite(("DTSTART", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_start_point()))) |
paul@563 | 530 | rwrite(("DTEND", {"VALUE" : "DATE-TIME"}, format_datetime(period.get_end_point()))) |
paul@327 | 531 | |
paul@458 | 532 | for p in periods: |
paul@458 | 533 | if p.transp == "OPAQUE": |
paul@529 | 534 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@562 | 535 | map(format_datetime, [p.get_start_point(), p.get_end_point()]) |
paul@529 | 536 | ))) |
paul@222 | 537 | |
paul@222 | 538 | return ("VFREEBUSY", {}, record) |
paul@222 | 539 | |
paul@213 | 540 | def parse_object(f, encoding, objtype=None): |
paul@213 | 541 | |
paul@213 | 542 | """ |
paul@213 | 543 | Parse the iTIP content from 'f' having the given 'encoding'. If 'objtype' is |
paul@213 | 544 | given, only objects of that type will be returned. Otherwise, the root of |
paul@213 | 545 | the content will be returned as a dictionary with a single key indicating |
paul@213 | 546 | the object type. |
paul@213 | 547 | |
paul@213 | 548 | Return None if the content was not readable or suitable. |
paul@213 | 549 | """ |
paul@213 | 550 | |
paul@213 | 551 | try: |
paul@213 | 552 | try: |
paul@213 | 553 | doctype, attrs, elements = obj = parse(f, encoding=encoding) |
paul@213 | 554 | if objtype and doctype == objtype: |
paul@213 | 555 | return to_dict(obj)[objtype][0] |
paul@213 | 556 | elif not objtype: |
paul@213 | 557 | return to_dict(obj) |
paul@213 | 558 | finally: |
paul@213 | 559 | f.close() |
paul@213 | 560 | |
paul@213 | 561 | # NOTE: Handle parse errors properly. |
paul@213 | 562 | |
paul@213 | 563 | except (ParseError, ValueError): |
paul@213 | 564 | pass |
paul@213 | 565 | |
paul@213 | 566 | return None |
paul@213 | 567 | |
paul@213 | 568 | def to_part(method, calendar): |
paul@213 | 569 | |
paul@213 | 570 | """ |
paul@213 | 571 | Write using the given 'method', the 'calendar' details to a MIME |
paul@213 | 572 | text/calendar part. |
paul@213 | 573 | """ |
paul@213 | 574 | |
paul@213 | 575 | encoding = "utf-8" |
paul@213 | 576 | out = StringIO() |
paul@213 | 577 | try: |
paul@213 | 578 | to_stream(out, make_calendar(calendar, method), encoding) |
paul@213 | 579 | part = MIMEText(out.getvalue(), "calendar", encoding) |
paul@213 | 580 | part.set_param("method", method) |
paul@213 | 581 | return part |
paul@213 | 582 | |
paul@213 | 583 | finally: |
paul@213 | 584 | out.close() |
paul@213 | 585 | |
paul@213 | 586 | def to_stream(out, fragment, encoding="utf-8"): |
paul@213 | 587 | iterwrite(out, encoding=encoding).append(fragment) |
paul@213 | 588 | |
paul@213 | 589 | # Structure access functions. |
paul@213 | 590 | |
paul@213 | 591 | def get_items(d, name, all=True): |
paul@213 | 592 | |
paul@213 | 593 | """ |
paul@213 | 594 | Get all items from 'd' for the given 'name', returning single items if |
paul@213 | 595 | 'all' is specified and set to a false value and if only one value is |
paul@213 | 596 | present for the name. Return None if no items are found for the name or if |
paul@213 | 597 | many items are found but 'all' is set to a false value. |
paul@213 | 598 | """ |
paul@213 | 599 | |
paul@213 | 600 | if d.has_key(name): |
paul@712 | 601 | items = [(value or None, attr) for value, attr in d[name]] |
paul@213 | 602 | if all: |
paul@462 | 603 | return items |
paul@462 | 604 | elif len(items) == 1: |
paul@462 | 605 | return items[0] |
paul@213 | 606 | else: |
paul@213 | 607 | return None |
paul@213 | 608 | else: |
paul@213 | 609 | return None |
paul@213 | 610 | |
paul@213 | 611 | def get_item(d, name): |
paul@213 | 612 | return get_items(d, name, False) |
paul@213 | 613 | |
paul@213 | 614 | def get_value_map(d, name): |
paul@213 | 615 | |
paul@213 | 616 | """ |
paul@213 | 617 | Return a dictionary for all items in 'd' having the given 'name'. The |
paul@213 | 618 | dictionary will map values for the name to any attributes or qualifiers |
paul@213 | 619 | that may have been present. |
paul@213 | 620 | """ |
paul@213 | 621 | |
paul@213 | 622 | items = get_items(d, name) |
paul@213 | 623 | if items: |
paul@213 | 624 | return dict(items) |
paul@213 | 625 | else: |
paul@213 | 626 | return {} |
paul@213 | 627 | |
paul@462 | 628 | def values_from_items(items): |
paul@462 | 629 | return map(lambda x: x[0], items) |
paul@462 | 630 | |
paul@213 | 631 | def get_values(d, name, all=True): |
paul@213 | 632 | if d.has_key(name): |
paul@462 | 633 | items = d[name] |
paul@462 | 634 | if not all and len(items) == 1: |
paul@462 | 635 | return items[0][0] |
paul@213 | 636 | else: |
paul@462 | 637 | return values_from_items(items) |
paul@213 | 638 | else: |
paul@213 | 639 | return None |
paul@213 | 640 | |
paul@213 | 641 | def get_value(d, name): |
paul@213 | 642 | return get_values(d, name, False) |
paul@213 | 643 | |
paul@417 | 644 | def get_date_value_items(d, name, tzid=None): |
paul@352 | 645 | |
paul@352 | 646 | """ |
paul@389 | 647 | Obtain items from 'd' having the given 'name', where a single item yields |
paul@389 | 648 | potentially many values. Return a list of tuples of the form (value, |
paul@389 | 649 | attributes) where the attributes have been given for the property in 'd'. |
paul@352 | 650 | """ |
paul@352 | 651 | |
paul@403 | 652 | items = get_items(d, name) |
paul@403 | 653 | if items: |
paul@403 | 654 | all_items = [] |
paul@403 | 655 | for item in items: |
paul@403 | 656 | values, attr = item |
paul@417 | 657 | if not attr.has_key("TZID") and tzid: |
paul@417 | 658 | attr["TZID"] = tzid |
paul@403 | 659 | if not isinstance(values, list): |
paul@403 | 660 | values = [values] |
paul@403 | 661 | for value in values: |
paul@403 | 662 | all_items.append((get_datetime(value, attr) or get_period(value, attr), attr)) |
paul@403 | 663 | return all_items |
paul@352 | 664 | else: |
paul@352 | 665 | return None |
paul@352 | 666 | |
paul@646 | 667 | def get_period_values(d, name, tzid=None): |
paul@630 | 668 | |
paul@630 | 669 | """ |
paul@630 | 670 | Return period values from 'd' for the given property 'name', using 'tzid' |
paul@646 | 671 | where specified to indicate the time zone. |
paul@630 | 672 | """ |
paul@630 | 673 | |
paul@630 | 674 | values = [] |
paul@630 | 675 | for value, attr in get_items(d, name) or []: |
paul@630 | 676 | if not attr.has_key("TZID") and tzid: |
paul@630 | 677 | attr["TZID"] = tzid |
paul@630 | 678 | start, end = get_period(value, attr) |
paul@646 | 679 | values.append(Period(start, end, tzid=tzid)) |
paul@630 | 680 | return values |
paul@630 | 681 | |
paul@506 | 682 | def get_utc_datetime(d, name, date_tzid=None): |
paul@506 | 683 | |
paul@506 | 684 | """ |
paul@506 | 685 | Return the value provided by 'd' for 'name' as a datetime in the UTC zone |
paul@506 | 686 | or as a date, converting any date to a datetime if 'date_tzid' is specified. |
paul@720 | 687 | If no datetime or date is available, None is returned. |
paul@506 | 688 | """ |
paul@506 | 689 | |
paul@348 | 690 | t = get_datetime_item(d, name) |
paul@348 | 691 | if not t: |
paul@348 | 692 | return None |
paul@348 | 693 | else: |
paul@348 | 694 | dt, attr = t |
paul@720 | 695 | return dt is not None and to_utc_datetime(dt, date_tzid) or None |
paul@289 | 696 | |
paul@289 | 697 | def get_datetime_item(d, name): |
paul@562 | 698 | |
paul@562 | 699 | """ |
paul@562 | 700 | Return the value provided by 'd' for 'name' as a datetime or as a date, |
paul@562 | 701 | together with the attributes describing it. Return None if no value exists |
paul@562 | 702 | for 'name' in 'd'. |
paul@562 | 703 | """ |
paul@562 | 704 | |
paul@348 | 705 | t = get_item(d, name) |
paul@348 | 706 | if not t: |
paul@348 | 707 | return None |
paul@348 | 708 | else: |
paul@348 | 709 | value, attr = t |
paul@613 | 710 | dt = get_datetime(value, attr) |
paul@616 | 711 | tzid = get_datetime_tzid(dt) |
paul@616 | 712 | if tzid: |
paul@616 | 713 | attr["TZID"] = tzid |
paul@613 | 714 | return dt, attr |
paul@213 | 715 | |
paul@528 | 716 | # Conversion functions. |
paul@528 | 717 | |
paul@792 | 718 | def get_address_parts(values): |
paul@792 | 719 | |
paul@792 | 720 | "Return name and address tuples for each of the given 'values'." |
paul@792 | 721 | |
paul@792 | 722 | l = [] |
paul@792 | 723 | for name, address in values and email.utils.getaddresses(values) or []: |
paul@792 | 724 | if is_mailto_uri(name): |
paul@792 | 725 | name = name[7:] # strip "mailto:" |
paul@792 | 726 | l.append((name, address)) |
paul@792 | 727 | return l |
paul@792 | 728 | |
paul@213 | 729 | def get_addresses(values): |
paul@790 | 730 | |
paul@790 | 731 | """ |
paul@790 | 732 | Return only addresses from the given 'values' which may be of the form |
paul@790 | 733 | "Common Name <recipient@domain>", with the latter part being the address |
paul@790 | 734 | itself. |
paul@790 | 735 | """ |
paul@790 | 736 | |
paul@792 | 737 | return [address for name, address in get_address_parts(values)] |
paul@213 | 738 | |
paul@213 | 739 | def get_address(value): |
paul@790 | 740 | |
paul@790 | 741 | "Return an e-mail address from the given 'value'." |
paul@790 | 742 | |
paul@712 | 743 | if not value: return None |
paul@792 | 744 | return get_addresses([value])[0] |
paul@792 | 745 | |
paul@792 | 746 | def get_verbose_address(value, attr=None): |
paul@792 | 747 | |
paul@792 | 748 | """ |
paul@792 | 749 | Return a verbose e-mail address featuring any name from the given 'value' |
paul@792 | 750 | and any accompanying 'attr' dictionary. |
paul@792 | 751 | """ |
paul@792 | 752 | |
paul@792 | 753 | name, address = get_address_parts([value])[0] |
paul@792 | 754 | if not name: |
paul@792 | 755 | name = attr and attr.get("CN") |
paul@792 | 756 | if name and address: |
paul@792 | 757 | return "%s <%s>" % (name, address) |
paul@792 | 758 | else: |
paul@792 | 759 | return address |
paul@792 | 760 | |
paul@792 | 761 | def is_mailto_uri(value): |
paul@792 | 762 | return value.lower().startswith("mailto:") |
paul@213 | 763 | |
paul@213 | 764 | def get_uri(value): |
paul@790 | 765 | |
paul@790 | 766 | "Return a URI for the given 'value'." |
paul@790 | 767 | |
paul@712 | 768 | if not value: return None |
paul@792 | 769 | return is_mailto_uri(value) and ("mailto:%s" % value[7:]) or \ |
paul@712 | 770 | ":" in value and value or \ |
paul@790 | 771 | "mailto:%s" % get_address(value) |
paul@213 | 772 | |
paul@792 | 773 | def uri_parts(values): |
paul@792 | 774 | |
paul@792 | 775 | "Return any common name plus the URI for each of the given 'values'." |
paul@792 | 776 | |
paul@792 | 777 | return [(name, get_uri(address)) for name, address in get_address_parts(values)] |
paul@792 | 778 | |
paul@309 | 779 | uri_value = get_uri |
paul@309 | 780 | |
paul@309 | 781 | def uri_values(values): |
paul@309 | 782 | return map(get_uri, values) |
paul@309 | 783 | |
paul@213 | 784 | def uri_dict(d): |
paul@213 | 785 | return dict([(get_uri(key), value) for key, value in d.items()]) |
paul@213 | 786 | |
paul@213 | 787 | def uri_item(item): |
paul@213 | 788 | return get_uri(item[0]), item[1] |
paul@213 | 789 | |
paul@213 | 790 | def uri_items(items): |
paul@213 | 791 | return [(get_uri(value), attr) for value, attr in items] |
paul@213 | 792 | |
paul@220 | 793 | # Operations on structure data. |
paul@220 | 794 | |
paul@682 | 795 | def is_new_object(old_sequence, new_sequence, old_dtstamp, new_dtstamp, ignore_dtstamp): |
paul@220 | 796 | |
paul@220 | 797 | """ |
paul@220 | 798 | Return for the given 'old_sequence' and 'new_sequence', 'old_dtstamp' and |
paul@682 | 799 | 'new_dtstamp', and the 'ignore_dtstamp' indication, whether the object |
paul@220 | 800 | providing the new information is really newer than the object providing the |
paul@220 | 801 | old information. |
paul@220 | 802 | """ |
paul@220 | 803 | |
paul@220 | 804 | have_sequence = old_sequence is not None and new_sequence is not None |
paul@220 | 805 | is_same_sequence = have_sequence and int(new_sequence) == int(old_sequence) |
paul@220 | 806 | |
paul@220 | 807 | have_dtstamp = old_dtstamp and new_dtstamp |
paul@220 | 808 | is_old_dtstamp = have_dtstamp and new_dtstamp < old_dtstamp or old_dtstamp and not new_dtstamp |
paul@220 | 809 | |
paul@220 | 810 | is_old_sequence = have_sequence and ( |
paul@220 | 811 | int(new_sequence) < int(old_sequence) or |
paul@220 | 812 | is_same_sequence and is_old_dtstamp |
paul@220 | 813 | ) |
paul@220 | 814 | |
paul@682 | 815 | return is_same_sequence and ignore_dtstamp or not is_old_sequence |
paul@220 | 816 | |
paul@630 | 817 | def get_periods(obj, tzid, end=None, inclusive=False): |
paul@256 | 818 | |
paul@256 | 819 | """ |
paul@618 | 820 | Return periods for the given object 'obj', employing the given 'tzid' where |
paul@618 | 821 | no time zone information is available (for whole day events, for example), |
paul@630 | 822 | confining materialised periods to before the given 'end' datetime. |
paul@618 | 823 | |
paul@630 | 824 | If 'end' is omitted, only explicit recurrences and recurrences from |
paul@630 | 825 | explicitly-terminated rules will be returned. |
paul@630 | 826 | |
paul@630 | 827 | If 'inclusive' is set to a true value, any period occurring at the 'end' |
paul@630 | 828 | will be included. |
paul@256 | 829 | """ |
paul@256 | 830 | |
paul@318 | 831 | rrule = obj.get_value("RRULE") |
paul@636 | 832 | parameters = rrule and get_parameters(rrule) |
paul@318 | 833 | |
paul@318 | 834 | # Use localised datetimes. |
paul@318 | 835 | |
paul@797 | 836 | main_period = obj.get_main_period(tzid) |
paul@797 | 837 | |
paul@797 | 838 | dtstart = main_period.get_start() |
paul@797 | 839 | dtstart_attr = main_period.get_start_attr() |
paul@797 | 840 | dtend = main_period.get_end() |
paul@797 | 841 | dtend_attr = main_period.get_end_attr() |
paul@797 | 842 | |
paul@650 | 843 | duration = dtend - dtstart |
paul@256 | 844 | |
paul@618 | 845 | # Attempt to get time zone details from the object, using the supplied zone |
paul@618 | 846 | # only as a fallback. |
paul@618 | 847 | |
paul@638 | 848 | obj_tzid = obj.get_tzid() |
paul@256 | 849 | |
paul@352 | 850 | if not rrule: |
paul@797 | 851 | periods = [main_period] |
paul@630 | 852 | |
paul@636 | 853 | elif end or parameters and parameters.has_key("UNTIL") or parameters.has_key("COUNT"): |
paul@630 | 854 | |
paul@352 | 855 | # Recurrence rules create multiple instances to be checked. |
paul@352 | 856 | # Conflicts may only be assessed within a period defined by policy |
paul@352 | 857 | # for the agent, with instances outside that period being considered |
paul@352 | 858 | # unchecked. |
paul@352 | 859 | |
paul@352 | 860 | selector = get_rule(dtstart, rrule) |
paul@352 | 861 | periods = [] |
paul@352 | 862 | |
paul@521 | 863 | until = parameters.get("UNTIL") |
paul@521 | 864 | if until: |
paul@650 | 865 | until_dt = to_timezone(get_datetime(until, dtstart_attr), obj_tzid) |
paul@650 | 866 | end = end and min(until_dt, end) or until_dt |
paul@521 | 867 | inclusive = True |
paul@521 | 868 | |
paul@630 | 869 | for recurrence_start in selector.materialise(dtstart, end, parameters.get("COUNT"), parameters.get("BYSETPOS"), inclusive): |
paul@630 | 870 | create = len(recurrence_start) == 3 and date or datetime |
paul@638 | 871 | recurrence_start = to_timezone(create(*recurrence_start), obj_tzid) |
paul@630 | 872 | recurrence_end = recurrence_start + duration |
paul@638 | 873 | periods.append(RecurringPeriod(recurrence_start, recurrence_end, tzid, "RRULE", dtstart_attr)) |
paul@352 | 874 | |
paul@635 | 875 | else: |
paul@635 | 876 | periods = [] |
paul@635 | 877 | |
paul@352 | 878 | # Add recurrence dates. |
paul@256 | 879 | |
paul@494 | 880 | rdates = obj.get_date_value_items("RDATE", tzid) |
paul@352 | 881 | |
paul@352 | 882 | if rdates: |
paul@494 | 883 | for rdate, rdate_attr in rdates: |
paul@389 | 884 | if isinstance(rdate, tuple): |
paul@541 | 885 | periods.append(RecurringPeriod(rdate[0], rdate[1], tzid, "RDATE", rdate_attr)) |
paul@389 | 886 | else: |
paul@541 | 887 | periods.append(RecurringPeriod(rdate, rdate + duration, tzid, "RDATE", rdate_attr)) |
paul@424 | 888 | |
paul@424 | 889 | # Return a sorted list of the periods. |
paul@424 | 890 | |
paul@542 | 891 | periods.sort() |
paul@352 | 892 | |
paul@352 | 893 | # Exclude exception dates. |
paul@352 | 894 | |
paul@638 | 895 | exdates = obj.get_date_value_items("EXDATE", tzid) |
paul@256 | 896 | |
paul@352 | 897 | if exdates: |
paul@638 | 898 | for exdate, exdate_attr in exdates: |
paul@389 | 899 | if isinstance(exdate, tuple): |
paul@638 | 900 | period = RecurringPeriod(exdate[0], exdate[1], tzid, "EXDATE", exdate_attr) |
paul@389 | 901 | else: |
paul@638 | 902 | period = RecurringPeriod(exdate, exdate + duration, tzid, "EXDATE", exdate_attr) |
paul@424 | 903 | i = bisect_left(periods, period) |
paul@458 | 904 | while i < len(periods) and periods[i] == period: |
paul@424 | 905 | del periods[i] |
paul@256 | 906 | |
paul@256 | 907 | return periods |
paul@256 | 908 | |
paul@606 | 909 | def get_sender_identities(mapping): |
paul@606 | 910 | |
paul@606 | 911 | """ |
paul@606 | 912 | Return a mapping from actual senders to the identities for which they |
paul@606 | 913 | have provided data, extracting this information from the given |
paul@606 | 914 | 'mapping'. |
paul@606 | 915 | """ |
paul@606 | 916 | |
paul@606 | 917 | senders = {} |
paul@606 | 918 | |
paul@606 | 919 | for value, attr in mapping.items(): |
paul@606 | 920 | sent_by = attr.get("SENT-BY") |
paul@606 | 921 | if sent_by: |
paul@606 | 922 | sender = get_uri(sent_by) |
paul@606 | 923 | else: |
paul@606 | 924 | sender = value |
paul@606 | 925 | |
paul@606 | 926 | if not senders.has_key(sender): |
paul@606 | 927 | senders[sender] = [] |
paul@606 | 928 | |
paul@606 | 929 | senders[sender].append(value) |
paul@606 | 930 | |
paul@606 | 931 | return senders |
paul@606 | 932 | |
paul@618 | 933 | def get_window_end(tzid, days=100): |
paul@606 | 934 | |
paul@618 | 935 | """ |
paul@618 | 936 | Return a datetime in the time zone indicated by 'tzid' marking the end of a |
paul@618 | 937 | window of the given number of 'days'. |
paul@618 | 938 | """ |
paul@618 | 939 | |
paul@618 | 940 | return to_timezone(datetime.now(), tzid) + timedelta(days) |
paul@606 | 941 | |
paul@213 | 942 | # vim: tabstop=4 expandtab shiftwidth=4 |