# 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_UserManager_Init {} {

	if {[file exists /etc/vftpd.users]} then {

		set config [open /etc/vftpd.users r]
 		set login ""

		while {[eof $config]==0} {
		   eval [gets $config]
		   if {$login!=""} then {
			.listbox#1 insert end $login
           		set login ""
           	   }
		}
		close $config
		.listbox#1 selection set 0
	}

	vftpd_AddUser_Activate
	return TCL_OK
}

proc vftpd_AddUser_Activate {} {

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

	.button#1 configure -state normal

	return TCL_OK
}

proc vftpd_AddUser_Cancel {lvl username} {

	destroy $lvl

	if {$username!=""} then {
		set here [vftpd_GetUserIndex $username]
		.listbox#1 selection clear 0 [.listbox#1 index end]
  		.listbox#1 selection set $here
	}

	vftpd_AddUser_Activate

	return TCL_OK
}

proc vftpd_AddUser {}  {
	toplevel .add
	wm title .add "Add user"
	.button#1 configure -state disabled
	.button#2 configure -state disabled
	.button#3 configure -state disabled
	source /usr/local/sbin/vftpd_eduser
	EdUser_ui .add
	return TCL_OK
}

proc vftpd_EdUser {} {
	set sel [.listbox#1 curselection]
	if {$sel==""} then {return TCL_OK}
	set username [.listbox#1 get $sel]
	toplevel .ed 
	wm title .ed "Edit user"
	.button#1 configure -state disabled
	.button#2 configure -state disabled
	.button#3 configure -state disabled
	source /usr/local/sbin/vftpd_eduser
 	EdUser_ui .ed $username
	return TCL_OK


}

proc vftpd_DelUser {username} {

  if {$username==""} then {return TCL_OK}

  set config [open /etc/vftpd.users r]
  set oldconfig [open /etc/vftpd.users.bak w+]
  fcopy $config $oldconfig
  close $config
  seek $oldconfig 0 start
  set config [open /etc/vftpd.users w]
  set login ""

  while {[eof $oldconfig]==0} {
	set buf "[gets $oldconfig]"
	eval $buf
	if {$login!=$username} then {
		puts $config $buf
	}
  }

  close $config
  close $oldconfig
  
  set here [vftpd_GetUserIndex $username]
  .listbox#1 delete $here
  if {$here==[.listbox#1 index end]} then {
	if {$here!=0} then {
		incr here -1
	}
  }
  .listbox#1 selection clear 0 [.listbox#1 index end]
  .listbox#1 selection set $here
  
  return TCL_OK
  
}

proc vftpd_RemoveButton {} {
  
  set sel [.listbox#1 curselection]
  if {$sel==""} then {return TCL_OK}
  set username [.listbox#1 get $sel]

  if {[tk_dialog .confirmation Confirmation "Delete account '$username' ?" "" 1 yes no]==1} then {
		return TCL_OK
  }
  
  vftpd_DelUser $username
  
  vftpd_AddUser_Activate

  return TCL_OK

}

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


proc vftpd_AddUser_OK {base _username _login _pass _port _maxCon _maxIdle _address _server_port _server_login _server_pass _path _chroot _down_perm _up_perm _chg_perm  _ratio_enable _ratio _ratio_ul _ratio_dl _cache_enable _cache_dir} {
	
  if {$_login==""} then {
	tk_dialog "$base.error" Error "Some fields are mandatory." "" 0 OK
	return TCL_OK
  }
  if {$_pass==""} then {set _pass {""}}
  if {$_port==""} then {
	tk_dialog "$base.error" Error "Some fields are mandatory." "" 0 OK
	return TCL_OK
  }
  if {$_maxCon==""} then {set _maxCon 0}
  if {$_maxIdle==""} then {set _maxIdle {""}}
  if {$_address==""} then {
	tk_dialog "$base.error" Error "Some fields are mandatory." "" 0 OK
	return TCL_OK
  }
  if {$_server_port==""} then {
	tk_dialog "$base.error" Error "Some fields are mandatory." "" 0 OK
	return TCL_OK
  }
  if {$_server_login==""} then {
	tk_dialog "$base.error" Error "Some fields are mandatory." "" 0 OK
	return TCL_OK
  }
  if {$_server_pass==""} then {set _server_pass {""}}
  if {$_path==""} then {set _path {""}}
  if {$_ratio==""} then {set _ratio {""}}
  if {$_ratio_ul==""} then {set _ratio_ul {""}}
  if {$_ratio_dl==""} then {set _ratio_dl {""}}
  if {$_cache_dir==""} then {set _cache_dir {""}}

  set username $_login
  set login ""

  if {[file exists /etc/vftpd.users]} then {
    set config [open /etc/vftpd.users r+]
  } else {  
    set config [open /etc/vftpd.users w+]
  }
  
  while {[eof $config]==0} {
	eval [gets $config]
 	if {$login>=$username} then {break}
  }

  if {$login==$username && ($base==".add" || ($base==".ed" && $_username!=$_login))} then {
	if {[tk_dialog "$base.confirmation" Confirmation "Replace existing '$_login' ?" "" 1 yes no]==1} then {
		close $config
		return TCL_OK
	}
	vftpd_DelUser $login
	
  } else {
	if {$base==".ed" && $_username==$_login} then {vftpd_DelUser $_login}
  }
  set oldconfig [open /etc/vftpd.users.bak w+]
  seek $config 0 start
  fcopy $config $oldconfig
  close $config
  seek $oldconfig 0 start
  set config [open /etc/vftpd.users w]

  set buf [gets $oldconfig]
  while {[eof $oldconfig]==0} {
 	eval $buf
	if {$login>$username} then {break}
	puts $config $buf
 	set buf [gets $oldconfig]
 }
  puts $config "set login $_login"
  puts $config "set pass $_pass"
  puts $config "set port $_port"
  puts $config "set maxCon $_maxCon"
  puts $config "set maxIdle $_maxIdle"
  puts $config "set address $_address"
  puts $config "set server_port $_server_port"
  puts $config "set server_login $_server_login"
  puts $config "set server_pass $_server_pass"
  puts $config "set path $_path"
  puts $config "set chroot $_chroot"
  puts $config "set down_perm $_down_perm"
  puts $config "set up_perm $_up_perm"
  puts $config "set chg_perm $_chg_perm"
  puts $config "set ratio_enable $_ratio_enable"
  puts $config "set ratio $_ratio"
  puts $config "set ratio_ul $_ratio_ul"
  puts $config "set ratio_dl $_ratio_dl"
  puts $config "set cache_enable $_cache_enable"
  puts $config "set cache_dir $_cache_dir"
  
  if {[eof $oldconfig]==0} then {
	puts $config $buf
	fcopy $oldconfig $config
  }
  close $config
  close $oldconfig
 
  if {$login>$username} then {
 	 set here [vftpd_GetUserIndex $login]
	 .listbox#1 selection clear 0 [.listbox#1 index end]
	 if {$here!="TCL_ERROR"} then {
	   .listbox#1 insert $here $_login
	   .listbox#1 selection set $here
         } else {
  	  .listbox#1 insert end $_login
	  .listbox#1 selection clear 0 [.listbox#1 index end]
	  .listbox#1 selection set [expr [.listbox#1 index end]-1]
         }

  } else {
  	.listbox#1 insert end $_login
	.listbox#1 selection clear 0 [.listbox#1 index end]
	.listbox#1 selection set [expr [.listbox#1 index end]-1]
  }

  destroy $base

  vftpd_AddUser_Activate

  return TCL_OK

}

return TCL_OK
