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