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