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