#!/bin/sh
# $Id: Pnews.SH,v 3.0 1992/02/23 21:25:39 davison Trn $
# 
# This software is Copyright 1991 by Stan Barber. 
#
# Permission is hereby granted to copy, reproduce, redistribute or otherwise
# use this software as long as: there is no monetary profit gained
# specifically from the use or reproduction of this software, it is not
# sold, rented, traded or otherwise marketed, and this copyright notice is
# included prominently in any copy made. 
#
# The author make no claims as to the fitness or correctness of this software
# for any use whatsoever, and it is provided as is. Any use of this software
# is at the user's own risk. 
#
# syntax: Pnews -h headerfile			or
#	  Pnews -h headerfile oldarticle	or
#         Pnews newsgroup title			or just
#         Pnews

export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh $0; kill $$)

# System dependencies

mailer="${RNMAILER-/usr/lib/sendmail}"
# if you change this to something that does signatures, take out signature code

case define in
define)
    # where recordings, distributions and moderators are kept
    lib=`/usr/lib/trn/filexp /var/lib/news`
    # where important rn things are kept
    rnlib=`/usr/lib/trn/filexp /usr/lib/trn`
    artcheck=trn-artchk
    nntplist=nntplist
    ;;
undef)
    # where recordings, distributions and moderators are kept
    lib="/var/lib/news"
    # where important rn things are kept
    rnlib="/usr/lib/trn"
    artcheck=/usr/bin/trn-artchk
    nntplist=/usr/bin/nntplist
    ;;
esac

# your organization name
orgname="/etc/organization"
# what pager you use--if you have kernal paging use cat
pager="${PAGER-/bin/more}"
# how you derive full names, bsd, usg, or other
nametype="bsd"
# default editor
defeditor="/usr/bin/vi"
# how not to echo with newline
n="-n"
c=""

# You should also look at the distribution warnings below marked !DIST!
# to make sure any distribution regions you are a member of are included.
# The following are some prototypical distribution groups.  If you do not
# use them all set the unused ones to a non-null string such as 'none'.
loc="none"
org="none"
multistate="none"
city="none"
state="none"
cntry="none"
cont="none"

active=/var/lib/news/active
test=test
sed=sed
echo=echo
cat=cat
egrep=egrep
grep=grep
tr=tr
inews=/usr/bin/inews
cp=cp
sleep=sleep
rm=rm
who=
mv=mv

nidump=
ypmatch=

orgname=${NEWSORG-${ORGANIZATION-$orgname}}
homedir=${HOME-$LOGDIR}
dotdir=${DOTDIR-$homedir}
tmpart=$dotdir/.article
speller=$rnlib/Speller

tmp="${TMPDIR-/tmp}"
newsgroups=${NEWSGROUPS-$lib/newsgroups}
active=${NEWSACTIVE-$active}
news_sig=${NEWSSIGNATURE-$dotdir/.news_sig}

if $test ! -f "$newsgroups"; then
    newsgroups="$tmp/Pnng.$$";
    rmlist="$newsgroups";
    cmdlist="$nntplist newsgroups $newsgroups ;";
fi
if $test ! -f "$active"; then
    active="$tmp/Pnact.$$";
    rmlist="$rmlist $active";
    cmdlist="$cmdlist $nntplist active $active";
fi

if $test -f $dotdir/.pnewsexpert; then
    expertise=expert
else
    $cat <<'EOM'
I see you've never used this version of Pnews before.  I will give you extra
help this first time through, but then you must remember what you learned.
If you don't understand any question, type h and a CR (carriage return) for
help.

If you've never posted an article to the net before, it is HIGHLY recommended
that you read the netiquette document found in news.announce.newusers so
that you'll know to avoid the commonest blunders.  To do that, interrupt
Pnews, get to the top-level prompt of [t]rn, and use the command
"g news.announce.newusers" to go to that group.

EOM
    expertise=beginner
fi

case $cntry in
  can) Stpr=Province ; stpr=province ;;
  *)   Stpr=State ; stpr=state ;;
esac

case $multistate in
  pnw) multistpr="Pacific NorthWest" ;;
  *)   multistpr="Multi-State Area" ;;
esac

headerfile=""
case $# in
0) ;;
*)  case $1 in
    -h)
	headerfile="$2"
	shift
	shift
	case $# in
	0)
	    oldart=""
	    ;;
	*)
	    oldart="$1"
	    shift
	    ;;
	esac
	;;
    esac
    ;;
esac

case $headerfile in
'')
    . $rnlib/Pnews.header
    ;;
*)
    $cat < $headerfile  > $tmpart
    ;;
esac
    rescue="$sleep 1; $cat $tmpart >>${HOME-$LOGDIR}/dead.article ; $rm -f $rmlist ; $echo Article appended to ${HOME-$LOGDIR}/dead.article ; exit"
    if $test -n "$rmlist" ; then
	trap "$rm -f $rmlist" 0
    fi
    trap "trap : 1; $rescue" 1
    trap "$rescue" 2

$echo ""

# extract the newsgroups list and distribution
hdr_newsgroups=`$sed -n -e '/^Newsgroups:/{' -e 's///' -e 's/,/ /g' -e p -e q -e '}' $tmpart`
hdr_distribution=`$sed -n -e '/^Distribution:/{' -e 's///' -e p -e q -e '}' $tmpart`

# check for "poster" magic cookie.  Allow erroneous user@site too.
flag=0
for ng in $hdr_newsgroups ; do
    case "$ng" in
	poster)	flag=1 ;;
	*@*) flag=1 ;;
	*)	;;
    esac
done
case $flag in
1)
    $cat <<'EOM'

The original author has requested that messages be sent back via mail
rather than posting to news.  Do you want to jump out of this and
EOM
    $echo $n "mail your reply instead? [yn] $c"
    read ans
    case $ans in
	n*) tweak="$tmp/Pntwk.$$"
	    $sed '1,/^[	 ]*$/{/^Newsgroups:/d;
		s/^X-ORIGINAL-NEWSGROUPS:/Newsgroups:/;}' $tmpart >$tweak
	    $cp $tweak $tmpart
	    $rm -f $tweak
	    ;;
	*)  exit ;;
    esac
    $echo " "
    $echo "The Cc: line should have the poster's requested return address."
    ;;
esac
  
# play recorded message
if $test -s ${lib}/recording ; then
     for ng in $hdr_newsgroups ; do
	_rec1=${lib}/`$sed -n "/^$ng/s/^.*	//p" ${lib}/recording`
	_tmp=`$echo $ng |$sed "s/\..*//"`
	_rec2=${lib}/`$cat -s ${lib}/recording|$grep ${_tmp}.all|$sed "s/^.*	//"`
	if $test -f ${_rec1} ; then
	    $cat -s ${_rec1}
	fi
	if $test -f ${_rec2} ; then
	    $cat -s ${_rec2}
	fi
    done
fi

# determine the distribution of this message
set X $hdr_distribution
shift
if $test $# -gt 0 ; then
    dist=$1.whatever
else
    set X $hdr_newsgroups
    shift
    if $test $# -gt 0 ; then
	dist=$1.whatever
    else
	dist=misc.whatever
    fi
fi
case $dist in
*.*)
    ;;
*)
    dist=$dist.whatever
    ;;
esac

case "$FAST_PNEWS" in
y*) ;;
*)
# tell them what we think they are doing... !DIST!
case $dist in
world.*|comp.*|news.*|sci.*|rec.*|misc.*|soc.*|talk.*|alt.*|'')
    $cat <<'EOM'
This program posts news to thousands of machines throughout the entire
civilized world.  Your message will cost the net hundreds if not thousands of
dollars to send everywhere.  Please be sure you know what you are doing.

EOM
    ;;
vmsnet.*)
    $echo 'This program posts news to many machines.'
    ;;
bit.*)
    $echo 'This program posts news to many machines on BITNET.'
    ;;
ddn.*)
    $echo 'This program posts news to many machines throughout the internet.'
    ;;
$cont.*)
    $echo 'This program posts news to many machines throughout the continent.'
    ;;
$cntry.*)
    $echo 'This program posts news to many machines throughout the country.'
    ;;
$multistate.*)
    $echo "This program posts news to many machines throughout the ${multistpr}."
    ;;
$state.*)
    $echo "This program posts news to many machines throughout the ${stpr}."
    ;;
$city.*)
    $echo 'This program posts news to many machines throughout the city.'
    ;;
$org.*)
    $echo 'This program posts news to machines throughout the organization.'
    ;;
$loc.*)
    $echo 'This program posts news to machines throughout the local organization.'
    ;;
*.*)
    $echo 'This program may post news to many machines.'
    ;;
to.*)
    $echo 'This program may post news to a particular machine.'
    ;;
*)
    $echo 'This program posts news to everyone on the machine.'
    ;;
esac
ans=""
while $test "X$ans" = X ; do
    $echo $n "Are you absolutely sure that you want to do this? [ny] $c"
    read ans
    case $ans in
    y*) ;;
    f*) ;;
    h*) $cat <<'EOH'

Type n or CR to exit, y to post.

EOH
	ans="" ;;
    *) exit ;;
    esac
done
;;
esac

# run nntplist in the background, if necessary
if $test -n "$cmdlist"; then
    ( eval $cmdlist ) >/dev/null 2>&1 &
fi

case "$FAST_PNEWS" in
y*) file=''
    $echo "" >> $tmpart
    state=edit
    ;;
*)  file=h;;
esac
while $test "X$file" = Xh ; do
    $echo ""
    $echo $n "Prepared file to include [none]: $c"
    read file
    case $file in
    h)
	$cat <<'EOH'

If you have already produced the body of your article, type the filename
for it here.  If you just want to proceed directly to the editor, type a
RETURN.  In any event, you will be allowed to edit as many times as you
want before you send off the article.
EOH
	;;
    '')
	$echo "" >> $tmpart
	state=edit
	;;
    *)
	$cat $file >>$tmpart
	state=check
	;;
    esac
done

$echo ""

if $test -r $news_sig; then
    $echo "-- " >> $tmpart
    $sed 4q $news_sig >> $tmpart
fi

while : ; do
    case $state in
    edit)
	case $expertise in
	beginner)
	    $cat </dev/null >$dotdir/.pnewsexpert
	    $cat <<'EOMessage'
A temporary file has been created for you to edit.  Be sure to leave at
least one blank line between the header and the body of your message.
(And until a certain bug is fixed all over the net, don't start the body of
your message with any indentation, or it may get eaten.)

Within the header may be fields that you don't understand.  If you don't
understand a field (or even if you do), you can simply leave it blank, and
it will go away when the article is posted.

Type return to get the default editor, or type the name of your favorite
editor.

EOMessage
	    ;;
	esac
	case "${VISUAL-${EDITOR-}}" in
	'')
	    tmp=h
	    ;;
	*)
	    tmp=''
	    ;;
	esac
	while $test "X$tmp" = Xh ; do
	    $echo $n "Editor [${VISUAL-${EDITOR-$defeditor}}]: $c"
	    read tmp
	    case $tmp in
	    h)
		$cat <<'EOH'

Type a return to get the default editor, or type the name of the editor you
prefer.  The default editor depends on the VISUAL and EDITOR environment
variables.

EOH
		;;
	    '')
		;;
	    *)
		VISUAL=$tmp
		export VISUAL
		;;
	    esac
	done
	trap : 2
	${VISUAL-${EDITOR-$defeditor}} $tmpart $oldart
	trap "$rescue" 2
	state=check
	;;
	
    check)
	# wait for possible background nntplist
	$test -n "$cmdlist" && wait

	# warn about long lines, malformed headers, misspelled newsgroups
	($artcheck $tmpart 79 $newsgroups $active) 2>/dev/null
	state=ask
	;;

    ask)
	$echo ""
	$echo $n "Check spelling, Send, Abort, Edit, or List? $c"
	read ans

	case "$ans" in
	[aA]*)
	    state=rescue
	    ;;
	[eE]*)
	    set $ans
	    case $# in
	    2)  VISUAL="$2" ;;
	    esac
	    state=edit
	    ;;
	[lL]*)
	    $pager $tmpart
	    state=ask
	    ;;
	[cC]*|[sS][pP]*)
	    $speller $tmpart
	    state=ask
	    ;;
	[sS]*)
	    state=send
	    ;;
	[hH]*)
	    $cat <<'EOH'

