2024-02-04 00:53:16 +01:00
|
|
|
//go:build freebsd || linux
|
2023-12-28 12:58:02 +01:00
|
|
|
|
|
|
|
package notify
|
|
|
|
|
|
|
|
import (
|
2024-01-19 05:14:25 +01:00
|
|
|
"time"
|
|
|
|
|
2023-12-28 12:58:02 +01:00
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
)
|
|
|
|
|
|
|
|
var closeListenerMap = make(map[uint32]func())
|
|
|
|
|
2024-04-29 18:13:50 +02:00
|
|
|
func notifyDBus(title string, body string, actionName string, timeout time.Duration, onclose func()) error {
|
2023-12-28 12:58:02 +01:00
|
|
|
bus, err := dbus.SessionBus()
|
|
|
|
if err != nil {
|
2024-03-03 01:38:11 +01:00
|
|
|
log.Error("could not get a dbus session: %s", err.Error())
|
2024-04-29 18:13:50 +02:00
|
|
|
return err
|
2023-12-28 12:58:02 +01:00
|
|
|
}
|
|
|
|
obj := bus.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
|
|
|
|
actions := []string{}
|
|
|
|
if actionName != "" {
|
|
|
|
actions = append(actions, actionName)
|
|
|
|
}
|
2024-01-19 05:14:25 +01:00
|
|
|
|
|
|
|
notificationID++
|
|
|
|
|
|
|
|
call := obj.Call("org.freedesktop.Notifications.Notify", 0, "goldwarden", uint32(notificationID), "", title, body, actions, map[string]dbus.Variant{}, int32(60000))
|
2023-12-28 12:58:02 +01:00
|
|
|
if call.Err != nil {
|
2024-03-03 01:38:11 +01:00
|
|
|
log.Error("could not call dbus object: %s", call.Err.Error())
|
2024-04-29 18:13:50 +02:00
|
|
|
return err
|
2023-12-28 12:58:02 +01:00
|
|
|
}
|
|
|
|
if len(call.Body) < 1 {
|
2024-04-29 18:13:50 +02:00
|
|
|
return err
|
2023-12-28 12:58:02 +01:00
|
|
|
}
|
|
|
|
id := call.Body[0].(uint32)
|
|
|
|
closeListenerMap[id] = onclose
|
|
|
|
|
2024-01-19 05:14:25 +01:00
|
|
|
if timeout == 0 {
|
2024-04-29 18:13:50 +02:00
|
|
|
return err
|
2024-01-19 05:14:25 +01:00
|
|
|
} else {
|
|
|
|
go func(id uint32) {
|
|
|
|
time.Sleep(timeout)
|
|
|
|
call := obj.Call("org.freedesktop.Notifications.CloseNotification", 0, uint32(id))
|
|
|
|
if call.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}(id)
|
|
|
|
}
|
2024-04-29 18:13:50 +02:00
|
|
|
|
|
|
|
return nil
|
2023-12-28 12:58:02 +01:00
|
|
|
}
|
|
|
|
|
2024-04-29 18:13:50 +02:00
|
|
|
func listenForNotificationsDBus() error {
|
2023-12-28 12:58:02 +01:00
|
|
|
bus, err := dbus.SessionBus()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = bus.AddMatchSignal(dbus.WithMatchInterface("org.freedesktop.Notifications"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signals := make(chan *dbus.Signal, 10)
|
|
|
|
bus.Signal(signals)
|
|
|
|
for {
|
2024-03-03 01:38:11 +01:00
|
|
|
message := <-signals
|
|
|
|
if message.Name == "org.freedesktop.Notifications.NotificationClosed" {
|
|
|
|
if len(message.Body) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
id, ok := message.Body[0].(uint32)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if id == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if closeListener, ok := closeListenerMap[id]; ok {
|
|
|
|
delete(closeListenerMap, id)
|
|
|
|
closeListener()
|
2023-12-28 12:58:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|