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