commit 93a9cd76afbf0c5a990e6eba9a02e5dca1845d90
parent b4a09018a97353fff0d692e2ef5de425190b31ad
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Mon, 27 Feb 2023 10:54:13 -0800
Make incmail more versatile and add incmailauto script
Incmail now takes passwords from the standard input instead of only
allowing one password from a file. It is also capable of sending
the retrieved mail to other uses instead.
To allow non-interactive use, incmailauto was created. With no
passwords in it's database, it works like incmail (prompting the
user for a password), but when incmail runs successfully, the
password is stored in it's database and is used on sequential runs
for that user on that host.
Diffstat:
2 files changed, 82 insertions(+), 3 deletions(-)
diff --git a/local/bin/bin/incmail b/local/bin/bin/incmail
@@ -1,5 +1,5 @@
#!/bin/sh -e
-# Copyright 2022 Jacob R. Edwards
+# Copyright 2022, 2023 Jacob R. Edwards
# Sync mail over ssh
#
# If using a key, you may restrict it like so:
@@ -8,5 +8,34 @@
# The pop3 that's used here is one of my programs, look for it where
# you got this (assuming you got it from me).
-ssh -f -L 10110:localhost:110 "${@:-mail}" sleep 10
-pop3 -sdp 10110 localhost sendmail "$USER" < "${XDG_CONFIG_HOME:-$HOME/.config}"/incmail
+usage() {
+ echo "usage: ${0##*/} [-q] [-to user] [user@]host" 1>&2
+ exit 1
+}
+
+flags=v
+while test $# -gt 0
+do
+ case "$1" in
+ (-to)
+ to="$2"
+ shift
+ ;;
+ (-q)
+ flags=
+ ;;
+ (-*)
+ usage
+ ;;
+ (*)
+ break
+ esac
+ shift
+done
+
+test $# -ne 1 &&
+ usage
+
+ssh -o ExitOnForwardFailure=yes -f -L 10110:localhost:110 "$1" sleep 2.5 ||
+ exit
+pop3 -sdp 10110 ${flags:+-"$flags"} localhost sendmail "${to:-"$USER"}"
diff --git a/local/bin/bin/incmailaway b/local/bin/bin/incmailaway
@@ -0,0 +1,50 @@
+#!/bin/sh
+# Copyright 2023 Jacob R. Edwards
+#
+# Password manager for incmail
+
+set -ae
+
+getpw() {
+ awk -v key="$1" 'key == $1 {
+ pw = $2
+ exit
+ }
+ END {
+ if (pw)
+ print pw
+ else
+ exit 1
+ }'
+}
+
+readpw() {
+ if ! test -t 0; then
+ read -r "$@"
+ else {
+ stty -echo
+ echo -n 'Password: ' 1>&2
+ read -r "$@"
+ echo
+ stty echo
+ } 1>&2; fi
+}
+
+name=incmail
+host="$(eval echo -n "\${$#}")"
+
+pwdb=/tmp/$name/$(id -u)-pwdb
+mkdir -p /tmp/$name
+chmod 600 "$pwdb" >> "$pwdb"
+
+pw="$(getpw "$host" < "$pwdb")" || {
+ newpw=true
+ if ! readpw pw || ! test "$pw"
+ then
+ echo "$name: $host: Password not known" 1>&2
+ exit 1
+ fi
+}
+
+echo "$pw" | incmail -q "$@"
+${newpw:-false} && echo "$host" "$pw" >> "$pwdb"