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 23 from imiptools.dates import ValidityError 24 from imiptools.handlers import Handler 25 from imiptools.handlers.common import CommonFreebusy, CommonEvent 26 27 class ResourceHandler(Handler, CommonEvent): 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() or not self.is_attendee(self.user): 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 # Ignore unknown objects. 62 # NOTE: We could issue a REFRESH to get such objects. 63 64 if not self.get_stored_object_version(): 65 return 66 67 # Record the event as a recurrence of the parent object. 68 69 self.update_recurrenceid() 70 event = self.obj.to_node() 71 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 72 73 # Update free/busy information. 74 75 self.update_event_in_freebusy(for_organiser=False) 76 77 def _schedule_for_attendee(self): 78 79 "Attempt to schedule the current object for the current user." 80 81 method = "REPLY" 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 scheduled = False 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 freebusy = self.store.get_freebusy(self.user) 105 scheduled = self.can_schedule(freebusy, periods) 106 107 # Where the corrected object can be scheduled, issue a counter 108 # request. 109 110 if scheduled and corrected: 111 method = "COUNTER" 112 113 # Find the next available slot if the event cannot be scheduled. 114 115 #elif not scheduled and len(periods) == 1: 116 117 # # Find a free period, update the object with the details. 118 119 # duration = periods[0].get_duration() 120 # free = invert_freebusy(freebusy) 121 122 # for found in periods_from(free, periods[0]): 123 # # NOTE: Correct the found period first. 124 # if found.get_duration() >= duration 125 # scheduled = True 126 # method = "COUNTER" 127 # # NOTE: Set the period using the original duration. 128 # break 129 130 # Update the participation of the resource in the object. 131 132 if method == "REPLY": 133 attendee_attr = self.update_participation(self.obj, 134 scheduled and "ACCEPTED" or "DECLINED") 135 136 # Set the complete event or an additional occurrence. 137 138 event = self.obj.to_node() 139 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 140 141 # Remove additional recurrences if handling a complete event. 142 143 if not self.recurrenceid: 144 self.store.remove_recurrences(self.user, self.uid) 145 146 # Update free/busy information. 147 148 self.update_event_in_freebusy(for_organiser=False) 149 else: 150 attendee_attr = self.obj.get_value_map("ATTENDEE")[self.user] 151 152 # Make a version of the object with just this attendee, update the 153 # DTSTAMP in the response, and return the object for sending. 154 155 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 156 self.update_dtstamp() 157 self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()])) 158 159 def _cancel_for_attendee(self): 160 161 """ 162 Cancel for the current user their attendance of the event described by 163 the current object. 164 """ 165 166 # Update free/busy information. 167 168 self.remove_event_from_freebusy() 169 170 # Update the stored event and cancel it. 171 172 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 173 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 174 175 class Event(ResourceHandler): 176 177 "An event handler." 178 179 def add(self): 180 181 "Add a new occurrence to an existing event." 182 183 self._record_and_respond(self._add_for_attendee) 184 185 def cancel(self): 186 187 "Cancel attendance for attendees." 188 189 self._record_and_respond(self._cancel_for_attendee) 190 191 def counter(self): 192 193 "Since this handler does not send requests, it will not handle replies." 194 195 pass 196 197 def declinecounter(self): 198 199 """ 200 Since this handler does not send counter proposals, it will not handle 201 replies to such proposals. 202 """ 203 204 pass 205 206 def publish(self): 207 208 """ 209 Resources only consider events sent as requests, not generally published 210 events. 211 """ 212 213 pass 214 215 def refresh(self): 216 217 """ 218 Refresh messages are typically sent to event organisers, but resources 219 do not act as organisers themselves. 220 """ 221 222 pass 223 224 def reply(self): 225 226 "Since this handler does not send requests, it will not handle replies." 227 228 pass 229 230 def request(self): 231 232 """ 233 Respond to a request by preparing a reply containing accept/decline 234 information for the recipient. 235 236 No support for countering requests is implemented. 237 """ 238 239 self._record_and_respond(self._schedule_for_attendee) 240 241 class Freebusy(Handler, CommonFreebusy): 242 243 "A free/busy handler." 244 245 def publish(self): 246 247 "Resources ignore generally published free/busy information." 248 249 pass 250 251 def reply(self): 252 253 "Since this handler does not send requests, it will not handle replies." 254 255 pass 256 257 # request provided by CommonFreeBusy.request 258 259 # Handler registry. 260 261 handlers = [ 262 ("VFREEBUSY", Freebusy), 263 ("VEVENT", Event), 264 ] 265 266 # vim: tabstop=4 expandtab shiftwidth=4