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@683 | 27 | class PersonHandler(Handler, CommonEvent): |
paul@55 | 28 | |
paul@67 | 29 | "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@110 | 140 | def _record_freebusy(self, from_organiser=True): |
paul@110 | 141 | |
paul@420 | 142 | """ |
paul@420 | 143 | Record free/busy information for a message originating from an organiser |
paul@420 | 144 | if 'from_organiser' is set to a true value. |
paul@420 | 145 | """ |
paul@110 | 146 | |
paul@292 | 147 | if from_organiser: |
paul@292 | 148 | organiser_item = self.require_organiser(from_organiser) |
paul@292 | 149 | if not organiser_item: |
paul@292 | 150 | return |
paul@271 | 151 | |
paul@292 | 152 | senders = [organiser_item] |
paul@292 | 153 | else: |
paul@292 | 154 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@292 | 155 | if not oa: |
paul@292 | 156 | return |
paul@218 | 157 | |
paul@292 | 158 | organiser_item, attendees = oa |
paul@292 | 159 | senders = attendees.items() |
paul@292 | 160 | |
paul@292 | 161 | if not senders: |
paul@292 | 162 | return |
paul@218 | 163 | |
paul@646 | 164 | freebusy = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] |
paul@562 | 165 | dtstart = self.obj.get_datetime("DTSTART") |
paul@562 | 166 | dtend = self.obj.get_datetime("DTEND") |
paul@620 | 167 | period = Period(dtstart, dtend, self.get_tzid()) |
paul@327 | 168 | |
paul@271 | 169 | for sender, sender_attr in senders: |
paul@702 | 170 | stored_freebusy = self.store.get_freebusy_for_other_for_update(self.user, sender) |
paul@702 | 171 | try: |
paul@702 | 172 | replace_overlapping(stored_freebusy, period, freebusy) |
paul@702 | 173 | self.store.set_freebusy_for_other_in_update(self.user, stored_freebusy, sender) |
paul@702 | 174 | finally: |
paul@702 | 175 | self.store.release_freebusy(self.user) |
paul@110 | 176 | |
paul@688 | 177 | def _refresh(self): |
paul@688 | 178 | |
paul@688 | 179 | """ |
paul@688 | 180 | Respond to a refresh message by providing complete event details to |
paul@688 | 181 | attendees. |
paul@688 | 182 | """ |
paul@688 | 183 | |
paul@688 | 184 | # Obtain valid organiser and attendee details. |
paul@688 | 185 | |
paul@688 | 186 | oa = self.require_organiser_and_attendees(False) |
paul@688 | 187 | if not oa: |
paul@688 | 188 | return False |
paul@688 | 189 | |
paul@688 | 190 | (organiser, organiser_attr), attendees = oa |
paul@688 | 191 | |
paul@688 | 192 | # Filter by stored attendees. |
paul@688 | 193 | |
paul@688 | 194 | obj = self.get_stored_object_version() |
paul@688 | 195 | stored_attendees = set(obj.get_values("ATTENDEE")) |
paul@688 | 196 | attendees = stored_attendees.intersection(attendees) |
paul@688 | 197 | |
paul@688 | 198 | if not attendees: |
paul@688 | 199 | return False |
paul@688 | 200 | |
paul@688 | 201 | # Assume that the outcome will be a request. It would not seem |
paul@688 | 202 | # completely bizarre to produce a publishing message instead if a |
paul@688 | 203 | # refresh message was unprovoked. |
paul@688 | 204 | |
paul@688 | 205 | method = "REQUEST" |
paul@688 | 206 | |
paul@694 | 207 | for attendee in attendees: |
paul@694 | 208 | responses = [] |
paul@688 | 209 | |
paul@694 | 210 | # Get the parent event, add SENT-BY details to the organiser. |
paul@694 | 211 | |
paul@694 | 212 | obj = self.get_stored_object_version() |
paul@688 | 213 | |
paul@694 | 214 | if self.is_participating(attendee, obj=obj): |
paul@694 | 215 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@694 | 216 | self.update_sender(organiser_attr) |
paul@694 | 217 | responses.append(obj.to_node()) |
paul@688 | 218 | |
paul@694 | 219 | # Get recurrences. |
paul@694 | 220 | |
paul@694 | 221 | if not self.recurrenceid: |
paul@694 | 222 | for recurrenceid in self.store.get_active_recurrences(self.user, self.uid): |
paul@688 | 223 | |
paul@688 | 224 | # Get the recurrence, add SENT-BY details to the organiser. |
paul@688 | 225 | |
paul@688 | 226 | obj = self.get_stored_object(self.uid, recurrenceid) |
paul@688 | 227 | |
paul@694 | 228 | if self.is_participating(attendee, obj=obj): |
paul@694 | 229 | organiser, organiser_attr = uri_item(obj.get_item("ORGANIZER")) |
paul@694 | 230 | self.update_sender(organiser_attr) |
paul@694 | 231 | responses.append(obj.to_node()) |
paul@694 | 232 | |
paul@694 | 233 | self.add_result(method, [get_address(attendee)], to_part(method, responses)) |
paul@688 | 234 | |
paul@688 | 235 | return True |
paul@688 | 236 | |
paul@216 | 237 | class Event(PersonHandler): |
paul@67 | 238 | |
paul@67 | 239 | "An event handler." |
paul@67 | 240 | |
paul@63 | 241 | def add(self): |
paul@63 | 242 | |
paul@681 | 243 | "Queue a suggested additional recurrence for any active event." |
paul@63 | 244 | |
paul@681 | 245 | if self._add(queue=True): |
paul@681 | 246 | return self.wrap("An addition to an event has been received.") |
paul@63 | 247 | |
paul@63 | 248 | def cancel(self): |
paul@63 | 249 | |
paul@142 | 250 | "Queue a cancellation of any active event." |
paul@63 | 251 | |
paul@665 | 252 | if self._record(from_organiser=True, queue=False, cancel=True): |
paul@668 | 253 | return self.wrap("An event cancellation has been received.", link=False) |
paul@63 | 254 | |
paul@63 | 255 | def counter(self): |
paul@63 | 256 | |
paul@63 | 257 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 258 | |
paul@668 | 259 | return self.wrap("A counter proposal to an event invitation has been received.", link=False) |
paul@63 | 260 | |
paul@63 | 261 | def declinecounter(self): |
paul@63 | 262 | |
paul@665 | 263 | # NOTE: Queue a rejected modification to any active event. |
paul@63 | 264 | |
paul@668 | 265 | return self.wrap("Your counter proposal to an event invitation has been declined.", link=False) |
paul@63 | 266 | |
paul@63 | 267 | def publish(self): |
paul@63 | 268 | |
paul@139 | 269 | "Register details of any relevant event." |
paul@63 | 270 | |
paul@665 | 271 | if self._record(from_organiser=True, queue=False): |
paul@665 | 272 | return self.wrap("Details of an event have been received.") |
paul@63 | 273 | |
paul@63 | 274 | def refresh(self): |
paul@63 | 275 | |
paul@688 | 276 | "Requests to refresh events are handled either here or by the client." |
paul@63 | 277 | |
paul@688 | 278 | if self.is_refreshing(): |
paul@688 | 279 | return self._refresh() |
paul@688 | 280 | else: |
paul@688 | 281 | return self.wrap("A request for updated event details has been received.") |
paul@63 | 282 | |
paul@61 | 283 | def reply(self): |
paul@61 | 284 | |
paul@61 | 285 | "Record replies and notify the recipient." |
paul@61 | 286 | |
paul@665 | 287 | if self._record(from_organiser=False, queue=False): |
paul@668 | 288 | return self.wrap("A reply to an event invitation has been received.") |
paul@61 | 289 | |
paul@61 | 290 | def request(self): |
paul@61 | 291 | |
paul@61 | 292 | "Hold requests and notify the recipient." |
paul@61 | 293 | |
paul@665 | 294 | if self._record(from_organiser=True, queue=True): |
paul@668 | 295 | return self.wrap("An event invitation has been received.") |
paul@60 | 296 | |
paul@108 | 297 | class Freebusy(PersonHandler, CommonFreebusy): |
paul@55 | 298 | |
paul@55 | 299 | "A free/busy handler." |
paul@55 | 300 | |
paul@55 | 301 | def publish(self): |
paul@63 | 302 | |
paul@110 | 303 | "Register free/busy information." |
paul@110 | 304 | |
paul@180 | 305 | self._record_freebusy(from_organiser=True) |
paul@180 | 306 | |
paul@180 | 307 | # Produce a message if configured to do so. |
paul@63 | 308 | |
paul@468 | 309 | if self.is_notifying(): |
paul@182 | 310 | return self.wrap("A free/busy update has been received.", link=False) |
paul@55 | 311 | |
paul@55 | 312 | def reply(self): |
paul@55 | 313 | |
paul@63 | 314 | "Record replies and notify the recipient." |
paul@63 | 315 | |
paul@180 | 316 | self._record_freebusy(from_organiser=False) |
paul@180 | 317 | |
paul@180 | 318 | # Produce a message if configured to do so. |
paul@139 | 319 | |
paul@468 | 320 | if self.is_notifying(): |
paul@182 | 321 | return self.wrap("A reply to a free/busy request has been received.", link=False) |
paul@55 | 322 | |
paul@55 | 323 | def request(self): |
paul@55 | 324 | |
paul@55 | 325 | """ |
paul@55 | 326 | Respond to a request by preparing a reply containing free/busy |
paul@468 | 327 | information for the recipient. |
paul@55 | 328 | """ |
paul@55 | 329 | |
paul@180 | 330 | # Produce a reply if configured to do so. |
paul@55 | 331 | |
paul@468 | 332 | if self.is_sharing(): |
paul@180 | 333 | return CommonFreebusy.request(self) |
paul@55 | 334 | |
paul@55 | 335 | # Handler registry. |
paul@55 | 336 | |
paul@55 | 337 | handlers = [ |
paul@55 | 338 | ("VFREEBUSY", Freebusy), |
paul@55 | 339 | ("VEVENT", Event), |
paul@55 | 340 | ] |
paul@55 | 341 | |
paul@55 | 342 | # vim: tabstop=4 expandtab shiftwidth=4 |