# HG changeset patch # User Paul Boddie # Date 1411416447 -7200 # Node ID cd9921fc12537e71335a72e07939f3e3cb1b73bd # Parent aa8c34d84ad2fc58eab8b2fbf2d9fa0c971a87be Added an exception handler and argument parsing for different recipient types. Added filtering of attendees by recipients. diff -r aa8c34d84ad2 -r cd9921fc1253 imip_agent.py --- a/imip_agent.py Mon Sep 22 18:28:56 2014 +0200 +++ b/imip_agent.py Mon Sep 22 22:07:27 2014 +0200 @@ -46,15 +46,16 @@ # Processing of incoming messages. -def process(f, args): +def process(f, original_recipients, recipients): - "Process content from the stream 'f' accompanied by the given 'args'." - - recipient = args[0] + """ + Process content from the stream 'f' accompanied by the given + 'original_recipients' and 'recipients'. + """ msg = message_from_file(f) print >>open("/tmp/imip.txt", "a"), "----" - print >>open("/tmp/imip.txt", "a"), args + print >>open("/tmp/imip.txt", "a"), original_recipients, recipients print >>open("/tmp/imip.txt", "a"), "----" print >>open("/tmp/imip.txt", "a"), msg.as_string() print >>open("/tmp/imip.txt", "a") @@ -65,7 +66,7 @@ if part.get_content_type() in itip_content_types and \ part.get_param("method"): - handle_itip_part(part, recipient) + handle_itip_part(part, original_recipients) def get_itip_elements(elements): d = {} @@ -98,7 +99,7 @@ else: return None -def handle_itip_part(part, recipient): +def handle_itip_part(part, recipients): method = part.get_param("method") # Decode the data and parse it. @@ -126,7 +127,7 @@ # Dispatch to a handler and obtain any response. - handler = cls(details) + handler = cls(details, recipients) part = methods[method](handler)() if part: @@ -136,8 +137,16 @@ "General handler support." - def __init__(self, details): + def __init__(self, details, recipients): + + """ + Initialise the handler with the 'details' of a calendar object and the + 'recipients' of the object. + """ + self.details = details + self.recipients = set(recipients) + self.uid = get_value(details, "UID") self.sequence = get_value(details, "SEQUENCE") self.store = imip_store.FileStore() @@ -148,6 +157,9 @@ def get_value(self, name, single=True): return get_value(self.details, name, single) + def filter_by_recipients(self, attr_values): + return [a for attr, value in attendees if value in self.recipients] + class Event(Handler): "An event handler." @@ -202,6 +214,10 @@ attendees = self.get_attr_value("ATTENDEE", False) organiser = self.get_attr_value("ORGANIZER") + # Only provide details for recipients who are also attendees. + + attendees = self.filter_by_recipients(attendees) + if not attendees and not organiser: return @@ -306,6 +322,26 @@ } if __name__ == "__main__": - process(sys.stdin, sys.argv[1:]) + try: + # Obtain the different kinds of recipients. + + original_recipients = [] + recipients = [] + + l = [] + + for arg in sys.argv[1:]: + if arg == "-o": + l = original_recipients + elif arg == "-r": + l = recipients + else: + l.append(arg) + + process(sys.stdin, original_recipients, recipients) + except: + sys.exit(EX_SOFTWARE) + else: + sys.exit(0) # vim: tabstop=4 expandtab shiftwidth=4