# HG changeset patch # User Paul Boddie # Date 1509490144 -3600 # Node ID 9baa0aae5b43c4c2a6539bdc867174d116dc4f4a # Parent 14b719dc5cbf11f92ad633a1dced29743891896e Provided a convenience function for instantiating handler objects. Added a mode that produces output instead of sending messages without also producing debugging information. diff -r 14b719dc5cbf -r 9baa0aae5b43 imiptools/__init__.py --- a/imiptools/__init__.py Tue Oct 31 23:47:31 2017 +0100 +++ b/imiptools/__init__.py Tue Oct 31 23:49:04 2017 +0100 @@ -61,6 +61,7 @@ self.journal_dir = None self.preferences_dir = None self.debug = False + self.output_only = False def get_store(self): @@ -138,7 +139,7 @@ for user in users: Recipient(user, messenger, store, publisher, journal, preferences_dir, self.handlers, self.outgoing_only, - self.debug).process(msg, senders) + self.debug, self.output_only).process(msg, senders) def process_args(self, args, stream): @@ -150,6 +151,7 @@ args = parse_args(args, {"--show-config" : ("show_config", False)}) self.debug = args["debug"] + self.output_only = args["output_only"] # Obtain arguments or configured defaults. @@ -243,18 +245,19 @@ "A processor acting as a client on behalf of a recipient." def __init__(self, user, messenger, store, publisher, journal, preferences_dir, - handlers, outgoing_only, debug): + handlers, outgoing_only, debug, output_only): """ Initialise the recipient with the given 'user' identity, 'messenger', - 'store', 'publisher', 'journal', 'preferences_dir', 'handlers', - 'outgoing_only' and 'debug' status. + 'store', 'publisher', 'journal', 'preferences_dir', 'handlers', and with + the given 'outgoing_only', 'debug' and 'output_only' status. """ Client.__init__(self, user, messenger, store, publisher, journal, preferences_dir) self.handlers = handlers self.outgoing_only = outgoing_only self.debug = debug + self.output_only = output_only def process(self, msg, senders): @@ -266,15 +269,7 @@ may be constructed according to individual preferences. """ - handlers = {} - - # Instantiate handlers for the supported methods. - - for name, cls in self.handlers: - handlers[name] = cls(senders, self.user and get_address(self.user), - self.messenger, self.store, self.publisher, - self.journal, self.preferences_dir) - + handlers = get_handlers(self, self.handlers, senders) handled = False # Check for participating recipients. Non-participating recipients will @@ -350,20 +345,22 @@ if incoming == "message-only": messages = [msg] + elif incoming == "summary-wraps-message": + messages = [self.messenger.wrap_message(msg, forwarded_parts)] else: summary = self.messenger.make_summary_message(msg, forwarded_parts) if incoming == "summary-then-message": messages = [summary, msg] elif incoming == "message-then-summary": messages = [msg, summary] - elif incoming == "summary-only": + else: # incoming == "summary-only": messages = [summary] - else: # incoming == "summary-wraps-message": - messages = [self.messenger.wrap_message(msg, forwarded_parts)] for message in messages: if self.debug: print >>sys.stderr, "Forwarded parts..." + + if self.debug or self.output_only: print message elif self.messenger.local_delivery(): self.messenger.sendmail([get_address(self.user)], message.as_string()) @@ -373,7 +370,11 @@ if not handled: if self.debug: print >>sys.stderr, "Unhandled parts..." + + if self.debug: print msg + elif self.output_only: + pass elif self.messenger.local_delivery(): self.messenger.sendmail([get_address(self.user)], msg.as_string()) @@ -388,6 +389,26 @@ else: return False +# Handler construction using an existing client. + +def get_handlers(client, handler_items, senders=None): + + """ + Return instantiated handlers using the information provided by 'client' + plus the appropriate 'handler_items' and the 'senders' of a message. + """ + + handlers = {} + + # Instantiate handlers for the supported methods. + + for name, cls in handler_items: + handlers[name] = cls(senders, client.user and get_address(client.user), + client.messenger, client.store, client.publisher, + client.journal, client.preferences_dir) + + return handlers + # Standard arguments used by imip-agent programs. def parse_args(args, extra_argdefs=None): @@ -404,6 +425,7 @@ "-l" : ("lmtp", None), "-L" : ("local_smtp", False), "-o" : ("original_recipients", []), + "-O" : ("output_only", False), "-p" : ("preferences_dir", None), "-P" : ("publishing_dir", None), "-s" : ("senders", []), @@ -495,6 +517,9 @@ -d Run in debug mode, producing informative output describing the behaviour of the program, displaying responses on standard output instead of sending messages +-O Run in output-only mode, producing handled incoming messages on standard + output instead of sending messages to the recipient (responses are sent as + normal, unhandled messages are discarded) Diagnostic options: