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@468 | 22 | from imiptools.data import get_address, get_uri, to_part |
paul@418 | 23 | from imiptools.handlers import Handler |
paul@108 | 24 | from imiptools.handlers.common import CommonFreebusy |
paul@380 | 25 | from imiptools.period import remove_affected_period |
paul@48 | 26 | |
paul@131 | 27 | class ResourceHandler(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@131 | 45 | # Process each attendee separately. |
paul@131 | 46 | |
paul@131 | 47 | calendar = [] |
paul@131 | 48 | |
paul@468 | 49 | # Process for the current user, a resource as attendee. |
paul@131 | 50 | |
paul@468 | 51 | if not self.have_new_object() or not self.is_attendee(self.user): |
paul@468 | 52 | return None |
paul@131 | 53 | |
paul@468 | 54 | # Collect response objects produced when handling the request. |
paul@131 | 55 | |
paul@468 | 56 | response = handle_for_attendee() |
paul@468 | 57 | if response: |
paul@468 | 58 | calendar.append(response) |
paul@131 | 59 | |
paul@131 | 60 | return calendar |
paul@131 | 61 | |
paul@468 | 62 | def _schedule_for_attendee(self): |
paul@131 | 63 | |
paul@420 | 64 | """ |
paul@468 | 65 | Schedule the current object for the current user. |
paul@420 | 66 | """ |
paul@420 | 67 | |
paul@291 | 68 | # Interpretation of periods can depend on the time zone. |
paul@291 | 69 | |
paul@468 | 70 | tzid = self.get_tzid() |
paul@291 | 71 | |
paul@131 | 72 | # If newer than any old version, discard old details from the |
paul@131 | 73 | # free/busy record and check for suitability. |
paul@131 | 74 | |
paul@468 | 75 | periods = self.obj.get_periods_for_freebusy(tzid, self.get_window_end()) |
paul@468 | 76 | freebusy = self.store.get_freebusy(self.user) |
paul@131 | 77 | scheduled = self.can_schedule(freebusy, periods) |
paul@131 | 78 | |
paul@468 | 79 | attendee_attr = self.obj.get_value_map("ATTENDEE").get(self.user) |
paul@468 | 80 | |
paul@131 | 81 | attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" |
paul@267 | 82 | if attendee_attr.has_key("RSVP"): |
paul@267 | 83 | del attendee_attr["RSVP"] |
paul@468 | 84 | if self.messenger and self.messenger.sender != get_address(self.user): |
paul@131 | 85 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@131 | 86 | |
paul@131 | 87 | # Make a version of the request with just this attendee. |
paul@131 | 88 | |
paul@468 | 89 | self.obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@131 | 90 | |
paul@158 | 91 | # Update the DTSTAMP. |
paul@158 | 92 | |
paul@158 | 93 | self.update_dtstamp() |
paul@158 | 94 | |
paul@361 | 95 | # Set the complete event or an additional occurrence. |
paul@334 | 96 | |
paul@213 | 97 | event = self.obj.to_node() |
paul@468 | 98 | self.store.set_event(self.user, self.uid, self.recurrenceid, event) |
paul@131 | 99 | |
paul@381 | 100 | # Remove additional recurrences if handling a complete event. |
paul@381 | 101 | |
paul@381 | 102 | if not self.recurrenceid: |
paul@468 | 103 | self.store.remove_recurrences(self.user, self.uid) |
paul@381 | 104 | |
paul@131 | 105 | # Only update free/busy details if the event is scheduled. |
paul@131 | 106 | |
paul@131 | 107 | if scheduled: |
paul@361 | 108 | self.update_freebusy(freebusy, periods) |
paul@131 | 109 | else: |
paul@361 | 110 | self.remove_from_freebusy(freebusy) |
paul@361 | 111 | |
paul@381 | 112 | # Remove either original recurrence or additional recurrence |
paul@381 | 113 | # details depending on whether an additional recurrence or a |
paul@381 | 114 | # complete event are being handled, respectively. |
paul@361 | 115 | |
paul@381 | 116 | self.remove_freebusy_for_recurrences(freebusy) |
paul@468 | 117 | self.store.set_freebusy(self.user, freebusy) |
paul@131 | 118 | |
paul@468 | 119 | if self.publisher and self.is_sharing(): |
paul@468 | 120 | self.publisher.set_freebusy(self.user, freebusy) |
paul@131 | 121 | |
paul@131 | 122 | return event |
paul@131 | 123 | |
paul@468 | 124 | def _cancel_for_attendee(self): |
paul@131 | 125 | |
paul@420 | 126 | """ |
paul@468 | 127 | Cancel for the current user their attendance of the event described by |
paul@468 | 128 | the current object. |
paul@420 | 129 | """ |
paul@420 | 130 | |
paul@508 | 131 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@468 | 132 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@361 | 133 | |
paul@468 | 134 | freebusy = self.store.get_freebusy(self.user) |
paul@468 | 135 | self.remove_from_freebusy(freebusy) |
paul@468 | 136 | self.store.set_freebusy(self.user, freebusy) |
paul@131 | 137 | |
paul@468 | 138 | if self.publisher and self.is_sharing(): |
paul@468 | 139 | self.publisher.set_freebusy(self.user, freebusy) |
paul@131 | 140 | |
paul@131 | 141 | return None |
paul@131 | 142 | |
paul@131 | 143 | class Event(ResourceHandler): |
paul@48 | 144 | |
paul@48 | 145 | "An event handler." |
paul@48 | 146 | |
paul@48 | 147 | def add(self): |
paul@48 | 148 | pass |
paul@48 | 149 | |
paul@48 | 150 | def cancel(self): |
paul@131 | 151 | |
paul@131 | 152 | "Cancel attendance for attendees." |
paul@131 | 153 | |
paul@131 | 154 | self._record_and_respond(self._cancel_for_attendee) |
paul@48 | 155 | |
paul@48 | 156 | def counter(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 declinecounter(self): |
paul@48 | 163 | |
paul@48 | 164 | """ |
paul@48 | 165 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 166 | replies to such proposals. |
paul@48 | 167 | """ |
paul@48 | 168 | |
paul@48 | 169 | pass |
paul@48 | 170 | |
paul@48 | 171 | def publish(self): |
paul@48 | 172 | pass |
paul@48 | 173 | |
paul@48 | 174 | def refresh(self): |
paul@48 | 175 | pass |
paul@48 | 176 | |
paul@48 | 177 | def reply(self): |
paul@48 | 178 | |
paul@48 | 179 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 180 | |
paul@48 | 181 | pass |
paul@48 | 182 | |
paul@48 | 183 | def request(self): |
paul@48 | 184 | |
paul@48 | 185 | """ |
paul@48 | 186 | Respond to a request by preparing a reply containing accept/decline |
paul@468 | 187 | information for the recipient. |
paul@48 | 188 | |
paul@48 | 189 | No support for countering requests is implemented. |
paul@48 | 190 | """ |
paul@48 | 191 | |
paul@131 | 192 | response = self._record_and_respond(self._schedule_for_attendee) |
paul@131 | 193 | if response: |
paul@228 | 194 | self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) |
paul@48 | 195 | |
paul@251 | 196 | class Freebusy(Handler, CommonFreebusy): |
paul@48 | 197 | |
paul@48 | 198 | "A free/busy handler." |
paul@48 | 199 | |
paul@48 | 200 | def publish(self): |
paul@48 | 201 | pass |
paul@48 | 202 | |
paul@48 | 203 | def reply(self): |
paul@48 | 204 | |
paul@48 | 205 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 206 | |
paul@48 | 207 | pass |
paul@48 | 208 | |
paul@108 | 209 | # request provided by CommonFreeBusy.request |
paul@48 | 210 | |
paul@48 | 211 | # Handler registry. |
paul@48 | 212 | |
paul@48 | 213 | handlers = [ |
paul@48 | 214 | ("VFREEBUSY", Freebusy), |
paul@48 | 215 | ("VEVENT", Event), |
paul@48 | 216 | ] |
paul@48 | 217 | |
paul@48 | 218 | # vim: tabstop=4 expandtab shiftwidth=4 |