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