#!/usr/bin/env nix-shell #!nix-shell -p python3Packages.dbus-python python3Packages.pygobject3 #!nix-shell -i xonsh # insert commands to be run here import dbus import dbus.lowlevel from gi.repository import GLib as gobject from dbus.mainloop.glib import DBusGMainLoop import sys $NOTIFY_KEY $NOTIFY_PK def parse_urgency(b): if b == 0: return "low" elif b == 1: return "normal" elif b == 2: return "critical" return "low" def msg_cb(bus, msg): # The filter is handed *every* message on this connection (e.g. the bus's # own NameAcquired signal), not just the eavesdropped Notify calls. Ignore # anything that isn't a real Notify method call, and don't claim it as # HANDLED so we never interfere with other traffic. if not (msg.get_interface() == "org.freedesktop.Notifications" and msg.get_member() == "Notify" and isinstance(msg, dbus.lowlevel.MethodCallMessage)): return dbus.lowlevel.HANDLER_RESULT_NOT_YET_HANDLED try: args = msg.get_args_list() source = args[0] title = args[3] body = args[4] data = args[6] urgency = parse_urgency(data["urgency"]) if urgency == "critical": content = "**{}**\n{}".format(title, body).strip() nostril --envelope --sec $NOTIFY_KEY --content @(content) -p $NOTIFY_PK | nostcat ws://relay.jb55.com wss://notify.damus.io else: content = "{}\n{}".format(title, body).strip() nostril --envelope --sec $NOTIFY_KEY -p $NOTIFY_PK --content @(content) | nostcat wss://notify.damus.io except Exception as e: print("monitor-notifications: error forwarding notification: {}".format(e), file=sys.stderr) # We only eavesdrop on Notify -- we are not its real destination (dunst is). # Returning HANDLED tells libdbus this method call is consumed so it does NOT # auto-reply to the sender with org.freedesktop.DBus.Error.UnknownMethod, # which is what was making notify-send/dunst report failure. return dbus.lowlevel.HANDLER_RESULT_HANDLED if __name__ == '__main__': DBusGMainLoop(set_as_default=True) bus = dbus.SessionBus() string = "interface='org.freedesktop.Notifications',member='Notify',eavesdrop='true'" bus.add_match_string(string) bus.add_message_filter(msg_cb) mainloop = gobject.MainLoop () mainloop.run ()