#! /bin/sh
# NOTE: this script needs to be run by the news user.
# Debian systems will not allow normal users to run ctlinnd and innxmit or rnews.
# Script distributed with suck but modified by Christophe Le Bars for the Debian 
# package.
# authinfo compatibility by Brian Mays <brian@debian.org>.

# fix for it using NNTP_USER and NNTP_PASS instead of passing it on
# the command line.  More secure, by Tollef Fog Heen <tfheen@debian.org>

COMMAND=$0
ETCDIR=/etc/suck			# location of sucknewsrc* and killfile*
GETNEWSCONF=${ETCDIR}/get-news.conf	# defaults for this script
EXIT_STATUS='exit 0'

if [ -z "${PAGER}" ]; then
        PAGER=more
fi

LOCAL_SERVER=`grep ^server: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c8-`
REMOTE_SERVER=`grep ^remoteserver: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c14-`
REMOTE_PORT=`grep ^remoteport: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c12-`
SITE=`grep ^outgoingfile: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -c14-`

# see if SITE_userid exists.  If so, use if.  Else use the standard.
USER=`grep ^${SITE}_userid: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -d: -f 2-`
if [ ! "$USER" ]; then
    USER=`grep ^userid: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -d: -f 2-`
fi

# see if SITE_password exists.  If so, use if.  Else use the standard.
PASSWD=`grep ^${SITE}_password: ${GETNEWSCONF} \
		| awk -F: '{gsub(" ","");print}' | cut -d: -f 2-`
if [ ! "$PASSWD" ]; then
    PASSWD=`grep ^password: ${GETNEWSCONF} \
		| awk '{gsub(" ","");print}' | cut -d: -f 2-`
fi
OPTS=`grep ^suckoptions: ${GETNEWSCONF} | cut -c13-`
ROPTS=`grep ^rpostopstions: ${GETNEWSCONF} | cut -c14-`

# Per site options

if grep -q "^${SITE}_suckoptions:" ${GETNEWSCONF}; then
    OPTS="`grep ^${SITE}_suckoptions: ${GETNEWSCONF} | cut -d: -f 2-`"
fi
if grep -q "^${SITE}_rpostopstions:" ${GETNEWSCONF}; then
    ROPTS="`grep ^${SITE}_rpostopstions: ${GETNEWSCONF} | cut -d: -f 2-`"
fi
if grep -q "^{SITE}_remoteport:" ${GETNEWSCONF}; then
    REMOTE_PORT="`grep -q "^{SITE}_remoteport:" ${GETNEWSCONF} | cut -d: -f 2-`"
fi

REMOTE_SERVER_DEFAULT=${REMOTE_SERVER}

if [ -z "${LOCAL_SERVER}" ]; then
        LOCAL_SERVER=localhost
fi

if [ -z "${REMOTE_PORT}" ]; then
	REMOTE_PORT=119
fi

while [ ! -z "$1" ]; do
case "$1" in
  -outgoingfile)
                shift
                SITE_OPTION=$1
                shift
    ;;
  -server)
                shift
                LOCAL_SERVER=$1
                shift
    ;;
  -getonly)
                shift
                GETONLY=1
    ;;
  -postonly)
                shift
                POSTONLY=1
    ;;
  -userid)
		shift
		USER=$1
		shift
    ;;
  -password)
		shift
		PASSWD=$1
		shift
    ;;
  -noauth)
		shift
		USER=""
		PASSWD=""
    ;;
  -port)
		shift
		REMOTE_PORT=$1
		shift
    ;;
  -q)
		shift
		echo ${OPTS} | grep -- -q - > /dev/null && echo() {
		    /bin/echo "$@" > /dev/null ;
		}
		OPTS="$OPTS -q"

    ;;
  -h)
	$PAGER <<__END__
Usage:  $COMMAND [-option parm] [<sitename>]

    <sitename>		The NNTP server you will connect to.
                        (if nothing, $COMMAND read ${GETNEWSCONF})

Options:
    -outgoingfile	<filename>  	The file of your remote server
					outgoing articles.
                            		(default = the remote server name)
    -userid		<userid>	The userid to send to the remote
					server.
    -password		<password>	The password to send to the remote
					server.
    -noauth				Do not send userid and password.
					(even if they are specified in
					${GETNEWSCONF})
    -port               <port>          Set remote port number.
    -server    		<sitename>  	Your local NNTP server.
                            		(default = "$LOCAL_SERVER")
    -q                                  Do not display the BPS and article
                                        count messages during download.  
                                        Multiple -q makes $COMMAND silent.
    -getonly                            Only get new messages, do not post
    -postonly                           Only post outgoing messages, do not get
                                        new ones
