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 Schedule the current object for the current user. 65 """ 66 67 # Interpretation of periods can depend on the time zone. 68 69 tzid = self.get_tzid() 70 71 # If newer than any old version, discard old details from the 72 # free/busy record and check for suitability. 73 74 periods = self.obj.get_periods(tzid, self.get_window_end()) 75 freebusy = self.store.get_freebusy(self.user) 76 scheduled = self.can_schedule(freebusy, periods) 77 78 # Update the participation of the resource in the object. 79 80 attendee_attr = self.update_participation(self.obj, 81 scheduled and "ACCEPTED" or "DECLINED") 82 83 # Set the complete event or an additional occurrence. 84 85 event = self.obj.to_node() 86 self.store.set_event(self.user, self.uid, self.recurrenceid, event) 87 88 # Remove additional recurrences if handling a complete event. 89 90 if not self.recurrenceid: 91 self.store.remove_recurrences(self.user, self.uid) 92 93 # Update free/busy information. 94 95 self.update_event_in_freebusy(from_organiser=False) 96 97 # Make a version of the object with just this attendee, update the 98 # DTSTAMP in the response, and return the object for sending. 99 100 self.obj["ATTENDEE"] = [(self.user, attendee_attr)] 101 self.update_dtstamp() 102 return self.obj.to_node() 103 104 def _cancel_for_attendee(self): 105 106 """ 107 Cancel for the current user their attendance of the event described by 108 the current object. 109 """ 110 111 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 112 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 113 114 # Update free/busy information. 115 116 self.remove_event_from_freebusy() 117 118 # No response is required. 119 120 return None 121 122 class Event(ResourceHandler): 123 124 "An event handler." 125 126 def add(self): 127 pass 128 129 def cancel(self): 130 131 "Cancel attendance for attendees." 132 133 self._record_and_respond(self._cancel_for_attendee) 134 135 def counter(self): 136 137 "Since this handler does not send requests, it will not handle replies." 138 139 pass 140 141 def declinecounter(self): 142 143 """ 144 Since this handler does not send counter proposals, it will not handle 145 replies to such proposals. 146 """ 147 148 pass 149 150 def publish(self): 151 pass 152 153 def refresh(self): 154 155 """ 156 Refresh messages are typically sent to event organisers, but resources 157 do not act as organisers themselves. 158 """ 159 160 def reply(self): 161 162 "Since this handler does not send requests, it will not handle replies." 163 164 pass 165 166 def request(self): 167 168 """ 169 Respond to a request by preparing a reply containing accept/decline 170 information for the recipient. 171 172 No support for countering requests is implemented. 173 """ 174 175 response = self._record_and_respond(self._schedule_for_attendee) 176 if response: 177 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 178 179 class Freebusy(Handler, CommonFreebusy): 180 181 "A free/busy handler." 182 183 def publish(self): 184 pass 185 186 def reply(self): 187 188 "Since this handler does not send requests, it will not handle replies." 189 190 pass 191 192 # request provided by CommonFreeBusy.request 193 194 # Handler registry. 195 196 handlers = [ 197 ("VFREEBUSY", Freebusy), 198 ("VEVENT", Event), 199 ] 200 201 # vim: tabstop=4 expandtab shiftwidth=4