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

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

  DEFAULT='unsubscribe'
  SENDER="${SND}@$HOST"

  ${EZBIN}/ezmlm-sub "$DIR" "$SENDER"
  check_manage unsubscribe-U -U 0 "failed with -U" unsub-ok \
      "$LIST-return-@$HOST" "$SENDER"
  ${EZBIN}/ezmlm-issubn "$DIR" && \
	fatal "unsubscribe with -U failed"

  ${EZBIN}/ezmlm-sub "$DIR" "$SENDER"
  touch "$DIR"/nounsubconfirm
  check_manage unsubscribe-nounsubconfirm '' 0 "failed with nounsubconfirm" unsub-ok \
      "$LIST-return-@$HOST" "$SENDER"
  ${EZBIN}/ezmlm-issubn "$DIR" && \
	fatal "unsubscribe with nounsubconfirm failed"
  rm "$DIR"/nounsubconfirm

  ${EZBIN}/ezmlm-unsub "$DIR" digest "$SENDER"

# test that access to the deny db is restricted to remote admins
  DEFAULT='deny-subscribe'
  check_manage deny '' 100 'Deny open to regular subscribers'

  SENDER="${MOD}@$HOST"
  ${EZBIN}/ezmlm-sub "$DIR" mod "$SENDER" || exit 100
  check_manage admin-deny '' 0 'Deny access denied to remote admin' mod-sub-confirm \
      "$LIST-deny-return-@$HOST" "$SENDER"

# make non-moderated
  rm -f "${DIR}/modsub" || \
	{ echo "Failed to remove DIR/modsub"; exit 99; }

# make non-remote
  rm -f "${DIR}/remote" || \
	{ echo "Failed to remove DIR/remote"; exit 99; }
  check_manage noremote-deny '' 100 'Deny even without remote/modsub'

# restore remote/SENDER/mod/DEFAULT
  ${EZBIN}/ezmlm-unsub "$DIR" mod "$SENDER" || exit 100
  SENDER="${SND}@$HOST"		# restore order
  touch "${DIR}/remote" || \
	{ echo "Failed to create DIR/remote"; exit 99; }
  DEFAULT='digest-subscribe'

  check_manage digest-subscribe-S -S 0 'failed with -S' sub-ok \
      "$LIST-digest-return-@$HOST" "$SENDER"
  ${EZBIN}/ezmlm-issubn "$DIR" digest || \
	fatal "digest-subscribe with -S failed"

  ${EZBIN}/ezmlm-unsub "$DIR" digest "$SENDER"
  touch "$DIR"/nosubconfirm
  check_manage digest-subscribe-nosubconfirm '' 0 'failed with nosubconfirm' sub-ok \
      "$LIST-digest-return-@$HOST" "$SENDER"
  ${EZBIN}/ezmlm-issubn "$DIR" digest || \
	fatal "digest-subscribe with nosubconfirm failed"
  rm "$DIR"/nosubconfirm

  ${EZBIN}/ezmlm-unsub "$DIR" digest "$SENDER"
  touch "${DIR}/modsub" || \
	{ echo "Failed to restore DIR/modsub"; exit 99; }

  SENDER="${MAN}@$HOST"

  echo "X-num: sub1" > "$TMP"
  echo "From: Mr. User requests <${MAN}@$HOST>" >> "$TMP"
  echo >> "$TMP"
  check_manage sub1 '' 0 'digest-subscribe with request failed' sub-confirm \
      "$LIST-digest-return-@$HOST" "$SENDER"

  grep T"${MAN}@$HOST" "$QQENV" >/dev/null 2>&1 || \
	fatal "failed getting digest-subscribe confirm request"

  # Add moderator
  ${EZBIN}/ezmlm-sub "$DIR" mod "${MOD}@$HOST"

  DEFAULT=`grep "Reply-To:" "$QQMSG" | cut -d' ' -f2 | cut -d'@' -f1 | cut -c"$LISTLEN"-` || \
	fatal "failed to find confirm address in -subscribe reply"
  qqclean
  echo "X-num: sub3" > "$TMP"
  echo "From: Mr. User confirms <$SENDER>" >> "$TMP"
  echo >> "$TMP"
  check_manage sub3 '' 0 'failed to send user conf for sub1' mod-sub-confirm \
      "$LIST-digest-return-@$HOST" "$MOD@$HOST"
  msgexists || \
	fatal "failed to receive user conf for sub1"
  SENDER="${MOD}@$HOST"
  ${EZBIN}/ezmlm-issubn "$DIR" && \
	fatal "unsub without mod for moderated list failed"
  grep 'sub3' "$QQMSG" >/dev/null 2>&1 || \
	fatal "failed getting subscribe moderation confirm request"

  # confirm subscription request
  DEFAULT=`grep "Reply-To:" "$QQMSG" | cut -d' ' -f2 | cut -d'@' -f1 | cut -c"$LISTLEN-"` || \
	fatal "no confirm address in sub3 mod confirm request"
  echo "X-num: modR1" > "$TMP"
  echo "FROM: moderator agrees <$SENDER>" >> "$TMP"
  echo >> "$TMP"
  check_manage modR1 '' 0 'failed to send digest sub mod accept for sub3' sub-ok \
      "$LIST-digest-return-@$HOST" "$MAN@$HOST"

  SENDER="${MAN}@$HOST"
  ${EZBIN}/ezmlm-sub "$DIR" "$SENDER"
  DEFAULT='unsubscribe'
  echo "X-num: sub2" > "$TMP"
  check_manage sub2 '' 0 'unsubscribe request failed' unsub-confirm \
      "$LIST-return-@$HOST" "$SENDER"

  DEFAULT=`grep "Reply-To:" "$QQMSG" | cut -d' ' -f2 | cut -d'@' -f1 | cut -c"$LISTLEN"-` || \
	fatal "failed to find confirm address in -unsubscribe reply"
  echo "X-num: sub4" > "$TMP"
  check_manage sub4 '' 0 'failed to send conf for sub2' unsub-ok \
      "$LIST-return-@$HOST" "$SENDER"

# -get function for backwards compatibility
  DEFAULT='get.1'
  echo "X-num: manget1" > "$TMP"
  check_manage get.1 '' 0 'get failed' get \
      "$LIST-return-@$HOST" "$SENDER"

  # Check -get.1 reply
  grep 'msg1' "$QQMSG" >/dev/null || \
	fatal "get.1 failed to return archived message"

# -C should disable it
  check_manage get-C -C 100 '-C failed to disable -get'

  echo "OK"
