dmenupass

X11 username/password autotyper with dmenu selection
git clone git://git.deurzen.net/dmenupass
Log | Files | Refs | LICENSE

dmenupass (2444B)


      1 #!/usr/bin/env bash
      2 set -Eeo pipefail
      3 
      4 arg="$1"
      5 if [[ $arg == "--copy" ]]; then
      6   prompt="Copy:"
      7   copy=1
      8 elif [[ $arg == "--print" ]]; then
      9   prompt="Print:";
     10   print=1
     11 else
     12   prompt="Autotype:"
     13   autotype=1
     14 fi
     15 
     16 shopt -s nullglob globstar
     17 
     18 username_delim='user'
     19 password_delim='pass'
     20 
     21 username_field_delim='user:'
     22 password_field_delim='pass:'
     23 sequence_field_delim='dpseq:'
     24 url_field_delim='url:'
     25 
     26 default_sequence='user :tab pass :enter'
     27 
     28 
     29 __init__() {
     30   prefix=${PASSWORD_STORE_DIR:-~/.password-store}
     31   password_itemlist=( "$prefix"/**/*.gpg )
     32   password_itemlist=( "${password_itemlist[@]#"$prefix"/}" )
     33   password_itemlist=( "${password_itemlist[@]%.gpg}" )
     34   password_item=$(printf '%s\n' "${password_itemlist[@]}" | dmenu -p "$prompt" "$@")
     35   [[ -n $password_item ]] || exit
     36   [ ! -z "$copy" ] && { __copy__ 2>/dev/null; exit; }
     37   [ ! -z "$print" ] && { __print__ 2>/dev/null; exit; }
     38   password_itemfile="$prefix/${password_item}.gpg"
     39 
     40   while IFS= read -r line
     41   do
     42     key=$(awk '{print $1;}' <<< "$line")
     43     value=${line##$key }
     44     case "$key" in
     45     "${username_field_delim}")
     46       username="$value"
     47       ;;
     48     "${password_field_delim}")
     49       password="$value"
     50       ;;
     51     "${sequence_field_delim}")
     52       sequence="$value"
     53       ;;
     54     "${url_field_delim}")
     55       url="$value"
     56       ;;
     57     *);;
     58     esac
     59   done < <(pass "$password_item" || kill -HUP $$;)
     60 }
     61 
     62 __run__() {
     63   for token in ${sequence:-${default_sequence}}
     64   do
     65     case "$token" in
     66     "${username_delim}")
     67       xdotool type --clearmodifiers "$username"
     68       ;;
     69     "${password_delim}")
     70       xdotool type --clearmodifiers "$password"
     71       ;;
     72     ':wait')
     73       sleep 0.2
     74       ;;
     75     ':pause')
     76       sleep 1
     77       ;;
     78     ':input')
     79       xdotool key semicolon key t
     80       sleep 0.1
     81       xdotool key a
     82       ;;
     83     ':tab')
     84       xdotool key Tab
     85       ;;
     86     ':enter')
     87       xdotool key KP_Enter
     88       ;;
     89     *);;
     90     esac
     91   done
     92 
     93   unset -v username
     94   unset -v password
     95   unset -v sequence
     96   unset -v url
     97 }
     98 
     99 __copy__() {
    100   password=$(pass $password_item | grep ^$password_delim | awk '{print $2}')
    101   if type xclip >/dev/null 2>&1; then
    102     echo -n "$password" | xclip -selection c
    103   elif type xsel >/dev/null 2>&1; then
    104     echo -n "$password" | xsel -b
    105   fi
    106   unset -v password
    107 }
    108 
    109 __print__() {
    110   password=$(pass $password_item | grep ^$password_delim | awk '{print $2}')
    111   echo -n "$password"
    112   unset -v password
    113 }
    114 
    115 __init__ 2>/dev/null
    116 __run__ 2>/dev/null