paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@48 | 3 | """ |
paul@48 | 4 | Handlers for a resource. |
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@48 | 20 | """ |
paul@48 | 21 | |
paul@581 | 22 | from imiptools.data import get_address, to_part |
paul@662 | 23 | from imiptools.dates import ValidityError |
paul@418 | 24 | from imiptools.handlers import Handler |
paul@683 | 25 | from imiptools.handlers.common import CommonFreebusy, CommonEvent |
paul@48 | 26 | |
paul@725 | 27 | class ResourceHandler(CommonEvent, Handler): |
paul@131 | 28 | |
paul@131 | 29 | "Handling mechanisms specific to resources." |
paul@131 | 30 | |
paul@131 | 31 | def _record_and_respond(self, handle_for_attendee): |
paul@131 | 32 | |
paul@420 | 33 | """ |
paul@420 | 34 | Record details from the incoming message, using the given |
paul@420 | 35 | 'handle_for_attendee' callable to process any valid message |
paul@420 | 36 | appropriately. |
paul@420 | 37 | """ |
paul@420 | 38 | |
paul@131 | 39 | oa = self.require_organiser_and_attendees() |
paul@131 | 40 | if not oa: |
paul@131 | 41 | return None |
paul@131 | 42 | |
paul@131 | 43 | organiser_item, attendees = oa |
paul@131 | 44 | |
paul@468 | 45 | # Process for the current user, a resource as attendee. |
paul@131 | 46 | |
paul@468 | 47 | if not self.have_new_object() or not self.is_attendee(self.user): |
paul@468 | 48 | return None |
paul@131 | 49 | |
paul@468 | 50 | # Collect response objects produced when handling the request. |
paul@131 | 51 | |
paul@662 | 52 | handle_for_attendee() |
paul@131 | 53 | |
paul@676 | 54 | def _add_for_attendee(self): |
paul@131 | 55 | |
paul@420 | 56 | """ |
paul@676 | 57 | Attempt to add a recurrence to an existing object for the current user. |
paul@676 | 58 | This does not request a response concerning participation, apparently. |
paul@420 | 59 | """ |
paul@420 | 60 | |
paul@676 | 61 | # Ignore unknown objects. |
paul@676 | 62 | # NOTE: We could issue a REFRESH to get such objects. |
paul@676 | 63 | |
paul@676 | 64 | if not self.get_stored_object_version(): |
paul@676 | 65 | return |
paul@676 | 66 | |
paul@676 | 67 | # Record the event as a recurrence of the parent object. |
paul@676 | 68 | |
paul@676 | 69 | self.update_recurrenceid() |
paul@676 | 70 | event = self.obj.to_node() |
paul@676 | 71 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@676 | 72 | |
paul@676 | 73 | # Update free/busy information. |
paul@676 | 74 | |
paul@676 | 75 | self.update_event_in_freebusy(for_organiser=False) |
paul@676 | 76 | |
paul@676 | 77 | def _schedule_for_attendee(self): |
paul@676 | 78 | |
paul@676 | 79 | "Attempt to schedule the current object for the current user." |
paul@676 | 80 | |
paul@662 | 81 | method = "REPLY" |
paul@710 | 82 | attendee_attr = self.obj.get_value_map("ATTENDEE")[self.user] |
paul@662 | 83 | |
paul@655 | 84 | # Check any constraints on the request. |
paul@655 | 85 | |
paul@655 | 86 | try: |
paul@662 | 87 | corrected = self.correct_object() |
paul@655 | 88 | |
paul@655 | 89 | # Refuse to schedule obviously invalid requests. |
paul@655 | 90 | |
paul@662 | 91 | except ValidityError: |
paul@710 | 92 | attendee_attr = self.update_participation(self.obj, "DECLINED") |
paul@291 | 93 | |
paul@655 | 94 | # With a valid request, determine whether the event can be scheduled. |
paul@655 | 95 | |
paul@655 | 96 | else: |
paul@655 | 97 | # Interpretation of periods can depend on the time zone. |
paul@655 | 98 | |
paul@655 | 99 | tzid = self.get_tzid() |
paul@131 | 100 | |
paul@655 | 101 | # If newer than any old version, discard old details from the |
paul@655 | 102 | # free/busy record and check for suitability. |
paul@655 | 103 | |
paul@655 | 104 | periods = self.obj.get_periods(tzid, self.get_window_end()) |
paul@710 | 105 | |
paul@710 | 106 | freebusy = self.store.get_freebusy_for_update(self.user) |
paul@710 | 107 | try: |
paul@710 | 108 | offers = self.store.get_freebusy_offers(self.user) |
paul@655 | 109 | |
paul@710 | 110 | # Check the periods against any scheduled events and against |
paul@710 | 111 | # any outstanding offers. |
paul@662 | 112 | |
paul@710 | 113 | scheduled = self.can_schedule(freebusy, periods) |
paul@710 | 114 | scheduled = scheduled and self.can_schedule(offers, periods) |
paul@662 | 115 | |
paul@710 | 116 | # Where the corrected object can be scheduled, issue a counter |
paul@710 | 117 | # request. |
paul@662 | 118 | |
paul@710 | 119 | if scheduled and corrected: |
paul@710 | 120 | method = "COUNTER" |
paul@662 | 121 | |
paul@710 | 122 | # Find the next available slot if the event cannot be scheduled. |
paul@710 | 123 | |
paul@710 | 124 | #elif not scheduled and len(periods) == 1: |
paul@662 | 125 | |
paul@710 | 126 | # # Find a free period, update the object with the details. |
paul@710 | 127 | |
paul@710 | 128 | # duration = periods[0].get_duration() |
paul@710 | 129 | # free = invert_freebusy(freebusy) |
paul@662 | 130 | |
paul@710 | 131 | # for found in periods_from(free, periods[0]): |
paul@710 | 132 | # # NOTE: Correct the found period first. |
paul@710 | 133 | # if found.get_duration() >= duration |
paul@710 | 134 | # scheduled = True |
paul@710 | 135 | # method = "COUNTER" |
paul@710 | 136 | # # NOTE: Set the period using the original duration. |
paul@710 | 137 | # break |
paul@131 | 138 | |
paul@710 | 139 | # Update the participation of the resource in the object. |
paul@710 | 140 | |
paul@710 | 141 | attendee_attr = self.update_participation(self.obj, |
paul@710 | 142 | scheduled and "ACCEPTED" or "DECLINED") |
paul@468 | 143 | |
paul@710 | 144 | # Update free/busy information. |
paul@710 | 145 | |
paul@710 | 146 | if method == "REPLY": |
paul@710 | 147 | self.update_event_in_freebusy(for_organiser=False) |
paul@710 | 148 | self.remove_event_from_freebusy_offers() |
paul@131 | 149 | |
paul@710 | 150 | # For countered proposals, record the offer in the resource's |
paul@710 | 151 | # free/busy collection. |
paul@334 | 152 | |
paul@710 | 153 | elif method == "COUNTER": |
paul@710 | 154 | self.update_event_in_freebusy_offers() |
paul@131 | 155 | |
paul@710 | 156 | finally: |
paul@710 | 157 | self.store.release_freebusy(self.user) |
paul@662 | 158 | |
paul@710 | 159 | # Set the complete event or an additional occurrence. |
paul@710 | 160 | |
paul@711 | 161 | if method == "REPLY": |
paul@711 | 162 | event = self.obj.to_node() |
paul@711 | 163 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@381 | 164 | |
paul@711 | 165 | # Remove additional recurrences if handling a complete event. |
paul@381 | 166 | |
paul@711 | 167 | if not self.recurrenceid: |
paul@711 | 168 | self.store.remove_recurrences(self.user, self.uid) |
paul@361 | 169 | |
paul@580 | 170 | # Make a version of the object with just this attendee, update the |
paul@580 | 171 | # DTSTAMP in the response, and return the object for sending. |
paul@574 | 172 | |
paul@574 | 173 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@574 | 174 | self.update_dtstamp() |
paul@662 | 175 | self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()])) |
paul@131 | 176 | |
paul@468 | 177 | def _cancel_for_attendee(self): |
paul@131 | 178 | |
paul@420 | 179 | """ |
paul@468 | 180 | Cancel for the current user their attendance of the event described by |
paul@468 | 181 | the current object. |
paul@420 | 182 | """ |
paul@420 | 183 | |
paul@580 | 184 | # Update free/busy information. |
paul@131 | 185 | |
paul@580 | 186 | self.remove_event_from_freebusy() |
paul@580 | 187 | |
paul@672 | 188 | # Update the stored event and cancel it. |
paul@672 | 189 | |
paul@672 | 190 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@672 | 191 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@672 | 192 | |
paul@710 | 193 | def _revoke_for_attendee(self): |
paul@710 | 194 | |
paul@710 | 195 | "Revoke any counter-proposal recorded as a free/busy offer." |
paul@710 | 196 | |
paul@710 | 197 | self.remove_event_from_freebusy_offers() |
paul@710 | 198 | |
paul@131 | 199 | class Event(ResourceHandler): |
paul@48 | 200 | |
paul@48 | 201 | "An event handler." |
paul@48 | 202 | |
paul@48 | 203 | def add(self): |
paul@676 | 204 | |
paul@676 | 205 | "Add a new occurrence to an existing event." |
paul@676 | 206 | |
paul@676 | 207 | self._record_and_respond(self._add_for_attendee) |
paul@48 | 208 | |
paul@48 | 209 | def cancel(self): |
paul@131 | 210 | |
paul@131 | 211 | "Cancel attendance for attendees." |
paul@131 | 212 | |
paul@131 | 213 | self._record_and_respond(self._cancel_for_attendee) |
paul@48 | 214 | |
paul@48 | 215 | def counter(self): |
paul@48 | 216 | |
paul@48 | 217 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 218 | |
paul@48 | 219 | pass |
paul@48 | 220 | |
paul@48 | 221 | def declinecounter(self): |
paul@48 | 222 | |
paul@710 | 223 | "Revoke any counter-proposal." |
paul@48 | 224 | |
paul@710 | 225 | self._record_and_respond(self._revoke_for_attendee) |
paul@48 | 226 | |
paul@48 | 227 | def publish(self): |
paul@676 | 228 | |
paul@676 | 229 | """ |
paul@676 | 230 | Resources only consider events sent as requests, not generally published |
paul@676 | 231 | events. |
paul@676 | 232 | """ |
paul@676 | 233 | |
paul@48 | 234 | pass |
paul@48 | 235 | |
paul@48 | 236 | def refresh(self): |
paul@626 | 237 | |
paul@626 | 238 | """ |
paul@626 | 239 | Refresh messages are typically sent to event organisers, but resources |
paul@626 | 240 | do not act as organisers themselves. |
paul@626 | 241 | """ |
paul@48 | 242 | |
paul@676 | 243 | pass |
paul@676 | 244 | |
paul@48 | 245 | def reply(self): |
paul@48 | 246 | |
paul@48 | 247 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 248 | |
paul@48 | 249 | pass |
paul@48 | 250 | |
paul@48 | 251 | def request(self): |
paul@48 | 252 | |
paul@48 | 253 | """ |
paul@48 | 254 | Respond to a request by preparing a reply containing accept/decline |
paul@468 | 255 | information for the recipient. |
paul@48 | 256 | |
paul@48 | 257 | No support for countering requests is implemented. |
paul@48 | 258 | """ |
paul@48 | 259 | |
paul@662 | 260 | self._record_and_respond(self._schedule_for_attendee) |
paul@48 | 261 | |
paul@725 | 262 | class Freebusy(CommonFreebusy, Handler): |
paul@48 | 263 | |
paul@48 | 264 | "A free/busy handler." |
paul@48 | 265 | |
paul@48 | 266 | def publish(self): |
paul@676 | 267 | |
paul@676 | 268 | "Resources ignore generally published free/busy information." |
paul@676 | 269 | |
paul@48 | 270 | pass |
paul@48 | 271 | |
paul@48 | 272 | def reply(self): |
paul@48 | 273 | |
paul@48 | 274 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 275 | |
paul@48 | 276 | pass |
paul@48 | 277 | |
paul@108 | 278 | # request provided by CommonFreeBusy.request |
paul@48 | 279 | |
paul@48 | 280 | # Handler registry. |
paul@48 | 281 | |
paul@48 | 282 | handlers = [ |
paul@48 | 283 | ("VFREEBUSY", Freebusy), |
paul@48 | 284 | ("VEVENT", Event), |
paul@48 | 285 | ] |
paul@48 | 286 | |
paul@48 | 287 | # vim: tabstop=4 expandtab shiftwidth=4 |