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 return True 218 219 # Merge the attendance for the received object. 220 221 elif self.merge_attendance(attendees): 222 return self.update_freebusy_from_attendees(attendees) 223 224 return False 225 226 def _refresh(self, organiser, attendees): 227 228 """ 229 Respond to a refresh message by providing complete event details to 230 attendees. 231 """ 232 233 # Filter by stored attendees. 234 235 obj = self.get_stored_object_version() 236 stored_attendees = set(obj.get_values("ATTENDEE")) 237 attendees = stored_attendees.intersection(attendees) 238 239 if not attendees: 240 return False 241 242 # Produce REQUEST and CANCEL results. 243 244 for attendee in attendees: 245 methods, parts = self.get_message_parts(obj, "REQUEST", attendee) 246 self.add_results(methods, [get_address(attendee)], parts) 247 248 return True 249 250 class Event(PersonHandler): 251 252 "An event handler." 253 254 def add(self): 255 256 "Queue a suggested additional recurrence for any active event." 257 258 _ = self.get_translator() 259 260 if self.allow_add() and self._process(self._add, queue=True): 261 return self.wrap(_("An addition to an event has been received.")) 262 263 def cancel(self): 264 265 "Queue a cancellation of any active event." 266 267 _ = self.get_translator() 268 269 if self._cancel(): 270 return self.wrap(_("An event cancellation has been received."), link=False) 271 272 def counter(self): 273 274 "Record a counter-proposal to a proposed event." 275 276 _ = self.get_translator() 277 278 if self._process(self._counter, from_organiser=False): 279 return self.wrap(_("A counter proposal to an event invitation has been received."), link=True) 280 281 def declinecounter(self): 282 283 "Record a rejection of a counter-proposal." 284 285 _ = self.get_translator() 286 287 if self._process(self._declinecounter): 288 return self.wrap(_("Your counter proposal to an event invitation has been declined."), link=True) 289 290 def publish(self): 291 292 "Register details of any relevant event." 293 294 _ = self.get_translator() 295 296 if self._publish(): 297 return self.wrap(_("Details of an event have been received.")) 298 299 def refresh(self): 300 301 "Requests to refresh events are handled either here or by the client." 302 303 _ = self.get_translator() 304 305 if self.is_refreshing(): 306 return self._process(self._refresh, from_organiser=False) 307 else: 308 return self.wrap(_("A request for updated event details has been received.")) 309 310 def reply(self): 311 312 "Record replies and notify the recipient." 313 314 _ = self.get_translator() 315 316 if self._process(self._schedule_for_organiser, from_organiser=False): 317 return self.wrap(_("A reply to an event invitation has been received.")) 318 319 def request(self): 320 321 "Hold requests and notify the recipient." 322 323 _ = self.get_translator() 324 325 if self._process(self._schedule_for_attendee, queue=True): 326 return self.wrap(_("An event invitation has been received.")) 327 328 class Freebusy(CommonFreebusy, Handler): 329 330 "A free/busy handler." 331 332 def publish(self): 333 334 "Register free/busy information." 335 336 _ = self.get_translator() 337 338 self._record_freebusy(from_organiser=True) 339 340 # Produce a message if configured to do so. 341 342 if self.is_notifying(): 343 return self.wrap(_("A free/busy update has been received."), link=False) 344 345 def reply(self): 346 347 "Record replies and notify the recipient." 348 349 _ = self.get_translator() 350 351 self._record_freebusy(from_organiser=False) 352 353 # Produce a message if configured to do so. 354 355 if self.is_notifying(): 356 return self.wrap(_("A reply to a free/busy request has been received."), link=False) 357 358 def request(self): 359 360 """ 361 Respond to a request by preparing a reply containing free/busy 362 information for the recipient. 363 """ 364 365 # Produce a reply if configured to do so. 366 367 if self.is_sharing(): 368 return CommonFreebusy.request(self) 369 370 # Handler registry. 371 372 handlers = [ 373 ("VFREEBUSY", Freebusy), 374 ("VEVENT", Event), 375 ] 376 377 # vim: tabstop=4 expandtab shiftwidth=4