#!/bin/sh

# Maintainer's sanity check

OPTIONS=""
export OPTIONS
TZ=EST5EDT
export TZ
if test "$PERL" = ""
then
    PERL="perl"
fi

echo '****************************' >&2
echo '* Sanity check in progress *' >&2
echo '****************************' >&2
#test ! -d confmdtest || find confmdtest -exec chmod u+rwx {} \;
#test ! -d confmdtest2 || find confmdtest2 -exec chmod u+rwx {} \;
test ! -d confmdtest || chmod -R u+rwx confmdtest
test ! -d confmdtest2 || chmod -R u+rwx confmdtest2
rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1

if test "$IMAPINVOKE" = ""
then
    IMAPINVOKE="./imapd"
fi

cat >confmdtest/cur/msg1:2,S <<EOF || exit 1
From: John <john@example.com>
To: Steve <steve@example.com>, Tom <tom@example.com>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="b1"
Subject: This is the message

foobar
--b1
Content-Type: text/plain

This is section 1

--b1
Content-Type: text/plain

This is section 2

--b1
Content-Type: message/rfc822

Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="b2"
Subject: This is message part 3

foobar
--b2
Content-Type: text/plain

This is section 3.1

--b2
Content-Type: text/plain

This is section 3.2

--b2--
--b1
Content-Type: multipart/mixed; boundary="b3"

foobar
--b3
Content-Type: text/plain

This is section 4.1

--b3
Content-Type: message/rfc822

Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="b4"
Subject: This is message part 4.2

foobar
--b4
Content-Type: text/plain

This is section 4.2.1

--b4
Content-Type: multipart/alternative; boundary="b5"

foobar
--b5
Content-Type: text/plain

This is section 4.2.2.1

--b5
Content-Type: text/plain

This is section 4.2.2.2

--b5--

--b4--

--b3--

--b1--
EOF

inituid() {

initdir="$1"

if test "$initdir" = ""
then
    initdir="confmdtest"
fi

initinbox="$2"
if test "$initinbox" = ""
then
    initinbox="inbox"
else
    initinbox="inbox.$initinbox"
fi

env IMAP_BROKENUIDV=1 MAILDIR=$initdir $IMAPINVOKE >confmdtest.stdout <<EOF
a001 select $initinbox
a logout
EOF

}

inituid

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
a001 select inbox
a002 fetch 1 (bodystructure)
a003 fetch 1 (body[])
a004 fetch 1 (body[]<500.100>)
a005 fetch 1 (body[1])
a006 fetch 1 (body[1.text])
a007 fetch 1 (body[2])
a008 fetch 1 (body[2.text])
a009 fetch 1 (body[3.header])
a010 fetch 1 (body[3.mime])
a011 fetch 1 (body[3.mime]<10,50>)
a012 fetch 1 (body[3.1])
a013 fetch 1 (body[3.2])
a014 fetch 1 (body[4.1])
a015 fetch 1 (body[4.2.header])
a016 fetch 1 (body[4.2.1])
a017 fetch 1 (body[4.2.2.1])
a018 fetch 1 (body[4.2.2.2])
a019 fetch 1 (envelope)
a020 fetch 1 (body)
a021 fetch 1 (envelope body)
a022 fetch 1 (bodystructure)
a023 fetch 1 (rfc822.size)
a024 fetch 1 (all)
a025 fetch 1 (fast)
a026 fetch 1 (full)
a027 fetch 1 (rfc822.text)
a028 fetch 1 (body[header.fields(content-type)])
adone logout
EOF

rm -f confmdtest/cur/msg1:2,S

cat >confmdtest/new/msg2 <<EOF || exit 1
From: John <john@example.com>
To: Steve <steve@example.com>,
    Tom <tom@example.com>
Mime-Version: 1.0
Date: Wed, 22 Sep 1999 15:41:09 -0200
Content-Type: multipart/mixed; boundary="b1"
Subject: This is the message

foobar
--b1
Content-Type: text/plain

This is section 1

--b1
Content-Type: message/rfc822

Subject: This is message part 4.2
From: dave@example.org
To: tom@example.org
Cc: steve@example.org
Mime-Version: 1.0
content-type: text/plain
content-transfer-encoding: quoted-printable


M=41ry had a little lamb, it's fleece was white as snow.  And everywhere
Mary went, the lamb was sure to go.

--b1--
EOF

inituid

cat >confmdtest/new/msg3 <<EOF || exit 1
From: todd@example.org
To: kevin@example.org
Subject: today's meeting

Today's meeting has been cancelled
EOF
cat >confmdtest/new/msg4 <<EOF || exit 1
Subject: New MIME headers test
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="c1"
Content-Language: en


--c1
Content-Type: text/plain
Content-ID: <foo@bar>
Content-Description:  MIME test message
Content-MD5: aaaabbbb

test

--c1--
EOF
touch -t 199901010000 confmdtest/new/msg3

