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