1 #!/usr/bin/env python 2 3 """ 4 Handlers for a person for whom scheduling is performed, inspecting outgoing 5 messages to obtain scheduling done externally. 6 7 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from imiptools.data import get_window_end, uri_dict, uri_item, uri_values 24 from imiptools.handlers import Handler 25 from imiptools.period import remove_affected_period 26 27 class PersonHandler(Handler): 28 29 "Handling mechanisms specific to people." 30 31 def _get_identity(self, from_organiser=True): 32 33 """ 34 Get the identity of interest in a usable form for any unprocessed 35 object. 36 """ 37 38 identity, attr = item = uri_item(self.obj.get_item(from_organiser and "ORGANIZER" or "ATTENDEE")) 39 40 # Check for event using UID. 41 42 if not self.have_new_object(identity): 43 return None 44 45 return item 46 47 def _record(self, from_organiser=True, update_freebusy=False): 48 49 """ 50 Record details from the current object given a message originating 51 from an organiser if 'from_organiser' is set to a true value, updating 52 free/busy information if 'update_freebusy' is set to a true value. 53 """ 54 55 item = self._get_identity(from_organiser) 56 if not item: 57 return False 58 59 identity, attr = item 60 61 # Update the object. 62 63 if from_organiser: 64 65 # Set the complete event or an additional occurrence. 66 67 self.store.set_event(identity, 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(identity, self.uid) 73 74 else: 75 # Obtain valid attendees, merging their attendance with the stored 76 # object. 77 78 attendees = self.require_attendees(from_organiser) 79 self.merge_attendance(attendees, identity) 80 81 # Remove any associated request. 82 83 self.store.dequeue_request(identity, self.uid, self.recurrenceid) 84 85 # Update free/busy information. 86 87 if update_freebusy: 88 89 freebusy = self.store.get_freebusy(identity) 90 91 # Interpretation of periods can depend on the time zone. 92 93 tzid = self.get_tzid(identity) 94 95 # Use the stored event in case the reply is incomplete, as is seen 96 # when Claws sends a REPLY for an object originally employing 97 # recurrence information. 98 99 obj = self.get_object(identity) 100 if not obj: 101 return False # although this should not happen 102 103 # If newer than any old version, discard old details from the 104 # free/busy record and check for suitability. 105 106 periods = obj.get_periods_for_freebusy(tzid, get_window_end(tzid)) 107 108 self.update_freebusy_for_participant(freebusy, periods, attr, 109 from_organiser and self.is_not_attendee(identity, obj)) 110 111 # Remove either original recurrence or additional recurrence 112 # details depending on whether an additional recurrence or a 113 # complete event are being handled, respectively. 114 115 self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(identity, self.uid)) 116 self.store.set_freebusy(identity, freebusy) 117 118 if self.publisher: 119 self.publisher.set_freebusy(identity, freebusy) 120 121 return True 122 123 def _remove(self, from_organiser=True, update_freebusy=False): 124 125 "Remove free/busy information for any unprocessed object." 126 127 item = self._get_identity(from_organiser) 128 if not item: 129 return False 130 131 identity, attr = item 132 133 # Only cancel the event completely if all attendees are given. 134 # NOTE: Need to also check for recurrence identifiers and selective 135 # NOTE: cancellations. 136 137 obj = self.get_object(identity) 138 if not obj: 139 return False 140 141 attendees = uri_dict(obj.get_value_map("ATTENDEE")) 142 all_attendees = set(attendees.keys()) 143 given_attendees = set(uri_values(self.obj.get_values("ATTENDEE"))) 144 145 if given_attendees == all_attendees: 146 self.store.cancel_event(identity, self.uid, self.recurrenceid) 147 148 # Otherwise, remove the given attendees and update the event. 149 150 else: 151 for attendee in given_attendees: 152 if attendees.has_key(attendee): 153 del attendees[attendee] 154 obj["ATTENDEE"] = attendees.items() 155 156 # Update the stored object with sequence information. 157 158 obj["SEQUENCE"] = self.obj.get_items("SEQUENCE") 159 obj["DTSTAMP"] = self.obj.get_items("DTSTAMP") 160 161 # Set the complete event if not an additional occurrence. 162 163 self.store.set_event(identity, self.uid, self.recurrenceid, obj.to_node()) 164 165 # Remove any associated request. 166 167 self.store.dequeue_request(identity, self.uid, self.recurrenceid) 168 169 # Update free/busy information. 170 171 if update_freebusy: 172 freebusy = self.store.get_freebusy(identity) 173 self.remove_from_freebusy(freebusy) 174 self.store.set_freebusy(identity, freebusy) 175 176 if self.publisher: 177 self.publisher.set_freebusy(identity, freebusy) 178 179 return True 180 181 class Event(PersonHandler): 182 183 "An event handler." 184 185 def add(self): 186 pass 187 188 def cancel(self): 189 self._remove(True, True) 190 191 def counter(self): 192 pass 193 194 def declinecounter(self): 195 pass 196 197 def publish(self): 198 self._record(True, True) 199 200 def refresh(self): 201 pass 202 203 def reply(self): 204 self._record(False, True) 205 206 def request(self): 207 self._record(True, True) 208 209 class Freebusy(PersonHandler): 210 211 "A free/busy handler." 212 213 def publish(self): 214 pass 215 216 def reply(self): 217 pass 218 219 def request(self): 220 pass 221 222 # Handler registry. 223 224 handlers = [ 225 ("VFREEBUSY", Freebusy), 226 ("VEVENT", Event), 227 ] 228 229 # vim: tabstop=4 expandtab shiftwidth=4