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@580 | 25 | from imiptools.handlers.common import Outgoing |
paul@96 | 26 | |
paul@580 | 27 | class PersonHandler(Handler, Outgoing): |
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@580 | 41 | def _record(self, from_organiser=True): |
paul@140 | 42 | |
paul@420 | 43 | """ |
paul@420 | 44 | Record details from the current object given a message originating |
paul@580 | 45 | from an organiser if 'from_organiser' is set to a true value. |
paul@420 | 46 | """ |
paul@140 | 47 | |
paul@491 | 48 | self.set_identity(from_organiser) |
paul@468 | 49 | |
paul@682 | 50 | # Check for a new event, tolerating not-strictly-new events if the |
paul@682 | 51 | # attendee is responding. |
paul@468 | 52 | |
paul@682 | 53 | if not self.have_new_object(strict=from_organiser): |
paul@99 | 54 | return False |
paul@97 | 55 | |
paul@268 | 56 | # Update the object. |
paul@96 | 57 | |
paul@268 | 58 | if from_organiser: |
paul@334 | 59 | |
paul@361 | 60 | # Set the complete event or an additional occurrence. |
paul@334 | 61 | |
paul@468 | 62 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@334 | 63 | |
paul@381 | 64 | # Remove additional recurrences if handling a complete event. |
paul@381 | 65 | |
paul@381 | 66 | if not self.recurrenceid: |
paul@468 | 67 | self.store.remove_recurrences(self.user, self.uid) |
paul@381 | 68 | |
paul@268 | 69 | else: |
paul@420 | 70 | # Obtain valid attendees, merging their attendance with the stored |
paul@420 | 71 | # object. |
paul@420 | 72 | |
paul@420 | 73 | attendees = self.require_attendees(from_organiser) |
paul@468 | 74 | self.merge_attendance(attendees) |
paul@96 | 75 | |
paul@160 | 76 | # Remove any associated request. |
paul@160 | 77 | |
paul@468 | 78 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@160 | 79 | |
paul@96 | 80 | # Update free/busy information. |
paul@96 | 81 | |
paul@586 | 82 | self.update_event_in_freebusy(from_organiser) |
paul@96 | 83 | |
paul@96 | 84 | return True |
paul@96 | 85 | |
paul@580 | 86 | def _remove(self, from_organiser=True): |
paul@140 | 87 | |
paul@580 | 88 | """ |
paul@580 | 89 | Remove details from the current object given a message originating |
paul@580 | 90 | from an organiser if 'from_organiser' is set to a true value. |
paul@580 | 91 | """ |
paul@140 | 92 | |
paul@468 | 93 | self.set_identity(from_organiser) |
paul@468 | 94 | |
paul@468 | 95 | # Check for event using UID. |
paul@468 | 96 | |
paul@468 | 97 | if not self.have_new_object(): |
paul@140 | 98 | return False |
paul@140 | 99 | |
paul@527 | 100 | # Obtain any stored object, using parent object details if a newly- |
paul@527 | 101 | # indicated occurrence is referenced. |
paul@312 | 102 | |
paul@606 | 103 | obj = self.get_stored_object_version() |
paul@527 | 104 | old = not obj and self.get_parent_object() or obj |
paul@527 | 105 | |
paul@527 | 106 | if not old: |
paul@420 | 107 | return False |
paul@261 | 108 | |
paul@527 | 109 | # Only cancel the event completely if all attendees are given. |
paul@527 | 110 | |
paul@527 | 111 | attendees = uri_dict(old.get_value_map("ATTENDEE")) |
paul@312 | 112 | all_attendees = set(attendees.keys()) |
paul@312 | 113 | given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) |
paul@502 | 114 | cancel_entire_event = given_attendees == all_attendees |
paul@312 | 115 | |
paul@577 | 116 | # Keep the event for the organiser. |
paul@577 | 117 | |
paul@502 | 118 | if cancel_entire_event: |
paul@468 | 119 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@261 | 120 | |
paul@312 | 121 | # Otherwise, remove the given attendees and update the event. |
paul@312 | 122 | |
paul@527 | 123 | elif obj: |
paul@312 | 124 | for attendee in given_attendees: |
paul@314 | 125 | if attendees.has_key(attendee): |
paul@314 | 126 | del attendees[attendee] |
paul@312 | 127 | obj["ATTENDEE"] = attendees.items() |
paul@264 | 128 | |
paul@312 | 129 | # Update the stored object with sequence information. |
paul@312 | 130 | |
paul@527 | 131 | if obj: |
paul@577 | 132 | obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") or [] |
paul@577 | 133 | obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") or [] |
paul@312 | 134 | |
paul@672 | 135 | # Update free/busy information. |
paul@672 | 136 | |
paul@672 | 137 | if cancel_entire_event or self.user in given_attendees: |
paul@672 | 138 | self.remove_event_from_freebusy() |
paul@672 | 139 | |
paul@527 | 140 | # Set the complete event if not an additional occurrence. For any newly- |
paul@527 | 141 | # indicated occurrence, use the received event details. |
paul@334 | 142 | |
paul@527 | 143 | self.store.set_event(self.user, self.uid, self.recurrenceid, (obj or self.obj).to_node()) |
paul@264 | 144 | |
paul@261 | 145 | # Remove any associated request. |
paul@261 | 146 | |
paul@468 | 147 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@261 | 148 | |
paul@140 | 149 | return True |
paul@140 | 150 | |
paul@96 | 151 | class Event(PersonHandler): |
paul@96 | 152 | |
paul@96 | 153 | "An event handler." |
paul@96 | 154 | |
paul@96 | 155 | def add(self): |
paul@96 | 156 | pass |
paul@96 | 157 | |
paul@96 | 158 | def cancel(self): |
paul@580 | 159 | self._remove(True) |
paul@96 | 160 | |
paul@96 | 161 | def counter(self): |
paul@96 | 162 | pass |
paul@96 | 163 | |
paul@96 | 164 | def declinecounter(self): |
paul@96 | 165 | pass |
paul@96 | 166 | |
paul@96 | 167 | def publish(self): |
paul@580 | 168 | self._record(True) |
paul@96 | 169 | |
paul@96 | 170 | def refresh(self): |
paul@353 | 171 | pass |
paul@96 | 172 | |
paul@96 | 173 | def reply(self): |
paul@580 | 174 | self._record(False) |
paul@96 | 175 | |
paul@96 | 176 | def request(self): |
paul@580 | 177 | self._record(True) |
paul@96 | 178 | |
paul@96 | 179 | # Handler registry. |
paul@96 | 180 | |
paul@96 | 181 | handlers = [ |
paul@96 | 182 | ("VEVENT", Event), |
paul@96 | 183 | ] |
paul@96 | 184 | |
paul@96 | 185 | # vim: tabstop=4 expandtab shiftwidth=4 |