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