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(Handler, CommonEvent): 28 29 "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 _record_freebusy(self, from_organiser=True): 141 142 """ 143 Record free/busy information for a message originating from an organiser 144 if 'from_organiser' is set to a true value. 145 """ 146 147 if from_organiser: 148 organiser_item = self.require_organiser(from_organiser) 149 if not organiser_item: 150 return 151 152 senders = [organiser_item] 153 else: 154 oa = self.require_organiser_and_attendees(from_organiser) 155 if not oa: 156 return 157 158 organiser_item, attendees = oa 159 senders = attendees.items() 160 161 if not senders: 162 return 163 164 freebusy = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] 165 dtstart = self.obj.get_datetime("DTSTART") 166 dtend = self.obj.get_datetime("DTEND") 167 period = Period(dtstart, dtend, self.get_tzid()) 168 169 for sender, sender_attr in senders: 170 stored_freebusy = self.store.get_freebusy_for_other_for_update(self.user, sender) 171 try: 172 replace_overlapping(stored_freebusy, period, freebusy) 173 self.store.set_freebusy_for_other_in_update(self.user, stored_freebusy, sender) 174 finally: 175 self.store.release_freebusy(self.user) 176 177 def _refresh(self): 178 179 """ 180 Respond to a refresh message by providing complete event details to 181 attendees. 182 """ 183 184 # Obtain valid organiser and attendee details. 185 186 oa = self.require_organiser_and_attendees(False) 187 if not oa: 188 return False 189 190 (organiser, organiser_attr), attendees = oa 191 192 # Filter by stored attendees. 193 194 obj = self.get_stored_object_version() 195 stored_attendees = set(obj.get_values("ATTENDEE")) 196 attendees = stored_attendees.intersection(attendees) 197 198 if not attendees: 199 return False 200 201 # Assume that the outcome will be a request. It would not seem 202 # completely bizarre to produce a publishing message instead if a 203 # refresh message was unprovoked. 204 205 method = "REQUEST" 206 207 for attendee in attendees: 208 responses = [] 209 210 # Get the parent event, add SENT-BY details to the organiser. 211 212 obj = self.get_stored_object_version() 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 # Get recurrences. 220 221 if not self.recurrenceid: 222 for recurrenceid in self.store.get_active_recurrences(self.user, self.uid): 223 224 # Get the recurrence, add SENT-BY details to the organiser. 225 226 obj = self.get_stored_object(self.uid, recurrenceid) 227 228 if self.is_participating(attendee, obj=obj): 229 organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) 230 self.update_sender(organiser_attr) 231 responses.append(obj.to_node()) 232 233 self.add_result(method, [get_address(attendee)], to_part(method, responses)) 234 235 return True 236 237 class Event(PersonHandler): 238 239 "An event handler." 240 241 def add(self): 242 243 "Queue a suggested additional recurrence for any active event." 244 245 if self._add(queue=True): 246 return self.wrap("An addition to an event has been received.") 247 248 def cancel(self): 249 250 "Queue a cancellation of any active event." 251 252 if self._record(from_organiser=True, queue=False, cancel=True): 253 return self.wrap("An event cancellation has been received.", link=False) 254 255 def counter(self): 256 257 # NOTE: Queue a suggested modification to any active event. 258 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 Freebusy(PersonHandler, CommonFreebusy): 298 299 "A free/busy handler." 300 301 def publish(self): 302 303 "Register free/busy information." 304 305 self._record_freebusy(from_organiser=True) 306 307 # Produce a message if configured to do so. 308 309 if self.is_notifying(): 310 return self.wrap("A free/busy update has been received.", link=False) 311 312 def reply(self): 313 314 "Record replies and notify the recipient." 315 316 self._record_freebusy(from_organiser=False) 317 318 # Produce a message if configured to do so. 319 320 if self.is_notifying(): 321 return self.wrap("A reply to a free/busy request has been received.", link=False) 322 323 def request(self): 324 325 """ 326 Respond to a request by preparing a reply containing free/busy 327 information for the recipient. 328 """ 329 330 # Produce a reply if configured to do so. 331 332 if self.is_sharing(): 333 return CommonFreebusy.request(self) 334 335 # Handler registry. 336 337 handlers = [ 338 ("VFREEBUSY", Freebusy), 339 ("VEVENT", Event), 340 ] 341 342 # vim: tabstop=4 expandtab shiftwidth=4