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