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.content import Handler 24 from imiptools.data import get_window_end, uri_dict, uri_item, uri_values 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 "Record free/busy and object information." 49 50 item = self._get_identity(from_organiser) 51 if not item: 52 return False 53 54 identity, attr = item 55 56 # Update the object. 57 58 if from_organiser: 59 60 # Remove additional occurrences if a complete event is moved. 61 62 has_moved = self.handle_moved_event(identity) 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 else: 69 has_moved = False 70 organiser_item, attendees = self.require_organiser_and_attendees(from_organiser) 71 self.merge_attendance(attendees, identity) 72 73 # Remove any associated request. 74 75 self.store.dequeue_request(identity, self.uid, self.recurrenceid) 76 77 # Detach any recurrence from its parent. 78 79 self.detach_recurrence(identity) 80 81 # Update free/busy information. 82 83 if update_freebusy: 84 85 freebusy = self.store.get_freebusy(identity) 86 87 # Interpretation of periods can depend on the time zone. 88 89 tzid = self.get_tzid(identity) 90 91 # Use the stored event in case the reply is incomplete, as is seen 92 # when Claws sends a REPLY for an object originally employing 93 # recurrence information. Additionally, parent object details will 94 # also be consulted if available. 95 96 obj = self.get_object(identity) 97 98 # If newer than any old version, discard old details from the 99 # free/busy record and check for suitability. 100 101 periods = obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) 102 103 self.update_freebusy_for_participant(freebusy, periods, attr, 104 from_organiser and self.is_not_attendee(identity, obj), 105 has_moved) 106 107 self.remove_freebusy_for_original_recurrence(freebusy) 108 self.store.set_freebusy(identity, freebusy) 109 110 if self.publisher: 111 self.publisher.set_freebusy(identity, 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 item = self._get_identity(from_organiser) 120 if not item: 121 return False 122 123 identity, attr = item 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(identity) 130 131 attendees = uri_dict(obj.get_value_map("ATTENDEE")) 132 all_attendees = set(attendees.keys()) 133 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 134 135 if given_attendees == all_attendees: 136 self.store.cancel_event(identity, self.uid, self.recurrenceid) 137 138 # Otherwise, remove the given attendees and update the event. 139 140 else: 141 for attendee in given_attendees: 142 if attendees.has_key(attendee): 143 del attendees[attendee] 144 obj["ATTENDEE"] = attendees.items() 145 146 # Update the stored object with sequence information. 147 148 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") 149 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") 150 151 # Set the complete event if not an additional occurrence. 152 153 self.store.set_event(identity, self.uid, self.recurrenceid, obj.to_node()) 154 155 # Remove any associated request. 156 157 self.store.dequeue_request(identity, self.uid, self.recurrenceid) 158 159 # Update free/busy information. 160 161 if update_freebusy: 162 freebusy = self.store.get_freebusy(identity) 163 self.remove_from_freebusy(freebusy) 164 self.store.set_freebusy(identity, freebusy) 165 166 if self.publisher: 167 self.publisher.set_freebusy(identity, freebusy) 168 169 return True 170 171 class Event(PersonHandler): 172 173 "An event handler." 174 175 def add(self): 176 pass 177 178 def cancel(self): 179 self._remove(True, True) 180 181 def counter(self): 182 pass 183 184 def declinecounter(self): 185 pass 186 187 def publish(self): 188 self._record(True, True) 189 190 def refresh(self): 191 pass 192 193 def reply(self): 194 self._record(False, True) 195 196 def request(self): 197 self._record(True, True) 198 199 class Freebusy(PersonHandler): 200 201 "A free/busy handler." 202 203 def publish(self): 204 pass 205 206 def reply(self): 207 pass 208 209 def request(self): 210 pass 211 212 class Journal(PersonHandler): 213 214 "A journal entry handler." 215 216 def add(self): 217 pass 218 219 def cancel(self): 220 self._remove(True) 221 222 def publish(self): 223 self._record(True) 224 225 class Todo(PersonHandler): 226 227 "A to-do item handler." 228 229 def add(self): 230 pass 231 232 def cancel(self): 233 self._remove(True) 234 235 def counter(self): 236 pass 237 238 def declinecounter(self): 239 pass 240 241 def publish(self): 242 self._record(True) 243 244 def refresh(self): 245 pass 246 247 def reply(self): 248 self._record(False) 249 250 def request(self): 251 self._record(True) 252 253 # Handler registry. 254 255 handlers = [ 256 ("VFREEBUSY", Freebusy), 257 ("VEVENT", Event), 258 ("VTODO", Todo), 259 ("VJOURNAL", Journal), 260 ] 261 262 # vim: tabstop=4 expandtab shiftwidth=4