1 #!/usr/bin/env python 2 3 """ 4 Handlers for a resource. 5 6 Copyright (C) 2014, 2015, 2016 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.handlers import Handler 24 from imiptools.handlers.common import CommonFreebusy, CommonEvent 25 from imiptools.handlers.scheduling import apply_scheduling_functions, \ 26 scheduling_functions 27 28 class ResourceHandler(CommonEvent, Handler): 29 30 "Handling mechanisms specific to resources." 31 32 def _process(self, handle_for_attendee): 33 34 """ 35 Record details from the incoming message, using the given 36 'handle_for_attendee' callable to process any valid message 37 appropriately. 38 """ 39 40 oa = self.require_organiser_and_attendees() 41 if not oa: 42 return None 43 44 organiser_item, attendees = oa 45 46 # Process for the current user, a resource as attendee. 47 48 if not self.have_new_object(): 49 return None 50 51 # Collect response objects produced when handling the request. 52 53 handle_for_attendee() 54 55 def _add_for_attendee(self): 56 57 """ 58 Attempt to add a recurrence to an existing object for the current user. 59 This does not request a response concerning participation, apparently. 60 """ 61 62 # Request details where configured, doing so for unknown objects anyway. 63 64 if self.will_refresh(): 65 self.make_refresh() 66 return 67 68 # Record the event as a recurrence of the parent object. 69 70 self.update_recurrenceid() 71 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 72 73 # Remove any previous cancellations involving this event. 74 75 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 76 77 # Update free/busy information. 78 79 self.update_event_in_freebusy(for_organiser=False) 80 81 def _schedule_for_attendee(self): 82 83 "Attempt to schedule the current object for the current user." 84 85 attendee_attr = uri_dict(self.obj.get_value_map("ATTENDEE"))[self.user] 86 scheduled = self.schedule() 87 88 # Update the participation of the resource in the object. 89 # Update free/busy information. 90 91 if scheduled in ("ACCEPTED", "DECLINED"): 92 method = "REPLY" 93 attendee_attr = self.update_participation(scheduled) 94 95 self.update_event_in_freebusy(for_organiser=False) 96 self.remove_event_from_freebusy_offers() 97 98 # Set the complete event or an additional occurrence. 99 100 event = self.obj.to_node() 101 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 102 103 # Remove additional recurrences if handling a complete event. 104 # Also remove any previous cancellations involving this event. 105 106 if not self.recurrenceid: 107 self.store.remove_recurrences(self.user, self.uid) 108 self.store.remove_cancellations(self.user, self.uid) 109 else: 110 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 111 112 # For countered proposals, record the offer in the resource's 113 # free/busy collection. 114 115 elif scheduled == "COUNTER": 116 method = "COUNTER" 117 self.update_event_in_freebusy_offers() 118 119 # For inappropriate periods, reply declining participation. 120 121 else: 122 method = "REPLY" 123 attendee_attr = self.update_participation("DECLINED") 124 125 # Make a version of the object with just this attendee, update the 126 # DTSTAMP in the response, and return the object for sending. 127 128 self.update_sender(attendee_attr) 129 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 130 self.update_dtstamp() 131 self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()])) 132 133 def _cancel_for_attendee(self): 134 135 """ 136 Cancel for the current user their attendance of the event described by 137 the current object. 138 """ 139 140 # Update free/busy information. 141 142 self.remove_event_from_freebusy() 143 144 # Update the stored event and cancel it. 145 146 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 147 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 148 149 def _revoke_for_attendee(self): 150 151 "Revoke any counter-proposal recorded as a free/busy offer." 152 153 self.remove_event_from_freebusy_offers() 154 155 # Scheduling details. 156 157 def schedule(self): 158 159 """ 160 Attempt to schedule the current object, returning an indication of the 161 kind of response to be returned: "COUNTER" for counter-proposals, 162 "ACCEPTED" for acceptances, "DECLINED" for rejections, and None for 163 invalid requests. 164 """ 165 166 # Obtain a list of scheduling functions. 167 168 functions = self.get_preferences().get("scheduling_function", 169 "schedule_in_freebusy").split("\n") 170 171 # Obtain the actual scheduling functions. 172 173 functions = map(scheduling_functions.get, functions) 174 175 return apply_scheduling_functions(functions, self) 176 177 class Event(ResourceHandler): 178 179 "An event handler." 180 181 def add(self): 182 183 "Add a new occurrence to an existing event." 184 185 self._process(self._add_for_attendee) 186 187 def cancel(self): 188 189 "Cancel attendance for attendees." 190 191 self._process(self._cancel_for_attendee) 192 193 def counter(self): 194 195 "Since this handler does not send requests, it will not handle replies." 196 197 pass 198 199 def declinecounter(self): 200 201 "Revoke any counter-proposal." 202 203 self._process(self._revoke_for_attendee) 204 205 def publish(self): 206 207 """ 208 Resources only consider events sent as requests, not generally published 209 events. 210 """ 211 212 pass 213 214 def refresh(self): 215 216 """ 217 Refresh messages are typically sent to event organisers, but resources 218 do not act as organisers themselves. 219 """ 220 221 pass 222 223 def reply(self): 224 225 "Since this handler does not send requests, it will not handle replies." 226 227 pass 228 229 def request(self): 230 231 """ 232 Respond to a request by preparing a reply containing accept/decline 233 information for the recipient. 234 235 No support for countering requests is implemented. 236 """ 237 238 self._process(self._schedule_for_attendee) 239 240 class Freebusy(CommonFreebusy, Handler): 241 242 "A free/busy handler." 243 244 def publish(self): 245 246 "Resources ignore generally published free/busy information." 247 248 self._record_freebusy(from_organiser=True) 249 250 def reply(self): 251 252 "Since this handler does not send requests, it will not handle replies." 253 254 pass 255 256 # request provided by CommonFreeBusy.request 257 258 # Handler registry. 259 260 handlers = [ 261 ("VFREEBUSY", Freebusy), 262 ("VEVENT", Event), 263 ] 264 265 # vim: tabstop=4 expandtab shiftwidth=4