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