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@468 | 64 | Schedule the current object for the current user. |
paul@420 | 65 | """ |
paul@420 | 66 | |
paul@291 | 67 | # Interpretation of periods can depend on the time zone. |
paul@291 | 68 | |
paul@468 | 69 | tzid = self.get_tzid() |
paul@291 | 70 | |
paul@131 | 71 | # If newer than any old version, discard old details from the |
paul@131 | 72 | # free/busy record and check for suitability. |
paul@131 | 73 | |
paul@557 | 74 | periods = self.obj.get_periods(tzid, self.get_window_end()) |
paul@468 | 75 | freebusy = self.store.get_freebusy(self.user) |
paul@131 | 76 | scheduled = self.can_schedule(freebusy, periods) |
paul@131 | 77 | |
paul@580 | 78 | # Update the participation of the resource in the object. |
paul@468 | 79 | |
paul@581 | 80 | attendee_attr = self.update_participation(self.obj, |
paul@581 | 81 | scheduled and "ACCEPTED" or "DECLINED") |
paul@131 | 82 | |
paul@361 | 83 | # Set the complete event or an additional occurrence. |
paul@334 | 84 | |
paul@213 | 85 | event = self.obj.to_node() |
paul@468 | 86 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@131 | 87 | |
paul@381 | 88 | # Remove additional recurrences if handling a complete event. |
paul@381 | 89 | |
paul@381 | 90 | if not self.recurrenceid: |
paul@468 | 91 | self.store.remove_recurrences(self.user, self.uid) |
paul@381 | 92 | |
paul@580 | 93 | # Update free/busy information. |
paul@361 | 94 | |
paul@580 | 95 | self.update_event_in_freebusy(from_organiser=False) |
paul@361 | 96 | |
paul@580 | 97 | # Make a version of the object with just this attendee, update the |
paul@580 | 98 | # DTSTAMP in the response, and return the object for sending. |
paul@574 | 99 | |
paul@574 | 100 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@574 | 101 | self.update_dtstamp() |
paul@614 | 102 | return self.obj.to_node() |
paul@131 | 103 | |
paul@468 | 104 | def _cancel_for_attendee(self): |
paul@131 | 105 | |
paul@420 | 106 | """ |
paul@468 | 107 | Cancel for the current user their attendance of the event described by |
paul@468 | 108 | the current object. |
paul@420 | 109 | """ |
paul@420 | 110 | |
paul@508 | 111 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@468 | 112 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@361 | 113 | |
paul@580 | 114 | # Update free/busy information. |
paul@131 | 115 | |
paul@580 | 116 | self.remove_event_from_freebusy() |
paul@580 | 117 | |
paul@580 | 118 | # No response is required. |
paul@131 | 119 | |
paul@131 | 120 | return None |
paul@131 | 121 | |
paul@131 | 122 | class Event(ResourceHandler): |
paul@48 | 123 | |
paul@48 | 124 | "An event handler." |
paul@48 | 125 | |
paul@48 | 126 | def add(self): |
paul@48 | 127 | pass |
paul@48 | 128 | |
paul@48 | 129 | def cancel(self): |
paul@131 | 130 | |
paul@131 | 131 | "Cancel attendance for attendees." |
paul@131 | 132 | |
paul@131 | 133 | self._record_and_respond(self._cancel_for_attendee) |
paul@48 | 134 | |
paul@48 | 135 | def counter(self): |
paul@48 | 136 | |
paul@48 | 137 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 138 | |
paul@48 | 139 | pass |
paul@48 | 140 | |
paul@48 | 141 | def declinecounter(self): |
paul@48 | 142 | |
paul@48 | 143 | """ |
paul@48 | 144 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 145 | replies to such proposals. |
paul@48 | 146 | """ |
paul@48 | 147 | |
paul@48 | 148 | pass |
paul@48 | 149 | |
paul@48 | 150 | def publish(self): |
paul@48 | 151 | pass |
paul@48 | 152 | |
paul@48 | 153 | def refresh(self): |
paul@48 | 154 | pass |
paul@48 | 155 | |
paul@48 | 156 | def reply(self): |
paul@48 | 157 | |
paul@48 | 158 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 159 | |
paul@48 | 160 | pass |
paul@48 | 161 | |
paul@48 | 162 | def request(self): |
paul@48 | 163 | |
paul@48 | 164 | """ |
paul@48 | 165 | Respond to a request by preparing a reply containing accept/decline |
paul@468 | 166 | information for the recipient. |
paul@48 | 167 | |
paul@48 | 168 | No support for countering requests is implemented. |
paul@48 | 169 | """ |
paul@48 | 170 | |
paul@131 | 171 | response = self._record_and_respond(self._schedule_for_attendee) |
paul@131 | 172 | if response: |
paul@228 | 173 | self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) |
paul@48 | 174 | |
paul@251 | 175 | class Freebusy(Handler, CommonFreebusy): |
paul@48 | 176 | |
paul@48 | 177 | "A free/busy handler." |
paul@48 | 178 | |
paul@48 | 179 | def publish(self): |
paul@48 | 180 | pass |
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@108 | 188 | # request provided by CommonFreeBusy.request |
paul@48 | 189 | |
paul@48 | 190 | # Handler registry. |
paul@48 | 191 | |
paul@48 | 192 | handlers = [ |
paul@48 | 193 | ("VFREEBUSY", Freebusy), |
paul@48 | 194 | ("VEVENT", Event), |
paul@48 | 195 | ] |
paul@48 | 196 | |
paul@48 | 197 | # vim: tabstop=4 expandtab shiftwidth=4 |