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