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