aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/menupass
blob: 844ef330aa9a3f25fc6af7e87b712ac6b5131803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/bash

# Inspiration taken from
# https://git.zx2c4.com/password-store/tree/contrib/dmenu/passmenu

shopt -s nullglob globstar

notititle="🔑 Password store"
cleartime=30
lastpass_cache="$HOME/.cache/lastpass"

prefix=${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg )
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )


notify_pass() {
  notify-send \
    "$notititle" \
    "The password for $1 has been copied to the clipboard and will be cleared in 30 seconds"
}

notify_spass() {
  notify-send "$notititle" "Password for $1: $2"
}

notify_nocache() {
  notify-send "$notititle" "No cache about previously selected login"
}

show_menu() {
  password=$(printf '%s\n' "${password_files[@]}" | rofi -dmenu -p "$1")
  [ -n "$password" ] || exit
}

copy_password() {
  thepassword=$(pass show "$1"| sed -n 1p)
  wl-copy "$thepassword"
}

copy_login() {
  thelogin=$(pass show "$1"| sed -n 2p)
  wl-copy "$thelogin"
  notify-send "$notititle" "The login $thelogin has been copied to the clipboard"
  printf "%s" "$1" > $lastpass_cache
}

wait_and_clear() {
  sleep $cleartime
  wl-copy ""
  notify-send "🔑 Password store" "Clipboard has been cleared"
}


case $1 in
  -l)
    show_menu "Show and copy login to clipboard"
    copy_login $password;;
  -s)
    show_menu "Show and copy password to clipboard"
    copy_password $password
    notify_spass "$password" "$thepassword"
    wait_and_clear;;
  -f)
    ## Copy the password of the last copied login
    [ -f $lastpass_cache ] || (notify_nocache && exit)
    password=$(cat $lastpass_cache)
    copy_password $password
    notify_pass "$password" "$thepassword"
    wait_and_clear;;
  *)
    show_menu "Copy password to clipboard"
    copy_password $password
    notify_pass "$password" "$thepassword"
    wait_and_clear;;
esac