1 #!/usr/bin/env python 2 3 """ 4 Common calendar client utilities. 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 datetime import datetime 23 from imiptools.data import get_address, get_uri, get_window_end, \ 24 make_freebusy, to_part, \ 25 uri_dict, uri_items, uri_values 26 from imiptools.dates import format_datetime, get_default_timezone, \ 27 get_timestamp, to_timezone 28 from imiptools.period import update_freebusy 29 from imiptools.profile import Preferences 30 import imip_store 31 32 def update_attendees(obj, attendees, removed): 33 34 """ 35 Update the attendees in 'obj' with the given 'attendees' and 'removed' 36 attendee lists. A list is returned containing the attendees whose 37 attendance should be cancelled. 38 """ 39 40 to_cancel = [] 41 42 existing_attendees = uri_values(obj.get_values("ATTENDEE") or []) 43 added = set(attendees).difference(existing_attendees) 44 45 if added or removed: 46 attendees = uri_items(obj.get_items("ATTENDEE") or []) 47 sequence = obj.get_value("SEQUENCE") 48 49 if removed: 50 remaining = [] 51 52 for attendee, attendee_attr in attendees: 53 if attendee in removed: 54 55 # Without a sequence number, assume that the event has not 56 # been published and that attendees can be silently removed. 57 58 if sequence is not None: 59 to_cancel.append((attendee, attendee_attr)) 60 else: 61 remaining.append((attendee, attendee_attr)) 62 63 attendees = remaining 64 65 if added: 66 for attendee in added: 67 attendee = attendee.strip() 68 if attendee: 69 attendees.append((get_uri(attendee), {"PARTSTAT" : "NEEDS-ACTION", "RSVP" : "TRUE"})) 70 71 obj["ATTENDEE"] = attendees 72 73 return to_cancel 74 75 class Client: 76 77 "Common handler and manager methods." 78 79 default_window_size = 100 80 81 def __init__(self, user, messenger=None, store=None, publisher=None): 82 self.user = user 83 self.messenger = messenger 84 self.store = store or imip_store.FileStore() 85 86 try: 87 self.publisher = publisher or imip_store.FilePublisher() 88 except OSError: 89 self.publisher = None 90 91 self.preferences = None 92 93 def get_preferences(self): 94 if not self.preferences and self.user: 95 self.preferences = Preferences(self.user) 96 return self.preferences 97 98 def get_tzid(self): 99 prefs = self.get_preferences() 100 return prefs and prefs.get("TZID") or get_default_timezone() 101 102 def get_window_size(self): 103 prefs = self.get_preferences() 104 try: 105 return prefs and int(prefs.get("window_size")) or self.default_window_size 106 except (TypeError, ValueError): 107 return self.default_window_size 108 109 def get_window_end(self): 110 return get_window_end(self.get_tzid(), self.get_window_size()) 111 112 def is_sharing(self): 113 prefs = self.get_preferences() 114 return prefs and prefs.get("freebusy_sharing") == "share" or False 115 116 def is_bundling(self): 117 prefs = self.get_preferences() 118 return prefs and prefs.get("freebusy_bundling") == "always" or False 119 120 def is_notifying(self): 121 prefs = self.get_preferences() 122 return prefs and prefs.get("freebusy_messages") == "notify" or False 123 124 # Common operations on calendar data. 125 126 def is_participating(self, attr, as_organiser=False): 127 return as_organiser or not attr or attr.get("PARTSTAT") != "DECLINED" 128 129 def get_overriding_transparency(self, attr, as_organiser=False): 130 return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None 131 132 def update_participation(self, obj, partstat=None): 133 134 """ 135 Update the participation in 'obj' of the user with the given 'partstat'. 136 """ 137 138 attendee_attr = uri_dict(obj.get_value_map("ATTENDEE")).get(self.user) 139 if not attendee_attr: 140 return None 141 if partstat: 142 attendee_attr["PARTSTAT"] = partstat 143 if attendee_attr.has_key("RSVP"): 144 del attendee_attr["RSVP"] 145 self.update_sender(attendee_attr) 146 return attendee_attr 147 148 def update_sender(self, attr): 149 150 "Update the SENT-BY attribute of the 'attr' sender metadata." 151 152 if self.messenger and self.messenger.sender != get_address(self.user): 153 attr["SENT-BY"] = get_uri(self.messenger.sender) 154 155 # Free/busy operations. 156 157 def get_freebusy_part(self): 158 159 """ 160 Return a message part containing free/busy information for the user. 161 """ 162 163 if self.is_sharing() and self.is_bundling(): 164 165 # Invent a unique identifier. 166 167 utcnow = get_timestamp() 168 uid = "imip-agent-%s-%s" % (utcnow, get_address(self.user)) 169 170 freebusy = self.store.get_freebusy(self.user) 171 172 user_attr = {} 173 self.update_sender(user_attr) 174 return to_part("PUBLISH", [make_freebusy(freebusy, uid, self.user, user_attr)]) 175 176 return None 177 178 class ClientForObject(Client): 179 180 "A client maintaining a specific object." 181 182 def __init__(self, obj, user, messenger=None, store=None, publisher=None): 183 Client.__init__(self, user, messenger, store, publisher) 184 self.set_object(obj) 185 186 def set_object(self, obj): 187 self.obj = obj 188 self.uid = obj and self.obj.get_uid() 189 self.recurrenceid = obj and self.obj.get_recurrenceid() 190 self.sequence = obj and self.obj.get_value("SEQUENCE") 191 self.dtstamp = obj and self.obj.get_value("DTSTAMP") 192 193 def _update_freebusy(self, freebusy, periods, recurrenceid, transp=None): 194 195 """ 196 Update the 'freebusy' collection with the given 'periods', indicating an 197 explicit 'recurrenceid' to affect either a recurrence or the parent 198 event. 199 """ 200 201 update_freebusy(freebusy, periods, 202 transp or self.obj.get_value("TRANSP") or "OPAQUE", 203 self.uid, recurrenceid, 204 self.obj.get_value("SUMMARY"), 205 self.obj.get_value("ORGANIZER")) 206 207 def update_freebusy(self, freebusy, periods, transp=None): 208 209 """ 210 Update the 'freebusy' collection for this event with the given 211 'periods'. 212 """ 213 214 self._update_freebusy(freebusy, periods, self.recurrenceid, transp) 215 216 def update_freebusy_for_participant(self, freebusy, periods, attr, for_organiser=False): 217 218 """ 219 Update the 'freebusy' collection using the given 'periods', subject to 220 the 'attr' provided for the participant, indicating whether this is 221 being generated 'for_organiser' or not. 222 """ 223 224 # Organisers employ a special transparency if not attending. 225 226 if self.is_participating(attr, for_organiser): 227 self.update_freebusy(freebusy, periods, 228 transp=self.get_overriding_transparency(attr, for_organiser)) 229 else: 230 self.remove_from_freebusy(freebusy) 231 232 # Object update methods. 233 234 def update_dtstamp(self): 235 236 "Update the DTSTAMP in the current object." 237 238 dtstamp = self.obj.get_utc_datetime("DTSTAMP") 239 utcnow = to_timezone(datetime.utcnow(), "UTC") 240 self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})] 241 242 def set_sequence(self, increment=False): 243 244 "Update the SEQUENCE in the current object." 245 246 sequence = self.obj.get_value("SEQUENCE") or "0" 247 self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})] 248 249 # vim: tabstop=4 expandtab shiftwidth=4