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