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