#! /bin/sh
# server.censor - censor headers, assuming input is a mail message.
#               Some details depend on UofT CSRI environment.
#		Jean-Francois Lamy, 1987-11-02
#
PATH=/bin:/usr/bin; export PATH
NEWSCTL=${NEWSCTL-/usr/lib/news}; export NEWSCTL
NEWSBIN=${NEWSBIN-/usr/lib/newsbin}; export NEWSBIN
NEWSARTS=${NEWSARTS-/usr/spool/news}; export NEWSARTS

# pass 0 - dredge up defaults
host="`hostname`.toronto.edu"
defdate="`set \`date\`; echo $1, $3-$2-\`echo $6 | sed 's/^..//'\` $4 $5`"
defmsgid="`set \`date\`; echo \<$6$2$3.\`echo $4 | tr -d :\`.$$@$host\>`"

sed -e "s/DEFMSGID/$defmsgid/" -e "s/DEFDATE/$defdate/" <<\! >/tmp/aj$$awk 

# pass 1 - note presence | absence of certain headers

# a header keyword: remember it and its value
/^[^\t ]*:/ { hdrval[$1] = $0; keyword=$1 }
# a continuation: concatenate this line to the value
!/^[^\t ]*:/ { hdrval[keyword] = hdrval[keyword] "\n" $0 }

END {
	# pass 2 - deduce & omit & emit headers
	subjname = "Subject:"
	ctlname = "Control:"
	ngname = "Newsgroups:"
	msgidname = "Message-ID:"
	typoname =  "Message-Id:"
	pathname = "Path:"
	datename = "Date:"
	fromname = "From:"
	orgname = "Organization:"
	distrname = "Distribution:"

	# fill in missing headers
	if (hdrval[typoname] != "") {	# spelling hack
		hdrval[msgidname] = hdrval[typoname]
		hdrval[typoname] = ""
		# fix spelling: Message-Id: -> Message-ID:
		nf = split(hdrval[msgidname], fields);	# bust up
		fields[1] = msgidname;		# fix spelling
		hdrval[msgidname] = fields[1];	# reassemble...
		for (i = 2; i <= nf; i++)
			hdrval[msgidname] = hdrval[msgidname] " " fields[i]
	}

	# We trust From:, Date: and Message-ID: as generated by mailer.
	# hdrval[datename] = datename " " "DEFDATE" # in case Date: breaks news

	# NNTP POST command may forget to put a message ID...
	
	if (hdrval[msgidname] == "")
		hdrval[msgidname] = msgidname " " "DEFMSGID"
	
	# snuff some headers
	distworld = distrname " world"
	if (hdrval[distrname] == distworld)
		hdrval[distrname] = ""

	# turn Subject: cmsg into a proper Control: header.
	if (substr(hdrval[subjname],1,14) == "Subject: cmsg ")
		hdrval[ctlname] = ctlname " " substr(hdrval[subjname],15)

	# warn if no Newsgroups:
	if (hdrval[ngname] == "")
		print "no newsgroups header!" | "cat >&2"

	# favour Newsgroups: & Control: for benefit of rnews
	if (hdrval[ngname] != "") {
		print hdrval[ngname]
		hdrval[ngname] = ""	# no Newsgroups: to print now
	}
	if (hdrval[ctlname] != "") {
		print hdrval[ctlname]
		hdrval[ctlname] = ""	# no Control: to print now
	}

	# B news kludgery: print Path: before From:
	if (hdrval[pathname] != "") {
		print hdrval[pathname]
		hdrval[pathname] = ""	# no Path: to print now
	}
	if (hdrval[fromname] != "") {
		print hdrval[fromname]
		hdrval[fromname] = ""	# no From: to print now
	}

	# have pity on readers: put Subject: next
	if (hdrval[subjname] != "") {
		print hdrval[subjname]
		hdrval[subjname] = ""	# no Subject: to print now
	}

	# print misc. headers in random order, unless they are empty.
	for (i in hdrval)
		if (hdrval[i] != "" && hdrval[i] != i " ")
			print hdrval[i]
}
!
cat $* | 
sed -e 's/^From:[ 	]*\(.*\)  *<\(.*\)>/From: \2 \(\1\)/' \
    -e '/^Received:/d' \
    -e '/^To:/d' \
    -e '/^X-To:/d' \
    -e '/^Cc:/d' \
    -e 's/^Original-//' |
tr -d '\1-\7\13\14\16-\37' |	# strip invisible chars, a la B news
	awk -f /tmp/aj$$awk
rm -f /tmp/aj$$awk
