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