1 #!/usr/bin/env python 2 3 from email import message_from_file 4 from vCalendar import parse 5 import sys 6 7 try: 8 from cStringIO import StringIO 9 except ImportError: 10 from StringIO import StringIO 11 12 # Postfix exit codes. 13 14 EX_USAGE = 64 15 EX_DATAERR = 65 16 EX_NOINPUT = 66 17 EX_NOUSER = 67 18 EX_NOHOST = 68 19 EX_UNAVAILABLE = 69 20 EX_SOFTWARE = 70 21 EX_OSERR = 71 22 EX_OSFILE = 72 23 EX_CANTCREAT = 73 24 EX_IOERR = 74 25 EX_TEMPFAIL = 75 26 EX_PROTOCOL = 76 27 EX_NOPERM = 77 28 EX_CONFIG = 78 29 30 # Permitted iTIP content types. 31 32 itip_content_types = [ 33 "text/calendar", # from RFC 6047 34 "text/x-vcalendar", "application/ics", # other possibilities 35 ] 36 37 def process(f): 38 msg = message_from_file(f) 39 40 # Handle messages with iTIP parts. 41 42 for part in msg.walk(): 43 if part.get_content_type() in itip_content_types and \ 44 part.get_param("method"): 45 46 handle_itip_part(part) 47 48 def get_itip_elements(elements): 49 d = {} 50 for name, attr, value in elements: 51 if isinstance(value, list): 52 d[name] = attr, get_itip_elements(value) 53 else: 54 d[name] = attr, value 55 return d 56 57 def get_value(d, name): 58 if d.has_key(name): 59 attr, value = d[name] 60 return value 61 else: 62 return None 63 64 def handle_itip_part(part): 65 method = part.get_param("method") 66 67 f = StringIO(part.get_payload(decode=True)) 68 doctype, attrs, elements = parse(f, encoding=part.get_content_charset()) 69 70 if doctype == "VCALENDAR": 71 itip = get_itip_elements(elements) 72 73 if get_value(itip, "METHOD") == method: 74 for name, handler in [ 75 ("VFREEBUSY", handle_itip_freebusy), 76 ("VEVENT", handle_itip_event), 77 ("VTODO", handle_itip_todo), 78 ("VJOURNAL", handle_itip_journal), 79 ]: 80 81 obj = get_value(itip, name) 82 if obj: 83 uid = get_value(obj, "UID") 84 if uid: 85 handler(obj, method) 86 87 def handle_itip_freebusy(freebusy, method): 88 print >>open("/tmp/imip.txt", "a"), freebusy 89 90 def handle_itip_event(event, method): 91 print >>open("/tmp/imip.txt", "a"), event 92 93 def handle_itip_todo(todo, method): 94 print >>open("/tmp/imip.txt", "a"), todo 95 96 def handle_itip_journal(journal, method): 97 print >>open("/tmp/imip.txt", "a"), journal 98 99 if __name__ == "__main__": 100 process(sys.stdin) 101 102 # vim: tabstop=4 expandtab shiftwidth=4