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