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.obj.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 self.store.cancel_event(attendee, self.uid) 96 97 freebusy = self.store.get_freebusy(attendee) 98 self.remove_from_freebusy(freebusy, attendee) 99 100 if self.publisher: 101 self.publisher.set_freebusy(attendee, freebusy) 102 103 return None 104 105 class Event(ResourceHandler): 106 107 "An event handler." 108 109 def add(self): 110 pass 111 112 def cancel(self): 113 114 "Cancel attendance for attendees." 115 116 self._record_and_respond(self._cancel_for_attendee) 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 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 157 158 class Freebusy(Handler, CommonFreebusy): 159 160 "A free/busy handler." 161 162 def publish(self): 163 pass 164 165 def reply(self): 166 167 "Since this handler does not send requests, it will not handle replies." 168 169 pass 170 171 # request provided by CommonFreeBusy.request 172 173 class Journal(ResourceHandler): 174 175 "A journal entry handler." 176 177 def add(self): 178 pass 179 180 def cancel(self): 181 pass 182 183 def publish(self): 184 pass 185 186 class Todo(ResourceHandler): 187 188 "A to-do item handler." 189 190 def add(self): 191 pass 192 193 def cancel(self): 194 pass 195 196 def counter(self): 197 198 "Since this handler does not send requests, it will not handle replies." 199 200 pass 201 202 def declinecounter(self): 203 204 """ 205 Since this handler does not send counter proposals, it will not handle 206 replies to such proposals. 207 """ 208 209 pass 210 211 def publish(self): 212 pass 213 214 def refresh(self): 215 pass 216 217 def reply(self): 218 219 "Since this handler does not send requests, it will not handle replies." 220 221 pass 222 223 def request(self): 224 pass 225 226 # Handler registry. 227 228 handlers = [ 229 ("VFREEBUSY", Freebusy), 230 ("VEVENT", Event), 231 ("VTODO", Todo), 232 ("VJOURNAL", Journal), 233 ] 234 235 # vim: tabstop=4 expandtab shiftwidth=4