2021-09-18 19:05:40 +02:00
|
|
|
// +build !windows,!darwin
|
|
|
|
|
2018-03-20 21:24:50 +01:00
|
|
|
// gomuks - A terminal Matrix client written in Go.
|
2020-04-19 17:10:14 +02:00
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
2018-03-20 21:24:50 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2019-01-17 13:13:25 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
2018-03-20 21:24:50 +01:00
|
|
|
// 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
|
2019-01-17 13:13:25 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2018-03-20 21:24:50 +01:00
|
|
|
//
|
2019-01-17 13:13:25 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-20 21:24:50 +01:00
|
|
|
|
|
|
|
package notification
|
|
|
|
|
2021-09-20 02:51:08 +02:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
2021-09-20 02:45:15 +02:00
|
|
|
var notifySendPath string
|
2021-09-20 02:51:08 +02:00
|
|
|
var audioCommand string
|
2021-09-20 02:45:15 +02:00
|
|
|
var tryAudioCommands = []string{"ogg123", "paplay"}
|
2021-09-20 02:51:08 +02:00
|
|
|
var soundNormal = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga"
|
|
|
|
var soundCritical = "/usr/share/sounds/freedesktop/stereo/complete.oga"
|
|
|
|
|
|
|
|
func getSoundPath(env, defaultPath string) string {
|
|
|
|
if path, ok := os.LookupEnv(env); ok {
|
|
|
|
// Sound file overriden by environment
|
|
|
|
return path
|
|
|
|
} else if _, err := os.Stat(defaultPath); os.IsNotExist(err) {
|
|
|
|
// Sound file doesn't exist, disable it
|
|
|
|
return ""
|
|
|
|
} else {
|
|
|
|
// Default sound file exists and wasn't overridden by environment
|
|
|
|
return defaultPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
2021-09-20 02:45:15 +02:00
|
|
|
|
|
|
|
if notifySendPath, err = exec.LookPath("notify-send"); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-20 02:51:08 +02:00
|
|
|
for _, cmd := range tryAudioCommands {
|
|
|
|
if audioCommand, err = exec.LookPath(cmd); err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
soundNormal = getSoundPath("GOMUKS_SOUND_NORMAL", soundNormal)
|
|
|
|
soundCritical = getSoundPath("GOMUKS_SOUND_CRITICAL", soundCritical)
|
|
|
|
}
|
2018-03-20 21:24:50 +01:00
|
|
|
|
2018-03-26 17:04:10 +02:00
|
|
|
func Send(title, text string, critical, sound bool) error {
|
2021-09-20 02:45:15 +02:00
|
|
|
if len(notifySendPath) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-20 21:24:50 +01:00
|
|
|
args := []string{"-a", "gomuks"}
|
2019-03-26 21:09:10 +01:00
|
|
|
if !critical {
|
|
|
|
args = append(args, "-u", "low")
|
2018-03-20 21:24:50 +01:00
|
|
|
}
|
2021-09-20 02:51:08 +02:00
|
|
|
//if iconPath {
|
|
|
|
// args = append(args, "-i", iconPath)
|
|
|
|
//}
|
2018-03-20 21:24:50 +01:00
|
|
|
args = append(args, title, text)
|
2021-09-20 02:51:08 +02:00
|
|
|
if sound && len(audioCommand) > 0 && len(soundNormal) > 0 {
|
|
|
|
audioFile := soundNormal
|
|
|
|
if critical && len(soundCritical) > 0 {
|
|
|
|
audioFile = soundCritical
|
2018-03-26 17:04:10 +02:00
|
|
|
}
|
2021-09-20 02:52:20 +02:00
|
|
|
go func() {
|
|
|
|
_ = exec.Command(audioCommand, audioFile).Run()
|
|
|
|
}()
|
2018-03-26 17:04:10 +02:00
|
|
|
}
|
2021-09-20 02:45:15 +02:00
|
|
|
return exec.Command(notifySendPath, args...).Run()
|
2018-03-20 21:24:50 +01:00
|
|
|
}
|