#!/bin/sh
# $Id: t-delay-3.sh,v 1.9 2007/01/11 02:00:26 ca Exp $
# Copyright (c) 2005 Sendmail, Inc. and its suppliers.
#	All rights reserved.
#
# By using this file, you agree to the terms and conditions set
# forth in the LICENSE file which can be found at the top level of
# the sendmail distribution.
#
# ----------------------------------------
# test delay message; including text of bounces delivered via lmtp to smtps3
# deliver original message too
# run smtp1.sh with different parameters
# ----------------------------------------
#
test -s stop && exit 1
#

if test X"${MTA_NO_SLOW_TEST}" != "X"
then
  echo "SKIPPED: $0: MTA_NO_SLOW_TEST is set"
  exit 0
fi

SD=`dirname $0`
if test "${SD}" = "."
then
   SD=../chkmts
fi
. ${SD}/common.sh

I=""
ERRS=0
OK="t-delay-3.ok"
OUT="t-delay-3.out"
TMP="t-delay-3.tmp"
EDBROK="t-delay-3.edbr"

if test X"`uname`" != "XOSF1"
then
while getopts s:V FLAG
do
  case "${FLAG}" in
    s) SKIP="${SKIP} ${OPTARG}";;
    V) VERBOSE=true;;
  esac
done
shift `expr ${OPTIND} - 1`
fi

# cd to working directory for alias file!
cd ../qmgr || exit 1

if ./qmgr -VVV 2>/dev/null | ${GREP} MTA_DELAYED_DSN >/dev/null
then
  :
else
  echo "SKIPPED: $0: qmgr not compiled with MTA_DELAYED_DSN"
  exit 0
fi

# create aliases
${CAT} > ${ALI} <<EOF
abuse:	local:
postmaster:	local:
user:	local:
sender: local:
EOF
# create map
rm -f ${ALIMAP}
${MM} < ${ALI}
rm -f ${MTMAP}

# run some test programs

# ----------------------------------------
# temporary failure from SMTP sink; create a warning DSN; deliver both
# check content
TEST=1
if echo "${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
ERR=false
${CAT} > ${EDBROK} <<EOF
EOF
if ${SHELL} ${SD}/smtp1.sh -Y '-o 40' -b -O ${EDBROK} -3 lmtpsock -Ssender@local.dom -R451@local.host -Q '-T w=8' -i 10 -s 80 -E 3 -X 1 -A 1
then
  if test -s ${B1}
  then
     ${EGREP} -v '^Date: ' ${B1} |\
	 ${SED} -e '/^Received:/,$d' \
		-e 's/
$//' -e 's/Mailer-Daemon@.*/Mailer-Daemon@/' > ${OUT}

     ${CAT} > ${OK} <<EOF
Return-Path: <>
From: Mailer-Daemon@
Subject: Delayed mail
To: <sender@local.dom>

${DSN_DEL}

Recipient:
<451@local.host>
Remote-MTA: 127.0.0.1
Reason:
451 whatever
during RCPT


Your original mail follows:
EOF
    if ${DIFF} -w ${OK} ${OUT} >/dev/null
    then
      ls ?/S0* > ${TMP} 2>/dev/null
      if test -s ${TMP}
      then
        ${VERBOSE} && echo "$0: CDB entry still exists"
        ERR=true
      fi
    else
      ERR=true
    fi

  else
     ERR=true
  fi
else
  ERR=true
fi
if ${ERR}
then
   echo "$0: test ${TEST} failed"
   ERRS=`expr ${ERRS} + 1 `
   test X"${MTA_STOPONERROR}" != X && exit 1
fi
fi


# ----------------------------------------
# end
if test "${ERRS}" = "0"
then
  rm -f ${EDBROK}
  exit 0
else
  echo "${ERRS} error(s)"
  exit 1
fi


syntax highlighted by Code2HTML, v. 0.9.1