1 #!/usr/bin/env python 2 3 """ 4 Handlers for a person for whom scheduling is performed. 5 6 Copyright (C) 2014, 2015, 2016 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.data import get_address, uri_dict 23 from imiptools.handlers import Handler 24 from imiptools.handlers.common import CommonFreebusy, CommonEvent 25 26 class PersonHandler(CommonEvent, Handler): 27 28 "Event handling mechanisms specific to people." 29 30 def _process(self, handle, from_organiser=True, **kw): 31 32 """ 33 Obtain valid organiser and attendee details in order to invoke the given 34 'handle' callable, with 'from_organiser' being indicated to obtain the 35 details. Any additional keyword arguments will be passed to 'handle'. 36 """ 37 38 oa = self.require_organiser_and_attendees(from_organiser) 39 if not oa: 40 return False 41 42 (organiser, organiser_attr), attendees = oa 43 return handle(organiser, attendees, **kw) 44 45 def _add(self, organiser, attendees, queue=True): 46 47 """ 48 Add an event occurrence for the current object or produce a response 49 that requests the event details to be sent again. 50 """ 51 52 # Request details where configured, doing so for unknown objects anyway. 53 54 if self.will_refresh(): 55 self.make_refresh() 56 return 57 58 # Record the event as a recurrence of the parent object. 59 60 self.update_recurrenceid() 61 62 # Update the recipient's record of the organiser's schedule. 63 64 self.update_freebusy_from_organiser(organiser) 65 66 # Set the additional occurrence. 67 68 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 69 70 # Remove any previous cancellations involving this event. 71 72 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 73 74 # Queue any request, if appropriate. 75 76 if queue: 77 self.store.queue_request(self.user, self.uid, self.recurrenceid) 78 79 return True 80 81 def _counter(self, organiser, attendees): 82 83 """ 84 Record details from a counter-proposal, updating the stored object with 85 attendance information. 86 """ 87 88 # Update the attendance for the sender. 89 90 attendee = self.get_sending_attendee() 91 if not attendee: 92 return False 93 94 self.merge_attendance({attendee : attendees[attendee]}) 95 96 # Queue any counter-proposal for perusal. 97 98 self.store.set_counter(self.user, attendee, self.obj.to_node(), self.uid, self.recurrenceid) 99 self.store.queue_request(self.user, self.uid, self.recurrenceid, "COUNTER") 100 101 return True 102 103 def _cancel(self): 104 105 "Record an event cancellation." 106 107 # Handle an event being published by the sender to themself. 108 109 organiser_item = self.require_organiser() 110 if organiser_item: 111 organiser, organiser_attr = organiser_item 112 if self.user == organiser: 113 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 114 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 115 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 116 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 117 self.remove_event_from_freebusy() 118 self.remove_freebusy_from_attendees(uri_dict(self.obj.get_value_map("ATTENDEE"))) 119 return True 120 121 return self._process(self._schedule_for_attendee, queue=False, cancel=True) 122 123 def _declinecounter(self, organiser, attendees): 124 125 "Revoke any counter-proposal recorded as a free/busy offer." 126 127 self.remove_event_from_freebusy_offers() 128 return True 129 130 def _publish(self): 131 132 "Record details of a published event." 133 134 # Handle an event being published by the sender to themself. 135 136 organiser_item = self.require_organiser() 137 if organiser_item: 138 organiser, organiser_attr = organiser_item 139 if self.user == organiser: 140 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 141 self.update_event_in_freebusy() 142 return True 143 144 return self._process(self._schedule_for_attendee, queue=False) 145 146 def _schedule_for_attendee(self, organiser, attendees, queue=False, cancel=False): 147 148 """ 149 Record details from the current object given a message originating 150 from an organiser if 'from_organiser' is set to a true value, queuing a 151 request if 'queue' is set to a true value, or cancelling an event if 152 'cancel' is set to a true value. 153 """ 154 155 # Process for the current user, an attendee. 156 157 if not self.have_new_object(): 158 return False 159 160 # Remove additional recurrences if handling a complete event. 161 # Also remove any previous cancellations involving this event. 162 163 if not self.recurrenceid: 164 self.store.remove_recurrences(self.user, self.uid) 165 self.store.remove_cancellations(self.user, self.uid) 166 else: 167 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 168 169 # Queue any request, if appropriate. 170 171 if queue: 172 self.store.queue_request(self.user, self.uid, self.recurrenceid) 173 174 # Set the complete event or an additional occurrence. 175 176 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 177 178 # Cancel complete events or particular occurrences in recurring 179 # events. 180 181 if cancel: 182 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 183 184 # Remove any associated request. 185 186 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 187 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 188 189 # No return message will occur to update the free/busy 190 # information, so this is done here using outgoing message 191 # functionality. 192 193 self.remove_event_from_freebusy() 194 195 # Update the recipient's record of the organiser's schedule. 196 197 self.remove_freebusy_from_organiser(organiser) 198 199 else: 200 self.update_freebusy_from_organiser(organiser) 201 202 return True 203 204 def _schedule_for_organiser(self, organiser, attendees): 205 206 "As organiser, update attendance from valid attendees." 207 208 # Occurrences that are still part of a parent object are separated, 209 # attendance information transferred, and the free/busy details updated. 210 211 if self.is_newly_separated_occurrence(): 212 if self.make_separate_occurrence(for_organiser=True): 213 214 # Update free/busy details for the event. 215 216 self.update_event_in_freebusy(for_organiser=True) 217 218 # Produce a REQUEST for the created occurrence for other 219 # attendees of the parent event. 220 221 obj = self.get_parent_object() 222 stored_attendees = set(obj.get_values("ATTENDEE")) 223 attendees = stored_attendees.difference(attendees) 224 225 for attendee in attendees: 226 methods, parts = self.get_message_parts(self.obj, "REQUEST", attendee) 227 self.add_results(methods, [get_address(attendee)], parts) 228 229 return True 230 231 # Merge the attendance for the received object. 232 233 elif self.merge_attendance(attendees): 234 return self.update_freebusy_from_attendees(attendees) 235 236 return False 237 238 def _refresh(self, organiser, attendees): 239 240 """ 241 Respond to a refresh message by providing complete event details to 242 attendees. 243 """ 244 245 # Filter by stored attendees. 246 247 obj = self.get_stored_object_version() 248 stored_attendees = set(obj.get_values("ATTENDEE")) 249 attendees = stored_attendees.intersection(attendees) 250 251 if not attendees: 252 return False 253 254 # Produce REQUEST and CANCEL results. 255 256 for attendee in attendees: 257 methods, parts = self.get_message_parts(obj, "REQUEST", attendee) 258 self.add_results(methods, [get_address(attendee)], parts) 259 260 return True 261 262 class Event(PersonHandler): 263 264 "An event handler." 265 266 def add(self): 267 268 "Queue a suggested additional recurrence for any active event." 269 270 _ = self.get_translator() 271 272 if self.allow_add() and self._process(self._add, queue=True): 273 self.wrap(_("An addition to an event has been received.")) 274 275 def cancel(self): 276 277 "Queue a cancellation of any active event." 278 279 _ = self.get_translator() 280 281 if self._cancel(): 282 self.wrap(_("An event cancellation has been received."), link=False) 283 284 def counter(self): 285 286 "Record a counter-proposal to a proposed event." 287 288 _ = self.get_translator() 289 290 if self._process(self._counter, from_organiser=False): 291 self.wrap(_("A counter proposal to an event invitation has been received."), link=True) 292 293 def declinecounter(self): 294 295 "Record a rejection of a counter-proposal." 296 297 _ = self.get_translator() 298 299 if self._process(self._declinecounter): 300 self.wrap(_("Your counter proposal to an event invitation has been declined."), link=True) 301 302 def publish(self): 303 304 "Register details of any relevant event." 305 306 _ = self.get_translator() 307 308 if self._publish(): 309 self.wrap(_("Details of an event have been received.")) 310 311 def refresh(self): 312 313 "Requests to refresh events are handled either here or by the client." 314 315 _ = self.get_translator() 316 317 if self.is_refreshing(): 318 self._process(self._refresh, from_organiser=False) 319 else: 320 self.wrap(_("A request for updated event details has been received.")) 321 322 def reply(self): 323 324 "Record replies and notify the recipient." 325 326 _ = self.get_translator() 327 328 if self._process(self._schedule_for_organiser, from_organiser=False): 329 self.wrap(_("A reply to an event invitation has been received.")) 330 331 def request(self): 332 333 "Hold requests and notify the recipient." 334 335 _ = self.get_translator() 336 337 if self._process(self._schedule_for_attendee, queue=True): 338 self.wrap(_("An event invitation has been received.")) 339 340 class Freebusy(CommonFreebusy, Handler): 341 342 "A free/busy handler." 343 344 def publish(self): 345 346 "Register free/busy information." 347 348 _ = self.get_translator() 349 350 self._record_freebusy(from_organiser=True) 351 352 # Produce a message if configured to do so. 353 354 if self.is_notifying(): 355 self.wrap(_("A free/busy update has been received."), link=False) 356 357 def reply(self): 358 359 "Record replies and notify the recipient." 360 361 _ = self.get_translator() 362 363 self._record_freebusy(from_organiser=False) 364 365 # Produce a message if configured to do so. 366 367 if self.is_notifying(): 368 self.wrap(_("A reply to a free/busy request has been received."), link=False) 369 370 def request(self): 371 372 """ 373 Respond to a request by preparing a reply containing free/busy 374 information for the recipient. 375 """ 376 377 # Produce a reply if configured to do so. 378 379 if self.is_sharing(): 380 return CommonFreebusy.request(self) 381 382 # Handler registry. 383 384 handlers = [ 385 ("VFREEBUSY", Freebusy), 386 ("VEVENT", Event), 387 ] 388 389 # vim: tabstop=4 expandtab shiftwidth=4