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