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