__END__
	exit 0
    ;;
  *)
                REMOTE_SERVER=$1
                shift
    ;;
esac
done

if [ -z "${SITE}" ]; then
        SITE=${REMOTE_SERVER}
fi

if [ ! "${REMOTE_SERVER_DEFAULT}" = "${REMOTE_SERVER}" ]; then
        SITE=${REMOTE_SERVER}
fi

if [ ! -z "${SITE_OPTION}" ]; then
        SITE=${SITE_OPTION}
fi

if [ ! -z "${USER}" ]; then
        NNTP_USER="${USER}"
	AUTHOPTS="-Q"
	export NNTP_USER
fi
if [ ! -z "${PASSWD}" ]; then
        NNTP_PASS="${PASSWD}"
	AUTHOPTS="-Q"
	export NNTP_PASS
fi

BINDIR=/usr/bin				# base directory for suck rpost and testhost
RPOST=${BINDIR}/rpost			# my rpost
SUCK=${BINDIR}/suck			# my suck
TESTHOST=${BINDIR}/testhost		# my testhost
SCRIPT=/usr/lib/suck/put-news		# my filter for rpost

NEWSDIR=/usr/lib/news			# base directory for INN news binaries 

# Don't use innxmit any more - it's deprecated since inn2 suddenly
# changed it's mind about it.

# INNXMIT=${NEWSDIR}/bin/innxmit	# location of innxmit
CTLINND=${NEWSDIR}/bin/ctlinnd		# location of ctlinnd
RNEWS=${BINDIR}/rnews   		# location of rnews

# location of the list of articles to upload
NEWSSPOOLDIR=/var/spool/news		# base directory for articles to be rposted
[ -d ${NEWSSPOOLDIR}/.outgoing ] && OUTGOING=${NEWSSPOOLDIR}/.outgoing/${SITE}
[ -d ${NEWSSPOOLDIR}/out.going ] && OUTGOING=${NEWSSPOOLDIR}/out.going/${SITE}
[ -d ${NEWSSPOOLDIR}/outgoing ] && OUTGOING=${NEWSSPOOLDIR}/outgoing/${SITE}
OUTGOINGNEW=${OUTGOING}.new		# temporarily used during upload of articles

STATEDIR=/var/lib/suck				# location for suck state files
OUTFILE=${STATEDIR}/rpost.${REMOTE_SERVER}.$$	# used by rpost as article after it is filtered
BATCHFILE=${STATEDIR}/batch.${REMOTE_SERVER}.$$	# Name of batchfile to build for rnews or innxmit

SPOOLDIR=/var/spool/suck			# location for suck spool
MSGDIR=${SPOOLDIR}/${REMOTE_SERVER}		# where to put MultiFile articles when getting them

LOGDIR=/var/log/suck				# location for suck log files
ERRLOG=${LOGDIR}/errlog
GETNEWSLOG=${LOGDIR}/get-news.log
LASTDIR=${LOGDIR}/${REMOTE_SERVER}		# last get-news suck files

if [ -f "${INNXMIT}" ]; then 
	BATCHMODE="-bi"
elif [ -f "${RNEWS}" ]; then 
	BATCHMODE="-br"
else
    echo "I don't know what kind of download we want!"
    exit 1
fi


# is the local host up and running so we can post articles we download?
${TESTHOST} ${LOCAL_SERVER} -s -e
LOCAL_RESULT=$?
if [ ${LOCAL_RESULT} -ne 0 ]; then
	echo "Bad status for the ${LOCAL_SERVER} local NNTP news server"
	exit 1
fi

# is the remote host up and running so we can download articles?
${TESTHOST} ${REMOTE_SERVER} -N ${REMOTE_PORT} ${AUTHOPTS} -s -e
REMOTE_RESULT=$?
if [ ${REMOTE_RESULT} -ne 0 ]; then
	echo "Bad status for the ${REMOTE_SERVER} remote NNTP news server"
	exit 2
fi

[ -d ${LASTDIR} ] || mkdir ${LASTDIR}

