paul@441 | 1 | #!/usr/bin/env python |
paul@441 | 2 | |
paul@441 | 3 | """ |
paul@441 | 4 | Common calendar client utilities. |
paul@441 | 5 | |
paul@441 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@441 | 7 | |
paul@441 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@441 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@441 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@441 | 11 | version. |
paul@441 | 12 | |
paul@441 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@441 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@441 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@441 | 16 | details. |
paul@441 | 17 | |
paul@441 | 18 | You should have received a copy of the GNU General Public License along with |
paul@441 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@441 | 20 | """ |
paul@441 | 21 | |
paul@601 | 22 | from datetime import datetime |
paul@581 | 23 | from imiptools.data import get_address, get_uri, get_window_end, uri_dict, uri_items, uri_values |
paul@601 | 24 | from imiptools.period import update_freebusy |
paul@443 | 25 | from imiptools.profile import Preferences |
paul@601 | 26 | from imiptools.dates import format_datetime, get_default_timezone, \ |
paul@601 | 27 | to_timezone |
paul@441 | 28 | |
paul@473 | 29 | def update_attendees(obj, attendees, removed): |
paul@441 | 30 | |
paul@441 | 31 | """ |
paul@473 | 32 | Update the attendees in 'obj' with the given 'attendees' and 'removed' |
paul@441 | 33 | attendee lists. A list is returned containing the attendees whose |
paul@441 | 34 | attendance should be cancelled. |
paul@441 | 35 | """ |
paul@441 | 36 | |
paul@441 | 37 | to_cancel = [] |
paul@441 | 38 | |
paul@473 | 39 | existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) |
paul@473 | 40 | added = set(attendees).difference(existing_attendees) |
paul@473 | 41 | |
paul@441 | 42 | if added or removed: |
paul@441 | 43 | attendees = uri_items(obj.get_items("ATTENDEE") or []) |
paul@460 | 44 | sequence = obj.get_value("SEQUENCE") |
paul@441 | 45 | |
paul@441 | 46 | if removed: |
paul@441 | 47 | remaining = [] |
paul@441 | 48 | |
paul@441 | 49 | for attendee, attendee_attr in attendees: |
paul@441 | 50 | if attendee in removed: |
paul@460 | 51 | |
paul@460 | 52 | # Without a sequence number, assume that the event has not |
paul@460 | 53 | # been published and that attendees can be silently removed. |
paul@460 | 54 | |
paul@460 | 55 | if sequence is not None: |
paul@441 | 56 | to_cancel.append((attendee, attendee_attr)) |
paul@441 | 57 | else: |
paul@441 | 58 | remaining.append((attendee, attendee_attr)) |
paul@441 | 59 | |
paul@441 | 60 | attendees = remaining |
paul@441 | 61 | |
paul@441 | 62 | if added: |
paul@441 | 63 | for attendee in added: |
paul@473 | 64 | attendee = attendee.strip() |
paul@473 | 65 | if attendee: |
paul@473 | 66 | attendees.append((get_uri(attendee), {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) |
paul@441 | 67 | |
paul@441 | 68 | obj["ATTENDEE"] = attendees |
paul@441 | 69 | |
paul@441 | 70 | return to_cancel |
paul@441 | 71 | |
paul@443 | 72 | class Client: |
paul@443 | 73 | |
paul@443 | 74 | "Common handler and manager methods." |
paul@443 | 75 | |
paul@467 | 76 | default_window_size = 100 |
paul@467 | 77 | |
paul@601 | 78 | def __init__(self, user, messenger=None): |
paul@443 | 79 | self.user = user |
paul@601 | 80 | self.messenger = messenger |
paul@443 | 81 | self.preferences = None |
paul@443 | 82 | |
paul@443 | 83 | def get_preferences(self): |
paul@467 | 84 | if not self.preferences and self.user: |
paul@443 | 85 | self.preferences = Preferences(self.user) |
paul@443 | 86 | return self.preferences |
paul@443 | 87 | |
paul@443 | 88 | def get_tzid(self): |
paul@443 | 89 | prefs = self.get_preferences() |
paul@467 | 90 | return prefs and prefs.get("TZID") or get_default_timezone() |
paul@443 | 91 | |
paul@443 | 92 | def get_window_size(self): |
paul@443 | 93 | prefs = self.get_preferences() |
paul@443 | 94 | try: |
paul@467 | 95 | return prefs and int(prefs.get("window_size")) or self.default_window_size |
paul@443 | 96 | except (TypeError, ValueError): |
paul@467 | 97 | return self.default_window_size |
paul@443 | 98 | |
paul@443 | 99 | def get_window_end(self): |
paul@443 | 100 | return get_window_end(self.get_tzid(), self.get_window_size()) |
paul@443 | 101 | |
paul@443 | 102 | def is_sharing(self): |
paul@467 | 103 | prefs = self.get_preferences() |
paul@467 | 104 | return prefs and prefs.get("freebusy_sharing") == "share" or False |
paul@443 | 105 | |
paul@443 | 106 | def is_bundling(self): |
paul@467 | 107 | prefs = self.get_preferences() |
paul@467 | 108 | return prefs and prefs.get("freebusy_bundling") == "always" or False |
paul@467 | 109 | |
paul@467 | 110 | def is_notifying(self): |
paul@467 | 111 | prefs = self.get_preferences() |
paul@467 | 112 | return prefs and prefs.get("freebusy_messages") == "notify" or False |
paul@443 | 113 | |
paul@581 | 114 | # Common operations on calendar data. |
paul@581 | 115 | |
paul@582 | 116 | def update_participation(self, obj, partstat=None): |
paul@581 | 117 | |
paul@581 | 118 | """ |
paul@581 | 119 | Update the participation in 'obj' of the user with the given 'partstat'. |
paul@581 | 120 | """ |
paul@581 | 121 | |
paul@581 | 122 | attendee_attr = uri_dict(obj.get_value_map("ATTENDEE")).get(self.user) |
paul@582 | 123 | if not attendee_attr: |
paul@582 | 124 | return None |
paul@582 | 125 | if partstat: |
paul@582 | 126 | attendee_attr["PARTSTAT"] = partstat |
paul@581 | 127 | if attendee_attr.has_key("RSVP"): |
paul@581 | 128 | del attendee_attr["RSVP"] |
paul@584 | 129 | self.update_sender(attendee_attr) |
paul@581 | 130 | return attendee_attr |
paul@581 | 131 | |
paul@584 | 132 | def update_sender(self, attr): |
paul@584 | 133 | |
paul@584 | 134 | "Update the SENT-BY attribute of the 'attr' sender metadata." |
paul@584 | 135 | |
paul@584 | 136 | if self.messenger and self.messenger.sender != get_address(self.user): |
paul@584 | 137 | attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@584 | 138 | |
paul@601 | 139 | class ClientForObject(Client): |
paul@601 | 140 | |
paul@601 | 141 | "A client maintaining a specific object." |
paul@601 | 142 | |
paul@601 | 143 | def __init__(self, obj, user, messenger=None): |
paul@601 | 144 | Client.__init__(self, user, messenger) |
paul@601 | 145 | self.set_object(obj) |
paul@601 | 146 | |
paul@601 | 147 | def set_object(self, obj): |
paul@601 | 148 | self.obj = obj |
paul@601 | 149 | self.uid = obj and self.obj.get_uid() |
paul@601 | 150 | self.recurrenceid = obj and self.obj.get_recurrenceid() |
paul@601 | 151 | self.sequence = obj and self.obj.get_value("SEQUENCE") |
paul@601 | 152 | self.dtstamp = obj and self.obj.get_value("DTSTAMP") |
paul@601 | 153 | |
paul@601 | 154 | def _update_freebusy(self, freebusy, periods, recurrenceid, transp=None): |
paul@601 | 155 | |
paul@601 | 156 | """ |
paul@601 | 157 | Update the 'freebusy' collection with the given 'periods', indicating an |
paul@601 | 158 | explicit 'recurrenceid' to affect either a recurrence or the parent |
paul@601 | 159 | event. |
paul@601 | 160 | """ |
paul@601 | 161 | |
paul@601 | 162 | update_freebusy(freebusy, periods, |
paul@601 | 163 | transp or self.obj.get_value("TRANSP") or "OPAQUE", |
paul@601 | 164 | self.uid, recurrenceid, |
paul@601 | 165 | self.obj.get_value("SUMMARY"), |
paul@601 | 166 | self.obj.get_value("ORGANIZER")) |
paul@601 | 167 | |
paul@601 | 168 | def update_freebusy(self, freebusy, periods, transp=None): |
paul@601 | 169 | |
paul@601 | 170 | """ |
paul@601 | 171 | Update the 'freebusy' collection for this event with the given |
paul@601 | 172 | 'periods'. |
paul@601 | 173 | """ |
paul@601 | 174 | |
paul@601 | 175 | self._update_freebusy(freebusy, periods, self.recurrenceid, transp) |
paul@601 | 176 | |
paul@601 | 177 | def update_freebusy_for_participant(self, freebusy, periods, attr, for_organiser=False): |
paul@601 | 178 | |
paul@601 | 179 | """ |
paul@601 | 180 | Update the 'freebusy' collection using the given 'periods', subject to |
paul@601 | 181 | the 'attr' provided for the participant, indicating whether this is |
paul@601 | 182 | being generated 'for_organiser' or not. |
paul@601 | 183 | """ |
paul@601 | 184 | |
paul@601 | 185 | # Organisers employ a special transparency if not attending. |
paul@601 | 186 | |
paul@601 | 187 | if self.is_participating(attr, for_organiser): |
paul@601 | 188 | self.update_freebusy(freebusy, periods, |
paul@601 | 189 | transp=self.get_overriding_transparency(attr, for_organiser)) |
paul@601 | 190 | else: |
paul@601 | 191 | self.remove_from_freebusy(freebusy) |
paul@601 | 192 | |
paul@601 | 193 | def is_participating(self, attr, as_organiser=False): |
paul@601 | 194 | return as_organiser or not attr or attr.get("PARTSTAT") != "DECLINED" |
paul@601 | 195 | |
paul@601 | 196 | def get_overriding_transparency(self, attr, as_organiser=False): |
paul@601 | 197 | return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None |
paul@601 | 198 | |
paul@601 | 199 | def update_dtstamp(self): |
paul@601 | 200 | |
paul@601 | 201 | "Update the DTSTAMP in the current object." |
paul@601 | 202 | |
paul@601 | 203 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@601 | 204 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@601 | 205 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@601 | 206 | |
paul@601 | 207 | def set_sequence(self, increment=False): |
paul@601 | 208 | |
paul@601 | 209 | "Update the SEQUENCE in the current object." |
paul@601 | 210 | |
paul@601 | 211 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@601 | 212 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@601 | 213 | |
paul@441 | 214 | # vim: tabstop=4 expandtab shiftwidth=4 |