#!/bin/sh
#
# Copyright 1998 - 2011 Double Precision, Inc.  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

if test "01" != "00" -a -x ../makedat/makedatprog
then

cat <<EOF >testsuite.chk
foo	bar
foo.bar.com	foobar
foobar@foo.bar.com	barfoo
.
EOF
../makedat/makedatprog testsuite.chk testsuite.tmp testsuite.dat

cat <<EOF >testsuite.recipe
if ( gdbmopen("$PWD/testsuite.dat", "R") != 0 )
{
	exit
}

FOO=gdbmfetch("foo")
echo "FOO: \$FOO"

FOO=gdbmfetch("foobar@foo.bar.com", "D")
echo "FOO: \$FOO"
FOO=gdbmfetch("foobaz@foo.bar.com", "D")
echo "FOO: \$FOO"
EOF

chmod go-rw testsuite.recipe
cat <<EOF >testsuite.chk
FOO: bar
FOO: barfoo
FOO: foobar
EOF
./maildrop ./testsuite.recipe </dev/null | diff - testsuite.chk

fi


rm -rf testsuite.maildir
../maildir/maildirmake ./testsuite.maildir

cat <<EOF >testsuite.recipe

FLAGS="S"
to "$PWD/testsuite.maildir"
EOF

chmod go-rw testsuite.recipe

echo "To: testsuite" | ./maildrop ./testsuite.recipe

echo "2,S" >testsuite.chk

ls testsuite.maildir/cur | sed 's/.*://' | diff -U 3 - testsuite.chk

if test "@maildirquota@" = 1
then
	rm -rf testsuite.maildir
	../maildir/maildirmake ./testsuite.maildir

	cat <<EOF >testsuite.recipe

	MAILDIRQUOTA="1C"
	exception {
		cc "$PWD/testsuite.maildir"
		echo "Delivered message one"
		cc "$PWD/testsuite.maildir"
		echo "Delivered message two"
	}
	echo "Done."
	exit
EOF

	chmod go-rw testsuite.recipe
	cat <<EOF >testsuite.chk
Delivered message one
Done.
EOF
	echo "To: testsuite" | ./maildrop ./testsuite.recipe 2>/dev/null \
		| diff - testsuite.chk
fi

cat <<EOF >testsuite.recipe
if ( 2 && 3 )
{
     echo "Yes1"
}

if ( 2 && 0 )
{
     echo "No1"
}

if ( 2 & 1 )
{
     echo "No2"
}

if ( 2 & 3 )
{
     echo "Yes2"
}

a=(4 || 2)

echo "Four: \$a"

a=(4 | 2)

echo "Six: \$a"

a=(5 && 3)

echo "Three: \$a"

a=(5 & 3)

echo "One: \$a"

a=! 5

echo "Zero: \$a"

a= ~5

echo "Minus six: \$a"

EOF

chmod go-rw testsuite.recipe
./maildrop ./testsuite.recipe </dev/null


./reformail -s /bin/bash -c 'cat >testmsg.$FILENO' <<EOF

From message1
From: nobody@example.com
Subject: message1

message1

From message2
From: nobody@example.com
Subject: message2

message2
EOF

./reformail -x subject: <testmsg.000
./reformail -x subject: <testmsg.001
rm testmsg.00[01]

rm -f testcache.dat
./reformail -D 2000 testcache.dat <<EOF
Subject: dummy
Message-ID: <1>

EOF
echo "Not dupe: $?"
./reformail -D 2000 testcache.dat <<EOF
Subject: dummy
Message-ID: <1>

EOF
echo "Not dupe: $?"
rm -f testcache.dat

./reformail -x from: -x subject: <<EOF
From: extract1@example.com
Subject: subject1
Date: today

test
EOF
./reformail -X from: -X subject: <<EOF
From: extract2@example.com
Subject: subject2
Date: today

test
EOF

./reformail -a 'From: me@example.com' -a 'Subject: replacementsubject' <<EOF
From: nobody@example.com

-a test
EOF

./reformail -A 'From: me@example.com' -A 'Subject: replacementsubject' <<EOF
From: nobody@example.com

-A test
EOF

./reformail -c <<EOF
From: nobody@example.com
Subject: catenate
	header test
To: nobody@example.com

test
EOF

./reformail -f0 <<EOF
From testing
Return-Path: old
Subject: remove From_ test

