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, uri_dict 23 from imiptools.handlers import Handler 24 from imiptools.handlers.common import CommonFreebusy, CommonEvent 25 26 class PersonHandler(CommonEvent, Handler): 27 28 "Event handling mechanisms specific to people." 29 30 def _process(self, handle, from_organiser=True, **kw): 31 32 """ 33 Obtain valid organiser and attendee details in order to invoke the given 34 'handle' callable, with 'from_organiser' being indicated to obtain the 35 details. Any additional keyword arguments will be passed to 'handle'. 36 """ 37 38 oa = self.require_organiser_and_attendees(from_organiser) 39 if not oa: 40 return False 41 42 (organiser, organiser_attr), attendees = oa 43 return handle(organiser, attendees, **kw) 44 45 def _add(self, organiser, attendees, queue=True): 46 47 """ 48 Add an event occurrence for the current object or produce a response 49 that requests the event details to be sent again. 50 """ 51 52 # Request details where configured, doing so for unknown objects anyway. 53 54 if self.will_refresh(): 55 self.make_refresh() 56 return 57 58 # Record the event as a recurrence of the parent object. 59 60 self.update_recurrenceid() 61 62 # Update the recipient's record of the organiser's schedule. 63 64 self.update_freebusy_from_organiser(organiser) 65 66 # Set the additional occurrence. 67 68 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 69 70 # Remove any previous cancellations involving this event. 71 72 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 73 74 # Queue any request, if appropriate. 75 76 if queue: 77 self.store.queue_request(self.user, self.uid, self.recurrenceid) 78 79 return True 80 81 def _counter(self, organiser, attendees): 82 83 """ 84 Record details from a counter-proposal, updating the stored object with 85 attendance information. 86 """ 87 88 # Update the attendance for the sender. 89 90 attendee = self.get_sending_attendee() 91 if not attendee: 92 return False 93 94 self.merge_attendance({attendee : attendees[attendee]}) 95 96 # Queue any counter-proposal for perusal. 97 98 self.store.set_counter(self.user, attendee, self.obj.to_node(), self.uid, self.recurrenceid) 99 self.store.queue_request(self.user, self.uid, self.recurrenceid, "COUNTER") 100 101 return True 102 103 def _cancel(self): 104 105 "Record an event cancellation." 106 107 # Handle an event being published by the sender to themself. 108 109 organiser_item = self.require_organiser() 110 if organiser_item: 111 organiser, organiser_attr = organiser_item 112 if self.user == organiser: 113 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 114 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 115 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 116 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 117 self.remove_event_from_freebusy() 118 self.remove_freebusy_from_attendees(uri_dict(self.obj.get_value_map("ATTENDEE"))) 119 return True 120 121 return self._process(self._schedule_for_attendee, queue=False, cancel=True) 122 123 def _declinecounter(self, organiser, attendees): 124 125 "Revoke any counter-proposal recorded as a free/busy offer." 126 127 self.remove_event_from_freebusy_offers() 128 return True 129 130 def _publish(self): 131 132 "Record details of a published event." 133 134 # Handle an event being published by the sender to themself. 135 136 organiser_item = self.require_organiser() 137 if organiser_item: 138 organiser, organiser_attr = organiser_item 139 if self.user == organiser: 140 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 141 self.update_event_in_freebusy() 142 return True 143 144 return self._process(self._schedule_for_attendee, queue=False) 145 146 def _schedule_for_attendee(self, organiser, attendees, queue=False, cancel=False): 147 148 """ 149 Record details from the current object given a message originating 150 from an organiser if 'from_organiser' is set to a true value, queuing a 151 request if 'queue' is set to a true value, or cancelling an event if 152 'cancel' is set to a true value. 153 """ 154 155 # Process for the current user, an attendee. 156 157 if not self.have_new_object(): 158 return False 159 160 # Remove additional recurrences if handling a complete event. 161 # Also remove any previous cancellations involving this event. 162 163 if not self.recurrenceid: 164 self.store.remove_recurrences(self.user, self.uid) 165 self.store.remove_cancellations(self.user, self.uid) 166 else: 167 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 168 169 # Queue any request, if appropriate. 170 171 if queue: 172 self.store.queue_request(self.user, self.uid, self.recurrenceid) 173 174 # Set the complete event or an additional occurrence. 175 176 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 177 178 # Cancel complete events or particular occurrences in recurring 179 # events. 180 181 if cancel: 182 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 183 184 # Remove any associated request. 185 186 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 187 self.store.remove_counters(self.user, self.uid, self.recurrenceid) 188 189 # No return message will occur to update the free/busy 190 # information, so this is done here using outgoing message 191 # functionality. 192 193 self.remove_event_from_freebusy() 194 195 # Update the recipient's record of the organiser's schedule. 196 197 self.remove_freebusy_from_organiser(organiser) 198 199 else: 200 self.update_freebusy_from_organiser(organiser) 201 202 return True 203 204 def _schedule_for_organiser(self, organiser, attendees): 205 206 "As organiser, update attendance from valid attendees." 207 208 if self.merge_attendance(attendees): 209 self.update_freebusy_from_attendees(attendees) 210 211 return True 212 213 def _refresh(self, organiser, attendees): 214 215 """ 216 Respond to a refresh message by providing complete event details to 217 attendees. 218 """ 219 220 # Filter by stored attendees. 221 222 obj = self.get_stored_object_version() 223 stored_attendees = set(obj.get_values("ATTENDEE")) 224 attendees = stored_attendees.intersection(attendees) 225 226 if not attendees: 227 return False 228 229 # Produce REQUEST and CANCEL results. 230 231 for attendee in attendees: 232 methods, parts = self.get_message_parts(obj, "REQUEST", attendee) 233 self.add_results(methods, [get_address(attendee)], parts) 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 _ = self.get_translator() 246 247 if self.allow_add() and self._process(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 _ = self.get_translator() 255 256 if self._cancel(): 257 return self.wrap(_("An event cancellation has been received."), link=False) 258 259 def counter(self): 260 261 "Record a counter-proposal to a proposed event." 262 263 _ = self.get_translator() 264 265 if self._process(self._counter, from_organiser=False): 266 return self.wrap(_("A counter proposal to an event invitation has been received."), link=True) 267 268 def declinecounter(self): 269 270 "Record a rejection of a counter-proposal." 271 272 _ = self.get_translator() 273 274 if self._process(self._declinecounter): 275 return self.wrap(_("Your counter proposal to an event invitation has been declined."), link=True) 276 277 def publish(self): 278 279 "Register details of any relevant event." 280 281 _ = self.get_translator() 282 283 if self._publish(): 284 return self.wrap(_("Details of an event have been received.")) 285 286 def refresh(self): 287 288 "Requests to refresh events are handled either here or by the client." 289 290 _ = self.get_translator() 291 292 if self.is_refreshing(): 293 return self._process(self._refresh, from_organiser=False) 294 else: 295 return self.wrap(_("A request for updated event details has been received.")) 296 297 def reply(self): 298 299 "Record replies and notify the recipient." 300 301 _ = self.get_translator() 302 303 if self._process(self._schedule_for_organiser, from_organiser=False): 304 return self.wrap(_("A reply to an event invitation has been received.")) 305 306 def request(self): 307 308 "Hold requests and notify the recipient." 309 310 _ = self.get_translator() 311 312 if self._process(self._schedule_for_attendee, queue=True): 313 return self.wrap(_("An event invitation has been received.")) 314 315 class Freebusy(CommonFreebusy, Handler): 316 317 "A free/busy handler." 318 319 def publish(self): 320 321 "Register free/busy information." 322 323 _ = self.get_translator() 324 325 self._record_freebusy(from_organiser=True) 326 327 # Produce a message if configured to do so. 328 329 if self.is_notifying(): 330 return self.wrap(_("A free/busy update has been received."), link=False) 331 332 def reply(self): 333 334 "Record replies and notify the recipient." 335 336 _ = self.get_translator() 337 338 self._record_freebusy(from_organiser=False) 339 340 # Produce a message if configured to do so. 341 342 if self.is_notifying(): 343 return self.wrap(_("A reply to a free/busy request has been received."), link=False) 344 345 def request(self): 346 347 """ 348 Respond to a request by preparing a reply containing free/busy 349 information for the recipient. 350 """ 351 352 # Produce a reply if configured to do so. 353 354 if self.is_sharing(): 355 return CommonFreebusy.request(self) 356 357 # Handler registry. 358 359 handlers = [ 360 ("VFREEBUSY", Freebusy), 361 ("VEVENT", Event), 362 ] 363 364 # vim: tabstop=4 expandtab shiftwidth=4