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