#!/bin/sh
#
# notifynomailbox - Notify header part of bounced mail that deliverred
#                   to this host but no such mailbox here.
#
# Copyright (c) 2001-2002 SATOH Fumiyasu. All rights reserved.
# Date: 2002-01-25, since 2001-09-10
# Version: 1.0.6
# License: GNU General Public License ver.2
#
# Usage: in .qmail-default:
#        |notifynomailbox address ...
#        |bouncesaying "Sorry, no mailbox here by that name. (#5.1.1)"
#
# Options:
#    -s subject  Content of subject header field
#    -n          Print the notice message rather than sending
#
# $Id$

# FIXME: Add -b option to do not strip the original message body
# FIXME: Read bouncefrom and bouncehost control files?

# Default options
cmd_name=notifynomailbox
qmail_home=/var/qmail
me_file=$qmail_home/control/me
h_subject='nomailbox notice'

# Parse options
while getopts s:n opt; do
    case $opt in
    s)
	h_subject="$OPTARG"
	;;
    n)
	opt_n='-n'
	;;
    *)
	exit 0
	;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -eq 0 ]; then
    echo "$cmd_name: usage: $cmd_name address ..." 1>&2
    exit 0
fi
if [ ! -s $me_file ]; then
    echo "unable to read control $me_file" 1>&2
    exit 0
fi

# Do not notify if $SENDER is null to avoid looping
[ -z "$SENDER" ] && exit 0

# Make header fields
me=`head -1 $me_file`
h_from=MAILER-DAEMON@$me
h_to=''
for to in "$@"; do
    [ -n "$h_to" ] && h_to="$h_to, "
    h_to="$h_to$to"
done

# Make notice message and send it
(cat <<EOF_MAIL; awk '/^$/{exit} {print}') | \
qmail-inject $opt_n -f '' -- "$@"
From: $h_from
To: $h_to
Subject: $h_subject

Hi. This is the $cmd_name program at $me.
The qmail-send tried to deliver a message to local, but no such
mailbox on $me. This is a warning message for
postmaster. Bounce notice message was sent to envelope sender.

Sender: <$SENDER>
Recipient: <$RECIPIENT>

--- Below this line is a copy of the message (header only for privacy).

EOF_MAIL

exit 0
