2021-08-21 08:19:49 +00:00
|
|
|
/*
|
|
|
|
* 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 (
|
2022-05-11 20:24:12 +00:00
|
|
|
"context"
|
2021-08-21 08:19:49 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"go.arsenm.dev/infinitime"
|
2021-10-04 08:05:01 +00:00
|
|
|
"go.arsenm.dev/itd/translit"
|
2021-08-21 08:19:49 +00:00
|
|
|
)
|
|
|
|
|
2022-05-11 20:24:12 +00:00
|
|
|
func initNotifRelay(ctx context.Context, dev *infinitime.Device) error {
|
2021-08-21 08:19:49 +00:00
|
|
|
// Connect to dbus session bus
|
2022-05-11 20:24:12 +00:00
|
|
|
bus, err := newSessionBusConn(ctx)
|
2021-08-21 08:19:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define rules to listen for
|
|
|
|
var rules = []string{
|
|
|
|
"type='method_call',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
|
|
|
|
}
|
|
|
|
var flag uint = 0
|
|
|
|
// Becode monitor for notifications
|
2022-05-11 20:24:12 +00:00
|
|
|
call := bus.BusObject().CallWithContext(
|
|
|
|
ctx, "org.freedesktop.DBus.Monitoring.BecomeMonitor", 0, rules, flag,
|
|
|
|
)
|
2021-08-21 08:19:49 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// For every event sent to channel
|
|
|
|
for v := range 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
|
|
|
|
}
|
|
|
|
|
2022-02-21 19:20:02 +00:00
|
|
|
maps := k.Strings("notifs.translit.use")
|
|
|
|
translit.Transliterators["custom"] = translit.Map(k.Strings("notifs.translit.custom"))
|
2021-10-05 00:45:26 +00:00
|
|
|
sender = translit.Transliterate(sender, maps...)
|
|
|
|
summary = translit.Transliterate(summary, maps...)
|
|
|
|
body = translit.Transliterate(body, maps...)
|
2021-10-04 08:05:01 +00:00
|
|
|
|
2021-08-21 08:19:49 +00:00
|
|
|
var msg string
|
2022-09-23 04:04:50 +00:00
|
|
|
msg = body
|
|
|
|
// If sender doesn't exist, use summary
|
|
|
|
if sender == "" {
|
|
|
|
sender = summary
|
|
|
|
// if all three exist, set message to summary and body
|
|
|
|
} else if summary != "" {
|
|
|
|
msg = fmt.Sprintf("%s\n%s", summary, body)
|
2021-08-21 08:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev.Notify(sender, msg)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Info().Msg("Relaying notifications to InfiniTime")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignored checks whether any fields were ignored in the config
|
|
|
|
func ignored(sender, summary, body string) bool {
|
2022-02-21 19:20:02 +00:00
|
|
|
ignoreSender := k.Strings("notifs.ignore.sender")
|
|
|
|
ignoreSummary := k.Strings("notifs.ignore.summary")
|
|
|
|
ignoreBody := k.Strings("notifs.ignore.body")
|
2021-08-21 08:19:49 +00:00
|
|
|
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
|
|
|
|
}
|