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@213 | 22 | from imiptools.content import Handler |
paul@213 | 23 | from imiptools.data import get_address, get_uri, to_part |
paul@291 | 24 | from imiptools.dates import get_default_timezone |
paul@108 | 25 | from imiptools.handlers.common import CommonFreebusy |
paul@291 | 26 | from imiptools.profile import Preferences |
paul@48 | 27 | |
paul@131 | 28 | class ResourceHandler(Handler): |
paul@131 | 29 | |
paul@131 | 30 | "Handling mechanisms specific to resources." |
paul@131 | 31 | |
paul@131 | 32 | def _record_and_respond(self, handle_for_attendee): |
paul@131 | 33 | |
paul@131 | 34 | oa = self.require_organiser_and_attendees() |
paul@131 | 35 | if not oa: |
paul@131 | 36 | return None |
paul@131 | 37 | |
paul@131 | 38 | organiser_item, attendees = oa |
paul@131 | 39 | |
paul@131 | 40 | # Process each attendee separately. |
paul@131 | 41 | |
paul@131 | 42 | calendar = [] |
paul@131 | 43 | |
paul@131 | 44 | for attendee, attendee_attr in attendees.items(): |
paul@131 | 45 | |
paul@131 | 46 | # Check for event using UID. |
paul@131 | 47 | |
paul@213 | 48 | if not self.have_new_object(attendee): |
paul@131 | 49 | continue |
paul@131 | 50 | |
paul@131 | 51 | # Collect response objects produced when handling the request. |
paul@131 | 52 | |
paul@131 | 53 | response = handle_for_attendee(attendee, attendee_attr) |
paul@131 | 54 | if response: |
paul@131 | 55 | calendar.append(response) |
paul@131 | 56 | |
paul@131 | 57 | return calendar |
paul@131 | 58 | |
paul@131 | 59 | def _schedule_for_attendee(self, attendee, attendee_attr): |
paul@131 | 60 | |
paul@291 | 61 | # Interpretation of periods can depend on the time zone. |
paul@291 | 62 | |
paul@291 | 63 | preferences = Preferences(attendee) |
paul@291 | 64 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@291 | 65 | |
paul@131 | 66 | # If newer than any old version, discard old details from the |
paul@131 | 67 | # free/busy record and check for suitability. |
paul@131 | 68 | |
paul@291 | 69 | periods = self.obj.get_periods_for_freebusy(tzid) |
paul@173 | 70 | freebusy = self.store.get_freebusy(attendee) |
paul@131 | 71 | scheduled = self.can_schedule(freebusy, periods) |
paul@131 | 72 | |
paul@131 | 73 | attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" |
paul@267 | 74 | if attendee_attr.has_key("RSVP"): |
paul@267 | 75 | del attendee_attr["RSVP"] |
paul@131 | 76 | if self.messenger and self.messenger.sender != get_address(attendee): |
paul@131 | 77 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@131 | 78 | |
paul@131 | 79 | # Make a version of the request with just this attendee. |
paul@131 | 80 | |
paul@213 | 81 | self.obj["ATTENDEE"] = [(attendee, attendee_attr)] |
paul@131 | 82 | |
paul@158 | 83 | # Update the DTSTAMP. |
paul@158 | 84 | |
paul@158 | 85 | self.update_dtstamp() |
paul@158 | 86 | |
paul@334 | 87 | # Set the complete event if not an additional occurrence. |
paul@334 | 88 | |
paul@213 | 89 | event = self.obj.to_node() |
paul@334 | 90 | recurrenceid = self.obj.get_value("RECURRENCE-ID") |
paul@334 | 91 | |
paul@334 | 92 | if not recurrenceid: |
paul@334 | 93 | self.store.set_event(attendee, self.uid, event) |
paul@334 | 94 | else: |
paul@334 | 95 | self.store.set_recurrence(attendee, self.uid, recurrenceid, event) |
paul@131 | 96 | |
paul@131 | 97 | # Only update free/busy details if the event is scheduled. |
paul@131 | 98 | |
paul@131 | 99 | if scheduled: |
paul@131 | 100 | self.update_freebusy(freebusy, attendee, periods) |
paul@131 | 101 | else: |
paul@131 | 102 | self.remove_from_freebusy(freebusy, attendee) |
paul@131 | 103 | |
paul@131 | 104 | if self.publisher: |
paul@131 | 105 | self.publisher.set_freebusy(attendee, freebusy) |
paul@131 | 106 | |
paul@131 | 107 | return event |
paul@131 | 108 | |
paul@131 | 109 | def _cancel_for_attendee(self, attendee, attendee_attr): |
paul@131 | 110 | |
paul@264 | 111 | self.store.cancel_event(attendee, self.uid) |
paul@264 | 112 | |
paul@173 | 113 | freebusy = self.store.get_freebusy(attendee) |
paul@131 | 114 | self.remove_from_freebusy(freebusy, attendee) |
paul@131 | 115 | |
paul@131 | 116 | if self.publisher: |
paul@131 | 117 | self.publisher.set_freebusy(attendee, freebusy) |
paul@131 | 118 | |
paul@131 | 119 | return None |
paul@131 | 120 | |
paul@131 | 121 | class Event(ResourceHandler): |
paul@48 | 122 | |
paul@48 | 123 | "An event handler." |
paul@48 | 124 | |
paul@48 | 125 | def add(self): |
paul@48 | 126 | pass |
paul@48 | 127 | |
paul@48 | 128 | def cancel(self): |
paul@131 | 129 | |
paul@131 | 130 | "Cancel attendance for attendees." |
paul@131 | 131 | |
paul@131 | 132 | self._record_and_respond(self._cancel_for_attendee) |
paul@48 | 133 | |
paul@48 | 134 | def counter(self): |
paul@48 | 135 | |
paul@48 | 136 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 137 | |
paul@48 | 138 | pass |
paul@48 | 139 | |
paul@48 | 140 | def declinecounter(self): |
paul@48 | 141 | |
paul@48 | 142 | """ |
paul@48 | 143 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 144 | replies to such proposals. |
paul@48 | 145 | """ |
paul@48 | 146 | |
paul@48 | 147 | pass |
paul@48 | 148 | |
paul@48 | 149 | def publish(self): |
paul@48 | 150 | pass |
paul@48 | 151 | |
paul@48 | 152 | def refresh(self): |
paul@48 | 153 | pass |
paul@48 | 154 | |
paul@48 | 155 | def reply(self): |
paul@48 | 156 | |
paul@48 | 157 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 158 | |
paul@48 | 159 | pass |
paul@48 | 160 | |
paul@48 | 161 | def request(self): |
paul@48 | 162 | |
paul@48 | 163 | """ |
paul@48 | 164 | Respond to a request by preparing a reply containing accept/decline |
paul@48 | 165 | information for each indicated attendee. |
paul@48 | 166 | |
paul@48 | 167 | No support for countering requests is implemented. |
paul@48 | 168 | """ |
paul@48 | 169 | |
paul@131 | 170 | response = self._record_and_respond(self._schedule_for_attendee) |
paul@131 | 171 | if response: |
paul@228 | 172 | self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) |
paul@48 | 173 | |
paul@251 | 174 | class Freebusy(Handler, CommonFreebusy): |
paul@48 | 175 | |
paul@48 | 176 | "A free/busy handler." |
paul@48 | 177 | |
paul@48 | 178 | def publish(self): |
paul@48 | 179 | pass |
paul@48 | 180 | |
paul@48 | 181 | def reply(self): |
paul@48 | 182 | |
paul@48 | 183 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 184 | |
paul@48 | 185 | pass |
paul@48 | 186 | |
paul@108 | 187 | # request provided by CommonFreeBusy.request |
paul@48 | 188 | |
paul@131 | 189 | class Journal(ResourceHandler): |
paul@48 | 190 | |
paul@48 | 191 | "A journal entry handler." |
paul@48 | 192 | |
paul@48 | 193 | def add(self): |
paul@48 | 194 | pass |
paul@48 | 195 | |
paul@48 | 196 | def cancel(self): |
paul@48 | 197 | pass |
paul@48 | 198 | |
paul@48 | 199 | def publish(self): |
paul@48 | 200 | pass |
paul@48 | 201 | |
paul@131 | 202 | class Todo(ResourceHandler): |
paul@48 | 203 | |
paul@48 | 204 | "A to-do item handler." |
paul@48 | 205 | |
paul@48 | 206 | def add(self): |
paul@48 | 207 | pass |
paul@48 | 208 | |
paul@48 | 209 | def cancel(self): |
paul@48 | 210 | pass |
paul@48 | 211 | |
paul@48 | 212 | def counter(self): |
paul@48 | 213 | |
paul@48 | 214 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 215 | |
paul@48 | 216 | pass |
paul@48 | 217 | |
paul@48 | 218 | def declinecounter(self): |
paul@48 | 219 | |
paul@48 | 220 | """ |
paul@48 | 221 | Since this handler does not send counter proposals, it will not handle |
paul@48 | 222 | replies to such proposals. |
paul@48 | 223 | """ |
paul@48 | 224 | |
paul@48 | 225 | pass |
paul@48 | 226 | |
paul@48 | 227 | def publish(self): |
paul@48 | 228 | pass |
paul@48 | 229 | |
paul@48 | 230 | def refresh(self): |
paul@48 | 231 | pass |
paul@48 | 232 | |
paul@48 | 233 | def reply(self): |
paul@48 | 234 | |
paul@48 | 235 | "Since this handler does not send requests, it will not handle replies." |
paul@48 | 236 | |
paul@48 | 237 | pass |
paul@48 | 238 | |
paul@48 | 239 | def request(self): |
paul@48 | 240 | pass |
paul@48 | 241 | |
paul@48 | 242 | # Handler registry. |
paul@48 | 243 | |
paul@48 | 244 | handlers = [ |
paul@48 | 245 | ("VFREEBUSY", Freebusy), |
paul@48 | 246 | ("VEVENT", Event), |
paul@48 | 247 | ("VTODO", Todo), |
paul@48 | 248 | ("VJOURNAL", Journal), |
paul@48 | 249 | ] |
paul@48 | 250 | |
paul@48 | 251 | # vim: tabstop=4 expandtab shiftwidth=4 |