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 107 obj = self.get_object(identity) 108 109 attendees = uri_dict(obj.get_value_map("ATTENDEE")) 110 all_attendees = set(attendees.keys()) 111 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 112 113 if given_attendees == all_attendees: 114 self.store.cancel_event(identity, self.uid) 115 116 # Otherwise, remove the given attendees and update the event. 117 118 else: 119 for attendee in given_attendees: 120 del attendees[attendee] 121 obj["ATTENDEE"] = attendees.items() 122 123 # Update the stored object with sequence information. 124 125 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") 126 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") 127 128 self.store.set_event(identity, self.uid, obj.to_node()) 129 130 # Remove any associated request. 131 132 self.store.dequeue_request(identity, self.uid) 133 134 # Update free/busy information. 135 136 if update_freebusy: 137 freebusy = self.store.get_freebusy(identity) 138 self.remove_from_freebusy(freebusy, identity) 139 140 if self.publisher: 141 self.publisher.set_freebusy(identity, freebusy) 142 143 return True 144 145 class Event(PersonHandler): 146 147 "An event handler." 148 149 def add(self): 150 pass 151 152 def cancel(self): 153 self._remove(True, True) 154 155 def counter(self): 156 pass 157 158 def declinecounter(self): 159 pass 160 161 def publish(self): 162 self._record(True, True) 163 164 def refresh(self): 165 self._record(True, True) 166 167 def reply(self): 168 self._record(False, True) 169 170 def request(self): 171 self._record(True, True) 172 173 class Freebusy(PersonHandler): 174 175 "A free/busy handler." 176 177 def publish(self): 178 pass 179 180 def reply(self): 181 pass 182 183 def request(self): 184 pass 185 186 class Journal(PersonHandler): 187 188 "A journal entry handler." 189 190 def add(self): 191 pass 192 193 def cancel(self): 194 self._remove(True) 195 196 def publish(self): 197 self._record(True) 198 199 class Todo(PersonHandler): 200 201 "A to-do item handler." 202 203 def add(self): 204 pass 205 206 def cancel(self): 207 self._remove(True) 208 209 def counter(self): 210 pass 211 212 def declinecounter(self): 213 pass 214 215 def publish(self): 216 self._record(True) 217 218 def refresh(self): 219 self._record(True) 220 221 def reply(self): 222 self._record(False) 223 224 def request(self): 225 self._record(True) 226 227 # Handler registry. 228 229 handlers = [ 230 ("VFREEBUSY", Freebusy), 231 ("VEVENT", Event), 232 ("VTODO", Todo), 233 ("VJOURNAL", Journal), 234 ] 235 236 # vim: tabstop=4 expandtab shiftwidth=4