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 oa = self.require_organiser_and_attendees(from_organiser) 83 if not oa: 84 return 85 86 organiser_item, attendees = oa 87 senders = from_organiser and [organiser_item] or attendees 88 89 if not senders: 90 return 91 92 freebusy = [] 93 94 for value in self.obj.get_values("FREEBUSY") or []: 95 if not isinstance(value, list): 96 value = [value] 97 for v in value: 98 try: 99 start, end = v.split("/", 1) 100 freebusy.append((start, end)) 101 except ValueError: 102 pass 103 104 for sender, sender_attr in senders: 105 self.store.set_freebusy_for_other(get_uri(self.recipient), freebusy, sender) 106 107 class Event(PersonHandler): 108 109 "An event handler." 110 111 def add(self): 112 113 # NOTE: Queue a suggested modification to any active event. 114 115 return self.wrap("An addition to an event has been received.", link=False) 116 117 def cancel(self): 118 119 "Queue a cancellation of any active event." 120 121 self._record(from_organiser=True, queue=False, cancel=True) 122 return self.wrap("A cancellation has been received.", link=False) 123 124 def counter(self): 125 126 # NOTE: Queue a suggested modification to any active event. 127 128 return self.wrap("A counter proposal has been received.", link=False) 129 130 def declinecounter(self): 131 132 # NOTE: Queue a suggested modification to any active event. 133 134 return self.wrap("A declining counter proposal has been received.", link=False) 135 136 def publish(self): 137 138 "Register details of any relevant event." 139 140 self._record(from_organiser=True, queue=False) 141 return self.wrap("Details of an event have been received.") 142 143 def refresh(self): 144 145 "Update details of any active event." 146 147 self._record(from_organiser=True, queue=False) 148 return self.wrap("An event update has been received.") 149 150 def reply(self): 151 152 "Record replies and notify the recipient." 153 154 self._record(from_organiser=False, queue=False) 155 return self.wrap("A reply has been received.") 156 157 def request(self): 158 159 "Hold requests and notify the recipient." 160 161 self._record(from_organiser=True, queue=True) 162 return self.wrap("A request has been received.") 163 164 class Freebusy(PersonHandler, CommonFreebusy): 165 166 "A free/busy handler." 167 168 def publish(self): 169 170 "Register free/busy information." 171 172 self._record_freebusy(from_organiser=True) 173 174 # Produce a message if configured to do so. 175 176 preferences = Preferences(get_uri(self.recipient)) 177 if preferences.get("freebusy_messages") == "notify": 178 return self.wrap("A free/busy update has been received.", link=False) 179 180 def reply(self): 181 182 "Record replies and notify the recipient." 183 184 self._record_freebusy(from_organiser=False) 185 186 # Produce a message if configured to do so. 187 188 preferences = Preferences(get_uri(self.recipient)) 189 if preferences.get("freebusy_messages") == "notify": 190 return self.wrap("A reply to a free/busy request has been received.", link=False) 191 192 def request(self): 193 194 """ 195 Respond to a request by preparing a reply containing free/busy 196 information for each indicated attendee. 197 """ 198 199 # Produce a reply if configured to do so. 200 201 preferences = Preferences(get_uri(self.recipient)) 202 if preferences.get("freebusy_sharing") == "share": 203 return CommonFreebusy.request(self) 204 205 class Journal(PersonHandler): 206 207 "A journal entry handler." 208 209 def add(self): 210 211 # NOTE: Queue a suggested modification to any active entry. 212 213 return self.wrap("An addition to a journal entry has been received.", link=False) 214 215 def cancel(self): 216 217 # NOTE: Queue a suggested modification to any active entry. 218 219 return self.wrap("A cancellation has been received.", link=False) 220 221 def publish(self): 222 223 # NOTE: Register details of any relevant entry. 224 225 self._record(from_organiser=True, queue=False) 226 return self.wrap("Details of a journal entry have been received.") 227 228 class Todo(PersonHandler): 229 230 "A to-do item handler." 231 232 def add(self): 233 234 # NOTE: Queue a suggested modification to any active item. 235 236 return self.wrap("An addition to an item has been received.", link=False) 237 238 def cancel(self): 239 240 # NOTE: Queue a suggested modification to any active item. 241 242 return self.wrap("A cancellation has been received.", link=False) 243 244 def counter(self): 245 246 # NOTE: Queue a suggested modification to any active item. 247 248 return self.wrap("A counter proposal has been received.", link=False) 249 250 def declinecounter(self): 251 252 # NOTE: Queue a suggested modification to any active item. 253 254 return self.wrap("A declining counter proposal has been received.", link=False) 255 256 def publish(self): 257 258 "Register details of any relevant item." 259 260 self._record(from_organiser=True, queue=False) 261 return self.wrap("Details of an item have been received.") 262 263 def refresh(self): 264 265 "Update details of any active item." 266 267 self._record(from_organiser=True, queue=False) 268 return self.wrap("An item update has been received.") 269 270 def reply(self): 271 272 "Record replies and notify the recipient." 273 274 self._record(from_organiser=False, queue=False) 275 return self.wrap("A reply has been received.") 276 277 def request(self): 278 279 "Hold requests and notify the recipient." 280 281 self._record(from_organiser=True, queue=True) 282 return self.wrap("A request has been received.") 283 284 # Handler registry. 285 286 handlers = [ 287 ("VFREEBUSY", Freebusy), 288 ("VEVENT", Event), 289 ("VTODO", Todo), 290 ("VJOURNAL", Journal), 291 ] 292 293 # vim: tabstop=4 expandtab shiftwidth=4