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 from imiptools.dates import format_datetime, get_default_timezone, to_timezone 26 from imiptools.profile import Preferences 27 28 class PersonHandler(Handler): 29 30 "Handling mechanisms specific to people." 31 32 def _get_identity(self, from_organiser=True): 33 34 """ 35 Get the identity of interest in a usable form for any unprocessed 36 object. 37 """ 38 39 identity, attr = item = uri_item(self.obj.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) 40 41 # Check for event using UID. 42 43 if not self.have_new_object(identity): 44 return None 45 46 return item 47 48 def _record(self, from_organiser=True, update_freebusy=False): 49 50 "Record free/busy and object information." 51 52 item = self._get_identity(from_organiser) 53 if not item: 54 return False 55 56 identity, attr = item 57 58 # Update the object. 59 60 if from_organiser: 61 62 # Set the complete event if not an additional occurrence. 63 64 event = self.obj.to_node() 65 self.store.set_event(identity, self.uid, self.recurrenceid, event) 66 67 else: 68 organiser_item, attendees = self.require_organiser_and_attendees(from_organiser) 69 self.merge_attendance(attendees, identity) 70 71 # Remove any associated request. 72 73 self.store.dequeue_request(identity, self.uid, self.recurrenceid) 74 75 # Interpretation of periods can depend on the time zone. 76 77 preferences = Preferences(identity) 78 tzid = preferences.get("TZID") or get_default_timezone() 79 80 # Where a recurring object is updated by a specific occurrence, the 81 # details of the recurring "parent" must be changed. 82 83 if self.recurrenceid: 84 obj = self.get_parent_object(identity) 85 recurrence = self.obj.get_datetime("RECURRENCE-ID") 86 87 if obj.has_recurrence(tzid, recurrence): 88 item = obj.get_item("EXDATE") 89 if item: 90 exdates, exdate_attr = item 91 if not isinstance(exdates, list): 92 exdates = [exdates] 93 else: 94 exdates, exdate_attr = [], {} 95 96 # Convert the occurrence to the same time regime as the other 97 # exceptions. 98 99 exdate_tzid = exdate_attr.get("TZID") 100 exdate = recurrence 101 if exdate_tzid: 102 exdate = to_timezone(exdate, exdate_tzid) 103 else: 104 exdate = to_timezone(exdate, "UTC") 105 106 # Update the exceptions and store the modified parent event. 107 108 exdates.append(format_datetime(exdate)) 109 obj["EXDATE"] = [(exdates, exdate_attr)] 110 111 self.store.set_event(identity, self.uid, None, obj.to_node()) 112 113 # Update free/busy information. 114 115 if update_freebusy: 116 117 freebusy = self.store.get_freebusy(identity) 118 119 # Use the stored event in case the reply is incomplete, as is seen 120 # when Claws sends a REPLY for an object originally employing 121 # recurrence information. Additionally, parent object details will 122 # also be consulted if available. 123 124 obj = self.get_object(identity) 125 126 # If newer than any old version, discard old details from the 127 # free/busy record and check for suitability. 128 129 periods = obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) 130 131 if attr.get("PARTSTAT") != "DECLINED": 132 self.update_freebusy(freebusy, identity, periods) 133 else: 134 self.remove_from_freebusy(freebusy, identity) 135 136 # For any parent object, refresh the updated periods. 137 138 if self.recurrenceid: 139 obj = self.get_parent_object(identity) 140 periods = obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) 141 142 self._update_freebusy(freebusy, identity, periods, None) 143 144 if self.publisher: 145 self.publisher.set_freebusy(identity, freebusy) 146 147 return True 148 149 def _remove(self, from_organiser=True, update_freebusy=False): 150 151 "Remove free/busy information for any unprocessed object." 152 153 item = self._get_identity(from_organiser) 154 if not item: 155 return False 156 157 identity, attr = item 158 159 # Only cancel the event completely if all attendees are given. 160 # NOTE: Need to also check for recurrence identifiers and selective 161 # NOTE: cancellations. 162 163 obj = self.get_object(identity) 164 165 attendees = uri_dict(obj.get_value_map("ATTENDEE")) 166 all_attendees = set(attendees.keys()) 167 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 168 169 if given_attendees == all_attendees: 170 self.store.cancel_event(identity, self.uid, self.recurrenceid) 171 172 # Otherwise, remove the given attendees and update the event. 173 174 else: 175 for attendee in given_attendees: 176 if attendees.has_key(attendee): 177 del attendees[attendee] 178 obj["ATTENDEE"] = attendees.items() 179 180 # Update the stored object with sequence information. 181 182 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") 183 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") 184 185 # Set the complete event if not an additional occurrence. 186 187 event = obj.to_node() 188 self.store.set_event(identity, self.uid, self.recurrenceid, event) 189 190 # Remove any associated request. 191 192 self.store.dequeue_request(identity, self.uid, self.recurrenceid) 193 194 # Update free/busy information. 195 196 if update_freebusy: 197 freebusy = self.store.get_freebusy(identity) 198 self.remove_from_freebusy(freebusy, identity) 199 200 if self.publisher: 201 self.publisher.set_freebusy(identity, freebusy) 202 203 return True 204 205 class Event(PersonHandler): 206 207 "An event handler." 208 209 def add(self): 210 pass 211 212 def cancel(self): 213 self._remove(True, True) 214 215 def counter(self): 216 pass 217 218 def declinecounter(self): 219 pass 220 221 def publish(self): 222 self._record(True, True) 223 224 def refresh(self): 225 pass 226 227 def reply(self): 228 self._record(False, True) 229 230 def request(self): 231 self._record(True, True) 232 233 class Freebusy(PersonHandler): 234 235 "A free/busy handler." 236 237 def publish(self): 238 pass 239 240 def reply(self): 241 pass 242 243 def request(self): 244 pass 245 246 class Journal(PersonHandler): 247 248 "A journal entry handler." 249 250 def add(self): 251 pass 252 253 def cancel(self): 254 self._remove(True) 255 256 def publish(self): 257 self._record(True) 258 259 class Todo(PersonHandler): 260 261 "A to-do item handler." 262 263 def add(self): 264 pass 265 266 def cancel(self): 267 self._remove(True) 268 269 def counter(self): 270 pass 271 272 def declinecounter(self): 273 pass 274 275 def publish(self): 276 self._record(True) 277 278 def refresh(self): 279 pass 280 281 def reply(self): 282 self._record(False) 283 284 def request(self): 285 self._record(True) 286 287 # Handler registry. 288 289 handlers = [ 290 ("VFREEBUSY", Freebusy), 291 ("VEVENT", Event), 292 ("VTODO", Todo), 293 ("VJOURNAL", Journal), 294 ] 295 296 # vim: tabstop=4 expandtab shiftwidth=4