#! /bin/sh
##  $Revision: 1.17 $
##  Newgroup control-message handler

PROG=newgroup
##  Some shells don't pass in $* unless we explicitly pass it in here.
## =()<. @<_PATH_PARSECTL>@ "$@">()=
. /usr/local/news/parsecontrol "$@"

##  Valid parameters and group name?
case "$#" in
1|2)
    ;;
*)
    ${WRITELOG} ${LOGFILE} "Unparseable newgroup by ${FROM}" <${ARTICLE}
    exit
    ;;
esac
case "$1" in
*[\ \	/:]*)
    ${WRITELOG} ${LOGFILE} "Unsafe newgroup by ${FROM}" <${ARTICLE}
    exit
    ;;
esac

##  Make sure article is approved.
sed -e '/^$/q' <${ARTICLE} >${TEMP}
grep '^Approved:' <${TEMP} >/dev/null 2>&1 || {
    rm ${TEMP}
    ${WRITELOG} ${LOGFILE} "Unapproved newgroup by ${FROM}" <${ARTICLE}
    exit
}
rm ${TEMP}

case "$2" in
[Mm]*)
    set $1 m
    ;;
*)
    set $1 y
    ;;
esac

##  Compare group against active file.
LINE=`grep "^$1 " <${ACTIVE} 2>/dev/null`
case "${LINE}" in
"")
    ##  New group.
    WORD=created
    ;;
"$1 "*" $2")
    ##  Exact match; ignore it.
    exit
    ;;
"$1 "*" ="*|"$1 "*" x"|"$1 "*" j")
    ${WRITELOG} ${LOGFILE} "Rejected special newsgroup ($1) by ${FROM}" \
	    <${ARTICLE}
    exit
    ;;
*)
    case "$2" in
    m)
	WORD="made moderated"
	;;
    y)
	WORD="made unmoderated"
	;;
    esac
    ;;
esac

case ${ACTION} in
mail)
    P1=$1
    P2=$2
    export FROM WORD NEWSBIN P1 P2 ARTICLE
    (
	echo "${FROM} requested that $1 be ${WORD}."
	echo 'To do this, type the following:'
	echo "	${NEWSBIN}/ctlinnd newgroup ${P1} ${P2} ${FROM}"
	echo ''
	echo 'The original article was:'
	cat ${ARTICLE}
    ) | ${WRITELOG} ${LOGFILE} "newgroup $1 $2 ${FROM}" ${NEWSMASTER}
    exit
    ;;
logit)
    ${WRITELOG} ${LOGFILE} "skipping newgroup $1 $2 ${FROM} (would be ${WORD})" \
	<${ARTICLE}
    exit
    ;;
esac

##  Do the work.
ctlinnd -s newgroup $1 $2 "${FROM}"
${WRITELOG} ${LOGFILE} "newgroup $1 $2 ${WORD} ${FROM}" <${ARTICLE}

##  Try to update the newsgroups file.
case $WORD in
created)
    sed <${ARTICLE} >>${NEWSGROUPS} -n -e '
	:top
	/^For your newsgroups file:$/b print
	n;b top
	:print
	n;p;q'
    ;;
esac

exit
