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 # Set the complete event if not an additional occurrence. 88 89 event = self.obj.to_node() 90 recurrenceid = self.obj.get_value("RECURRENCE-ID") 91 92 if not recurrenceid: 93 self.store.set_event(attendee, self.uid, event) 94 else: 95 self.store.set_recurrence(attendee, self.uid, recurrenceid, event) 96 97 # Only update free/busy details if the event is scheduled. 98 99 if scheduled: 100 self.update_freebusy(freebusy, attendee, periods) 101 else: 102 self.remove_from_freebusy(freebusy, attendee) 103 104 if self.publisher: 105 self.publisher.set_freebusy(attendee, freebusy) 106 107 return event 108 109 def _cancel_for_attendee(self, attendee, attendee_attr): 110 111 self.store.cancel_event(attendee, self.uid) 112 113 freebusy = self.store.get_freebusy(attendee) 114 self.remove_from_freebusy(freebusy, attendee) 115 116 if self.publisher: 117 self.publisher.set_freebusy(attendee, freebusy) 118 119 return None 120 121 class Event(ResourceHandler): 122 123 "An event handler." 124 125 def add(self): 126 pass 127 128 def cancel(self): 129 130 "Cancel attendance for attendees." 131 132 self._record_and_respond(self._cancel_for_attendee) 133 134 def counter(self): 135 136 "Since this handler does not send requests, it will not handle replies." 137 138 pass 139 140 def declinecounter(self): 141 142 """ 143 Since this handler does not send counter proposals, it will not handle 144 replies to such proposals. 145 """ 146 147 pass 148 149 def publish(self): 150 pass 151 152 def refresh(self): 153 pass 154 155 def reply(self): 156 157 "Since this handler does not send requests, it will not handle replies." 158 159 pass 160 161 def request(self): 162 163 """ 164 Respond to a request by preparing a reply containing accept/decline 165 information for each indicated attendee. 166 167 No support for countering requests is implemented. 168 """ 169 170 response = self._record_and_respond(self._schedule_for_attendee) 171 if response: 172 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 173 174 class Freebusy(Handler, CommonFreebusy): 175 176 "A free/busy handler." 177 178 def publish(self): 179 pass 180 181 def reply(self): 182 183 "Since this handler does not send requests, it will not handle replies." 184 185 pass 186 187 # request provided by CommonFreeBusy.request 188 189 class Journal(ResourceHandler): 190 191 "A journal entry handler." 192 193 def add(self): 194 pass 195 196 def cancel(self): 197 pass 198 199 def publish(self): 200 pass 201 202 class Todo(ResourceHandler): 203 204 "A to-do item handler." 205 206 def add(self): 207 pass 208 209 def cancel(self): 210 pass 211 212 def counter(self): 213 214 "Since this handler does not send requests, it will not handle replies." 215 216 pass 217 218 def declinecounter(self): 219 220 """ 221 Since this handler does not send counter proposals, it will not handle 222 replies to such proposals. 223 """ 224 225 pass 226 227 def publish(self): 228 pass 229 230 def refresh(self): 231 pass 232 233 def reply(self): 234 235 "Since this handler does not send requests, it will not handle replies." 236 237 pass 238 239 def request(self): 240 pass 241 242 # Handler registry. 243 244 handlers = [ 245 ("VFREEBUSY", Freebusy), 246 ("VEVENT", Event), 247 ("VTODO", Todo), 248 ("VJOURNAL", Journal), 249 ] 250 251 # vim: tabstop=4 expandtab shiftwidth=4