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