#! /bin/sh
# inews [-p] [-debug k] [-x site] [-hMD] [-t subj] [-n ng] [-e exp] [-F ref] \
#  [-d dist] [-a mod] [-f from] [-o org] [-C ng] [file...] - backward
#	compatible news injection interface supporting all the old sludge:
#	censor locally-posted article and field the "inews -C" kludge;
#	munge the articles, enforce feeble attempts at Usenet security,
#	generate lots of silly headers.
#
# Yes, it's slow and awkward.  The alternative is casting a lot of
# local policy in C.

# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
. ${NEWSCONFIG-/usr/local/lib/news/bin/config}
export NEWSCTL NEWSBIN NEWSARTS NEWSPATH NEWSUMASK NEWSMASTER NEWSCONFIG
PATH=$NEWSCTL/bin:$NEWSBIN/inject:$NEWSBIN:$NEWSPATH
export PATH

injnewsopts=
hdrspresent=no

input=/tmp/in$$in		# uncensored input
inhdrs=/tmp/in$$hdr		# generated by tear: headers
inbody=/tmp/in$$body		# generated by tear: body
rmlist="$inhdrs $inbody $input"

umask $NEWSUMASK

# "inews -p": invoke rnews
case "$1" in
-p)
	shift
	
	PATH=$NEWSCTL/bin:$NEWSBIN/relay:$NEWSBIN:$NEWSPATH
	export PATH
	exec relaynews -r $*	# feed directly to relaynews, seat belts off
	;;
esac

# process arguments: for options, cat headers onto $input; cat files onto $input
>$input

### option processing ###

while :
do
	case $# in
	0)	break ;;		# arguments exhausted
	esac

	case "$1" in
	# peculiar to C news
	-debug|-x)	injnewsopts="$injnewsopts $1 ''$2"; shift ;;
	-[ANV])		injnewsopts="$injnewsopts $1" ;;
	-W)	;;			# obsolete
	# useful standard options
	-h)	hdrspresent=yes ;;
	# silly options supplied by newsreaders
	-a)	shift; echo "Approved: $1"	>>$input ;;
	-c)	shift; echo "Control: $1"	>>$input ;;
	-d)	shift; echo "Distribution: $1"	>>$input ;;
	-e)	shift; echo "Expires: $1"	>>$input ;;
	-f)	shift; echo "From: $1"		>>$input ;;
	-n)	shift; echo "Newsgroups: $1"	>>$input ;;
	-o)	shift; echo "Organization: $1"	>>$input ;;
	-r)	shift; echo "Reply-To: $1"	>>$input ;;
	-t)	shift; echo "Subject: $1"	>>$input ;;	# aka Title:
	-D)	# obsolete, undocumented: meant "don't check for recordings".
		# last present in B 2.10.1, invoked by readnews for followups.
		;;
	-F)	# undocumented in B 2.10.1, documented in B 2.11.
		shift; echo "References: $1" >>$input ;;
	-M)	# this apparently just sets From: to the author of the article
		# instead of the poster (moderator), by leaving the From: line
		# alone (under -h); easy to implement.
		;;

	-C)
		cat <<eg >&2
$0: you either want to use addgroup (see newsaux(8)) to make
$0: $2 locally, or this to make it network-wide:
injnews <<'!'
Control: newgroup $2
Subject: newgroup $2
Newsgroups: $2
Approved: you@hostname

Non-empty.
!
eg
		exit 1
		;;
	-*)
		echo "$0: bad option $1; usage too large, see inews(1)" >&2
		exit 1
		;;
	*)	break ;;			# is a filename
	esac
	shift					# pass option
done

### collect input ###

# B 2.11 kludge: assume -h if input starts with headers.
# apparently the B 2.11 newsreaders assume this.
tear /tmp/in$$ $*
if test -s $inhdrs; then
	hdrspresent=yes
fi
case "$hdrspresent" in
no)	echo "" >>$input; hdrspresent=yes ;;
esac
# capture incoming news in case relaynews fails
if cat $inhdrs $inbody >>$input; then
	: far out
else
	echo "$0: lost news; cat status $?" >&2
	exit 1
fi

### the input is now fully assembled in $input; beat it up ###

exec <$input
rm -f $rmlist
exec injnews $injnewsopts
