1 #!/usr/bin/env python 2 3 """ 4 Common handler functionality for different entities. 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_address, get_uri, make_freebusy, to_part, \ 23 uri_dict 24 from imiptools.dates import format_datetime 25 from imiptools.period import Period 26 27 class CommonFreebusy: 28 29 "Common free/busy mix-in." 30 31 def request(self): 32 33 """ 34 Respond to a request by preparing a reply containing free/busy 35 information for each indicated attendee. 36 """ 37 38 oa = self.require_organiser_and_attendees() 39 if not oa: 40 return 41 42 (organiser, organiser_attr), attendees = oa 43 44 # Get the details for each attendee. 45 46 responses = [] 47 rwrite = responses.append 48 49 # For replies, the organiser and attendee are preserved. 50 51 for attendee, attendee_attr in attendees.items(): 52 freebusy = self.store.get_freebusy(attendee) 53 54 # Indicate the actual sender of the reply. 55 56 if self.messenger: 57 attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) 58 59 tzid = self.obj.get_tzid() or self.get_tzid() 60 dtstart = self.obj.get_datetime("DTSTART") 61 dtend = self.obj.get_datetime("DTEND") 62 period = dtstart and dtend and Period(dtstart, dtend, tzid) or None 63 64 rwrite(make_freebusy(freebusy, self.uid, organiser, organiser_attr, attendee, attendee_attr, period)) 65 66 # Return the reply. 67 68 self.add_result("REPLY", [get_address(organiser)], to_part("REPLY", responses)) 69 70 class Outgoing: 71 72 "Common outgoing message handling functionality." 73 74 def update_event_in_freebusy(self, from_organiser=True): 75 76 "Update free/busy information when handling an object." 77 78 freebusy = self.store.get_freebusy(self.user) 79 80 # Use the stored event in case the reply is incomplete, as is seen 81 # when Claws sends a REPLY for an object originally employing 82 # recurrence information. 83 84 obj = self.get_definitive_object(from_organiser) 85 if not obj: 86 return False # although this should not happen 87 88 # If newer than any old version, discard old details from the 89 # free/busy record and check for suitability. 90 91 # Interpretation of periods can depend on the time zone. 92 93 periods = obj.get_periods(self.get_tzid(), self.get_window_end()) 94 95 # Obtain the attendance attributes for this user, if available. 96 97 attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) 98 self.update_freebusy_for_participant(freebusy, periods, attendees.get(self.user), from_organiser) 99 100 # Remove original recurrence details replaced by additional 101 # recurrences, as well as obsolete additional recurrences. 102 103 self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) 104 self.store.set_freebusy(self.user, freebusy) 105 106 if self.publisher and self.is_sharing(): 107 self.publisher.set_freebusy(self.user, freebusy) 108 109 return True 110 111 def remove_event_from_freebusy(self): 112 113 "Remove free/busy information when handling an object." 114 115 freebusy = self.store.get_freebusy(self.user) 116 self.remove_from_freebusy(freebusy) 117 self.remove_freebusy_for_recurrences(freebusy) 118 self.store.set_freebusy(self.user, freebusy) 119 120 if self.publisher and self.is_sharing(): 121 self.publisher.set_freebusy(self.user, freebusy) 122 123 # vim: tabstop=4 expandtab shiftwidth=4