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@604 | 23 | from imiptools.data import get_address, get_uri, get_window_end, \ |
paul@604 | 24 | make_freebusy, to_part, \ |
paul@604 | 25 | uri_dict, uri_items, uri_values |
paul@604 | 26 | from imiptools.dates import format_datetime, get_default_timezone, \ |
paul@604 | 27 | get_timestamp, to_timezone |
paul@601 | 28 | from imiptools.period import update_freebusy |
paul@443 | 29 | from imiptools.profile import Preferences |
paul@604 | 30 | import imip_store |
paul@441 | 31 | |
paul@473 | 32 | def update_attendees(obj, attendees, removed): |
paul@441 | 33 | |
paul@441 | 34 | """ |
paul@473 | 35 | Update the attendees in 'obj' with the given 'attendees' and 'removed' |
paul@441 | 36 | attendee lists. A list is returned containing the attendees whose |
paul@441 | 37 | attendance should be cancelled. |
paul@441 | 38 | """ |
paul@441 | 39 | |
paul@441 | 40 | to_cancel = [] |
paul@441 | 41 | |
paul@473 | 42 | existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) |
paul@473 | 43 | added = set(attendees).difference(existing_attendees) |
paul@473 | 44 | |
paul@441 | 45 | if added or removed: |
paul@441 | 46 | attendees = uri_items(obj.get_items("ATTENDEE") or []) |
paul@460 | 47 | sequence = obj.get_value("SEQUENCE") |
paul@441 | 48 | |
paul@441 | 49 | if removed: |
paul@441 | 50 | remaining = [] |
paul@441 | 51 | |
paul@441 | 52 | for attendee, attendee_attr in attendees: |
paul@441 | 53 | if attendee in removed: |
paul@460 | 54 | |
paul@460 | 55 | # Without a sequence number, assume that the event has not |
paul@460 | 56 | # been published and that attendees can be silently removed. |
paul@460 | 57 | |
paul@460 | 58 | if sequence is not None: |
paul@441 | 59 | to_cancel.append((attendee, attendee_attr)) |
paul@441 | 60 | else: |
paul@441 | 61 | remaining.append((attendee, attendee_attr)) |
paul@441 | 62 | |
paul@441 | 63 | attendees = remaining |
paul@441 | 64 | |
paul@441 | 65 | if added: |
paul@441 | 66 | for attendee in added: |
paul@473 | 67 | attendee = attendee.strip() |
paul@473 | 68 | if attendee: |
paul@473 | 69 | attendees.append((get_uri(attendee), {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) |
paul@441 | 70 | |
paul@441 | 71 | obj["ATTENDEE"] = attendees |
paul@441 | 72 | |
paul@441 | 73 | return to_cancel |
paul@441 | 74 | |
paul@443 | 75 | class Client: |
paul@443 | 76 | |
paul@443 | 77 | "Common handler and manager methods." |
paul@443 | 78 | |
paul@467 | 79 | default_window_size = 100 |
paul@467 | 80 | |
paul@604 | 81 | def __init__(self, user, messenger=None, store=None, publisher=None): |
paul@443 | 82 | self.user = user |
paul@601 | 83 | self.messenger = messenger |
paul@604 | 84 | self.store = store or imip_store.FileStore() |
paul@604 | 85 | |
paul@604 | 86 | try: |
paul@604 | 87 | self.publisher = publisher or imip_store.FilePublisher() |
paul@604 | 88 | except OSError: |
paul@604 | 89 | self.publisher = None |
paul@604 | 90 | |
paul@443 | 91 | self.preferences = None |
paul@443 | 92 | |
paul@443 | 93 | def get_preferences(self): |
paul@467 | 94 | if not self.preferences and self.user: |
paul@443 | 95 | self.preferences = Preferences(self.user) |
paul@443 | 96 | return self.preferences |
paul@443 | 97 | |
paul@443 | 98 | def get_tzid(self): |
paul@443 | 99 | prefs = self.get_preferences() |
paul@467 | 100 | return prefs and prefs.get("TZID") or get_default_timezone() |
paul@443 | 101 | |
paul@443 | 102 | def get_window_size(self): |
paul@443 | 103 | prefs = self.get_preferences() |
paul@443 | 104 | try: |
paul@467 | 105 | return prefs and int(prefs.get("window_size")) or self.default_window_size |
paul@443 | 106 | except (TypeError, ValueError): |
paul@467 | 107 | return self.default_window_size |
paul@443 | 108 | |
paul@443 | 109 | def get_window_end(self): |
paul@443 | 110 | return get_window_end(self.get_tzid(), self.get_window_size()) |
paul@443 | 111 | |
paul@443 | 112 | def is_sharing(self): |
paul@467 | 113 | prefs = self.get_preferences() |
paul@467 | 114 | return prefs and prefs.get("freebusy_sharing") == "share" or False |
paul@443 | 115 | |
paul@443 | 116 | def is_bundling(self): |
paul@467 | 117 | prefs = self.get_preferences() |
paul@467 | 118 | return prefs and prefs.get("freebusy_bundling") == "always" or False |
paul@467 | 119 | |
paul@467 | 120 | def is_notifying(self): |
paul@467 | 121 | prefs = self.get_preferences() |
paul@467 | 122 | return prefs and prefs.get("freebusy_messages") == "notify" or False |
paul@443 | 123 | |
paul@581 | 124 | # Common operations on calendar data. |
paul@581 | 125 | |
paul@604 | 126 | def is_participating(self, attr, as_organiser=False): |
paul@604 | 127 | return as_organiser or not attr or attr.get("PARTSTAT") != "DECLINED" |
paul@604 | 128 | |
paul@604 | 129 | def get_overriding_transparency(self, attr, as_organiser=False): |
paul@604 | 130 | return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None |
paul@604 | 131 | |
paul@582 | 132 | def update_participation(self, obj, partstat=None): |
paul@581 | 133 | |
paul@581 | 134 | """ |
paul@581 | 135 | Update the participation in 'obj' of the user with the given 'partstat'. |
paul@581 | 136 | """ |
paul@581 | 137 | |
paul@581 | 138 | attendee_attr = uri_dict(obj.get_value_map("ATTENDEE")).get(self.user) |
paul@582 | 139 | if not attendee_attr: |
paul@582 | 140 | return None |
paul@582 | 141 | if partstat: |
paul@582 | 142 | attendee_attr["PARTSTAT"] = partstat |
paul@581 | 143 | if attendee_attr.has_key("RSVP"): |
paul@581 | 144 | del attendee_attr["RSVP"] |
paul@584 | 145 | self.update_sender(attendee_attr) |
paul@581 | 146 | return attendee_attr |
paul@581 | 147 | |
paul@584 | 148 | def update_sender(self, attr): |
paul@584 | 149 | |
paul@584 | 150 | "Update the SENT-BY attribute of the 'attr' sender metadata." |
paul@584 | 151 | |
paul@584 | 152 | if self.messenger and self.messenger.sender != get_address(self.user): |
paul@584 | 153 | attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@584 | 154 | |
paul@604 | 155 | # Free/busy operations. |
paul@604 | 156 | |
paul@604 | 157 | def get_freebusy_part(self): |
paul@604 | 158 | |
paul@604 | 159 | """ |
paul@604 | 160 | Return a message part containing free/busy information for the user. |
paul@604 | 161 | """ |
paul@604 | 162 | |
paul@604 | 163 | if self.is_sharing() and self.is_bundling(): |
paul@604 | 164 | |
paul@604 | 165 | # Invent a unique identifier. |
paul@604 | 166 | |
paul@604 | 167 | utcnow = get_timestamp() |
paul@604 | 168 | uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) |
paul@604 | 169 | |
paul@604 | 170 | freebusy = self.store.get_freebusy(self.user) |
paul@604 | 171 | |
paul@604 | 172 | user_attr = {} |
paul@604 | 173 | self.update_sender(user_attr) |
paul@604 | 174 | return to_part("PUBLISH", [make_freebusy(freebusy, uid, self.user, user_attr)]) |
paul@604 | 175 | |
paul@604 | 176 | return None |
paul@604 | 177 | |
paul@601 | 178 | class ClientForObject(Client): |
paul@601 | 179 | |
paul@601 | 180 | "A client maintaining a specific object." |
paul@601 | 181 | |
paul@604 | 182 | def __init__(self, obj, user, messenger=None, store=None, publisher=None): |
paul@604 | 183 | Client.__init__(self, user, messenger, store, publisher) |
paul@601 | 184 | self.set_object(obj) |
paul@601 | 185 | |
paul@601 | 186 | def set_object(self, obj): |
paul@601 | 187 | self.obj = obj |
paul@601 | 188 | self.uid = obj and self.obj.get_uid() |
paul@601 | 189 | self.recurrenceid = obj and self.obj.get_recurrenceid() |
paul@601 | 190 | self.sequence = obj and self.obj.get_value("SEQUENCE") |
paul@601 | 191 | self.dtstamp = obj and self.obj.get_value("DTSTAMP") |
paul@601 | 192 | |
paul@601 | 193 | def _update_freebusy(self, freebusy, periods, recurrenceid, transp=None): |
paul@601 | 194 | |
paul@601 | 195 | """ |
paul@601 | 196 | Update the 'freebusy' collection with the given 'periods', indicating an |
paul@601 | 197 | explicit 'recurrenceid' to affect either a recurrence or the parent |
paul@601 | 198 | event. |
paul@601 | 199 | """ |
paul@601 | 200 | |
paul@601 | 201 | update_freebusy(freebusy, periods, |
paul@601 | 202 | transp or self.obj.get_value("TRANSP") or "OPAQUE", |
paul@601 | 203 | self.uid, recurrenceid, |
paul@601 | 204 | self.obj.get_value("SUMMARY"), |
paul@601 | 205 | self.obj.get_value("ORGANIZER")) |
paul@601 | 206 | |
paul@601 | 207 | def update_freebusy(self, freebusy, periods, transp=None): |
paul@601 | 208 | |
paul@601 | 209 | """ |
paul@601 | 210 | Update the 'freebusy' collection for this event with the given |
paul@601 | 211 | 'periods'. |
paul@601 | 212 | """ |
paul@601 | 213 | |
paul@601 | 214 | self._update_freebusy(freebusy, periods, self.recurrenceid, transp) |
paul@601 | 215 | |
paul@601 | 216 | def update_freebusy_for_participant(self, freebusy, periods, attr, for_organiser=False): |
paul@601 | 217 | |
paul@601 | 218 | """ |
paul@601 | 219 | Update the 'freebusy' collection using the given 'periods', subject to |
paul@601 | 220 | the 'attr' provided for the participant, indicating whether this is |
paul@601 | 221 | being generated 'for_organiser' or not. |
paul@601 | 222 | """ |
paul@601 | 223 | |
paul@601 | 224 | # Organisers employ a special transparency if not attending. |
paul@601 | 225 | |
paul@601 | 226 | if self.is_participating(attr, for_organiser): |
paul@601 | 227 | self.update_freebusy(freebusy, periods, |
paul@601 | 228 | transp=self.get_overriding_transparency(attr, for_organiser)) |
paul@601 | 229 | else: |
paul@601 | 230 | self.remove_from_freebusy(freebusy) |
paul@601 | 231 | |
paul@604 | 232 | # Object update methods. |
paul@601 | 233 | |
paul@601 | 234 | def update_dtstamp(self): |
paul@601 | 235 | |
paul@601 | 236 | "Update the DTSTAMP in the current object." |
paul@601 | 237 | |
paul@601 | 238 | dtstamp = self.obj.get_utc_datetime("DTSTAMP") |
paul@601 | 239 | utcnow = to_timezone(datetime.utcnow(), "UTC") |
paul@601 | 240 | self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] |
paul@601 | 241 | |
paul@601 | 242 | def set_sequence(self, increment=False): |
paul@601 | 243 | |
paul@601 | 244 | "Update the SEQUENCE in the current object." |
paul@601 | 245 | |
paul@601 | 246 | sequence = self.obj.get_value("SEQUENCE") or "0" |
paul@601 | 247 | self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] |
paul@601 | 248 | |
paul@441 | 249 | # vim: tabstop=4 expandtab shiftwidth=4 |