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, to_part, uri_dict, uri_item 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 51 # Record the event as a recurrence of the parent object. 52 53 self.update_recurrenceid() 54 55 # Update the recipient's record of the organiser's schedule. 56 57 self.update_freebusy_from_organiser(organiser) 58 59 # Stop if requesting the full event. 60 61 if self.will_refresh(): 62 return 63 64 # Set the additional occurrence. 65 66 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 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 _declinecounter(self): 107 108 "Revoke any counter-proposal recorded as a free/busy offer." 109 110 # Obtain valid organiser and attendee details. 111 112 oa = self.require_organiser_and_attendees() 113 if not oa: 114 return False 115 116 self.remove_event_from_freebusy_offers() 117 118 return True 119 120 def _publish(self): 121 122 "Record details of a published event." 123 124 # Handle an event being published by the sender to themself. 125 126 organiser_item = self.require_organiser() 127 if organiser_item: 128 organiser, organiser_attr = organiser_item 129 if self.user == organiser: 130 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 131 return True 132 133 return self._record(from_organiser=True, queue=False) 134 135 def _record(self, from_organiser=True, queue=False, cancel=False): 136 137 """ 138 Record details from the current object given a message originating 139 from an organiser if 'from_organiser' is set to a true value, queuing a 140 request if 'queue' is set to a true value, or cancelling an event if 141 'cancel' is set to a true value. 142 """ 143 144 # Obtain valid organiser and attendee details. 145 146 oa = self.require_organiser_and_attendees(from_organiser) 147 if not oa: 148 return False 149 150 (organiser, organiser_attr), attendees = oa 151 152 # Handle notifications and invitations. 153 154 if from_organiser: 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 # Cancel complete events or particular occurrences in recurring 176 # events. 177 178 if cancel: 179 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 180 181 # Remove any associated request. 182 183 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 184 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 185 186 # No return message will occur to update the free/busy 187 # information, so this is done here using outgoing message 188 # functionality. 189 190 self.remove_event_from_freebusy() 191 192 # Update the recipient's record of the organiser's schedule. 193 194 self.remove_freebusy_from_organiser(organiser) 195 196 else: 197 self.update_freebusy_from_organiser(organiser) 198 199 # Set the complete event or an additional occurrence. 200 201 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 202 203 # As organiser, update attendance from valid attendees. 204 205 else: 206 if self.merge_attendance(attendees): 207 self.update_freebusy_from_attendees(attendees) 208 209 return True 210 211 def _refresh(self): 212 213 """ 214 Respond to a refresh message by providing complete event details to 215 attendees. 216 """ 217 218 # Obtain valid organiser and attendee details. 219 220 oa = self.require_organiser_and_attendees(False) 221 if not oa: 222 return False 223 224 (organiser, organiser_attr), attendees = oa 225 226 # Filter by stored attendees. 227 228 obj = self.get_stored_object_version() 229 stored_attendees = set(obj.get_values("ATTENDEE")) 230 attendees = stored_attendees.intersection(attendees) 231 232 if not attendees: 233 return False 234 235 # Assume that the outcome will be a request. It would not seem 236 # completely bizarre to produce a publishing message instead if a 237 # refresh message was unprovoked. 238 239 method = "REQUEST" 240 241 for attendee in attendees: 242 responses = [] 243 244 # Get the parent event, add SENT-BY details to the organiser. 245 246 obj = self.get_stored_object_version() 247 248 if self.is_participating(attendee, obj=obj): 249 organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) 250 self.update_sender(organiser_attr) 251 responses.append(obj.to_node()) 252 253 # Get recurrences. 254 255 if not self.recurrenceid: 256 for recurrenceid in self.store.get_active_recurrences(self.user, self.uid): 257 258 # Get the recurrence, add SENT-BY details to the organiser. 259 260 obj = self.get_stored_object(self.uid, recurrenceid) 261 262 if self.is_participating(attendee, obj=obj): 263 organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) 264 self.update_sender(organiser_attr) 265 responses.append(obj.to_node()) 266 267 self.add_result(method, [get_address(attendee)], to_part(method, responses)) 268 269 return True 270 271 class Event(PersonHandler): 272 273 "An event handler." 274 275 def add(self): 276 277 "Queue a suggested additional recurrence for any active event." 278 279 if self.allow_add() and self._add(queue=True): 280 return self.wrap("An addition to an event has been received.") 281 282 def cancel(self): 283 284 "Queue a cancellation of any active event." 285 286 if self._record(from_organiser=True, queue=False, cancel=True): 287 return 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 if self._counter(): 294 return self.wrap("A counter proposal to an event invitation has been received.", link=True) 295 296 def declinecounter(self): 297 298 "Record a rejection of a counter-proposal." 299 300 if self._declinecounter(): 301 return self.wrap("Your counter proposal to an event invitation has been declined.", link=True) 302 303 def publish(self): 304 305 "Register details of any relevant event." 306 307 if self._publish(): 308 return self.wrap("Details of an event have been received.") 309 310 def refresh(self): 311 312 "Requests to refresh events are handled either here or by the client." 313 314 if self.is_refreshing(): 315 return self._refresh() 316 else: 317 return self.wrap("A request for updated event details has been received.") 318 319 def reply(self): 320 321 "Record replies and notify the recipient." 322 323 if self._record(from_organiser=False, queue=False): 324 return self.wrap("A reply to an event invitation has been received.") 325 326 def request(self): 327 328 "Hold requests and notify the recipient." 329 330 if self._record(from_organiser=True, queue=True): 331 return self.wrap("An event invitation has been received.") 332 333 class PersonFreebusy(CommonFreebusy, Handler): 334 335 "Free/busy handling mechanisms specific to people." 336 337 def _record_freebusy(self, from_organiser=True): 338 339 """ 340 Record free/busy information for a message originating from an organiser 341 if 'from_organiser' is set to a true value. 342 """ 343 344 if from_organiser: 345 organiser_item = self.require_organiser(from_organiser) 346 if not organiser_item: 347 return 348 349 senders = [organiser_item] 350 else: 351 oa = self.require_organiser_and_attendees(from_organiser) 352 if not oa: 353 return 354 355 organiser_item, attendees = oa 356 senders = attendees.items() 357 358 if not senders: 359 return 360 361 freebusy = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] 362 dtstart = self.obj.get_datetime("DTSTART") 363 dtend = self.obj.get_datetime("DTEND") 364 period = Period(dtstart, dtend, self.get_tzid()) 365 366 for sender, sender_attr in senders: 367 stored_freebusy = self.store.get_freebusy_for_other(self.user, sender) 368 replace_overlapping(stored_freebusy, period, freebusy) 369 self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) 370 371 class Freebusy(PersonFreebusy): 372 373 "A free/busy handler." 374 375 def publish(self): 376 377 "Register free/busy information." 378 379 self._record_freebusy(from_organiser=True) 380 381 # Produce a message if configured to do so. 382 383 if self.is_notifying(): 384 return self.wrap("A free/busy update has been received.", link=False) 385 386 def reply(self): 387 388 "Record replies and notify the recipient." 389 390 self._record_freebusy(from_organiser=False) 391 392 # Produce a message if configured to do so. 393 394 if self.is_notifying(): 395 return self.wrap("A reply to a free/busy request has been received.", link=False) 396 397 def request(self): 398 399 """ 400 Respond to a request by preparing a reply containing free/busy 401 information for the recipient. 402 """ 403 404 # Produce a reply if configured to do so. 405 406 if self.is_sharing(): 407 return CommonFreebusy.request(self) 408 409 # Handler registry. 410 411 handlers = [ 412 ("VFREEBUSY", Freebusy), 413 ("VEVENT", Event), 414 ] 415 416 # vim: tabstop=4 expandtab shiftwidth=4