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