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