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, Outgoing 26 27 class ResourceHandler(Handler, Outgoing): 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 _schedule_for_attendee(self): 55 56 """ 57 Attempt to schedule the current object for the current user. 58 """ 59 60 method = "REPLY" 61 62 # Check any constraints on the request. 63 64 try: 65 corrected = self.correct_object() 66 67 # Refuse to schedule obviously invalid requests. 68 69 except ValidityError: 70 scheduled = False 71 72 # With a valid request, determine whether the event can be scheduled. 73 74 else: 75 # Interpretation of periods can depend on the time zone. 76 77 tzid = self.get_tzid() 78 79 # If newer than any old version, discard old details from the 80 # free/busy record and check for suitability. 81 82 periods = self.obj.get_periods(tzid, self.get_window_end()) 83 freebusy = self.store.get_freebusy(self.user) 84 scheduled = self.can_schedule(freebusy, periods) 85 86 # Where the corrected object can be scheduled, issue a counter 87 # request. 88 89 if scheduled and corrected: 90 method = "COUNTER" 91 92 # Find the next available slot if the event cannot be scheduled. 93 94 #elif not scheduled and len(periods) == 1: 95 96 # # Find a free period, update the object with the details. 97 98 # duration = periods[0].get_duration() 99 # free = invert_freebusy(freebusy) 100 101 # for found in periods_from(free, periods[0]): 102 # # NOTE: Correct the found period first. 103 # if found.get_duration() >= duration 104 # scheduled = True 105 # method = "COUNTER" 106 # # NOTE: Set the period using the original duration. 107 # break 108 109 # Update the participation of the resource in the object. 110 111 if method == "REPLY": 112 attendee_attr = self.update_participation(self.obj, 113 scheduled and "ACCEPTED" or "DECLINED") 114 115 # Set the complete event or an additional occurrence. 116 117 event = self.obj.to_node() 118 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 119 120 # Remove additional recurrences if handling a complete event. 121 122 if not self.recurrenceid: 123 self.store.remove_recurrences(self.user, self.uid) 124 125 # Update free/busy information. 126 127 self.update_event_in_freebusy(from_organiser=False) 128 else: 129 attendee_attr = self.obj.get_value_map("ATTENDEE")[self.user] 130 131 # Make a version of the object with just this attendee, update the 132 # DTSTAMP in the response, and return the object for sending. 133 134 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 135 self.update_dtstamp() 136 self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()])) 137 138 def _cancel_for_attendee(self): 139 140 """ 141 Cancel for the current user their attendance of the event described by 142 the current object. 143 """ 144 145 # Update free/busy information. 146 147 self.remove_event_from_freebusy() 148 149 # Update the stored event and cancel it. 150 151 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 152 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 153 154 class Event(ResourceHandler): 155 156 "An event handler." 157 158 def add(self): 159 pass 160 161 def cancel(self): 162 163 "Cancel attendance for attendees." 164 165 self._record_and_respond(self._cancel_for_attendee) 166 167 def counter(self): 168 169 "Since this handler does not send requests, it will not handle replies." 170 171 pass 172 173 def declinecounter(self): 174 175 """ 176 Since this handler does not send counter proposals, it will not handle 177 replies to such proposals. 178 """ 179 180 pass 181 182 def publish(self): 183 pass 184 185 def refresh(self): 186 187 """ 188 Refresh messages are typically sent to event organisers, but resources 189 do not act as organisers themselves. 190 """ 191 192 def reply(self): 193 194 "Since this handler does not send requests, it will not handle replies." 195 196 pass 197 198 def request(self): 199 200 """ 201 Respond to a request by preparing a reply containing accept/decline 202 information for the recipient. 203 204 No support for countering requests is implemented. 205 """ 206 207 self._record_and_respond(self._schedule_for_attendee) 208 209 class Freebusy(Handler, CommonFreebusy): 210 211 "A free/busy handler." 212 213 def publish(self): 214 pass 215 216 def reply(self): 217 218 "Since this handler does not send requests, it will not handle replies." 219 220 pass 221 222 # request provided by CommonFreeBusy.request 223 224 # Handler registry. 225 226 handlers = [ 227 ("VFREEBUSY", Freebusy), 228 ("VEVENT", Event), 229 ] 230 231 # vim: tabstop=4 expandtab shiftwidth=4