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