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