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