# lists-lib.pl
# Common functions 


# list_ous()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "ou"
sub list_ous
{
# read the OU list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading OU File List
	$lnum = 0;
	open(FILEOUS, $config{'ou_list'});
	while(<FILEOUS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEOUS);
return @rv;
}

# list_apps()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "app"
sub list_apps
{
# read the APP list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading APP File List
	$lnum = 0;
	open(FILEAPPS, $config{'app_list'});
	while(<FILEAPPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'grpalias' => $lin[2], 
				    'obj' => $lin[3], 'ens' => $lin[4], 
				    'maxsess' => $lin[5], 
				    'descrip' => $lin[6], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEAPPS);
return @rv;
}

# list_grpapps()
# Returns an array of hashtable, each containing info about one grpapps. Each hash
# will always contain the keys "alias" "name" "grpapps"
sub list_grpapps
{
# read the GRPAPPS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading GRPAPPS File List
	$lnum = 0;
	open(FILEGRPAPPS, $config{'grpapps_list'});
	while(<FILEGRPAPPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEGRPAPPS);
return @rv;
}

# list_domains()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "domain"
sub list_domains
{
# read the DOMAIN list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading DOMAIN File List
	$lnum = 0;
	open(FILEDOMAINS, $config{'domain_list'});
	while(<FILEDOMAINS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEDOMAINS);
return @rv;
}

# list_grphosts()
# Returns an array of hashtable, each containing info about one grphosts. Each hash
# will always contain the keys "alias" "name" "grphosts"
sub list_grphosts
{
# read the GRPHOSTS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading GRPHOSTS File List
	$lnum = 0;
	open(COUNTSESSFILE, $config{'grphosts_list'});
	while(<COUNTSESSFILE>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(COUNTSESSFILE);
return @rv;
}

# list_hosts()
# Returns an array of hashtable, each containing info about one hosts. Each hash
# will always contain the keys "alias" "name" "hosts"
sub list_hosts
{
# read the HOSTS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading HOSTS File List
	$lnum = 0;
	open(FILEHOSTS, $config{'hosts_list'});
	while(<FILEHOSTS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'grpalias' => $lin[2], 
				    'obj' => $lin[3], 'ens' => $lin[4], 
				    'ipname' => $lin[5], 
				    'maxsess' => $lin[6], 
				    'descrip' => $lin[7], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEHOSTS);
return @rv;
}

# list_hostsuser()
# Returns an array of hashtable, each containing info about one hostsuser. Each hash
# will always contain the keys "alias" "name" "hostsuser"
sub list_hostsuser
{
# read the HOSTSUSER list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading HOSTS File List
	$lnum = 0;
	open(FILEHOSTSUSER, $config{'hostsuser_list'});
	while(<FILEHOSTSUSER>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'username' => $lin[0],'passwd' => $lin[1],
				    'name' => $lin[2],
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEHOSTSUSER);
return @rv;
}

# list_profcache()
# Returns an array of hashtable, each containing info about one hostsuser. Each hash
# will always contain the keys "alias" "name" "hostsuser"
sub list_profcache
{
# read the PROFCACHE list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading PROFCACHE File List
	$lnum = 0;
	open(FILEPROFCACHE, $config{'profcache_list'});
	while(<FILEPROFCACHE>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],'name' => $lin[1],
				    'hostusername' => $lin[2],
				    'hostname' => $lin[3],
				    'grphost' => $lin[4],
				    'username' => $lin[5],
				    'ou' => $lin[6],
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEPROFCACHE);
return @rv;
}

# list_actions()
# Returns an array of hashtable, each containing info about actions. Each hash
# will always contain the keys "alias" "name", etc. 
sub list_actions
{
# read the ACCTIONS list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading ACCTIONS File List
	$lnum = 0;
	open(FILEACCTIONS, $config{'act_list'});
	while(<FILEACCTIONS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
                        if ( $task_type eq "" || $task_type eq $lin[9] ) {
			      push(@rv, { 'alias' => $lin[0],'name' => $lin[1],
			                  'target' => $lin[2],
			      	          'object' => $lin[3],
				          'condition' => $lin[4],
				          'conditionValue' => $lin[5],
				          'action' => $lin[6],
				          'script' => $lin[7],
				          'when' => $lin[8],
				          'how' => $lin[9],
				          'num' => scalar(@rv) });
			      $idx{$lin[0]} = $rv[$#rv];
                       }
		}
		$lnum++;
	}
	close(FILEACCTIONS);
        return @rv;
}

# list_links()
# Returns an array of hashtable, each containing info about one link. Each hash
# will always contain the keys "alias" "name" "link"
sub list_links
{
# read the Links list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading Links File List
	$lnum = 0;
	open(FILELKS, $config{'tta_links'});
	while(<FILELKS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILELKS);
return @rv;
}

