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