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