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 uri_dict 23 from imiptools.handlers import Handler 24 from imiptools.handlers.common import CommonFreebusy, Outgoing 25 from imiptools.period import FreeBusyPeriod, Period, replace_overlapping 26 27 class PersonHandler(Handler, Outgoing): 28 29 "Handling mechanisms specific to people." 30 31 def _record(self, from_organiser=True, queue=False, cancel=False): 32 33 """ 34 Record details from the current object given a message originating 35 from an organiser if 'from_organiser' is set to a true value, queuing a 36 request if 'queue' is set to a true value, or cancelling an event if 37 'cancel' is set to a true value. 38 """ 39 40 # Obtain valid organiser and attendee details. 41 42 oa = self.require_organiser_and_attendees(from_organiser) 43 if not oa: 44 return False 45 46 (organiser, organiser_attr), attendees = oa 47 48 # Handle notifications and invitations. 49 50 if from_organiser: 51 52 # Process for the current user, an attendee. 53 54 if not self.have_new_object() or not self.is_attendee(self.user): 55 return False 56 57 # Indicate the organiser's implicit attendance if mentioned in the 58 # cancellation message. 59 60 if cancel: 61 obj_attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) 62 if obj_attendees.has_key(organiser): 63 obj_attendees[organiser]["PARTSTAT"] = "DECLINED" 64 65 # Set the complete event or an additional occurrence. 66 67 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 68 69 # Remove additional recurrences if handling a complete event. 70 71 if not self.recurrenceid: 72 self.store.remove_recurrences(self.user, self.uid) 73 74 # Queue any request, if appropriate. 75 76 if queue: 77 self.store.queue_request(self.user, self.uid, self.recurrenceid) 78 79 # Cancel complete events or particular occurrences in recurring 80 # events. 81 82 elif cancel: 83 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 84 85 # Remove any associated request. 86 87 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 88 89 # No return message will occur to update the free/busy 90 # information, so this is done here using outgoing message 91 # functionality. 92 93 self.remove_event_from_freebusy() 94 95 # Update the recipient's record of the organiser's schedule. 96 97 self.update_freebusy_from_organiser(organiser) 98 99 # As organiser, update attendance from valid attendees. 100 101 else: 102 if self.merge_attendance(attendees): 103 self.update_freebusy_from_attendees(attendees) 104 105 return True 106 107 def _record_freebusy(self, from_organiser=True): 108 109 """ 110 Record free/busy information for a message originating from an organiser 111 if 'from_organiser' is set to a true value. 112 """ 113 114 if from_organiser: 115 organiser_item = self.require_organiser(from_organiser) 116 if not organiser_item: 117 return 118 119 senders = [organiser_item] 120 else: 121 oa = self.require_organiser_and_attendees(from_organiser) 122 if not oa: 123 return 124 125 organiser_item, attendees = oa 126 senders = attendees.items() 127 128 if not senders: 129 return 130 131 freebusy = [] 132 133 for value in self.obj.get_values("FREEBUSY") or []: 134 if not isinstance(value, list): 135 value = [value] 136 for v in value: 137 try: 138 start, end = v.split("/", 1) 139 freebusy.append(FreeBusyPeriod(start, end)) 140 except ValueError: 141 pass 142 143 dtstart = self.obj.get_datetime("DTSTART") 144 dtend = self.obj.get_datetime("DTEND") 145 period = Period(dtstart, dtend, self.get_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