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