#!/usr/bin/perl -w
#
# mail2news
# From the mail2news mini-HOWTO and extensively adapted by 
# Bek Oberin <gossamer@tertius.net.au>
#
# Below corections by Joop Boonen <jboonen@worldonline.nl>
#
# Added correct handeling of To and Cc
# also  added multiline reading of To and Cc
# and corrected X-$to to $to
#
# Last updated by jboonen on Sun Feb  25 21:39:18 CET 2001
#

use strict;
use vars qw($inews $iopts $postinghost $nntphost $approved $organization $VERSION);

#
# User Config
#


# Where's your posting program?
$inews = "/usr/bin/inews";
$iopts = "-h";
#$inews = "/usr/bin/rnews";
#$iopts = "-v";

# for debugging - output to screen
#$inews = "cat";
#$iopts = "";

$nntphost = "localhost";
$postinghost = "localhost";
#$postinghost = "tertius.net.au";

# Approved:  line.  Can be anything, really.
$approved = "yes";
$organization = "none";

#
# Constants
#

$VERSION = "1.3";

#
# Parse args
#

my $newsgroup;
if ($#ARGV < 0) {
   # $newsgroup = "test";
   # we'll expect the newsgroup line in the body
} elsif ($#ARGV == 0) {
   $newsgroup = $ARGV[0];
} else {
   die "usage: mail2news [newsgroup]\n";
}

#
# Sig handler
#

# in case inews dumps core or something crazy
$SIG{'PIPE'} = "plumber";
sub plumber { die "mail2news: \"$inews\" died prematurely!\n"; }

#
# Make sure inews can find the server
#
$ENV{"NNTPSERVER"} = $nntphost;

#
# Main starts here
#

$approved ||= "yes";   # just make sure it's -something-
$organization ||= "mail2news gateway";   

open (INEWS, "| $inews $iopts") || die "mail2news: can't run $inews\n";

my $to;
my $cc;
my $saw_approved = 0;
my $saw_organization = 0;
my $saw_subject = 0;
my $saw_msgid = 0;
my $saw_path = 0;
my $saw_newsgroup = 0;
my $saw_references = 0;
my $in_reply = 0;
# header munging loop
while (<STDIN>) {
   last if /^$/;

   # transform real from: line back to icky style
   s/^From:\s+(.*) <(.*)>/From: $2 ($1)/;

   s/^Message-Id/Message-ID/i;

   # check if "To:" is in the line in this case put it in $to
   if ($_=~/^To: /) {
        $to=$_;
	# check if there are more than one address via last character ,
	# if yes than read next line and add it to $to
        while ($_=~/\,$/) {
		chomp ($_=<STDIN>);
		$to="$to"."$_\n";
                        }
		}

   # check if "cc:" (ignore case) is in the line in this case put it in $cc
   if ($_=~/^Cc: /i) {
	$cc=$_;
	# check if there are more than one address via last character ,
	# if yes than read next line and add it to $cc
	while ($_=~/\,$/) {
		chomp ($_=<STDIN>);
		$cc="$cc"."$_\n";
			}
		}

   $in_reply = $_ if /^In-Reply-To: /;

   # transform from_ line to path header
   if (!$saw_path &&
        (s/^From\s+(\S+)@(\S+).*/Path: $2!$1/ || 
         s/^From\s+(\S+)[^@]*$/Path: $1\n/)) {
      $saw_path ||= ($1 eq 'Path');
   }

   next if /^Message-ID/ && $saw_msgid;
   next if /^Path/ && $saw_path;
   next if /^Newsgroups/ && $newsgroup;  # argv newsgroup overrides
   next if /^Message-ID:.*@.*@/;  # invalid message-id, we don't want it

   print INEWS
      if /^(Date|From|Subject|Path|Newsgroups|Message-ID|Approved|Organization|Content-Type|MIME-Version|References):/i;

   if (defined($1)) {
      $saw_references ||= ($1 eq 'References');
      $saw_path ||= ($1 eq 'Path');
      $saw_subject ||= ($1 eq 'Subject');
      $saw_msgid ||= ($1 eq 'Message-ID');
      $saw_newsgroup ||= ($1 eq 'Newsgroups');
      $saw_approved ||= ($1 eq 'Approved');
      $saw_organization ||= ($1 eq 'Organization');
   }
}

die "mail2news: didn't get newsgroup from either headers or ARGV\n"
   unless $newsgroup || $saw_newsgroup;

my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time);
my $madeupid = "\<$year$mon$mday.$hour$min$sec.$$\@$postinghost\>";

print INEWS "Newsgroups: $newsgroup\n" if $newsgroup && !$saw_newsgroup;
print INEWS "Subject: unknown\n" unless $saw_subject;
print INEWS "Message-ID: $madeupid\n" unless $saw_msgid;
print INEWS "Approved: $approved\n" unless $saw_approved;
print INEWS "Organisation: $organization\n" unless $saw_organization;
print INEWS "$to" if $to;
print INEWS "$cc" if $cc;
if (!$saw_references && $in_reply) {
   $in_reply =~ m/(<[^@>]+@[^>]+>)/;
   print INEWS "References: $1\n" if $1;
}
print INEWS "\n";

print INEWS while <STDIN>;   # gobble rest of message

close INEWS;
exit $?;

#
# End.
#


