1 #!/usr/bin/env python 2 3 """ 4 Handlers for a person for whom scheduling is performed. 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, uri_dict, uri_items 24 from imiptools.handlers.common import CommonFreebusy 25 from imiptools.profile import Preferences 26 27 class PersonHandler(Handler): 28 29 "Handling mechanisms specific to people." 30 31 def _record_and_deliver(self, from_organiser=True, queue=False, cancel=False): 32 33 oa = self.require_organiser_and_attendees(from_organiser) 34 if not oa: 35 return False 36 37 (organiser, organiser_attr), attendees = organiser_item, attendees = oa 38 39 # Handle notifications and invitations. 40 41 if from_organiser: 42 43 # Process each attendee separately. 44 45 for attendee, attendee_attr in attendees.items(): 46 47 if not self.have_new_object(attendee): 48 continue 49 50 # Record other party free/busy information. 51 52 if organiser != attendee: 53 freebusy = self.store.get_freebusy_for_other(attendee, organiser) 54 55 if organiser_attr.get("PARTSTAT") != "DECLINED": 56 self.update_freebusy_for_other(freebusy, attendee, organiser, self.obj.get_periods()) 57 else: 58 self.remove_from_freebusy_for_other(freebusy, attendee, organiser) 59 60 # Store the object and queue any request. 61 62 self.store.set_event(attendee, self.uid, self.obj.to_node()) 63 64 if queue: 65 self.store.queue_request(attendee, self.uid) 66 elif cancel: 67 self.store.cancel_event(attendee, self.uid) 68 69 # No return message will occur to update the free/busy 70 # information, so this is done here. 71 72 freebusy = self.store.get_freebusy(attendee) 73 self.remove_from_freebusy(freebusy, attendee) 74 75 if self.publisher: 76 self.publisher.set_freebusy(attendee, freebusy) 77 78 # As organiser, update attendance. 79 80 else: 81 obj = self.get_object(organiser) 82 83 if obj and self.have_new_object(organiser, obj=obj): 84 85 # Get attendee details in a usable form. 86 87 attendee_map = uri_dict(obj.get_value_map("ATTENDEE")) 88 89 for attendee, attendee_attr in attendees.items(): 90 91 # Update attendance in the loaded object. 92 93 attendee_map[attendee] = attendee_attr 94 95 # Record other party free/busy information. 96 97 if organiser != attendee: 98 freebusy = self.store.get_freebusy_for_other(organiser, attendee) 99 100 if attendee_attr.get("PARTSTAT") != "DECLINED": 101 self.update_freebusy_for_other(freebusy, organiser, attendee, self.obj.get_periods()) 102 else: 103 self.remove_from_freebusy_for_other(freebusy, organiser, attendee) 104 105 # Set the new details and store the object. 106 107 obj["ATTENDEE"] = attendee_map.items() 108 109 self.store.set_event(organiser, self.uid, obj.to_node()) 110 111 return True 112 113 def _record_freebusy(self, from_organiser=True): 114 115 "Record free/busy information for the received information." 116 117 senders = self.obj.get_items(from_organiser and "ORGANIZER" or "ATTENDEE") 118 119 if not senders: 120 return 121 122 freebusy = [] 123 124 for value in self.obj.get_values("FREEBUSY") or []: 125 if not isinstance(value, list): 126 value = [value] 127 for v in value: 128 try: 129 start, end = v.split("/", 1) 130 freebusy.append((start, end)) 131 except ValueError: 132 pass 133 134 for sender, sender_attr in uri_items(senders): 135 self.store.set_freebusy_for_other(get_uri(self.recipient), freebusy, sender) 136 137 class Event(PersonHandler): 138 139 "An event handler." 140 141 def add(self): 142 143 # NOTE: Queue a suggested modification to any active event. 144 145 return self.wrap("An addition to an event has been received.", link=False) 146 147 def cancel(self): 148 149 "Queue a cancellation of any active event." 150 151 self._record_and_deliver(from_organiser=True, queue=False, cancel=True) 152 return self.wrap("A cancellation has been received.", link=False) 153 154 def counter(self): 155 156 # NOTE: Queue a suggested modification to any active event. 157 158 return self.wrap("A counter proposal has been received.", link=False) 159 160 def declinecounter(self): 161 162 # NOTE: Queue a suggested modification to any active event. 163 164 return self.wrap("A declining counter proposal has been received.", link=False) 165 166 def publish(self): 167 168 "Register details of any relevant event." 169 170 self._record_and_deliver(from_organiser=True, queue=False) 171 return self.wrap("Details of an event have been received.") 172 173 def refresh(self): 174 175 "Update details of any active event." 176 177 self._record_and_deliver(from_organiser=True, queue=False) 178 return self.wrap("An event update has been received.") 179 180 def reply(self): 181 182 "Record replies and notify the recipient." 183 184 self._record_and_deliver(from_organiser=False, queue=False) 185 return self.wrap("A reply has been received.") 186 187 def request(self): 188 189 "Hold requests and notify the recipient." 190 191 self._record_and_deliver(from_organiser=True, queue=True) 192 return self.wrap("A request has been received.") 193 194 class Freebusy(PersonHandler, CommonFreebusy): 195 196 "A free/busy handler." 197 198 def publish(self): 199 200 "Register free/busy information." 201 202 self._record_freebusy(from_organiser=True) 203 204 # Produce a message if configured to do so. 205 206 preferences = Preferences(get_uri(self.recipient)) 207 if preferences.get("freebusy_messages") == "notify": 208 return self.wrap("A free/busy update has been received.", link=False) 209 210 def reply(self): 211 212 "Record replies and notify the recipient." 213 214 self._record_freebusy(from_organiser=False) 215 216 # Produce a message if configured to do so. 217 218 preferences = Preferences(get_uri(self.recipient)) 219 if preferences.get("freebusy_messages") == "notify": 220 return self.wrap("A reply to a free/busy request has been received.", link=False) 221 222 def request(self): 223 224 """ 225 Respond to a request by preparing a reply containing free/busy 226 information for each indicated attendee. 227 """ 228 229 # Produce a reply if configured to do so. 230 231 preferences = Preferences(get_uri(self.recipient)) 232 if preferences.get("freebusy_sharing") == "share": 233 return CommonFreebusy.request(self) 234 235 class Journal(PersonHandler): 236 237 "A journal entry handler." 238 239 def add(self): 240 241 # NOTE: Queue a suggested modification to any active entry. 242 243 return self.wrap("An addition to a journal entry has been received.", link=False) 244 245 def cancel(self): 246 247 # NOTE: Queue a suggested modification to any active entry. 248 249 return self.wrap("A cancellation has been received.", link=False) 250 251 def publish(self): 252 253 # NOTE: Register details of any relevant entry. 254 255 self._record_and_deliver(from_organiser=True, queue=False) 256 return self.wrap("Details of a journal entry have been received.") 257 258 class Todo(PersonHandler): 259 260 "A to-do item handler." 261 262 def add(self): 263 264 # NOTE: Queue a suggested modification to any active item. 265 266 return self.wrap("An addition to an item has been received.", link=False) 267 268 def cancel(self): 269 270 # NOTE: Queue a suggested modification to any active item. 271 272 return self.wrap("A cancellation has been received.", link=False) 273 274 def counter(self): 275 276 # NOTE: Queue a suggested modification to any active item. 277 278 return self.wrap("A counter proposal has been received.", link=False) 279 280 def declinecounter(self): 281 282 # NOTE: Queue a suggested modification to any active item. 283 284 return self.wrap("A declining counter proposal has been received.", link=False) 285 286 def publish(self): 287 288 "Register details of any relevant item." 289 290 self._record_and_deliver(from_organiser=True, queue=False) 291 return self.wrap("Details of an item have been received.") 292 293 def refresh(self): 294 295 "Update details of any active item." 296 297 self._record_and_deliver(from_organiser=True, queue=False) 298 return self.wrap("An item update has been received.") 299 300 def reply(self): 301 302 "Record replies and notify the recipient." 303 304 self._record_and_deliver(from_organiser=False, queue=False) 305 return self.wrap("A reply has been received.") 306 307 def request(self): 308 309 "Hold requests and notify the recipient." 310 311 self._record_and_deliver(from_organiser=True, queue=True) 312 return self.wrap("A request has been received.") 313 314 # Handler registry. 315 316 handlers = [ 317 ("VFREEBUSY", Freebusy), 318 ("VEVENT", Event), 319 ("VTODO", Todo), 320 ("VJOURNAL", Journal), 321 ] 322 323 # vim: tabstop=4 expandtab shiftwidth=4