Skip to content

Commit

Permalink
Add DBus interaction (gopasspw#434)
Browse files Browse the repository at this point in the history
This commit adds DBus interaction, i.e. unclip notifications and klipper
history clearing.

Fix broken clearClipboard method.

Fixes gopasspw#432
Fixes gopasspw#433
  • Loading branch information
dominikschulz authored Oct 29, 2017
1 parent 1143168 commit 5b86f24
Show file tree
Hide file tree
Showing 41 changed files with 5,716 additions and 6 deletions.
6 changes: 1 addition & 5 deletions action/clipboard_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"os/exec"
"strconv"
"syscall"
"time"
)

// clearClipboard will spwan a copy of gopass that waits in a detached background
Expand All @@ -20,10 +19,7 @@ import (
func clearClipboard(ctx context.Context, content []byte, timeout int) error {
hash := fmt.Sprintf("%x", sha256.Sum256(content))

ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, os.Args[0], "unclip", "--timeout", strconv.Itoa(timeout))
cmd := exec.Command(os.Args[0], "unclip", "--timeout", strconv.Itoa(timeout))
// https://groups.google.com/d/msg/golang-nuts/shST-SDqIp4/za4oxEiVtI0J
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Expand Down
14 changes: 13 additions & 1 deletion action/unclip.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

// Unclip tries to erase the content of the clipboard
func (s *Action) Unclip(ctx context.Context, c *cli.Context) error {
force := c.Bool("force")
timeout := c.Int("timeout")
checksum := os.Getenv("GOPASS_UNCLIP_CHECKSUM")

Expand All @@ -25,12 +26,23 @@ func (s *Action) Unclip(ctx context.Context, c *cli.Context) error {

hash := fmt.Sprintf("%x", sha256.Sum256([]byte(cur)))

if hash != checksum {
if hash != checksum && !force {
return nil
}

if err := clipboard.WriteAll(""); err != nil {
_ = s.unclipNotify(ctx, "Failed to clear clipboard")
return s.exitError(ctx, ExitIO, err, "failed to write clipboard: %s", err)
}

if err := s.clearClipboardHistory(ctx); err != nil {
_ = s.unclipNotify(ctx, "Failed to clear clipboard history")
return s.exitError(ctx, ExitIO, err, "failed to clear clipboard history: %s", err)
}

if err := s.unclipNotify(ctx, "Clipboard has been cleared"); err != nil {
return s.exitError(ctx, ExitIO, err, "failed to send unclip notification: %s", err)
}

return nil
}
43 changes: 43 additions & 0 deletions action/unclip_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// +build linux

package action

import (
"context"
"strings"

"github.com/godbus/dbus"
)

func (s *Action) clearClipboardHistory(ctx context.Context) error {
conn, err := dbus.SessionBus()
if err != nil {
return err
}

obj := conn.Object("org.kde.klipper", "/klipper")
call := obj.Call("org.kde.klipper.klipper.clearClipboardHistory", 0)
if call.Err != nil {
if strings.HasPrefix(call.Err.Error(), "The name org.kde.klipper was not provided") {
return nil
}
return call.Err
}

return nil
}

func (s *Action) unclipNotify(ctx context.Context, msg string) error {
conn, err := dbus.SessionBus()
if err != nil {
return err
}

obj := conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
call := obj.Call("org.freedesktop.Notifications.Notify", 0, "gopass", uint32(0), "", "gopass - clipboard", msg, []string{}, map[string]dbus.Variant{}, int32(5000))
if call.Err != nil {
return err
}

return nil
}
13 changes: 13 additions & 0 deletions action/unclip_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !linux

package action

import "context"

func (s *Action) clearClipboardHistory(ctx context.Context) error {
return nil
}

func (s *Action) unclipNotify(ctx context.Context, msg string) error {
return nil
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,10 @@ func main() {
Name: "timeout",
Usage: "Time to wait",
},
cli.BoolFlag{
Name: "force",
Usage: "Clear clipboard even if checksum mismatches",
},
},
},
{
Expand Down
50 changes: 50 additions & 0 deletions vendor/github.com/godbus/dbus/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/godbus/dbus/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/godbus/dbus/MAINTAINERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions vendor/github.com/godbus/dbus/README.markdown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5b86f24

Please sign in to comment.