update mail paste service with some improvements - www.codemadness.org - www.codemadness.org saait content files
(HTM) git clone git://git.codemadness.org/www.codemadness.org
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 7585493c071d815494da5d3e517f4eed25232996
(DIR) parent 35eb34dbf1708b5f550bd2beb089a2e10d26814a
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 30 Oct 2023 19:42:17 +0100
update mail paste service with some improvements
Diffstat:
M output/atom.xml | 2 +-
M output/atom_content.xml | 136 +++++++++++++++++--------------
M output/rss_content.xml | 134 +++++++++++++++++--------------
M output/sitemap.xml | 2 +-
M pages/mailservice.cfg | 2 +-
M pages/mailservice.md | 131 +++++++++++++++++--------------
6 files changed, 222 insertions(+), 185 deletions(-)
---
(DIR) diff --git a/output/atom.xml b/output/atom.xml
@@ -10,7 +10,7 @@
<title>Setup your own mail paste service</title>
<link rel="alternate" type="text/html" href="https://www.codemadness.org/mailservice.html" />
<id>https://www.codemadness.org/mailservice.html</id>
- <updated>2023-10-25T00:00:00Z</updated>
+ <updated>2023-10-30T00:00:00Z</updated>
<published>2023-10-25T00:00:00Z</published>
<author>
<name>Hiltjo</name>
(DIR) diff --git a/output/atom_content.xml b/output/atom_content.xml
@@ -10,7 +10,7 @@
<title>Setup your own mail paste service</title>
<link rel="alternate" type="text/html" href="https://www.codemadness.org/mailservice.html" />
<id>https://www.codemadness.org/mailservice.html</id>
- <updated>2023-10-25T00:00:00Z</updated>
+ <updated>2023-10-30T00:00:00Z</updated>
<published>2023-10-25T00:00:00Z</published>
<author>
<name>Hiltjo</name>
@@ -18,7 +18,7 @@
</author>
<summary>Setup your own mail paste service using mblaze</summary>
<content type="html"><![CDATA[<h1>Setup your own mail paste service</h1>
- <p><strong>Last modification on </strong> <time>2023-10-25</time></p>
+ <p><strong>Last modification on </strong> <time>2023-10-30</time></p>
<h2>How it works</h2>
<ul>
<li>The user sends a mail with an attachment to a certain mail address, for
@@ -29,11 +29,9 @@ shellscript.</li>
</ul>
<h2>What it does</h2>
<ul>
-<li>Process a mail with an attachment automatically. In this example only one
-attachment is supported.</li>
-<li>The script processes the attachment in the mail and stores it.</li>
-<li>It will mail (back) the URL where the file is stored, for now to one static
-mail address.</li>
+<li>Process a mail with the attachments automatically.</li>
+<li>The script processes the attachments in the mail and stores them.</li>
+<li>It will mail (back) the URL where the file(s) are stored.</li>
</ul>
<p>This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the
gopher daemon geomyidae.</p>
@@ -51,83 +49,97 @@ described below. Copy the below contents in /usr/local/bin/paste-mail</p>
<pre><code>#!/bin/sh
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")
+
+# check if allowed or not.
+case "$from" in
+"hiltjo@codemadness.org")
+ ;;
+*)
+ exit 0;;
+esac
# 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";;
-*)
- ext="bin";;
-esac
-
-file="$tmpfile.$ext"
-mv "$tmpfile" "$file"
-b=$(basename "$file")
-
-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
</code></pre>
<p>The mail daemon processing the mail needs of course to be able to have
permissions to write to the specified directory. The user who received the mail
needs to be able to read it from a location they can access and have
permissions for it also.</p>
<h2>Room for improvements</h2>
-<p>Some ideas for improvements:</p>
-<ul>
-<li>Handle multiple attachments.</li>
-<li>Handle mailing back to more users, while using a safe (whitelist?) mechanism
-to prevent spam bots etc.</li>
-<li>Handling of more filetypes and file extension in a smarter way.</li>
-<li>Modify it to any preferences you might have.</li>
-</ul>
+<p>This is just an example script. There is room for many improvements.
+Feel free to modify it to any preferences you might have.</p>
<h2>References</h2>
<ul>
<li><a href="https://man.openbsd.org/aliases">https://man.openbsd.org/aliases</a></li>
(DIR) diff --git a/output/rss_content.xml b/output/rss_content.xml
@@ -13,7 +13,7 @@
<dc:date>2023-10-25T00:00:00Z</dc:date>
<author>Hiltjo</author>
<description><![CDATA[<h1>Setup your own mail paste service</h1>
- <p><strong>Last modification on </strong> <time>2023-10-25</time></p>
+ <p><strong>Last modification on </strong> <time>2023-10-30</time></p>
<h2>How it works</h2>
<ul>
<li>The user sends a mail with an attachment to a certain mail address, for
@@ -24,11 +24,9 @@ shellscript.</li>
</ul>
<h2>What it does</h2>
<ul>
-<li>Process a mail with an attachment automatically. In this example only one
-attachment is supported.</li>
-<li>The script processes the attachment in the mail and stores it.</li>
-<li>It will mail (back) the URL where the file is stored, for now to one static
-mail address.</li>
+<li>Process a mail with the attachments automatically.</li>
+<li>The script processes the attachments in the mail and stores them.</li>
+<li>It will mail (back) the URL where the file(s) are stored.</li>
</ul>
<p>This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the
gopher daemon geomyidae.</p>
@@ -46,83 +44,97 @@ described below. Copy the below contents in /usr/local/bin/paste-mail</p>
<pre><code>#!/bin/sh
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")
+
+# check if allowed or not.
+case "$from" in
+"hiltjo@codemadness.org")
+ ;;
+*)
+ exit 0;;
+esac
# 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";;
-*)
- ext="bin";;
-esac
-
-file="$tmpfile.$ext"
-mv "$tmpfile" "$file"
-b=$(basename "$file")
-
-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
</code></pre>
<p>The mail daemon processing the mail needs of course to be able to have
permissions to write to the specified directory. The user who received the mail
needs to be able to read it from a location they can access and have
permissions for it also.</p>
<h2>Room for improvements</h2>
-<p>Some ideas for improvements:</p>
-<ul>
-<li>Handle multiple attachments.</li>
-<li>Handle mailing back to more users, while using a safe (whitelist?) mechanism
-to prevent spam bots etc.</li>
-<li>Handling of more filetypes and file extension in a smarter way.</li>
-<li>Modify it to any preferences you might have.</li>
-</ul>
+<p>This is just an example script. There is room for many improvements.
+Feel free to modify it to any preferences you might have.</p>
<h2>References</h2>
<ul>
<li><a href="https://man.openbsd.org/aliases">https://man.openbsd.org/aliases</a></li>
(DIR) diff --git a/output/sitemap.xml b/output/sitemap.xml
@@ -2,7 +2,7 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.codemadness.org/mailservice.html</loc>
- <lastmod>2023-10-25</lastmod>
+ <lastmod>2023-10-30</lastmod>
</url>
<url>
<loc>https://www.codemadness.org/todo-application.html</loc>
(DIR) diff --git a/pages/mailservice.cfg b/pages/mailservice.cfg
@@ -3,4 +3,4 @@ id = mailservice
description = Setup your own mail paste service using mblaze
keywords = mail, paste, service
created = 2023-10-25
-updated = 2023-10-25
+updated = 2023-10-30
(DIR) diff --git a/pages/mailservice.md b/pages/mailservice.md
@@ -9,11 +9,9 @@
## What it does
-* Process a mail with an attachment automatically. In this example only one
- attachment is supported.
-* The script processes the attachment in the mail and stores it.
-* It will mail (back) the URL where the file is stored, for now to one static
- mail address.
+* Process a mail with the attachments automatically.
+* The script processes the attachments in the mail and stores them.
+* It will mail (back) the URL where the file(s) are stored.
This script is tested on OpenBSD using OpenBSD smtpd and OpenBSD httpd and the
gopher daemon geomyidae.
@@ -42,69 +40,89 @@ Script:
#!/bin/sh
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")
+
+ # check if allowed or not.
+ case "$from" in
+ "hiltjo@codemadness.org")
+ ;;
+ *)
+ exit 0;;
+ esac
# 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";;
- *)
- ext="bin";;
- esac
-
- file="$tmpfile.$ext"
- mv "$tmpfile" "$file"
- b=$(basename "$file")
-
- 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
The mail daemon processing the mail needs of course to be able to have
@@ -115,13 +133,8 @@ permissions for it also.
## Room for improvements
-Some ideas for improvements:
-
-* Handle multiple attachments.
-* Handle mailing back to more users, while using a safe (whitelist?) mechanism
- to prevent spam bots etc.
-* Handling of more filetypes and file extension in a smarter way.
-* Modify it to any preferences you might have.
+This is just an example script. There is room for many improvements.
+Feel free to modify it to any preferences you might have.
## References