inituid

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
b000 status inbox ( MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)
b001 select inbox
b001a status inbox ( MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)
b002 fetch 1:2 (flags uid)
b003 search header "content-type" "multipart"
b004 search 2 header "content-type" "multipart"
b005 search to tom@example
b006 search subject "message part 4.2"
b007 search before "1-Feb-1999"
b008 search on "1-Jan-1999"
b009 search senton "22-Sep-1999"
b010 search recent
b011 fetch 1 (rfc822.header)
b012 fetch 1:2 (flags)
b013 search seen
b014 search body "mary had a little lamb"
b015 uid search body "mary had a little lamb"
b016 store 1:2 +flags(\flagged)
b017 store 1 flags(\seen \deleted)
b018 expunge
b019 fetch 1 (flags uid)
b020 create inbox.bozo
b021 uid copy 3 inbox.bozo
b022 select inbox.bozo
b023 status inbox.bozo (uidnext)
b024 fetch 1 (flags)
b025 append inbox.bozo (\Seen) {11}
test

test

b026 append inbox.bozo (\Seen \Flagged) {11}
test

test

b027 noop
b028 fetch 1 flags
b029 fetch 1 flags
b030 select inbox
b031 fetch 2 (bodystructure)
b032 search from todd@example.org all
adone logout
EOF

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
foo select inbox.bozo
foo store 1 +flags \Deleted
foo expunge
foo logout
EOF
rm -rf confmdtest
mkdir confmdtest || exit 1
mkdir confmdtest/tmp || exit 1
mkdir confmdtest/cur || exit 1
mkdir confmdtest/new || exit 1
echo "10000S,2C" >confmdtest/maildirsize
env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
c001 select inbox
c002 append inbox {5}
test

c003 append inbox {5}
test

c004 append inbox {5}
test

c005 noop
c006 store 1 +flags \Deleted
c007 append inbox {5}
test

c008 noop
c009 append inbox {5}
test

c010 store 1 -flags \Deleted
cdone logout
EOF
cat confmdtest/maildirsize
rm -rf confmdtest
mkdir confmdtest || exit 1
mkdir confmdtest/tmp || exit 1
mkdir confmdtest/cur || exit 1
mkdir confmdtest/new || exit 1
echo "10000S,5C" >confmdtest/maildirsize
env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
d001 select inbox
d002 append inbox {5}
test

d003 noop
d004 copy 1 inbox
d005 noop
ddone logout
EOF
cat confmdtest/maildirsize
env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
e001 select inbox
e002 copy 1:2 inbox
e003 noop
edone logout
EOF
cat confmdtest/maildirsize
../maildir/maildirmake -f Trash confmdtest || exit 1

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
f001 select inbox
f002 copy 1:2 inbox
f002 copy 1:2 inbox.Trash
f003 noop
f004 select inbox.Trash
f005 copy 1:2 inbox
f006 copy 1:2 inbox.Trash
f007 noop
f008 copy 1 inbox
f009 select inbox
fdone logout
EOF
cat confmdtest/maildirsize
echo "Counts:" `ls confmdtest/cur | wc -l` `ls confmdtest/.Trash/cur | wc -l`
env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
g001 select inbox
g002 select Trash
g003 select INBOX.Trash
g004 delete INBOX.Trash
g005 close
g006 delete INBOX.Trash
g007 create INBOX.a
g008 delete INBOX.a/
g009 delete INBOX.a
g010 create inbox.a/
gdone logout
EOF

rm -rf confmdtest2
../maildir/maildirmake confmdtest2
../maildir/maildirmake -f a confmdtest2
../maildir/maildirmake -f b confmdtest2

