#!/bin/sh
# $Id: t-pm-hdrmod-2.sh,v 1.4 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 header modifications: insert (created by pmilter)
# uses smtpcnf.sh.
# ----------------------------------------
#
test -s stop && exit 1
#

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

SCRIPTS=`cd ${SD} && pwd`
OUT="t-pm-hdrmod-0.out"

../smtps/smtps -VVV > ${OUT}
if ${GREP} 'MTA_USE_PMILTER' ${OUT} >/dev/null
then
  :
else
  echo "SKIPPED: $0: smtps not compiled with MTA_USE_PMILTER (--enable-pmilter)"
  exit 0
fi

OK="t-pm-hdrmod-0.ok"
PMPRG="-p ../libpmilter/t-pmilter-1"
PMOPT=""

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

# cd to right directory (where the programs will be executed).
cd ../qmgr || exit 1
rm -f ${MTMAP}

${CAT} > ${SMXCNF} <<EOF
qmgr { Log_Level = 12; wait_for_server=4; wait_for_client=4; }
smar { Log_Level = 12; nameserver = ${NS}; }
smtpc { Log_Level = 12; remote_port=${SNKPORT}; wait_for_server=4; }

smtps {
Log_Level = 19;
daemon_address = localhost:${SRVPORT};
wait_for_server=4;
policy_milter { socket {type=unix; path="pmilter.sock";} timeout=2; }
}
EOF

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

# ----------------------------------------
# insert header
TEST=1
if echo "${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
if ${SHELL} ${SCRIPTS}/smtpcnf.sh -3 lmtpsock ${PMPRG} ${PMOPT} -P "-i 1=HeaderIns:insert1" -Z 'smtpc2' -c '-E' -E 0 -A 1 -X 0 $@ -S"a@b.c" -R"user@local.dom"
then
  ERR=false
  if test -s ${B1}
  then
     ${EGREP} -v '^Date: ' ${B1} |\
	 ${SED} -e '/^Received:/,/^	id /d' \
		-e 's/
$//' -e 's/Mailer-Daemon@.*/Mailer-Daemon@/' > ${OUT}

     ${CAT} > ${OK} <<EOF
Return-Path: <a@b.c>
HeaderIns:insert1
From: me
To: you
Subject: test

body
.
EOF
    if ${DIFF} -w ${OK} ${OUT} >/dev/null
    then
      :
    else
      ERR=true
    fi
  else
     ERR=true
  fi

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

# ----------------------------------------
# insert header
TEST=2
if echo "${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
if ${SHELL} ${SCRIPTS}/smtpcnf.sh -3 lmtpsock ${PMPRG} ${PMOPT} -P "-i 2=HeaderIns:insert2" -Z 'smtpc2' -c '-E' -E 0 -A 1 -X 0 $@ -S"a@b.c" -R"user@local.dom"
then
  ERR=false
  if test -s ${B1}
  then
     ${EGREP} -v '^Date: ' ${B1} |\
	 ${SED} -e '/^Received:/,/^	id /d' \
		-e 's/
$//' -e 's/Mailer-Daemon@.*/Mailer-Daemon@/' > ${OUT}

     ${CAT} > ${OK} <<EOF
Return-Path: <a@b.c>
From: me
HeaderIns:insert2
To: you
Subject: test

body
.
EOF
    if ${DIFF} -w ${OK} ${OUT} >/dev/null
    then
      :
    else
      ERR=true
    fi
  else
     ERR=true
  fi

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


# ----------------------------------------
# prepend, insert, and append headers
TEST=3
if echo "${SKIP} X" | ${GREP} -v " ${TEST} " > /dev/null
then
if ${SHELL} ${SCRIPTS}/smtpcnf.sh -3 lmtpsock ${PMPRG} ${PMOPT} -P "-M p=HeaderPre:_prepend_3 -M a=HeaderApp:_append_4 -M p=HeaderPre2:_prepend_3_2 -M a=HeaderApp2:_append_4_2 -i 2=HeaderInsert:_insert_2 -i 3=HdrIns:_insert_header_at_pos_3" -Z 'smtpc2' -c '-E' -E 0 -A 1 -X 0 $@ -S"a@b.c" -R"user@local.dom"
then
  ERR=false
  if test -s ${B1}
  then
     ${EGREP} -v '^Date: ' ${B1} |\
	 ${SED} -e '/^Received:/,/^	id /d' \
		-e 's/
$//' -e 's/Mailer-Daemon@.*/Mailer-Daemon@/' > ${OUT}

     ${CAT} > ${OK} <<EOF
Return-Path: <a@b.c>
HeaderPre: prepend 3
HeaderPre2: prepend 3 2
From: me
HeaderInsert: insert 2
To: you
HdrIns: insert header at pos 3
Subject: test
HeaderApp: append 4
HeaderApp2: append 4 2

body
.
EOF
    if ${DIFF} -w ${OK} ${OUT} >/dev/null
    then
      :
    else
      ERR=true
    fi
  else
     ERR=true
  fi

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



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


syntax highlighted by Code2HTML, v. 0.9.1