1 #!/usr/bin/env python 2 3 """ 4 Common handler functionality for different entities. 5 6 Copyright (C) 2014, 2015, 2016, 2017 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_address, get_uri, make_freebusy, to_part, \ 23 uri_dict 24 from imiptools.dates import format_datetime 25 from imiptools.freebusy import FreeBusyPeriod 26 from imiptools.period import Period 27 28 class CommonFreebusy: 29 30 "Common free/busy mix-in." 31 32 def _record_freebusy(self, from_organiser=True): 33 34 """ 35 Record free/busy information for a message originating from an organiser 36 if 'from_organiser' is set to a true value. 37 """ 38 39 if from_organiser: 40 organiser_item = self.require_organiser(from_organiser) 41 if not organiser_item: 42 return 43 44 senders = [organiser_item] 45 else: 46 oa = self.require_organiser_and_attendees(from_organiser) 47 if not oa: 48 return 49 50 organiser_item, attendees = oa 51 senders = attendees.items() 52 53 if not senders: 54 return 55 56 freebusy = [FreeBusyPeriod(p.get_start_point(), p.get_end_point()) for p in self.obj.get_period_values("FREEBUSY")] 57 dtstart = self.obj.get_datetime("DTSTART") 58 dtend = self.obj.get_datetime("DTEND") 59 period = Period(dtstart, dtend, self.get_tzid()) 60 61 for sender, sender_attr in senders: 62 stored_freebusy = self.store.get_freebusy_for_other_for_update(self.user, sender) 63 stored_freebusy.replace_overlapping(period, freebusy) 64 self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) 65 66 def request(self): 67 68 """ 69 Respond to a request by preparing a reply containing free/busy 70 information for each indicated attendee. 71 """ 72 73 oa = self.require_organiser_and_attendees() 74 if not oa: 75 return 76 77 (organiser, organiser_attr), attendees = oa 78 79 # Get the details for each attendee. 80 81 responses = [] 82 rwrite = responses.append 83 84 # For replies, the organiser and attendee are preserved. 85 86 for attendee, attendee_attr in attendees.items(): 87 freebusy = self.store.get_freebusy(attendee) 88 89 # Indicate the actual sender of the reply. 90 91 self.update_sender_attr(attendee_attr) 92 93 dtstart = self.obj.get_datetime("DTSTART") 94 dtend = self.obj.get_datetime("DTEND") 95 period = dtstart and dtend and Period(dtstart, dtend, self.get_tzid()) or None 96 97 rwrite(make_freebusy(freebusy, self.uid, organiser, organiser_attr, attendee, attendee_attr, period)) 98 99 # Return the reply. 100 101 self.add_result("REPLY", [get_address(organiser)], to_part("REPLY", responses)) 102 103 class CommonEvent: 104 105 "Common outgoing message handling functionality mix-in." 106 107 def is_usable(self, method=None): 108 109 "Return whether the current object is usable with the given 'method'." 110 111 return self.obj and ( 112 method in ("CANCEL", "REFRESH") or 113 self.obj.get_datetime("DTSTART") and 114 (self.obj.get_datetime("DTEND") or self.obj.get_duration("DURATION"))) 115 116 def will_refresh(self): 117 118 """ 119 Indicate whether a REFRESH message should be used to respond to an ADD 120 message. 121 """ 122 123 return not self.get_stored_object_version() or self.get_add_method_response() == "refresh" 124 125 def make_refresh(self): 126 127 "Make a REFRESH message." 128 129 organiser = get_uri(self.obj.get_value("ORGANIZER")) 130 attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) 131 132 # Indicate the actual sender of the message. 133 134 attendee_attr = attendees[self.user] 135 self.update_sender_attr(attendee_attr) 136 137 # Make a new object with a minimal property selection. 138 139 obj = self.obj.copy() 140 obj.preserve(("ORGANIZER", "DTSTAMP", "UID", "RECURRENCE-ID")) 141 obj["ATTENDEE"] = [(self.user, attendee_attr)] 142 143 # Send a REFRESH message in response. 144 145 self.add_result("REFRESH", [get_address(organiser)], obj.to_part("REFRESH")) 146 147 def is_newly_separated_occurrence(self): 148 149 "Return whether the current object is a newly-separated occurrence." 150 151 # Obtain any stored object. 152 153 obj = self.get_stored_object_version() 154 155 # Handle any newly-separated, valid occurrence. 156 157 return not obj and self.is_recurrence() 158 159 def make_separate_occurrence(self, for_organiser=False): 160 161 """ 162 Set the current object as a separate occurrence and redefine free/busy 163 records in terms of this new occurrence for other participants. 164 """ 165 166 parent = self.get_parent_object() 167 if not parent: 168 return False 169 170 # Transfer attendance information from the parent. 171 172 parent_attendees = uri_dict(parent.get_value_map("ATTENDEE")) 173 attendee_map = uri_dict(self.obj.get_value_map("ATTENDEE")) 174 175 for attendee, attendee_attr in parent_attendees.items(): 176 if not attendee_map.has_key(attendee): 177 attendee_map[attendee] = attendee_attr 178 179 self.obj["ATTENDEE"] = attendee_map.items() 180 self.obj.remove_all(["RDATE", "RRULE"]) 181 182 # Create or revive the occurrence. 183 184 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid) 185 self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) 186 187 # Update free/busy details for the current object for all attendees. 188 189 self.update_freebusy_from_attendees(attendee_map.keys()) 190 191 return True 192 193 # vim: tabstop=4 expandtab shiftwidth=4