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