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