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.data import get_uri 23 from imiptools.dates import format_datetime 24 from imiptools.handlers import Handler 25 from imiptools.handlers.common import CommonFreebusy 26 from imiptools.period import Period, 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 """ 36 Record details from the current object given a message originating 37 from an organiser if 'from_organiser' is set to a true value, queuing a 38 request if 'queue' is set to a true value, or cancelling an event if 39 'cancel' is set to a true value. 40 """ 41 42 # Obtain valid organiser and attendee details. 43 44 oa = self.require_organiser_and_attendees(from_organiser) 45 if not oa: 46 return False 47 48 (organiser, organiser_attr), attendees = organiser_item, attendees = oa 49 50 # Handle notifications and invitations. 51 52 if from_organiser: 53 54 # Process each attendee separately. 55 56 for attendee, attendee_attr in attendees.items(): 57 58 if not self.have_new_object(attendee): 59 continue 60 61 # Set the complete event or an additional occurrence. 62 63 self.store.set_event(attendee, self.uid, self.recurrenceid, self.obj.to_node()) 64 65 # Remove additional recurrences if handling a complete event. 66 67 if not self.recurrenceid: 68 self.store.remove_recurrences(attendee, self.uid) 69 70 # Queue any request. 71 72 if queue: 73 self.store.queue_request(attendee, self.uid, self.recurrenceid) 74 elif cancel: 75 self.store.cancel_event(attendee, self.uid, self.recurrenceid) 76 77 # No return message will occur to update the free/busy 78 # information, so this is done here. 79 80 freebusy = self.store.get_freebusy(attendee) 81 self.remove_from_freebusy(freebusy) 82 83 self.store.set_freebusy(attendee, freebusy) 84 85 if self.publisher: 86 self.publisher.set_freebusy(attendee, freebusy) 87 88 self.update_freebusy_from_organiser(attendee, organiser_item) 89 90 # As organiser, update attendance from valid attendees. 91 92 else: 93 if self.merge_attendance(attendees, organiser): 94 self.update_freebusy_from_attendees(organiser, attendees) 95 96 return True 97 98 def _record_freebusy(self, from_organiser=True): 99 100 """ 101 Record free/busy information for a message originating from an organiser 102 if 'from_organiser' is set to a true value. 103 """ 104 105 if from_organiser: 106 organiser_item = self.require_organiser(from_organiser) 107 if not organiser_item: 108 return 109 110 senders = [organiser_item] 111 else: 112 oa = self.require_organiser_and_attendees(from_organiser) 113 if not oa: 114 return 115 116 organiser_item, attendees = oa 117 senders = attendees.items() 118 119 if not senders: 120 return 121 122 freebusy = [] 123 124 for value in self.obj.get_values("FREEBUSY") or []: 125 if not isinstance(value, list): 126 value = [value] 127 for v in value: 128 try: 129 start, end = v.split("/", 1) 130 freebusy.append(Period(start, end)) 131 except ValueError: 132 pass 133 134 dtstart = format_datetime(self.obj.get_utc_datetime("DTSTART")) 135 dtend = format_datetime(self.obj.get_utc_datetime("DTEND")) 136 user = get_uri(self.recipient) 137 138 for sender, sender_attr in senders: 139 stored_freebusy = self.store.get_freebusy_for_other(user, sender) 140 replace_overlapping(stored_freebusy, Period(dtstart, dtend), freebusy) 141 self.store.set_freebusy_for_other(user, stored_freebusy, sender) 142 143 class Event(PersonHandler): 144 145 "An event handler." 146 147 def add(self): 148 149 # NOTE: Queue a suggested modification to any active event. 150 151 return self.wrap("An addition to an event has been received.", link=False) 152 153 def cancel(self): 154 155 "Queue a cancellation of any active event." 156 157 self._record(from_organiser=True, queue=False, cancel=True) 158 return self.wrap("A cancellation has been received.", link=False) 159 160 def counter(self): 161 162 # NOTE: Queue a suggested modification to any active event. 163 164 return self.wrap("A counter proposal has been received.", link=False) 165 166 def declinecounter(self): 167 168 # NOTE: Queue a suggested modification to any active event. 169 170 return self.wrap("A declining counter proposal has been received.", link=False) 171 172 def publish(self): 173 174 "Register details of any relevant event." 175 176 self._record(from_organiser=True, queue=False) 177 return self.wrap("Details of an event have been received.") 178 179 def refresh(self): 180 181 "Generate details of any active event." 182 183 # NOTE: Return event details if configured to do so. 184 185 return self.wrap("A request for updated event details has been received.") 186 187 def reply(self): 188 189 "Record replies and notify the recipient." 190 191 self._record(from_organiser=False, queue=False) 192 return self.wrap("A reply has been received.") 193 194 def request(self): 195 196 "Hold requests and notify the recipient." 197 198 self._record(from_organiser=True, queue=True) 199 return self.wrap("A request has been received.") 200 201 class Freebusy(PersonHandler, CommonFreebusy): 202 203 "A free/busy handler." 204 205 def publish(self): 206 207 "Register free/busy information." 208 209 self._record_freebusy(from_organiser=True) 210 211 # Produce a message if configured to do so. 212 213 preferences = Preferences(get_uri(self.recipient)) 214 if preferences.get("freebusy_messages") == "notify": 215 return self.wrap("A free/busy update has been received.", link=False) 216 217 def reply(self): 218 219 "Record replies and notify the recipient." 220 221 self._record_freebusy(from_organiser=False) 222 223 # Produce a message if configured to do so. 224 225 preferences = Preferences(get_uri(self.recipient)) 226 if preferences.get("freebusy_messages") == "notify": 227 return self.wrap("A reply to a free/busy request has been received.", link=False) 228 229 def request(self): 230 231 """ 232 Respond to a request by preparing a reply containing free/busy 233 information for each indicated attendee. 234 """ 235 236 # Produce a reply if configured to do so. 237 238 preferences = Preferences(get_uri(self.recipient)) 239 if preferences.get("freebusy_sharing") == "share": 240 return CommonFreebusy.request(self) 241 242 # Handler registry. 243 244 handlers = [ 245 ("VFREEBUSY", Freebusy), 246 ("VEVENT", Event), 247 ] 248 249 # vim: tabstop=4 expandtab shiftwidth=4