# at-lib.pl
# Functions for listing and creating at jobs

do '../web-lib.pl';
&init_config();
do '../ui-lib.pl';
%access = &get_module_acl();

do "$config{'at_style'}-lib.pl";

# wrap_lines(text, width)
# Given a multi-line string, return an array of lines wrapped to
# the given width
sub wrap_lines
{
local @rv;
local $w = $_[1];
foreach $rest (split(/\n/, $_[0])) {
	if ($rest =~ /\S/) {
		while(length($rest) > $w) {
			push(@rv, substr($rest, 0, $w));
			$rest = substr($rest, $w);
			}
		push(@rv, $rest);
		}
	else {
		# Empty line .. keep as it is
		push(@rv, $rest);
		}
	}
return @rv;
}

# can_edit_user(&access, user)
sub can_edit_user
{
local %umap;
map { $umap{$_}++; } split(/\s+/, $_[0]->{'users'});
if ($_[0]->{'mode'} == 1 && !$umap{$_[1]} ||
    $_[0]->{'mode'} == 2 && $umap{$_[1]}) { return 0; }
elsif ($_[0]->{'mode'} == 3) {
	return $remote_user eq $_[1];
	}
else {
	return 1;
	}
}

# list_allowed()
# Returns a list of all users in the cron allow file
sub list_allowed
{
local(@rv, $_);
&open_readfile(ALLOW, $config{allow_file});
while(<ALLOW>) {
	next if (/^\s*#/);
	chop; push(@rv, $_) if (/\S/);
	}
close(ALLOW);
return @rv;
}


# list_denied()
# Return a list of users from the cron deny file
sub list_denied
{
local(@rv, $_);
&open_readfile(DENY, $config{deny_file});
while(<DENY>) {
	next if (/^\s*#/);
	chop; push(@rv, $_) if (/\S/);
	}
close(DENY);
return @rv;
}


# save_allowed(user, user, ...)
# Save the list of allowed users
sub save_allowed
{
&lock_file($config{allow_file});
if (@_) {
	local($_);
	&open_tempfile(ALLOW, ">$config{allow_file}");
	foreach (@_) {
		&print_tempfile(ALLOW, $_,"\n");
		}
	&close_tempfile(ALLOW);
	chmod(0444, $config{allow_file});
	}
else {
	&unlink_file($config{allow_file});
	}
&unlock_file($config{allow_file});
}


# save_denied(user, user, ...)
# Save the list of denied users
sub save_denied
{
&lock_file($config{deny_file});
if (@_) {
	local($_);
	&open_tempfile(DENY, "> $config{deny_file}");
	foreach (@_) {
		&print_tempfile(DENY, $_,"\n");
		}
	&close_tempfile(DENY);
	chmod(0444, $config{deny_file});
	}
else {
	&unlink_file($config{deny_file});
	}
&unlock_file($config{deny_file});
}

# can_use_at(user)
# Returns 1 if some user is allowed to use At jobs, based on the allow
# any deny files.
sub can_use_at
{
local ($user) = @_;
if (!$config{'allow_file'}) {
	return 1;	# not supported by OS
	}
elsif (@allow = &list_allowed()) {
	return &indexof($user, @allow) >= 0;	# check allowed list
	}
elsif (@deny = &list_denied()) {
	return &indexof($user, @denied) < 0;	# check denied list
	}
else {
	return 1;	# if neither exists, fall back to allowing all
	}
}

1;

