Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libnotify bindings, close notification after touch #13

Merged
merged 7 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/menefotto/go-libnotify v0.0.0-20160821232856-351dc16572b9
github.com/rjeczalik/notify v0.9.2
github.com/scylladb/go-set v1.0.2
github.com/sirupsen/logrus v1.4.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0C
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/menefotto/go-libnotify v0.0.0-20160821232856-351dc16572b9 h1:rT1Icv4lVUyxLfZbs4SptWweIkEZSoYsrMCBjhUjUnk=
github.com/menefotto/go-libnotify v0.0.0-20160821232856-351dc16572b9/go.mod h1:KdXlwZrg/gkgHuk2b2zSfkabln9xiNgG/GlRhDcFzWE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
Expand Down
39 changes: 31 additions & 8 deletions notifier/libnotify.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
package notifier

import (
"os/exec"
"sync"

libnotify "github.com/menefotto/go-libnotify"
n-st marked this conversation as resolved.
Show resolved Hide resolved
log "github.com/sirupsen/logrus"
)

func notifySend(text string) error {
return exec.Command("notify-send", text).Run()
}

// SetupLibnotifyNotifier configures a notifier to show all touch requests with libnotify
func SetupLibnotifyNotifier(notifiers *sync.Map) {
touch := make(chan Message, 10)
notifiers.Store("notifier/libnotify", touch)

if !libnotify.Init("yubikey-touch-detector") {
log.Error("Cannot initialize desktop notifications!")
return
}
n-st marked this conversation as resolved.
Show resolved Hide resolved
defer libnotify.UnInit()

notification := libnotify.NotificationNew("YubiKey is waiting for a touch", "", "")
if notification == nil {
log.Error("Cannot create desktop notification!")
return
}

activeTouchWaits := 0

for {
value := <-touch
if value == GPG_ON || value == U2F_ON || value == HMAC_ON {
err := notifySend("YubiKey is waiting for a touch")
if err != nil {
log.Error("Cannot send desktop notification: ", err)
activeTouchWaits++
}
if value == GPG_OFF || value == U2F_OFF || value == HMAC_OFF {
n-st marked this conversation as resolved.
Show resolved Hide resolved
activeTouchWaits--
}
if activeTouchWaits > 0 {
// Error check (!= nil) not possible because menefotto/go-libnotify
// uses a custom wrapper instead of builtin 'error'
if err := notification.Show(); err.Error() != "" {
log.Error("Cannot show notification: ", err.Error())
}
} else {
// Error check (!= nil) not possible because menefotto/go-libnotify
// uses a custom wrapper instead of builtin 'error'
if err := notification.Close(); err.Error() != "" {
log.Error("Cannot close notification: ", err.Error())
}
}
}
Expand Down