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 # Get the parent event, add SENT-BY details to the organiser. 209 210 obj = self.get_stored_object_version() 211 organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) 212 self.update_sender(organiser_attr) 213 responses = [obj.to_node()] 214 215 # Get recurrences. 216 217 cancelled = self.store.get_cancellations(self.user) 218 219 if not self.recurrenceid: 220 for recurrenceid in self.store.get_recurrences(self.user, self.uid): 221 if (self.uid, recurrenceid) not in cancelled: 222 223 # Get the recurrence, add SENT-BY details to the organiser. 224 225 obj = self.get_stored_object(self.uid, recurrenceid) 226 organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) 227 self.update_sender(organiser_attr) 228 responses.append(obj.to_node()) 229 230 self.add_result(method, map(get_address, attendees), to_part(method, responses)) 231 232 return True 233 234 class Event(PersonHandler): 235 236 "An event handler." 237 238 def add(self): 239 240 "Queue a suggested additional recurrence for any active event." 241 242 if self._add(queue=True): 243 return self.wrap("An addition to an event has been received.") 244 245 def cancel(self): 246 247 "Queue a cancellation of any active event." 248 249 if self._record(from_organiser=True, queue=False, cancel=True): 250 return self.wrap("An event cancellation has been received.", link=False) 251 252 def counter(self): 253 254 # NOTE: Queue a suggested modification to any active event. 255 256 return self.wrap("A counter proposal to an event invitation has been received.", link=False) 257 258 def declinecounter(self): 259 260 # NOTE: Queue a rejected modification to any active event. 261 262 return self.wrap("Your counter proposal to an event invitation has been declined.", link=False) 263 264 def publish(self): 265 266 "Register details of any relevant event." 267 268 if self._record(from_organiser=True, queue=False): 269 return self.wrap("Details of an event have been received.") 270 271 def refresh(self): 272 273 "Requests to refresh events are handled either here or by the client." 274 275 if self.is_refreshing(): 276 return self._refresh() 277 else: 278 return self.wrap("A request for updated event details has been received.") 279 280 def reply(self): 281 282 "Record replies and notify the recipient." 283 284 if self._record(from_organiser=False, queue=False): 285 return self.wrap("A reply to an event invitation has been received.") 286 287 def request(self): 288 289 "Hold requests and notify the recipient." 290 291 if self._record(from_organiser=True, queue=True): 292 return self.wrap("An event invitation has been received.") 293 294 class Freebusy(PersonHandler, CommonFreebusy): 295 296 "A free/busy handler." 297 298 def publish(self): 299 300 "Register free/busy information." 301 302 self._record_freebusy(from_organiser=True) 303 304 # Produce a message if configured to do so. 305 306 if self.is_notifying(): 307 return self.wrap("A free/busy update has been received.", link=False) 308 309 def reply(self): 310 311 "Record replies and notify the recipient." 312 313 self._record_freebusy(from_organiser=False) 314 315 # Produce a message if configured to do so. 316 317 if self.is_notifying(): 318 return self.wrap("A reply to a free/busy request has been received.", link=False) 319 320 def request(self): 321 322 """ 323 Respond to a request by preparing a reply containing free/busy 324 information for the recipient. 325 """ 326 327 # Produce a reply if configured to do so. 328 329 if self.is_sharing(): 330 return CommonFreebusy.request(self) 331 332 # Handler registry. 333 334 handlers = [ 335 ("VFREEBUSY", Freebusy), 336 ("VEVENT", Event), 337 ] 338 339 # vim: tabstop=4 expandtab shiftwidth=4