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