From: jamesd@techbook.com (James Deibele)
Subject: Re: .QWK mail reader for Waffle (part 2)
Keywords: waffle, qwk, mail, offline
Organization: TECHbooks of Beaverton Oregon - Public Access Unix
Date: Sat, 12 Dec 1992 03:29:16 GMT

OK, so we've handled the incoming UNIX side of things for our reader.
What about the outgoing side?  Well, mail is a minor problem.  We have
to know whether we're splitting up a standard UNIX mail file (one big 
file, with "From " differentiating messages or possibly ^A^A^A^A if 
it's MMDF format) or a standard Waffle 1.65 file (differentiated by 
^A^A^A^A, but could be 1.64 (one message, one file in $LOGNAME.X format 
or converted from 1.65 to 1.64 format).  And our destination might be
one big file or one file per message.  This needs to be straightened
out.  But let's put it aside for a little while.

The Korn shell script below will zip up messages at a fairly standard
rate of 100 messages in 30 seconds.  At 2400 baud, each 100 messages
should take about 10 minutes to download.  I confess I was surprised
that zip wasn't more efficient at packing messages.  I expect it would 
be more efficient if it were working on larger files, like catting a 
bunch of messages to one text file.  I suspect that this script could be
easily converted to a C program for DOS Waffle folks.

#! /bin/ksh
#
# news_out: This will read a file "newsgrp.lst" in the $TEMP directory
# that has the format "comp.bbs.waffle 123 waffle" and produce a ZIP
# file for each group that has new messages.  If invoked as "news_out
# cat", the ZIP file will each be one large text file.  Otherwise they 
# will be made up of copies of each message file.
#
#$LOGNAME supplied by shell
# Constant location for $ACTIVE
ACTIVE="/usr/lib/news/active"
WAFFLE="/home/bbs"
TEMP="/var/tmp/$LOGNAME"

ncat()
{
# group, low, dir, and name come from main part of program
#
# If $ACTIVE is being maintained properly, we can skip using "ls"
# to find high message.  We can get high and low from $ACTIVE.
#
# Note space at end of group so "comp.bbs " != "comp.bbs.waffle"
# If $ACTIVE is being maintained properly, the space should make
# each group name unique.
#
toss=`grep "^$group " $ACTIVE`
echo $toss
#
# if not found, send email to user with error message
#
if [[ $toss = "" ]]  
then
	echo $group foobarred
	mailx $LOGNAME -s "$group not here" < $WAFFLE/system/missing.grp
	return
fi
cd /usr/spool/news/$dir
low=`echo $toss | cut -f 3 -d " "`
high=`echo $toss | cut -f 2 -d " "`
#low=`ls | sort | head -1`
#high=`ls | sort | tail -1`
echo $low $high $name
# Did we just add this group?  If so, $last will be 0 or 1.
if (( $last < $low ))
then
	last=$low
	echo "last being set to low"
else
	((last=$last+1))
	echo "last was accurate"
fi
# If no new messages to process, return to main
if (( $last >= $high ))
then
	echo "high greater than last"
	return 0
fi
if [[ $1 = "cat" ]]
then
	# This produces one big text file, then zips it.
	while (( $last < $high ))
	do 
		echo "catting"
		cat $last >> $TEMP/$name
		((last=$last+1))
	done
	zip -m $TEMP/$name.zip $TEMP/$name
	# -m "moves" (deletes after zipping) text file
	echo $group $high $name >> $TEMP/new
else
	pack
fi

}

pack()
{
string=""
x=10
loop="?"
counter=$last
# ones are handled differently
while ( (( $last < $high )) && (( $counter % 10 != 0 )) )
do
	string=$string" $counter"
	echo $string
	((counter=counter+1))
done
while (( $last < $high ))
do
	((counter=counter/10))
	while ( (( $last < $high )) && (($counter % 10 != 0 )) )
	do
		string=$string" $counter$loop"
		echo "Counter is $counter and last is $last"
		((counter=counter+1))
		((last=counter*x))
	done
	loop=$loop"?"
	((x=x*10))
done
zip $TEMP/$name.zip $string
echo $group $high $name >> $TEMP/new
}

integer last=0
# The "echo" is slower but more reliable, depending on what the user
# might have been doing with the IFS variable.
while read group last name
do 
#	group=`echo $line | cut -f1 -d " "`
	dir=`echo $group | tr "\." "/"`
#	last=`echo $line | cut -f2 -d " "
#	name=`echo $line | cut -f3 -d " "
	echo $group $dir $last $name
	ncat
done < $TEMP/newsgrp.lst
mv $TEMP/new $TEMP/newsgrp.lst