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_for_freebusy(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 either original recurrence or additional recurrence 104 # details depending on whether an additional recurrence or a 105 # complete event are being handled, respectively. 106 107 self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) 108 self.store.set_freebusy(self.user, freebusy) 109 110 if self.publisher and self.is_sharing(): 111 self.publisher.set_freebusy(self.user, freebusy) 112 113 return True 114 115 def _remove(self, from_organiser=True, update_freebusy=False): 116 117 "Remove free/busy information for any unprocessed object." 118 119 self.set_identity(from_organiser) 120 121 # Check for event using UID. 122 123 if not self.have_new_object(): 124 return False 125 126 # Only cancel the event completely if all attendees are given. 127 # NOTE: Need to also check for recurrence identifiers and selective 128 # NOTE: cancellations. 129 130 obj = self.get_object() 131 if not obj: 132 return False 133 134 attendees = uri_dict(obj.get_value_map("ATTENDEE")) 135 all_attendees = set(attendees.keys()) 136 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 137 138 if given_attendees == all_attendees: 139 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 140 141 # Otherwise, remove the given attendees and update the event. 142 143 else: 144 for attendee in given_attendees: 145 if attendees.has_key(attendee): 146 del attendees[attendee] 147 obj["ATTENDEE"] = attendees.items() 148 149 # Update the stored object with sequence information. 150 151 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") 152 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") 153 154 # Set the complete event if not an additional occurrence. 155 156 self.store.set_event(self.user, self.uid, self.recurrenceid, obj.to_node()) 157 158 # Remove any associated request. 159 160 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 161 162 # Update free/busy information. 163 164 if update_freebusy: 165 freebusy = self.store.get_freebusy(self.user) 166 self.remove_from_freebusy(freebusy) 167 self.store.set_freebusy(self.user, freebusy) 168 169 if self.publisher and self.is_sharing(): 170 self.publisher.set_freebusy(self.user, freebusy) 171 172 return True 173 174 class Event(PersonHandler): 175 176 "An event handler." 177 178 def add(self): 179 pass 180 181 def cancel(self): 182 self._remove(True, True) 183 184 def counter(self): 185 pass 186 187 def declinecounter(self): 188 pass 189 190 def publish(self): 191 self._record(True, True) 192 193 def refresh(self): 194 pass 195 196 def reply(self): 197 self._record(False, True) 198 199 def request(self): 200 self._record(True, True) 201 202 class Freebusy(PersonHandler): 203 204 "A free/busy handler." 205 206 def publish(self): 207 pass 208 209 def reply(self): 210 pass 211 212 def request(self): 213 pass 214 215 # Handler registry. 216 217 handlers = [ 218 ("VFREEBUSY", Freebusy), 219 ("VEVENT", Event), 220 ] 221 222 # vim: tabstop=4 expandtab shiftwidth=4