# This simulates inaccessible permissions. chmod 000 does not work as root.
rm -rf confmdtest2/.b/*
ln -s nonexistent confmdtest2/.b/tmp
ln -s nonexistent confmdtest2/.b/cur
ln -s nonexistent confmdtest2/.b/new

echo "test `pwd`/confmdtest2" >confmdtest/shared-maildirs

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
h001 list "" "*"
h002 list "" "%"
h003 list "" "%.%"
h004 list "" "%.%.%"
h005 list "shared" "*"
h006 list "shared.test" "*"
h007 list "INBOX" ""
h008 list "shared" ""
h009 list "shared.test" ""
h010 subscribe inbox
h011 list "" "*"
h012 lsub "" "*"
h013 subscribe shared.test.a
h014 list "" "*"
h015 lsub "" "*"
h016 subscribe shared.test.b
h017 list "" "*"
h018 lsub "" "*"
h019 unsubscribe shared.test.a
h020 list "" "*"
h021 lsub "" "*"
h022 subscribe shared.test.a
h023 list "" "*"
h024 lsub "" "*"
h025 delete shared.test.a
hdone logout
EOF

cat >confmdtest2/.a/new/msg1 <<EOF
Subject: message 1

message 1
EOF

inituid confmdtest2 a

cat >confmdtest2/.a/new/msg2 <<EOF
Subject: message 2

message 2
EOF

inituid confmdtest2 a

cat >confmdtest/new/msg1 <<EOF
Subject: message 1
EOF

cat >confmdtest/.Trash/new/msg2 <<EOF
Subject: message 2
EOF

../maildir/maildirmake -f c confmdtest
../maildir/maildirmake -f Trash confmdtest2

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
ii001 list "" "*"
ii002 list "" "*"
iidone logout
EOF

env IMAP_BROKENUIDV=1 IMAP_CHECK_ALL_FOLDERS=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
ii003 list "" "*"
ii004 list "" "*"
iidone logout
EOF

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
i001 select shared.test.b
i002 select shared.test.a
i003 close
i004 select shared.test.a
i005 append shared.test.a {5}
test

i006 noop
i007 fetch 1:* (flags)
i008 select inbox
i009 copy 1:2 shared.test.a
i010 select shared.test.a
i011 fetch 1:* (flags)
i012 append shared.test.b {5}
i013 copy 1 shared.test.b
i014 store 1:2 +flags (\Deleted)
i015 expunge
idone logout
EOF
chmod -R u+rwx confmdtest
chmod -R u+rwx confmdtest2

rm -f confmdtest/new/*
rm -f confmdtest/cur/*

cat >confmdtest/new/msg1 <<EOF
From: John <john1@example.com>
To: John <cca4@example.com>, <ccb1@example.com>
Cc: John <toa1@example.com>, <tob4@example.com>
Subject: Re[2]: [foo] message 1
Date: Wed, 22 Sep 1999 15:41:09 -0200

message 1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
EOF

inituid

cat >confmdtest/new/msg2 <<EOF
From: John <john2@example.com>
To: John <cca3@example.com>, <ccb2@example.com>
Cc: John <toa2@example.com>, <tob3@example.com>
Subject: message 2
Date: Wed, 22 Sep 1999 15:41:00 -0200

message 2
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
EOF

inituid

cat >confmdtest/new/msg3 <<EOF
From: John <john3@example.com>
To: John <cca2@example.com>, <ccb3@example.com>
Cc: John <toa3@example.com>, <tob2@example.com>
Subject: message 1
Date: Wed, 22 Sep 1999 15:41:00 -0200

message 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
EOF

inituid

cat >confmdtest/new/msg4 <<EOF
From: John <john4@example.com>
To: John <cca1@example.com>, <ccb4@example.com>
Cc: John <toa4@example.com>, <tob1@example.com>
Subject: [fwd:message 1] (fwd)
Date: Wed, 15 Sep 1999 15:41:00 -0200

message 3
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
EOF

inituid

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
T001 select INBOX
T002 fetch 1:* (uid body[header.fields(date)])
T003 THREAD ORDEREDSUBJECT US-ASCII ALL
T004 THREAD ORDEREDSUBJECT US-ASCII SENTSINCE 19-SEP-1999
S001 SORT (SUBJECT DATE) US-ASCII ALL
S002 SORT (SUBJECT REVERSE DATE) US-ASCII ALL
S003 SORT (REVERSE SUBJECT DATE) US-ASCII ALL
S004 SORT (FROM) US-ASCII ALL
S005 SORT (REVERSE FROM) US-ASCII ALL
S006 SORT (FROM) US-ASCII SENTSINCE 19-SEP-1999
S007 SORT (REVERSE FROM) US-ASCII SENTSINCE 19-SEP-1999
S008 SORT (TO) US-ASCII ALL
S009 SORT (CC) US-ASCII ALL
S010 SORT (REVERSE SIZE) US-ASCII ALL
TDONE logout
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake -f Trash confmdtest || exit 1
env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
J001 list "" "*"
J002 lsub "" "*"
J003 SUBSCRIBE INBOX
J004 SUBSCRIBE INBOX.Trash
J005 list "" "*"
J006 lsub "" "*"
J007 UNSUBSCRIBE INBOX
J008 LIST "" "*"
J009 LSUB "" "*"
JDONE logout
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1

cat >confmdtest/new/msg1 <<EOF
From: postmaster
Subject: thread 1
Message-ID: <mid1>
Date: Sat, 05 Feb 2000 13:34:02 -0800

message 1
EOF

inituid

cat >confmdtest/new/msg2 <<EOF
From: postmaster
Subject: thread 1.1
Date: Sat, 05 Feb 2000 13:34:02 -0800
Message-ID: <mid2>
References: <mid1>

message 2
EOF

inituid

cat >confmdtest/new/msg3 <<EOF
From: postmaster
Date: Sat, 05 Feb 2000 13:34:02 -0800
Subject: thread 1.2
In-Reply-To: <mid2>

message 3
EOF

inituid

cat >confmdtest/new/msg4 <<EOF
From: postmaster
Date: Sat, 05 Feb 2000 13:34:02 -0800
Subject: thread 1.2
References: <mid1>

message 4
EOF

inituid

cat >confmdtest/new/msg5 <<EOF
From: postmaster
Date: Sat, 05 Feb 2000 13:34:02 -0800
Subject: thread 1.2
Message-ID: <mid11>
References: <mid10>

message 5
EOF

inituid

cat >confmdtest/new/msg6 <<EOF
From: postmaster
Date: Sat, 05 Feb 2000 13:34:01 -0800
Subject: thread 1.2
Message-ID: <mid12>
References: <mid10>

message 6
EOF

inituid

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
K001 select INBOX
K002 THREAD REFERENCES US-ASCII ALL
KDONE logout
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake -f Trash confmdtest || exit 1

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
L001 CREATE INBOX.a
L002 CREATE INBOX.a.a
L003 CREATE INBOX.aa
L004 RENAME INBOX.a INBOX.b
L005 LIST "" "*"
L006 RENAME INBOX.b INBOX.a
L007 LIST "" "*"
L008 RENAME INBOX.a. INBOX.b.
L009 LIST "" "*"
L010 RENAME INBOX.b. INBOX.a.
L011 LIST "" "*"
L012 CREATE INBOX.b.a
L013 DELETE INBOX.a
L014 CREATE INBOX.a.a
L015 LIST "" "*"
L016 RENAME INBOX.a INBOX.b
L017 RENAME INBOX.a. INBOX.b.
L018 SELECT INBOX
L019 APPEND INBOX (\Seen) {5}
test

L020 NOOP
L021 COPY 1 INBOX
L022 COPY 1 INBOX
L023 COPY 1 INBOX
L024 COPY 1 INBOX
L025 COPY 1 INBOX
L026 COPY 1 INBOX
L027 NOOP
L028 STORE 2:3,5:7 +FLAGS \Deleted
L029 FETCH 1:* (UID FLAGS)
L030 UID EXPUNGE 2,3:5
L031 FETCH 1:* (UID FLAGS)
LDONE logout
EOF

rm -rf confmdtest

../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake -q 10C confmdtest || exit 1

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
M001 CREATE INBOX.a
M002 APPEND INBOX (\Seen) {5}
test

M003 SELECT INBOX
M004 COPY 1 INBOX
M005 COPY 1 INBOX
M006 NOOP
M007 COPY 1:3 INBOX.a
M008 RENAME INBOX.a INBOX.b
MDONE logout
EOF
cat confmdtest/maildirsize
env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
M009 DELETE INBOX.b
MDONE logout
EOF
cat confmdtest/maildirsize

rm -rf confmdtest

../maildir/maildirmake confmdtest || exit 1
env IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
N001 LIST "" "*"
N002 append inbox (\Deleted) {11}
test

test

N003 LIST "" "*"
N004 SELECT inbox
N005 EXPUNGE
N006 LIST "" "*"
NDONE logout
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake -q10C confmdtest
env IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
O001 append inbox NIL {11}
test

test

o002 SELECT inbox
o003 COPY 1 inbox
o004 NOOP
o005 STORE 1 +FLAGS \Deleted
o006 COPY 1:2 inbox
o007 NOOP
o008 logout
EOF
cat confmdtest/maildirsize
rm -f confmdtest/maildirsize

env IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
p001 SELECT INBOX
p002 CREATE INBOX.a
p003 COPY 1 INBOX.a
p004 SELECT INBOX.a
p005 STORE 1 +FLAGS(-Label1 \Deleted)
p006 append INBOX.a (-Label2) {11}
test

test

p007 EXPUNGE
p008 FETCH 1:* FLAGS
p009 COPY 1 INBOX.a
p010 NOOP
P011 FETCH 1:* FLAGS
P012 STORE 1 -FLAGS (-Label2)
P013 STORE 1 +FLAGS (\SEEN)
P013 FETCH 1:* FLAGS
P014 SEARCH SEEN
P015 SEARCH KEYWORD -LABEL2
P016 SEARCH UNKEYWORD -Label2
P017 SEARCH KEYWORD -Label1
P018 COPY 1:* INBOX.a
P019 COPY 1:* INBOX.a
P020 NOOP
P021 COPY 1:* INBOX.a
P022 COPY 1:* INBOX.a
P023 NOOP
P025 STORE 4 +FLAGS -Label1
P024 FETCH 1:* FLAGS
P025 SEARCH KEYWORD -Label2
P026 SEARCH KEYWORD -Label2 KEYWORD -Label1
P027 STORE 4 +FLAGS -Label3
P028 STORE 4 +FLAGS -Label4
pDONE LOGOUT
EOF

env IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
Q001 select INBOX
Q002 fetch 1:* FLAGS
Q003 status INBOX.Trash MESSAGES
Q004 expunge
Q005 status INBOX.Trash MESSAGES
Q006 COPY 1 INBOX.a
Q007 STORE 1:* +FLAGS \Deleted
Q008 NOOP
Q009 EXPUNGE
Q005 status INBOX.Trash MESSAGES
qDONE LOGOUT
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1

env IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
R001 CREATE INBOX.a
R002 LIST (ACL MYRIGHTS POSTADDRESS) "" "*"
R003 ACL STORE INBOX owner -t
R004 ACL STORE (INBOX *) owner +d
R005 ACL STORE INBOX.a user=systemuser1 alr
R006 ACL STORE INBOX.a user=systemuser1 +w
R007 ACL STORE INBOX.a user=systemuser1 -w
R008 ACL STORE INBOX.a user=systemuser1 -alr
R009 ACL STORE INBOX.a user=systemuser1 alr
R010 ACL STORE INBOX.a user=systemuser1 ""
R011 ACL STORE INBOX.a user=systemuser1 alr
R012 ACL SET (INBOX *) "owner" "acdilrsw" administrators "acdilrsw" user=systemuser2 alr
R013 ACL DELETE INBOX.a user=systemuser1
R014 ACL DELETE INBOX.a user=systemuser2
R015 ACL STORE INBOX.a owner -a
R016 ACL STORE INBOX.a -owner +wt
R017 ACL STORE INBOX.a -owner -w
R018 ACL DELETE INBOX.a -owner
R019 ACL STORE INBOX.a -owner +a
R020 ACL STORE INBOX.a owner -ci
R021 CREATE INBOX.a.b
R022 ACL STORE INBOX.a owner +c
R023 CREATE INBOX.a.b
R024 LIST (ACL) "" INBOX.a.b
R025 CREATE INBOX.c.d
R026 LIST (ACL) "" "INBOX.c*"
R027 ACL STORE (INBOX.c*) -owner x
R028 DELETE INBOX.c.d
R029 ACL DELETE INBOX.c.d -owner
R030 DELETE INBOX.c.d
R031 DELETE INBOX.c.
R032 LIST (ACL) "" "*"
R033 DELETE INBOX.a
R034 LIST (ACL) "" "*"
R035 RENAME INBOX.a INBOX.f
R036 LIST (ACL) "" "*"
R037 CREATE INBOX.f
R038 LIST (ACL) "" "*"
R039 ACL STORE INBOX owner -i
R040 SELECT INBOX
R041 APPEND INBOX (\Seen \Deleted \Answered Foo) {0}
R042 ACL STORE INBOX owner +i
R043 APPEND INBOX (\Seen \Deleted \Answered Foo) {11}
test

test

R043 NOOP
R044 FETCH 1 (FLAGS)
R045 ACL STORE INBOX owner -tw
R046 APPEND INBOX (\Seen \Deleted \Answered Foo) {11}
test

test

R047 NOOP
R048 FETCH 2 (FLAGS)
R049 ACL STORE INBOX owner +tw
R050 ACL STORE INBOX owner -s
R051 APPEND INBOX (\Seen \Deleted \Answered Foo) {11}
test

test

R052 NOOP
R053 FETCH 3 (FLAGS)
R054 STORE 2:3 +FLAGS (\Deleted)
R055 STORE 1 -FLAGS (\Deleted)
R056 EXPUNGE
R057 STORE 1 +FLAGS (\Deleted)
R058 ACL STORE INBOX owner +s
R059 CREATE INBOX.x
R060 COPY 1 INBOX.x
R061 SELECT INBOX.x
R062 FETCH 1 (FLAGS)
R063 SELECT INBOX
R064 ACL STORE INBOX.x owner -s
R065 COPY 1 INBOX.x
R066 SELECT INBOX.x
R067 FETCH 2 (FLAGS)
R068 SELECT INBOX
R069 ACL STORE INBOX.x owner +s
R070 ACL STORE INBOX.x owner -w
R071 COPY 1 INBOX.x
R072 SELECT INBOX.x
R073 FETCH 3 (FLAGS)
R074 SELECT INBOX
R075 ACL STORE INBOX.x owner +w
R076 ACL STORE INBOX.x owner -t
R077 COPY 1 INBOX.x
R078 SELECT INBOX.x
R079 FETCH 4 (FLAGS)
R080 SELECT INBOX
RDONE LOGOUT
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake -f a confmdtest || exit 1
cat >confmdtest/.a/cur/msg1:2,S <<EOF || exit 1
From: John <john@example.com>
To: john <john@example.com>
Subject: test

test
EOF
env IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
S001 SELECT INBOX.a
S002 COPY 1 INBOX.a
S003 NOOP
S004 COPY 1:2 INBOX.a
S005 NOOP
S006 ACL STORE INBOX.a owner -e
S007 STORE 1 +FLAGS(\Deleted)
S008 EXPUNGE
S009 CLOSE
S010 SELECT INBOX.a
S011 ACL STORE INBOX.a owner +e
S012 EXPUNGE
S013 STORE 1 +FLAGS(\Deleted)
S014 CLOSE
S015 SELECT INBOX.a
S016 CLOSE
S017 ACL STORE INBOX.a owner -r
S018 SELECT INBOX.a
S019 EXAMINE INBOX.a
S020 STATUS INBOX.a (UIDVALIDITY)
S021 ACL STORE INBOX.a owner +r
S022 SELECT INBOX.a
S023 APPEND INBOX.a NIL {11}
test

test

S024 NOOP
S025 STORE 1:* FLAGS ()
S027 ACL STORE INBOX.a owner -s
S028 FETCH 1 (BODY[])
S029 ACL STORE INBOX.a owner +s
S030 FETCH 1 (BODY[])
S031 ACL STORE INBOX.a owner -s
S032 STORE 1:2 FLAGS(\Seen \Deleted Foo)
S033 STORE 1:2 -FLAGS(\Seen \Deleted Foo)
S034 STORE 2 +FLAGS(\Seen \Deleted \Answered Foo)
S035 FETCH 1:2 (FLAGS)
S036 ACL STORE INBOX.a owner +s
S037 ACL STORE INBOX.a owner -w
S038 STORE 1:2 FLAGS(\Seen \Deleted Bar)
S039 STORE 1:2 -FLAGS(\Seen \Deleted Bar)
S040 STORE 1:2 +FLAGS(\Seen \Deleted Bar)
S041 ACL STORE INBOX.a owner +w
S042 STORE 2 -FLAGS (\Deleted)
S043 ACL STORE INBOX.a owner -t
S044 STORE 1:2 FLAGS (\Deleted \Seen Bar)
S045 STORE 1:2 -FLAGS (\Deleted \Seen Bar)
S046 STORE 1:2 +FLAGS (\Deleted \Seen Bar)
S047 CLOSE
S048 ACL STORE INBOX user=courierimaptestuser1 alr
S049 ACL STORE INBOX user=courierimaptestuser2 lr
S050 LIST (ACL) "" INBOX
SDONE LOGOUT
EOF

rm -rf confmdtest*
../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake confmdtest2 || exit 1
../maildir/maildirmake confmdtest3 || exit 1
mkdir confmdtest4 || exit 1
cat >confmdtest4/index <<EOF || exit 1
user0	1	1	`pwd`	confmdtest
a	*	indexa
b	*	indexb
EOF
echo "user1	1	1	`pwd`	confmdtest2" >confmdtest4/indexa || exit 1
echo "aashared	1	1	`pwd`	confmdtest5" >>confmdtest4/indexa || exit 1
echo "user2	1	1	`pwd`	confmdtest3" >confmdtest4/indexb || exit 1

IMAP_SHAREDINDEXFILE=`pwd`/confmdtest4/index
export IMAP_SHAREDINDEXFILE

env AUTHENTICATED=user1 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest2 $IMAPINVOKE <<EOF
T001 ACL STORE INBOX user=user0 +l
T002 CREATE INBOX.a
T003 ACL STORE INBOX.a anyone +lr
T004 append INBOX.a (\Seen) {22}
Subject: user1

user1

T005 LOGOUT
EOF

env AUTHENTICATED=user2 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest3 $IMAPINVOKE <<EOF
T010 ACL STORE INBOX anyone +l
T011 CREATE INBOX.a
T012 append INBOX.a (\Seen) {22}
Subject: user2

user2

T013 LOGOUT
EOF

env AUTHENTICATED=user0 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
T020 list(acl) "" "#shared.*"
T021 ACL STORE INBOX anyone acdilrsw
T022 list(acl) "" "#shared.%"
T023 LOGOUT
EOF

env AUTHENTICATED=user2 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest3 $IMAPINVOKE <<EOF
T030 LIST "" "#shared.*"
T031 LIST "" "#shared.%"
T032 LIST "" "#shared.a.%"
T033 LIST "" "#shared.c.%"
T034 SELECT "#shared.a.user1"
T035 SELECT "#shared.a.user1.a"
T036 FETCH 1:* (ENVELOPE)
T037 STATUS #shared.a.user1 (UIDVALIDITY)
T038 COPY 1 #shared.user0
T039 SELECT #shared.user0
T040 CREATE #shared.a.user1.b
T041 CREATE #shared.c
T042 CREATE #shared.user0.foo
T043 RENAME #shared.user0.foo #shared.user0.bar
T044 LIST(ACL) "" "#shared.user0*"
T045 ACL STORE #shared.a.user1.a anyone lr
T046 CREATE #shared.user0.foo
T047 ACL STORE #shared.user0.foo anyone alr
T048 ACL STORE #shared.user0.bar anyone -x
T049 RENAME #shared.user0.bar #shared.user0.foo.bar
T050 ACL STORE #shared.user0.bar anyone +x
T051 RENAME #shared.user0.bar #shared.user0.foo.bar
T052 ACL STORE #shared.user0.foo anyone +c
T053 RENAME #shared.user0.bar #shared.user0.foo.bar
T054 LIST "" "#shared.user0*"
TDONE LOGOUT
EOF

cat >confmdtest4/indexb <<EOF
use&2	1	1	`pwd`	confmdtest3
user.2	1	1	`pwd`	confmdtest3
usér/3	1	1	`pwd`	confmdtest3
EOF

env AUTHENTICATED=user1 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest2 $IMAPINVOKE <<EOF
a ENABLE UTF8=ACCEPT
T055 LIST "" "#shared.b.*"
T056 LIST "" "#shared.b.%"
T057 LIST "" "#shared.b.user 2.*"
T058 LIST "" "#shared.b.use&-2.*"
TDONE LOGOUT
EOF

env IMAP_SHAREDMUNGENAMES=1 AUTHENTICATED=user1 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest2 $IMAPINVOKE <<EOF
a ENABLE UTF8=ACCEPT
T055 LIST "" "#shared.b.*"
T056 LIST "" "#shared.b.%"
T057 LIST "" "#shared.b.user\\\\:2.*"
T058 LIST "" "#shared.b.use&-2.*"
T059 CREATE INBOX.a.b
T060 SELECT INBOX.a.b
T061 RENAME INBOX.a INBOX.aa
T062 CLOSE
T063 RENAME INBOX.a INBOX.aa
T064 RENAME INBOX INBOX.c
T065 RENAME INBOX.aa #shared.user0.aa
T066 LIST "" #shared.*
T067 LIST "" #shared.%
T068 LIST "" #shared.%
T069 LIST "" #shared.%.%
T070 LIST "" #shared.%.%.%
T071 LIST "" #shared.%.%.%.%
TDONE LOGOUT
EOF

echo "usergroup1	1	1	`pwd`	confmdtest" >confmdtest4/indexgroup1 || exit 1

env IMAP_SHAREDMUNGENAMES=1 AUTHENTICATED=user1 OPTIONS=sharedgroup=group1 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest2 $IMAPINVOKE <<EOF
T072 LIST "" #shared.*
T073 LIST(ACL) "" INBOX
T074 ACL STORE INBOX user=fred lr
T075 ACL STORE INBOX -user=john lr
T076 ACL STORE INBOX -authuser lr
T077 GETACL INBOX
T078 SETACL INBOX -authuser lcr
T079 SETACL INBOX -john lcr
T080 SETACL INBOX fred cr
T081 GETACL INBOX
T082 LIST(ACL) "" INBOX
T083 DELETEACL INBOX -john
T084 DELETEACL INBOX fred
T085 GETACL INBOX
T086 LIST(ACL) "" INBOX
TDONE LOGOUT
EOF

rm -rf confmdtest confmdtest2
../maildir/maildirmake confmdtest
../maildir/maildirmake -f Trash confmdtest
../maildir/maildirmake -f folder:with:colonTD confmdtest
../maildir/maildirmake -S confmdtest2
../maildir/maildirmake -s write -f writable confmdtest2
../maildir/maildirmake --add another=`pwd`/confmdtest2 confmdtest

echo '#! /bin/sh' >confsendmail
echo "cat >`pwd`/confsendmail.out" >>confsendmail
chmod +x confsendmail

echo >confmdtest/.Trash/cur/msg:2
touch -t 202001011200 confmdtest/.Trash/cur/msg:2

echo >confmdtest/.folder:with:colonTD/cur/001:2,T
echo >confmdtest/.folder:with:colonTD/cur/002TD:2,T
echo >confmdtest/.folder:with:colonTD/cur/003:2,ATDB

env IMAP_BROKENUIDV=1 IMAP_TRASHFOLDERNAME=Trash IMAP_EMPTYTRASH=Trash:7 \
    MAILDIR=confmdtest AUTHENTICATED=user1 OUTBOX=.Outbox \
    IMAP_MOVE_EXPUNGE_TO_TRASH=1 \
    SENDMAIL=`pwd`/confsendmail $IMAPINVOKE <<EOF
U001 APPEND INBOX (\Seen) {5}
test

U002 LIST "" "*"
U003 SELECT INBOX
U004 COPY 1 "shared.another.writable"
U005 STATUS invalid (UIDVALIDITY)
U006 DELETE INBOX
U007 LISTRIGHTS INBOX user=user1
U008 LISTRIGHTS shared.another.writable user=user1
U009 MYRIGHTS INBOX
U010 COPY 1 public.nonexistent
U011 COPY 1 INBOX.nonexistent
U012 CREATE INBOX.Outbox
U013 APPEND INBOX (\Seen) {5}
test

U014 NOOP
U015 COPY 1,2 INBOX.Outbox
U016 COPY 1 INBOX.Outbox
U017 SELECT INBOX.folder:with:colonTD
U019 EXPUNGE
EOF
echo "===Sent=="
cat confsendmail.out
echo "===Trash="
ls confmdtest/.Trash/cur
echo "==="
rm -f confsendmail confsendmail.out

../maildir/maildirmake -q 2C ./confmdtest
env IMAP_BROKENUIDV=1 IMAP_TRASHFOLDERNAME=Trash IMAP_EMPTYTRASH=Trash:7 \
    MAILDIR=confmdtest AUTHENTICATED=user1 OUTBOX=.Outbox \
    IMAP_KEYWORDS=2\
    IMAP_MOVE_EXPUNGE_TO_TRASH=1 \
    SENDMAIL=`pwd`/confsendmail $IMAPINVOKE <<EOF
U021 SELECT INBOX
U022 STORE 1 +FLAGS(Label1)
U023 COPY 1 INBOX
EOF

rm -rf confmdtest*

../maildir/maildirmake confmdtest || exit 1
../maildir/maildirmake -f Trash confmdtest || exit 1
../maildir/maildirmake -f Trash2 confmdtest || exit 1

env IMAP_EMPTYTRASH=2 ./imapd.testmockpurge confmdtest <<EOF | grep maildir_purge
TRASH1 SELECT INBOX
TRASH1 LOGOUT
EOF
env IMAP_EMPTYTRASH=Trash:2,Trash2:3 ./imapd.testmockpurge confmdtest <<EOF | grep maildir_purge
TRASH2 LOGOUT
EOF

rm -rf confmdtest
../maildir/maildirmake confmdtest || exit 1

env IMAP_BROKENUIDV=1 AUTHENTICATED=user1 $IMAPINVOKE confmdtest <<EOF
V001 ACL STORE INBOX user=user2 +w
V002 CREATE INBOX.Folder.Subfolder
V003 ACL STORE INBOX.Folder user=user2 +r
V004 LIST (ACL) "" "INBOX.Folder"
V005 CREATE INBOX.Folder
V006 LIST (ACL) "" "INBOX.Folder"
V007 GETQUOTAROOT INBOX
V008 LIST "invalid" "%"
EOF
ls confmdtest/courierimaphieracl 2>&1

cat >confmdtest/cur/msg1:2,S <<EOF || exit 1
From: John <john@example.com>
To: Steve <steve@example.com>, Tom <tom@example.com>
Subject: thread1
Mime-Version: 1.0
Date: Sat, 05 Feb 2000 13:34:03 -0800
Content-Type: multipart/mixed; boundary="b1"
Message-Id: <1@example.com>
Subject: This is the message

message
EOF

env IMAP_BROKENUIDV=1 AUTHENTICATED=user1 $IMAPINVOKE confmdtest <<EOF
V010 SELECT INBOX
EOF

cat >confmdtest/cur/msg2:2,S <<EOF || exit 1
From: John <john@example.com>
To: Steve <steve@example.com>, Tom <tom@example.com>
Subject: thread1
Mime-Version: 1.0
Date: Sat, 05 Feb 2000 13:34:01 -0800
Content-Type: multipart/mixed; boundary="b1"
Message-Id: <2@example.com>
References: <1@example.com>
Subject: This is the message

message
EOF

env IMAP_BROKENUIDV=1 AUTHENTICATED=user1 $IMAPINVOKE confmdtest <<EOF
V010 SELECT INBOX
V010 THREAD ORDEREDSUBJECT US-ASCII ALL
EOF


rm -rf confmdtest confmdtest2

../maildir/maildirmake confmdtest2
../maildir/maildirmake -f a confmdtest2

mkdir confmdtest2/.a/cur/msg1:2,S

../maildir/maildirmake confmdtest

echo "test `pwd`/confmdtest2" >confmdtest/shared-maildirs

env IMAP_BROKENUIDV=1 AUTHENTICATED=user1 $IMAPINVOKE confmdtest <<EOF
W001 SUBSCRIBE shared.test.a
W002 SELECT shared.test.a
W003 STORE 1 +FLAGS (\Deleted)
W004 LOGOUT
EOF
ls confmdtest/shared-folders/test/a/cur 2>&1
env IMAP_BROKENUIDV=1 AUTHENTICATED=user1 $IMAPINVOKE confmdtest <<EOF
W005 SELECT shared.test.a
W006 EXPUNGE
EOF
ls confmdtest/shared-folders/test/a/cur 2>&1
rm -rf confmdtest confmdtest2

../maildir/maildirmake confmdtest

cat >confmdtest/cur/msg1:2,S <<EOF || exit 1
Subject: MIME fetch test
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="xx1"

--xx1
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Hello world!

--xx1
Content-Type: text/html
Content-Transfer-Encoding: 7bit

<p>Hello world!</p>

--xx1--
EOF

env IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
X001 SELECT INBOX
X002 FETCH 1 (BODYSTRUCTURE)
X003 FETCH 1 (BODY)
X004 FETCH 1 (BODY[])
X005 FETCH 1 (BODY[HEADER])
X006 FETCH 1 (BODY[1])
X007 FETCH 1 (BODY[2])
X008 FETCH 1 (BODY[1.HEADER])
X009 FETCH 1 (BODY[HEADER.FIELDS (Subject Content-Type)])
X010 FETCH 1 (BODY[2.HEADER.FIELDS.NOT (Content-Type)])
X011 FETCH 1 (BODY[2]<0.4>)
X012 FETCH 1 (BODY[2]<4.4>)
X013 FETCH 1 (BODY[2]<0.4>)
X014 FETCH 1 (ENVELOPE RFC822)
X015 FETCH 1 (RFC822.HEADER RFC822.SIZE)
X016 FETCH 1 (RFC822.TEXT)
X017 APPEND INBOX (\Seen) "01-Jan-2022 12:00:00 -0500" {5}
test

XDONE LOGOUT
EOF
rm -f confmdtest/cur/msg1*

f="`ls confmdtest/cur/*`"
$PERL -e "use POSIX qw(strftime); print strftime(\"%m/%Y\\n\", localtime [stat('$f')]->[9])"
rm -rf confmdtest


../maildir/maildirmake confmdtest
env IMAP_SHAREDMUNGENAMES=1 AUTHENTICATED=user1 IMAP_KEYWORDS=1 IMAP_MOVE_EXPUNGE_TO_TRASH=1 IMAP_BROKENUIDV=1 MAILDIR=confmdtest $IMAPINVOKE <<EOF
Y001 ENABLE UTF8=ACCEPT
Y002 APPEND INBOX UTF8 (~{14}
Subject: test
)
Y003 SELECT INBOX
Y004 FETCH 1 (RFC822.HEADER FLAGS)
Y005 APPEND INBOX (\Seen) UTF8 (~{15}
Subject: test2
)
Y006 NOOP
Y005 FETCH 2 (RFC822.HEADER FLAGS)
YDONE LOGOUT
EOF
rm -rf confmdtest
