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