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@418 | 23 | from imiptools.handlers import Handler |
paul@580 | 24 | from imiptools.handlers.common import CommonFreebusy, Outgoing |
paul@48 | 25 | |
paul@580 | 26 | class ResourceHandler(Handler, Outgoing): |
paul@131 | 27 | |
paul@131 | 28 | "Handling mechanisms specific to resources." |
paul@131 | 29 | |
paul@131 | 30 | def _record_and_respond(self, handle_for_attendee): |
paul@131 | 31 | |
paul@420 | 32 | """ |
paul@420 | 33 | Record details from the incoming message, using the given |
paul@420 | 34 | 'handle_for_attendee' callable to process any valid message |
paul@420 | 35 | appropriately. |
paul@420 | 36 | """ |
paul@420 | 37 | |
paul@131 | 38 | oa = self.require_organiser_and_attendees() |
paul@131 | 39 | if not oa: |
paul@131 | 40 | return None |
paul@131 | 41 | |
paul@131 | 42 | organiser_item, attendees = oa |
paul@131 | 43 | |
paul@131 | 44 | # Process each attendee separately. |
paul@131 | 45 | |
paul@131 | 46 | calendar = [] |
paul@131 | 47 | |
paul@468 | 48 | # Process for the current user, a resource as attendee. |
paul@131 | 49 | |
paul@468 | 50 | if not self.have_new_object() or not self.is_attendee(self.user): |
paul@468 | 51 | return None |
paul@131 | 52 | |
paul@468 | 53 | # Collect response objects produced when handling the request. |
paul@131 | 54 | |
paul@468 | 55 | response = handle_for_attendee() |
paul@468 | 56 | if response: |
paul@468 | 57 | calendar.append(response) |
paul@131 | 58 | |
paul@131 | 59 | return calendar |
paul@131 | 60 | |
paul@468 | 61 | def _schedule_for_attendee(self): |
paul@131 | 62 | |
paul@420 | 63 | """ |
paul@655 | 64 | Attempt to schedule the current object for the current user. |
paul@420 | 65 | """ |
paul@420 | 66 | |
paul@655 | 67 | # Check any constraints on the request. |
paul@655 | 68 | |
paul@655 | 69 | try: |
paul@655 | 70 | check = self.check_object() |
paul@655 | 71 | |
paul@655 | 72 | # NOTE: Support countering by correcting any invalid values and |
paul@655 | 73 | # NOTE: attempting to schedule using the corrected values. |
paul@291 | 74 | |
paul@655 | 75 | if check: |
paul@655 | 76 | raise self.ValidityError |
paul@655 | 77 | |
paul@655 | 78 | # Refuse to schedule obviously invalid requests. |
paul@655 | 79 | |
paul@655 | 80 | except self.ValidityError: |
paul@655 | 81 | scheduled = False |
paul@291 | 82 | |
paul@655 | 83 | # With a valid request, determine whether the event can be scheduled. |
paul@655 | 84 | |
paul@655 | 85 | else: |
paul@655 | 86 | # Interpretation of periods can depend on the time zone. |
paul@655 | 87 | |
paul@655 | 88 | tzid = self.get_tzid() |
paul@131 | 89 | |
paul@655 | 90 | # If newer than any old version, discard old details from the |
paul@655 | 91 | # free/busy record and check for suitability. |
paul@655 | 92 | |
paul@655 | 93 | periods = self.obj.get_periods(tzid, self.get_window_end()) |
paul@655 | 94 | freebusy = self.store.get_freebusy(self.user) |
paul@655 | 95 | scheduled = self.can_schedule(freebusy, periods) |
paul@655 | 96 | |
paul@655 | 97 | # NOTE: Support countering by finding the next available slot if |
paul@655 | 98 | # NOTE: the event cannot be scheduled. |
paul@131 | 99 | |
paul@580 | 100 | # Update the participation of the resource in the object. |
paul@468 | 101 | |
paul@581 | 102 | attendee_attr = self.update_participation(self.obj, |
paul@581 | 103 | scheduled and "ACCEPTED" or "DECLINED") |
paul@131 | 104 | |
paul@361 | 105 | # Set the complete event or an additional occurrence. |
paul@334 | 106 | |
paul@213 | 107 | event = self.obj.to_node() |
paul@468 | 108 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@131 | 109 | |
paul@381 | 110 | # Remove additional recurrences if handling a complete event. |
paul@381 | 111 | |
paul@381 | 112 | if not self.recurrenceid: |
paul@468 | 113 | self.store.remove_recurrences(self.user, self.uid) |
paul@381 | 114 | |
paul@580 | 115 | # Update free/busy information. |
paul@361 | 116 | |
paul@580 | 117 | self.update_event_in_freebusy(from_organiser=False) |
paul@361 | 118 | |
paul@580 | 119 | # Make a version of the object with just this attendee, update the |
paul@580 | 120 | # DTSTAMP in the response, and return the object for sending. |
paul@574 | 121 | |
paul@574 | 122 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@574 | 123 | self.update_dtstamp() |
paul@614 | 124 | return self.obj.to_node() |
paul@131 | 125 | |
paul@468 | 126 | def _cancel_for_attendee(self): |
paul@131 | 127 | |
paul@420 | 128 | """ |
paul@468 | 129 | Cancel for the current user their attendance of the event described by |
paul@468 | 130 | the current object. |
paul@420 | 131 | """ |
paul@420 | 132 | |
paul@508 | 133 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@468 | 134 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@361 | 135 | |
paul@580 | 136 | # Update free/busy information. |
paul@131 | 137 | |
paul@580 | 138 | self.remove_event_from_freebusy() |
paul@580 | 139 | |
paul@580 | 140 | # No response is required. |
paul@131 | 141 | |
paul@131 | 142 | return None |
paul@131 | 143 | |
paul@131 | 144 | class Event(ResourceHandler): |
paul@48 | 145 | |
paul@48 | 146 | "An event handler." |
paul@48 | 147 | |
paul@48 | 148 | def add(self): |
paul@48 | 149 | pass |
paul@48 | 150 | |
paul@48 | 151 | def cancel(self): |
paul@131 | 152 | |
paul@131 | 153 | "Cancel attendance for attendees." |
paul@131 | 154 | |
paul@131 | 155 | self._record_and_respond(self._cancel_for_attendee) |
paul@48 | 156 | |
paul@48 | 157 | def counter(self): |
paul@48 | 158 | |
paul@48 | 159 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 160 | |
paul@48 | 161 | pass |
paul@48 | 162 | |
paul@48 | 163 | def declinecounter(self): |
paul@48 | 164 | |
paul@48 | 165 | """ |
paul@48 | 166 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 167 | replies to such proposals. |
paul@48 | 168 | """ |
paul@48 | 169 | |
paul@48 | 170 | pass |
paul@48 | 171 | |
paul@48 | 172 | def publish(self): |
paul@48 | 173 | pass |
paul@48 | 174 | |
paul@48 | 175 | def refresh(self): |
paul@626 | 176 | |
paul@626 | 177 | """ |
paul@626 | 178 | Refresh messages are typically sent to event organisers, but resources |
paul@626 | 179 | do not act as organisers themselves. |
paul@626 | 180 | """ |
paul@48 | 181 | |
paul@48 | 182 | def reply(self): |
paul@48 | 183 | |
paul@48 | 184 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 185 | |
paul@48 | 186 | pass |
paul@48 | 187 | |
paul@48 | 188 | def request(self): |
paul@48 | 189 | |
paul@48 | 190 | """ |
paul@48 | 191 | Respond to a request by preparing a reply containing accept/decline |
paul@468 | 192 | information for the recipient. |
paul@48 | 193 | |
paul@48 | 194 | No support for countering requests is implemented. |
paul@48 | 195 | """ |
paul@48 | 196 | |
paul@131 | 197 | response = self._record_and_respond(self._schedule_for_attendee) |
paul@131 | 198 | if response: |
paul@228 | 199 | self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) |
paul@48 | 200 | |
paul@251 | 201 | class Freebusy(Handler, CommonFreebusy): |
paul@48 | 202 | |
paul@48 | 203 | "A free/busy handler." |
paul@48 | 204 | |
paul@48 | 205 | def publish(self): |
paul@48 | 206 | pass |
paul@48 | 207 | |
paul@48 | 208 | def reply(self): |
paul@48 | 209 | |
paul@48 | 210 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 211 | |
paul@48 | 212 | pass |
paul@48 | 213 | |
paul@108 | 214 | # request provided by CommonFreeBusy.request |
paul@48 | 215 | |
paul@48 | 216 | # Handler registry. |
paul@48 | 217 | |
paul@48 | 218 | handlers = [ |
paul@48 | 219 | ("VFREEBUSY", Freebusy), |
paul@48 | 220 | ("VEVENT", Event), |
paul@48 | 221 | ] |
paul@48 | 222 | |
paul@48 | 223 | # vim: tabstop=4 expandtab shiftwidth=4 |