1.1 --- a/imiptools/__init__.py Tue Oct 31 23:47:31 2017 +0100
1.2 +++ b/imiptools/__init__.py Tue Oct 31 23:49:04 2017 +0100
1.3 @@ -61,6 +61,7 @@
1.4 self.journal_dir = None
1.5 self.preferences_dir = None
1.6 self.debug = False
1.7 + self.output_only = False
1.8
1.9 def get_store(self):
1.10
1.11 @@ -138,7 +139,7 @@
1.12 for user in users:
1.13 Recipient(user, messenger, store, publisher, journal,
1.14 preferences_dir, self.handlers, self.outgoing_only,
1.15 - self.debug).process(msg, senders)
1.16 + self.debug, self.output_only).process(msg, senders)
1.17
1.18 def process_args(self, args, stream):
1.19
1.20 @@ -150,6 +151,7 @@
1.21 args = parse_args(args, {"--show-config" : ("show_config", False)})
1.22
1.23 self.debug = args["debug"]
1.24 + self.output_only = args["output_only"]
1.25
1.26 # Obtain arguments or configured defaults.
1.27
1.28 @@ -243,18 +245,19 @@
1.29 "A processor acting as a client on behalf of a recipient."
1.30
1.31 def __init__(self, user, messenger, store, publisher, journal, preferences_dir,
1.32 - handlers, outgoing_only, debug):
1.33 + handlers, outgoing_only, debug, output_only):
1.34
1.35 """
1.36 Initialise the recipient with the given 'user' identity, 'messenger',
1.37 - 'store', 'publisher', 'journal', 'preferences_dir', 'handlers',
1.38 - 'outgoing_only' and 'debug' status.
1.39 + 'store', 'publisher', 'journal', 'preferences_dir', 'handlers', and with
1.40 + the given 'outgoing_only', 'debug' and 'output_only' status.
1.41 """
1.42
1.43 Client.__init__(self, user, messenger, store, publisher, journal, preferences_dir)
1.44 self.handlers = handlers
1.45 self.outgoing_only = outgoing_only
1.46 self.debug = debug
1.47 + self.output_only = output_only
1.48
1.49 def process(self, msg, senders):
1.50
1.51 @@ -266,15 +269,7 @@
1.52 may be constructed according to individual preferences.
1.53 """
1.54
1.55 - handlers = {}
1.56 -
1.57 - # Instantiate handlers for the supported methods.
1.58 -
1.59 - for name, cls in self.handlers:
1.60 - handlers[name] = cls(senders, self.user and get_address(self.user),
1.61 - self.messenger, self.store, self.publisher,
1.62 - self.journal, self.preferences_dir)
1.63 -
1.64 + handlers = get_handlers(self, self.handlers, senders)
1.65 handled = False
1.66
1.67 # Check for participating recipients. Non-participating recipients will
1.68 @@ -350,20 +345,22 @@
1.69
1.70 if incoming == "message-only":
1.71 messages = [msg]
1.72 + elif incoming == "summary-wraps-message":
1.73 + messages = [self.messenger.wrap_message(msg, forwarded_parts)]
1.74 else:
1.75 summary = self.messenger.make_summary_message(msg, forwarded_parts)
1.76 if incoming == "summary-then-message":
1.77 messages = [summary, msg]
1.78 elif incoming == "message-then-summary":
1.79 messages = [msg, summary]
1.80 - elif incoming == "summary-only":
1.81 + else: # incoming == "summary-only":
1.82 messages = [summary]
1.83 - else: # incoming == "summary-wraps-message":
1.84 - messages = [self.messenger.wrap_message(msg, forwarded_parts)]
1.85
1.86 for message in messages:
1.87 if self.debug:
1.88 print >>sys.stderr, "Forwarded parts..."
1.89 +
1.90 + if self.debug or self.output_only:
1.91 print message
1.92 elif self.messenger.local_delivery():
1.93 self.messenger.sendmail([get_address(self.user)], message.as_string())
1.94 @@ -373,7 +370,11 @@
1.95 if not handled:
1.96 if self.debug:
1.97 print >>sys.stderr, "Unhandled parts..."
1.98 +
1.99 + if self.debug:
1.100 print msg
1.101 + elif self.output_only:
1.102 + pass
1.103 elif self.messenger.local_delivery():
1.104 self.messenger.sendmail([get_address(self.user)], msg.as_string())
1.105
1.106 @@ -388,6 +389,26 @@
1.107 else:
1.108 return False
1.109
1.110 +# Handler construction using an existing client.
1.111 +
1.112 +def get_handlers(client, handler_items, senders=None):
1.113 +
1.114 + """
1.115 + Return instantiated handlers using the information provided by 'client'
1.116 + plus the appropriate 'handler_items' and the 'senders' of a message.
1.117 + """
1.118 +
1.119 + handlers = {}
1.120 +
1.121 + # Instantiate handlers for the supported methods.
1.122 +
1.123 + for name, cls in handler_items:
1.124 + handlers[name] = cls(senders, client.user and get_address(client.user),
1.125 + client.messenger, client.store, client.publisher,
1.126 + client.journal, client.preferences_dir)
1.127 +
1.128 + return handlers
1.129 +
1.130 # Standard arguments used by imip-agent programs.
1.131
1.132 def parse_args(args, extra_argdefs=None):
1.133 @@ -404,6 +425,7 @@
1.134 "-l" : ("lmtp", None),
1.135 "-L" : ("local_smtp", False),
1.136 "-o" : ("original_recipients", []),
1.137 + "-O" : ("output_only", False),
1.138 "-p" : ("preferences_dir", None),
1.139 "-P" : ("publishing_dir", None),
1.140 "-s" : ("senders", []),
1.141 @@ -495,6 +517,9 @@
1.142 -d Run in debug mode, producing informative output describing the behaviour
1.143 of the program, displaying responses on standard output instead of sending
1.144 messages
1.145 +-O Run in output-only mode, producing handled incoming messages on standard
1.146 + output instead of sending messages to the recipient (responses are sent as
1.147 + normal, unhandled messages are discarded)
1.148
1.149 Diagnostic options:
1.150