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@213 | 22 | from imiptools.content import Handler |
paul@271 | 23 | from imiptools.data import get_uri |
paul@327 | 24 | from imiptools.dates import format_datetime |
paul@216 | 25 | from imiptools.handlers.common import CommonFreebusy |
paul@327 | 26 | from imiptools.period import replace_overlapping |
paul@179 | 27 | from imiptools.profile import Preferences |
paul@55 | 28 | |
paul@67 | 29 | class PersonHandler(Handler): |
paul@55 | 30 | |
paul@67 | 31 | "Handling mechanisms specific to people." |
paul@55 | 32 | |
paul@271 | 33 | def _record(self, from_organiser=True, queue=False, cancel=False): |
paul@55 | 34 | |
paul@100 | 35 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@55 | 36 | if not oa: |
paul@61 | 37 | return False |
paul@55 | 38 | |
paul@94 | 39 | (organiser, organiser_attr), attendees = organiser_item, attendees = oa |
paul@94 | 40 | |
paul@110 | 41 | # Handle notifications and invitations. |
paul@55 | 42 | |
paul@100 | 43 | if from_organiser: |
paul@110 | 44 | |
paul@110 | 45 | # Process each attendee separately. |
paul@110 | 46 | |
paul@100 | 47 | for attendee, attendee_attr in attendees.items(): |
paul@100 | 48 | |
paul@213 | 49 | if not self.have_new_object(attendee): |
paul@100 | 50 | continue |
paul@100 | 51 | |
paul@100 | 52 | # Store the object and queue any request. |
paul@55 | 53 | |
paul@213 | 54 | self.store.set_event(attendee, self.uid, self.obj.to_node()) |
paul@55 | 55 | |
paul@100 | 56 | if queue: |
paul@100 | 57 | self.store.queue_request(attendee, self.uid) |
paul@142 | 58 | elif cancel: |
paul@142 | 59 | self.store.cancel_event(attendee, self.uid) |
paul@100 | 60 | |
paul@261 | 61 | # No return message will occur to update the free/busy |
paul@261 | 62 | # information, so this is done here. |
paul@261 | 63 | |
paul@261 | 64 | freebusy = self.store.get_freebusy(attendee) |
paul@261 | 65 | self.remove_from_freebusy(freebusy, attendee) |
paul@261 | 66 | |
paul@261 | 67 | if self.publisher: |
paul@261 | 68 | self.publisher.set_freebusy(attendee, freebusy) |
paul@261 | 69 | |
paul@268 | 70 | self.update_freebusy_from_organiser(attendee, organiser_item) |
paul@268 | 71 | |
paul@100 | 72 | # As organiser, update attendance. |
paul@55 | 73 | |
paul@100 | 74 | else: |
paul@268 | 75 | if self.merge_attendance(attendees, organiser): |
paul@268 | 76 | self.update_freebusy_from_attendees(organiser, attendees) |
paul@61 | 77 | |
paul@61 | 78 | return True |
paul@61 | 79 | |
paul@110 | 80 | def _record_freebusy(self, from_organiser=True): |
paul@110 | 81 | |
paul@110 | 82 | "Record free/busy information for the received information." |
paul@110 | 83 | |
paul@292 | 84 | if from_organiser: |
paul@292 | 85 | organiser_item = self.require_organiser(from_organiser) |
paul@292 | 86 | if not organiser_item: |
paul@292 | 87 | return |
paul@271 | 88 | |
paul@292 | 89 | senders = [organiser_item] |
paul@292 | 90 | else: |
paul@292 | 91 | oa = self.require_organiser_and_attendees(from_organiser) |
paul@292 | 92 | if not oa: |
paul@292 | 93 | return |
paul@218 | 94 | |
paul@292 | 95 | organiser_item, attendees = oa |
paul@292 | 96 | senders = attendees.items() |
paul@292 | 97 | |
paul@292 | 98 | if not senders: |
paul@292 | 99 | return |
paul@218 | 100 | |
paul@110 | 101 | freebusy = [] |
paul@111 | 102 | |
paul@213 | 103 | for value in self.obj.get_values("FREEBUSY") or []: |
paul@110 | 104 | if not isinstance(value, list): |
paul@110 | 105 | value = [value] |
paul@110 | 106 | for v in value: |
paul@110 | 107 | try: |
paul@110 | 108 | start, end = v.split("/", 1) |
paul@110 | 109 | freebusy.append((start, end)) |
paul@110 | 110 | except ValueError: |
paul@110 | 111 | pass |
paul@110 | 112 | |
paul@327 | 113 | dtstart = format_datetime(self.obj.get_utc_datetime("DTSTART")) |
paul@327 | 114 | dtend = format_datetime(self.obj.get_utc_datetime("DTEND")) |
paul@327 | 115 | user = get_uri(self.recipient) |
paul@327 | 116 | |
paul@271 | 117 | for sender, sender_attr in senders: |
paul@335 | 118 | stored_freebusy = self.store.get_freebusy_for_other(user, sender) |
paul@327 | 119 | replace_overlapping(stored_freebusy, (dtstart, dtend), freebusy) |
paul@337 | 120 | self.store.set_freebusy_for_other(user, sender, stored_freebusy) |
paul@110 | 121 | |
paul@216 | 122 | class Event(PersonHandler): |
paul@67 | 123 | |
paul@67 | 124 | "An event handler." |
paul@67 | 125 | |
paul@63 | 126 | def add(self): |
paul@63 | 127 | |
paul@63 | 128 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 129 | |
paul@182 | 130 | return self.wrap("An addition to an event has been received.", link=False) |
paul@63 | 131 | |
paul@63 | 132 | def cancel(self): |
paul@63 | 133 | |
paul@142 | 134 | "Queue a cancellation of any active event." |
paul@63 | 135 | |
paul@271 | 136 | self._record(from_organiser=True, queue=False, cancel=True) |
paul@182 | 137 | return self.wrap("A cancellation has been received.", link=False) |
paul@63 | 138 | |
paul@63 | 139 | def counter(self): |
paul@63 | 140 | |
paul@63 | 141 | # NOTE: Queue a suggested modification to any active event. |
paul@63 | 142 | |
paul@182 | 143 | return self.wrap("A counter proposal has been received.", link=False) |
paul@63 | 144 | |
paul@63 | 145 | def declinecounter(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("A declining counter proposal has been received.", link=False) |
paul@63 | 150 | |
paul@63 | 151 | def publish(self): |
paul@63 | 152 | |
paul@139 | 153 | "Register details of any relevant event." |
paul@63 | 154 | |
paul@271 | 155 | self._record(from_organiser=True, queue=False) |
paul@182 | 156 | return self.wrap("Details of an event have been received.") |
paul@63 | 157 | |
paul@63 | 158 | def refresh(self): |
paul@63 | 159 | |
paul@139 | 160 | "Update details of any active event." |
paul@63 | 161 | |
paul@271 | 162 | self._record(from_organiser=True, queue=False) |
paul@182 | 163 | return self.wrap("An event update has been received.") |
paul@63 | 164 | |
paul@61 | 165 | def reply(self): |
paul@61 | 166 | |
paul@61 | 167 | "Record replies and notify the recipient." |
paul@61 | 168 | |
paul@271 | 169 | self._record(from_organiser=False, queue=False) |
paul@182 | 170 | return self.wrap("A reply has been received.") |
paul@61 | 171 | |
paul@61 | 172 | def request(self): |
paul@61 | 173 | |
paul@61 | 174 | "Hold requests and notify the recipient." |
paul@61 | 175 | |
paul@271 | 176 | self._record(from_organiser=True, queue=True) |
paul@216 | 177 | return self.wrap("A request has been received.") |
paul@60 | 178 | |
paul@108 | 179 | class Freebusy(PersonHandler, CommonFreebusy): |
paul@55 | 180 | |
paul@55 | 181 | "A free/busy handler." |
paul@55 | 182 | |
paul@55 | 183 | def publish(self): |
paul@63 | 184 | |
paul@110 | 185 | "Register free/busy information." |
paul@110 | 186 | |
paul@180 | 187 | self._record_freebusy(from_organiser=True) |
paul@180 | 188 | |
paul@180 | 189 | # Produce a message if configured to do so. |
paul@63 | 190 | |
paul@180 | 191 | preferences = Preferences(get_uri(self.recipient)) |
paul@180 | 192 | if preferences.get("freebusy_messages") == "notify": |
paul@182 | 193 | return self.wrap("A free/busy update has been received.", link=False) |
paul@55 | 194 | |
paul@55 | 195 | def reply(self): |
paul@55 | 196 | |
paul@63 | 197 | "Record replies and notify the recipient." |
paul@63 | 198 | |
paul@180 | 199 | self._record_freebusy(from_organiser=False) |
paul@180 | 200 | |
paul@180 | 201 | # Produce a message if configured to do so. |
paul@139 | 202 | |
paul@180 | 203 | preferences = Preferences(get_uri(self.recipient)) |
paul@180 | 204 | if preferences.get("freebusy_messages") == "notify": |
paul@182 | 205 | return self.wrap("A reply to a free/busy request has been received.", link=False) |
paul@55 | 206 | |
paul@55 | 207 | def request(self): |
paul@55 | 208 | |
paul@55 | 209 | """ |
paul@55 | 210 | Respond to a request by preparing a reply containing free/busy |
paul@55 | 211 | information for each indicated attendee. |
paul@55 | 212 | """ |
paul@55 | 213 | |
paul@180 | 214 | # Produce a reply if configured to do so. |
paul@55 | 215 | |
paul@180 | 216 | preferences = Preferences(get_uri(self.recipient)) |
paul@180 | 217 | if preferences.get("freebusy_sharing") == "share": |
paul@180 | 218 | return CommonFreebusy.request(self) |
paul@55 | 219 | |
paul@67 | 220 | class Journal(PersonHandler): |
paul@55 | 221 | |
paul@55 | 222 | "A journal entry handler." |
paul@55 | 223 | |
paul@55 | 224 | def add(self): |
paul@63 | 225 | |
paul@63 | 226 | # NOTE: Queue a suggested modification to any active entry. |
paul@63 | 227 | |
paul@182 | 228 | return self.wrap("An addition to a journal entry has been received.", link=False) |
paul@55 | 229 | |
paul@55 | 230 | def cancel(self): |
paul@63 | 231 | |
paul@63 | 232 | # NOTE: Queue a suggested modification to any active entry. |
paul@63 | 233 | |
paul@182 | 234 | return self.wrap("A cancellation has been received.", link=False) |
paul@55 | 235 | |
paul@55 | 236 | def publish(self): |
paul@63 | 237 | |
paul@63 | 238 | # NOTE: Register details of any relevant entry. |
paul@63 | 239 | |
paul@271 | 240 | self._record(from_organiser=True, queue=False) |
paul@182 | 241 | return self.wrap("Details of a journal entry have been received.") |
paul@55 | 242 | |
paul@67 | 243 | class Todo(PersonHandler): |
paul@55 | 244 | |
paul@55 | 245 | "A to-do item handler." |
paul@55 | 246 | |
paul@55 | 247 | def add(self): |
paul@63 | 248 | |
paul@63 | 249 | # NOTE: Queue a suggested modification to any active item. |
paul@63 | 250 | |
paul@182 | 251 | return self.wrap("An addition to an item has been received.", link=False) |
paul@55 | 252 | |
paul@55 | 253 | def cancel(self): |
paul@63 | 254 | |
paul@63 | 255 | # NOTE: Queue a suggested modification to any active item. |
paul@63 | 256 | |
paul@182 | 257 | return self.wrap("A cancellation has been received.", link=False) |
paul@55 | 258 | |
paul@55 | 259 | def counter(self): |
paul@55 | 260 | |
paul@63 | 261 | # NOTE: Queue a suggested modification to any active item. |
paul@55 | 262 | |
paul@182 | 263 | return self.wrap("A counter proposal has been received.", link=False) |
paul@55 | 264 | |
paul@55 | 265 | def declinecounter(self): |
paul@55 | 266 | |
paul@63 | 267 | # NOTE: Queue a suggested modification to any active item. |
paul@55 | 268 | |
paul@182 | 269 | return self.wrap("A declining counter proposal has been received.", link=False) |
paul@55 | 270 | |
paul@55 | 271 | def publish(self): |
paul@63 | 272 | |
paul@139 | 273 | "Register details of any relevant item." |
paul@63 | 274 | |
paul@271 | 275 | self._record(from_organiser=True, queue=False) |
paul@182 | 276 | return self.wrap("Details of an item have been received.") |
paul@55 | 277 | |
paul@55 | 278 | def refresh(self): |
paul@63 | 279 | |
paul@139 | 280 | "Update details of any active item." |
paul@63 | 281 | |
paul@271 | 282 | self._record(from_organiser=True, queue=False) |
paul@182 | 283 | return self.wrap("An item update has been received.") |
paul@55 | 284 | |
paul@55 | 285 | def reply(self): |
paul@55 | 286 | |
paul@63 | 287 | "Record replies and notify the recipient." |
paul@63 | 288 | |
paul@271 | 289 | self._record(from_organiser=False, queue=False) |
paul@182 | 290 | return self.wrap("A reply has been received.") |
paul@55 | 291 | |
paul@55 | 292 | def request(self): |
paul@63 | 293 | |
paul@63 | 294 | "Hold requests and notify the recipient." |
paul@63 | 295 | |
paul@271 | 296 | self._record(from_organiser=True, queue=True) |
paul@182 | 297 | return self.wrap("A request has been received.") |
paul@55 | 298 | |
paul@55 | 299 | # Handler registry. |
paul@55 | 300 | |
paul@55 | 301 | handlers = [ |
paul@55 | 302 | ("VFREEBUSY", Freebusy), |
paul@55 | 303 | ("VEVENT", Event), |
paul@55 | 304 | ("VTODO", Todo), |
paul@55 | 305 | ("VJOURNAL", Journal), |
paul@55 | 306 | ] |
paul@55 | 307 | |
paul@55 | 308 | # vim: tabstop=4 expandtab shiftwidth=4 |