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 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 # 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 cancel_entire_event = given_attendees == all_attendees 137 138 if cancel_entire_event: 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 168 if cancel_entire_event: 169 self.store.set_freebusy(self.user, freebusy) 170 171 if self.publisher and self.is_sharing(): 172 self.publisher.set_freebusy(self.user, freebusy) 173 174 return True 175 176 class Event(PersonHandler): 177 178 "An event handler." 179 180 def add(self): 181 pass 182 183 def cancel(self): 184 self._remove(True, True) 185 186 def counter(self): 187 pass 188 189 def declinecounter(self): 190 pass 191 192 def publish(self): 193 self._record(True, True) 194 195 def refresh(self): 196 pass 197 198 def reply(self): 199 self._record(False, True) 200 201 def request(self): 202 self._record(True, True) 203 204 # Handler registry. 205 206 handlers = [ 207 ("VEVENT", Event), 208 ] 209 210 # vim: tabstop=4 expandtab shiftwidth=4