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