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 dtstart = self.obj.get_utc_datetime("DTSTART") 140 dtend = self.obj.get_utc_datetime("DTEND") 141 142 for sender, sender_attr in senders: 143 stored_freebusy = self.store.get_freebusy_for_other(self.user, sender) 144 replace_overlapping(stored_freebusy, Period(dtstart, dtend), freebusy) 145 self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) 146 147 class Event(PersonHandler): 148 149 "An event handler." 150 151 def add(self): 152 153 # NOTE: Queue a suggested modification to any active event. 154 155 return self.wrap("An addition to an event has been received.", link=False) 156 157 def cancel(self): 158 159 "Queue a cancellation of any active event." 160 161 self._record(from_organiser=True, queue=False, cancel=True) 162 return self.wrap("A cancellation has been received.", link=False) 163 164 def counter(self): 165 166 # NOTE: Queue a suggested modification to any active event. 167 168 return self.wrap("A counter proposal has been received.", link=False) 169 170 def declinecounter(self): 171 172 # NOTE: Queue a suggested modification to any active event. 173 174 return self.wrap("A declining counter proposal has been received.", link=False) 175 176 def publish(self): 177 178 "Register details of any relevant event." 179 180 self._record(from_organiser=True, queue=False) 181 return self.wrap("Details of an event have been received.") 182 183 def refresh(self): 184 185 "Generate details of any active event." 186 187 # NOTE: Return event details if configured to do so. 188 189 return self.wrap("A request for updated event details has been received.") 190 191 def reply(self): 192 193 "Record replies and notify the recipient." 194 195 self._record(from_organiser=False, queue=False) 196 return self.wrap("A reply has been received.") 197 198 def request(self): 199 200 "Hold requests and notify the recipient." 201 202 self._record(from_organiser=True, queue=True) 203 return self.wrap("A request has been received.") 204 205 class Freebusy(PersonHandler, CommonFreebusy): 206 207 "A free/busy handler." 208 209 def publish(self): 210 211 "Register free/busy information." 212 213 self._record_freebusy(from_organiser=True) 214 215 # Produce a message if configured to do so. 216 217 if self.is_notifying(): 218 return self.wrap("A free/busy update has been received.", link=False) 219 220 def reply(self): 221 222 "Record replies and notify the recipient." 223 224 self._record_freebusy(from_organiser=False) 225 226 # Produce a message if configured to do so. 227 228 if self.is_notifying(): 229 return self.wrap("A reply to a free/busy request has been received.", link=False) 230 231 def request(self): 232 233 """ 234 Respond to a request by preparing a reply containing free/busy 235 information for the recipient. 236 """ 237 238 # Produce a reply if configured to do so. 239 240 if self.is_sharing(): 241 return CommonFreebusy.request(self) 242 243 # Handler registry. 244 245 handlers = [ 246 ("VFREEBUSY", Freebusy), 247 ("VEVENT", Event), 248 ] 249 250 # vim: tabstop=4 expandtab shiftwidth=4