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