| 1 | //go:build !windows && !darwin |
| 2 | |
| 3 | // gomuks - A terminal Matrix client written in Go. |
| 4 | // Copyright (C) 2020 Tulir Asokan |
| 5 | // |
| 6 | // This program is free software: you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU Affero General Public License as published by |
| 8 | // the Free Software Foundation, either version 3 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // This program is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU Affero General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU Affero General Public License |
| 17 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | |
| 19 | package notificationscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/ |
| 20 | |
| 21 | import ( |
| 22 | "osscip-go gomod github.com/golang/go/src go1.21 os/" |
| 23 | "os/execscip-go gomod github.com/golang/go/src go1.21 `os/exec`/" |
| 24 | ) |
| 25 | |
| 26 | var notifySendPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/notifySendPath. string |
| 27 | var audioCommandscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/audioCommand. string |
| 28 | var tryAudioCommandsscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/tryAudioCommands. = []string{"ogg123", "paplay"} |
| 29 | var soundNormalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundNormal. = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga" |
| 30 | var soundCriticalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundCritical. = "/usr/share/sounds/freedesktop/stereo/complete.oga" |
| 31 | |
| 32 | func getSoundPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/getSoundPath().(env, defaultPath string) string { |
| 33 | if path, ok := osscip-go gomod github.com/golang/go/src go1.21 os/.LookupEnvscip-go gomod github.com/golang/go/src go1.21 os/LookupEnv().(env); ok { |
| 34 | // Sound file overriden by environment |
| 35 | return path |
| 36 | } else if _, err := osscip-go gomod github.com/golang/go/src go1.21 os/.Statscip-go gomod github.com/golang/go/src go1.21 os/Stat().(defaultPath); osscip-go gomod github.com/golang/go/src go1.21 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.21 os/IsNotExist().(err) { |
| 37 | // Sound file doesn't exist, disable it |
| 38 | return "" |
| 39 | } else { |
| 40 | // Default sound file exists and wasn't overridden by environment |
| 41 | return defaultPath |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func initscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/init().() { |
| 46 | var err error |
| 47 | |
| 48 | if notifySendPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/notifySendPath., err = execscip-go gomod github.com/golang/go/src go1.21 `os/exec`/.LookPathscip-go gomod github.com/golang/go/src go1.21 `os/exec`/LookPath().("notify-send"); err != nil { |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | for _, cmd := range tryAudioCommandsscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/tryAudioCommands. { |
| 53 | if audioCommandscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/audioCommand., err = execscip-go gomod github.com/golang/go/src go1.21 `os/exec`/.LookPathscip-go gomod github.com/golang/go/src go1.21 `os/exec`/LookPath().(cmd); err == nil { |
| 54 | break |
| 55 | } |
| 56 | } |
| 57 | soundNormalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundNormal. = getSoundPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/getSoundPath().("GOMUKS_SOUND_NORMAL", soundNormalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundNormal.) |
| 58 | soundCriticalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundCritical. = getSoundPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/getSoundPath().("GOMUKS_SOUND_CRITICAL", soundCriticalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundCritical.) |
| 59 | } |
| 60 | |
| 61 | func Sendscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/Send().(title, text string, critical, sound bool) error { |
| 62 | if len(notifySendPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/notifySendPath.) == 0 { |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | args := []string{"-a", "gomuks"} |
| 67 | if !critical { |
| 68 | args = append(args, "-u", "low") |
| 69 | } |
| 70 | //if iconPath { |
| 71 | // args = append(args, "-i", iconPath) |
| 72 | //} |
| 73 | args = append(args, title, text) |
| 74 | if sound && len(audioCommandscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/audioCommand.) > 0 && len(soundNormalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundNormal.) > 0 { |
| 75 | audioFile := soundNormalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundNormal. |
| 76 | if critical && len(soundCriticalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundCritical.) > 0 { |
| 77 | audioFile = soundCriticalscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/soundCritical. |
| 78 | } |
| 79 | go func() { |
| 80 | _ = execscip-go gomod github.com/golang/go/src go1.21 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.21 `os/exec`/Command().(audioCommandscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/audioCommand., audioFile).Runscip-go gomod github.com/golang/go/src go1.21 `os/exec`/Cmd#Run().() |
| 81 | }() |
| 82 | } |
| 83 | return execscip-go gomod github.com/golang/go/src go1.21 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.21 `os/exec`/Command().(notifySendPathscip-go gomod maunium.net/go/gomuks . `maunium.net/go/gomuks/lib/notification`/notifySendPath., args...).Runscip-go gomod github.com/golang/go/src go1.21 `os/exec`/Cmd#Run().() |
| 84 | } |