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