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.handlers import Handler 24 from imiptools.handlers.common import CommonFreebusy, Outgoing 25 26 class ResourceHandler(Handler, Outgoing): 27 28 "Handling mechanisms specific to resources." 29 30 def _record_and_respond(self, handle_for_attendee): 31 32 """ 33 Record details from the incoming message, using the given 34 'handle_for_attendee' callable to process any valid message 35 appropriately. 36 """ 37 38 oa = self.require_organiser_and_attendees() 39 if not oa: 40 return None 41 42 organiser_item, attendees = oa 43 44 # Process each attendee separately. 45 46 calendar = [] 47 48 # Process for the current user, a resource as attendee. 49 50 if not self.have_new_object() or not self.is_attendee(self.user): 51 return None 52 53 # Collect response objects produced when handling the request. 54 55 response = handle_for_attendee() 56 if response: 57 calendar.append(response) 58 59 return calendar 60 61 def _schedule_for_attendee(self): 62 63 """ 64 Attempt to schedule the current object for the current user. 65 """ 66 67 # Check any constraints on the request. 68 69 try: 70 check = self.check_object() 71 72 # NOTE: Support countering by correcting any invalid values and 73 # NOTE: attempting to schedule using the corrected values. 74 75 if check: 76 raise self.ValidityError 77 78 # Refuse to schedule obviously invalid requests. 79 80 except self.ValidityError: 81 scheduled = False 82 83 # With a valid request, determine whether the event can be scheduled. 84 85 else: 86 # Interpretation of periods can depend on the time zone. 87 88 tzid = self.get_tzid() 89 90 # If newer than any old version, discard old details from the 91 # free/busy record and check for suitability. 92 93 periods = self.obj.get_periods(tzid, self.get_window_end()) 94 freebusy = self.store.get_freebusy(self.user) 95 scheduled = self.can_schedule(freebusy, periods) 96 97 # NOTE: Support countering by finding the next available slot if 98 # NOTE: the event cannot be scheduled. 99 100 # Update the participation of the resource in the object. 101 102 attendee_attr = self.update_participation(self.obj, 103 scheduled and "ACCEPTED" or "DECLINED") 104 105 # Set the complete event or an additional occurrence. 106 107 event = self.obj.to_node() 108 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 109 110 # Remove additional recurrences if handling a complete event. 111 112 if not self.recurrenceid: 113 self.store.remove_recurrences(self.user, self.uid) 114 115 # Update free/busy information. 116 117 self.update_event_in_freebusy(from_organiser=False) 118 119 # Make a version of the object with just this attendee, update the 120 # DTSTAMP in the response, and return the object for sending. 121 122 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 123 self.update_dtstamp() 124 return self.obj.to_node() 125 126 def _cancel_for_attendee(self): 127 128 """ 129 Cancel for the current user their attendance of the event described by 130 the current object. 131 """ 132 133 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 134 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 135 136 # Update free/busy information. 137 138 self.remove_event_from_freebusy() 139 140 # No response is required. 141 142 return None 143 144 class Event(ResourceHandler): 145 146 "An event handler." 147 148 def add(self): 149 pass 150 151 def cancel(self): 152 153 "Cancel attendance for attendees." 154 155 self._record_and_respond(self._cancel_for_attendee) 156 157 def counter(self): 158 159 "Since this handler does not send requests, it will not handle replies." 160 161 pass 162 163 def declinecounter(self): 164 165 """ 166 Since this handler does not send counter proposals, it will not handle 167 replies to such proposals. 168 """ 169 170 pass 171 172 def publish(self): 173 pass 174 175 def refresh(self): 176 177 """ 178 Refresh messages are typically sent to event organisers, but resources 179 do not act as organisers themselves. 180 """ 181 182 def reply(self): 183 184 "Since this handler does not send requests, it will not handle replies." 185 186 pass 187 188 def request(self): 189 190 """ 191 Respond to a request by preparing a reply containing accept/decline 192 information for the recipient. 193 194 No support for countering requests is implemented. 195 """ 196 197 response = self._record_and_respond(self._schedule_for_attendee) 198 if response: 199 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 200 201 class Freebusy(Handler, CommonFreebusy): 202 203 "A free/busy handler." 204 205 def publish(self): 206 pass 207 208 def reply(self): 209 210 "Since this handler does not send requests, it will not handle replies." 211 212 pass 213 214 # request provided by CommonFreeBusy.request 215 216 # Handler registry. 217 218 handlers = [ 219 ("VFREEBUSY", Freebusy), 220 ("VEVENT", Event), 221 ] 222 223 # vim: tabstop=4 expandtab shiftwidth=4