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, 101 from_organiser and not self.is_attendee(self.user, obj)) 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