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