#!/usr/local/bin/perl5 

$tmp="/tmp/pr.$$";
@passwd = getpwuid($<);
$user =  $passwd[0];
$adm_path = "/nnnn/nnn/gnats-adm";

open(CATEGORIES, "$adm_path/catlist");
while ($login = <CATEGORIES>) {
   chop($login);
   $cat = <CATEGORIES>;
   chop($cat);
   $category{$login} = $cat;
}
close(CATEGORIES);

open(RESPONSIBLES, "$adm_path/categories");
while (<RESPONSIBLES>) {
   chop;
   if (/^#/){ next; }
   @items = split(/:/);                 # an associative array with the
   $responsible{$items[0]} = $items[2]; # responsible on each category
   }
close(RESPONSIBLES);

open(ADDRESS, "$adm_path/responsible");
while (<ADDRESS>) {
   chop;
   if (/^#/){ next; }
   @items = split(/:/);           # an associative array with the email
   $email{$items[0]} = $items[2]; # address of each responsible
   }
close(ADDRESS);

open(FORM,"$adm_path/sendprform");
@form=<FORM>;
close(FORM);

while (<>) {
    s/\t/ /;
	if (/^Received:/) { next; }
	if (/^Message-Id:/) { next; }
	if (/^X-Rerouted-To:/) { next; }
	s/^>/-/;
	if (($t)=/^From: (.*)\n/) {
#		open(L,">>/var/adm/sendpr.log");
#		(@now)=localtime(time);
#		printf(L "%02d%02d%02d %02d:%02d %s\n",
#			$now[5],$now[4],$now[3],$now[2],$now[1],$_);
#		close(L);

#	if we use the log file note that $now[4], what is the month,
#	is a value in the range 0..11, so we have to modify it to put 
#	a real value in the log file

		$fromaddr=$t unless (($fromaddr)=$t=~/<?\s*([\w.\-]+\@[\w.\-]+)\s*>?/);

#	$fromaddr is taking only the value of the submitter address filtering 
#	the angle brackets and any other thing like the submitter name

		# if you want to filter mails from outside your organization
		# uncomment the following line and modify the name of the 
		# domain in the regular expression, which is cern.ch, and put
		# your own domain
		
		#$addr_cern = ($fromaddr=~/cern\.ch/i || $fromaddr !~ /@/ ) ; 

		# to check if the mail comes from a CERN machine 
		# or from a local user 
                # if not, we must notify only the responsible, 
                # without generating a problem report

		next;
	} elsif (($t)=/^Subject: (.*)\n/) {
		$subject=$t;
		next;
	}
	elsif (/^Cc: /) { next; }
	elsif (/^To: /) { next; }
	elsif (/^Date: /) { next; }
        elsif (/[->]*We have received your problem report/) { exit; }
        # The following line is to filter the messages coming from our
	# internal alarm system. If you have one, perhaps you should 
	# uncomment the line and change the regular expression
	# elsif (($subject)=~/SURE UPDATE$/) { exit; }
	push(@prob,$_);
}

	
open(P,">$tmp") || die "$tmp: $!\n";
print P "From $fromaddr\n";
foreach (@form) {
	if (/^SEND-PR:/) { next; }
	if (/^Reply-To:/) {
		print P "Reply-To: $fromaddr\n";
	} elsif ( /^From:/ ) {
		print P "From: $fromaddr\n";
	} elsif (! $addr_cern && /^Subject:/) {   # if the mail comes from outside 
	        print P "Subject:  $subject\n";   # CERN, we fill the subject field 
        } elsif ( ! $addr_cern && /^To:/ )  {     # and we'll send it to the responsible

	        $resp_addr = $email{$responsible{$category{$user}}}; 

		print P "To: $resp_addr\n";       # for that category,instead of sending
                                                  # it to bugs account
	} elsif (/^>Originator:/) {
		print P ">Originator:    $from\n";
	} elsif (/^>Confidential:/) {
		s/:.*/:  no/; print P;
	} elsif (/^>Synopsis:/) {
		print P ">Synopsis:      $subject\n";
	} elsif (/^>Severity:/) {
		s/:.*/:      serious/; print P;
	} elsif (/^>Priority:/) {
		s/:.*/:      medium/; print P;
	} elsif (/^>Category:/) {
		print P ">Category:      $category{$user}\n";
	} elsif (/^>Class:/) {
		s/:.*/:         support/; print P;
	} elsif (/^>Description:/) {
	        s/<.*>/ /;
		print P;
		if (! $addr_cern) { # if the mail comes from outside CERN,
                                    # we add warning message in the description field

		#
		# You should change the message and adapt it to your site.
		#
		    print P "\n********************************************************************************\n";
		    print P " NOTE: This message arrived in the $category{$user} support account from a source\n";
		    print P "       external to CERN. It has been delivered directly to you and has not\n";
		    print P "       been entered in the CN Problem Report Management scheme.\n";
		    print P "********************************************************************************\n\n";
		}
		foreach (@prob) {
			print P " " . $_;
		}
	} else {
	        s/<.*>/ /;
		print P;
	}
    }


close(P);

system ("/usr/lib/sendmail -t < $tmp");

if (-r $tmp) { unlink $tmp; }

exit;
