From c6be14bd0c4533ffa002332312ad6ba7be8664dd Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Wed, 17 Sep 2025 11:37:51 +0200 Subject: [PATCH] [TASK] Init --- README.md | 26 ++++++++++++++++++++++++++ app.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 README.md create mode 100644 app.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..ee56446 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# remotenotify + +A small script which forwards dbus notifications from one bus to an other + +# why? + +I needed a way to forward messages from a chat application running on an other machine to my main machine. + +# how? + +Based on snippets from: + +- https://stackoverflow.com/a/75122112 +- https://pychao.com/2021/03/01/sending-desktop-notification-in-linux-with-python-with-d-bus-directly/ + +# config + +In order to work the local bus must be available on the remote machine. Only do this if you trust the remote machine! In my case it is only a second workstation used exclusively by me. + +``` +ssh -R /run/user/1000/bus-remote:/run/user/1000/bus $REMOTEMACHINE python app.py +``` + +# licence + +I don't think I am allowed to give this script a licence as it is basically snippets from the two pages copied together. If you want to use the code refer to the licences of the original posts. diff --git a/app.py b/app.py new file mode 100644 index 0000000..b90da11 --- /dev/null +++ b/app.py @@ -0,0 +1,52 @@ +#import dbus + +#bus_local = dbus.bus.BusConnection('unix:path=/run/user/1000/bus') +#bus_remote = dbus.bus.BusConnection('unix:path=/run/user/1000/bus-remote') + +# https://stackoverflow.com/a/75122112 +# https://pychao.com/2021/03/01/sending-desktop-notification-in-linux-with-python-with-d-bus-directly/ + +import dbus +from gi.repository import GLib +from dbus.mainloop.glib import DBusGMainLoop +from pprint import pprint + +def msg_cb(bus, msg): + if msg.get_interface() == 'org.freedesktop.Notifications' and msg.get_member() == 'Notify': + args = msg.get_args_list() + print("Notification from '%s'" % args[0]) + print("Summary: %s" % args[3]) + print("Body: %s" % args[4]) + + bus_remote = dbus.bus.BusConnection('unix:path=/run/user/1000/bus-remote') + + item = "org.freedesktop.Notifications" + + notfy_intf = dbus.Interface( + bus_remote.get_object(item, "/"+item.replace(".", "/")), item) + + notfy_intf.Notify( + args[0], 0, "", args[3], args[4], + [], {"urgency": 1}, -1) + + +def main(): + DBusGMainLoop(set_as_default=True) + bus = dbus.SessionBus() + + + obj_dbus = bus.get_object('org.freedesktop.DBus', + '/org/freedesktop/DBus') + obj_dbus.BecomeMonitor(["interface='org.freedesktop.Notifications'"], + dbus.UInt32(0), + interface='org.freedesktop.Notifications') + + bus.add_message_filter(msg_cb) + + print('start') + + mainloop = GLib.MainLoop() + mainloop.run() + +if __name__ == '__main__': + main()