Skip to content

Commit 27b3a9b

Browse files
authored
Merge pull request #41 from dyne/startup-lazy-filter-cache
perf: lazy-load generated filter cache on startup
2 parents af6f496 + a7f4adb commit 27b3a9b

9 files changed

Lines changed: 149 additions & 41 deletions

File tree

extras/test/test-mutt-crypto.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env zsh
2+
3+
set -euo pipefail
4+
5+
script_dir="${0:A:h}"
6+
source "${script_dir}/lib/test_helpers.zsh"
7+
test_setup "${0}"
8+
9+
cleanup() {
10+
test_cleanup
11+
}
12+
trap cleanup EXIT INT TERM
13+
14+
command -v mutt >/dev/null 2>&1 || {
15+
print -- "SKIP test-mutt-crypto.sh: missing mutt"
16+
exit 0
17+
}
18+
19+
muttrc="${tmp_root}/muttrc"
20+
cat > "${muttrc}" <<EOF
21+
source "${work_root}/mutt/crypto"
22+
EOF
23+
24+
crypto_settings="$(mutt -F "${muttrc}" \
25+
-Q crypt_use_gpgme \
26+
-Q crypt_autosmime \
27+
-Q smime_verify_command \
28+
-Q smime_verify_opaque_command </dev/null)"
29+
30+
assert_contains "${crypto_settings}" "crypt_use_gpgme is unset" "disable gpgme"
31+
assert_contains "${crypto_settings}" "crypt_autosmime is unset" "disable automatic smime"
32+
assert_contains "${crypto_settings}" 'smime_verify_command="/bin/false"' "fail fast smime verify"
33+
assert_contains "${crypto_settings}" 'smime_verify_opaque_command="/bin/false"' "fail fast opaque smime verify"

