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 26 class PersonHandler(Handler): 27 28 "Handling mechanisms specific to people." 29 30 def _get_identity(self, from_organiser=True): 31 32 """ 33 Get the identity of interest in a usable form for any unprocessed 34 object. 35 """ 36 37 identity, attr = item = uri_item(self.obj.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) 38 39 # Check for event using UID. 40 41 if not self.have_new_object(identity): 42 return None 43 44 return item 45 46 def _record(self, from_organiser=True, update_freebusy=False): 47 48 "Record free/busy and object information." 49 50 item = self._get_identity(from_organiser) 51 if not item: 52 return False 53 54 identity, attr = item 55 56 # Update the object. 57 58 if from_organiser: 59 self.store.set_event(identity, self.uid, self.obj.to_node()) 60 else: 61 organiser_item, attendees = self.require_organiser_and_attendees(from_organiser) 62 self.merge_attendance(attendees, identity) 63 64 # Remove any associated request. 65 66 self.store.dequeue_request(identity, self.uid) 67 68 # Update free/busy information. 69 70 if update_freebusy: 71 72 # If newer than any old version, discard old details from the 73 # free/busy record and check for suitability. 74 75 periods = self.obj.get_periods() 76 freebusy = self.store.get_freebusy(identity) 77 78 if from_organiser or attr.get("PARTSTAT") != "DECLINED": 79 self.update_freebusy(freebusy, identity, periods) 80 else: 81 self.remove_from_freebusy(freebusy, identity) 82 83 if self.publisher: 84 self.publisher.set_freebusy(identity, freebusy) 85 86 return True 87 88 def _remove(self, from_organiser=True, update_freebusy=False): 89 90 "Remove free/busy information for any unprocessed object." 91 92 item = self._get_identity(from_organiser) 93 if not item: 94 return False 95 96 identity, attr = item 97 98 # Store the object, updating any sequence information. 99 100 self.store.set_event(identity, self.uid, self.obj.to_node()) 101 102 # Cancel the event. 103 104 self.store.cancel_event(identity, self.uid) 105 106 # Remove any associated request. 107 108 self.store.dequeue_request(identity, self.uid) 109 110 # Update free/busy information. 111 112 if update_freebusy: 113 freebusy = self.store.get_freebusy(identity) 114 self.remove_from_freebusy(freebusy, identity) 115 116 if self.publisher: 117 self.publisher.set_freebusy(identity, freebusy) 118 119 return True 120 121 class Event(PersonHandler): 122 123 "An event handler." 124 125 def add(self): 126 pass 127 128 def cancel(self): 129 self._remove(True, True) 130 131 def counter(self): 132 pass 133 134 def declinecounter(self): 135 pass 136 137 def publish(self): 138 self._record(True, True) 139 140 def refresh(self): 141 self._record(True, True) 142 143 def reply(self): 144 self._record(False, True) 145 146 def request(self): 147 self._record(True, True) 148 149 class Freebusy(PersonHandler): 150 151 "A free/busy handler." 152 153 def publish(self): 154 pass 155 156 def reply(self): 157 pass 158 159 def request(self): 160 pass 161 162 class Journal(PersonHandler): 163 164 "A journal entry handler." 165 166 def add(self): 167 pass 168 169 def cancel(self): 170 self._remove(True) 171 172 def publish(self): 173 self._record(True) 174 175 class Todo(PersonHandler): 176 177 "A to-do item handler." 178 179 def add(self): 180 pass 181 182 def cancel(self): 183 self._remove(True) 184 185 def counter(self): 186 pass 187 188 def declinecounter(self): 189 pass 190 191 def publish(self): 192 self._record(True) 193 194 def refresh(self): 195 self._record(True) 196 197 def reply(self): 198 self._record(False) 199 200 def request(self): 201 self._record(True) 202 203 # Handler registry. 204 205 handlers = [ 206 ("VFREEBUSY", Freebusy), 207 ("VEVENT", Event), 208 ("VTODO", Todo), 209 ("VJOURNAL", Journal), 210 ] 211 212 # vim: tabstop=4 expandtab shiftwidth=4