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