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