paul@55 | 1 | #!/usr/bin/env python |
paul@55 | 2 | |
paul@55 | 3 | """ |
paul@55 | 4 | Handlers for a person for whom scheduling is performed. |
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@55 | 20 | """ |
paul@55 | 21 | |
paul@610 | 22 | from imiptools.data import uri_dict |
paul@418 | 23 | from imiptools.handlers import Handler |
paul@584 | 24 | from imiptools.handlers.common import CommonFreebusy, Outgoing |
paul@529 | 25 | from imiptools.period import FreeBusyPeriod, Period, replace_overlapping |
paul@55 | 26 | |
paul@584 | 27 | class PersonHandler(Handler, Outgoing): |
paul@55 | 28 | |
paul@67 | 29 | "Handling mechanisms specific to people." |
paul@55 | 30 | |
paul@271 | 31 | def _record(self, from_organiser=True, queue=False, cancel=False): |
paul@55 | 32 | |
paul@420 | 33 | """ |
paul@420 | 34 | Record details from the current object given a message originating |
paul@420 | 35 | from an organiser if 'from_organiser' is set to a true value, queuing a |
paul@420 | 36 | request if 'queue' is set to a true value, or cancelling an event if |
paul@420 | 37 | 'cancel' is set to a true value. |
paul@420 | 38 | """ |
paul@420 | 39 | |
paul@420 | 40 | # Obtain valid organiser and attendee details. |
paul@420 | 41 | |
paul@100 | 42 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@55 | 43 | if not oa: |
paul@61 | 44 | return False |
paul@55 | 45 | |
paul@606 | 46 | (organiser, organiser_attr), attendees = oa |
paul@94 | 47 | |
paul@110 | 48 | # Handle notifications and invitations. |
paul@55 | 49 | |
paul@100 | 50 | if from_organiser: |
paul@110 | 51 | |
paul@468 | 52 | # Process for the current user, an attendee. |
paul@100 | 53 | |
paul@468 | 54 | if not self.have_new_object() or not self.is_attendee(self.user): |
paul@468 | 55 | return False |
paul@100 | 56 | |
paul@589 | 57 | # Indicate the organiser's implicit attendance if mentioned in the |
paul@589 | 58 | # cancellation message. |
paul@589 | 59 | |
paul@589 | 60 | if cancel: |
paul@610 | 61 | obj_attendees = uri_dict(self.obj.get_value_map("ATTENDEE")) |
paul@589 | 62 | if obj_attendees.has_key(organiser): |
paul@589 | 63 | obj_attendees[organiser]["PARTSTAT"] = "DECLINED" |
paul@589 | 64 | |
paul@468 | 65 | # Set the complete event or an additional occurrence. |
paul@334 | 66 | |
paul@468 | 67 | self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node()) |
paul@361 | 68 | |
paul@468 | 69 | # Remove additional recurrences if handling a complete event. |
paul@381 | 70 | |
paul@468 | 71 | if not self.recurrenceid: |
paul@468 | 72 | self.store.remove_recurrences(self.user, self.uid) |
paul@381 | 73 | |
paul@493 | 74 | # Queue any request, if appropriate. |
paul@55 | 75 | |
paul@468 | 76 | if queue: |
paul@468 | 77 | self.store.queue_request(self.user, self.uid, self.recurrenceid) |
paul@493 | 78 | |
paul@493 | 79 | # Cancel complete events or particular occurrences in recurring |
paul@493 | 80 | # events. |
paul@493 | 81 | |
paul@468 | 82 | elif cancel: |
paul@468 | 83 | self.store.cancel_event(self.user, self.uid, self.recurrenceid) |
paul@100 | 84 | |
paul@504 | 85 | # Remove any associated request. |
paul@504 | 86 | |
paul@504 | 87 | self.store.dequeue_request(self.user, self.uid, self.recurrenceid) |
paul@504 | 88 | |
paul@468 | 89 | # No return message will occur to update the free/busy |
paul@584 | 90 | # information, so this is done here using outgoing message |
paul@584 | 91 | # functionality. |
paul@361 | 92 | |
paul@589 | 93 | self.remove_event_from_freebusy() |
paul@589 | 94 | |
paul@589 | 95 | # Update the recipient's record of the organiser's schedule. |
paul@261 | 96 | |
paul@606 | 97 | self.update_freebusy_from_organiser(organiser) |
paul@268 | 98 | |
paul@420 | 99 | # As organiser, update attendance from valid attendees. |
paul@55 | 100 | |
paul@100 | 101 | else: |
paul@468 | 102 | if self.merge_attendance(attendees): |
paul@468 | 103 | self.update_freebusy_from_attendees(attendees) |
paul@61 | 104 | |
paul@61 | 105 | return True |
paul@61 | 106 | |
paul@110 | 107 | def _record_freebusy(self, from_organiser=True): |
paul@110 | 108 | |
paul@420 | 109 | """ |
paul@420 | 110 | Record free/busy information for a message originating from an organiser |
paul@420 | 111 | if 'from_organiser' is set to a true value. |
paul@420 | 112 | """ |
paul@110 | 113 | |
paul@292 | 114 | if from_organiser: |
paul@292 | 115 | organiser_item = self.require_organiser(from_organiser) |
paul@292 | 116 | if not organiser_item: |
paul@292 | 117 | return |
paul@271 | 118 | |
paul@292 | 119 | senders = [organiser_item] |
paul@292 | 120 | else: |
paul@292 | 121 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@292 | 122 | if not oa: |
paul@292 | 123 | return |
paul@218 | 124 | |
paul@292 | 125 | organiser_item, attendees = oa |
paul@292 | 126 | senders = attendees.items() |
paul@292 | 127 | |
paul@292 | 128 | if not senders: |
paul@292 | 129 | return |
paul@218 | 130 | |
paul@630 | 131 | freebusy = self.obj.get_period_values("FREEBUSY", period_type=FreeBusyPeriod) |
paul@562 | 132 | dtstart = self.obj.get_datetime("DTSTART") |
paul@562 | 133 | dtend = self.obj.get_datetime("DTEND") |
paul@620 | 134 | period = Period(dtstart, dtend, self.get_tzid()) |
paul@327 | 135 | |
paul@271 | 136 | for sender, sender_attr in senders: |
paul@468 | 137 | stored_freebusy = self.store.get_freebusy_for_other(self.user, sender) |
paul@562 | 138 | replace_overlapping(stored_freebusy, period, freebusy) |
paul@468 | 139 | self.store.set_freebusy_for_other(self.user, stored_freebusy, sender) |
paul@110 | 140 | |
paul@216 | 141 | class Event(PersonHandler): |
paul@67 | 142 | |
paul@67 | 143 | "An event handler." |
paul@67 | 144 | |
paul@63 | 145 | def add(self): |
paul@63 | 146 | |
paul@63 | 147 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 148 | |
paul@182 | 149 | return self.wrap("An addition to an event has been received.", link=False) |
paul@63 | 150 | |
paul@63 | 151 | def cancel(self): |
paul@63 | 152 | |
paul@142 | 153 | "Queue a cancellation of any active event." |
paul@63 | 154 | |
paul@271 | 155 | self._record(from_organiser=True, queue=False, cancel=True) |
paul@182 | 156 | return self.wrap("A cancellation has been received.", link=False) |
paul@63 | 157 | |
paul@63 | 158 | def counter(self): |
paul@63 | 159 | |
paul@63 | 160 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 161 | |
paul@182 | 162 | return self.wrap("A counter proposal has been received.", link=False) |
paul@63 | 163 | |
paul@63 | 164 | def declinecounter(self): |
paul@63 | 165 | |
paul@63 | 166 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 167 | |
paul@182 | 168 | return self.wrap("A declining counter proposal has been received.", link=False) |
paul@63 | 169 | |
paul@63 | 170 | def publish(self): |
paul@63 | 171 | |
paul@139 | 172 | "Register details of any relevant event." |
paul@63 | 173 | |
paul@271 | 174 | self._record(from_organiser=True, queue=False) |
paul@182 | 175 | return self.wrap("Details of an event have been received.") |
paul@63 | 176 | |
paul@63 | 177 | def refresh(self): |
paul@63 | 178 | |
paul@353 | 179 | "Generate details of any active event." |
paul@63 | 180 | |
paul@353 | 181 | # NOTE: Return event details if configured to do so. |
paul@353 | 182 | |
paul@353 | 183 | return self.wrap("A request for updated event details has been received.") |
paul@63 | 184 | |
paul@61 | 185 | def reply(self): |
paul@61 | 186 | |
paul@61 | 187 | "Record replies and notify the recipient." |
paul@61 | 188 | |
paul@271 | 189 | self._record(from_organiser=False, queue=False) |
paul@182 | 190 | return self.wrap("A reply has been received.") |
paul@61 | 191 | |
paul@61 | 192 | def request(self): |
paul@61 | 193 | |
paul@61 | 194 | "Hold requests and notify the recipient." |
paul@61 | 195 | |
paul@271 | 196 | self._record(from_organiser=True, queue=True) |
paul@216 | 197 | return self.wrap("A request has been received.") |
paul@60 | 198 | |
paul@108 | 199 | class Freebusy(PersonHandler, CommonFreebusy): |
paul@55 | 200 | |
paul@55 | 201 | "A free/busy handler." |
paul@55 | 202 | |
paul@55 | 203 | def publish(self): |
paul@63 | 204 | |
paul@110 | 205 | "Register free/busy information." |
paul@110 | 206 | |
paul@180 | 207 | self._record_freebusy(from_organiser=True) |
paul@180 | 208 | |
paul@180 | 209 | # Produce a message if configured to do so. |
paul@63 | 210 | |
paul@468 | 211 | if self.is_notifying(): |
paul@182 | 212 | return self.wrap("A free/busy update has been received.", link=False) |
paul@55 | 213 | |
paul@55 | 214 | def reply(self): |
paul@55 | 215 | |
paul@63 | 216 | "Record replies and notify the recipient." |
paul@63 | 217 | |
paul@180 | 218 | self._record_freebusy(from_organiser=False) |
paul@180 | 219 | |
paul@180 | 220 | # Produce a message if configured to do so. |
paul@139 | 221 | |
paul@468 | 222 | if self.is_notifying(): |
paul@182 | 223 | return self.wrap("A reply to a free/busy request has been received.", link=False) |
paul@55 | 224 | |
paul@55 | 225 | def request(self): |
paul@55 | 226 | |
paul@55 | 227 | """ |
paul@55 | 228 | Respond to a request by preparing a reply containing free/busy |
paul@468 | 229 | information for the recipient. |
paul@55 | 230 | """ |
paul@55 | 231 | |
paul@180 | 232 | # Produce a reply if configured to do so. |
paul@55 | 233 | |
paul@468 | 234 | if self.is_sharing(): |
paul@180 | 235 | return CommonFreebusy.request(self) |
paul@55 | 236 | |
paul@55 | 237 | # Handler registry. |
paul@55 | 238 | |
paul@55 | 239 | handlers = [ |
paul@55 | 240 | ("VFREEBUSY", Freebusy), |
paul@55 | 241 | ("VEVENT", Event), |
paul@55 | 242 | ] |
paul@55 | 243 | |
paul@55 | 244 | # vim: tabstop=4 expandtab shiftwidth=4 |