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