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