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, get_uri, get_window_end, to_part 23 from imiptools.dates import get_default_timezone 24 from imiptools.handlers import Handler 25 from imiptools.handlers.common import CommonFreebusy 26 from imiptools.period import remove_affected_period 27 28 class ResourceHandler(Handler): 29 30 "Handling mechanisms specific to resources." 31 32 def _record_and_respond(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 each attendee separately. 47 48 calendar = [] 49 50 for attendee, attendee_attr in attendees.items(): 51 52 # Check for event using UID. 53 54 if not self.have_new_object(attendee): 55 continue 56 57 # Collect response objects produced when handling the request. 58 59 response = handle_for_attendee(attendee, attendee_attr) 60 if response: 61 calendar.append(response) 62 63 return calendar 64 65 def _schedule_for_attendee(self, attendee, attendee_attr): 66 67 """ 68 Schedule for the given 'attendee' and accompanying 'attendee_attr' the 69 current object. 70 """ 71 72 # Interpretation of periods can depend on the time zone. 73 74 tzid = self.get_tzid(attendee) 75 76 # If newer than any old version, discard old details from the 77 # free/busy record and check for suitability. 78 79 periods = self.obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) 80 freebusy = self.store.get_freebusy(attendee) 81 scheduled = self.can_schedule(freebusy, periods) 82 83 attendee_attr["PARTSTAT"] = scheduled and "ACCEPTED" or "DECLINED" 84 if attendee_attr.has_key("RSVP"): 85 del attendee_attr["RSVP"] 86 if self.messenger and self.messenger.sender != get_address(attendee): 87 attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) 88 89 # Make a version of the request with just this attendee. 90 91 self.obj["ATTENDEE"] = [(attendee, attendee_attr)] 92 93 # Update the DTSTAMP. 94 95 self.update_dtstamp() 96 97 # Set the complete event or an additional occurrence. 98 99 event = self.obj.to_node() 100 self.store.set_event(attendee, self.uid, self.recurrenceid, event) 101 102 # Remove additional recurrences if handling a complete event. 103 104 if not self.recurrenceid: 105 self.store.remove_recurrences(attendee, self.uid) 106 107 # Only update free/busy details if the event is scheduled. 108 109 if scheduled: 110 self.update_freebusy(freebusy, periods) 111 else: 112 self.remove_from_freebusy(freebusy) 113 114 # Remove either original recurrence or additional recurrence 115 # details depending on whether an additional recurrence or a 116 # complete event are being handled, respectively. 117 118 self.remove_freebusy_for_recurrences(freebusy) 119 self.store.set_freebusy(attendee, freebusy) 120 121 if self.publisher: 122 self.publisher.set_freebusy(attendee, freebusy) 123 124 return event 125 126 def _cancel_for_attendee(self, attendee, attendee_attr): 127 128 """ 129 Cancel for the given 'attendee' and accompanying 'attendee_attr' their 130 attendance of the event described by the current object. 131 """ 132 133 self.store.cancel_event(attendee, self.uid, self.recurrenceid) 134 135 freebusy = self.store.get_freebusy(attendee) 136 self.remove_from_freebusy(freebusy) 137 138 self.store.set_freebusy(attendee, freebusy) 139 140 if self.publisher: 141 self.publisher.set_freebusy(attendee, freebusy) 142 143 return None 144 145 class Event(ResourceHandler): 146 147 "An event handler." 148 149 def add(self): 150 pass 151 152 def cancel(self): 153 154 "Cancel attendance for attendees." 155 156 self._record_and_respond(self._cancel_for_attendee) 157 158 def counter(self): 159 160 "Since this handler does not send requests, it will not handle replies." 161 162 pass 163 164 def declinecounter(self): 165 166 """ 167 Since this handler does not send counter proposals, it will not handle 168 replies to such proposals. 169 """ 170 171 pass 172 173 def publish(self): 174 pass 175 176 def refresh(self): 177 pass 178 179 def reply(self): 180 181 "Since this handler does not send requests, it will not handle replies." 182 183 pass 184 185 def request(self): 186 187 """ 188 Respond to a request by preparing a reply containing accept/decline 189 information for each indicated attendee. 190 191 No support for countering requests is implemented. 192 """ 193 194 response = self._record_and_respond(self._schedule_for_attendee) 195 if response: 196 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 197 198 class Freebusy(Handler, CommonFreebusy): 199 200 "A free/busy handler." 201 202 def publish(self): 203 pass 204 205 def reply(self): 206 207 "Since this handler does not send requests, it will not handle replies." 208 209 pass 210 211 # request provided by CommonFreeBusy.request 212 213 # Handler registry. 214 215 handlers = [ 216 ("VFREEBUSY", Freebusy), 217 ("VEVENT", Event), 218 ] 219 220 # vim: tabstop=4 expandtab shiftwidth=4