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