#!/usr/bin/perl -T
#$Id: ajax.cgi,v 1.11 2025/07/19 06:53:24 sqko Exp $
use strict;
use warnings;
use RFC::Main;
use CGI qw(:standard :netscape);
use RFC::RFCCGI qw(xss);
print header(-charset=>'utf-8');
exit unless (param);
my $ref = &RFC::RFCCGI::get_user_data_ref();
my $uid = $$ref{'uid'};
my $user = $$ref{'name'};
my $pass = $$ref{'pass'};
my $action = param('action');
exit unless $action;
if ($action eq 'logout')
{
    print  xss(&RFC::Main::passwd_client('logout_user',$user,$pass));
    exit;
}
if ($action eq 'is_logged_in')
{
    my $value =  &RFC::Main::passwd_client('is_logged_in',$user)+0;
    if ($value)
    {
        print "YES\n";
    }
    else
    {
        print "NO\n";
    }
    exit;
}

if ($action eq 'list_ttys')
{
    my $cmd = '/bin/ps -elf | /bin/grep serial | /bin/grep tty';
    my @list = `$cmd`;
    my %ttys = ();
    foreach my $line(@list)
    {
	chomp $line;
	if ($line =~ /(ttyS\d+)$/i)
	{
	    $ttys{$1} = 1;
	    next;
	}
	if ($line =~ /(ttyS\d+)/i)
	{
	    $ttys{$1} = 1;
	}
    }
    $cmd = '/bin/ps -elf | /bin/grep relay | /bin/grep tty';
    @list = `$cmd`;
    foreach my $line(@list)
    {
	chomp $line;
	if ($line =~ /(ttyS\d+)$/i)
	{
	    $ttys{$1} = 1;
	    next;
	}
	if ($line =~ /(ttyS\d+)/i)
	{
	    $ttys{$1} = 1;
	}
    }
    if (%ttys)
    {
	print "Curently avaliable tty's\n";
	print join("\n",(sort {$a cmp $b} keys %ttys)),"\n";
    }
    else
    {
	print "Error: no serial servers could be found!";
    }
    exit;
}

exit unless ($uid);


if ($action eq 'change_global_cv')
{
    unless ($$ref{'admin'})
    {
	print "Error: Only administrators can change global configuration!\n";
	exit;
    }
    my $section = param('section');
    my $key = param('key');
    my $new_val = param('value');
    my $config = RFC::Main::config();
    $config->change_config_value($section,$key,$new_val);
    print "OK\n$section\n$key\n$new_val";
    exit;
}
