paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from email import message_from_file |
paul@12 | 4 | from email.mime.multipart import MIMEMultipart |
paul@8 | 5 | from email.mime.text import MIMEText |
paul@8 | 6 | from smtplib import SMTP |
paul@8 | 7 | from vCalendar import parse, iterwrite, ParseError, SECTION_TYPES |
paul@2 | 8 | import imip_store |
paul@0 | 9 | import sys |
paul@0 | 10 | |
paul@0 | 11 | try: |
paul@0 | 12 | from cStringIO import StringIO |
paul@0 | 13 | except ImportError: |
paul@0 | 14 | from StringIO import StringIO |
paul@0 | 15 | |
paul@12 | 16 | OWNER = "resource+manager@example.com" |
paul@12 | 17 | |
paul@0 | 18 | # Postfix exit codes. |
paul@0 | 19 | |
paul@0 | 20 | EX_USAGE = 64 |
paul@0 | 21 | EX_DATAERR = 65 |
paul@0 | 22 | EX_NOINPUT = 66 |
paul@0 | 23 | EX_NOUSER = 67 |
paul@0 | 24 | EX_NOHOST = 68 |
paul@0 | 25 | EX_UNAVAILABLE = 69 |
paul@0 | 26 | EX_SOFTWARE = 70 |
paul@0 | 27 | EX_OSERR = 71 |
paul@0 | 28 | EX_OSFILE = 72 |
paul@0 | 29 | EX_CANTCREAT = 73 |
paul@0 | 30 | EX_IOERR = 74 |
paul@0 | 31 | EX_TEMPFAIL = 75 |
paul@0 | 32 | EX_PROTOCOL = 76 |
paul@0 | 33 | EX_NOPERM = 77 |
paul@0 | 34 | EX_CONFIG = 78 |
paul@0 | 35 | |
paul@0 | 36 | # Permitted iTIP content types. |
paul@0 | 37 | |
paul@0 | 38 | itip_content_types = [ |
paul@0 | 39 | "text/calendar", # from RFC 6047 |
paul@0 | 40 | "text/x-vcalendar", "application/ics", # other possibilities |
paul@0 | 41 | ] |
paul@0 | 42 | |
paul@8 | 43 | # Sending of outgoing messages. |
paul@8 | 44 | |
paul@8 | 45 | def sendmail(sender, recipients, data): |
paul@8 | 46 | smtp = SMTP("localhost") |
paul@8 | 47 | smtp.sendmail(sender, recipients, data) |
paul@8 | 48 | smtp.quit() |
paul@8 | 49 | |
paul@8 | 50 | # Processing of incoming messages. |
paul@8 | 51 | |
paul@9 | 52 | def process(f, original_recipients, recipients): |
paul@8 | 53 | |
paul@9 | 54 | """ |
paul@9 | 55 | Process content from the stream 'f' accompanied by the given |
paul@9 | 56 | 'original_recipients' and 'recipients'. |
paul@9 | 57 | """ |
paul@8 | 58 | |
paul@0 | 59 | msg = message_from_file(f) |
paul@13 | 60 | senders = msg.get_all("Reply-To") or msg.get_all("From") |
paul@0 | 61 | |
paul@0 | 62 | # Handle messages with iTIP parts. |
paul@0 | 63 | |
paul@12 | 64 | all_parts = [] |
paul@12 | 65 | |
paul@0 | 66 | for part in msg.walk(): |
paul@0 | 67 | if part.get_content_type() in itip_content_types and \ |
paul@0 | 68 | part.get_param("method"): |
paul@0 | 69 | |
paul@12 | 70 | all_parts += handle_itip_part(part, original_recipients) |
paul@12 | 71 | |
paul@12 | 72 | # Pack the parts into a single message. |
paul@12 | 73 | |
paul@12 | 74 | if all_parts: |
paul@12 | 75 | if len(all_parts) > 1: |
paul@12 | 76 | message = MIMEMultipart("alternative", _subparts=all_parts) |
paul@12 | 77 | else: |
paul@12 | 78 | message = all_parts[0] |
paul@12 | 79 | |
paul@12 | 80 | message["From"] = OWNER |
paul@13 | 81 | for sender in senders: |
paul@13 | 82 | message["To"] = sender |
paul@12 | 83 | |
paul@12 | 84 | if "-d" in sys.argv: |
paul@12 | 85 | print message |
paul@13 | 86 | else: |
paul@13 | 87 | sendmail(OWNER, senders, message.as_string()) |
paul@0 | 88 | |
paul@0 | 89 | def get_itip_elements(elements): |
paul@0 | 90 | d = {} |
paul@0 | 91 | for name, attr, value in elements: |
paul@8 | 92 | if not d.has_key(name): |
paul@8 | 93 | d[name] = [] |
paul@8 | 94 | if name in SECTION_TYPES: |
paul@11 | 95 | d[name].append((get_itip_elements(value), attr)) |
paul@0 | 96 | else: |
paul@11 | 97 | d[name].append((value, attr)) |
paul@0 | 98 | return d |
paul@0 | 99 | |
paul@11 | 100 | def get_items(d, name, all=True): |
paul@5 | 101 | if d.has_key(name): |
paul@5 | 102 | values = d[name] |
paul@11 | 103 | if not all and len(values) == 1: |
paul@5 | 104 | return values[0] |
paul@5 | 105 | else: |
paul@5 | 106 | return values |
paul@5 | 107 | else: |
paul@5 | 108 | return None |
paul@5 | 109 | |
paul@11 | 110 | def get_item(d, name): |
paul@11 | 111 | return get_items(d, name, False) |
paul@11 | 112 | |
paul@10 | 113 | def get_value_map(d, name): |
paul@11 | 114 | items = get_items(d, name) |
paul@10 | 115 | if items: |
paul@11 | 116 | return dict(items) |
paul@10 | 117 | else: |
paul@10 | 118 | return {} |
paul@10 | 119 | |
paul@11 | 120 | def get_values(d, name, all=True): |
paul@0 | 121 | if d.has_key(name): |
paul@2 | 122 | values = d[name] |
paul@11 | 123 | if not all and len(values) == 1: |
paul@11 | 124 | return values[0][0] |
paul@2 | 125 | else: |
paul@11 | 126 | return map(lambda x: x[0], values) |
paul@0 | 127 | else: |
paul@0 | 128 | return None |
paul@0 | 129 | |
paul@11 | 130 | def get_value(d, name): |
paul@11 | 131 | return get_values(d, name, False) |
paul@10 | 132 | |
paul@10 | 133 | def get_address(value): |
paul@10 | 134 | return value.startswith("mailto:") and value[7:] or value |
paul@10 | 135 | |
paul@10 | 136 | def get_uri(value): |
paul@10 | 137 | return value.startswith("mailto:") and value or "mailto:%s" % value |
paul@10 | 138 | |
paul@12 | 139 | def to_part(method, calendar): |
paul@12 | 140 | out = StringIO() |
paul@12 | 141 | try: |
paul@12 | 142 | w = iterwrite(out, encoding="utf-8") |
paul@12 | 143 | calendar[:0] = [ |
paul@12 | 144 | ("METHOD", {}, method), |
paul@12 | 145 | ("VERSION", {}, "2.0") |
paul@12 | 146 | ] |
paul@12 | 147 | w.write("VCALENDAR", {}, calendar) |
paul@12 | 148 | return MIMEText(out.getvalue(), "calendar", "utf-8") |
paul@12 | 149 | finally: |
paul@12 | 150 | out.close() |
paul@12 | 151 | |
paul@9 | 152 | def handle_itip_part(part, recipients): |
paul@12 | 153 | |
paul@12 | 154 | "Handle the given iTIP 'part' for the given 'recipients'." |
paul@12 | 155 | |
paul@0 | 156 | method = part.get_param("method") |
paul@0 | 157 | |
paul@8 | 158 | # Decode the data and parse it. |
paul@8 | 159 | |
paul@0 | 160 | f = StringIO(part.get_payload(decode=True)) |
paul@8 | 161 | |
paul@8 | 162 | try: |
paul@8 | 163 | doctype, attrs, elements = parse(f, encoding=part.get_content_charset()) |
paul@8 | 164 | except (ParseError, ValueError): |
paul@8 | 165 | sys.exit(EX_DATAERR) |
paul@8 | 166 | |
paul@8 | 167 | # Only handle calendar information. |
paul@0 | 168 | |
paul@12 | 169 | all_parts = [] |
paul@12 | 170 | |
paul@0 | 171 | if doctype == "VCALENDAR": |
paul@0 | 172 | itip = get_itip_elements(elements) |
paul@0 | 173 | |
paul@8 | 174 | # Require consistency between declared and employed methods. |
paul@8 | 175 | |
paul@0 | 176 | if get_value(itip, "METHOD") == method: |
paul@2 | 177 | |
paul@8 | 178 | # Look for different kinds of sections. |
paul@8 | 179 | |
paul@12 | 180 | all_objects = [] |
paul@12 | 181 | |
paul@2 | 182 | for name, cls in handlers: |
paul@11 | 183 | for details in get_values(itip, name) or []: |
paul@8 | 184 | |
paul@8 | 185 | # Dispatch to a handler and obtain any response. |
paul@2 | 186 | |
paul@9 | 187 | handler = cls(details, recipients) |
paul@12 | 188 | object = methods[method](handler)() |
paul@12 | 189 | |
paul@12 | 190 | # Concatenate responses for a single calendar object. |
paul@12 | 191 | |
paul@12 | 192 | if object: |
paul@12 | 193 | all_objects += object |
paul@8 | 194 | |
paul@12 | 195 | # Obtain a message part for the objects. |
paul@12 | 196 | |
paul@12 | 197 | if all_objects: |
paul@12 | 198 | all_parts.append(to_part(method, all_objects)) |
paul@12 | 199 | |
paul@12 | 200 | return all_parts |
paul@2 | 201 | |
paul@2 | 202 | class Handler: |
paul@8 | 203 | |
paul@8 | 204 | "General handler support." |
paul@8 | 205 | |
paul@9 | 206 | def __init__(self, details, recipients): |
paul@9 | 207 | |
paul@9 | 208 | """ |
paul@9 | 209 | Initialise the handler with the 'details' of a calendar object and the |
paul@9 | 210 | 'recipients' of the object. |
paul@9 | 211 | """ |
paul@9 | 212 | |
paul@2 | 213 | self.details = details |
paul@9 | 214 | self.recipients = set(recipients) |
paul@9 | 215 | |
paul@2 | 216 | self.uid = get_value(details, "UID") |
paul@2 | 217 | self.sequence = get_value(details, "SEQUENCE") |
paul@2 | 218 | self.store = imip_store.FileStore() |
paul@2 | 219 | |
paul@11 | 220 | def get_items(self, name, all=True): |
paul@11 | 221 | return get_items(self.details, name, all) |
paul@11 | 222 | |
paul@11 | 223 | def get_item(self, name): |
paul@11 | 224 | return get_item(self.details, name) |
paul@5 | 225 | |
paul@10 | 226 | def get_value_map(self, name): |
paul@10 | 227 | return get_value_map(self.details, name) |
paul@10 | 228 | |
paul@11 | 229 | def get_values(self, name, all=True): |
paul@11 | 230 | return get_values(self.details, name, all) |
paul@11 | 231 | |
paul@11 | 232 | def get_value(self, name): |
paul@11 | 233 | return get_value(self.details, name) |
paul@2 | 234 | |
paul@10 | 235 | def filter_by_recipients(self, values): |
paul@10 | 236 | return self.recipients.intersection(map(get_address, values)) |
paul@9 | 237 | |
paul@14 | 238 | def require_organiser_and_attendees(self): |
paul@14 | 239 | attendee_map = self.get_value_map("ATTENDEE") |
paul@14 | 240 | organiser = self.get_item("ORGANIZER") |
paul@14 | 241 | |
paul@14 | 242 | # Only provide details for recipients who are also attendees. |
paul@14 | 243 | |
paul@14 | 244 | attendees = {} |
paul@14 | 245 | for attendee in map(get_uri, self.filter_by_recipients(attendee_map)): |
paul@14 | 246 | attendees[attendee] = attendee_map[attendee] |
paul@14 | 247 | |
paul@14 | 248 | if not attendees and not organiser: |
paul@14 | 249 | return None |
paul@14 | 250 | |
paul@14 | 251 | return organiser, attendees |
paul@14 | 252 | |
paul@2 | 253 | class Event(Handler): |
paul@6 | 254 | |
paul@6 | 255 | "An event handler." |
paul@6 | 256 | |
paul@2 | 257 | def add(self): |
paul@2 | 258 | pass |
paul@2 | 259 | |
paul@2 | 260 | def cancel(self): |
paul@2 | 261 | pass |
paul@2 | 262 | |
paul@2 | 263 | def counter(self): |
paul@2 | 264 | pass |
paul@2 | 265 | |
paul@2 | 266 | def declinecounter(self): |
paul@2 | 267 | pass |
paul@2 | 268 | |
paul@6 | 269 | def publish(self): |
paul@6 | 270 | pass |
paul@6 | 271 | |
paul@2 | 272 | def refresh(self): |
paul@2 | 273 | pass |
paul@2 | 274 | |
paul@2 | 275 | def reply(self): |
paul@6 | 276 | |
paul@6 | 277 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 278 | |
paul@2 | 279 | pass |
paul@2 | 280 | |
paul@2 | 281 | def request(self): |
paul@14 | 282 | |
paul@14 | 283 | """ |
paul@14 | 284 | Respond to a request by preparing a reply containing accept/decline |
paul@14 | 285 | information for each indicated attendee. |
paul@14 | 286 | """ |
paul@2 | 287 | |
paul@2 | 288 | class Freebusy(Handler): |
paul@6 | 289 | |
paul@6 | 290 | "A free/busy handler." |
paul@6 | 291 | |
paul@6 | 292 | def publish(self): |
paul@6 | 293 | pass |
paul@6 | 294 | |
paul@2 | 295 | def reply(self): |
paul@2 | 296 | |
paul@6 | 297 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 298 | |
paul@6 | 299 | pass |
paul@0 | 300 | |
paul@2 | 301 | def request(self): |
paul@6 | 302 | |
paul@6 | 303 | """ |
paul@12 | 304 | Respond to a request by preparing a reply containing free/busy |
paul@12 | 305 | information for each indicated attendee. |
paul@6 | 306 | """ |
paul@6 | 307 | |
paul@14 | 308 | oa = self.require_organiser_and_attendees() |
paul@14 | 309 | if not oa: |
paul@14 | 310 | return None |
paul@9 | 311 | |
paul@14 | 312 | (organiser, organiser_attr), attendees = oa |
paul@5 | 313 | |
paul@12 | 314 | # Construct an appropriate fragment. |
paul@12 | 315 | |
paul@12 | 316 | calendar = [] |
paul@12 | 317 | cwrite = calendar.append |
paul@2 | 318 | |
paul@12 | 319 | # Get the details for each attendee. |
paul@5 | 320 | |
paul@14 | 321 | for attendee, attendee_attr in attendees.items(): |
paul@12 | 322 | freebusy = self.store.get_freebusy(attendee) |
paul@2 | 323 | |
paul@12 | 324 | if freebusy: |
paul@12 | 325 | record = [] |
paul@12 | 326 | rwrite = record.append |
paul@12 | 327 | |
paul@12 | 328 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@14 | 329 | rwrite(("ATTENDEE", attendee_attr, attendee)) |
paul@12 | 330 | rwrite(("UID", {}, self.uid)) |
paul@5 | 331 | |
paul@12 | 332 | for start, end in freebusy: |
paul@12 | 333 | rwrite(("FREEBUSY", {}, [start, end])) |
paul@5 | 334 | |
paul@12 | 335 | cwrite(("VFREEBUSY", {}, record)) |
paul@5 | 336 | |
paul@12 | 337 | # Return the reply. |
paul@8 | 338 | |
paul@12 | 339 | return calendar |
paul@0 | 340 | |
paul@2 | 341 | class Journal(Handler): |
paul@6 | 342 | |
paul@6 | 343 | "A journal entry handler." |
paul@6 | 344 | |
paul@2 | 345 | def add(self): |
paul@2 | 346 | pass |
paul@2 | 347 | |
paul@2 | 348 | def cancel(self): |
paul@2 | 349 | pass |
paul@2 | 350 | |
paul@6 | 351 | def publish(self): |
paul@6 | 352 | pass |
paul@6 | 353 | |
paul@2 | 354 | class Todo(Handler): |
paul@6 | 355 | |
paul@6 | 356 | "A to-do item handler." |
paul@6 | 357 | |
paul@2 | 358 | def add(self): |
paul@2 | 359 | pass |
paul@2 | 360 | |
paul@2 | 361 | def cancel(self): |
paul@2 | 362 | pass |
paul@2 | 363 | |
paul@2 | 364 | def counter(self): |
paul@2 | 365 | pass |
paul@0 | 366 | |
paul@2 | 367 | def declinecounter(self): |
paul@2 | 368 | pass |
paul@2 | 369 | |
paul@6 | 370 | def publish(self): |
paul@6 | 371 | pass |
paul@6 | 372 | |
paul@2 | 373 | def refresh(self): |
paul@2 | 374 | pass |
paul@2 | 375 | |
paul@2 | 376 | def reply(self): |
paul@6 | 377 | |
paul@6 | 378 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 379 | |
paul@2 | 380 | pass |
paul@2 | 381 | |
paul@2 | 382 | def request(self): |
paul@2 | 383 | pass |
paul@2 | 384 | |
paul@2 | 385 | # Handler registry. |
paul@0 | 386 | |
paul@2 | 387 | handlers = [ |
paul@2 | 388 | ("VFREEBUSY", Freebusy), |
paul@2 | 389 | ("VEVENT", Event), |
paul@2 | 390 | ("VTODO", Todo), |
paul@2 | 391 | ("VJOURNAL", Journal), |
paul@2 | 392 | ] |
paul@0 | 393 | |
paul@2 | 394 | methods = { |
paul@2 | 395 | "ADD" : lambda handler: handler.add, |
paul@2 | 396 | "CANCEL" : lambda handler: handler.cancel, |
paul@2 | 397 | "COUNTER" : lambda handler: handler.counter, |
paul@2 | 398 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@2 | 399 | "PUBLISH" : lambda handler: handler.publish, |
paul@2 | 400 | "REFRESH" : lambda handler: handler.refresh, |
paul@2 | 401 | "REPLY" : lambda handler: handler.reply, |
paul@2 | 402 | "REQUEST" : lambda handler: handler.request, |
paul@2 | 403 | } |
paul@0 | 404 | |
paul@11 | 405 | |
paul@11 | 406 | def main(): |
paul@11 | 407 | |
paul@11 | 408 | # Obtain the different kinds of recipients. |
paul@9 | 409 | |
paul@11 | 410 | original_recipients = [] |
paul@11 | 411 | recipients = [] |
paul@9 | 412 | |
paul@11 | 413 | l = [] |
paul@9 | 414 | |
paul@11 | 415 | for arg in sys.argv[1:]: |
paul@11 | 416 | if arg == "-o": |
paul@11 | 417 | l = original_recipients |
paul@11 | 418 | elif arg == "-r": |
paul@11 | 419 | l = recipients |
paul@11 | 420 | elif arg == "-d": |
paul@11 | 421 | pass |
paul@11 | 422 | else: |
paul@11 | 423 | l.append(arg) |
paul@11 | 424 | |
paul@11 | 425 | process(sys.stdin, original_recipients, recipients) |
paul@9 | 426 | |
paul@11 | 427 | if __name__ == "__main__": |
paul@11 | 428 | if "-d" in sys.argv[1:]: |
paul@11 | 429 | main() |
paul@9 | 430 | else: |
paul@11 | 431 | try: |
paul@11 | 432 | main() |
paul@11 | 433 | except SystemExit, value: |
paul@11 | 434 | sys.exit(value) |
paul@11 | 435 | except Exception, exc: |
paul@11 | 436 | type, value, tb = sys.exc_info() |
paul@11 | 437 | print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) |
paul@11 | 438 | sys.exit(EX_TEMPFAIL) |
paul@11 | 439 | sys.exit(0) |
paul@0 | 440 | |
paul@0 | 441 | # vim: tabstop=4 expandtab shiftwidth=4 |