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