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