src/jaro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ main() {
245245
{ option.is_set -R } && { muttflags+=" -R " }
246246
{ option.is_set -f } && { FORCE=1 }
247247

248+
check_bin "$subcommand"
249+
248250
# clean up options from param
249251
# PARAM=(${PARAM// -? /})
250252

@@ -357,7 +359,6 @@ main() {
357359
}
358360

359361

360-
check_bin
361362
main $@
362363
# endgame NOERRORS
363364
# return $exitcode

src/mutt/crypto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ set pgp_auto_decode
88
set pgp_strict_enc
99
set pgp_verify_sig = yes
1010

11+
# JaroMail configures classic PGP commands below. Avoid mutt's GPGME
12+
# backend here because it also invokes gpgsm for S/MIME signatures and
13+
# can block message opening on attached smime.p7s files.
14+
unset crypt_use_gpgme
15+
unset crypt_autosmime
16+
set smime_verify_command="/bin/false"
17+
set smime_verify_opaque_command="/bin/false"
18+
1119
# # CLEARTEXT INLINE GPG
1220
# set pgp_create_traditional = no
1321
# macro compose \cx "Fgpg --clearsign\nyy"

src/zlibs/addressbook

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ sender_isknown() {
171171
local _mail_input="${body:-}"
172172
[[ -z "${_mail_input}" ]] && _mail_input="$(cat)"
173173
if command -v maddr > /dev/null; then
174+
mblaze_ensure
174175
local email_regex="([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})"
175176
e_from=`print - "${_mail_input}" | maddr -h 'from' - | grep -oP "$email_regex"`
176177
e_from=${e_from:l}

src/zlibs/bootstrap

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ bootstrap_root="${bootstrap_file:A:h:h}"
142142
# global variable for mutt options
143143
vars+=(muttflags)
144144

145-
autoload colors; colors
146-
147145
# which command to use when creating dirs
148146
mkdir="`command -v mkdir` -m 700 -p"
149147

@@ -170,8 +168,6 @@ bootstrap_root="${bootstrap_file:A:h:h}"
170168

171169
# own mblaze workdir and init empty sequence
172170
export MBLAZE="$MAILDIRS/cache/mblaze"
173-
mkdir -p "${MBLAZE}"
174-
touch ${MBLAZE}/seq
175171

176172
PATH="$WORKDIR/bin:$PATH"
177173

@@ -239,51 +235,25 @@ bootstrap_root="${bootstrap_file:A:h:h}"
239235
}
240236

241237
# binary programs recognition
242-
check_bin() {
243-
# required programs
244-
for req in pinentry fetchmail gpg msmtp; do
238+
check_required_bins() {
239+
for req in "$@"; do
245240
isfound $req
246241
{ test $? != 0 } && {
247242
error "Cannot find $req. Please install it."
248243
exit 1
249244
}
250245
done
246+
}
251247

252-
# make sure a gnupg dir exists
248+
ensure_gnupg_dir() {
253249
{ test -r $HOME/.gnupg/pubring.gpg } || {
254250
${=mkdir} $HOME/.gnupg
255251
touch $HOME/.gnupg/pubring.gpg
256252
touch $HOME/.gnupg/secring.gpg
257253
}
254+
}
258255

259-
# which find command to use
260-
case $OS in
261-
GNU) find="find -O3" ;;
262-
MAC) find="gfind -O3" ;;
263-
*) find="find"
264-
esac
265-
266-
# which wipe command to use
267-
if isfound wipe; then
268-
rm="wipe -f -s -q -R /dev/urandom"
269-
elif isfound srm; then
270-
rm="srm -m"
271-
else
272-
rm="rm -f"
273-
fi
274-
func "Rm binary: $rm"
275-
276-
# which mutt binary to use
277-
if isfound mutt; then
278-
mutt_exec=mutt
279-
elif isfound neomutt; then
280-
mutt_exec=neomutt
281-
fi
282-
pgpewrap="${WORKDIR}/bin/gpgewrap"
283-
dotlock="${WORKDIR}/bin/dotlock"
284-
285-
func "Mutt binary: $mutt_exec"
286-
func "Notmuch binary: `command -v notmuch`"
256+
check_keyring() {
287257
func "Keyring set: $JARO_KEYRING"
288258
if [[ "$JARO_KEYRING" == "" ]]; then
289259
# check for pass, else fallback
@@ -324,6 +294,79 @@ check_bin() {
324294
GNOMEKEY=1 ;;
325295
esac
326296
fi
297+
}
298+
299+
ensure_mutt() {
300+
if isfound mutt; then
301+
mutt_exec=mutt
302+
elif isfound neomutt; then
303+
mutt_exec=neomutt
304+
else
305+
error "Cannot find mutt or neomutt. Please install one."
306+
exit 1
307+
fi
308+
func "Mutt binary: $mutt_exec"
309+
}
310+
311+
mblaze_ensure() {
312+
export MBLAZE=${MBLAZE:-"$MAILDIRS/cache/mblaze"}
313+
mkdir -p "${MBLAZE}"
314+
touch "${MBLAZE}/seq"
315+
}
316+
317+
check_bin() {
318+
local cmd="${1:-$subcommand}"
319+
320+
# which find command to use
321+
case $OS in
322+
GNU) find="find -O3" ;;
323+
MAC) find="gfind -O3" ;;
324+
*) find="find"
325+
esac
326+
327+
# which wipe command to use
328+
if isfound wipe; then
329+
rm="wipe -f -s -q -R /dev/urandom"
330+
elif isfound srm; then
331+
rm="srm -m"
332+
else
333+
rm="rm -f"
334+
fi
335+
func "Rm binary: $rm"
336+
337+
pgpewrap="${WORKDIR}/bin/gpgewrap"
338+
dotlock="${WORKDIR}/bin/dotlock"
339+
340+
case "$cmd" in
341+
fetch)
342+
check_required_bins pinentry fetchmail gpg
343+
ensure_gnupg_dir
344+
check_keyring
345+
;;
346+
send|smtp)
347+
check_required_bins pinentry gpg msmtp
348+
ensure_gnupg_dir
349+
check_keyring
350+
;;
351+
peek|open|compose|__empty|__unknown:*)
352+
ensure_mutt
353+
[[ "$cmd" = "peek" ]] && {
354+
check_required_bins pinentry gpg
355+
ensure_gnupg_dir
356+
check_keyring
357+
}
358+
;;
359+
passwd|askpass)
360+
check_required_bins pinentry gpg
361+
ensure_gnupg_dir
362+
check_keyring
363+
;;
364+
init|wizard)
365+
check_required_bins pinentry gpg
366+
ensure_gnupg_dir
367+
check_keyring
368+
;;
369+
esac
327370

