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