# COPYRIGHT POLICY
# ----------------
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to Free Software Foundation, Inc.
# 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Copyright (C) 1998 Luc Deschenaux <lcd@gkb.com>
#
# DISCLAIMER
# ----------
# Use it at your own risk. You're sole responsible for anything that could
# happend before, while, after or without using this program.

proc vftpd_ipaccess_init {} {
      
	 if {[file exists vftpd.allow]} then {

		set config [open vftpd.allow r]
		
	        set buf [gets $config]
		while {[eof $config]==0} {
		   if {$buf!=""} then {
			.listbox#1 insert end "A: $buf"
           	   }
		   set buf [gets $config]
		}
		close $config
		.listbox#1 selection set 0
	}
	
	if {[file exists vftpd.deny]} then {

		set config [open vftpd.deny r]
		
		set buf [gets $config]
		while {[eof $config]==0} {
		   if {$buf!=""} then {
			.listbox#1 insert end "D: $buf"
           	   }
		   set buf [gets $config]
		}
		close $config
		.listbox#1 selection set 0
	}

	vftpd_ipaccess_Activate
	return TCL_OK

}

proc vftpd_ipaccess_Activate {} {

	if {[.listbox#1 index end]!=0}	{
		.button#2 configure -state normal
	} else	{
		.button#2 configure -state disabled
	}

	.button#1 configure -state normal

	return TCL_OK
}

proc vftpd_ipaccess_GetIndex {rule} {
	
	set index [.listbox#1 index end]
	
	while {$index!=0} {
		incr index -1
		if {[.listbox#1 get $index]==$rule} then {
			return $index
		}
		
	}
		
	return TCL_ERROR	
	
}

proc vftpd_ipaccess_allow {host} {

        if {$host==""} {return TCL_OK}
	set index [.listbox#1 index end]
	
	if {[vftpd_ipaccess_GetIndex "A: $host"]=="TCL_ERROR"} {
		if {$index!=0}	{
			while {$index!=0} {
				incr index -1
				if {"[.listbox#1 get $index]"<"D:"} then {
					incr index 1
					break
				}
			}
		}
		.listbox#1 insert $index "A: $host"
	}
	vftpd_ipaccess_Activate
	
	return TCL_OK

}

proc vftpd_ipaccess_deny {host} {

        if {$host==""} {return TCL_OK}
	set index [.listbox#1 index end]
	
	if {[vftpd_ipaccess_GetIndex "D: $host"]=="TCL_ERROR"} {
		if {$index!=0}	{
			while {$index!=0} {
				incr index -1
				if {"[.listbox#1 get $index]"<"Z:"} then {
					incr index 1
					break
				}
			}
		}
		.listbox#1 insert $index "D: $host"
	}
	vftpd_ipaccess_Activate
	
	return TCL_OK

}

proc vftpd_ipaccess_remove {} {
  
  set sel [.listbox#1 curselection]
  if {$sel==""} then {return TCL_OK}
  
  upvar host host
  set host [lindex [.listbox#1 get $sel] 1]
  
  .listbox#1 delete $sel
  if {$sel==[.listbox#1 index end]} then {
	if {$sel!=0} then {
		incr sel -1
	}
  }
  .listbox#1 selection clear 0 [.listbox#1 index end]
  .listbox#1 selection set $sel
  
  vftpd_ipaccess_Activate

  return TCL_OK

}

proc vftpd_ipaccess_write {allow deny value} {
	
	set file [lindex $value 0]
	set value [lindex $value 1]
	
	if {$file=={A:}} {
		puts $allow $value
	} else {
		puts $deny $value
	}	
	return TCL_OK
}

proc vftpd_ipaccess_ok {base host_radiobutton class_radiobutton hamCount hamTime hostsdeny} {

	set autoban [open vftpd.autoban w]
	if {$host_radiobutton!=""} {
		set ban host
	} else {
		set ban class
	}
	if {$hamCount==""} {set hamCount {""}}
	if {$hamTime==""} {set hamTime {""}}
	puts $autoban "set ban $ban"
	puts $autoban "set hamCount $hamCount"
	puts $autoban "set hamTime $hamTime"
	puts $autoban "set hostsdeny $hostsdeny"
	close $autoban
	
	set allow [open vftpd.allow w]
	set deny [open vftpd.deny w]
	
	set count [.listbox#1 index end]
	set index 0
	while {$index<$count} {
		vftpd_ipaccess_write $allow $deny [.listbox#1 get $index]
		incr index 1
	}
	close $allow
	close $deny 
	
	exit 0
}

return TCL_OK

