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