mail-alias-paste-service: improvements - randomcrap - random crap programs of varying quality
(HTM) git clone git://git.codemadness.org/randomcrap
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 1592a2f1945ebb568608a0a41a8a44473b6d9d9c
(DIR) parent e919b4388786321584392aad54286e5c4645f618
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 30 Oct 2023 19:35:32 +0100
mail-alias-paste-service: improvements
- Handle multiple attachments.
- Some small whitelist logic.
- Try to use the same extension of the filename, in the reply show the original filename.
- Remove the file -i -b mime-type matching (the glob logic was incorrect too).
Diffstat:
M mail-alias-paste-service | 121 +++++++++++++++++--------------
1 file changed, 68 insertions(+), 53 deletions(-)
---
(DIR) diff --git a/mail-alias-paste-service b/mail-alias-paste-service
@@ -1,71 +1,86 @@
#!/bin/sh
-# Mail alias:
-# paste: |/usr/local/bin/paste-mail
-#
-# Dependencies:
-# mblaze
d="/home/www/domains/www.codemadness.org/htdocs/mailpaste"
-tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX)
+tmpmsg=$(mktemp)
tmpmail=$(mktemp)
-# store mail temporarily, on exit remove temporary file.
-trap "rm -f $tmpmail" EXIT
+cleanup() {
+ rm -f "$tmpmail" "$tmpmsg"
+}
+
+# store whole mail from stdin temporarily, on exit remove temporary file.
+trap "cleanup" EXIT
cat > "$tmpmail"
-# don't store mail sequence.
+# mblaze: don't store mail sequence.
MAILSEQ=/dev/null
export MAILSEQ
-# get from address.
-from=$(maddr -h 'From' /dev/stdin < "$tmpmail")
+# get from address (without display name).
+from=$(maddr -a -h 'From' /dev/stdin < "$tmpmail")
-# prevent mail loop.
-if printf '%s' "$from" | grep -q "paste@"; then
- exit 0
-fi
-
-# extract (first) attachment.
-mshow -n -O /dev/stdin "*" < "$tmpmail" > "$tmpfile"
-
-# determine extension by mimetype.
-mimetype=$(file -b -i "$tmpfile")
-case "$mimetype" in \
-"application/zip")
- ext="zip";;
-"application/pdf")
- ext="pdf";;
-"*/png")
- ext="png";;
-"*/gif")
- ext="gif";;
-"text/*")
- ext="txt";;
+# check if allowed or not.
+case "$from" in
+"hiltjo@codemadness.org")
+ ;;
*)
- ext="bin";;
+ exit 0;;
esac
-file="$tmpfile.$ext"
-mv "$tmpfile" "$file"
-b=$(basename "$file")
+# prevent mail loop.
+if printf '%s' "$from" | grep -q "pasty@"; then
+ exit 0
+fi
-chmod 666 "$file"
+echo "Thank you for using the enterprise paste service." > "$tmpmsg"
+echo "" >> "$tmpmsg"
+echo "Your file(s) are available at:" >> "$tmpmsg"
+echo "" >> "$tmpmsg"
+
+# process each attachment.
+mshow -n -q -t /dev/stdin < "$tmpmail" | sed -nE 's@.*name="(.*)".*@\1@p' | while read -r name; do
+ test "$name" = "" && continue
+
+ # extract attachment.
+ tmpfile=$(mktemp -p "$d" XXXXXXXXXXXX)
+ mshow -n -O /dev/stdin "$name" < "$tmpmail" > "$tmpfile"
+
+ # use file extension.
+ ext="${name##*/}"
+ case "$ext" in
+ *.tar.*)
+ # special case: support .tar.gz, tar.bz2, etc.
+ ext="tar.${ext##*.}";;
+ *.*)
+ ext="${ext##*.}";;
+ *)
+ ext="";;
+ esac
+ ext="${ext%%*.}"
+
+ # use file extension if it is set.
+ outputfile="$tmpfile"
+ if test "$ext" != ""; then
+ outputfile="$tmpfile.$ext"
+ fi
+ mv "$tmpfile" "$outputfile"
+ b=$(basename "$outputfile")
+
+ chmod 666 "$outputfile"
+ url="gopher://codemadness.org/9/mailpaste/$b"
+
+ echo "$name:" >> "$tmpmsg"
+ echo " Text file: gopher://codemadness.org/0/mailpaste/$b" >> "$tmpmsg"
+ echo " Image file: gopher://codemadness.org/I/mailpaste/$b" >> "$tmpmsg"
+ echo " Binary file: gopher://codemadness.org/9/mailpaste/$b" >> "$tmpmsg"
+ echo "" >> "$tmpmsg"
+done
+
+echo "" >> "$tmpmsg"
+echo "Sincerely," >> "$tmpmsg"
+echo "Your friendly paste_bot" >> "$tmpmsg"
# mail back the user.
+mail -r "$from" -s "Your files" "$from" < "$tmpmsg"
-url="gopher://codemadness.org/9/mailpaste/$b"
-
-mail -r hiltjo@codemadness.org -s "Your file: $url" "$from" <<!__EOF__
-Thank you for using the enterprise paste service.
-
-Your file is available at:
-
-Text file: gopher://codemadness.org/0/mailpaste/$b
-Image file: gopher://codemadness.org/I/mailpaste/$b
-Binary file: gopher://codemadness.org/9/mailpaste/$b
-
-Sincerely,
-Your friendly paste_bot
-!__EOF__
-
-rm -f "$tmpmail"
+cleanup