#!/bin/tcsh

# Mail-to-Usenet
# By Matthew Ghio
#
# This script is run when mail comes in.
#
# If the news server rejects the post, it will bounce it back to the sender.
# If the connection to the news server fails, it will leave the message in
# /tmp/mail2news*  You'll need to have a cron job to retry these when the
# news server comes back online again.

# You need to put your NNTP server here.
setenv NNTPSERVER myriad.pc.cc.cmu.edu

# Put your hostname here
setenv HOSTNAME news.alias.net


# Grab the from line for sending bounces
setenv FROM `echo $< | awk '{print $2}'`

/usr/lib/news/mail2news/m2nh >/tmp/mailnews$$
/usr/lib/news/inews -h /tmp/mailnews$$ >>&/tmp/mailnews$$.error

if (`/usr/bin/grep -c "" /tmp/mailnews$$.error` == 0 ) then
  rm -f /tmp/mailnews$$.error
  rm -f /tmp/mailnews$$
else
 if (`/usr/bin/grep -ci duplicate /tmp/mailnews$$.error` != 0 ) then
  rm -f /tmp/mailnews$$.error
  rm -f /tmp/mailnews$$
 endif
endif

if (-r /tmp/mailnews$$.error) then
 if (`/usr/bin/grep -ciE "connect|400" /tmp/mailnews$$.error` == 0 ) then
  (echo "From: mail2news daemon <mail2news@$HOSTNAME>";\
   echo "Subject: Post failed";\
   echo "";\
   echo "Your message could not be posted due to the following error:";\
   echo "";\
   cat /tmp/mailnews$$.error;\
   echo "";echo "";\
   echo "----- Undelivered Message Follows -----";echo "";\
   cat /tmp/mailnews$$)|/usr/lib/sendmail $FROM
  rm -f /tmp/mailnews$$.error
  rm -f /tmp/mailnews$$
 endif
endif
