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