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