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

Support xsel in addition to xclip #3

Merged
merged 6 commits into from
Jan 2, 2019
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BitWarden Rofi Menu

This is a work in progress to get the BitWarden cli functionality in an easy Rofi menu.
This is a work in progress to get the BitWarden cli functionality in an easy Rofi menu.
On selecting an entry, the password is copied to your clipboard for 5 seconds. During those 5 seconds, a notification is shown indicating which password you are copying at that time.

![bitwarden-rofi](img/screenshot1.png)
Expand All @@ -24,4 +24,4 @@ You can either execute the script from a terminal or by binding it to a key comb

- bitwarden-cli
- jq
- xclip
- xclip or xsel
73 changes: 64 additions & 9 deletions bwmenu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ BW_HASH_FILE=~/.bwhash
# Holds the available items in memory
ITEMS=

# Stores which command will be used to deal with clipboards
CLIPBOARD_MODE=

# source the hash file to gain access to the BitWarden CLI
# Pre fetch all the items
load_items() {
Expand All @@ -15,14 +18,21 @@ load_items() {
if ! ITEMS=$(bw list items 2>/dev/null); then
mpw=$(rofi -dmenu -p "Master Password" -password) || exit $?
bw unlock "$mpw" | grep -e 'export.*=="' -o > $BW_HASH_FILE || {
rofi -e "Failed to unlock BitWarden.
exit_error 1 "Failed to unlock BitWarden.
Please try again."
exit 1
}
load_items
fi
}

exit_error() {
local code="$1"
local message="$2"

rofi -e "$message"
exit "$code"
}

# Show the Rofi menu with options
# Reads items from stdin
rofi_menu() {
Expand Down Expand Up @@ -84,10 +94,7 @@ show_folders() {

# re-sync the BitWarden items with the server
sync_bitwarden() {
bw sync &>/dev/null || {
rofi -e "Failed to sync bitwarden"
exit 1
}
bw sync &>/dev/null || exit_error 1 "Failed to sync bitwarden"

load_items
show_items
Expand All @@ -104,6 +111,53 @@ on_rofi_exit() {
esac
}

# Set $CLIPBOARD_MODE to a command that will put stdin into the clipboard.
select_copy_command() {
if hash xclip 2>/dev/null; then
CLIPBOARD_MODE=xclip
elif hash xsel 2>/dev/null; then
CLIPBOARD_MODE=xsel
else
exit_error 1 "No clipboard command found. Please install either xclip or xsel."
fi
}

clipboard-set() {
clipboard-${CLIPBOARD_MODE}-set
}

clipboard-get() {
clipboard-${CLIPBOARD_MODE}-get
}

clipboard-clear() {
clipboard-${CLIPBOARD_MODE}-clear
}

clipboard-xclip-set() {
xclip -selection clipboard -r
}

clipboard-xclip-get() {
xclip -selection clipboard -o
}

clipboard-xclip-clear() {
echo -n "" | xclip -selection clipboard -r
}

clipboard-xsel-set() {
xsel --clipboard --input
}

clipboard-xsel-get() {
xsel --clipboard
}

clipboard-xsel-clear() {
xsel --clipboard --delete
}

# Output the password
# copy to clipboard and give the user feedback that the password is copied
# $1: json item
Expand All @@ -113,11 +167,11 @@ show_password() {
# send a notification for 5 seconds (-t 5000)
# not sure if icon will be present everywhere, /usr/share/icons is default icon location
notify-send "<b>$(echo "$1" | jq -r '.name')</b> copied" "${pass:0:4}****" -t 5000 -i /usr/share/icons/hicolor/64x64/apps/bitwarden.png
echo "$pass" | xclip -selection clipboard -r
echo -n "$pass" | clipboard-set

sleep 5
if [ "$(xclip -selection clipboard -o)" = "$pass" ]; then
echo '' | xclip -selection clipboard -r
if [[ "$(clipboard-get)" == "$pass" ]]; then
clipboard-clear
fi
}

Expand All @@ -126,5 +180,6 @@ if ! [ -f $BW_HASH_FILE ]; then
chmod 600 $BW_HASH_FILE
fi

select_copy_command
load_items
show_items