1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
package notify
import (
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/godbus/dbus/v5"
)
// Name is the well-known bus name the daemon owns.
const Name = "org.freedesktop.Notifications"
const (
objPath = "/org/freedesktop/Notifications"
iface = "org.freedesktop.Notifications"
ctrlPath = "/xyz/danix/Notifyd"
ctrlIface = "xyz.danix.Notifyd"
)
// Service is the org.freedesktop.Notifications object.
type Service struct {
conn *dbus.Conn
store *Store
dir string
mu sync.Mutex
timers map[uint32]*time.Timer
}
func NewService(conn *dbus.Conn, dir string) *Service {
s := &Service{conn: conn, dir: dir, timers: map[uint32]*time.Timer{}}
s.store = NewStore(s.emitClosed, func(live, history []Popup) {
if err := Publish(s.dir, live, history); err != nil {
log.Printf("notifyd: publish: %v", err)
}
}, s.removeImage)
return s
}
// removeImage unlinks only what the daemon wrote, so a client's own image-path
// is never touched. A cleaned path must sit directly inside the image
// directory, which also refuses a `..` traversal to a sibling daemon file.
func (s *Service) removeImage(path string) {
if path == "" {
return
}
if filepath.Dir(filepath.Clean(path)) != ImagesDir(s.dir) {
return
}
os.Remove(path)
}
// Start exports the interfaces and empties the state. Nothing from a previous
// run is resurrected.
func (s *Service) Start() error {
if err := s.conn.Export(s, dbus.ObjectPath(objPath), iface); err != nil {
return err
}
if err := s.conn.Export(&control{s}, dbus.ObjectPath(ctrlPath), ctrlIface); err != nil {
return err
}
if err := os.RemoveAll(ImagesDir(s.dir)); err != nil {
log.Printf("notifyd: clear images: %v", err)
}
s.store.Reset()
return nil
}
func (s *Service) emitClosed(id, reason uint32) {
s.conn.Emit(dbus.ObjectPath(objPath), iface+".NotificationClosed", id, reason)
}
// GetCapabilities tells clients what the daemon understands. actions and
// body-markup are load-bearing: mail-notify sends actions and escapes its body
// because the running dunst advertises markup. body-images says the daemon
// decodes image-data and image-path, not just a static app icon.
func (s *Service) GetCapabilities() ([]string, *dbus.Error) {
return []string{"actions", "body-markup", "body-images", "icon-static", "persistence"}, nil
}
func (s *Service) GetServerInformation() (string, string, string, string, *dbus.Error) {
return "danix", "danix", "0.1", "1.2", nil
}
// Notify is the spec's entry point. The id is returned to the client; a
// replace reuses the id of what it replaced.
func (s *Service) Notify(appName string, replacesID uint32, appIcon, summary, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) {
u := UrgencyFromHints(hints)
tag := StackTagFromHints(hints)
now := time.Now().UnixMilli()
ms := EffectiveTimeoutMS(expireTimeout, u)
// Spec priority is image-data > image-path > the deprecated icon_data. A
// raw blob cannot be written until Add has assigned the id the filename is
// derived from, so any raw tier is held in pendingImage.
var pendingImage []byte
n := &Popup{
App: appName,
Summary: summary,
Body: body,
Urgency: u,
Icon: ResolveIcon(appIcon),
Actions: ParseActions(actions),
Created: now,
}
var raw *RawImage
if r, ok := ImageDataFromHints(hints); ok {
raw = r
} else if path, ok := ImagePathFromHints(hints); ok {
n.Image = ResolveIcon(path)
} else if r, ok := IconDataFromHints(hints); ok {
raw = r
}
if raw != nil {
if data, err := raw.PNG(); err == nil {
pendingImage = data
} else {
log.Printf("notifyd: encode image: %v", err)
}
}
if ms > 0 {
n.Expires = now + ms
}
id, _ := s.store.Add(n, tag, replacesID)
if pendingImage != nil {
if path, err := WriteImage(s.dir, id, pendingImage); err == nil {
s.store.SetImage(id, path)
} else {
log.Printf("notifyd: write image: %v", err)
}
}
s.arm(id, ms)
return id, nil
}
// CloseNotification is the spec's programmatic close, reason 3.
func (s *Service) CloseNotification(id uint32) *dbus.Error {
s.stopTimer(id)
s.store.Dismiss(id, 3)
return nil
}
func (s *Service) arm(id uint32, ms int64) {
s.mu.Lock()
defer s.mu.Unlock()
s.stopTimerLocked(id)
if ms <= 0 {
return
}
var t *time.Timer
t = time.AfterFunc(time.Duration(ms)*time.Millisecond, func() {
s.mu.Lock()
current := s.timers[id] == t
if current {
delete(s.timers, id)
}
s.mu.Unlock()
// Only the current timer may expire the id: a timer left over from a
// replaced notification must not close its successor.
if current {
s.store.Expire(id)
}
})
s.timers[id] = t
}
func (s *Service) stopTimer(id uint32) {
s.mu.Lock()
defer s.mu.Unlock()
s.stopTimerLocked(id)
}
func (s *Service) stopTimerLocked(id uint32) {
if t, ok := s.timers[id]; ok {
t.Stop()
delete(s.timers, id)
}
}
|