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