paul@55 | 1 | #!/usr/bin/env python |
paul@55 | 2 | |
paul@55 | 3 | """ |
paul@55 | 4 | Handlers for a person for whom scheduling is performed. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@55 | 20 | """ |
paul@55 | 21 | |
paul@688 | 22 | from imiptools.data import get_address, to_part, uri_dict, uri_item |
paul@418 | 23 | from imiptools.handlers import Handler |
paul@683 | 24 | from imiptools.handlers.common import CommonFreebusy, CommonEvent |
paul@529 | 25 | from imiptools.period import FreeBusyPeriod, Period, replace_overlapping |
paul@55 | 26 | |
paul@725 | 27 | class PersonHandler(CommonEvent, Handler): |
paul@55 | 28 | |
paul@725 | 29 | "Event handling mechanisms specific to people." |
paul@55 | 30 | |
paul@681 | 31 | def _add(self, queue=True): |
paul@681 | 32 | |
paul@734 | 33 | """ |
paul@734 | 34 | Add an event occurrence for the current object or produce a response |
paul@734 | 35 | that requests the event details to be sent again. |
paul@734 | 36 | """ |
paul@681 | 37 | |
paul@681 | 38 | # Obtain valid organiser and attendee details. |
paul@681 | 39 | |
paul@681 | 40 | oa = self.require_organiser_and_attendees() |
paul@681 | 41 | if not oa: |
paul@681 | 42 | return False |
paul@681 | 43 | |
paul@681 | 44 | (organiser, organiser_attr), attendees = oa |
paul@681 | 45 | |
paul@734 | 46 | # Request details where configured, doing so for unknown objects anyway. |
paul@734 | 47 | |
paul@734 | 48 | refreshing = not self.get_stored_object_version() or self.get_add_method_response() == "refresh" |
paul@734 | 49 | |
paul@734 | 50 | if refreshing: |
paul@734 | 51 | |
paul@734 | 52 | # Add SENT-BY details to the recipient's attributes. |
paul@681 | 53 | |
paul@734 | 54 | attendee_attr = attendees[self.user] |
paul@734 | 55 | self.update_sender(attendee_attr) |
paul@734 | 56 | |
paul@734 | 57 | # Make a new object with a minimal property selection. |
paul@734 | 58 | |
paul@734 | 59 | obj = self.obj.copy() |
paul@734 | 60 | obj.preserve(("ORGANIZER", "DTSTAMP", "UID", "RECURRENCE-ID")) |
paul@734 | 61 | obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@734 | 62 | |
paul@734 | 63 | # Send a REFRESH message in response. |
paul@734 | 64 | |
paul@734 | 65 | self.add_result("REFRESH", [get_address(organiser)], obj.to_part("REFRESH")) |
paul@681 | 66 | |
paul@681 | 67 | # Record the event as a recurrence of the parent object. |
paul@681 | 68 | |
paul@681 | 69 | self.update_recurrenceid() |
paul@681 | 70 | |
paul@734 | 71 | # Update the recipient's record of the organiser's schedule. |
paul@734 | 72 | |
paul@734 | 73 | self.update_freebusy_from_organiser(organiser) |
paul@734 | 74 | |
paul@734 | 75 | # Stop if requesting the full event. |
paul@734 | 76 | |
paul@734 | 77 | if refreshing: |
paul@734 | 78 | return |
paul@734 | 79 | |
paul@734 | 80 | # Set the additional occurrence. |
paul@734 | 81 | |
paul@734 | 82 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@734 | 83 | |
paul@681 | 84 | # Queue any request, if appropriate. |
paul@681 | 85 | |
paul@681 | 86 | if queue: |
paul@681 | 87 | self.store.queue_request(self.user, self.uid, self.recurrenceid) |
paul@681 | 88 | |
paul@681 | 89 | return True |
paul@681 | 90 | |
paul@271 | 91 | def _record(self, from_organiser=True, queue=False, cancel=False): |
paul@55 | 92 | |
paul@420 | 93 | """ |
paul@420 | 94 | Record details from the current object given a message originating |
paul@420 | 95 | from an organiser if 'from_organiser' is set to a true value, queuing a |
paul@420 | 96 | request if 'queue' is set to a true value, or cancelling an event if |
paul@420 | 97 | 'cancel' is set to a true value. |
paul@420 | 98 | """ |
paul@420 | 99 | |
paul@420 | 100 | # Obtain valid organiser and attendee details. |
paul@420 | 101 | |
paul@100 | 102 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@55 | 103 | if not oa: |
paul@61 | 104 | return False |
paul@55 | 105 | |
paul@606 | 106 | (organiser, organiser_attr), attendees = oa |
paul@94 | 107 | |
paul@110 | 108 | # Handle notifications and invitations. |
paul@55 | 109 | |
paul@100 | 110 | if from_organiser: |
paul@110 | 111 | |
paul@468 | 112 | # Process for the current user, an attendee. |
paul@100 | 113 | |
paul@468 | 114 | if not self.have_new_object() or not self.is_attendee(self.user): |
paul@468 | 115 | return False |
paul@100 | 116 | |
paul@468 | 117 | # Remove additional recurrences if handling a complete event. |
paul@381 | 118 | |
paul@468 | 119 | if not self.recurrenceid: |
paul@468 | 120 | self.store.remove_recurrences(self.user, self.uid) |
paul@381 | 121 | |
paul@493 | 122 | # Queue any request, if appropriate. |
paul@55 | 123 | |
paul@468 | 124 | if queue: |
paul@468 | 125 | self.store.queue_request(self.user, self.uid, self.recurrenceid) |
paul@493 | 126 | |
paul@493 | 127 | # Cancel complete events or particular occurrences in recurring |
paul@493 | 128 | # events. |
paul@493 | 129 | |
paul@697 | 130 | if cancel: |
paul@468 | 131 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@698 | 132 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@100 | 133 | |
paul@504 | 134 | # Remove any associated request. |
paul@504 | 135 | |
paul@504 | 136 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@504 | 137 | |
paul@468 | 138 | # No return message will occur to update the free/busy |
paul@584 | 139 | # information, so this is done here using outgoing message |
paul@584 | 140 | # functionality. |
paul@361 | 141 | |
paul@589 | 142 | self.remove_event_from_freebusy() |
paul@589 | 143 | |
paul@697 | 144 | # Update the recipient's record of the organiser's schedule. |
paul@261 | 145 | |
paul@697 | 146 | self.remove_freebusy_from_organiser(organiser) |
paul@697 | 147 | |
paul@697 | 148 | else: |
paul@697 | 149 | self.update_freebusy_from_organiser(organiser) |
paul@268 | 150 | |
paul@672 | 151 | # Set the complete event or an additional occurrence. |
paul@672 | 152 | |
paul@672 | 153 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@672 | 154 | |
paul@420 | 155 | # As organiser, update attendance from valid attendees. |
paul@55 | 156 | |
paul@100 | 157 | else: |
paul@468 | 158 | if self.merge_attendance(attendees): |
paul@468 | 159 | self.update_freebusy_from_attendees(attendees) |
paul@61 | 160 | |
paul@61 | 161 | return True |
paul@61 | 162 | |
paul@688 | 163 | def _refresh(self): |
paul@688 | 164 | |
paul@688 | 165 | """ |
paul@688 | 166 | Respond to a refresh message by providing complete event details to |
paul@688 | 167 | attendees. |
paul@688 | 168 | """ |
paul@688 | 169 | |
paul@688 | 170 | # Obtain valid organiser and attendee details. |
paul@688 | 171 | |
paul@688 | 172 | oa = self.require_organiser_and_attendees(False) |
paul@688 | 173 | if not oa: |
paul@688 | 174 | return False |
paul@688 | 175 | |
paul@688 | 176 | (organiser, organiser_attr), attendees = oa |
paul@688 | 177 | |
paul@688 | 178 | # Filter by stored attendees. |
paul@688 | 179 | |
paul@688 | 180 | obj = self.get_stored_object_version() |
paul@688 | 181 | stored_attendees = set(obj.get_values("ATTENDEE")) |
paul@688 | 182 | attendees = stored_attendees.intersection(attendees) |
paul@688 | 183 | |
paul@688 | 184 | if not attendees: |
paul@688 | 185 | return False |
paul@688 | 186 | |
paul@688 | 187 | # Assume that the outcome will be a request. It would not seem |
paul@688 | 188 | # completely bizarre to produce a publishing message instead if a |
paul@688 | 189 | # refresh message was unprovoked. |
paul@688 | 190 | |
paul@688 | 191 | method = "REQUEST" |
paul@688 | 192 | |
paul@694 | 193 | for attendee in attendees: |
paul@694 | 194 | responses = [] |
paul@688 | 195 | |
paul@694 | 196 | # Get the parent event, add SENT-BY details to the organiser. |
paul@694 | 197 | |
paul@694 | 198 | obj = self.get_stored_object_version() |
paul@688 | 199 | |
paul@694 | 200 | if self.is_participating(attendee, obj=obj): |
paul@694 | 201 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@694 | 202 | self.update_sender(organiser_attr) |
paul@694 | 203 | responses.append(obj.to_node()) |
paul@688 | 204 | |
paul@694 | 205 | # Get recurrences. |
paul@694 | 206 | |
paul@694 | 207 | if not self.recurrenceid: |
paul@694 | 208 | for recurrenceid in self.store.get_active_recurrences(self.user, self.uid): |
paul@688 | 209 | |
paul@688 | 210 | # Get the recurrence, add SENT-BY details to the organiser. |
paul@688 | 211 | |
paul@688 | 212 | obj = self.get_stored_object(self.uid, recurrenceid) |
paul@688 | 213 | |
paul@694 | 214 | if self.is_participating(attendee, obj=obj): |
paul@694 | 215 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@694 | 216 | self.update_sender(organiser_attr) |
paul@694 | 217 | responses.append(obj.to_node()) |
paul@694 | 218 | |
paul@694 | 219 | self.add_result(method, [get_address(attendee)], to_part(method, responses)) |
paul@688 | 220 | |
paul@688 | 221 | return True |
paul@688 | 222 | |
paul@216 | 223 | class Event(PersonHandler): |
paul@67 | 224 | |
paul@67 | 225 | "An event handler." |
paul@67 | 226 | |
paul@63 | 227 | def add(self): |
paul@63 | 228 | |
paul@681 | 229 | "Queue a suggested additional recurrence for any active event." |
paul@63 | 230 | |
paul@734 | 231 | if self.allow_add() and self._add(queue=True): |
paul@681 | 232 | return self.wrap("An addition to an event has been received.") |
paul@63 | 233 | |
paul@63 | 234 | def cancel(self): |
paul@63 | 235 | |
paul@142 | 236 | "Queue a cancellation of any active event." |
paul@63 | 237 | |
paul@665 | 238 | if self._record(from_organiser=True, queue=False, cancel=True): |
paul@668 | 239 | return self.wrap("An event cancellation has been received.", link=False) |
paul@63 | 240 | |
paul@63 | 241 | def counter(self): |
paul@63 | 242 | |
paul@63 | 243 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 244 | |
paul@668 | 245 | return self.wrap("A counter proposal to an event invitation has been received.", link=False) |
paul@63 | 246 | |
paul@63 | 247 | def declinecounter(self): |
paul@63 | 248 | |
paul@665 | 249 | # NOTE: Queue a rejected modification to any active event. |
paul@63 | 250 | |
paul@668 | 251 | return self.wrap("Your counter proposal to an event invitation has been declined.", link=False) |
paul@63 | 252 | |
paul@63 | 253 | def publish(self): |
paul@63 | 254 | |
paul@139 | 255 | "Register details of any relevant event." |
paul@63 | 256 | |
paul@665 | 257 | if self._record(from_organiser=True, queue=False): |
paul@665 | 258 | return self.wrap("Details of an event have been received.") |
paul@63 | 259 | |
paul@63 | 260 | def refresh(self): |
paul@63 | 261 | |
paul@688 | 262 | "Requests to refresh events are handled either here or by the client." |
paul@63 | 263 | |
paul@688 | 264 | if self.is_refreshing(): |
paul@688 | 265 | return self._refresh() |
paul@688 | 266 | else: |
paul@688 | 267 | return self.wrap("A request for updated event details has been received.") |
paul@63 | 268 | |
paul@61 | 269 | def reply(self): |
paul@61 | 270 | |
paul@61 | 271 | "Record replies and notify the recipient." |
paul@61 | 272 | |
paul@665 | 273 | if self._record(from_organiser=False, queue=False): |
paul@668 | 274 | return self.wrap("A reply to an event invitation has been received.") |
paul@61 | 275 | |
paul@61 | 276 | def request(self): |
paul@61 | 277 | |
paul@61 | 278 | "Hold requests and notify the recipient." |
paul@61 | 279 | |
paul@665 | 280 | if self._record(from_organiser=True, queue=True): |
paul@668 | 281 | return self.wrap("An event invitation has been received.") |
paul@60 | 282 | |
paul@725 | 283 | class PersonFreebusy(CommonFreebusy, Handler): |
paul@725 | 284 | |
paul@725 | 285 | "Free/busy handling mechanisms specific to people." |
paul@725 | 286 | |
paul@725 | 287 | def _record_freebusy(self, from_organiser=True): |
paul@725 | 288 | |
paul@725 | 289 | """ |
paul@725 | 290 | Record free/busy information for a message originating from an organiser |
paul@725 | 291 | if 'from_organiser' is set to a true value. |
paul@725 | 292 | """ |
paul@725 | 293 | |
paul@725 | 294 | if from_organiser: |
paul@725 | 295 | organiser_item = self.require_organiser(from_organiser) |
paul@725 | 296 | if not organiser_item: |
paul@725 | 297 | return |
paul@725 | 298 | |
paul@725 | 299 | senders = [organiser_item] |
paul@725 | 300 | else: |
paul@725 | 301 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@725 | 302 | if not oa: |
paul@725 | 303 | return |
paul@725 | 304 | |
paul@725 | 305 | organiser_item, attendees = oa |
paul@725 | 306 | senders = attendees.items() |
paul@725 | 307 | |
paul@725 | 308 | if not senders: |
paul@725 | 309 | return |
paul@725 | 310 | |
paul@725 | 311 | freebusy = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] |
paul@725 | 312 | dtstart = self.obj.get_datetime("DTSTART") |
paul@725 | 313 | dtend = self.obj.get_datetime("DTEND") |
paul@725 | 314 | period = Period(dtstart, dtend, self.get_tzid()) |
paul@725 | 315 | |
paul@725 | 316 | for sender, sender_attr in senders: |
paul@730 | 317 | stored_freebusy = self.store.get_freebusy_for_other(self.user, sender) |
paul@730 | 318 | replace_overlapping(stored_freebusy, period, freebusy) |
paul@730 | 319 | self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) |
paul@725 | 320 | |
paul@725 | 321 | class Freebusy(PersonFreebusy): |
paul@55 | 322 | |
paul@55 | 323 | "A free/busy handler." |
paul@55 | 324 | |
paul@55 | 325 | def publish(self): |
paul@63 | 326 | |
paul@110 | 327 | "Register free/busy information." |
paul@110 | 328 | |
paul@180 | 329 | self._record_freebusy(from_organiser=True) |
paul@180 | 330 | |
paul@180 | 331 | # Produce a message if configured to do so. |
paul@63 | 332 | |
paul@468 | 333 | if self.is_notifying(): |
paul@182 | 334 | return self.wrap("A free/busy update has been received.", link=False) |
paul@55 | 335 | |
paul@55 | 336 | def reply(self): |
paul@55 | 337 | |
paul@63 | 338 | "Record replies and notify the recipient." |
paul@63 | 339 | |
paul@180 | 340 | self._record_freebusy(from_organiser=False) |
paul@180 | 341 | |
paul@180 | 342 | # Produce a message if configured to do so. |
paul@139 | 343 | |
paul@468 | 344 | if self.is_notifying(): |
paul@182 | 345 | return self.wrap("A reply to a free/busy request has been received.", link=False) |
paul@55 | 346 | |
paul@55 | 347 | def request(self): |
paul@55 | 348 | |
paul@55 | 349 | """ |
paul@55 | 350 | Respond to a request by preparing a reply containing free/busy |
paul@468 | 351 | information for the recipient. |
paul@55 | 352 | """ |
paul@55 | 353 | |
paul@180 | 354 | # Produce a reply if configured to do so. |
paul@55 | 355 | |
paul@468 | 356 | if self.is_sharing(): |
paul@180 | 357 | return CommonFreebusy.request(self) |
paul@55 | 358 | |
paul@55 | 359 | # Handler registry. |
paul@55 | 360 | |
paul@55 | 361 | handlers = [ |
paul@55 | 362 | ("VFREEBUSY", Freebusy), |
paul@55 | 363 | ("VEVENT", Event), |
paul@55 | 364 | ] |
paul@55 | 365 | |
paul@55 | 366 | # vim: tabstop=4 expandtab shiftwidth=4 |