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