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@737 | 48 | if self.will_refresh(): |
paul@737 | 49 | self.make_refresh() |
paul@681 | 50 | |
paul@681 | 51 | # Record the event as a recurrence of the parent object. |
paul@681 | 52 | |
paul@681 | 53 | self.update_recurrenceid() |
paul@681 | 54 | |
paul@734 | 55 | # Update the recipient's record of the organiser's schedule. |
paul@734 | 56 | |
paul@734 | 57 | self.update_freebusy_from_organiser(organiser) |
paul@734 | 58 | |
paul@734 | 59 | # Stop if requesting the full event. |
paul@734 | 60 | |
paul@737 | 61 | if self.will_refresh(): |
paul@734 | 62 | return |
paul@734 | 63 | |
paul@734 | 64 | # Set the additional occurrence. |
paul@734 | 65 | |
paul@734 | 66 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@734 | 67 | |
paul@681 | 68 | # Queue any request, if appropriate. |
paul@681 | 69 | |
paul@681 | 70 | if queue: |
paul@681 | 71 | self.store.queue_request(self.user, self.uid, self.recurrenceid) |
paul@681 | 72 | |
paul@681 | 73 | return True |
paul@681 | 74 | |
paul@743 | 75 | def _counter(self): |
paul@743 | 76 | |
paul@743 | 77 | """ |
paul@743 | 78 | Record details from a counter-proposal, updating the stored object with |
paul@743 | 79 | attendance information. |
paul@743 | 80 | """ |
paul@743 | 81 | |
paul@743 | 82 | # Obtain valid organiser and attendee details. |
paul@743 | 83 | |
paul@743 | 84 | oa = self.require_organiser_and_attendees(from_organiser=False) |
paul@743 | 85 | if not oa: |
paul@743 | 86 | return False |
paul@743 | 87 | |
paul@743 | 88 | (organiser, organiser_attr), attendees = oa |
paul@743 | 89 | |
paul@849 | 90 | # Update the attendance for the sender. |
paul@743 | 91 | |
paul@849 | 92 | attendee = self.get_sending_attendee() |
paul@849 | 93 | if not attendee: |
paul@849 | 94 | return False |
paul@743 | 95 | |
paul@849 | 96 | if self.merge_attendance({attendee : attendees[attendee]}): |
paul@849 | 97 | self.update_freebusy_from_attendees([attendee]) |
paul@743 | 98 | |
paul@747 | 99 | # Queue any counter-proposal for perusal. |
paul@747 | 100 | |
paul@849 | 101 | self.store.set_counter(self.user, attendee, self.obj.to_node(), self.uid, self.recurrenceid) |
paul@747 | 102 | self.store.queue_request(self.user, self.uid, self.recurrenceid, "COUNTER") |
paul@747 | 103 | |
paul@743 | 104 | return True |
paul@743 | 105 | |
paul@804 | 106 | def _declinecounter(self): |
paul@804 | 107 | |
paul@804 | 108 | "Revoke any counter-proposal recorded as a free/busy offer." |
paul@804 | 109 | |
paul@804 | 110 | # Obtain valid organiser and attendee details. |
paul@804 | 111 | |
paul@804 | 112 | oa = self.require_organiser_and_attendees() |
paul@804 | 113 | if not oa: |
paul@804 | 114 | return False |
paul@804 | 115 | |
paul@804 | 116 | self.remove_event_from_freebusy_offers() |
paul@804 | 117 | |
paul@804 | 118 | return True |
paul@804 | 119 | |
paul@831 | 120 | def _publish(self): |
paul@831 | 121 | |
paul@831 | 122 | "Record details of a published event." |
paul@831 | 123 | |
paul@831 | 124 | # Handle an event being published by the sender to themself. |
paul@831 | 125 | |
paul@831 | 126 | organiser_item = self.require_organiser() |
paul@831 | 127 | if organiser_item: |
paul@831 | 128 | organiser, organiser_attr = organiser_item |
paul@831 | 129 | if self.user == organiser: |
paul@831 | 130 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@831 | 131 | return True |
paul@831 | 132 | |
paul@831 | 133 | return self._record(from_organiser=True, queue=False) |
paul@831 | 134 | |
paul@271 | 135 | def _record(self, from_organiser=True, queue=False, cancel=False): |
paul@55 | 136 | |
paul@420 | 137 | """ |
paul@420 | 138 | Record details from the current object given a message originating |
paul@420 | 139 | from an organiser if 'from_organiser' is set to a true value, queuing a |
paul@420 | 140 | request if 'queue' is set to a true value, or cancelling an event if |
paul@420 | 141 | 'cancel' is set to a true value. |
paul@420 | 142 | """ |
paul@420 | 143 | |
paul@420 | 144 | # Obtain valid organiser and attendee details. |
paul@420 | 145 | |
paul@100 | 146 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@55 | 147 | if not oa: |
paul@61 | 148 | return False |
paul@55 | 149 | |
paul@606 | 150 | (organiser, organiser_attr), attendees = oa |
paul@94 | 151 | |
paul@110 | 152 | # Handle notifications and invitations. |
paul@55 | 153 | |
paul@100 | 154 | if from_organiser: |
paul@110 | 155 | |
paul@468 | 156 | # Process for the current user, an attendee. |
paul@100 | 157 | |
paul@738 | 158 | if not self.have_new_object(): |
paul@468 | 159 | return False |
paul@100 | 160 | |
paul@468 | 161 | # Remove additional recurrences if handling a complete event. |
paul@796 | 162 | # Also remove any previous cancellations involving this event. |
paul@381 | 163 | |
paul@468 | 164 | if not self.recurrenceid: |
paul@468 | 165 | self.store.remove_recurrences(self.user, self.uid) |
paul@796 | 166 | self.store.remove_cancellations(self.user, self.uid) |
paul@796 | 167 | else: |
paul@796 | 168 | self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) |
paul@381 | 169 | |
paul@493 | 170 | # Queue any request, if appropriate. |
paul@55 | 171 | |
paul@468 | 172 | if queue: |
paul@468 | 173 | self.store.queue_request(self.user, self.uid, self.recurrenceid) |
paul@493 | 174 | |
paul@493 | 175 | # Cancel complete events or particular occurrences in recurring |
paul@493 | 176 | # events. |
paul@493 | 177 | |
paul@697 | 178 | if cancel: |
paul@468 | 179 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@100 | 180 | |
paul@504 | 181 | # Remove any associated request. |
paul@504 | 182 | |
paul@504 | 183 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@809 | 184 | self.store.remove_counters(self.user, self.uid, self.recurrenceid) |
paul@504 | 185 | |
paul@468 | 186 | # No return message will occur to update the free/busy |
paul@584 | 187 | # information, so this is done here using outgoing message |
paul@584 | 188 | # functionality. |
paul@361 | 189 | |
paul@589 | 190 | self.remove_event_from_freebusy() |
paul@589 | 191 | |
paul@697 | 192 | # Update the recipient's record of the organiser's schedule. |
paul@261 | 193 | |
paul@697 | 194 | self.remove_freebusy_from_organiser(organiser) |
paul@697 | 195 | |
paul@697 | 196 | else: |
paul@697 | 197 | self.update_freebusy_from_organiser(organiser) |
paul@268 | 198 | |
paul@672 | 199 | # Set the complete event or an additional occurrence. |
paul@672 | 200 | |
paul@672 | 201 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@672 | 202 | |
paul@420 | 203 | # As organiser, update attendance from valid attendees. |
paul@55 | 204 | |
paul@100 | 205 | else: |
paul@468 | 206 | if self.merge_attendance(attendees): |
paul@468 | 207 | self.update_freebusy_from_attendees(attendees) |
paul@61 | 208 | |
paul@61 | 209 | return True |
paul@61 | 210 | |
paul@688 | 211 | def _refresh(self): |
paul@688 | 212 | |
paul@688 | 213 | """ |
paul@688 | 214 | Respond to a refresh message by providing complete event details to |
paul@688 | 215 | attendees. |
paul@688 | 216 | """ |
paul@688 | 217 | |
paul@688 | 218 | # Obtain valid organiser and attendee details. |
paul@688 | 219 | |
paul@688 | 220 | oa = self.require_organiser_and_attendees(False) |
paul@688 | 221 | if not oa: |
paul@688 | 222 | return False |
paul@688 | 223 | |
paul@688 | 224 | (organiser, organiser_attr), attendees = oa |
paul@688 | 225 | |
paul@688 | 226 | # Filter by stored attendees. |
paul@688 | 227 | |
paul@688 | 228 | obj = self.get_stored_object_version() |
paul@688 | 229 | stored_attendees = set(obj.get_values("ATTENDEE")) |
paul@688 | 230 | attendees = stored_attendees.intersection(attendees) |
paul@688 | 231 | |
paul@688 | 232 | if not attendees: |
paul@688 | 233 | return False |
paul@688 | 234 | |
paul@688 | 235 | # Assume that the outcome will be a request. It would not seem |
paul@688 | 236 | # completely bizarre to produce a publishing message instead if a |
paul@688 | 237 | # refresh message was unprovoked. |
paul@688 | 238 | |
paul@688 | 239 | method = "REQUEST" |
paul@688 | 240 | |
paul@694 | 241 | for attendee in attendees: |
paul@694 | 242 | responses = [] |
paul@688 | 243 | |
paul@694 | 244 | # Get the parent event, add SENT-BY details to the organiser. |
paul@694 | 245 | |
paul@694 | 246 | obj = self.get_stored_object_version() |
paul@688 | 247 | |
paul@694 | 248 | if self.is_participating(attendee, obj=obj): |
paul@694 | 249 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@694 | 250 | self.update_sender(organiser_attr) |
paul@694 | 251 | responses.append(obj.to_node()) |
paul@688 | 252 | |
paul@694 | 253 | # Get recurrences. |
paul@694 | 254 | |
paul@694 | 255 | if not self.recurrenceid: |
paul@694 | 256 | for recurrenceid in self.store.get_active_recurrences(self.user, self.uid): |
paul@688 | 257 | |
paul@688 | 258 | # Get the recurrence, add SENT-BY details to the organiser. |
paul@688 | 259 | |
paul@688 | 260 | obj = self.get_stored_object(self.uid, recurrenceid) |
paul@688 | 261 | |
paul@694 | 262 | if self.is_participating(attendee, obj=obj): |
paul@694 | 263 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@694 | 264 | self.update_sender(organiser_attr) |
paul@694 | 265 | responses.append(obj.to_node()) |
paul@694 | 266 | |
paul@694 | 267 | self.add_result(method, [get_address(attendee)], to_part(method, responses)) |
paul@688 | 268 | |
paul@688 | 269 | return True |
paul@688 | 270 | |
paul@216 | 271 | class Event(PersonHandler): |
paul@67 | 272 | |
paul@67 | 273 | "An event handler." |
paul@67 | 274 | |
paul@63 | 275 | def add(self): |
paul@63 | 276 | |
paul@681 | 277 | "Queue a suggested additional recurrence for any active event." |
paul@63 | 278 | |
paul@734 | 279 | if self.allow_add() and self._add(queue=True): |
paul@681 | 280 | return self.wrap("An addition to an event has been received.") |
paul@63 | 281 | |
paul@63 | 282 | def cancel(self): |
paul@63 | 283 | |
paul@142 | 284 | "Queue a cancellation of any active event." |
paul@63 | 285 | |
paul@665 | 286 | if self._record(from_organiser=True, queue=False, cancel=True): |
paul@668 | 287 | return self.wrap("An event cancellation has been received.", link=False) |
paul@63 | 288 | |
paul@63 | 289 | def counter(self): |
paul@63 | 290 | |
paul@743 | 291 | "Record a counter-proposal to a proposed event." |
paul@743 | 292 | |
paul@743 | 293 | if self._counter(): |
paul@804 | 294 | return self.wrap("A counter proposal to an event invitation has been received.", link=True) |
paul@63 | 295 | |
paul@63 | 296 | def declinecounter(self): |
paul@63 | 297 | |
paul@804 | 298 | "Record a rejection of a counter-proposal." |
paul@63 | 299 | |
paul@804 | 300 | if self._declinecounter(): |
paul@804 | 301 | return self.wrap("Your counter proposal to an event invitation has been declined.", link=True) |
paul@63 | 302 | |
paul@63 | 303 | def publish(self): |
paul@63 | 304 | |
paul@139 | 305 | "Register details of any relevant event." |
paul@63 | 306 | |
paul@831 | 307 | if self._publish(): |
paul@665 | 308 | return self.wrap("Details of an event have been received.") |
paul@63 | 309 | |
paul@63 | 310 | def refresh(self): |
paul@63 | 311 | |
paul@688 | 312 | "Requests to refresh events are handled either here or by the client." |
paul@63 | 313 | |
paul@688 | 314 | if self.is_refreshing(): |
paul@688 | 315 | return self._refresh() |
paul@688 | 316 | else: |
paul@688 | 317 | return self.wrap("A request for updated event details has been received.") |
paul@63 | 318 | |
paul@61 | 319 | def reply(self): |
paul@61 | 320 | |
paul@61 | 321 | "Record replies and notify the recipient." |
paul@61 | 322 | |
paul@665 | 323 | if self._record(from_organiser=False, queue=False): |
paul@668 | 324 | return self.wrap("A reply to an event invitation has been received.") |
paul@61 | 325 | |
paul@61 | 326 | def request(self): |
paul@61 | 327 | |
paul@61 | 328 | "Hold requests and notify the recipient." |
paul@61 | 329 | |
paul@665 | 330 | if self._record(from_organiser=True, queue=True): |
paul@668 | 331 | return self.wrap("An event invitation has been received.") |
paul@60 | 332 | |
paul@725 | 333 | class PersonFreebusy(CommonFreebusy, Handler): |
paul@725 | 334 | |
paul@725 | 335 | "Free/busy handling mechanisms specific to people." |
paul@725 | 336 | |
paul@725 | 337 | def _record_freebusy(self, from_organiser=True): |
paul@725 | 338 | |
paul@725 | 339 | """ |
paul@725 | 340 | Record free/busy information for a message originating from an organiser |
paul@725 | 341 | if 'from_organiser' is set to a true value. |
paul@725 | 342 | """ |
paul@725 | 343 | |
paul@725 | 344 | if from_organiser: |
paul@725 | 345 | organiser_item = self.require_organiser(from_organiser) |
paul@725 | 346 | if not organiser_item: |
paul@725 | 347 | return |
paul@725 | 348 | |
paul@725 | 349 | senders = [organiser_item] |
paul@725 | 350 | else: |
paul@725 | 351 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@725 | 352 | if not oa: |
paul@725 | 353 | return |
paul@725 | 354 | |
paul@725 | 355 | organiser_item, attendees = oa |
paul@725 | 356 | senders = attendees.items() |
paul@725 | 357 | |
paul@725 | 358 | if not senders: |
paul@725 | 359 | return |
paul@725 | 360 | |
paul@725 | 361 | freebusy = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] |
paul@725 | 362 | dtstart = self.obj.get_datetime("DTSTART") |
paul@725 | 363 | dtend = self.obj.get_datetime("DTEND") |
paul@725 | 364 | period = Period(dtstart, dtend, self.get_tzid()) |
paul@725 | 365 | |
paul@725 | 366 | for sender, sender_attr in senders: |
paul@730 | 367 | stored_freebusy = self.store.get_freebusy_for_other(self.user, sender) |
paul@730 | 368 | replace_overlapping(stored_freebusy, period, freebusy) |
paul@730 | 369 | self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) |
paul@725 | 370 | |
paul@725 | 371 | class Freebusy(PersonFreebusy): |
paul@55 | 372 | |
paul@55 | 373 | "A free/busy handler." |
paul@55 | 374 | |
paul@55 | 375 | def publish(self): |
paul@63 | 376 | |
paul@110 | 377 | "Register free/busy information." |
paul@110 | 378 | |
paul@180 | 379 | self._record_freebusy(from_organiser=True) |
paul@180 | 380 | |
paul@180 | 381 | # Produce a message if configured to do so. |
paul@63 | 382 | |
paul@468 | 383 | if self.is_notifying(): |
paul@182 | 384 | return self.wrap("A free/busy update has been received.", link=False) |
paul@55 | 385 | |
paul@55 | 386 | def reply(self): |
paul@55 | 387 | |
paul@63 | 388 | "Record replies and notify the recipient." |
paul@63 | 389 | |
paul@180 | 390 | self._record_freebusy(from_organiser=False) |
paul@180 | 391 | |
paul@180 | 392 | # Produce a message if configured to do so. |
paul@139 | 393 | |
paul@468 | 394 | if self.is_notifying(): |
paul@182 | 395 | return self.wrap("A reply to a free/busy request has been received.", link=False) |
paul@55 | 396 | |
paul@55 | 397 | def request(self): |
paul@55 | 398 | |
paul@55 | 399 | """ |
paul@55 | 400 | Respond to a request by preparing a reply containing free/busy |
paul@468 | 401 | information for the recipient. |
paul@55 | 402 | """ |
paul@55 | 403 | |
paul@180 | 404 | # Produce a reply if configured to do so. |
paul@55 | 405 | |
paul@468 | 406 | if self.is_sharing(): |
paul@180 | 407 | return CommonFreebusy.request(self) |
paul@55 | 408 | |
paul@55 | 409 | # Handler registry. |
paul@55 | 410 | |
paul@55 | 411 | handlers = [ |
paul@55 | 412 | ("VFREEBUSY", Freebusy), |
paul@55 | 413 | ("VEVENT", Event), |
paul@55 | 414 | ] |
paul@55 | 415 | |
paul@55 | 416 | # vim: tabstop=4 expandtab shiftwidth=4 |