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