From 00277c22cf0bcc04a850ec51426eb98ee1ba3241 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sat, 28 May 2016 19:19:22 -0700 Subject: [PATCH] allow MOTD to be displayed multiple times (one displayed message per received welcome["motd"]) There's not much value in prohibiting the server from sending multiple MOTD messages, and it would prevent us from using it to display a "your client is using an old API, please upgrade" message after having already sent a regular "please donate" MOTD message. (We could send a second welcome message with ["error"] to kill the client, but ["motd"] is the most convenient way to deliver a non-fatal warning). --- src/wormhole/test/test_wormhole.py | 7 +++++-- src/wormhole/wormhole.py | 5 +---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wormhole/test/test_wormhole.py b/src/wormhole/test/test_wormhole.py index 9bfc97d..ba54123 100644 --- a/src/wormhole/test/test_wormhole.py +++ b/src/wormhole/test/test_wormhole.py @@ -47,10 +47,13 @@ class Welcome(unittest.TestCase): [mock.call.write(u"Server (at relay_url) says:\n" " message of\n the day"), mock.call.write(u"\n")]) - # motd is only displayed once + # motd can be displayed multiple times with mock.patch("sys.stderr") as stderr2: w.handle_welcome({u"motd": u"second message"}) - self.assertEqual(stderr2.method_calls, []) + self.assertEqual(stderr2.method_calls, + [mock.call.write(u"Server (at relay_url) says:\n" + " second message"), + mock.call.write(u"\n")]) def test_current_version(self): w = wormhole._WelcomeHandler(u"relay_url", u"2.0", None) diff --git a/src/wormhole/wormhole.py b/src/wormhole/wormhole.py index d6ff875..fafb966 100644 --- a/src/wormhole/wormhole.py +++ b/src/wormhole/wormhole.py @@ -178,18 +178,15 @@ class _WelcomeHandler: def __init__(self, url, current_version, signal_error): self._ws_url = url self._version_warning_displayed = False - self._motd_displayed = False self._current_version = current_version self._signal_error = signal_error def handle_welcome(self, welcome): - if ("motd" in welcome and - not self._motd_displayed): + if "motd" in welcome: motd_lines = welcome["motd"].splitlines() motd_formatted = "\n ".join(motd_lines) print("Server (at %s) says:\n %s" % (self._ws_url, motd_formatted), file=sys.stderr) - self._motd_displayed = True # Only warn if we're running a release version (e.g. 0.0.6, not # 0.0.6-DISTANCE-gHASH). Only warn once.