################
# ezmlm-manage #
################

prompt "ezmlm-manage (2/2):   "

# now test remote admin functions
# add a few addresses to allow
${EZBIN}/ezmlm-sub "$DIR" allow "aaa@bbb" "ccc@ddd" "eee@fff"

# test -edit
qqclean
echo "#TEST_TEXT#" > "${DIR}/text/test"
DEFAULT="edit.test-$MAN=$HOST"
echo "X-num: edit1" > "$TMP"
check_manage edit1 -e 100 'failed to reject edit request from non-mod'

DEFAULT="edit.test-$MOD=$HOST"
echo "X-num: edit2" > "$TMP"
rm "$DIR"/modcanedit
check_manage edit2 '' 100 '-E failed for edit2'

echo "X-num: edit3" > "$TMP"
touch "$DIR"/modcanedit
check_manage edit3 '' 0 'modcanedit failed for remote admin for edit3' 'edit test' \
    "$LIST-return-@$HOST" "$MOD@$HOST"
grep "^Subject: EDIT test for ${LIST}@${HOST}$" "$QQMSG" >/dev/null \
|| fatal "edit3 had incorrect subject"
rm "$DIR"/modcanedit

# complete edit. SENDER can be any address
SENDER="${MAN}@$HOST"
grep 'edit3' "$QQMSG" >/dev/null 2>&1 || \
	fatal "failed getting edit reply for edit3"
grep "#TEST_TEXT#" "$QQMSG" >/dev/null 2>&1 || \
	fatal "old text missing in edit3 edit reply"
DEFAULT=`grep "Reply-To:" "$QQMSG" | cut -d' ' -f2 | cut -d'@' -f1 | cut -c"$LISTLEN"-` || \
	fatal "no reply address in edit3 edit reply"
echo "X-num: edit4" > "$TMP"
echo >> "$TMP"
echo "%%% START OF TEXT FILE" >> "$TMP"
echo "#NEW_TEXT#" >> "$TMP"
echo "%%% END OF TEXT FILE" >> "$TMP"
check_manage edit4 -e 0 'failed to send edit4 reply for edit3' edit-done \
    "$LIST-return-@$HOST" "$MAN@$HOST"
grep "#NEW_TEXT#" "${DIR}/text/test" >/dev/null 2>&1 || \
	fatal "edit4 failed to update text file"
grep "^Subject: Success editing test for ${LIST}@${HOST}$" "$QQMSG" >/dev/null \
|| fatal "edit4 had incorrect subject"

# test list/log
DEFAULT="allow-list-$MAN=$HOST"
echo "X-num: list1" > "$TMP"
check_manage list-l -l 100 'failed to reject list request from non-mod'

DEFAULT="allow-log-$MAN=$HOST"
echo "X-num: log1" > "$TMP"
check_manage log-l -l 100 'failed to reject log request from non-mod'

DEFAULT="allow-list-$MOD=$HOST"
echo "X-num: list2" > "$TMP"
rm "$DIR"/modcanlist
check_manage list2 '' 100 '-L failed to reject list request'

qqclean
echo "X-num: list3" > "$TMP"
check_manage list3 -l 0 '-l failed for remote admin' list \
    "$LIST-allow-return-@$HOST" "$MOD@$HOST"

grep 'list3' "$QQMSG" >/dev/null 2>&1 || \
	fatal "failed getting -list reply to list3"
grep "aaa@bbb" "$QQMSG" > /dev/null 2>&1 || \
	fatal "failed to get list reply to list3"

qqclean
echo "X-num: list4" > "$TMP"
touch "$DIR"/modcanlist
check_manage list4 '' 0 'modcanlist failed for remote admin for list3' list \
    "$LIST-allow-return-@$HOST" "$MOD@$HOST"

grep 'list4' "$QQMSG" >/dev/null 2>&1 || \
	fatal "failed getting -list reply to list4"
grep "aaa@bbb" "$QQMSG" > /dev/null 2>&1 || \
	fatal "failed to get list reply to list4"
rm "$DIR"/modcanlist

DEFAULT="allow-log-$MOD=$HOST"
echo "X-num: log2" > "$TMP"
check_manage log2 '' 100 '-L failed to reject log request'

qqclean
echo "X-num: log3" > "$TMP"
check_manage log3 -l 0 '-l failed for remote admin for log3' log \
    "$LIST-allow-return-@$HOST" "$MOD@$HOST"
grep 'log3' "$QQMSG" >/dev/null 2>&1 || \
	fatal "failed getting -log reply to log3"
grep "aaa@bbb" "$QQMSG" | grep "+m" > /dev/null 2>&1 || \
	fatal "failed to get log reply to log3"

echo "OK"
