-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifs.go
127 lines (111 loc) · 3.44 KB
/
notifs.go
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
/*
* itd uses bluetooth low energy to communicate with InfiniTime devices
* Copyright (C) 2021 Arsen Musayelyan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"fmt"
"github.com/godbus/dbus/v5"
"go.elara.ws/itd/infinitime"
"go.elara.ws/itd/internal/utils"
"go.elara.ws/itd/translit"
"go.elara.ws/logger/log"
)
func initNotifRelay(ctx context.Context, wg WaitGroup, dev *infinitime.Device) error {
// Connect to dbus session bus
bus, err := utils.NewSessionBusConn(ctx)
if err != nil {
return err
}
// Define rules to listen for
rules := []string{
"type='method_call',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
}
var flag uint = 0
// Becode monitor for notifications
call := bus.BusObject().CallWithContext(
ctx, "org.freedesktop.DBus.Monitoring.BecomeMonitor", 0, rules, flag,
)
if call.Err != nil {
return call.Err
}
// Create channel to store notifications
notifCh := make(chan *dbus.Message, 10)
// Send events to channel
bus.Eavesdrop(notifCh)
wg.Add(1)
go func() {
defer wg.Done("notifRelay")
// For every event sent to channel
for {
select {
case v := <-notifCh:
// If firmware is updating, skip
if firmwareUpdating {
continue
}
// If body does not contain 5 elements, skip
if len(v.Body) < 5 {
continue
}
// Get requred fields
sender, summary, body := v.Body[0].(string), v.Body[3].(string), v.Body[4].(string)
// If fields are ignored in config, skip
if ignored(sender, summary, body) {
continue
}
maps := k.Strings("notifs.translit.use")
translit.Transliterators["custom"] = translit.Map(k.Strings("notifs.translit.custom"))
sender = translit.Transliterate(sender, maps...)
summary = translit.Transliterate(summary, maps...)
body = translit.Transliterate(body, maps...)
var msg string
// If summary does not exist, set message to body.
// If it does, set message to summary, two newlines, and then body
if summary == "" {
msg = body
} else {
msg = fmt.Sprintf("%s\n\n%s", summary, body)
}
dev.Notify(sender, msg)
case <-ctx.Done():
bus.Close()
return
}
}
}()
log.Info("Relaying notifications to InfiniTime").Send()
return nil
}
// ignored checks whether any fields were ignored in the config
func ignored(sender, summary, body string) bool {
ignoreSender := k.Strings("notifs.ignore.sender")
ignoreSummary := k.Strings("notifs.ignore.summary")
ignoreBody := k.Strings("notifs.ignore.body")
return strSlcContains(ignoreSender, sender) ||
strSlcContains(ignoreSummary, summary) ||
strSlcContains(ignoreBody, body)
}
// strSliceContains checks whether a string slice contains a string
func strSlcContains(ss []string, s string) bool {
for _, str := range ss {
if str == s {
return true
}
}
return false
}