| 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 notification |
| 20 | |
| 21 | import ( |
| 22 | "os" |
| 23 | "os/exec" |
| 24 | ) |
| 25 | |
| 26 | var notifySendPath string |
| 27 | var audioCommand string |
| 28 | var tryAudioCommands = []string{"ogg123", "paplay"} |
| 29 | var soundNormal = "/usr/share/sounds/freedesktop/stereo/message-new-instant.oga" |
| 30 | var soundCritical = "/usr/share/sounds/freedesktop/stereo/complete.oga" |
| 31 | |
| 32 | func getSoundPath(env, defaultPath string) string { |
| 33 | if path, ok := os.LookupEnv(env); ok { |
| 34 | // Sound file overriden by environment |
| 35 | return path |
| 36 | } else if _, err := os.Stat(defaultPath); 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 init() { |
| 46 | var err error |
| 47 | |
| 48 | if notifySendPath, err = exec.LookPath("notify-send"); err != nil { |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | for _, cmd := range tryAudioCommands { |
| 53 | if audioCommand, err = exec.LookPath(cmd); err == nil { |
| 54 | break |
| 55 | } |
| 56 | } |
| 57 | soundNormal = getSoundPath("GOMUKS_SOUND_NORMAL", soundNormal) |
| 58 | soundCritical = getSoundPath("GOMUKS_SOUND_CRITICAL", soundCritical) |
| 59 | } |
| 60 | |
| 61 | func Send(title, text string, critical, sound bool) error { |
| 62 | if len(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(audioCommand) > 0 && len(soundNormal) > 0 { |
| 75 | audioFile := soundNormal |
| 76 | if critical && len(soundCritical) > 0 { |
| 77 | audioFile = soundCritical |
| 78 | } |
| 79 | go func() { |
| 80 | _ = exec.Command(audioCommand, audioFile).Run() |
| 81 | }() |
| 82 | } |
| 83 | return exec.Command(notifySendPath, args...).Run() |
| 84 | } |