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_item, uri_values 24 from imiptools.handlers import Handler 25 26 class PersonHandler(Handler): 27 28 "Handling mechanisms specific to people." 29 30 def set_identity(self, from_organiser=True): 31 32 """ 33 Set the current user for the current object. 34 """ 35 36 self.user, attr = uri_item(self.obj.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) 37 38 def _record(self, from_organiser=True, update_freebusy=False): 39 40 """ 41 Record details from the current object given a message originating 42 from an organiser if 'from_organiser' is set to a true value, updating 43 free/busy information if 'update_freebusy' is set to a true value. 44 """ 45 46 self.set_identity(from_organiser) 47 48 # Check for event using UID. 49 50 if not self.have_new_object(): 51 return False 52 53 # Update the object. 54 55 if from_organiser: 56 57 # Set the complete event or an additional occurrence. 58 59 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 60 61 # Remove additional recurrences if handling a complete event. 62 63 if not self.recurrenceid: 64 self.store.remove_recurrences(self.user, self.uid) 65 66 else: 67 # Obtain valid attendees, merging their attendance with the stored 68 # object. 69 70 attendees = self.require_attendees(from_organiser) 71 self.merge_attendance(attendees) 72 73 # Remove any associated request. 74 75 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 76 77 # Update free/busy information. 78 79 if update_freebusy: 80 81 freebusy = self.store.get_freebusy(self.user) 82 83 # Use the stored event in case the reply is incomplete, as is seen 84 # when Claws sends a REPLY for an object originally employing 85 # recurrence information. 86 87 obj = self.get_object() 88 if not obj: 89 return False # although this should not happen 90 91 # If newer than any old version, discard old details from the 92 # free/busy record and check for suitability. 93 94 # Interpretation of periods can depend on the time zone. 95 96 periods = obj.get_periods(self.get_tzid(), self.get_window_end()) 97 98 # Obtain the attendance attributes for this user, if available. 99 100 attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) 101 self.update_freebusy_for_participant(freebusy, periods, attendees.get(self.user), from_organiser) 102 103 # Remove original recurrence details replaced by additional 104 # recurrences, as well as obsolete additional recurrences. 105 106 self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) 107 self.store.set_freebusy(self.user, freebusy) 108 109 if self.publisher and self.is_sharing(): 110 self.publisher.set_freebusy(self.user, freebusy) 111 112 return True 113 114 def _remove(self, from_organiser=True, update_freebusy=False): 115 116 "Remove free/busy information for any unprocessed object." 117 118 self.set_identity(from_organiser) 119 120 # Check for event using UID. 121 122 if not self.have_new_object(): 123 return False 124 125 # Obtain any stored object, using parent object details if a newly- 126 # indicated occurrence is referenced. 127 128 obj = self.get_object() 129 old = not obj and self.get_parent_object() or obj 130 131 if not old: 132 return False 133 134 # Only cancel the event completely if all attendees are given. 135 136 attendees = uri_dict(old.get_value_map("ATTENDEE")) 137 all_attendees = set(attendees.keys()) 138 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 139 cancel_entire_event = given_attendees == all_attendees 140 141 if cancel_entire_event: 142 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 143 144 # Otherwise, remove the given attendees and update the event. 145 146 elif obj: 147 for attendee in given_attendees: 148 if attendees.has_key(attendee): 149 del attendees[attendee] 150 obj["ATTENDEE"] = attendees.items() 151 152 # Update the stored object with sequence information. 153 154 if obj: 155 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") 156 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") 157 158 # Set the complete event if not an additional occurrence. For any newly- 159 # indicated occurrence, use the received event details. 160 161 self.store.set_event(self.user, self.uid, self.recurrenceid, (obj or self.obj).to_node()) 162 163 # Remove any associated request. 164 165 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 166 167 # Update free/busy information. 168 169 if update_freebusy: 170 freebusy = self.store.get_freebusy(self.user) 171 self.remove_from_freebusy(freebusy) 172 173 if cancel_entire_event or self.user in given_attendees: 174 self.store.set_freebusy(self.user, freebusy) 175 176 if self.publisher and self.is_sharing(): 177 self.publisher.set_freebusy(self.user, freebusy) 178 179 return True 180 181 class Event(PersonHandler): 182 183 "An event handler." 184 185 def add(self): 186 pass 187 188 def cancel(self): 189 self._remove(True, True) 190 191 def counter(self): 192 pass 193 194 def declinecounter(self): 195 pass 196 197 def publish(self): 198 self._record(True, True) 199 200 def refresh(self): 201 pass 202 203 def reply(self): 204 self._record(False, True) 205 206 def request(self): 207 self._record(True, True) 208 209 # Handler registry. 210 211 handlers = [ 212 ("VEVENT", Event), 213 ] 214 215 # vim: tabstop=4 expandtab shiftwidth=4