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@705 | 23 | from imiptools.client import Client |
paul@729 | 24 | from imiptools.data import get_uri, uri_dict, uri_values |
paul@418 | 25 | from imiptools.handlers import Handler |
paul@683 | 26 | from imiptools.handlers.common import CommonEvent |
paul@96 | 27 | |
paul@725 | 28 | class PersonHandler(CommonEvent, Handler): |
paul@96 | 29 | |
paul@96 | 30 | "Handling mechanisms specific to people." |
paul@96 | 31 | |
paul@729 | 32 | def set_identity(self, method): |
paul@130 | 33 | |
paul@140 | 34 | """ |
paul@729 | 35 | Set the current user for the current object in the context of the given |
paul@729 | 36 | 'method'. It is usually set when initialising the handler, using the |
paul@729 | 37 | recipient details, but outgoing messages do not reference the recipient |
paul@729 | 38 | in this way. |
paul@140 | 39 | """ |
paul@130 | 40 | |
paul@729 | 41 | if self.obj: |
paul@729 | 42 | from_organiser = method in self.organiser_methods |
paul@729 | 43 | self.user = get_uri(self.obj.get_value(from_organiser and "ORGANIZER" or "ATTENDEE")) |
paul@140 | 44 | |
paul@684 | 45 | def _add(self): |
paul@684 | 46 | |
paul@684 | 47 | "Add a recurrence for the current object." |
paul@684 | 48 | |
paul@705 | 49 | if not Client.is_participating(self): |
paul@705 | 50 | return False |
paul@684 | 51 | |
paul@684 | 52 | # Obtain valid organiser and attendee details. |
paul@684 | 53 | |
paul@684 | 54 | oa = self.require_organiser_and_attendees() |
paul@684 | 55 | if not oa: |
paul@684 | 56 | return False |
paul@684 | 57 | |
paul@684 | 58 | (organiser, organiser_attr), attendees = oa |
paul@684 | 59 | |
paul@684 | 60 | # Ignore unknown objects. |
paul@684 | 61 | |
paul@684 | 62 | if not self.get_stored_object_version(): |
paul@684 | 63 | return |
paul@684 | 64 | |
paul@684 | 65 | # Record the event as a recurrence of the parent object. |
paul@684 | 66 | |
paul@684 | 67 | self.update_recurrenceid() |
paul@684 | 68 | |
paul@733 | 69 | # Set the additional occurrence. |
paul@733 | 70 | |
paul@733 | 71 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@733 | 72 | |
paul@684 | 73 | # Update free/busy information. |
paul@684 | 74 | |
paul@684 | 75 | self.update_event_in_freebusy() |
paul@684 | 76 | |
paul@684 | 77 | return True |
paul@684 | 78 | |
paul@799 | 79 | def _record(self, from_organiser=True, counter=False): |
paul@140 | 80 | |
paul@420 | 81 | """ |
paul@420 | 82 | Record details from the current object given a message originating |
paul@580 | 83 | from an organiser if 'from_organiser' is set to a true value. |
paul@420 | 84 | """ |
paul@140 | 85 | |
paul@705 | 86 | if not Client.is_participating(self): |
paul@705 | 87 | return False |
paul@468 | 88 | |
paul@682 | 89 | # Check for a new event, tolerating not-strictly-new events if the |
paul@682 | 90 | # attendee is responding. |
paul@468 | 91 | |
paul@682 | 92 | if not self.have_new_object(strict=from_organiser): |
paul@99 | 93 | return False |
paul@97 | 94 | |
paul@268 | 95 | # Update the object. |
paul@96 | 96 | |
paul@268 | 97 | if from_organiser: |
paul@334 | 98 | |
paul@361 | 99 | # Set the complete event or an additional occurrence. |
paul@334 | 100 | |
paul@468 | 101 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@334 | 102 | |
paul@381 | 103 | # Remove additional recurrences if handling a complete event. |
paul@796 | 104 | # Also remove any previous cancellations involving this event. |
paul@381 | 105 | |
paul@381 | 106 | if not self.recurrenceid: |
paul@468 | 107 | self.store.remove_recurrences(self.user, self.uid) |
paul@796 | 108 | self.store.remove_cancellations(self.user, self.uid) |
paul@796 | 109 | else: |
paul@796 | 110 | self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) |
paul@381 | 111 | |
paul@268 | 112 | else: |
paul@420 | 113 | # Obtain valid attendees, merging their attendance with the stored |
paul@420 | 114 | # object. |
paul@420 | 115 | |
paul@420 | 116 | attendees = self.require_attendees(from_organiser) |
paul@468 | 117 | self.merge_attendance(attendees) |
paul@96 | 118 | |
paul@160 | 119 | # Remove any associated request. |
paul@160 | 120 | |
paul@468 | 121 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@809 | 122 | self.store.remove_counters(self.user, self.uid, self.recurrenceid) |
paul@160 | 123 | |
paul@96 | 124 | # Update free/busy information. |
paul@96 | 125 | |
paul@799 | 126 | if not counter: |
paul@799 | 127 | self.update_event_in_freebusy(from_organiser) |
paul@799 | 128 | |
paul@799 | 129 | # For countered proposals, record the offer in the resource's |
paul@799 | 130 | # free/busy collection. |
paul@799 | 131 | |
paul@799 | 132 | else: |
paul@799 | 133 | self.update_event_in_freebusy_offers() |
paul@96 | 134 | |
paul@96 | 135 | return True |
paul@96 | 136 | |
paul@697 | 137 | def _remove(self): |
paul@140 | 138 | |
paul@580 | 139 | """ |
paul@580 | 140 | Remove details from the current object given a message originating |
paul@580 | 141 | from an organiser if 'from_organiser' is set to a true value. |
paul@580 | 142 | """ |
paul@140 | 143 | |
paul@705 | 144 | if not Client.is_participating(self): |
paul@705 | 145 | return False |
paul@468 | 146 | |
paul@468 | 147 | # Check for event using UID. |
paul@468 | 148 | |
paul@468 | 149 | if not self.have_new_object(): |
paul@140 | 150 | return False |
paul@140 | 151 | |
paul@527 | 152 | # Obtain any stored object, using parent object details if a newly- |
paul@527 | 153 | # indicated occurrence is referenced. |
paul@312 | 154 | |
paul@606 | 155 | obj = self.get_stored_object_version() |
paul@527 | 156 | old = not obj and self.get_parent_object() or obj |
paul@527 | 157 | |
paul@527 | 158 | if not old: |
paul@420 | 159 | return False |
paul@261 | 160 | |
paul@527 | 161 | # Only cancel the event completely if all attendees are given. |
paul@527 | 162 | |
paul@527 | 163 | attendees = uri_dict(old.get_value_map("ATTENDEE")) |
paul@312 | 164 | all_attendees = set(attendees.keys()) |
paul@312 | 165 | given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) |
paul@690 | 166 | cancel_entire_event = not all_attendees.difference(given_attendees) |
paul@312 | 167 | |
paul@697 | 168 | # Update the recipient's record of the organiser's schedule. |
paul@697 | 169 | |
paul@697 | 170 | self.remove_freebusy_from_organiser(self.obj.get_value("ORGANIZER")) |
paul@697 | 171 | |
paul@312 | 172 | # Otherwise, remove the given attendees and update the event. |
paul@312 | 173 | |
paul@694 | 174 | if not cancel_entire_event and obj: |
paul@312 | 175 | for attendee in given_attendees: |
paul@314 | 176 | if attendees.has_key(attendee): |
paul@314 | 177 | del attendees[attendee] |
paul@312 | 178 | obj["ATTENDEE"] = attendees.items() |
paul@264 | 179 | |
paul@312 | 180 | # Update the stored object with sequence information. |
paul@312 | 181 | |
paul@527 | 182 | if obj: |
paul@577 | 183 | obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") or [] |
paul@577 | 184 | obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") or [] |
paul@312 | 185 | |
paul@672 | 186 | # Update free/busy information. |
paul@672 | 187 | |
paul@672 | 188 | if cancel_entire_event or self.user in given_attendees: |
paul@672 | 189 | self.remove_event_from_freebusy() |
paul@672 | 190 | |
paul@527 | 191 | # Set the complete event if not an additional occurrence. For any newly- |
paul@527 | 192 | # indicated occurrence, use the received event details. |
paul@334 | 193 | |
paul@527 | 194 | self.store.set_event(self.user, self.uid, self.recurrenceid, (obj or self.obj).to_node()) |
paul@264 | 195 | |
paul@694 | 196 | # Perform any cancellation after recording the latest state of the |
paul@694 | 197 | # event. |
paul@694 | 198 | |
paul@694 | 199 | if cancel_entire_event: |
paul@694 | 200 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@694 | 201 | |
paul@261 | 202 | # Remove any associated request. |
paul@261 | 203 | |
paul@468 | 204 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@809 | 205 | self.store.remove_counters(self.user, self.uid, self.recurrenceid) |
paul@261 | 206 | |
paul@140 | 207 | return True |
paul@140 | 208 | |
paul@804 | 209 | def _declinecounter(self): |
paul@804 | 210 | |
paul@804 | 211 | "Remove any counter-proposals for the given event." |
paul@804 | 212 | |
paul@804 | 213 | if not Client.is_participating(self): |
paul@804 | 214 | return False |
paul@804 | 215 | |
paul@804 | 216 | # Check for event using UID. |
paul@804 | 217 | |
paul@804 | 218 | if not self.have_new_object(): |
paul@804 | 219 | return False |
paul@804 | 220 | |
paul@809 | 221 | self.remove_counters(uri_values(self.obj.get_values("ATTENDEE"))) |
paul@804 | 222 | |
paul@96 | 223 | class Event(PersonHandler): |
paul@96 | 224 | |
paul@96 | 225 | "An event handler." |
paul@96 | 226 | |
paul@96 | 227 | def add(self): |
paul@686 | 228 | |
paul@686 | 229 | "Record the addition of a recurrence to an event." |
paul@686 | 230 | |
paul@684 | 231 | self._add() |
paul@96 | 232 | |
paul@96 | 233 | def cancel(self): |
paul@686 | 234 | |
paul@686 | 235 | "Remove an event or a recurrence." |
paul@686 | 236 | |
paul@697 | 237 | self._remove() |
paul@96 | 238 | |
paul@96 | 239 | def counter(self): |
paul@686 | 240 | |
paul@799 | 241 | "Record an offer made by a counter-proposal." |
paul@686 | 242 | |
paul@799 | 243 | self._record(False, True) |
paul@96 | 244 | |
paul@96 | 245 | def declinecounter(self): |
paul@686 | 246 | |
paul@804 | 247 | "Expire any offer made by a counter-proposal." |
paul@686 | 248 | |
paul@804 | 249 | self._declinecounter() |
paul@96 | 250 | |
paul@96 | 251 | def publish(self): |
paul@686 | 252 | |
paul@686 | 253 | "Published events are recorded." |
paul@686 | 254 | |
paul@580 | 255 | self._record(True) |
paul@96 | 256 | |
paul@96 | 257 | def refresh(self): |
paul@686 | 258 | |
paul@686 | 259 | "Requests to refresh events do not provide event information." |
paul@686 | 260 | |
paul@353 | 261 | pass |
paul@96 | 262 | |
paul@96 | 263 | def reply(self): |
paul@686 | 264 | |
paul@686 | 265 | "Replies to requests are inspected for attendee information." |
paul@686 | 266 | |
paul@580 | 267 | self._record(False) |
paul@96 | 268 | |
paul@96 | 269 | def request(self): |
paul@686 | 270 | |
paul@686 | 271 | "Record events sent for potential scheduling." |
paul@686 | 272 | |
paul@580 | 273 | self._record(True) |
paul@96 | 274 | |
paul@96 | 275 | # Handler registry. |
paul@96 | 276 | |
paul@96 | 277 | handlers = [ |
paul@96 | 278 | ("VEVENT", Event), |
paul@96 | 279 | ] |
paul@96 | 280 | |
paul@96 | 281 | # vim: tabstop=4 expandtab shiftwidth=4 |