1 #!/usr/bin/env python 2 3 """ 4 Handlers for a person for whom scheduling is performed. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.content import Handler, get_address, get_uri, to_part, uri_dict, uri_items 23 from imiptools.handlers.common import CommonFreebusy 24 from imiptools.profile import Preferences 25 from vCalendar import to_node 26 27 class PersonHandler(Handler): 28 29 "Handling mechanisms specific to people." 30 31 def _record_and_deliver(self, objtype, from_organiser=True, queue=False, cancel=False): 32 33 oa = self.require_organiser_and_attendees(from_organiser) 34 if not oa: 35 return False 36 37 (organiser, organiser_attr), attendees = organiser_item, attendees = oa 38 39 # Handle notifications and invitations. 40 41 if from_organiser: 42 43 # Process each attendee separately. 44 45 for attendee, attendee_attr in attendees.items(): 46 47 if not self.have_new_object(attendee, objtype): 48 continue 49 50 # Record other party free/busy information. 51 52 if organiser != attendee: 53 freebusy = self.store.get_freebusy_for_other(attendee, organiser) 54 55 if organiser_attr.get("PARTSTAT") != "DECLINED": 56 self.update_freebusy_for_other(freebusy, attendee, organiser, self.get_periods()) 57 else: 58 self.remove_from_freebusy_for_other(freebusy, attendee, organiser) 59 60 # Store the object and queue any request. 61 62 self.store.set_event(attendee, self.uid, to_node( 63 {objtype : [(self.details, {})]} 64 )) 65 66 if queue: 67 self.store.queue_request(attendee, self.uid) 68 elif cancel: 69 self.store.cancel_event(attendee, self.uid) 70 71 # As organiser, update attendance. 72 73 else: 74 obj = self.get_object(organiser, objtype) 75 76 if obj and self.have_new_object(organiser, objtype, obj): 77 78 # Get attendee details in a usable form. 79 80 attendee_map = uri_dict(self.get_value_map("ATTENDEE")) 81 82 for attendee, attendee_attr in attendees.items(): 83 84 # Update attendance in the loaded object. 85 86 attendee_map[attendee] = attendee_attr 87 88 # Record other party free/busy information. 89 90 if organiser != attendee: 91 freebusy = self.store.get_freebusy_for_other(organiser, attendee) 92 93 if attendee_attr.get("PARTSTAT") != "DECLINED": 94 self.update_freebusy_for_other(freebusy, organiser, attendee, self.get_periods()) 95 else: 96 self.remove_from_freebusy_for_other(freebusy, organiser, attendee) 97 98 # Set the new details and store the object. 99 100 obj["ATTENDEE"] = attendee_map.items() 101 102 self.store.set_event(organiser, self.uid, to_node( 103 {objtype : [(obj, {})]} 104 )) 105 106 return True 107 108 def _record_freebusy(self, from_organiser=True): 109 110 "Record free/busy information for the received information." 111 112 freebusy = [] 113 114 for value in self.get_values("FREEBUSY") or []: 115 if not isinstance(value, list): 116 value = [value] 117 for v in value: 118 try: 119 start, end = v.split("/", 1) 120 freebusy.append((start, end)) 121 except ValueError: 122 pass 123 124 for sender, sender_attr in uri_items(self.get_items(from_organiser and "ORGANIZER" or "ATTENDEE")): 125 self.store.set_freebusy_for_other(get_uri(self.recipient), freebusy, sender) 126 127 class Event(PersonHandler): 128 129 "An event handler." 130 131 def add(self): 132 133 # NOTE: Queue a suggested modification to any active event. 134 135 return self.wrap("An addition to an event has been received.", link=False) 136 137 def cancel(self): 138 139 "Queue a cancellation of any active event." 140 141 self._record_and_deliver("VEVENT", from_organiser=True, queue=False, cancel=True) 142 return self.wrap("A cancellation has been received.", link=False) 143 144 def counter(self): 145 146 # NOTE: Queue a suggested modification to any active event. 147 148 return self.wrap("A counter proposal has been received.", link=False) 149 150 def declinecounter(self): 151 152 # NOTE: Queue a suggested modification to any active event. 153 154 return self.wrap("A declining counter proposal has been received.", link=False) 155 156 def publish(self): 157 158 "Register details of any relevant event." 159 160 self._record_and_deliver("VEVENT", from_organiser=True, queue=False) 161 return self.wrap("Details of an event have been received.") 162 163 def refresh(self): 164 165 "Update details of any active event." 166 167 self._record_and_deliver("VEVENT", from_organiser=True, queue=False) 168 return self.wrap("An event update has been received.") 169 170 def reply(self): 171 172 "Record replies and notify the recipient." 173 174 self._record_and_deliver("VEVENT", from_organiser=False, queue=False) 175 return self.wrap("A reply has been received.") 176 177 def request(self): 178 179 "Hold requests and notify the recipient." 180 181 self._record_and_deliver("VEVENT", from_organiser=True, queue=True) 182 return self.wrap("A request has been received.") 183 184 class Freebusy(PersonHandler, CommonFreebusy): 185 186 "A free/busy handler." 187 188 def publish(self): 189 190 "Register free/busy information." 191 192 self._record_freebusy(from_organiser=True) 193 194 # Produce a message if configured to do so. 195 196 preferences = Preferences(get_uri(self.recipient)) 197 if preferences.get("freebusy_messages") == "notify": 198 return self.wrap("A free/busy update has been received.", link=False) 199 200 def reply(self): 201 202 "Record replies and notify the recipient." 203 204 self._record_freebusy(from_organiser=False) 205 206 # Produce a message if configured to do so. 207 208 preferences = Preferences(get_uri(self.recipient)) 209 if preferences.get("freebusy_messages") == "notify": 210 return self.wrap("A reply to a free/busy request has been received.", link=False) 211 212 def request(self): 213 214 """ 215 Respond to a request by preparing a reply containing free/busy 216 information for each indicated attendee. 217 """ 218 219 # Produce a reply if configured to do so. 220 221 preferences = Preferences(get_uri(self.recipient)) 222 if preferences.get("freebusy_sharing") == "share": 223 return CommonFreebusy.request(self) 224 225 class Journal(PersonHandler): 226 227 "A journal entry handler." 228 229 def add(self): 230 231 # NOTE: Queue a suggested modification to any active entry. 232 233 return self.wrap("An addition to a journal entry has been received.", link=False) 234 235 def cancel(self): 236 237 # NOTE: Queue a suggested modification to any active entry. 238 239 return self.wrap("A cancellation has been received.", link=False) 240 241 def publish(self): 242 243 # NOTE: Register details of any relevant entry. 244 245 self._record_and_deliver("VJOURNAL", from_organiser=True, queue=False) 246 return self.wrap("Details of a journal entry have been received.") 247 248 class Todo(PersonHandler): 249 250 "A to-do item handler." 251 252 def add(self): 253 254 # NOTE: Queue a suggested modification to any active item. 255 256 return self.wrap("An addition to an item has been received.", link=False) 257 258 def cancel(self): 259 260 # NOTE: Queue a suggested modification to any active item. 261 262 return self.wrap("A cancellation has been received.", link=False) 263 264 def counter(self): 265 266 # NOTE: Queue a suggested modification to any active item. 267 268 return self.wrap("A counter proposal has been received.", link=False) 269 270 def declinecounter(self): 271 272 # NOTE: Queue a suggested modification to any active item. 273 274 return self.wrap("A declining counter proposal has been received.", link=False) 275 276 def publish(self): 277 278 "Register details of any relevant item." 279 280 self._record_and_deliver("VTODO", from_organiser=True, queue=False) 281 return self.wrap("Details of an item have been received.") 282 283 def refresh(self): 284 285 "Update details of any active item." 286 287 self._record_and_deliver("VTODO", from_organiser=True, queue=False) 288 return self.wrap("An item update has been received.") 289 290 def reply(self): 291 292 "Record replies and notify the recipient." 293 294 self._record_and_deliver("VTODO", from_organiser=False, queue=False) 295 return self.wrap("A reply has been received.") 296 297 def request(self): 298 299 "Hold requests and notify the recipient." 300 301 self._record_and_deliver("VTODO", from_organiser=True, queue=True) 302 return self.wrap("A request has been received.") 303 304 # Handler registry. 305 306 handlers = [ 307 ("VFREEBUSY", Freebusy), 308 ("VEVENT", Event), 309 ("VTODO", Todo), 310 ("VJOURNAL", Journal), 311 ] 312 313 # vim: tabstop=4 expandtab shiftwidth=4