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@292 | 56 | if self.messenger: |
paul@292 | 57 | attendee_attr["SENT-BY"] = get_uri(self.messenger.sender) |
paul@292 | 58 | |
paul@562 | 59 | dtstart = self.obj.get_datetime("DTSTART") |
paul@562 | 60 | dtend = self.obj.get_datetime("DTEND") |
paul@620 | 61 | period = dtstart and dtend and Period(dtstart, dtend, self.get_tzid()) or None |
paul@327 | 62 | |
paul@562 | 63 | rwrite(make_freebusy(freebusy, self.uid, organiser, organiser_attr, attendee, attendee_attr, period)) |
paul@183 | 64 | |
paul@183 | 65 | # Return the reply. |
paul@183 | 66 | |
paul@228 | 67 | self.add_result("REPLY", [get_address(organiser)], to_part("REPLY", responses)) |
paul@183 | 68 | |
paul@683 | 69 | class CommonEvent: |
paul@580 | 70 | |
paul@606 | 71 | "Common outgoing message handling functionality mix-in." |
paul@580 | 72 | |
paul@720 | 73 | def is_usable(self): |
paul@720 | 74 | |
paul@720 | 75 | "Return whether the current object is usable." |
paul@720 | 76 | |
paul@720 | 77 | return self.obj and self.obj.get_value("DTSTART") |
paul@720 | 78 | |
paul@676 | 79 | def update_event_in_freebusy(self, for_organiser=True): |
paul@580 | 80 | |
paul@676 | 81 | """ |
paul@676 | 82 | Update free/busy information when handling an object, doing so for the |
paul@676 | 83 | organiser of an event if 'for_organiser' is set to a true value. |
paul@676 | 84 | """ |
paul@580 | 85 | |
paul@702 | 86 | freebusy = self.store.get_freebusy_for_update(self.user) |
paul@702 | 87 | try: |
paul@702 | 88 | # Obtain the attendance attributes for this user, if available. |
paul@580 | 89 | |
paul@702 | 90 | self.update_freebusy_for_participant(freebusy, self.user, for_organiser) |
paul@580 | 91 | |
paul@702 | 92 | # Remove original recurrence details replaced by additional |
paul@702 | 93 | # recurrences, as well as obsolete additional recurrences. |
paul@580 | 94 | |
paul@702 | 95 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@702 | 96 | self.store.set_freebusy_in_update(self.user, freebusy) |
paul@580 | 97 | |
paul@702 | 98 | if self.publisher and self.is_sharing(): |
paul@702 | 99 | self.publisher.set_freebusy(self.user, freebusy) |
paul@580 | 100 | |
paul@702 | 101 | # Update free/busy provider information if the event may recur |
paul@702 | 102 | # indefinitely. |
paul@580 | 103 | |
paul@702 | 104 | if self.possibly_recurring_indefinitely(): |
paul@702 | 105 | self.store.append_freebusy_provider(self.user, self.obj) |
paul@672 | 106 | |
paul@702 | 107 | finally: |
paul@702 | 108 | self.store.release_freebusy(self.user) |
paul@672 | 109 | |
paul@580 | 110 | return True |
paul@580 | 111 | |
paul@580 | 112 | def remove_event_from_freebusy(self): |
paul@580 | 113 | |
paul@580 | 114 | "Remove free/busy information when handling an object." |
paul@580 | 115 | |
paul@702 | 116 | freebusy = self.store.get_freebusy_for_update(self.user) |
paul@702 | 117 | try: |
paul@702 | 118 | self.remove_from_freebusy(freebusy) |
paul@702 | 119 | self.remove_freebusy_for_recurrences(freebusy) |
paul@702 | 120 | self.store.set_freebusy_in_update(self.user, freebusy) |
paul@702 | 121 | |
paul@702 | 122 | if self.publisher and self.is_sharing(): |
paul@702 | 123 | self.publisher.set_freebusy(self.user, freebusy) |
paul@580 | 124 | |
paul@702 | 125 | # Update free/busy provider information if the event may recur |
paul@702 | 126 | # indefinitely. |
paul@580 | 127 | |
paul@702 | 128 | if self.possibly_recurring_indefinitely(): |
paul@702 | 129 | self.store.remove_freebusy_provider(self.user, self.obj) |
paul@672 | 130 | |
paul@702 | 131 | finally: |
paul@702 | 132 | self.store.release_freebusy(self.user) |
paul@672 | 133 | |
paul@710 | 134 | def update_event_in_freebusy_offers(self): |
paul@710 | 135 | |
paul@710 | 136 | "Update free/busy offers when handling an object." |
paul@710 | 137 | |
paul@710 | 138 | freebusy = self.store.get_freebusy_offers_for_update(self.user) |
paul@710 | 139 | try: |
paul@710 | 140 | # Obtain the attendance attributes for this user, if available. |
paul@710 | 141 | |
paul@710 | 142 | self.update_freebusy_for_participant(freebusy, self.user) |
paul@710 | 143 | |
paul@710 | 144 | # Remove original recurrence details replaced by additional |
paul@710 | 145 | # recurrences, as well as obsolete additional recurrences. |
paul@710 | 146 | |
paul@710 | 147 | self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid)) |
paul@710 | 148 | self.store.set_freebusy_offers_in_update(self.user, freebusy) |
paul@710 | 149 | |
paul@710 | 150 | finally: |
paul@710 | 151 | self.store.release_freebusy(self.user) |
paul@710 | 152 | |
paul@710 | 153 | return True |
paul@710 | 154 | |
paul@710 | 155 | def remove_event_from_freebusy_offers(self): |
paul@710 | 156 | |
paul@710 | 157 | "Remove free/busy offers when handling an object." |
paul@710 | 158 | |
paul@710 | 159 | freebusy = self.store.get_freebusy_offers_for_update(self.user) |
paul@710 | 160 | try: |
paul@710 | 161 | self.remove_from_freebusy(freebusy) |
paul@710 | 162 | self.remove_freebusy_for_recurrences(freebusy) |
paul@710 | 163 | self.store.set_freebusy_offers_in_update(self.user, freebusy) |
paul@710 | 164 | |
paul@710 | 165 | finally: |
paul@710 | 166 | self.store.release_freebusy(self.user) |
paul@710 | 167 | |
paul@710 | 168 | return True |
paul@710 | 169 | |
paul@108 | 170 | # vim: tabstop=4 expandtab shiftwidth=4 |