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 event = self.obj.to_node() 55 recurrenceid = self.obj.get_value("RECURRENCE-ID") 56 57 if not recurrenceid: 58 self.store.set_event(attendee, self.uid, event) 59 else: 60 self.store.set_recurrence(attendee, self.uid, recurrenceid, event) 61 62 # Queue any request. 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 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, other) 127 replace_overlapping(stored_freebusy, (dtstart, dtend), freebusy) 128 self.store.set_freebusy_for_other(user, other, 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 "Update details of any active event." 169 170 self._record(from_organiser=True, queue=False) 171 return self.wrap("An event update has been received.") 172 173 def reply(self): 174 175 "Record replies and notify the recipient." 176 177 self._record(from_organiser=False, queue=False) 178 return self.wrap("A reply has been received.") 179 180 def request(self): 181 182 "Hold requests and notify the recipient." 183 184 self._record(from_organiser=True, queue=True) 185 return self.wrap("A request has been received.") 186 187 class Freebusy(PersonHandler, CommonFreebusy): 188 189 "A free/busy handler." 190 191 def publish(self): 192 193 "Register free/busy information." 194 195 self._record_freebusy(from_organiser=True) 196 197 # Produce a message if configured to do so. 198 199 preferences = Preferences(get_uri(self.recipient)) 200 if preferences.get("freebusy_messages") == "notify": 201 return self.wrap("A free/busy update has been received.", link=False) 202 203 def reply(self): 204 205 "Record replies and notify the recipient." 206 207 self._record_freebusy(from_organiser=False) 208 209 # Produce a message if configured to do so. 210 211 preferences = Preferences(get_uri(self.recipient)) 212 if preferences.get("freebusy_messages") == "notify": 213 return self.wrap("A reply to a free/busy request has been received.", link=False) 214 215 def request(self): 216 217 """ 218 Respond to a request by preparing a reply containing free/busy 219 information for each indicated attendee. 220 """ 221 222 # Produce a reply if configured to do so. 223 224 preferences = Preferences(get_uri(self.recipient)) 225 if preferences.get("freebusy_sharing") == "share": 226 return CommonFreebusy.request(self) 227 228 class Journal(PersonHandler): 229 230 "A journal entry handler." 231 232 def add(self): 233 234 # NOTE: Queue a suggested modification to any active entry. 235 236 return self.wrap("An addition to a journal entry has been received.", link=False) 237 238 def cancel(self): 239 240 # NOTE: Queue a suggested modification to any active entry. 241 242 return self.wrap("A cancellation has been received.", link=False) 243 244 def publish(self): 245 246 # NOTE: Register details of any relevant entry. 247 248 self._record(from_organiser=True, queue=False) 249 return self.wrap("Details of a journal entry have been received.") 250 251 class Todo(PersonHandler): 252 253 "A to-do item handler." 254 255 def add(self): 256 257 # NOTE: Queue a suggested modification to any active item. 258 259 return self.wrap("An addition to an item has been received.", link=False) 260 261 def cancel(self): 262 263 # NOTE: Queue a suggested modification to any active item. 264 265 return self.wrap("A cancellation has been received.", link=False) 266 267 def counter(self): 268 269 # NOTE: Queue a suggested modification to any active item. 270 271 return self.wrap("A counter proposal has been received.", link=False) 272 273 def declinecounter(self): 274 275 # NOTE: Queue a suggested modification to any active item. 276 277 return self.wrap("A declining counter proposal has been received.", link=False) 278 279 def publish(self): 280 281 "Register details of any relevant item." 282 283 self._record(from_organiser=True, queue=False) 284 return self.wrap("Details of an item have been received.") 285 286 def refresh(self): 287 288 "Update details of any active item." 289 290 self._record(from_organiser=True, queue=False) 291 return self.wrap("An item update has been received.") 292 293 def reply(self): 294 295 "Record replies and notify the recipient." 296 297 self._record(from_organiser=False, queue=False) 298 return self.wrap("A reply has been received.") 299 300 def request(self): 301 302 "Hold requests and notify the recipient." 303 304 self._record(from_organiser=True, queue=True) 305 return self.wrap("A request has been received.") 306 307 # Handler registry. 308 309 handlers = [ 310 ("VFREEBUSY", Freebusy), 311 ("VEVENT", Event), 312 ("VTODO", Todo), 313 ("VJOURNAL", Journal), 314 ] 315 316 # vim: tabstop=4 expandtab shiftwidth=4