##################
# ezmlm-moderate #
##################

prompt "ezmlm-moderate:       "

# make sure we get the (mis)accepted message(s)
${EZBIN}/ezmlm-sub "$DIR" "${SND}@$HOST"

# REJ is set in 145-ezmlm-store, as the reject address for "mod1" message
LOCAL=`echo "$REJ" | cut -d@ -f1`

qqclean
DEFAULT=`echo "$LOCAL" | cut -c"$REJLEN"-`
{
    make_message
    echo
    echo '> %%%'
    echo '> rejection test message'
    echo '> %%%'
} | ${EZBIN}/ezmlm-moderate "$DIR" "${EZBIN}/ezmlm-send ${DIR}" \
	>"$ERR" 2>&1 || \
		fatal "failed on rejection"
msgexists ||
	fatal "failed to send rejection notice for message mod1"
# ${SND}@$HOST means it was rejected, not send through the list
grep "To: ${SND}@$HOST" "$QQMSG" > /dev/null 2>&1 || \
	fatal "failed to reject message mod1"
grep '^rejection test message$' "$QQMSG" >/dev/null 2>&1 || \
	fatal "reject message did not include given text"
checkenv rejection "$LIST-return-@$HOST" "$SND@$HOST"

qqclean
# ACC is set in 145-ezmlm-store, as the accept address for "mod3" message
LOCAL=`echo "$ACC" | cut -d@ -f1`
DEFAULT=`echo "$LOCAL" | cut -c"$ACCLEN"-`
${EZBIN}/ezmlm-moderate "$DIR" ${EZBIN}/ezmlm-send "$DIR" \
	 </dev/null >"$ERR" 2>&1 || \
		fatal "failed on acceptance"
msgexists || \
	fatal "failed to post message mod3"
# ${LIST}@$HOST means it was not rejected, but sent through the list
grep "To: ${LIST}@$HOST" $QQMSG > /dev/null 2>&1 || \
	fatal "failed to accept message mod3"
checkenv accept "$LIST-return-.*-@$HOST-@\\[\\]" "$SND@$HOST" "$MAN@$HOST"

ls -l "${DIR}/mod/rejected/" | grep '[0-9]' >/dev/null 2>&1 || \
	fatal "failed to write reject stub"
ls -l "${DIR}/mod/accepted/" | grep '[0-9]' >/dev/null 2>&1 || \
	fatal "failed to write accept stub"

REJ1=`echo "$REJ" | sed s/reject/accept/`
LOCAL=`echo "$REJ1" | cut -d@ -f1`
DEFAULT=`echo "$LOCAL" | cut -c"$REJLEN"-`
${EZBIN}/ezmlm-moderate "$DIR" "${EZBIN}/ezmlm-send ${DIR}" \
	</dev/null >/dev/null 2>&1 && \
	fatal "failed to bounce accept of rejected message"

LOCAL=`echo "$REJ" | cut -d@ -f1`
DEFAULT=`echo "$LOCAL" | cut -c"$REJLEN"-`
${EZBIN}/ezmlm-moderate "$DIR" "${EZBIN}/ezmlm-send ${DIR}" \
	</dev/null >/dev/null 2>&1 || \
	fatal "failed to silently ignore re-rejection"

ACC1=`echo "$ACC" | sed s/accept/reject/`
LOCAL=`echo "$ACC1" | cut -d@ -f1`
DEFAULT=`echo "$LOCAL" | cut -c"$REJLEN"-`
${EZBIN}/ezmlm-moderate "$DIR" "${EZBIN}/ezmlm-send ${DIR}" \
	</dev/null >/dev/null 2>&1 && \
	fatal "failed to bounce reject of accepted message"

LOCAL=`echo "$ACC" | cut -d@ -f1`
DEFAULT=`echo "$LOCAL" | cut -c"$ACCLEN"-`
${EZBIN}/ezmlm-moderate "$DIR" "${EZBIN}/ezmlm-send ${DIR}" \
	</dev/null >/dev/null 2>&1 || \
	fatal "failed to silently ignore re-acceptance"

echo "OK"

# cleanup
${EZBIN}/ezmlm-unsub "$DIR" "${SND}@$HOST"
