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@312 | 24 | from imiptools.data import uri_dict, uri_item, uri_values |
paul@291 | 25 | from imiptools.dates import get_default_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@334 | 65 | recurrenceid = self.obj.get_value("RECURRENCE-ID") |
paul@334 | 66 | |
paul@334 | 67 | if not recurrenceid: |
paul@334 | 68 | self.store.set_event(identity, self.uid, event) |
paul@334 | 69 | else: |
paul@334 | 70 | self.store.set_recurrence(identity, self.uid, recurrenceid, event) |
paul@334 | 71 | |
paul@268 | 72 | else: |
paul@268 | 73 | organiser_item, attendees = self.require_organiser_and_attendees(from_organiser) |
paul@268 | 74 | self.merge_attendance(attendees, identity) |
paul@96 | 75 | |
paul@160 | 76 | # Remove any associated request. |
paul@160 | 77 | |
paul@160 | 78 | self.store.dequeue_request(identity, self.uid) |
paul@160 | 79 | |
paul@96 | 80 | # Update free/busy information. |
paul@96 | 81 | |
paul@96 | 82 | if update_freebusy: |
paul@97 | 83 | |
paul@291 | 84 | # Interpretation of periods can depend on the time zone. |
paul@291 | 85 | |
paul@291 | 86 | preferences = Preferences(identity) |
paul@291 | 87 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@291 | 88 | |
paul@97 | 89 | # If newer than any old version, discard old details from the |
paul@97 | 90 | # free/busy record and check for suitability. |
paul@97 | 91 | |
paul@291 | 92 | periods = self.obj.get_periods_for_freebusy(tzid) |
paul@173 | 93 | freebusy = self.store.get_freebusy(identity) |
paul@97 | 94 | |
paul@284 | 95 | if attr.get("PARTSTAT") != "DECLINED": |
paul@122 | 96 | self.update_freebusy(freebusy, identity, periods) |
paul@122 | 97 | else: |
paul@122 | 98 | self.remove_from_freebusy(freebusy, identity) |
paul@97 | 99 | |
paul@96 | 100 | if self.publisher: |
paul@96 | 101 | self.publisher.set_freebusy(identity, freebusy) |
paul@96 | 102 | |
paul@96 | 103 | return True |
paul@96 | 104 | |
paul@261 | 105 | def _remove(self, from_organiser=True, update_freebusy=False): |
paul@140 | 106 | |
paul@140 | 107 | "Remove free/busy information for any unprocessed object." |
paul@140 | 108 | |
paul@213 | 109 | item = self._get_identity(from_organiser) |
paul@140 | 110 | if not item: |
paul@140 | 111 | return False |
paul@140 | 112 | |
paul@140 | 113 | identity, attr = item |
paul@140 | 114 | |
paul@312 | 115 | # Only cancel the event completely if all attendees are given. |
paul@314 | 116 | # NOTE: Need to also check for recurrence identifiers and selective |
paul@314 | 117 | # NOTE: cancellations. |
paul@312 | 118 | |
paul@312 | 119 | obj = self.get_object(identity) |
paul@261 | 120 | |
paul@312 | 121 | attendees = uri_dict(obj.get_value_map("ATTENDEE")) |
paul@312 | 122 | all_attendees = set(attendees.keys()) |
paul@312 | 123 | given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) |
paul@312 | 124 | |
paul@312 | 125 | if given_attendees == all_attendees: |
paul@312 | 126 | self.store.cancel_event(identity, self.uid) |
paul@261 | 127 | |
paul@312 | 128 | # Otherwise, remove the given attendees and update the event. |
paul@312 | 129 | |
paul@312 | 130 | else: |
paul@312 | 131 | for attendee in given_attendees: |
paul@314 | 132 | if attendees.has_key(attendee): |
paul@314 | 133 | del attendees[attendee] |
paul@312 | 134 | obj["ATTENDEE"] = attendees.items() |
paul@264 | 135 | |
paul@312 | 136 | # Update the stored object with sequence information. |
paul@312 | 137 | |
paul@312 | 138 | obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") |
paul@312 | 139 | obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") |
paul@312 | 140 | |
paul@334 | 141 | # Set the complete event if not an additional occurrence. |
paul@334 | 142 | |
paul@334 | 143 | event = obj.to_node() |
paul@334 | 144 | recurrenceid = obj.get_value("RECURRENCE-ID") |
paul@334 | 145 | |
paul@334 | 146 | if not recurrenceid: |
paul@334 | 147 | self.store.set_event(identity, self.uid, event) |
paul@334 | 148 | else: |
paul@334 | 149 | self.store.set_recurrence(identity, self.uid, recurrenceid, event) |
paul@264 | 150 | |
paul@261 | 151 | # Remove any associated request. |
paul@261 | 152 | |
paul@261 | 153 | self.store.dequeue_request(identity, self.uid) |
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@261 | 159 | self.remove_from_freebusy(freebusy, identity) |
paul@261 | 160 | |
paul@261 | 161 | if self.publisher: |
paul@261 | 162 | self.publisher.set_freebusy(identity, freebusy) |
paul@140 | 163 | |
paul@140 | 164 | return True |
paul@140 | 165 | |
paul@96 | 166 | class Event(PersonHandler): |
paul@96 | 167 | |
paul@96 | 168 | "An event handler." |
paul@96 | 169 | |
paul@96 | 170 | def add(self): |
paul@96 | 171 | pass |
paul@96 | 172 | |
paul@96 | 173 | def cancel(self): |
paul@261 | 174 | self._remove(True, True) |
paul@96 | 175 | |
paul@96 | 176 | def counter(self): |
paul@96 | 177 | pass |
paul@96 | 178 | |
paul@96 | 179 | def declinecounter(self): |
paul@96 | 180 | pass |
paul@96 | 181 | |
paul@96 | 182 | def publish(self): |
paul@213 | 183 | self._record(True, True) |
paul@96 | 184 | |
paul@96 | 185 | def refresh(self): |
paul@213 | 186 | self._record(True, True) |
paul@96 | 187 | |
paul@96 | 188 | def reply(self): |
paul@213 | 189 | self._record(False, True) |
paul@96 | 190 | |
paul@96 | 191 | def request(self): |
paul@213 | 192 | self._record(True, True) |
paul@96 | 193 | |
paul@96 | 194 | class Freebusy(PersonHandler): |
paul@96 | 195 | |
paul@96 | 196 | "A free/busy handler." |
paul@96 | 197 | |
paul@96 | 198 | def publish(self): |
paul@96 | 199 | pass |
paul@96 | 200 | |
paul@96 | 201 | def reply(self): |
paul@96 | 202 | pass |
paul@96 | 203 | |
paul@96 | 204 | def request(self): |
paul@96 | 205 | pass |
paul@96 | 206 | |
paul@96 | 207 | class Journal(PersonHandler): |
paul@96 | 208 | |
paul@96 | 209 | "A journal entry handler." |
paul@96 | 210 | |
paul@96 | 211 | def add(self): |
paul@96 | 212 | pass |
paul@96 | 213 | |
paul@96 | 214 | def cancel(self): |
paul@213 | 215 | self._remove(True) |
paul@96 | 216 | |
paul@96 | 217 | def publish(self): |
paul@213 | 218 | self._record(True) |
paul@96 | 219 | |
paul@96 | 220 | class Todo(PersonHandler): |
paul@96 | 221 | |
paul@96 | 222 | "A to-do item handler." |
paul@96 | 223 | |
paul@96 | 224 | def add(self): |
paul@96 | 225 | pass |
paul@96 | 226 | |
paul@96 | 227 | def cancel(self): |
paul@213 | 228 | self._remove(True) |
paul@96 | 229 | |
paul@96 | 230 | def counter(self): |
paul@96 | 231 | pass |
paul@96 | 232 | |
paul@96 | 233 | def declinecounter(self): |
paul@96 | 234 | pass |
paul@96 | 235 | |
paul@96 | 236 | def publish(self): |
paul@213 | 237 | self._record(True) |
paul@96 | 238 | |
paul@96 | 239 | def refresh(self): |
paul@213 | 240 | self._record(True) |
paul@96 | 241 | |
paul@96 | 242 | def reply(self): |
paul@213 | 243 | self._record(False) |
paul@96 | 244 | |
paul@96 | 245 | def request(self): |
paul@213 | 246 | self._record(True) |
paul@96 | 247 | |
paul@96 | 248 | # Handler registry. |
paul@96 | 249 | |
paul@96 | 250 | handlers = [ |
paul@96 | 251 | ("VFREEBUSY", Freebusy), |
paul@96 | 252 | ("VEVENT", Event), |
paul@96 | 253 | ("VTODO", Todo), |
paul@96 | 254 | ("VJOURNAL", Journal), |
paul@96 | 255 | ] |
paul@96 | 256 | |
paul@96 | 257 | # vim: tabstop=4 expandtab shiftwidth=4 |