From 225701e1f6b17546887fedce542b44aa9640f432 Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Sun, 29 Oct 2017 11:37:26 +0100 Subject: [PATCH] Add DBus interaction This commit adds DBus interaction, i.e. unclip notifications and klipper history clearing. Fix broken clearClipboard method. Fixes #432 Fixes #433 --- action/clipboard_others.go | 6 +- action/unclip.go | 14 +- action/unclip_linux.go | 43 + action/unclip_others.go | 13 + main.go | 4 + vendor/github.com/godbus/dbus/CONTRIBUTING.md | 50 ++ vendor/github.com/godbus/dbus/LICENSE | 25 + vendor/github.com/godbus/dbus/MAINTAINERS | 3 + vendor/github.com/godbus/dbus/README.markdown | 44 + vendor/github.com/godbus/dbus/auth.go | 253 ++++++ .../github.com/godbus/dbus/auth_external.go | 26 + vendor/github.com/godbus/dbus/auth_sha1.go | 102 +++ vendor/github.com/godbus/dbus/call.go | 36 + vendor/github.com/godbus/dbus/conn.go | 683 +++++++++++++++ vendor/github.com/godbus/dbus/conn_darwin.go | 33 + vendor/github.com/godbus/dbus/conn_other.go | 42 + vendor/github.com/godbus/dbus/dbus.go | 427 +++++++++ vendor/github.com/godbus/dbus/decoder.go | 228 +++++ .../github.com/godbus/dbus/default_handler.go | 291 +++++++ vendor/github.com/godbus/dbus/doc.go | 69 ++ vendor/github.com/godbus/dbus/encoder.go | 210 +++++ vendor/github.com/godbus/dbus/export.go | 413 +++++++++ vendor/github.com/godbus/dbus/homedir.go | 28 + .../github.com/godbus/dbus/homedir_dynamic.go | 15 + .../github.com/godbus/dbus/homedir_static.go | 45 + vendor/github.com/godbus/dbus/message.go | 353 ++++++++ vendor/github.com/godbus/dbus/object.go | 147 ++++ .../godbus/dbus/server_interfaces.go | 89 ++ vendor/github.com/godbus/dbus/sig.go | 259 ++++++ .../godbus/dbus/transport_darwin.go | 6 + .../godbus/dbus/transport_generic.go | 50 ++ .../github.com/godbus/dbus/transport_tcp.go | 43 + .../github.com/godbus/dbus/transport_unix.go | 196 +++++ .../dbus/transport_unixcred_dragonfly.go | 95 ++ .../godbus/dbus/transport_unixcred_freebsd.go | 91 ++ .../godbus/dbus/transport_unixcred_linux.go | 25 + .../godbus/dbus/transport_unixcred_openbsd.go | 14 + vendor/github.com/godbus/dbus/variant.go | 144 +++ .../github.com/godbus/dbus/variant_lexer.go | 284 ++++++ .../github.com/godbus/dbus/variant_parser.go | 817 ++++++++++++++++++ vendor/vendor.json | 6 + 41 files changed, 5716 insertions(+), 6 deletions(-) create mode 100644 action/unclip_linux.go create mode 100644 action/unclip_others.go create mode 100644 vendor/github.com/godbus/dbus/CONTRIBUTING.md create mode 100644 vendor/github.com/godbus/dbus/LICENSE create mode 100644 vendor/github.com/godbus/dbus/MAINTAINERS create mode 100644 vendor/github.com/godbus/dbus/README.markdown create mode 100644 vendor/github.com/godbus/dbus/auth.go create mode 100644 vendor/github.com/godbus/dbus/auth_external.go create mode 100644 vendor/github.com/godbus/dbus/auth_sha1.go create mode 100644 vendor/github.com/godbus/dbus/call.go create mode 100644 vendor/github.com/godbus/dbus/conn.go create mode 100644 vendor/github.com/godbus/dbus/conn_darwin.go create mode 100644 vendor/github.com/godbus/dbus/conn_other.go create mode 100644 vendor/github.com/godbus/dbus/dbus.go create mode 100644 vendor/github.com/godbus/dbus/decoder.go create mode 100644 vendor/github.com/godbus/dbus/default_handler.go create mode 100644 vendor/github.com/godbus/dbus/doc.go create mode 100644 vendor/github.com/godbus/dbus/encoder.go create mode 100644 vendor/github.com/godbus/dbus/export.go create mode 100644 vendor/github.com/godbus/dbus/homedir.go create mode 100644 vendor/github.com/godbus/dbus/homedir_dynamic.go create mode 100644 vendor/github.com/godbus/dbus/homedir_static.go create mode 100644 vendor/github.com/godbus/dbus/message.go create mode 100644 vendor/github.com/godbus/dbus/object.go create mode 100644 vendor/github.com/godbus/dbus/server_interfaces.go create mode 100644 vendor/github.com/godbus/dbus/sig.go create mode 100644 vendor/github.com/godbus/dbus/transport_darwin.go create mode 100644 vendor/github.com/godbus/dbus/transport_generic.go create mode 100644 vendor/github.com/godbus/dbus/transport_tcp.go create mode 100644 vendor/github.com/godbus/dbus/transport_unix.go create mode 100644 vendor/github.com/godbus/dbus/transport_unixcred_dragonfly.go create mode 100644 vendor/github.com/godbus/dbus/transport_unixcred_freebsd.go create mode 100644 vendor/github.com/godbus/dbus/transport_unixcred_linux.go create mode 100644 vendor/github.com/godbus/dbus/transport_unixcred_openbsd.go create mode 100644 vendor/github.com/godbus/dbus/variant.go create mode 100644 vendor/github.com/godbus/dbus/variant_lexer.go create mode 100644 vendor/github.com/godbus/dbus/variant_parser.go diff --git a/action/clipboard_others.go b/action/clipboard_others.go index ecdc70b390..7b2431d3a9 100644 --- a/action/clipboard_others.go +++ b/action/clipboard_others.go @@ -10,7 +10,6 @@ import ( "os/exec" "strconv" "syscall" - "time" ) // clearClipboard will spwan a copy of gopass that waits in a detached background @@ -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, diff --git a/action/unclip.go b/action/unclip.go index da5025ed8b..793bf4c0d9 100644 --- a/action/unclip.go +++ b/action/unclip.go @@ -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") @@ -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 } diff --git a/action/unclip_linux.go b/action/unclip_linux.go new file mode 100644 index 0000000000..53b9a29327 --- /dev/null +++ b/action/unclip_linux.go @@ -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 +} diff --git a/action/unclip_others.go b/action/unclip_others.go new file mode 100644 index 0000000000..a7c15ab03b --- /dev/null +++ b/action/unclip_others.go @@ -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 +} diff --git a/main.go b/main.go index 500c656c4a..98fe5f2c65 100644 --- a/main.go +++ b/main.go @@ -987,6 +987,10 @@ func main() { Name: "timeout", Usage: "Time to wait", }, + cli.BoolFlag{ + Name: "force", + Usage: "Clear clipboard even if checksum mismatches", + }, }, }, { diff --git a/vendor/github.com/godbus/dbus/CONTRIBUTING.md b/vendor/github.com/godbus/dbus/CONTRIBUTING.md new file mode 100644 index 0000000000..c88f9b2bdd --- /dev/null +++ b/vendor/github.com/godbus/dbus/CONTRIBUTING.md @@ -0,0 +1,50 @@ +# How to Contribute + +## Getting Started + +- Fork the repository on GitHub +- Read the [README](README.markdown) for build and test instructions +- Play with the project, submit bugs, submit patches! + +## Contribution Flow + +This is a rough outline of what a contributor's workflow looks like: + +- Create a topic branch from where you want to base your work (usually master). +- Make commits of logical units. +- Make sure your commit messages are in the proper format (see below). +- Push your changes to a topic branch in your fork of the repository. +- Make sure the tests pass, and add any new tests as appropriate. +- Submit a pull request to the original repository. + +Thanks for your contributions! + +### Format of the Commit Message + +We follow a rough convention for commit messages that is designed to answer two +questions: what changed and why. The subject line should feature the what and +the body of the commit should describe the why. + +``` +scripts: add the test-cluster command + +this uses tmux to setup a test cluster that you can easily kill and +start for debugging. + +Fixes #38 +``` + +The format can be described more formally as follows: + +``` +: + + + +