From d37002a3c0b8eb9e2adb996f25a3b52e9adb8723 Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Tue, 11 Jul 2017 08:45:17 -0600 Subject: pt:Added custom delimiter and submission support Now pt supports a custom field delimiter. Most UIs that have a username and password field, changing to from username to password is done by pressing the Tab key. Some UIs however, switch with other keys (like Return). Now a pass entry can specifiy 'delim: ...', which will be used when the username/passwords are typed. Also added support for automatic submission. Previously we just typed username, delimiter, password, and let the user press enter for maximum choice. Now we allow the user, on a per-password basis, to specify whether they want submit to be pressed or not. This adds support for the 'submit: ...' field. Also abstracted field value retrieval out of the main function (and long oneliner commands), into the new getfield function. --- pt | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/pt b/pt index bbbd491..91aa0cc 100755 --- a/pt +++ b/pt @@ -60,23 +60,64 @@ function check_env { } +# +# Gets a field's value from the specified [multiline] string, using the +# specified delimiter. +# +# @param content Content to extract field value from +# @param delim Delimiter seperating fields and values +# @param field Field name to get value for +# +getfield() { + local _content="${1:-}" + local _delim="${2:-}" + local _field="${3:-}" + + printf "${_content}" \ + | sed -n "s/${_field}${_delim} *\(.*\)/\1/p" \ + | head -n 1 +} + + function main { + local _passentry # Contents of the requested pass entry + local _passpassword # Password from the pass entry + local _passusername # Username field from the pass entry + local _passdelim # UI Field delimiter (Tab, Return), from the pass entry + local _passsubmit # Boolean submit (1,y,yes...), from the pass entry + + local _delim=Tab # Default delimiter if not specified in pass entry + local _submit=0 # Default submit value if not specified in pass entry + # Exit failure if no password was specified [[ -z ${1} ]] && log "Please specify a password to be typed" && exit 1 check_env || exit 0 # Check if the password exists in the store - pass ${@} &> /dev/null + # Also, copy the contents into a variable if it exists + _passentry=$(pass ${@} 2>/dev/null) [[ $? -gt 0 ]] && log "Error: '${@}' is not in the password store" && exit 1 # Parse pass output into appropriate variables - local passfile="$(pass ${@})" - local username=$(echo -e "${passfile}" | sed -n 's/username: \(.*\)/\1/p' | head -n 1) - local pass=$(echo -e "${passfile}" | head -n 1) + _passpassword=$(printf "${_passentry}" | head -n1) + _passusername=$(getfield "${_passentry}" ':' username) + _passdelim=$(getfield "${_passentry}" ':' delim) + _passsubmit=$(getfield "${_passentry}" ':' submit) + + # If any of the known 'submit' values are specified, set submit to 1 + if [ "${_passsubmit}" = '1' ] \ + || [ "${_passsubmit}" = 'y' ] \ + || [ "${_passsubmit}" = 'yes' ]; then + _submit=1 + fi # Type the password - xdotool type --delay 15 "${username} ${pass}" + xdotool type --delay 15 "${_passusername}" + xdotool key "${_delim}" + xdotool type --delay 15 "${_passpassword}" + + [ "${_submit}" = 1 ] && xdotool key Return } main ${@} -- cgit v1.2.3