~miconoco/send-files

982ffe5f7d1a2946ce23f5e96f835dae08de77d4 — Christoph Polcin 4 years ago 87ed3eb
add send-files
1 files changed, 78 insertions(+), 0 deletions(-)

A send-files
A send-files => send-files +78 -0
@@ 0,0 1,78 @@
#!/bin/sh

set -efu

COMP="${COMP:-tar.gz}"
SUBF="${SUBF:-send-files}"
SEND="${SEND:-"msmtp -t"}"
ALGO="${ALGO:-AES256}"

_usage() {
	cat <<-EOF
	usage: $(basename "$0") [email] [subject] <FILE>...

	ENV:
	  ALGO   Cipher Algo, current: ${ALGO}
	         see gpg --version
	  COMP   tar auto compression via suffix, current: ${COMP}
	  SEND   sendmail compatible command, current: ${SEND}
	  SUBF   pass subfolder, current: ${SUBF}
	EOF
	exit 1
}

[ $# -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] && _usage

if [ -z "${1##*@*}" ]; then
	_to="$1"
	shift
else
	_to="$(pass "${SUBF}" | cut -s -d' ' -f 2 | fzy)"
fi

if [ -d "$1" ]; then
	_subject="$(cd "$1" || exit 1; pwd)"
	_subject="${_subject##*/}"
elif [ -f "$1" ]; then
	_subject="$1"
else
	_subject="$1"
	shift
fi

[ $# -lt 1 ] || [ ! -e "$1" ] && _usage

# TODO remove tmp files and stream directly

_tmp="$(mktemp --suffix=".${COMP}")"
_out="${_tmp}.gpg"
trap 'rm -- "$_tmp" "$_out"' EXIT

tar --sparse \
	--no-acls --no-xattrs \
	--exclude-caches --exclude-vcs-ignores \
	--auto-compress \
	-cvf "$_tmp" "$@"

pass "$SUBF/$_to" | gpg \
	--batch --passphrase-fd 0 \
	--cipher-algo "$ALGO" \
	--sign \
	--symmetric \
	--no-symkey-cache -o "${_out}" "$_tmp"

_fname="$(echo "$_subject" | sed 's/[^-[:alnum:]]\+/-/g')"
_fname="${_fname#-}"
_fname="$(date +%F-%H%M)-${_fname%-}.${COMP}.gpg"

cat <<EOF| $SEND
To: ${_to}
Subject: ${_subject}
Mime-Version:1.0
Content-Type: $(mimetype -b "${_out}"); name="${_fname}"
Content-Description: ${_fname}
Content-Disposition: attachment; filename="${_fname}"
Content-Transfer-Encoding: base64

$(openssl base64 -e -in "$_out")
EOF