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