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