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