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