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