328371
return 0
329372
}

src/zlibs/filters

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@
3333
# 8. All the rest -> unsorted
3434
#
3535

36-
# load zsh filter cache arrays
37-
test -r "$MAILDIRS/cache/filters" && {
36+
# Load zsh filter cache arrays only for commands that need them.
37+
load_filter_cache() {
38+
fn load_filter_cache
39+
40+
typeset -gAl filter_from
41+
typeset -galU filter_own
42+
typeset -gAl filter_to
43+
typeset -galU filter_whitelist
44+
typeset -galU filter_blacklist
45+
46+
[[ -r "$MAILDIRS/cache/filters" ]] || return 1
3847
source "$MAILDIRS/cache/filters"
3948
}
4049

@@ -230,8 +239,7 @@ filter_maildir() {
230239
maildirmake "$MAILDIRS/unsorted"
231240

232241
# loads up the filter cache (zsh compiled arrays)
233-
[[ -r "$MAILDIRS/cache/filters" ]] &&
234-
source $MAILDIRS/cache/filters
242+
load_filter_cache
235243

236244
mails="$(maildir_list_messages "$srcpath")"
237245
numm=`print $mails | wc -l`
@@ -243,6 +251,7 @@ filter_maildir() {
243251

244252
notice "Filtering maildir: $mdinput ($numm mails)"
245253
c=0
254+
mblaze_ensure
246255

247256
for m in ${(f)mails}; do
248257

@@ -551,6 +560,8 @@ update_sieve() {
551560

552561
#######
553562
# SIEVE
563+
load_filter_cache
564+
554565
act "generate sieve filter rules"
555566
id=`datestamp`.$RANDOM
556567
newlock "$MAILDIRS/Filters.sieve"

src/zlibs/keyring

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
# pass wrapper to set all env
2525
_pass() {
2626
fn pass $*
27+
[[ -t 0 ]] && {
28+
export GPG_TTY=$(tty)
29+
command -v gpg-connect-agent >/dev/null 2>&1 &&
30+
gpg-connect-agent updatestartuptty /bye >/dev/null 2>&1
31+
}
2732
PASSWORD_STORE_DIR=$PASSWORD_STORE_DIR pass $*
2833
}
2934

src/zlibs/maildirs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ maildirmake() {
5454

5555
# use mblaze if found
5656
command -v mmkdir > /dev/null && {
57+
mblaze_ensure
5758
mmkdir "$1"
5859
return $?
5960
}
@@ -90,6 +91,7 @@ maildir_list_messages() {
9091
maildircheck "$md" || return 1
9192

9293
if command -v mlist >/dev/null; then
94+
mblaze_ensure
9395
mlist "$md"
9496
return $?
9597
fi
@@ -108,6 +110,7 @@ maildir_list_folders() {
108110
[[ -d "$root" ]] || return 1
109111

110112
if command -v mdirs >/dev/null; then
113+
mblaze_ensure
111114
local folders
112115
folders="$(mdirs -a "$root")"
113116
for folder in ${(f)folders}; do
@@ -134,6 +137,7 @@ maildir_refile() {
134137
maildircheck "$dest" || maildirmake "$dest" || return 1
135138

136139
if command -v mrefile >/dev/null; then
140+
mblaze_ensure
137141
if [[ "$mode" == "copy" ]]; then
138142
mrefile -k "$msg" "$dest"
139143
else
@@ -240,6 +244,7 @@ deliver() {
240244

241245
# use mblaze if found
242246
command -v mdeliver > /dev/null && {
247+
mblaze_ensure
243248
last_deliver=`mdeliver -v "$MAILDIRS/$dest"`
244249
local ret=$?
245250
[[ $ret == 0 ]] || {

src/zlibs/parse

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
# extract all addresses found in a list of email files from stdin
2727
mblaze_extract_addresses() {
28+
mblaze_ensure
2829
_action="${1:-all}"
2930
case "${_action}" in
3031
sender)

0 commit comments

Comments
 (0)