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 attendee_attr.has_key("RSVP"): 68 del attendee_attr["RSVP"] 69 if self.messenger and self.messenger.sender != get_address(attendee): 70 attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) 71 72 # Make a version of the request with just this attendee. 73 74 self.obj["ATTENDEE"] = [(attendee, attendee_attr)] 75 76 # Update the DTSTAMP. 77 78 self.update_dtstamp() 79 80 event = self.obj.to_node() 81 self.store.set_event(attendee, self.uid, event) 82 83 # Only update free/busy details if the event is scheduled. 84 85 if scheduled: 86 self.update_freebusy(freebusy, attendee, periods) 87 else: 88 self.remove_from_freebusy(freebusy, attendee) 89 90 if self.publisher: 91 self.publisher.set_freebusy(attendee, freebusy) 92 93 return event 94 95 def _cancel_for_attendee(self, attendee, attendee_attr): 96 97 self.store.cancel_event(attendee, self.uid) 98 99 freebusy = self.store.get_freebusy(attendee) 100 self.remove_from_freebusy(freebusy, attendee) 101 102 if self.publisher: 103 self.publisher.set_freebusy(attendee, freebusy) 104 105 return None 106 107 class Event(ResourceHandler): 108 109 "An event handler." 110 111 def add(self): 112 pass 113 114 def cancel(self): 115 116 "Cancel attendance for attendees." 117 118 self._record_and_respond(self._cancel_for_attendee) 119 120 def counter(self): 121 122 "Since this handler does not send requests, it will not handle replies." 123 124 pass 125 126 def declinecounter(self): 127 128 """ 129 Since this handler does not send counter proposals, it will not handle 130 replies to such proposals. 131 """ 132 133 pass 134 135 def publish(self): 136 pass 137 138 def refresh(self): 139 pass 140 141 def reply(self): 142 143 "Since this handler does not send requests, it will not handle replies." 144 145 pass 146 147 def request(self): 148 149 """ 150 Respond to a request by preparing a reply containing accept/decline 151 information for each indicated attendee. 152 153 No support for countering requests is implemented. 154 """ 155 156 response = self._record_and_respond(self._schedule_for_attendee) 157 if response: 158 self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) 159 160 class Freebusy(Handler, 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