Skip to content

Instantly share code, notes, and snippets.

@Jayman2000
Last active September 11, 2023 16:09
Show Gist options
  • Save Jayman2000/30fb19578ad2dd978b9420b9739ec915 to your computer and use it in GitHub Desktop.
Save Jayman2000/30fb19578ad2dd978b9420b9739ec915 to your computer and use it in GitHub Desktop.
A script used to help test a resholve PR: <https://github.com/abathur/resholve/pull/103>
#!/usr/bin/env bash
# 🅭🄍1.0 This file is dedicated to the public domain using the CC0 1.0 Universal
# Public Domain Dedication <https://creativecommons.org/publicdomain/zero/1.0/>.
set -u
# Note: none of these variables should contain whitespace.
readonly file=/dev/null
readonly domain=jasonyundt.email
readonly name=foo
readonly email="$name@$domain"
readonly number=1234
readonly cmd_arg=hello
# This is example.org’s fingerprint.
readonly tls_fingerprint="5E:F2:F2:14:26:0A:B8:F5:8E:55:EE:A4:2E:4A:C0:4B:0F:17:18:07:D8:D1:18:5F:DD:D6:74:70:E9:AB:60:96"
# These arguments were found in msmtp(1) from Nixpkgs’s msmtp-1.8.22
# package.
readonly pretend_args=( -P --pretend )
readonly other_args=(
# General options
--version
--help
-v -d --debug
# Changing the mode of operation
--configure="$email"
-S --serverinfo
--rmqs="$domain"
# Configuration options
"-C $file" --file="$file"
"-a $name" --account="$name"
--host="$domain"
--port="$number"
--source-ip=::
--proxy-host="$domain"
--proxy-port="$number"
--socket="$file"
--timeout=off --timeout="$number"
--protocol=smtp --protocol=lmtp
--domain="$domain"
--auth --auth=on --auth=off
--user="$name"
# TODO: Can I get this to work?
#--passwordeval= --passwordeval="$cmd_arg"
--passwordeval="$cmd_arg"
--tls --tls=on --tls=off
--tls-starttls --tls-starttls=on --tls-starttls=off
--tls-trust-file="$file"
--tls-crl-file="$file"
--tls-fingerprint="$tls_fingerprint"
--tls-key-file="$file"
--tls-cert-file="$file"
--tls-certcheck --tls-certcheck=on --tls-certcheck=off
--tls-priorities=SYSTEM
--tls-host-override="$domain"
# According to msmtp(1), this option “[sets] or unset[s] minimum
# bit size of the Diffie-Hellman (DH) prime.” I’m assuming that
# putting no number after the equals is how you unset it, but
# I’m just guessing.
--tls-min-dh-prime-bits= --tls-min-dh-prime-bits="$number"
# Options specific to sendmail mode
"-f $email" --from="$email"
"-N off" "-N never" --dsn-notify=off --dsn-notify=never
"-R off" "-R full" --dsn-return=off --dsn-return=full
--set-from-header --set-from-header=auto --set-from-header=on --set-from-header=off
--set-date-header --set-date-header=auto --set-date-header=off
--set-msgid-header --set-msgid-header=auto --set-msgid-header=off
--remove-bcc-headers --remove-bcc-headers=on --remove-bcc-headers=off
--undisclosed-recipients --undisclosed-recipients=on --undisclosed-recipients=off
-X "-X $file" --logfile= --logfile="$file"
--logfile-time-format= --logfile-time-format="%b%d%H:%M:%S"
--syslog --syslog=on --syslog=off --syslog=LOG_MAIL
-t --read-recipients
--read-envelope-from
--aliases= --aliases="$file"
-Fbar
--auto-from --auto-from=on --auto-from=off
--maildomain= --maildomain="$domain"
# Options that are allowed, but ignored:
-Bfoo -Bbar
-bm
-G
-h0 -h1
-i
"-L foo" "-L bar"
-m
-n
"-O foo=bar" "-O bar=baz"
"-o0 foo" "-o1 bar"
)
readonly msmtpq_only_args=(
"--q-mgmt -r"
"--q-mgmt -R"
"--q-mgmt -d"
"--q-mgmt -p"
"--q-mgmt -a"
"--q-mgmt -h"
)
function echo_example_email_function
{
echo function example_email
echo '{'
# MIME requires CRLF. See
# <https://www.rfc-editor.org/rfc/rfc5322.html#section-2.1>.
echo -E echo -ne "'To: [email protected]\r\n'"
echo -E echo -ne "'MIME-Version: 1.0 (mime-construct 1.11)\r\n'"
echo -E echo -ne "'\r\n'"
echo '}'
}
function echo_check_exit_status_function
{
echo function check_exit_status
echo '{'
echo 'if [ "$?" -eq 64 ]'
echo then
echo 'echo msmtp was given bad arguments. This should never happen. 1>&2'
echo 'exit 1'
echo fi
echo '}'
}
function echo_msmtp_command
{
local -r cmd="$1"
local -r pretend_arg="$2"
local -r other_arg="$3"
echo -nE "example_email | $cmd $pretend_arg $other_arg"
local add_recipients=true
for args_that_cant_have_recipients in "-S" "--serverinfo" "--rmqs"
do
if [[ "$other_arg" == "$args_that_cant_have_recipients"* ]]
then
add_recipients=false
break
fi
done
if "$add_recipients"
then
echo -nE " -- --passwordeval=$cmd_arg --passwordeval=$cmd_arg --passwordeval=$cmd_arg"
fi
echo
echo check_exit_status
}
{
echo "#!/usr/bin/env nix-shell"
echo -E "#! nix-shell -i bash -p msmtp -p $cmd_arg"
echo "set -x"
echo
echo_example_email_function
echo
echo_check_exit_status_function
echo
for cmd in msmtp msmtpq
do
for pretend_arg in "${pretend_args[@]}"
do
for other_arg in "${other_args[@]}"
do
echo_msmtp_command "$cmd" "$pretend_arg" "$other_arg"
# Try replacing the equals sign with a space.
# --tls-min-dh-prime-bits= is an exception. To
# unset --tls-min-dh-prime-bits, you need to
# use an equals sign.
if [[ "$other_arg" == *=* ]] && [[ "$other_arg" != "--tls-min-dh-prime-bits=" ]]
then
other_arg="$(printf '%s' "$other_arg" | tr = " ")"
echo_msmtp_command "$cmd" "$pretend_arg" "$other_arg"
fi
done
done
if [ "$cmd" = msmtpq ]
then
for msmtpq_only_arg in "${msmtpq_only_args[@]}"
do
echo -E "example_email | $cmd $msmtpq_only_arg"
echo check_exit_status
done
fi
done
echo
echo echo "'Finished!'"
} | tee ./msmtp-test.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment