#! /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...] - inject news:
#	censor locally-posted article and field the "inews -C" kludge;
#	munge the articles, enforce bogus and pathetic attempts at
#	Usenet security, generate lotsa silly headers.
# =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
. ${NEWSCONFIG-/usr/local/lib/news/config}
export NEWSCTL NEWSBIN NEWSARTS NEWSPATH NEWSUMASK NEWSMASTER NEWSCONFIG
PATH=$NEWSCTL/bin:$NEWSBIN/inject:$NEWSBIN/relay:$NEWSBIN:$NEWSPATH; export PATH
PASSEDFROM='';	export PASSEDFROM	# passed to anne.jones in environ.

debug=''			# flags
exclusion=''
hdrspresent=no
autopost=no
waitcmd=''
relayopts=-i			# redirect stdout to log

whoami=/tmp/in$$who		# just created to determine effective uid
input=/tmp/in$$in		# uncensored input
inhdrs=/tmp/in$$hdr		# generated by tear: headers
inbody=/tmp/in$$body		# generated by tear: body
censart=/tmp/in$$cens		# censored input
nglist=/tmp/in$$ngs		# newsgroups: list
modroute=/tmp/in$$route		# route to moderator's forwarder
exitflag=/tmp/in$$exit		# exit status, if present
outfile=/tmp/in$$out		# relaynews stdout
grpok=/tmp/in$$grp		# flag file: groups okay if present
rmlist="$inhdrs $inbody $input $censart $nglist $modroute $exitflag $outfile $grpok"

umask $NEWSUMASK

# "inews -p": invoke rnews
case "$1" in
-p)
	shift
	exec rnews $*		# rnews, bailing out at or near line 1
	;;
esac

# parse arguments: for options, cat headers onto $input; cat files onto $input
>$input
cleanup="test ! -f $HOME/dead.article -o -w $HOME/dead.article &&
  cat $input >>$HOME/dead.article &&
  { echo $0: article in $HOME/dead.article >&2; rm -f $rmlist; }; exit 1"
trap "$cleanup" 0 1 2 3 15
while :
do
	case $# in
	0)	break ;;		# arguments exhausted
	esac

	case "$1" in
	# peculiar to C news
	-debug)	shift; debug="$1" ;;
	-A)	autopost=yes ;;		# wait for free space
	-V)	relayopts= ;;		# verbose: don't redirect stdout (or stderr)
	-W)	waitcmd=wait ;;		# wait for completion
	# useful standard options
	-h)	hdrspresent=yes ;;
	-x)	shift; exclusion="-x $1" ;;	# you're welcome, erik (2.11)
	# 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 ;;
	-n)	shift; echo "Newsgroups: $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.
		;;

	# pass next options as environment variables to anne.jones
	-f)	shift; PASSEDFROM="$1" ;;	# complex due to Sender:
	-o)	shift; ORGANIZATION="$1"; export ORGANIZATION ;;

	-C)	# megakludge-o-rama
		# first, permit only to super-users
		>$whoami
		case "`ls -l $whoami | awk '{print $3}'`" in
		root)	: a winner ;;
		*)
			echo "$0: only super-users may create news groups" >&2
			exit 1
			;;
		esac
		rm -f $whoami

		inewsopt="$1"		# for use in message body
		shift			# skip -C to get ng as $1

		cat <<! >>$input	# generate a control message
Newsgroups: $1
Control: newgroup $1
Subject: newgroup $1
Approved: above-user@above-host

This article generated by inews $inewsopt $1.
!
		;;
	-*)
		echo "$0: bad option $1" >&2
		exit 1
		;;
	*)					# is a filename; append file
		# B 2.11 kludge: assume -h if input starts with headers.
		# apparently the B 2.11 newsreaders assume this.
		tear /tmp/in$$ <$1
		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
		fileseen=yes
		;;
	esac
	shift		# pass option or filename (any value was done above)
done

# if no files named, read stdin
case "$fileseen" in
yes)	;;
*)
	# 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
	;;
esac
trap '' 1 2 15			# ignore signals to avoid losing articles

# run the remainder in the background for the benefit of impatient people
# who lack a window system
(
trap "$cleanup" 0
tear /tmp/in$$ <$input		# output in $inhdrs and $inbody
# pad zero-line articles, since old B [ir]news are confused by them
# and the news readers generate zero-line control messages, alas.
if test ! -s $inbody; then
	(echo '';
	 echo This article was probably generated by a buggy news reader.) \
	 >$inbody
fi

# deduce which tr we have: v6 or v7
case "`echo B | tr A-Z a-z `" in
b)	trversion=v7 ;;
B)	trversion=v6 ;;			# or System V
esac
export trversion

# post with new headers and .signature
(anne.jones <$inhdrs		# bash headers
 # echo "Lines: `		# sop to msb, just uncomment to use
 # if test -r $HOME/.signature; then
 #	(cat $inbody; echo "-- "; sed 4q $HOME/.signature) | wc -l
 # else
 #	wc -l <$inbody
 # fi
 # `"

 # strip invisible chars from body, a la B news
 case "$trversion" in
 v7)	tr -d '\1-\7\13\14\16-\37' ;;
 v6)	tr -d '[\1-\7]\13\14[\16-\37]' ;;
 esac <$inbody

# the NNTP server does this.
# if test -r $HOME/.signature; then
#	echo "-- "; sed 4q $HOME/.signature	# glue on first bit of signature
# fi
) >$censart

# do the deed...

$NEWSBIN/nntp/inews <$censart
status=$?

case "$status" in
0)
	rm -f $rmlist			# far out, it worked: clean up
	trap 0				# normal exit: cleanup done
	;;
esac
exit $status				# trap 0 may cleanup, make dead.article
) &
$waitcmd				# wait if -W given
trap 0					# let the background run on unmolested
exit