if [ ! "${POSTONLY}" ]; then

    date >> ${GETNEWSLOG}
    if [ "${REMOTE_SERVER_DEFAULT}" = "${REMOTE_SERVER}" ]; then
	    echo 'get-news: download articles with the default remote server' >> ${GETNEWSLOG}
	    ${SUCK} ${REMOTE_SERVER} ${OPTS} ${AUTHOPTS} ${BATCHMODE} ${BATCHFILE} -dt ${STATEDIR} -dm ${MSGDIR} -dd ${ETCDIR} -N ${REMOTE_PORT} -E ${ERRLOG} 

	    SUCK_STATUS=$?
	    SUFFIX=""
    else
	    echo 'get-news: download articles' >> ${GETNEWSLOG}

	    ${SUCK} ${REMOTE_SERVER} ${OPTS} ${AUTHOPTS} ${BATCHMODE} ${BATCHFILE} -dt ${STATEDIR} -dm ${MSGDIR} -dd ${ETCDIR} -p .${REMOTE_SERVER} -N ${REMOTE_PORT} -E ${ERRLOG}

	    SUCK_STATUS=$?
	    SUFFIX=".${REMOTE_SERVER}"
    fi
    if [ ${SUCK_STATUS} -eq 0 ]; then
	    echo "Downloaded Articles"
	    mv ${ETCDIR}/sucknewsrc${SUFFIX} ${ETCDIR}/sucknewsrc${SUFFIX}.old
	    mv ${STATEDIR}/suck.newrc${SUFFIX} ${ETCDIR}/sucknewsrc${SUFFIX}
	    if [ -f ${STATEDIR}/suck.sorted${SUFFIX} ]; then	
		    mv ${STATEDIR}/suck.sorted${SUFFIX} ${LASTDIR}
	    fi
	    if [ -f ${STATEDIR}/suck.killlog${SUFFIX} ]; then
		    mv ${STATEDIR}/suck.killlog${SUFFIX} ${LASTDIR}
	    fi
	    if [ -f ${ETCDIR}/suckothermsgs${SUFFIX} ]; then
		    mv ${ETCDIR}/suckothermsgs${SUFFIX} ${LASTDIR}
	    fi
    fi
fi

# location of articles
if [ -d ${NEWSSPOOLDIR}/articles ]; then
    OUTGOINGPREFIX=${NEWSSPOOLDIR}/articles
else
    OUTGOINGPREFIX=${NEWSSPOOLDIR}
fi

if [ -f ${BATCHFILE} ]; then	
# locally post articles
    if [ -f "${INNXMIT}" ]; then
        ${INNXMIT} ${LOCAL_SERVER} ${BATCHFILE}
    else
        NNTPSERVER=${LOCAL_SERVER} ${RNEWS} < ${BATCHFILE}
    fi
    if [ $? -ne 0 ]; then
        echo "Local posting error"
        EXIT_STATUS='exit -1'
    else
        echo "Posted Articles Locally"
        rm -rf ${MSGDIR} ${BATCHFILE}
   fi
fi	

if [ ! ${GETONLY} ]; then

    # now upload articles - first check whether we have some from last time.
    if [ -s ${OUTGOINGNEW} ]; then
	${RPOST} ${REMOTE_SERVER} -N ${REMOTE_PORT} ${AUTHOPTS} -E ${ERRLOG} ${ROPTS} -b ${OUTGOINGNEW} -p ${OUTGOINGPREFIX} -f ${SCRIPT} \$\$o=${OUTFILE}  \$\$i ${OUTFILE}
	if [ $? -ne 0 ]; then
	    echo "Error remote posting"
	    EXIT_STATUS='exit -1'
	else
	    rm -f ${OUTFILE} ${OUTGOINGNEW}
	fi
    fi

    ${CTLINND} flush ${SITE}

    if [ -s ${OUTGOING} ]; then

	# this is needed by INND so that the outgoing file will be
	# properly flushed and we have a new blank file to work with
	# when we are done
	# First mv the current one to a new file name
	# Since innd already has the file open, it doesn't care 
	# about the rename.
	# The flush will ensure that all articles to be posted have
	# been written out, close off the old one (already renamed)
	# and create a new one.

	mv ${OUTGOING} ${OUTGOINGNEW}
	${CTLINND} flush ${SITE}
	echo "reloaded ctlinnd"

	# outgoing articles to post
	${RPOST} ${REMOTE_SERVER} -N ${REMOTE_PORT} ${AUTHOPTS} -E ${ERRLOG} ${ROPTS} -b ${OUTGOINGNEW} -p ${OUTGOINGPREFIX} -f \$\$o=${OUTFILE} ${SCRIPT} \$\$i ${OUTFILE}

	if [ $? -ne 0 ]; then
	    echo "Error remote posting"
	    EXIT_STATUS='exit -1'
	else
	    echo "Remotely posted articles"
	    rm -f ${OUTFILE} ${OUTGOINGNEW}
	fi
    fi	

    echo "You can hang up the modem now"

fi

$EXIT_STATUS
