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