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