#! /bin/sh
# fixscript will replace this line with code to load innshellvars

##  $Revision: 1.22 $
##  Initial parsing of control messages.

WRITELOG=${PATHBIN}/writelog
SM="${PATHBIN}/sm -q"
AZ=ABCDEFGHIJKLMNOPQRSTUVWXYZ
az=abcdefghijklmnopqrstuvwxyz

PGPVERIFY=`echo ${PGPVERIFY} | tr ${AZ} ${az}`
if [ X${PGPVERIFY} = Xtrue -o X${PGPVERIFY} = Xyes -o X${PGPVERIFY} = Xon ]; then
	PGPVERIFY=true 
else
	PGPVERIFY=false
fi

MAILFAILURES=`echo ${CONTROLFAILNOTICE} | tr ${AZ} ${az}`
if [ X${MAILFAILURES} = Xtrue -o X${MAILFAILURES} = Xyes -o X${MAILFAILURES} = Xon ]; then
	MAILFAILURES=true
else
	MAILFAILURES=false
fi

ZN=0123456789
# Attempt to sanitize the address
FROM="`echo \"$1\" | tr ${AZ} ${az} | tr -dc \\\055${az}${ZN}+_.@%`"
REPLYTO="`echo \"$2\" | tr ${AZ} ${az} | tr -dc \\\055${az}${ZN}+_.@%`"
case "$3" in
"")
    ARTICLE=/dev/null
    ;;
@*@)
    ARTICLE="$3"
    ;;
