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