From c830e005ce3a72ec9e3b66b4d8c71aa645511c99 Mon Sep 17 00:00:00 2001 From: xxm Date: Fri, 21 Aug 2026 22:20:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(messenger):=20=E6=B7=BB=E5=8A=A0install=5F?= =?UTF-8?q?stdio=5Fhook=E7=BB=9F=E4=B8=80=E6=8B=A6=E6=88=AA=E8=84=9A?= =?UTF-8?q?=E6=9C=ACstdout=E6=8C=89=E9=80=9A=E9=81=93=E5=88=86=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/profile-scripts/messenger.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/deploy/profile-scripts/messenger.py b/deploy/profile-scripts/messenger.py index e6fa8d09..95101e89 100644 --- a/deploy/profile-scripts/messenger.py +++ b/deploy/profile-scripts/messenger.py @@ -127,6 +127,31 @@ def send_action_recommendation(title, content, source=None): return send(title=title, content=content, source=source, channel="xmpp") + +class _MessengerStdout: + def __init__(self, orig, script): + self.orig = orig + self.script = script + self.channel = get_channel(script) + self._buf = "" + def write(self, s): + if self.channel in ("xmpp", "both"): + self.orig.write(s); self.orig.flush() + else: + self._buf += s + return len(s) + def flush(self): + if self.channel in ("xmpp", "both"): + self.orig.flush() + + +def install_stdio_hook(script_name=None): + """在脚本入口调用:重定向 stdout 到 messenger 通道控制""" + if script_name is None: + script_name = os.path.basename(sys.argv[0]) if sys.argv else "mofin" + sys.stdout = _MessengerStdout(sys.stdout, script_name) + return get_channel(script_name) + if __name__ == "__main__": print("=== messenger 自测 ===") mapping = _load_script_channels()