1 #!/usr/bin/env python 2 3 """ 4 Handlers for a person for whom scheduling is performed, inspecting outgoing 5 messages to obtain scheduling done externally. 6 7 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from imiptools.data import uri_dict, uri_values 24 from imiptools.handlers import Handler 25 from imiptools.handlers.common import CommonEvent 26 27 class PersonHandler(CommonEvent, Handler): 28 29 "Handling mechanisms specific to people." 30 31 def _add(self): 32 33 "Add a recurrence for the current object." 34 35 # Check for event using UID. 36 37 if not self.have_new_object(): 38 return False 39 40 # Ignore unknown objects. 41 42 if not self.get_stored_object_version(): 43 return 44 45 # Record the event as a recurrence of the parent object. 46 47 self.update_recurrenceid() 48 49 # Set the additional occurrence. 50 51 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 52 53 # Update free/busy information. 54 55 self.update_event_in_freebusy() 56 57 return True 58 59 def _record(self, from_organiser=True, counter=False): 60 61 """ 62 Record details from the current object given a message originating 63 from an organiser if 'from_organiser' is set to a true value. 64 """ 65 66 # Check for a new event, tolerating not-strictly-new events if the 67 # attendee is responding. 68 69 if not self.have_new_object(strict=from_organiser): 70 return False 71 72 # Update the object. 73 74 if from_organiser: 75 76 # Set the complete event or an additional occurrence. 77 78 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 79 80 # Remove additional recurrences if handling a complete event. 81 # Also remove any previous cancellations involving this event. 82 83 if not self.recurrenceid: 84 self.store.remove_recurrences(self.user, self.uid) 85 self.store.remove_cancellations(self.user, self.uid) 86 else: 87 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 88 89 else: 90 # Obtain valid attendees, merging their attendance with the stored 91 # object. 92 93 attendees = self.require_attendees(from_organiser) 94 self.merge_attendance(attendees) 95 96 # Remove any associated request. 97 98 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 99 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 100 101 # Update free/busy information. 102 103 if not counter: 104 self.update_event_in_freebusy(from_organiser) 105 106 # For countered proposals, record the offer in the resource's 107 # free/busy collection. 108 109 else: 110 self.update_event_in_freebusy_offers() 111 112 return True 113 114 def _remove(self): 115 116 """ 117 Remove details from the current object given a message originating 118 from an organiser if 'from_organiser' is set to a true value. 119 """ 120 121 # Check for event using UID. 122 123 if not self.have_new_object(): 124 return False 125 126 # Obtain any stored object, using parent object details if a newly- 127 # indicated occurrence is referenced. 128 129 obj = self.get_stored_object_version() 130 old = not obj and self.get_parent_object() or obj 131 132 if not old: 133 return False 134 135 # Only cancel the event completely if all attendees are given. 136 137 attendees = uri_dict(old.get_value_map("ATTENDEE")) 138 all_attendees = set(attendees.keys()) 139 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 140 cancel_entire_event = not all_attendees.difference(given_attendees) 141 142 # Update the recipient's record of the organiser's schedule. 143 144 self.remove_freebusy_from_organiser(self.obj.get_value("ORGANIZER")) 145 146 # Otherwise, remove the given attendees and update the event. 147 148 if not cancel_entire_event and obj: 149 for attendee in given_attendees: 150 if attendees.has_key(attendee): 151 del attendees[attendee] 152 obj["ATTENDEE"] = attendees.items() 153 154 # Update the stored object with sequence information. 155 156 if obj: 157 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") or [] 158 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") or [] 159 160 # Update free/busy information. 161 162 if cancel_entire_event or self.user in given_attendees: 163 self.remove_event_from_freebusy() 164 165 # Set the complete event if not an additional occurrence. For any newly- 166 # indicated occurrence, use the received event details. 167 168 self.store.set_event(self.user, self.uid, self.recurrenceid, (obj or self.obj).to_node()) 169 170 # Perform any cancellation after recording the latest state of the 171 # event. 172 173 if cancel_entire_event: 174 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 175 176 # Remove any associated request. 177 178 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 179 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 180 181 return True 182 183 def _declinecounter(self): 184 185 "Remove any counter-proposals for the given event." 186 187 # Check for event using UID. 188 189 if not self.have_new_object(): 190 return False 191 192 self.remove_counters(uri_values(self.obj.get_values("ATTENDEE"))) 193 194 class Event(PersonHandler): 195 196 "An event handler." 197 198 def add(self): 199 200 "Record the addition of a recurrence to an event." 201 202 self._add() 203 204 def cancel(self): 205 206 "Remove an event or a recurrence." 207 208 self._remove() 209 210 def counter(self): 211 212 "Record an offer made by a counter-proposal." 213 214 self._record(False, True) 215 216 def declinecounter(self): 217 218 "Expire any offer made by a counter-proposal." 219 220 self._declinecounter() 221 222 def publish(self): 223 224 "Published events are recorded." 225 226 self._record(True) 227 228 def refresh(self): 229 230 "Requests to refresh events do not provide event information." 231 232 pass 233 234 def reply(self): 235 236 "Replies to requests are inspected for attendee information." 237 238 self._record(False) 239 240 def request(self): 241 242 "Record events sent for potential scheduling." 243 244 self._record(True) 245 246 # Handler registry. 247 248 handlers = [ 249 ("VEVENT", Event), 250 ] 251 252 # vim: tabstop=4 expandtab shiftwidth=4