#!/usr/bin/perl 

# 
# ColorFullWall ? ColoredF...Widgets ? ako to nazvat ?
#
# cfw.pl (alebo radsej cipchains ?)
#
# pouziva to kniznicu Curses::Widgets
# ... a potrebuje to VELKE kozmeticke upravy.
# 
# TiKO	<kotek@tuke.sk>
#
# Disclaimer:don't you understand a single word in slovak? try to email me, huh.
#

#use strict;	# s tymto mi to nezbehne
use Curses;
use Curses::Widgets qw( :all );

$IPCHAINS="/sbin/ipchains -L -n";

$mwh=new Curses;

&initscr();

$v_riadok=0;

$akcia='';

%akcia_list =   ( '1'	=> 'Add to current chain',
		  '2'	=> 'Delete from current chain',
		  '3'	=> 'Insert to current position',
		  '4'	=> 'Replace at current position',
		  '5'	=> 'other',
		  '8'	=> 'Quit - Last Choice');



do {

	@rules=&parse_ipchains();


	# List box in interactive mode.
	# $rule je vrateny znak
	# $v_riadok je index vybrateho pravidla

	($input, $v_riadok) = list_box( 'window'=> $mwh,
			  'title'=> 'Choose rule',
			  'ypos'=> 1,
			  'xpos'=> 1,
			  'lines'=> 26,
			  'cols'=> 76,
			  'list'=> \@rules,
			  'border'=> 'green',
			  'selected'=> $v_riadok);

	# $akcia je stalceny klaves ???
	# $sel je key z hashu akcia_list

	($input, $sel) = list_box( 'window'=> $mwh,
			  'title'=> "what to do ",
			  'ypos'=> 9,
			  'xpos'=> 10,
			  'lines'=> 5,
			  'cols'=> 45,
			  'list'=> \%akcia_list,
			  'border'=> 'green',
			  'selected'=> 1);
	
	($rule_num, $chain, $zbytok) = split( /\s+/ , $rules[$v_riadok], 3 );


	if ($sel==1) 
		{ $rule_text="-A ".$chain; }
	elsif ($sel==2)
		{ $rule_text="-D $chain $rule_num "; }
	elsif ($sel==3)
		{ $rule_text="-I $chain $rule_num "; }
	elsif ($sel==4)
		{ $rule_text="-R $chain $rule_num "; }
	elsif ($sel==5)
		{ $rule_text='-'; }
	else {
		$rule_text='';
		$akcia='q';
	}

	if ($rule_text ne '') {
		$iptext="/sbin/ipchains ".$rule_text;
	

		# $rule je prikaz
		# $status == 1 ak bolo stlacene ok inac == 0

		($rule, $status) = input_box( 'title'	=> "rule editor",
			   		'cols'		=> 70,
			   		'prompt'	=> "edit rule",
			   		'content'	=> $iptext,
			  	 	'border'	=> "blue");
		if ($status) {
			if (system($rule.' 2>>/dev/null')) {
			# chyba !
		        msg_box( 'title'        => "Critical Error!",
		                 'message'      => 'chybny prikaz pozri sa do /dev/null ;)',
		                 'border'       => "red");
			}
		}
	}
	


} while ($akcia ne 'q');


END {
	# The END block just ensures that Curses always cleans up behind
	# itself
	endwin();
}

exit 0;

#### koniec programu


# spracuje vystup ipchains -L -n vrati nacitane oriadkovane pole
#

sub parse_ipchains() {

open(FWNAMES,$IPCHAINS."|") || die "nepozname ipchains ???";

# vystup ipchains -L :

#Chain input (policy ACCEPT):
#target     prot opt     source                destination           ports
# pravidla ....

my $name, $count;
my $desc, @pomoc;
local @pole;

#$pole='';

$name=" ";

while (<FWNAMES>) {
	if ( /^Chain ([-_a-z0-9]+)/ ) {
		$count=1; 
		$name=$1;
		next;
	}

	if ( /^target/ ) { next; }

	$desc=$count." ".$name;
	
	$_ =~ s/\s+/ /g;
	
	push(@pole, $desc.' '.$_);
	
	$count++;
}

return @pole;

}	# of sub parse_ipchains()

###### tu to konci

########################### TODO ###############################################
#
# vstup priamo s /proc/sys/net/ip_[chains|fwnames]
# skrolovanie v zozname aj vpravo/vlavo
# *omnoho* lepsie *uhadnutie* prikazu ipchains .... 
# zobrazovanie len jedneho chainu v listingu
# rozumne menu, shortcuty a pod.
# cokolvek ta napadne
# a ine ...
#
# drzim palec. TiKO
