1 #!/usr/bin/env python 2 3 """ 4 Handlers for a resource. 5 6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools.content import Handler 23 from imiptools.data import get_address, get_uri, get_window_end, to_part 24 from imiptools.dates import get_default_timezone 25 from imiptools.handlers.common import CommonFreebusy 26 from imiptools.period import remove_affected_period 27 28 class ResourceHandler(Handler): 29 30 "Handling mechanisms specific to resources." 31 32 def _record_and_respond(self, handle_for_attendee): 33 34 oa = self.require_organiser_and_attendees() 35 if not oa: 36 return None 37 38 organiser_item, attendees = oa 39 40 # Process each attendee separately. 41 42 calendar = [] 43 44 for attendee, attendee_attr in attendees.items(): 45 46 # Check for event using UID. 47 48 if not self.have_new_object(attendee): 49 continue 50 51 # Collect response objects produced when handling the request. 52 53 response = handle_for_attendee(attendee, attendee_attr) 54 if response: 55 calendar.append(response) 56 57 return calendar 58 59 def _schedule_for_attendee(self, attendee, attendee_attr): 60 61 # Interpretation of periods can depend on the time zone. 62 63 tzid = self.get_tzid(attendee) 64 65 # If newer than any old version, discard old details from the 66 # free/busy record and check for suitability. 67 68 periods = self.obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) 69 freebusy = self.store.get_freebusy(attendee) 70 scheduled = self.can_schedule(freebusy, periods) 71 72 attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" 73 if attendee_attr.has_key("RSVP"): 74 del attendee_attr["RSVP"] 75 if self.messenger and self.messenger.sender != get_address(attendee): 76 attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) 77 78 # Make a version of the request with just this attendee. 79 80 self.obj["ATTENDEE"] = [(attendee, attendee_attr)] 81 82 # Update the DTSTAMP. 83 84 self.update_dtstamp() 85 86 # Set the complete event or an additional occurrence. 87 88 event = self.obj.to_node() 89 self.store.set_event(attendee, self.uid, self.recurrenceid, event) 90 91 # Only update free/busy details if the event is scheduled. 92 93 if scheduled: 94 self.update_freebusy(freebusy, periods) 95 else: 96 self.remove_from_freebusy(freebusy) 97 98 # Subtract any recurrences from the free/busy details of a parent 99 # object. 100 101 for recurrenceid in self.store.get_recurrences(attendee, self.uid): 102 remove_affected_period(freebusy, self.uid, recurrenceid) 103 104 self.store.set_freebusy(attendee, freebusy) 105 106 if self.publisher: 107 self.publisher.set_freebusy(attendee, freebusy) 108 109 return event 110 111 def _cancel_for_attendee(self, attendee, attendee_attr): 112 113 self.store.cancel_event(attendee, self.uid, self.recurrenceid) 114 115 freebusy = self.store.get_freebusy(attendee) 116 self.remove_from_freebusy(freebusy) 117 118 self.store.set_freebusy(attendee, freebusy) 119 120 if self.publisher: 121 self.publisher.set_freebusy(attendee, freebusy) 122 123 return None 124 125 class Event(ResourceHandler): 126 127 "An event handler." 128 129 def add(self): 130 pass 131 132 def cancel(self): 133 134 "Cancel attendance for attendees." 135 136 self._record_and_respond(self._cancel_for_attendee) 137 138 def counter(self): 139 140 "Since this handler does not send requests, it will not handle replies." 141 142 pass 143 144 def declinecounter(self): 145 146 """ 147 Since this handler does not send counter proposals, it will not handle 148 replies to such proposals. 149 """ 150 151 pass 152 153 def publish(self): 154 pass 155 156 def refresh(self): 157 pass 158 159 def reply(self): 160 161 "Since this handler does not send requests, it will not handle replies." 162 163 pass 164 165 def request(self): 166 167 """ 168 Respond to a request by preparing a reply containing accept/decline 169 information for each indicated attendee. 170 171 No support for countering requests is implemented. 172 """ 173 174 response = self._record_and_respond(self._schedule_for_attendee) 175 if response: 176 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 177 178 class Freebusy(Handler, CommonFreebusy): 179 180 "A free/busy handler." 181 182 def publish(self): 183 pass 184 185 def reply(self): 186 187 "Since this handler does not send requests, it will not handle replies." 188 189 pass 190 191 # request provided by CommonFreeBusy.request 192 193 class Journal(ResourceHandler): 194 195 "A journal entry handler." 196 197 def add(self): 198 pass 199 200 def cancel(self): 201 pass 202 203 def publish(self): 204 pass 205 206 class Todo(ResourceHandler): 207 208 "A to-do item handler." 209 210 def add(self): 211 pass 212 213 def cancel(self): 214 pass 215 216 def counter(self): 217 218 "Since this handler does not send requests, it will not handle replies." 219 220 pass 221 222 def declinecounter(self): 223 224 """ 225 Since this handler does not send counter proposals, it will not handle 226 replies to such proposals. 227 """ 228 229 pass 230 231 def publish(self): 232 pass 233 234 def refresh(self): 235 pass 236 237 def reply(self): 238 239 "Since this handler does not send requests, it will not handle replies." 240 241 pass 242 243 def request(self): 244 pass 245 246 # Handler registry. 247 248 handlers = [ 249 ("VFREEBUSY", Freebusy), 250 ("VEVENT", Event), 251 ("VTODO", Todo), 252 ("VJOURNAL", Journal), 253 ] 254 255 # vim: tabstop=4 expandtab shiftwidth=4