#!/usr/bin/perl

$AreaFixDir="/usr/local/lib/ifmail/areafix";
$NewsDir="/usr/spool/news/fido7/BAD";
$NewsCommand="/usr/lib/news/inews -h";
$AreaHeader="From: AreaFix <AreaFix\@oskin.msk.ru>\nNewsgroups: fido7.20.ROBOTS\nSubject: New area creation notification\n\n\n";
$NNTPServerFile="/etc/nntpserver";
$IfArea="/usr/local/lib/ifmail/etc/Areas";
$InNewsFeeds="/usr/lib/news/newsfeeds";
$OutNewsFeeds="/usr/spool/ifmail/areafix/newsfeeds.out";
#$EchoLinks="f188.n5020.z2 f41.n5020.z2 f510.n5020.z2 f411.n5020.z2 f324.n5020.z2";
$EchoLinks="f1056.n5020.z2 f427.n5020.z2 f510.n5020.z2 f411.n5020.z2 f324.n5020.z2 f473.n5020.z2 f317.n5030.z2";
$PathI=1;


open( nn, $NNTPServerFile ) || die "Cannot open $NNTPServerFile ($!)";
$NNTPServer=<nn>;
chop $NNTPServer;
close( nn );
	
open( dir, "ls ".$NewsDir." |" );
while( <dir> ) {
	chop;
	next if( $_ !~ /^[0-9]+$/ );
	$msg = $_;
	$hdr = 1;
	if( ! open( fm, $NewsDir."/".$msg )) {
		next;
	}
	undef( @Header );
	undef( $NewArea );
	undef( $Path );
	undef( $CrArea );
	while( <fm> ) {
		chop;
		if( defined( $hdr )) {
			if( /^[ \n]*$/ ) {
				undef( $hdr );
				$ret = &addarea( $NewArea, $Path );
				if( $ret == 0 ) {
					last;
				} elsif( $ret == 1 ) {
					$CrArea = 1;
					system "$AreaFixDir/newgroup fido7.$NewArea";
					print "$AreaFixDir/newgroup fido7.$NewArea\n";
				} else {
					$CrArea = 2;
				}
				open( OUT, "| ".$NewsCommand );
				for( $i = 0; $i <= $#Header; $i++ ) {
					if( $Header[$i] =~ /^Newsgroups:/ ) {
						$Header[$i] =~ s/BAD/$NewArea/;
					}
					print OUT $Header[$i]."\n";
				}
				print OUT "\n";
				next;
			}
			next if( /^NNTP-Posting-Host:/ );
			next if( /^Xref:/ );
			$Header[$#Header + 1] = $_;
			if( /^X-FTN-AREA:/ ) {
				$NewArea = $_;
				$NewArea =~ s/^X-FTN-AREA:[ \t]*//;
				$NewArea =~ tr/[a-z]/[A-Z]/;
				next;
			}
			if( /^Message-ID:/ ) {
				$t = time;
				$_ =~ s/<[0-9]+/<$t/;
				$Header[$#Header] = $_;
				next;
			}
			if( /^Path:/ ) {
				eval "\$_ =~ s/$NNTPServer!//";
#				eval "\$_ =~ s/$NNTPServer|//";
				$Path = $_;
				$Header[$#Header] = $_;
				next;
			}
		} else {
			print OUT "$_\n";
		}
	}
	close( OUT );
	close( fm );
	print "$msg\n";
	if( defined( $CrArea )) {
		system "$AreaFixDir/rmsg $msg";
	}
	sleep 1;
}
close( dir );

sub addarea {
	local( $area ) = $_[0];
	local( $path ) = $_[1];
	local( @link );
	local( $areas, $a, $nf, $feeds, $fd );
	undef( @areas );
	undef( @feeds );
	$path =~ s/^Path:[ \t]+//;
	@link = split( /!/, $path );
	$a = $link[$PathI];
	if( $EchoLinks !~ /$a/ ) {
		print "Cannot create area from $a\n";
		return 0;
	}
	if( ! open( IfArea )) {
		print "Cannot open $IfArea[$!]\n";
		return 0;
	}
	while( <IfArea> ) {
		chop;
		if( /^$area[ \t]/ ) {
			print "Area $area already exist.\n";
			return 2;
		}
		$areas[$#areas+1] = $_;
	};
	$areas[$#areas+1] = "$area fido7.$area fido7";
	if( ! open( InNewsFeeds )) {
		print "Cannot open $InNewsFeeds[$!]\n";
		return 0;
	}
	$nf = 0;
	while( <InNewsFeeds> ) {
		chop;
		$feeds[$#feeds+1] = $_;
		next if( /^[ \t]*#/ );
		if( $nf == 0 ) {
			if( $_ !~ /$a/ ) {
				next;
			}
			$nf = 1;
			next;
		}
		if( $nf == 1 ) {
			if( $_ =~ /[ \t]+:.+:$/ ) {
				$feeds[$#feeds+1] = $_;
				if( $feeds[$#feeds-2] =~ /[^,]\\$/ ) {
					$feeds[$#feeds-2] =~ s/\\$/,\\/;
				}
				$feeds[$#feeds-1] = "\tfido7.$area\\";
				$nf = 2;
				next;
			}
		}
	}
	@st = stat( $IfArea );
	rename( $IfArea, $IfArea.".bak" );
	open( fd, ">".$IfArea );
	for( $i = 0; $i <= $#areas; $i++ ) {
		print fd $areas[$i]."\n";
	}
	close( fd );
	chmod $st[2], $IfArea;
	chown $st[4], $st[5], $IfArea;
	@st = stat( $OutNewsFeeds );
	rename( $OutNewsFeeds, $OutNewsFeeds.".bak" );
	open( fd, ">".$OutNewsFeeds );
	for( $i = 0; $i <= $#feeds; $i++ ) {
		print fd $feeds[$i]."\n";
	}
	close( fd );
	chmod $st[2], $OutNewsFeeds;
	chown $st[4], $st[5], $OutNewsFeeds;
	system "$AreaFixDir/reload.newsfeeds";
	open( notify, "| $NewsCommand" );
	print notify $AreaHeader;
	@l = split( /\./, $link[$PathI] );
	undef( $l_z );
	undef( $l_n );
	undef( $l_f );
	undef( $l_p );
	for( $i = 0; $i <= $#l; $i++ ) {
		if( $l[$i] =~ /^z[0-9]+$/ ) {
			$l_z = $l[$i];
			$l_z =~ s/^z//;
		} elsif( $l[$i] =~ /^n[0-9]+/ ) {
			$l_n = $l[$i];
			$l_n =~ s/^n//;
		} elsif( $l[$i] =~ /^f[0-9]+/ ) {
			$l_f = $l[$i];
			$l_f =~ s/^f//;
		} elsif( $l[$i] =~ /^p[0-9]+/ ) {
			$l_p = $l[$i];
			$l_p =~ s/^p//;
		}
	}
	$l_z = 2 if( ! defined( $l_z ));
	$l_n = 5020 if( ! defined( $l_n ));
	$l_p = 0 if( ! defined( $l_p ));

	print notify "Area $NewArea created at 2:5020/20 by $l_z:$l_n/$l_f.$l_p\n";
	close( notify );
				
	return 1;
}
