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