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