# list_webtops()
# Returns an array of hashtable, each containing info about one webtop. Each hash
# will always contain the keys "alias" "name" "link"
sub list_webtops
{
# read the Webtops list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading Webtops File List
	$lnum = 0;
	open(FILEWEBTOPS, $config{'tta_webtops'});
	while(<FILEWEBTOPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
		        chomp($lin);
			@lin = split(/::/, $_, -1);
			push(@rv, { 'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2],
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(FILEWEBTOPS);
return @rv;
}

# load_ttaobj($temp_f)
# Returns an array of hashtable, each containing info about one webtop. Each hash
# will always contain the keys "alias" "name" "link" etc .
sub load_ttaobj
{
# read the obj properties from file
   local(@ttaobj, $_, %idx, $lnum, @lin);
 
	# start by reading from file List

	$lnum = 0;
	open(FILETTAOBJ, $temp_f);
	while(<FILETTAOBJ>) {
		 if (/\S/ && !/^[#\+\-]/ && $lnum gt 0 ) {
		        chomp($lin);
			@lin = split(/: /, $_, -1);
		        $lin[1]=~ s/"//g;
		        $is_space=chop($lin[1]);
                        if ( $is_space eq " " ) { chop($lin[1]); }
			push(@ttaobj, { 'property' => $lin[0],
				    'value' => $lin[1],
				    'num' => scalar(@ttaobj) });
			$idx{$lin[0]} = $ttaobj[$#ttaobj];
			}
		$lnum++;
		}
	close(FILETTAOBJ);
	unlink($temp_f);
    return @ttaobj;
}

sub load_scriptlist
{
# read the obj properties from file
   local(@script_list, $_, $d, $lnum, $title);
# Get Scripts List
	opendir(DIR, $config{'monitor_scripts_path'});
	foreach $d (readdir(DIR)) {
        	if ( $d =~ /(\.tcl)$/ ) {
              	$file_scr=$config{'monitor_scripts_path'}."/$d";
              	$lnum=0;
              	open(FILESCR, $file_scr);
              	while (<FILESCR>) {
                 	if (/^$config{'mon_title'}/ && $lnum gt 0 ) {
                        	$title=$_;
                        	$title=~s/$config{'mon_title'}//;
                        	push(@script_list, { 'desc' => $title,
                                    	'name' => $d,
                                    	'num' => scalar(@script_list) });
                        	last;
                        	}
                	$lnum++;
              	}
              	close(FILESCR);
        	}
	}
	closedir(DIR);
	@script_list= sort { $a->{'desc'} cmp $b->{'desc'} } @script_list;
  return @script_list;
}

# Returns an array of hashtable, each containing info about one file. Each hash
# will always contain the keys "alias" "name", etc
sub list_files
{
# read the filelist from file
  local ($wherepath); $wherepath = $_[0];   
  local ($temp_f, $elem);    
  local (@rv, $_, %idx, $lnum, @lin);
  local ($f_perm , $f_lsnum, $f_owner, $f_group, $f_size, $f_month);
  local ($f_day, $f_time, $f_name);

  $no_out=1;
  $temp_f = &tempname();
  $str=`(cd $wherepath ; ls -lt $ls_match >$temp_f) 2>/dev/null`;
  &webmin_log("list", $wherepath, $config{run_exec_user}, "$str");
#  if ( $str) {
	$lnum = 0;
	open(FILELIST, $temp_f);
	while(<FILELIST>) {
		s/\r|\n//g;
		@lin = split(/ /, $_, -1);
                $pos=0;
                $ifield=0;
                foreach $elem (@lin) {
                   if ( $elem ne "" ) {
                        if ( $pos eq 0 ) { $f_perm=$elem; }
                        if ( $pos eq 1 ) { $f_lsnum=$elem; }
                        if ( $pos eq 2 ) { $f_owner=$elem; }
                        if ( $pos eq 3 ) { $f_group=$elem; }
                        if ( $pos eq 4 ) { $f_size=$elem; }
                        if ( $pos eq 5 ) { $f_month=$elem; }
                        if ( $pos eq 6 ) { $f_day=$elem; }
                        if ( $pos eq 7 ) { $f_time=$elem; }
                        if ( $pos eq 8 ) { $f_name=$elem; }
                        $pos++;
                   }
                   $ifield++;
                }
                push(@rv,{ 'perm' => $f_perm , 'lsnum' => $f_lsnum,
                                'owner' => $f_owner, 'group' => $f_group,
                                'size' => $f_size, 'month' => $f_month,
                                'day' => $f_day, 'time' => $f_time,
                                'name' => $f_name,
                                'num' => scalar(@rv) });
                $idx{$lin[0]} = $rv[$#rv];
                $lnum++;
	}
	close(FILELIST);
#   }
return @rv;
}
# list_countsess()
sub list_countsess
{
# read the countsess list file
local(@rv, $_, %idx, $lnum, @lin);
	# start by reading COUNTSESS File List
	$lnum = 0;
	open(COUNTSESSFILE, $config{'cron_sessout_file'});
	while(<COUNTSESSFILE>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
                        push(@count_sess, { 'alias' => $lin[0], 'name' => $lin[1] ,
                                    'obj' => $lin[2] ,
                                    'objalias' => $lin[3] ,
                                    'objgrpalias' => $lin[4] ,
                                    'numsess' => $lin[5],
                                    'descrip' => $lin[6],
				    'num' => scalar(@rv) });
			$idx{$lin[0]} = $rv[$#rv];
			}
		$lnum++;
		}
	close(COUNTSESSFILE);
return @rv;
}
1;  # Return true
