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