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