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.content import to_part 23 24 class SupportFreebusy: 25 26 "Support for free/busy publishing and sharing." 27 28 def make_freebusy_to_publish(self, from_organiser=True): 29 30 """ 31 Make a freebusy object for publication for a user, providing either an 32 organiser's details if 'from_organiser' is set to a true value, or an 33 attendee's details otherwise. 34 """ 35 36 calendar = self.make_freebusy(from_organiser, publish=True) 37 38 # Return a published object. 39 40 return [(True, to_part("PUBLISH", calendar))] 41 42 def make_freebusy(self, from_organiser=True, publish=False): 43 44 """ 45 Make a freebusy object, providing either an organiser's details if 46 'from_organiser' is set to a true value, or an attendee's details 47 otherwise. 48 """ 49 50 oa = self.require_organiser_and_attendees() 51 if not oa: 52 return None 53 54 (organiser, organiser_attr), attendees = oa 55 56 # Get the details for each attendee. 57 58 calendar = [] 59 cwrite = calendar.append 60 61 for attendee, attendee_attr in attendees.items(): 62 63 # Construct an appropriate fragment. 64 65 freebusy = self.store.get_freebusy(from_organiser and organiser or attendee) 66 67 record = [] 68 rwrite = record.append 69 70 # For replies, the organiser is preserved. 71 72 if not publish or from_organiser: 73 rwrite(("ORGANIZER", organiser_attr, organiser)) 74 75 # For published objects, the organiser is actually the user whose 76 # information is being provided. 77 78 else: 79 rwrite(("ORGANIZER", attendee_attr, attendee)) 80 81 # For replies, the attendee is preserved. 82 # (Published objects do not employ the attendee property.) 83 84 if not publish: 85 rwrite(("ATTENDEE", attendee_attr, attendee)) 86 87 rwrite(("UID", {}, self.uid)) 88 89 if freebusy: 90 for start, end, uid, transp in freebusy: 91 if transp == "OPAQUE": 92 rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end]))) 93 94 cwrite(("VFREEBUSY", {}, record)) 95 96 # Return the object. 97 98 return calendar 99 100 class CommonFreebusy(SupportFreebusy): 101 102 "Common free/busy mix-in." 103 104 def request(self): 105 106 """ 107 Respond to a request by preparing a reply containing free/busy 108 information for each indicated attendee. 109 """ 110 111 calendar = self.make_freebusy(from_organiser=False) 112 113 # Return the reply. 114 115 return [(True, to_part("REPLY", calendar))] 116 117 # vim: tabstop=4 expandtab shiftwidth=4