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