Type c to check the article's spelling, s to send the article, a to abort
and append the article to dead.article, e to edit the article again, or l
to list the article with your pager.

To invoke an alternate editor, type 'e editor'.
EOH
	esac
	;;
    
    send)
	set X `$sed < $tmpart -n -e '/^Newsgroups: /{' -e p -e q -e '}'`
	shift
	case $# in
	2)
	    state=cleanup
	    if $test -f $lib/moderators; then
		tryinews=no
		shift
		case "$1" in
		*,*) set `$echo $1 | $tr ',' ' '`;;
		esac
		for newsgroup in $*; do
# the following screwy sed should prevent Eunice from hanging on no match
		    moderator=`$sed <$lib/moderators \
		    -e "/^$newsgroup[ 	]/!s/.*//" \
		    -e "s/^$newsgroup[ 	]//"`
		    case ${moderator}X in
		    X)  tryinews=yes
			;;
		    *)
			$echo Mailing to moderator $moderator
			case "$sign" in
			n*) ;;
			*)
			    if $test -f $homedir/.signature; then
				$echo $n "Append .signature file? [y] $c"
				read ans
				case $ans in
				''|y*)
				    $echo "-- " >> $tmpart
				    $cat $homedir/.signature >> $tmpart
				    ;;
				esac
			    fi
			    sign=no
			    ;;
			esac
			case "$mailer" in
			*recmail)
			    $echo To: $moderator | $cat - $tmpart | $mailer
			    ;;
			*)
			    $mailer $moderator < $tmpart
			    ;;
			esac
			case $? in
			0) ;;
			*)
			    $echo Unable to mail to moderator $moderator
			    state=ask
			    ;;
			esac
			;;
		    esac
		done
	    else
		tryinews=yes
	    fi
	    case "$tryinews" in
	    yes)
		headerstrip='1,/^[	 ]*$/{/^[A-Z][-A-Za-z0-9]*:[	 ]*$/d;
			/^X-ORIGINAL-NEWSGROUPS:.*$/d;
			/^[Cc][Cc]:/d;
			/^Distribution: world/d;}'
		if $sed "$headerstrip" $tmpart | $inews -h ; then
		    : null
		else
		    state=ask
		fi
		cc=`$sed -n '1,/^[	 ]*$/{/^[Cc][Cc]:[	 ][^	 ]/p;}' $tmpart|
		 $sed 's/^[Cc][Cc]:[	 ][	 ]*//'`
		if $test "X$cc" != X ; then
		    set X $cc
		    shift
		    case "$mailer" in
		    *recmail)
			($echo To: $cc
			 $sed "$headerstrip" $tmpart
			 if $test -f $homedir/.signature; then
			    $echo "-- " >> $tmpart
			    $cat $homedir/.signature >> $tmpart
			 fi) | $mailer
			;;
		    *)
			set X `echo $cc | $sed 's/,/ /g'`
			shift
			($sed "$headerstrip" $tmpart
			 if $test -f $homedir/.signature; then
			    $echo "-- " >> $tmpart
			    $cat $homedir/.signature >> $tmpart
			 fi) | $mailer $@
			;;
		    esac
		fi
		;;
	    esac
	    ;;
	*)
	    $echo ""
	    $echo "Malformed Newsgroups line."
	    $echo ""
	    $sleep 1
	    state=edit
	    ;;
	esac
	;;
    rescue)
	if $test -s $tmpart; then
		$cat $tmpart >> ${HOME-$LOGDIR}/dead.article
		$echo "Article appended to ${HOME-$LOGDIR}/dead.article"
		$echo "A copy may be temporarily found in $tmpart"
	else
		$echo "Null article discarded."
	fi
	exit
	;;
    cleanup)
	case "${AUTHORCOPY-none}" in
	none)
	    ;;
	*)
	    set X ${USER-${LOGNAME-`$who am i`}} unknown
	    shift
	    $rnlib/mbox.saver $tmpart "." "." 0 0 Pnews $AUTHORCOPY "From $1 `LANG= date`"
	    if $test $? -eq 0 ; then
		$echo "Article appended to $AUTHORCOPY"
	    else
		$echo "Cannot append to $AUTHORCOPY"
	    fi
	    ;;
	esac
	exit
	;;
    esac
done
