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 # Remove additional recurrences if handling a complete event. 66 67 if not self.recurrenceid: 68 self.store.remove_recurrences(self.user, self.uid) 69 70 # Queue any request, if appropriate. 71 72 if queue: 73 self.store.queue_request(self.user, self.uid, self.recurrenceid) 74 75 # Cancel complete events or particular occurrences in recurring 76 # events. 77 78 elif cancel: 79 self.store.cancel_event(self.user, self.uid, self.recurrenceid) 80 81 # Remove any associated request. 82 83 self.store.dequeue_request(self.user, self.uid, self.recurrenceid) 84 85 # No return message will occur to update the free/busy 86 # information, so this is done here using outgoing message 87 # functionality. 88 89 self.remove_event_from_freebusy() 90 91 # Update the recipient's record of the organiser's schedule. 92 93 self.update_freebusy_from_organiser(organiser) 94 95 # Set the complete event or an additional occurrence. 96 97 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 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 = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] 132 dtstart = self.obj.get_datetime("DTSTART") 133 dtend = self.obj.get_datetime("DTEND") 134 period = Period(dtstart, dtend, self.get_tzid()) 135 136 for sender, sender_attr in senders: 137 stored_freebusy = self.store.get_freebusy_for_other(self.user, sender) 138 replace_overlapping(stored_freebusy, period, freebusy) 139 self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) 140 141 class Event(PersonHandler): 142 143 "An event handler." 144 145 def add(self): 146 147 # NOTE: Queue a suggested modification to any active event. 148 149 return self.wrap("An addition to an event has been received.", link=False) 150 151 def cancel(self): 152 153 "Queue a cancellation of any active event." 154 155 if self._record(from_organiser=True, queue=False, cancel=True): 156 return self.wrap("An event cancellation has been received.", link=False) 157 158 def counter(self): 159 160 # NOTE: Queue a suggested modification to any active event. 161 162 return self.wrap("A counter proposal to an event invitation has been received.", link=False) 163 164 def declinecounter(self): 165 166 # NOTE: Queue a rejected modification to any active event. 167 168 return self.wrap("Your counter proposal to an event invitation has been declined.", link=False) 169 170 def publish(self): 171 172 "Register details of any relevant event." 173 174 if self._record(from_organiser=True, queue=False): 175 return self.wrap("Details of an event have been received.") 176 177 def refresh(self): 178 179 "Generate details of any active event." 180 181 # NOTE: Return event details if configured to do so. 182 183 return self.wrap("A request for updated event details has been received.") 184 185 def reply(self): 186 187 "Record replies and notify the recipient." 188 189 if self._record(from_organiser=False, queue=False): 190 return self.wrap("A reply to an event invitation has been received.") 191 192 def request(self): 193 194 "Hold requests and notify the recipient." 195 196 if self._record(from_organiser=True, queue=True): 197 return self.wrap("An event invitation has been received.") 198 199 class Freebusy(PersonHandler, CommonFreebusy): 200 201 "A free/busy handler." 202 203 def publish(self): 204 205 "Register free/busy information." 206 207 self._record_freebusy(from_organiser=True) 208 209 # Produce a message if configured to do so. 210 211 if self.is_notifying(): 212 return self.wrap("A free/busy update has been received.", link=False) 213 214 def reply(self): 215 216 "Record replies and notify the recipient." 217 218 self._record_freebusy(from_organiser=False) 219 220 # Produce a message if configured to do so. 221 222 if self.is_notifying(): 223 return self.wrap("A reply to a free/busy request has been received.", link=False) 224 225 def request(self): 226 227 """ 228 Respond to a request by preparing a reply containing free/busy 229 information for the recipient. 230 """ 231 232 # Produce a reply if configured to do so. 233 234 if self.is_sharing(): 235 return CommonFreebusy.request(self) 236 237 # Handler registry. 238 239 handlers = [ 240 ("VFREEBUSY", Freebusy), 241 ("VEVENT", Event), 242 ] 243 244 # vim: tabstop=4 expandtab shiftwidth=4