paul@96 | 1 | #!/usr/bin/env python |
paul@96 | 2 | |
paul@96 | 3 | """ |
paul@96 | 4 | Handlers for a person for whom scheduling is performed, inspecting outgoing |
paul@96 | 5 | messages to obtain scheduling done externally. |
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@96 | 21 | """ |
paul@96 | 22 | |
paul@213 | 23 | from imiptools.content import Handler |
paul@360 | 24 | from imiptools.data import get_window_end, uri_dict, uri_item, uri_values |
paul@360 | 25 | from imiptools.dates import format_datetime, get_default_timezone, to_timezone |
paul@291 | 26 | from imiptools.profile import Preferences |
paul@96 | 27 | |
paul@96 | 28 | class PersonHandler(Handler): |
paul@96 | 29 | |
paul@96 | 30 | "Handling mechanisms specific to people." |
paul@96 | 31 | |
paul@213 | 32 | def _get_identity(self, from_organiser=True): |
paul@130 | 33 | |
paul@140 | 34 | """ |
paul@140 | 35 | Get the identity of interest in a usable form for any unprocessed |
paul@140 | 36 | object. |
paul@140 | 37 | """ |
paul@130 | 38 | |
paul@213 | 39 | identity, attr = item = uri_item(self.obj.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) |
paul@96 | 40 | |
paul@97 | 41 | # Check for event using UID. |
paul@97 | 42 | |
paul@213 | 43 | if not self.have_new_object(identity): |
paul@140 | 44 | return None |
paul@140 | 45 | |
paul@140 | 46 | return item |
paul@140 | 47 | |
paul@213 | 48 | def _record(self, from_organiser=True, update_freebusy=False): |
paul@140 | 49 | |
paul@140 | 50 | "Record free/busy and object information." |
paul@140 | 51 | |
paul@213 | 52 | item = self._get_identity(from_organiser) |
paul@140 | 53 | if not item: |
paul@99 | 54 | return False |
paul@97 | 55 | |
paul@140 | 56 | identity, attr = item |
paul@140 | 57 | |
paul@268 | 58 | # Update the object. |
paul@96 | 59 | |
paul@268 | 60 | if from_organiser: |
paul@334 | 61 | |
paul@334 | 62 | # Set the complete event if not an additional occurrence. |
paul@334 | 63 | |
paul@334 | 64 | event = self.obj.to_node() |
paul@343 | 65 | self.store.set_event(identity, self.uid, self.recurrenceid, event) |
paul@334 | 66 | |
paul@268 | 67 | else: |
paul@268 | 68 | organiser_item, attendees = self.require_organiser_and_attendees(from_organiser) |
paul@268 | 69 | self.merge_attendance(attendees, identity) |
paul@96 | 70 | |
paul@160 | 71 | # Remove any associated request. |
paul@160 | 72 | |
paul@343 | 73 | self.store.dequeue_request(identity, self.uid, self.recurrenceid) |
paul@160 | 74 | |
paul@360 | 75 | # Interpretation of periods can depend on the time zone. |
paul@360 | 76 | |
paul@360 | 77 | preferences = Preferences(identity) |
paul@360 | 78 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@360 | 79 | |
paul@360 | 80 | # Where a recurring object is updated by a specific occurrence, the |
paul@360 | 81 | # details of the recurring "parent" must be changed. |
paul@360 | 82 | |
paul@360 | 83 | if self.recurrenceid: |
paul@360 | 84 | obj = self.get_parent_object(identity) |
paul@360 | 85 | recurrence = self.obj.get_datetime("RECURRENCE-ID") |
paul@360 | 86 | |
paul@360 | 87 | if obj.has_recurrence(tzid, recurrence): |
paul@360 | 88 | item = obj.get_item("EXDATE") |
paul@360 | 89 | if item: |
paul@360 | 90 | exdates, exdate_attr = item |
paul@360 | 91 | if not isinstance(exdates, list): |
paul@360 | 92 | exdates = [exdates] |
paul@360 | 93 | else: |
paul@360 | 94 | exdates, exdate_attr = [], {} |
paul@360 | 95 | |
paul@360 | 96 | # Convert the occurrence to the same time regime as the other |
paul@360 | 97 | # exceptions. |
paul@360 | 98 | |
paul@360 | 99 | exdate_tzid = exdate_attr.get("TZID") |
paul@360 | 100 | exdate = recurrence |
paul@360 | 101 | if exdate_tzid: |
paul@360 | 102 | exdate = to_timezone(exdate, exdate_tzid) |
paul@360 | 103 | else: |
paul@360 | 104 | exdate = to_timezone(exdate, "UTC") |
paul@360 | 105 | |
paul@360 | 106 | # Update the exceptions and store the modified parent event. |
paul@360 | 107 | |
paul@360 | 108 | exdates.append(format_datetime(exdate)) |
paul@360 | 109 | obj["EXDATE"] = [(exdates, exdate_attr)] |
paul@360 | 110 | |
paul@360 | 111 | self.store.set_event(identity, self.uid, None, obj.to_node()) |
paul@360 | 112 | |
paul@96 | 113 | # Update free/busy information. |
paul@96 | 114 | |
paul@96 | 115 | if update_freebusy: |
paul@97 | 116 | |
paul@360 | 117 | freebusy = self.store.get_freebusy(identity) |
paul@354 | 118 | |
paul@360 | 119 | # Use the stored event in case the reply is incomplete, as is seen |
paul@360 | 120 | # when Claws sends a REPLY for an object originally employing |
paul@360 | 121 | # recurrence information. Additionally, parent object details will |
paul@360 | 122 | # also be consulted if available. |
paul@354 | 123 | |
paul@360 | 124 | obj = self.get_object(identity) |
paul@291 | 125 | |
paul@97 | 126 | # If newer than any old version, discard old details from the |
paul@97 | 127 | # free/busy record and check for suitability. |
paul@97 | 128 | |
paul@360 | 129 | periods = obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) |
paul@97 | 130 | |
paul@284 | 131 | if attr.get("PARTSTAT") != "DECLINED": |
paul@122 | 132 | self.update_freebusy(freebusy, identity, periods) |
paul@122 | 133 | else: |
paul@122 | 134 | self.remove_from_freebusy(freebusy, identity) |
paul@97 | 135 | |
paul@360 | 136 | # For any parent object, refresh the updated periods. |
paul@360 | 137 | |
paul@360 | 138 | if self.recurrenceid: |
paul@360 | 139 | obj = self.get_parent_object(identity) |
paul@360 | 140 | periods = obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) |
paul@360 | 141 | |
paul@360 | 142 | self._update_freebusy(freebusy, identity, periods, None) |
paul@360 | 143 | |
paul@96 | 144 | if self.publisher: |
paul@96 | 145 | self.publisher.set_freebusy(identity, freebusy) |
paul@96 | 146 | |
paul@96 | 147 | return True |
paul@96 | 148 | |
paul@261 | 149 | def _remove(self, from_organiser=True, update_freebusy=False): |
paul@140 | 150 | |
paul@140 | 151 | "Remove free/busy information for any unprocessed object." |
paul@140 | 152 | |
paul@213 | 153 | item = self._get_identity(from_organiser) |
paul@140 | 154 | if not item: |
paul@140 | 155 | return False |
paul@140 | 156 | |
paul@140 | 157 | identity, attr = item |
paul@140 | 158 | |
paul@312 | 159 | # Only cancel the event completely if all attendees are given. |
paul@314 | 160 | # NOTE: Need to also check for recurrence identifiers and selective |
paul@314 | 161 | # NOTE: cancellations. |
paul@312 | 162 | |
paul@312 | 163 | obj = self.get_object(identity) |
paul@261 | 164 | |
paul@312 | 165 | attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@312 | 166 | all_attendees = set(attendees.keys()) |
paul@312 | 167 | given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) |
paul@312 | 168 | |
paul@312 | 169 | if given_attendees == all_attendees: |
paul@343 | 170 | self.store.cancel_event(identity, self.uid, self.recurrenceid) |
paul@261 | 171 | |
paul@312 | 172 | # Otherwise, remove the given attendees and update the event. |
paul@312 | 173 | |
paul@312 | 174 | else: |
paul@312 | 175 | for attendee in given_attendees: |
paul@314 | 176 | if attendees.has_key(attendee): |
paul@314 | 177 | del attendees[attendee] |
paul@312 | 178 | obj["ATTENDEE"] = attendees.items() |
paul@264 | 179 | |
paul@312 | 180 | # Update the stored object with sequence information. |
paul@312 | 181 | |
paul@312 | 182 | obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") |
paul@312 | 183 | obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") |
paul@312 | 184 | |
paul@334 | 185 | # Set the complete event if not an additional occurrence. |
paul@334 | 186 | |
paul@334 | 187 | event = obj.to_node() |
paul@343 | 188 | self.store.set_event(identity, self.uid, self.recurrenceid, event) |
paul@264 | 189 | |
paul@261 | 190 | # Remove any associated request. |
paul@261 | 191 | |
paul@343 | 192 | self.store.dequeue_request(identity, self.uid, self.recurrenceid) |
paul@261 | 193 | |
paul@261 | 194 | # Update free/busy information. |
paul@261 | 195 | |
paul@261 | 196 | if update_freebusy: |
paul@261 | 197 | freebusy = self.store.get_freebusy(identity) |
paul@261 | 198 | self.remove_from_freebusy(freebusy, identity) |
paul@261 | 199 | |
paul@261 | 200 | if self.publisher: |
paul@261 | 201 | self.publisher.set_freebusy(identity, freebusy) |
paul@140 | 202 | |
paul@140 | 203 | return True |
paul@140 | 204 | |
paul@96 | 205 | class Event(PersonHandler): |
paul@96 | 206 | |
paul@96 | 207 | "An event handler." |
paul@96 | 208 | |
paul@96 | 209 | def add(self): |
paul@96 | 210 | pass |
paul@96 | 211 | |
paul@96 | 212 | def cancel(self): |
paul@261 | 213 | self._remove(True, True) |
paul@96 | 214 | |
paul@96 | 215 | def counter(self): |
paul@96 | 216 | pass |
paul@96 | 217 | |
paul@96 | 218 | def declinecounter(self): |
paul@96 | 219 | pass |
paul@96 | 220 | |
paul@96 | 221 | def publish(self): |
paul@213 | 222 | self._record(True, True) |
paul@96 | 223 | |
paul@96 | 224 | def refresh(self): |
paul@353 | 225 | pass |
paul@96 | 226 | |
paul@96 | 227 | def reply(self): |
paul@213 | 228 | self._record(False, True) |
paul@96 | 229 | |
paul@96 | 230 | def request(self): |
paul@213 | 231 | self._record(True, True) |
paul@96 | 232 | |
paul@96 | 233 | class Freebusy(PersonHandler): |
paul@96 | 234 | |
paul@96 | 235 | "A free/busy handler." |
paul@96 | 236 | |
paul@96 | 237 | def publish(self): |
paul@96 | 238 | pass |
paul@96 | 239 | |
paul@96 | 240 | def reply(self): |
paul@96 | 241 | pass |
paul@96 | 242 | |
paul@96 | 243 | def request(self): |
paul@96 | 244 | pass |
paul@96 | 245 | |
paul@96 | 246 | class Journal(PersonHandler): |
paul@96 | 247 | |
paul@96 | 248 | "A journal entry handler." |
paul@96 | 249 | |
paul@96 | 250 | def add(self): |
paul@96 | 251 | pass |
paul@96 | 252 | |
paul@96 | 253 | def cancel(self): |
paul@213 | 254 | self._remove(True) |
paul@96 | 255 | |
paul@96 | 256 | def publish(self): |
paul@213 | 257 | self._record(True) |
paul@96 | 258 | |
paul@96 | 259 | class Todo(PersonHandler): |
paul@96 | 260 | |
paul@96 | 261 | "A to-do item handler." |
paul@96 | 262 | |
paul@96 | 263 | def add(self): |
paul@96 | 264 | pass |
paul@96 | 265 | |
paul@96 | 266 | def cancel(self): |
paul@213 | 267 | self._remove(True) |
paul@96 | 268 | |
paul@96 | 269 | def counter(self): |
paul@96 | 270 | pass |
paul@96 | 271 | |
paul@96 | 272 | def declinecounter(self): |
paul@96 | 273 | pass |
paul@96 | 274 | |
paul@96 | 275 | def publish(self): |
paul@213 | 276 | self._record(True) |
paul@96 | 277 | |
paul@96 | 278 | def refresh(self): |
paul@353 | 279 | pass |
paul@96 | 280 | |
paul@96 | 281 | def reply(self): |
paul@213 | 282 | self._record(False) |
paul@96 | 283 | |
paul@96 | 284 | def request(self): |
paul@213 | 285 | self._record(True) |
paul@96 | 286 | |
paul@96 | 287 | # Handler registry. |
paul@96 | 288 | |
paul@96 | 289 | handlers = [ |
paul@96 | 290 | ("VFREEBUSY", Freebusy), |
paul@96 | 291 | ("VEVENT", Event), |
paul@96 | 292 | ("VTODO", Todo), |
paul@96 | 293 | ("VJOURNAL", Journal), |
paul@96 | 294 | ] |
paul@96 | 295 | |
paul@96 | 296 | # vim: tabstop=4 expandtab shiftwidth=4 |