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