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