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