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@12 | 60 | sender = msg.get("Reply-To") or msg["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@12 | 81 | message["To"] = sender |
paul@12 | 82 | |
paul@12 | 83 | if "-d" in sys.argv: |
paul@12 | 84 | print message |
paul@0 | 85 | |
paul@0 | 86 | def get_itip_elements(elements): |
paul@0 | 87 | d = {} |
paul@0 | 88 | for name, attr, value in elements: |
paul@8 | 89 | if not d.has_key(name): |
paul@8 | 90 | d[name] = [] |
paul@8 | 91 | if name in SECTION_TYPES: |
paul@11 | 92 | d[name].append((get_itip_elements(value), attr)) |
paul@0 | 93 | else: |
paul@11 | 94 | d[name].append((value, attr)) |
paul@0 | 95 | return d |
paul@0 | 96 | |
paul@11 | 97 | def get_items(d, name, all=True): |
paul@5 | 98 | if d.has_key(name): |
paul@5 | 99 | values = d[name] |
paul@11 | 100 | if not all and len(values) == 1: |
paul@5 | 101 | return values[0] |
paul@5 | 102 | else: |
paul@5 | 103 | return values |
paul@5 | 104 | else: |
paul@5 | 105 | return None |
paul@5 | 106 | |
paul@11 | 107 | def get_item(d, name): |
paul@11 | 108 | return get_items(d, name, False) |
paul@11 | 109 | |
paul@10 | 110 | def get_value_map(d, name): |
paul@11 | 111 | items = get_items(d, name) |
paul@10 | 112 | if items: |
paul@11 | 113 | return dict(items) |
paul@10 | 114 | else: |
paul@10 | 115 | return {} |
paul@10 | 116 | |
paul@11 | 117 | def get_values(d, name, all=True): |
paul@0 | 118 | if d.has_key(name): |
paul@2 | 119 | values = d[name] |
paul@11 | 120 | if not all and len(values) == 1: |
paul@11 | 121 | return values[0][0] |
paul@2 | 122 | else: |
paul@11 | 123 | return map(lambda x: x[0], values) |
paul@0 | 124 | else: |
paul@0 | 125 | return None |
paul@0 | 126 | |
paul@11 | 127 | def get_value(d, name): |
paul@11 | 128 | return get_values(d, name, False) |
paul@10 | 129 | |
paul@10 | 130 | def get_address(value): |
paul@10 | 131 | return value.startswith("mailto:") and value[7:] or value |
paul@10 | 132 | |
paul@10 | 133 | def get_uri(value): |
paul@10 | 134 | return value.startswith("mailto:") and value or "mailto:%s" % value |
paul@10 | 135 | |
paul@12 | 136 | def to_part(method, calendar): |
paul@12 | 137 | out = StringIO() |
paul@12 | 138 | try: |
paul@12 | 139 | w = iterwrite(out, encoding="utf-8") |
paul@12 | 140 | calendar[:0] = [ |
paul@12 | 141 | ("METHOD", {}, method), |
paul@12 | 142 | ("VERSION", {}, "2.0") |
paul@12 | 143 | ] |
paul@12 | 144 | w.write("VCALENDAR", {}, calendar) |
paul@12 | 145 | return MIMEText(out.getvalue(), "calendar", "utf-8") |
paul@12 | 146 | finally: |
paul@12 | 147 | out.close() |
paul@12 | 148 | |
paul@9 | 149 | def handle_itip_part(part, recipients): |
paul@12 | 150 | |
paul@12 | 151 | "Handle the given iTIP 'part' for the given 'recipients'." |
paul@12 | 152 | |
paul@0 | 153 | method = part.get_param("method") |
paul@0 | 154 | |
paul@8 | 155 | # Decode the data and parse it. |
paul@8 | 156 | |
paul@0 | 157 | f = StringIO(part.get_payload(decode=True)) |
paul@8 | 158 | |
paul@8 | 159 | try: |
paul@8 | 160 | doctype, attrs, elements = parse(f, encoding=part.get_content_charset()) |
paul@8 | 161 | except (ParseError, ValueError): |
paul@8 | 162 | sys.exit(EX_DATAERR) |
paul@8 | 163 | |
paul@8 | 164 | # Only handle calendar information. |
paul@0 | 165 | |
paul@12 | 166 | all_parts = [] |
paul@12 | 167 | |
paul@0 | 168 | if doctype == "VCALENDAR": |
paul@0 | 169 | itip = get_itip_elements(elements) |
paul@0 | 170 | |
paul@8 | 171 | # Require consistency between declared and employed methods. |
paul@8 | 172 | |
paul@0 | 173 | if get_value(itip, "METHOD") == method: |
paul@2 | 174 | |
paul@8 | 175 | # Look for different kinds of sections. |
paul@8 | 176 | |
paul@12 | 177 | all_objects = [] |
paul@12 | 178 | |
paul@2 | 179 | for name, cls in handlers: |
paul@11 | 180 | for details in get_values(itip, name) or []: |
paul@8 | 181 | |
paul@8 | 182 | # Dispatch to a handler and obtain any response. |
paul@2 | 183 | |
paul@9 | 184 | handler = cls(details, recipients) |
paul@12 | 185 | object = methods[method](handler)() |
paul@12 | 186 | |
paul@12 | 187 | # Concatenate responses for a single calendar object. |
paul@12 | 188 | |
paul@12 | 189 | if object: |
paul@12 | 190 | all_objects += object |
paul@8 | 191 | |
paul@12 | 192 | # Obtain a message part for the objects. |
paul@12 | 193 | |
paul@12 | 194 | if all_objects: |
paul@12 | 195 | all_parts.append(to_part(method, all_objects)) |
paul@12 | 196 | |
paul@12 | 197 | return all_parts |
paul@2 | 198 | |
paul@2 | 199 | class Handler: |
paul@8 | 200 | |
paul@8 | 201 | "General handler support." |
paul@8 | 202 | |
paul@9 | 203 | def __init__(self, details, recipients): |
paul@9 | 204 | |
paul@9 | 205 | """ |
paul@9 | 206 | Initialise the handler with the 'details' of a calendar object and the |
paul@9 | 207 | 'recipients' of the object. |
paul@9 | 208 | """ |
paul@9 | 209 | |
paul@2 | 210 | self.details = details |
paul@9 | 211 | self.recipients = set(recipients) |
paul@9 | 212 | |
paul@2 | 213 | self.uid = get_value(details, "UID") |
paul@2 | 214 | self.sequence = get_value(details, "SEQUENCE") |
paul@2 | 215 | self.store = imip_store.FileStore() |
paul@2 | 216 | |
paul@11 | 217 | def get_items(self, name, all=True): |
paul@11 | 218 | return get_items(self.details, name, all) |
paul@11 | 219 | |
paul@11 | 220 | def get_item(self, name): |
paul@11 | 221 | return get_item(self.details, name) |
paul@5 | 222 | |
paul@10 | 223 | def get_value_map(self, name): |
paul@10 | 224 | return get_value_map(self.details, name) |
paul@10 | 225 | |
paul@11 | 226 | def get_values(self, name, all=True): |
paul@11 | 227 | return get_values(self.details, name, all) |
paul@11 | 228 | |
paul@11 | 229 | def get_value(self, name): |
paul@11 | 230 | return get_value(self.details, name) |
paul@2 | 231 | |
paul@10 | 232 | def filter_by_recipients(self, values): |
paul@10 | 233 | return self.recipients.intersection(map(get_address, values)) |
paul@9 | 234 | |
paul@2 | 235 | class Event(Handler): |
paul@6 | 236 | |
paul@6 | 237 | "An event handler." |
paul@6 | 238 | |
paul@2 | 239 | def add(self): |
paul@2 | 240 | pass |
paul@2 | 241 | |
paul@2 | 242 | def cancel(self): |
paul@2 | 243 | pass |
paul@2 | 244 | |
paul@2 | 245 | def counter(self): |
paul@2 | 246 | pass |
paul@2 | 247 | |
paul@2 | 248 | def declinecounter(self): |
paul@2 | 249 | pass |
paul@2 | 250 | |
paul@6 | 251 | def publish(self): |
paul@6 | 252 | pass |
paul@6 | 253 | |
paul@2 | 254 | def refresh(self): |
paul@2 | 255 | pass |
paul@2 | 256 | |
paul@2 | 257 | def reply(self): |
paul@6 | 258 | |
paul@6 | 259 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 260 | |
paul@2 | 261 | pass |
paul@2 | 262 | |
paul@2 | 263 | def request(self): |
paul@2 | 264 | pass |
paul@2 | 265 | |
paul@2 | 266 | class Freebusy(Handler): |
paul@6 | 267 | |
paul@6 | 268 | "A free/busy handler." |
paul@6 | 269 | |
paul@6 | 270 | def publish(self): |
paul@6 | 271 | pass |
paul@6 | 272 | |
paul@2 | 273 | def reply(self): |
paul@2 | 274 | |
paul@6 | 275 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 276 | |
paul@6 | 277 | pass |
paul@0 | 278 | |
paul@2 | 279 | def request(self): |
paul@6 | 280 | |
paul@6 | 281 | """ |
paul@12 | 282 | Respond to a request by preparing a reply containing free/busy |
paul@12 | 283 | information for each indicated attendee. |
paul@6 | 284 | """ |
paul@6 | 285 | |
paul@10 | 286 | attendee_map = self.get_value_map("ATTENDEE") |
paul@11 | 287 | organiser = self.get_item("ORGANIZER") |
paul@5 | 288 | |
paul@9 | 289 | # Only provide details for recipients who are also attendees. |
paul@9 | 290 | |
paul@10 | 291 | attendees = map(get_uri, self.filter_by_recipients(attendee_map)) |
paul@9 | 292 | |
paul@2 | 293 | if not attendees and not organiser: |
paul@2 | 294 | return |
paul@2 | 295 | |
paul@11 | 296 | organiser, organiser_attr = organiser |
paul@5 | 297 | |
paul@12 | 298 | # Construct an appropriate fragment. |
paul@12 | 299 | |
paul@12 | 300 | calendar = [] |
paul@12 | 301 | cwrite = calendar.append |
paul@2 | 302 | |
paul@12 | 303 | # Get the details for each attendee. |
paul@5 | 304 | |
paul@12 | 305 | for attendee in attendees: |
paul@12 | 306 | freebusy = self.store.get_freebusy(attendee) |
paul@2 | 307 | |
paul@12 | 308 | if freebusy: |
paul@12 | 309 | record = [] |
paul@12 | 310 | rwrite = record.append |
paul@12 | 311 | |
paul@12 | 312 | rwrite(("ORGANIZER", organiser_attr, organiser)) |
paul@12 | 313 | rwrite(("ATTENDEE", attendee_map[attendee], attendee)) |
paul@12 | 314 | rwrite(("UID", {}, self.uid)) |
paul@5 | 315 | |
paul@12 | 316 | for start, end in freebusy: |
paul@12 | 317 | rwrite(("FREEBUSY", {}, [start, end])) |
paul@5 | 318 | |
paul@12 | 319 | cwrite(("VFREEBUSY", {}, record)) |
paul@5 | 320 | |
paul@12 | 321 | # Return the reply. |
paul@8 | 322 | |
paul@12 | 323 | return calendar |
paul@0 | 324 | |
paul@2 | 325 | class Journal(Handler): |
paul@6 | 326 | |
paul@6 | 327 | "A journal entry handler." |
paul@6 | 328 | |
paul@2 | 329 | def add(self): |
paul@2 | 330 | pass |
paul@2 | 331 | |
paul@2 | 332 | def cancel(self): |
paul@2 | 333 | pass |
paul@2 | 334 | |
paul@6 | 335 | def publish(self): |
paul@6 | 336 | pass |
paul@6 | 337 | |
paul@2 | 338 | class Todo(Handler): |
paul@6 | 339 | |
paul@6 | 340 | "A to-do item handler." |
paul@6 | 341 | |
paul@2 | 342 | def add(self): |
paul@2 | 343 | pass |
paul@2 | 344 | |
paul@2 | 345 | def cancel(self): |
paul@2 | 346 | pass |
paul@2 | 347 | |
paul@2 | 348 | def counter(self): |
paul@2 | 349 | pass |
paul@0 | 350 | |
paul@2 | 351 | def declinecounter(self): |
paul@2 | 352 | pass |
paul@2 | 353 | |
paul@6 | 354 | def publish(self): |
paul@6 | 355 | pass |
paul@6 | 356 | |
paul@2 | 357 | def refresh(self): |
paul@2 | 358 | pass |
paul@2 | 359 | |
paul@2 | 360 | def reply(self): |
paul@6 | 361 | |
paul@6 | 362 | "Since this handler does not send requests, it will not handle replies." |
paul@6 | 363 | |
paul@2 | 364 | pass |
paul@2 | 365 | |
paul@2 | 366 | def request(self): |
paul@2 | 367 | pass |
paul@2 | 368 | |
paul@2 | 369 | # Handler registry. |
paul@0 | 370 | |
paul@2 | 371 | handlers = [ |
paul@2 | 372 | ("VFREEBUSY", Freebusy), |
paul@2 | 373 | ("VEVENT", Event), |
paul@2 | 374 | ("VTODO", Todo), |
paul@2 | 375 | ("VJOURNAL", Journal), |
paul@2 | 376 | ] |
paul@0 | 377 | |
paul@2 | 378 | methods = { |
paul@2 | 379 | "ADD" : lambda handler: handler.add, |
paul@2 | 380 | "CANCEL" : lambda handler: handler.cancel, |
paul@2 | 381 | "COUNTER" : lambda handler: handler.counter, |
paul@2 | 382 | "DECLINECOUNTER" : lambda handler: handler.declinecounter, |
paul@2 | 383 | "PUBLISH" : lambda handler: handler.publish, |
paul@2 | 384 | "REFRESH" : lambda handler: handler.refresh, |
paul@2 | 385 | "REPLY" : lambda handler: handler.reply, |
paul@2 | 386 | "REQUEST" : lambda handler: handler.request, |
paul@2 | 387 | } |
paul@0 | 388 | |
paul@11 | 389 | |
paul@11 | 390 | def main(): |
paul@11 | 391 | |
paul@11 | 392 | # Obtain the different kinds of recipients. |
paul@9 | 393 | |
paul@11 | 394 | original_recipients = [] |
paul@11 | 395 | recipients = [] |
paul@9 | 396 | |
paul@11 | 397 | l = [] |
paul@9 | 398 | |
paul@11 | 399 | for arg in sys.argv[1:]: |
paul@11 | 400 | if arg == "-o": |
paul@11 | 401 | l = original_recipients |
paul@11 | 402 | elif arg == "-r": |
paul@11 | 403 | l = recipients |
paul@11 | 404 | elif arg == "-d": |
paul@11 | 405 | pass |
paul@11 | 406 | else: |
paul@11 | 407 | l.append(arg) |
paul@11 | 408 | |
paul@11 | 409 | process(sys.stdin, original_recipients, recipients) |
paul@9 | 410 | |
paul@11 | 411 | if __name__ == "__main__": |
paul@11 | 412 | if "-d" in sys.argv[1:]: |
paul@11 | 413 | main() |
paul@9 | 414 | else: |
paul@11 | 415 | try: |
paul@11 | 416 | main() |
paul@11 | 417 | except SystemExit, value: |
paul@11 | 418 | sys.exit(value) |
paul@11 | 419 | except Exception, exc: |
paul@11 | 420 | type, value, tb = sys.exc_info() |
paul@11 | 421 | print >>sys.stderr, "Exception %s at %d" % (exc, tb.tb_lineno) |
paul@11 | 422 | sys.exit(EX_TEMPFAIL) |
paul@11 | 423 | sys.exit(0) |
paul@0 | 424 | |
paul@0 | 425 | # vim: tabstop=4 expandtab shiftwidth=4 |