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