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