imip-agent

Annotated imiptools/handlers/common.py

146:367b6c1b82b0
2015-01-12 Paul Boddie Added copyright and licensing notices. Exposed a path configuration setting in the manager program.
paul@108 1
#!/usr/bin/env python
paul@108 2
paul@108 3
"""
paul@108 4
Common handler functionality for different entities.
paul@146 5
paul@146 6
Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
paul@146 7
paul@146 8
This program is free software; you can redistribute it and/or modify it under
paul@146 9
the terms of the GNU General Public License as published by the Free Software
paul@146 10
Foundation; either version 3 of the License, or (at your option) any later
paul@146 11
version.
paul@146 12
paul@146 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@146 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@146 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@146 16
details.
paul@146 17
paul@146 18
You should have received a copy of the GNU General Public License along with
paul@146 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@108 20
"""
paul@108 21
paul@108 22
from imiptools.content import to_part
paul@108 23
paul@108 24
class CommonFreebusy:
paul@108 25
paul@108 26
    "Common free/busy mix-in."
paul@108 27
paul@108 28
    def request(self):
paul@108 29
paul@108 30
        """
paul@108 31
        Respond to a request by preparing a reply containing free/busy
paul@108 32
        information for each indicated attendee.
paul@108 33
        """
paul@108 34
paul@108 35
        oa = self.require_organiser_and_attendees()
paul@108 36
        if not oa:
paul@108 37
            return None
paul@108 38
paul@108 39
        (organiser, organiser_attr), attendees = organiser_item, attendees = oa
paul@108 40
paul@108 41
        # Validate the organiser, ignoring spoofed requests.
paul@108 42
paul@108 43
        if not self.validate_identities([organiser_item]):
paul@108 44
            return None
paul@108 45
paul@108 46
        # Construct an appropriate fragment.
paul@108 47
paul@108 48
        calendar = []
paul@108 49
        cwrite = calendar.append
paul@108 50
paul@108 51
        # Get the details for each attendee.
paul@108 52
paul@108 53
        for attendee, attendee_attr in attendees.items():
paul@108 54
            freebusy = self.store.get_freebusy(attendee)
paul@108 55
paul@108 56
            record = []
paul@108 57
            rwrite = record.append
paul@108 58
paul@108 59
            rwrite(("ORGANIZER", organiser_attr, organiser))
paul@108 60
            rwrite(("ATTENDEE", attendee_attr, attendee))
paul@108 61
            rwrite(("UID", {}, self.uid))
paul@108 62
paul@108 63
            if freebusy:
paul@112 64
                for start, end, uid, transp in freebusy:
paul@112 65
                    if transp == "OPAQUE":
paul@112 66
                        rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join([start, end])))
paul@108 67
paul@108 68
            cwrite(("VFREEBUSY", {}, record))
paul@108 69
paul@108 70
        # Return the reply.
paul@108 71
paul@108 72
        return "REPLY", to_part("REPLY", calendar)
paul@108 73
paul@108 74
# vim: tabstop=4 expandtab shiftwidth=4