Skip to content

Instantly share code, notes, and snippets.

@tiwo
Last active July 21, 2016 13:37
Show Gist options
  • Save tiwo/4673091 to your computer and use it in GitHub Desktop.
Save tiwo/4673091 to your computer and use it in GitHub Desktop.
delivering mail to a Maildir (in bash)
#!/bin/bash
#
# deliver-maildir -- safely deliver mail to a Maildir.
#
# For information on the Maildir convention, see
# http://en.wikipedia.org/wiki/Maildir
#
# This program is offered subject to the BSD three-clause license,
# see the bottom of this file for details.
set -e
umask 0037
verbose=""
usage() { # print a short explanation (for --help):
local me="`basename "$0"`"
cat <<EOF
usage: $me [--verbose] -- maildir
$me [--verbose] maildir
$me --help
$me
where 'maildir' is the path of a Maildir to deliver to. The first two forms
both deliver a mail read from standard input. Prefix the maildir path with an
argument consisting only of two hyphens ('--') if you are afraid the maildir
path could look like a switch (i.e., could start with '-').
Unless '--verbose' is given or errors arise, $me keeps quiet. '-v'
is allowed as a short form of '--verbose'. The delivery is safe, meaning that
if the underlying filesystem holds its promises, the whole mail will be in the
maildir after this script finishes successfully.
The third and fourth form print this help message and exit.
EOF
}
uniqid() { # print a unique id:
#echo -n $(( `date '+%s'` / 86400 )) # --> day number since epoch
tr -dc '0-9A-Za-z' < /dev/urandom | head -c 12 # --> [[:alnum:]]{12}
}
fail() { # print error message, then exit with error
echo "FATAL: $@" >&2
exit 1
}
warn() { # print warning message
echo "WARNING: $@" >&2
}
tests() { # test the maildir for sanity (existence, permissions)
local md="$1"
local id="$2"
[[ -d "$md" ]] || fail "destination Maildir \"$md\" does not exist"
[[ -d "$md/new" ]] || fail "destination Maildir \"$md/new\" does not exist"
[[ -d "$md/tmp" ]] || fail "destination Maildir \"$md/tmp\" does not exist"
[[ -d "$md/cur" ]] || warn "destination Maildir \"$md/cur\" does not exist"
[[ -w "$md/tmp" ]] || fail "no write permission for \"$md/tmp\""
[[ -w "$md/new" ]] || fail "no write permission for \"$md/new\""
[[ ! -e "$md/tmp/$id" ]] || fail "destination file \"$md/tmp/$id\" exists. This is a bug."
[[ ! -e "$md/new/$id" ]] || fail "destination file \"$md/new/$id\" exists. This is a bug."
}
main() {
case "$1" in
--) shift 1 ;;
--help | "") usage ; exit 0 ;;
--verbose | -v) verbose="yes" ; shift 1 ;;
-*) fail "unknown command-line switch $1" ;;
*) true ;;
esac
local md="$1"
local id=`uniqid`
[[ "$#" -eq 1 ]] || fail "need exactly one argument!"
tests "$md" "$id"
[[ -z "$verbose" ]] || echo "Give mail on standard input, end with EOF (Ctrl-D)..." >&2
cat > "$md/tmp/$id" || fail "write failed!"
[[ -z "$verbose" ]] || echo "Thanks. Now moving the file to \"$md/new/$id\"." >&2
mv -t "$md/new" "$md/tmp/$id" || fail "move failed!"
}
main "$@"
exit "$?"
# <LICENSE "BSD_3Clause">
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# (1) Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# (2) Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# (3)The name of the author may not be used to
# endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# </LICENSE>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment