#!/bin/sh
#
# Copyright 1998 - 2025 S. Varshavchik.  See COPYING for
# distribution information.

PWD=`pwd`

unset LC_CTYPE
unset LC_NUMERIC
unset LC_TIME
unset LC_COLLATE
unset LC_MONETARY
unset LC_MESSAGES
unset LC_PAPER
unset LC_NAME
unset LC_ADDRESS
unset LC_TELEPHONE
unset LC_MEASUREMENT
unset LC_IDENTIFICATION
unset LC_ALL
unset VERBOSE

exec 3>&2
if test "$VALGRIND" != ""
then
    VALGRIND="$VALGRIND --tool=memcheck $VALGRIND_EXTRA_OPTS --error-exitcode=1 --log-fd=3"
fi

function run() {

    LANG=en_US.utf-8 TZ=America/New_York $VALGRIND ./mailbot -X 60 "$@"
}

set -e

cat >testmailbot.1 <<EOF
From: =?iso-8859-1?Q?H=F3la!?= <test@example.com>
Return-Path: test2@example.com
Subject: Re: test message (fwd)
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1

Original
test
message
EOF

cat >testmailbot.msg <<EOF
Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8

autoreply text
EOF
run -m testmailbot.msg cat <testmailbot.1

cat >testmailbot.1 <<EOF
From: =?iso-8859-1?Q?H=F3la!?= sender <test@xn--80ah.example.com>
To: toaddress@example.com
Cc: ccaddress@example.com
X-Newsgroup: alt.test
Newsgroups: alt.test.1,alt.test.2
Message-Id: <message1@example.com>
Errors-To: test2@example.com
Date: Sun, 22 Nov 2009 11:57:32 -0400
Subject: Re: test message, =?iso-8859-1?Q?H=F3la!?= (fwd)
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1

Original
test
message
EOF
run -m testmailbot.msg cat <testmailbot.1
run -e -m testmailbot.msg \
    -S 'On %d,%nin %C (%N), %F (%f),%nin message %i, %s, writes:' \
    cat <testmailbot.1
run -T replyall -m testmailbot.msg cat <testmailbot.1
run -T replyall -N -m testmailbot.msg cat <testmailbot.1
run -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1
run -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

cat >testmailbot.1 <<EOF
Mime-Version: 1.0
From: nobody@example.com
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

H=F3la!
EOF

cat >testmailbot.msg <<EOF
Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8

autoreply text
EOF

echo "===================="
run -A 'X-Header1: Value' -A 'X-Header2: Value' -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1
echo "===================="
run -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

cat >testmailbot.1 <<EOF
Mime-Version: 1.0
From: nobody@example.com
Content-Type: multipart/alternative; boundary="zzz"

--zzz
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

H=F3la!

--zzz
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<h1>H=F3la!</h1>

--zzz--
EOF

echo "===================="
run -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1

echo "===================="
run -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

cat >testmailbot.1 <<EOF
Mime-Version: 1.0
From: nobody@example.com
Content-Type: multipart/mixed; boundary="zzz"

--zzz
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

H=F3la!

--zzz
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<h1>Attachment h=F3la!</h1>

--zzz--
EOF

echo "===================="
run -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

echo "===================="
run -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

cat >testmailbot.1 <<EOF
Mime-Version: 1.0
From: nobody@example.com
Subject: HTML message
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<h1>HTML message only</h1>
EOF

echo "===================="
run -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

echo "===================="
run -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

cat >testmailbot.1 <<EOF
Mime-Version: 1.0
From: nobody@example.com
Subject: multipart/alternative with attachment
Content-Type: multipart/mixed; boundary=zzz

--zzz
Content-Type: multipart/alternative; boundary=yyy


--yyy
Content-Type: text/plain; charset=iso-8859-1

Text version of an HTML message

--yyy
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

<h1>HTML message only</h1>

--yyy--

--zzz
Content-Type: text/plain

Text attachment

--zzz--
EOF

echo "===================="
run -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

echo "===================="
run -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

echo "===================="

run -T reply -m testmailbot.msg cat <<EOF
From: nobody@example.com
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Message
> Quote level 1
>> Quote level 2
>
 space
EOF

run -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <<EOF
From: nobody@example.com
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8

Message
> Quote level 1
>> Quote level 2
>
 space
EOF

cat >testmailbot.1 <<EOF
From: test@example.com
Return-Path: test2@example.com
Subject: Test message
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1

Original
test
message
EOF

cat >testmailbot.msg <<EOF
Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8
Content-Transfer-Encoding: 8bit

DSN message text
EOF

run -M nobody@example.com -N -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//;s/Arrival-Date:.*/Arrival-Date:/'

run -T replyfeedback -R abuse -n -N -m testmailbot.msg \
    --feedback-source-ip 127.0.0.1 \
    --feedback-incidents 2 \
    <testmailbot.1 >testmailbot.msgout

sed 's/=_.*//;s/Arrival-Date:.*/Arrival-Date:/;s/User-Agent:.*//' \
    <testmailbot.msgout

run -T feedback -R abuse -n -N -m testmailbot.msg \
    --feedback-source-ip 127.0.0.1 \
    --feedback-incidents 2 \
    <testmailbot.1 >testmailbot.msgout
sed 's/=_.*//;s/Arrival-Date:.*/Arrival-Date:/;s/User-Agent:.*//' \
    <testmailbot.msgout

if test "10" != "00" -a -x ../makedat/makedatprog
then
    cat >testmailbot.msg <<EOF
From: nobody@example.com
Subject: limited autoresponse

EOF
    echo "autoresponse" >testmailbot.msg2

    rm -f testmailbot.dat*

    run -T reply -d testmailbot.dat -t testmailbot.msg2 \
	      cat <testmailbot.msg >testmailbot.out

    if grep -q -F 'autoresponse' testmailbot.out
    then
	:
    else
	echo "-d test failed (part 1)"
    fi

    run -T reply -d testmailbot.dat -t testmailbot.msg2 \
	      cat <testmailbot.msg >testmailbot.out

    if grep -q -F 'autoresponse' testmailbot.out
    then
	echo "-d test failed (part 2)"
    fi

    run -T reply -d testmailbot.dat -t testmailbot.msg2 \
	      -D 0 cat <testmailbot.msg >testmailbot.out

    if grep -q -F 'autoresponse' testmailbot.out
    then
	:
    else
	echo "-d test failed (part 3)"
    fi

    rm -f testmailbot.dat* testmailbot.out
fi

echo "Autoresponse test" >testmailbot.msg2

cat >testmailbot.msg <<EOF
From: sender@example.com
To: mail@example.com
Subject: Autoresponse successful

Autoresponse test 1
EOF

run -T reply -r nobody@example.com,mail@example.com \
	  -t testmailbot.msg2 \
	  cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
To: something-else@example.com
Subject: Autoresponse failed

Autoresponse test 2 failed
EOF

run -T reply -r nobody@example.com,mail@example.com \
	  -t testmailbot.msg2 \
	  cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Cc: list@example.com,
  mail@example.com
Subject: Autoresponse successful

Autoresponse test 3
EOF

run -T reply -r nobody@example.com,mail@example.com \
	  -t testmailbot.msg2 \
	  cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Mime-Version: 1.0
Content-Type: MULTIPART/REPORT; ignore-this-is-fake
Subject: mailbot should ignore multipart/report

But it did not
EOF

run -T reply -t testmailbot.msg2 cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Mime-Version: 1.0
Precedence: junk
Subject: mailbot should ignore Precedence: junk

But it did not
EOF

run -T reply -t testmailbot.msg2 cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Mime-Version: 1.0
Auto-Submitted: yes
Subject: mailbot should ignore auto-submitted

But it did not
EOF

run -T reply -t testmailbot.msg2 cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Mime-Version: 1.0
Subject: original message is multipart/signed
Content-Type: multipart/signed; boundary="aaa"

--aaa
Content-Type: multipart/alternative; boundary="bbb"

--bbb
Content-Type: text/plain

Original message

--bbb--

--aaa
Content-Type: application/signature

signature

--aaa--
EOF

run -T reply -t testmailbot.msg2 cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Mime-Version: 1.0
Message-ID: <21@example.com>
References: <1@example.com>,
	    <2@example.com>,
	    <3@example.com>,
	    <4@example.com>,
	    <5@example.com>,
	    <6@example.com>,
	    <7@example.com>,
	    <8@example.com>,
	    <9@example.com>,
	    <10@example.com>,
	    <11@example.com>,
	    <12@example.com>,
	    <13@example.com>,
	    <14@example.com>,
	    <15@example.com>,
	    <16@example.com>,
	    <17@example.com>,
	    <18@example.com>,
	    <19@example.com>,
	    <20@example.com>
Subject: original message is a pgp message decoded by sqwebmail
Content-Type: multipart/x-mimegpg; boundary="aaa"

--aaa
Content-Type: text/x-gpg-output

PGP output log

--aaa
Content-Type: multipart/alternative; boundary="bbb"

--bbb
Content-Type: text/plain

Original message

--bbb--

--aaa--
EOF

run -T reply -t testmailbot.msg2 cat <testmailbot.msg

cat >testmailbot.msg <<EOF
From: nobody@example.com
Mime-Version: 1.0
X-Comment: original message
Subject: original message that will get a vacation autoresponse
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

h=F3la!
EOF

rm -rf testmailbot.msgmaildir
mkdir testmailbot.msgmaildir
mkdir testmailbot.msgmaildir/cur
mkdir testmailbot.msgmaildir/new
mkdir testmailbot.msgmaildir/tmp

cat >testmailbot.msgmaildir/cur/msg1:2, <<EOF
Mime-Version: 1.0
X-Comment: template 1
Content-Type: text/plain; charset=iso-8859-1
Subject: message 1

message 1 h=F3la!
EOF

touch --date='2025-01-01' testmailbot.msgmaildir/cur/msg1:2,

cat >testmailbot.msgmaildir/new/msg0:2, <<EOF
Mime-Version: 1.0
X-Comment: template 2
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
Subject: message 2

message 2 h=F3la!
EOF

run -T replydraft -l testmailbot.msgmaildir cat <testmailbot.msg \
    >testmailbot.msgout

sed 's/boundary=.*//;s/^--.*/--/' <testmailbot.msgout

rm -rf testmailbot.msg*
rm -f testmailbot.1

cat >testmailbot.msg <<EOF
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes

EOF

echo 'This is a flowed message. This is a flowed message. This is a flowed message.  ' >>testmailbot.msg
echo 'This is a flowed message. This is a flowed message. This is a flowed message.  ' >>testmailbot.msg
echo 'This is a flowed message. This is a flowed message.' >>testmailbot.msg

run -T reply cat <testmailbot.msg
rm -f testmailbot.msg
