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.data import get_address, to_part, uri_dict 23 from imiptools.dates import ValidityError 24 from imiptools.handlers import Handler 25 from imiptools.handlers.common import CommonFreebusy, CommonEvent 26 27 class ResourceHandler(CommonEvent, Handler): 28 29 "Handling mechanisms specific to resources." 30 31 def _record_and_respond(self, handle_for_attendee): 32 33 """ 34 Record details from the incoming message, using the given 35 'handle_for_attendee' callable to process any valid message 36 appropriately. 37 """ 38 39 oa = self.require_organiser_and_attendees() 40 if not oa: 41 return None 42 43 organiser_item, attendees = oa 44 45 # Process for the current user, a resource as attendee. 46 47 if not self.have_new_object(): 48 return None 49 50 # Collect response objects produced when handling the request. 51 52 handle_for_attendee() 53 54 def _add_for_attendee(self): 55 56 """ 57 Attempt to add a recurrence to an existing object for the current user. 58 This does not request a response concerning participation, apparently. 59 """ 60 61 # Request details where configured, doing so for unknown objects anyway. 62 63 if self.will_refresh(): 64 self.make_refresh() 65 return 66 67 # Record the event as a recurrence of the parent object. 68 69 self.update_recurrenceid() 70 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 71 72 # Update free/busy information. 73 74 self.update_event_in_freebusy(for_organiser=False) 75 76 def _schedule_for_attendee(self): 77 78 "Attempt to schedule the current object for the current user." 79 80 method = "REPLY" 81 attendee_attr = uri_dict(self.obj.get_value_map("ATTENDEE"))[self.user] 82 83 # Check any constraints on the request. 84 85 try: 86 corrected = self.correct_object() 87 88 # Refuse to schedule obviously invalid requests. 89 90 except ValidityError: 91 attendee_attr = self.update_participation(self.obj, "DECLINED") 92 93 # With a valid request, determine whether the event can be scheduled. 94 95 else: 96 # Interpretation of periods can depend on the time zone. 97 98 tzid = self.get_tzid() 99 100 # If newer than any old version, discard old details from the 101 # free/busy record and check for suitability. 102 103 periods = self.obj.get_periods(tzid, self.get_window_end()) 104 105 freebusy = self.store.get_freebusy(self.user) 106 offers = self.store.get_freebusy_offers(self.user) 107 108 # Check the periods against any scheduled events and against 109 # any outstanding offers. 110 111 scheduled = self.can_schedule(freebusy, periods) 112 scheduled = scheduled and self.can_schedule(offers, periods) 113 114 # Where the corrected object can be scheduled, issue a counter 115 # request. 116 117 if scheduled and corrected: 118 method = "COUNTER" 119 120 # Find the next available slot if the event cannot be scheduled. 121 122 #elif not scheduled and len(periods) == 1: 123 124 # # Find a free period, update the object with the details. 125 126 # duration = periods[0].get_duration() 127 # free = invert_freebusy(freebusy) 128 129 # for found in periods_from(free, periods[0]): 130 # # NOTE: Correct the found period first. 131 # if found.get_duration() >= duration 132 # scheduled = True 133 # method = "COUNTER" 134 # # NOTE: Set the period using the original duration. 135 # break 136 137 # Update the participation of the resource in the object. 138 # Update free/busy information. 139 140 if method == "REPLY": 141 attendee_attr = self.update_participation(self.obj, 142 scheduled and "ACCEPTED" or "DECLINED") 143 144 self.update_event_in_freebusy(for_organiser=False) 145 self.remove_event_from_freebusy_offers() 146 147 # For countered proposals, record the offer in the resource's 148 # free/busy collection. 149 150 elif method == "COUNTER": 151 self.update_event_in_freebusy_offers() 152 153 # Set the complete event or an additional occurrence. 154 155 if method == "REPLY": 156 event = self.obj.to_node() 157 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 158 159 # Remove additional recurrences if handling a complete event. 160 161 if not self.recurrenceid: 162 self.store.remove_recurrences(self.user, self.uid) 163 164 # Make a version of the object with just this attendee, update the 165 # DTSTAMP in the response, and return the object for sending. 166 167 self.update_sender(attendee_attr) 168 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 169 self.update_dtstamp() 170 self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()])) 171 172 def _cancel_for_attendee(self): 173 174 """ 175 Cancel for the current user their attendance of the event described by 176 the current object. 177 """ 178 179 # Update free/busy information. 180 181 self.remove_event_from_freebusy() 182 183 # Update the stored event and cancel it. 184 185 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 186 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 187 188 def _revoke_for_attendee(self): 189 190 "Revoke any counter-proposal recorded as a free/busy offer." 191 192 self.remove_event_from_freebusy_offers() 193 194 class Event(ResourceHandler): 195 196 "An event handler." 197 198 def add(self): 199 200 "Add a new occurrence to an existing event." 201 202 self._record_and_respond(self._add_for_attendee) 203 204 def cancel(self): 205 206 "Cancel attendance for attendees." 207 208 self._record_and_respond(self._cancel_for_attendee) 209 210 def counter(self): 211 212 "Since this handler does not send requests, it will not handle replies." 213 214 pass 215 216 def declinecounter(self): 217 218 "Revoke any counter-proposal." 219 220 self._record_and_respond(self._revoke_for_attendee) 221 222 def publish(self): 223 224 """ 225 Resources only consider events sent as requests, not generally published 226 events. 227 """ 228 229 pass 230 231 def refresh(self): 232 233 """ 234 Refresh messages are typically sent to event organisers, but resources 235 do not act as organisers themselves. 236 """ 237 238 pass 239 240 def reply(self): 241 242 "Since this handler does not send requests, it will not handle replies." 243 244 pass 245 246 def request(self): 247 248 """ 249 Respond to a request by preparing a reply containing accept/decline 250 information for the recipient. 251 252 No support for countering requests is implemented. 253 """ 254 255 self._record_and_respond(self._schedule_for_attendee) 256 257 class Freebusy(CommonFreebusy, Handler): 258 259 "A free/busy handler." 260 261 def publish(self): 262 263 "Resources ignore generally published free/busy information." 264 265 pass 266 267 def reply(self): 268 269 "Since this handler does not send requests, it will not handle replies." 270 271 pass 272 273 # request provided by CommonFreeBusy.request 274 275 # Handler registry. 276 277 handlers = [ 278 ("VFREEBUSY", Freebusy), 279 ("VEVENT", Event), 280 ] 281 282 # vim: tabstop=4 expandtab shiftwidth=4