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