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@580 | 22 | from imiptools.data import get_address, get_uri, make_freebusy, to_part, \ |
paul@580 | 23 | uri_dict |
paul@327 | 24 | from imiptools.dates import format_datetime |
paul@562 | 25 | from imiptools.period import Period |
paul@108 | 26 | |
paul@222 | 27 | class CommonFreebusy: |
paul@108 | 28 | |
paul@222 | 29 | "Common free/busy mix-in." |
paul@181 | 30 | |
paul@222 | 31 | def request(self): |
paul@181 | 32 | |
paul@181 | 33 | """ |
paul@222 | 34 | Respond to a request by preparing a reply containing free/busy |
paul@222 | 35 | information for each indicated attendee. |
paul@181 | 36 | """ |
paul@181 | 37 | |
paul@108 | 38 | oa = self.require_organiser_and_attendees() |
paul@108 | 39 | if not oa: |
paul@228 | 40 | return |
paul@108 | 41 | |
paul@181 | 42 | (organiser, organiser_attr), attendees = oa |
paul@108 | 43 | |
paul@181 | 44 | # Get the details for each attendee. |
paul@108 | 45 | |
paul@222 | 46 | responses = [] |
paul@222 | 47 | rwrite = responses.append |
paul@222 | 48 | |
paul@222 | 49 | # For replies, the organiser and attendee are preserved. |
paul@108 | 50 | |
paul@181 | 51 | for attendee, attendee_attr in attendees.items(): |
paul@222 | 52 | freebusy = self.store.get_freebusy(attendee) |
paul@292 | 53 | |
paul@292 | 54 | # Indicate the actual sender of the reply. |
paul@292 | 55 | |
paul@830 | 56 | self.update_sender(attendee_attr) |
paul@292 | 57 | |
paul@562 | 58 | dtstart = self.obj.get_datetime("DTSTART") |
paul@562 | 59 | dtend = self.obj.get_datetime("DTEND") |
paul@620 | 60 | period = dtstart and dtend and Period(dtstart, dtend, self.get_tzid()) or None |
paul@327 | 61 | |
paul@562 | 62 | rwrite(make_freebusy(freebusy, self.uid, organiser, organiser_attr, attendee, attendee_attr, period)) |
paul@183 | 63 | |
paul@183 | 64 | # Return the reply. |
paul@183 | 65 | |
paul@228 | 66 | self.add_result("REPLY", [get_address(organiser)], to_part("REPLY", responses)) |
paul@183 | 67 | |
paul@683 | 68 | class CommonEvent: |
paul@580 | 69 | |
paul@606 | 70 | "Common outgoing message handling functionality mix-in." |
paul@580 | 71 | |
paul@727 | 72 | def is_usable(self, method=None): |
paul@727 | 73 | |
paul@727 | 74 | "Return whether the current object is usable with the given 'method'." |
paul@720 | 75 | |
paul@727 | 76 | return self.obj and ( |
paul@727 | 77 | method in ("CANCEL", "REFRESH") or |
paul@727 | 78 | self.obj.get_datetime("DTSTART") and |
paul@727 | 79 | (self.obj.get_datetime("DTEND") or self.obj.get_duration("DURATION"))) |
paul@720 | 80 | |
paul@737 | 81 | def will_refresh(self): |
paul@737 | 82 | |
paul@737 | 83 | """ |
paul@737 | 84 | Indicate whether a REFRESH message should be used to respond to an ADD |
paul@737 | 85 | message. |
paul@737 | 86 | """ |
paul@737 | 87 | |
paul@737 | 88 | return not self.get_stored_object_version() or self.get_add_method_response() == "refresh" |
paul@737 | 89 | |
paul@737 | 90 | def make_refresh(self): |
paul@737 | 91 | |
paul@737 | 92 | "Make a REFRESH message." |
paul@737 | 93 | |
paul@737 | 94 | organiser = get_uri(self.obj.get_value("ORGANIZER")) |
paul@737 | 95 | attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@737 | 96 | |
paul@830 | 97 | # Indicate the actual sender of the message. |
paul@737 | 98 | |
paul@737 | 99 | attendee_attr = attendees[self.user] |
paul@737 | 100 | self.update_sender(attendee_attr) |
paul@737 | 101 | |
paul@737 | 102 | # Make a new object with a minimal property selection. |
paul@737 | 103 | |
paul@737 | 104 | obj = self.obj.copy() |
paul@737 | 105 | obj.preserve(("ORGANIZER", "DTSTAMP", "UID", "RECURRENCE-ID")) |
paul@737 | 106 | obj["ATTENDEE"] = [(self.user, attendee_attr)] |
paul@737 | 107 | |
paul@737 | 108 | # Send a REFRESH message in response. |
paul@737 | 109 | |
paul@737 | 110 | self.add_result("REFRESH", [get_address(organiser)], obj.to_part("REFRESH")) |
paul@737 | 111 | |
paul@108 | 112 | # vim: tabstop=4 expandtab shiftwidth=4 |