-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaerc
More file actions
executable file
·90 lines (73 loc) · 2.34 KB
/
aerc
File metadata and controls
executable file
·90 lines (73 loc) · 2.34 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# shellcheck disable=SC2128,SC2086,SC2155
###############################################################################
# Expand environment variables using envsubst and macros using m4 to #
# implement custom conditional logic in aerc configuration files. In #
# addition, for the accounts configuration file keepassxc-cli is #
# used to populate email credentials. #
###############################################################################
config_home=${XDG_CONFIG_HOME:-~/.config}/aerc
main=$config_home/aerc.conf
accounts=$config_home/accounts.conf
binds=$config_home/binds.conf
aerc=$(which --skip-tilde aerc)
database=${KEEPASSXC_DATABASE:-~/database.kdbx}
keyfile=${KEEPASSXC_KEYFILE:-} # optional
timeout=600
# https://stackoverflow.com/a/10660730
function rawurlencode(){
local pos char
for (( pos=0 ; pos<${#1} ; pos++ )); do
char=${1:$pos:1}
case "$char" in
[-_.~a-zA-Z0-9])
echo -n "${char}"
;;
*)
printf '%%%02x' "'$char"
esac
done
}
function get_accounts(){
while IFS= read -r line; do
if [[ "$line" =~ ^\[(.+)\]$ ]]; then
echo "${BASH_REMATCH[1]}"
fi
done < "$@"
}
function load_credentials(){
local entry secrets passwd="$(keyctl print "%user:$0" 2>/dev/null)" IFS=$'\n'
for entry in $(get_accounts "$@"); do
coproc keepassxc-cli show -a UserName -a Email -a Password -k "$keyfile" \
${passwd:+--quiet} "$database" "$entry"
if [[ -z "$passwd" ]]; then
read -rs passwd
fi
exec 3>&"${COPROC[0]}"
echo "$passwd">&"${COPROC[1]}"
wait $COPROC_PID
read -rd '' -u 3 -a secrets
# if we misspelled the password, try again
if [[ "${#secrets[@]}" -eq 0 ]]; then
load_credentials "$@"
return
fi
export ${entry@U}_USER=${secrets[0]}
export ${entry@U}_MAIL=${secrets[1]}
export ${entry@U}_PASS="$(rawurlencode ${secrets[2]})"
done
keyctl add user "$0" "$passwd" @u >/dev/null
keyctl timeout "%user:$0" "$timeout"
}
function expand(){
envsubst "$(compgen -eP '$')" <<< "$(< "$@")" | m4
}
if [[ "$BASH_SOURCE" == "$0" ]]; then
exec 3< <(
trap 'kill -- -$$' INT
load_credentials "$accounts"
expand "$accounts"
)
wait $!
exec "$aerc" -C <(expand "$main") -B <(expand "$binds") -A /dev/fd/3 "$@"
fi