|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # ex:sw=4 ts=4:ai: |
3 | 3 | # |
4 | | -# Copyright (c) 2012 by Krister Svanlund <[email protected]> |
| 4 | +# SPDX-FileCopyrightText: 2012 Krister Svanlund <[email protected]> |
| 5 | +# |
| 6 | +# SPDX-License-Identifier: GPL3 |
| 7 | +# |
5 | 8 | # based on tcl version: |
6 | 9 | # Remote Notification Script v1.1 |
7 | 10 | |
|
25 | 28 | # |
26 | 29 | # On the "client" (where the notifications will end up), host is |
27 | 30 | # the remote host where weechat is running: |
28 | | -# python2 location/of/pyrnotify.py 4321 & ssh -R 4321:localhost:4321 username@host |
| 31 | +# python2 location/of/pyrnotify.py 4321 & ssh -R 4321:localhost:4321 username@host |
29 | 32 | # You can have a second argument to specified the time to display the notification |
30 | 33 | # python2 location/of/pyrnotify.py 4321 2000 & ssh -R 4321:localhost:4321 username@host |
31 | 34 | # Important to remember is that you should probably setup the |
32 | 35 | # connection with public key encryption and use something like |
33 | 36 | # autossh to do this in the background. |
34 | 37 | # |
35 | 38 | # In weechat: |
36 | | -# /python load pyrnotify.py |
37 | | -# and set the port |
38 | | -# /set plugins.var.python.pyrnotify.port 4321 |
| 39 | +# /python load pyrnotify.py |
| 40 | +# and set the port |
| 41 | +# /set plugins.var.python.pyrnotify.port 4321 |
39 | 42 | # |
40 | 43 | # It is also possible to set which host pyrnotify shall connect to, |
41 | 44 | # this is not recommended. Using a ssh port-forward is much safer |
42 | 45 | # and doesn't require any ports but ssh to be open. |
43 | 46 |
|
44 | 47 | # ChangeLog: |
| 48 | +# 2025-12-06: Modernize code, avoid escaping using regex by using json |
| 49 | +# instead of shell-like serialization. |
45 | 50 | # |
46 | 51 | # 2018-08-20: Make it work with python3 |
47 | 52 | # use of sendall instead of send |
|
50 | 55 | # 2012-06-19: Added simple escaping to the title and body strings for |
51 | 56 | # the script to handle trailing backslashes. |
52 | 57 |
|
53 | | -from __future__ import print_function |
54 | 58 |
|
55 | 59 | try: |
56 | 60 | import weechat as w |
| 61 | + |
57 | 62 | in_weechat = True |
58 | 63 | except ImportError as e: |
59 | 64 | in_weechat = False |
60 | 65 |
|
61 | | -import os, sys, re |
| 66 | +import json |
| 67 | +import os |
| 68 | +import re |
62 | 69 | import socket |
63 | 70 | import subprocess |
64 | | -import shlex |
| 71 | +import sys |
65 | 72 |
|
66 | | -SCRIPT_NAME = "pyrnotify" |
67 | | -SCRIPT_AUTHOR = "Krister Svanlund <[email protected]>" |
68 | | -SCRIPT_VERSION = "1.1" |
| 73 | +SCRIPT_NAME = "pyrnotify" |
| 74 | +SCRIPT_AUTHOR = "Krister Svanlund <[email protected]>" |
| 75 | +SCRIPT_VERSION = "2.0" |
69 | 76 | SCRIPT_LICENSE = "GPL3" |
70 | | -SCRIPT_DESC = "Send remote notifications over SSH" |
| 77 | +SCRIPT_DESC = "Send remote notifications over SSH" |
71 | 78 |
|
72 | | -def escape(s): |
73 | | - return re.sub(r'([\\"\'])', r'\\\1', s) |
74 | 79 |
|
75 | 80 | def run_notify(icon, nick, chan, message): |
76 | | - host = w.config_get_plugin('host') |
| 81 | + host = w.config_get_plugin("host") |
77 | 82 | try: |
78 | 83 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
79 | | - s.connect((host, int(w.config_get_plugin('port')))) |
80 | | - msg = "normal {0} \"{1} to {2}\" \"{3}\"".format(icon, nick, escape(chan), escape(message)) |
81 | | - s.sendall(msg.encode('utf-8')) |
| 84 | + s.connect((host, int(w.config_get_plugin("port")))) |
| 85 | + msg = { |
| 86 | + "urgency": "normal", |
| 87 | + "icon": icon, |
| 88 | + "nick": nick, |
| 89 | + "chan": chan, |
| 90 | + "message": message, |
| 91 | + } |
| 92 | + s.sendall(json.dumps(msg, ensure_ascii=False).encode("UTF-8")) |
82 | 93 | s.close() |
83 | 94 | except Exception as e: |
84 | 95 | w.prnt("", "Could not send notification: {0}".format(e)) |
85 | 96 |
|
| 97 | + |
86 | 98 | def on_msg(*a): |
87 | 99 | if len(a) == 8: |
88 | 100 | data, buffer, timestamp, tags, displayed, highlight, sender, message = a |
89 | 101 | if data == "private" or int(highlight): |
90 | | - if data == "private" and w.config_get_plugin('pm-icon'): |
91 | | - icon = w.config_get_plugin('pm-icon') |
| 102 | + if data == "private" and w.config_get_plugin("pm-icon"): |
| 103 | + icon = w.config_get_plugin("pm-icon") |
92 | 104 | else: |
93 | | - icon = w.config_get_plugin('icon') |
94 | | - buffer = "me" if data == "private" else w.buffer_get_string(buffer, "short_name") |
| 105 | + icon = w.config_get_plugin("icon") |
| 106 | + buffer = ( |
| 107 | + "me" if data == "private" else w.buffer_get_string(buffer, "short_name") |
| 108 | + ) |
95 | 109 | run_notify(icon, sender, buffer, message) |
96 | | - #w.prnt("", str(a)) |
97 | 110 | return w.WEECHAT_RC_OK |
98 | 111 |
|
| 112 | + |
99 | 113 | def weechat_script(): |
100 | | - settings = {'host' : "localhost", |
101 | | - 'port' : "4321", |
102 | | - 'icon' : "utilities-terminal", |
103 | | - 'pm-icon' : "emblem-favorite"} |
104 | | - if w.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""): |
105 | | - for (kw, v) in settings.items(): |
| 114 | + settings = { |
| 115 | + "host": "localhost", |
| 116 | + "port": "4321", |
| 117 | + "icon": "utilities-terminal", |
| 118 | + "pm-icon": "emblem-favorite", |
| 119 | + } |
| 120 | + if w.register( |
| 121 | + SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", "" |
| 122 | + ): |
| 123 | + for kw, v in settings.items(): |
106 | 124 | if not w.config_get_plugin(kw): |
107 | 125 | w.config_set_plugin(kw, v) |
108 | 126 | w.hook_print("", "notify_message", "", 1, "on_msg", "") |
109 | 127 | w.hook_print("", "notify_private", "", 1, "on_msg", "private") |
110 | | - w.hook_print("", "notify_highlight", "", 1, "on_msg", "") # Not sure if this is needed |
| 128 | + w.hook_print( |
| 129 | + "", "notify_highlight", "", 1, "on_msg", "" |
| 130 | + ) # Not sure if this is needed |
111 | 131 |
|
112 | 132 |
|
113 | 133 | ###################################### |
114 | 134 | ## This is where the client starts, except for the global if-check nothing below this line is |
115 | 135 | ## supposed to be executed in weechat, instead it runs when the script is executed from |
116 | 136 | ## commandline. |
117 | 137 |
|
| 138 | + |
118 | 139 | def accept_connections(s, timeout=None): |
119 | 140 | conn, addr = s.accept() |
120 | 141 | try: |
121 | | - data = "" |
122 | | - d = conn.recv(1024) |
123 | | - while d: |
124 | | - data += d.decode('utf-8') |
125 | | - d = conn.recv(1024) |
| 142 | + data = b"" |
| 143 | + while d := conn.recv(1024): |
| 144 | + data += d |
126 | 145 | finally: |
127 | 146 | conn.close() |
128 | 147 | if data: |
129 | 148 | try: |
130 | | - urgency, icon, title, body = shlex.split(data) |
| 149 | + notif = json.loads(data.decode("UTF-8")) |
| 150 | + argv = [ |
| 151 | + "notify-send", |
| 152 | + "-u", |
| 153 | + notif["urgency"], |
| 154 | + "-c", |
| 155 | + "IRC", |
| 156 | + "-i", |
| 157 | + notif["icon"], |
| 158 | + "--", |
| 159 | + notif["chan"], |
| 160 | + notif["message"], |
| 161 | + ] |
131 | 162 | if timeout: |
132 | | - subprocess.call(["notify-send", "-t", timeout, "-u", urgency, "-c", "IRC", "-i", icon, escape(title), escape(body)]) |
133 | | - else: |
134 | | - subprocess.call(["notify-send", "-u", urgency, "-c", "IRC", "-i", icon, escape(title), escape(body)]) |
135 | | - |
136 | | - except ValueError as e: |
137 | | - print(e) |
138 | | - except OSError as e: |
| 163 | + argv.extend(["-t", timeout]) |
| 164 | + subprocess.run(argv) |
| 165 | + except (ValueError, OSError) as e: |
139 | 166 | print(e) |
140 | | - accept_connections(s, timeout) |
| 167 | + |
141 | 168 |
|
142 | 169 | def weechat_client(argv): |
| 170 | + port = int(argv[1]) if len(sys.argv) > 1 else 4321 |
143 | 171 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
144 | 172 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
145 | | - s.bind(("localhost", int(argv[1] if len(sys.argv) > 1 else 4321))) |
| 173 | + s.bind(("localhost", port)) |
146 | 174 | s.listen(5) |
147 | 175 | try: |
148 | | - accept_connections(s, argv[2] if len(sys.argv) > 2 else None) |
149 | | - except KeyboardInterrupt as e: |
150 | | - print("Keyboard interrupt") |
151 | | - print(e) |
| 176 | + while True: |
| 177 | + accept_connections(s, argv[2] if len(sys.argv) > 2 else None) |
| 178 | + except KeyboardInterrupt: |
| 179 | + return |
152 | 180 | finally: |
153 | 181 | s.close() |
154 | 182 |
|
155 | | -if __name__ == '__main__': |
| 183 | + |
| 184 | +if __name__ == "__main__": |
156 | 185 | if in_weechat: |
157 | 186 | weechat_script() |
158 | 187 | else: |
|
0 commit comments