test
EOF
./reformail -f0 <<EOF
Subject: remove From_ test

test
EOF
./reformail -f1 <<EOF
From testing
From: nobody@example.com
Subject: add From_ test

test
EOF
(./reformail -f1 | sed 's/example.com.*/example.com/') <<EOF
From: nobody@example.com
Subject: add From_ test

test
EOF
./reformail -i 'Header1: value1' -i 'Header2: value2' <<EOF
From: nobody@example.com
Subject: -i test
Header1: value1

test
EOF
./reformail -I 'Header1:' -I 'Header2: value2' <<EOF
From: nobody@example.com
Subject: -I test
Header1: value1
Header2: old value2

test
EOF
./reformail -R subject: old-subject: <<EOF
Subject: -R option test
From: nobody@example.com

-R option test
EOF
./reformail -u 'subject:' <<EOF
Subject: -u test, line1
Subject: -u test, line2

Testing -u option
EOF
./reformail -U 'subject:' <<EOF
Subject: -u test, line1
Subject: -u test, line2

Testing -u option
EOF


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
LANG=en_US.utf-8 ./mailbot -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
LANG=en_US.utf-8 ./mailbot -m testmailbot.msg cat <testmailbot.1
LANG=en_US.utf-8 TZ=EST5EDT ./mailbot -e -m testmailbot.msg \
    -S 'On %d,%nin %C (%N), %F (%f),%nin message %i, %s, writes:' \
    cat <testmailbot.1
LANG=en_US.utf-8 ./mailbot -T replyall -m testmailbot.msg cat <testmailbot.1
LANG=en_US.utf-8 ./mailbot -T replyall -N -m testmailbot.msg cat <testmailbot.1
LANG=en_US.utf-8 ./mailbot -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1
LANG=en_US.utf-8 ./mailbot -T forwardatt -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

cat >testsuite.recipe <<EOF
if (hasaddr("nobody@example.com"))
{
  echo "Found"
}
else
{
  echo "Not found"
}
exit
EOF

cat >testsuite.msg <<EOF
To: "nobody@host2.example.com" <nobody@example.com>

test
EOF
./maildrop testsuite.recipe <testsuite.msg

cat >testsuite.msg <<EOF
To: "nobody@example.com" <nobody@host2.example.com>

test
EOF
./maildrop testsuite.recipe <testsuite.msg

cat >testsuite.recipe <<EOF
echo getaddr('To: list: "user 1" <user1@example.com>, user2@example.com;')
exit
EOF
./maildrop testsuite.recipe </dev/null

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 "===================="
LANG=en_US.utf-8 ./mailbot -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1

echo "===================="
LANG=en_US.utf-8 ./mailbot -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 "===================="
LANG=en_US.utf-8 ./mailbot -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1

echo "===================="
LANG=en_US.utf-8 ./mailbot -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 "===================="
LANG=en_US.utf-8 ./mailbot -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

echo "===================="
LANG=en_US.utf-8 ./mailbot -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 "===================="
LANG=en_US.utf-8 ./mailbot -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

echo "===================="
LANG=en_US.utf-8 ./mailbot -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 "===================="
LANG=en_US.utf-8 ./mailbot -T forward -F '+++ Forwarded Message +++' -m testmailbot.msg cat <testmailbot.1 | sed 's/=_.*//'

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

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

LANG=en_US.utf-8 ./mailbot -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

LANG=en_US.utf-8 ./mailbot -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

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

LANG=en_US.utf-8 ./mailbot -T replyfeedback -R abuse -n -N -m testmailbot.msg \
    --feedback-source-ip 127.0.0.1 \
    --feedback-incidents 2 \
    <testmailbot.1 | sed 's/=_.*//;s/Arrival-Date:.*/Arrival-Date:/;s/User-Agent:.*//'

LANG=en_US.utf-8 ./mailbot -T feedback -R abuse -n -N -m testmailbot.msg \
    --feedback-source-ip 127.0.0.1 \
    --feedback-incidents 2 \
    <testmailbot.1 | sed 's/=_.*//;s/Arrival-Date:.*/Arrival-Date:/;s/User-Agent:.*//'

rm -f testsuite.recipe
rm -f testsuite.msg
rm -f testsuite.chk
rm -f testmailbot.msg
rm -f testmailbot.1
rm -f testsuite.dat
rm -rf testsuite.maildir
