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 event 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 pass 155 156 def reply(self): 157 158 "Since this handler does not send requests, it will not handle replies." 159 160 pass 161 162 def request(self): 163 164 """ 165 Respond to a request by preparing a reply containing accept/decline 166 information for the recipient. 167 168 No support for countering requests is implemented. 169 """ 170 171 response = self._record_and_respond(self._schedule_for_attendee) 172 if response: 173 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 174 175 class Freebusy(Handler, CommonFreebusy): 176 177 "A free/busy handler." 178 179 def publish(self): 180 pass 181 182 def reply(self): 183 184 "Since this handler does not send requests, it will not handle replies." 185 186 pass 187 188 # request provided by CommonFreeBusy.request 189 190 # Handler registry. 191 192 handlers = [ 193 ("VFREEBUSY", Freebusy), 194 ("VEVENT", Event), 195 ] 196 197 # vim: tabstop=4 expandtab shiftwidth=4