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