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