/*)
    ARTICLE="$3"
    ;;
*)
    ARTICLE="${SPOOL}/$3"
    ;;
esac
PATHHOST="$4"

# Catch an address left with a leading '-' just in case...
test -z "$MAILFAILURES" && MAILFAILURES=false
case "X$FROM" in
    X-*) 
        if $MAILFAILURES; then
            ${SM} ${ARTICLE} |${SED} -e 's/^~/~~/' \
                | ${MAILCMD} -s "Bad FROM address" ${NEWSMASTER}
        fi
        ${SM} ${ARTICLE} | \
            writelog $MOST_LOGS/badcontrol.log "`date` Bad FROM address"
        exit
        ;;
esac

case "X$REPLYTO" in
    X-*)
        if $MAILFAILURES; then
            ${SM} | ${SED} -e 's/^~/~~/' \
                | ${MAILCMD} -s "Bad REPLY-TO address" ${NEWSMASTER}
        fi
        ${SM} ${ARTICLE} | \
            writelog $MOST_LOGS/badcontrol.log "`date` Bad REPLY-TO address"
        exit
        ;;
esac

umask 002
TEMP=${TMPDIR}/ctl$$
##  Avoid egrep bugs with empty or-patterns.
test -z "${PROG}" && PROG=all
${EGREP} "^(${PROG}|all):" <${CTLFILE} >${TEMP}

##  Get any arguments.
if ${SM} ${ARTICLE} | grep "^Control:[ 	]*${PROG}" >/dev/null 2>&1 ; then
    set X `${SM} ${ARTICLE} | ${SED} -n -e "s/^Control:[ 	]*${PROG}//p" -e '/^$/q'  |tr -d '\015'`
    shift
else
    if ${SM} ${ARTICLE} | grep "^Subject:[ 	]*cmsg[ 	]*${PROG}" \
		 >/dev/null 2>&1 ; then
	set X `${SM} {$ARTICLE} | ${SED} -n -e "s/^Subject:[ 	]*cmsg[ 	]*${PROG}//p" \
		-e '/^$/q'`
	shift
    else
	rm -f ${TEMP}
	if $MAILFAILURES; then
		${SM} ${ARTICLE} | ${SED} -e 's/^~/~~/' \
			| ${MAILCMD} -s "Bad header by ${FROM}" ${NEWSMASTER}
	fi
	${SM} ${ARTICLE} |
	     writelog $MOST_LOGS/badcontrol.log "`date` Bad header by ${FROM}"
	exit
    fi
fi

# Check characters in values of variables that will be inside an eval
TRANS1="`echo \"$1\" | tr ${AZ} ${az} | tr -dc \\\055${az}${ZN}+_.`"
if [ ${1}X != ${TRANS1}X ]; then
    rm -f ${TEMP}
    if $MAILFAILURES; then
	${SM} ${ARTICLE} | ${SED} -e 's/^~/~~/' \
	    | ${MAILCMD} -s "Malformed newsgroup name by ${FROM}" ${NEWSMASTER}
    fi
    ${SM} ${ARTICLE} |
	writelog $MOST_LOGS/badcontrol.log \
	    "`date` Malformed newsgroup name by ${FROM}"
    exit
fi
TRANSP="`echo \"$PROG\" | tr ${AZ} ${az} | tr -dc \\\055${az}${ZN}+_.`"
if [ ${PROG}X != ${TRANSP}X ]; then
    rm -f ${TEMP}
    if $MAILFAILURES; then
	${SM} ${ARTICLE} | ${SED} -e 's/^~/~~/' \
	    | ${MAILCMD} -s "Unexpected program name by ${FROM}" ${NEWSMASTER}
    fi
    ${SM} ${ARTICLE} |
	writelog $MOST_LOGS/badcontrol.log \
	    "`date` Unexpected program name by ${FROM}"
    exit
fi

ACTION=mail
IFS=:
exec <${TEMP}
while read MESSAGE FROM_PAT NG_PAT ACT_PAT ; do
    eval "case \"${FROM}\" in
    ${FROM_PAT})
	##  Got a match -- if newgroup/rmgroup, must also
	##  match the newsgroup pattern.
	case \"${PROG}\" in
	newgroup|rmgroup)
	    case \"$1\" in
	    ${NG_PAT})
		ACTION=${ACT_PAT}
		;;
	    esac
	    ;;
	*)
	    ACTION=${ACT_PAT}
	    NEWSGROUPPATS=`echo X${NG_PAT} | ${SED} -e 's/^X//' -e 's/^/\\\\^/' -e 's/|/\\\\|\\\\^/g'`
	    ;;
	esac
    ;;
    esac"
done
rm -f ${TEMP}
IFS="`echo stn | tr stn ' \011\012'`"

LOGFILE=mail
case ${ACTION} in
drop)
    exit
    ;;
log)
    echo "`date` Ignored ${PROG} ${ARTICLE} by ${FROM} (${PATHHOST})" 1>&2
    exit
    ;;
log=*)
    LOGFILE=`expr "${ACTION}" : 'log=[	 ]*\(.*\)'`
    ACTION=logit
    ;;
doit=*)
    LOGFILE=`expr "${ACTION}" : 'doit=[	 ]*\(.*\)'`
    ACTION=doit
    ;;
verify-*=*)
    LOGFILE=`expr "${ACTION}" : 'verify-.*=[	 ]*\(.*\)'`
    PGPAUTH=`expr "${ACTION}" : 'verify-\([^=]*\)'`
    ACTION=verify
    ;;
verify-*)
    PGPAUTH=`expr "${ACTION}" : 'verify-\([^=]*\)'`
    ACTION=verify
    ;;
esac

if $PGPVERIFY; then
    PGPSENDER=`${SM} ${ARTICLE} | pgpverify`;
    PGP_STATUS=$?
    case "${ACTION}:${PGP_STATUS}" in
    verify:0)
        case "${PGPAUTH}" in
        "")
            ACTION=mail
	    LOGFILE=mail
            ;;
        ${PGPSENDER})
            ACTION=doit
            ;;
        *)
            ACTION=mail
	    LOGFILE=mail
            ;;
        esac       
        ;;
    # code 255 is failure to run pgp.
    verify:255)
	ACTION=logit
	LOGFILE=failedpgp
	;;
    # pgp ran, but failed to verify message.
    verify:*)
       ACTION=logit
       LOGFILE=badpgp
       ;;
    esac
fi

case "${LOGFILE}" in
"")
    LOGFILE=/dev/null
    ;;
mail|/*)
    ;;
*)
    LOGFILE=${MOST_LOGS}/${LOGFILE}.log
    ;;
esac
export LOGFILE
