#!/usr/bin/perl
#$Id: rfcctl,v 1.7 2024/01/24 09:54:56 sqko Exp $
use strict;
use warnings;
use RFC::Header;
use RFC::Main;

unless (@ARGV)
{
    print <<END;
Usage: rfcctl command [opt arguments]

Recognised commands (arguments after command):
In case of Yes/No options, the default is indicated with a capital letter
and will be set if no argument is specified

change-config section key new_value

add-errormail addr

del-errormail addr

list-errormails

add-systemmail addr

del-systemmail addr

list-systemmails

add-startmail addr

del-startmail addr

list-startmails

add-server servername

del-server servername

list-servers

account-regex regex

allow-exec yes/No

restrict-access yes/No

only-allow-explicit-data-access yes/No

set-listserver hostname

set-password-server hostname

add-data-access-authorisationid  authorisationid

del-data-access-authorisationid  authorisationid

list-data-access-authorisationids

set-report-access-authorisationid  authorisationid

END

    exit;
}

my $conf = &RFC::Main::config();
my %cmdlst = (
    'set-listserver' => \&set_listserver,
    'set-report-access-authorisationid' => \&set_reportid,
    'set-password-server' => \&set_pwdserver,
    'change-config' => \&change_config,
    'add-errormail' => \&add_errormail,
    'del-errormail' => \&del_errormail,
    'list-errormails' => \&list_errormails,
    'add-systemmail' => \&add_systemmail,
    'del-systemmail' => \&del_systemmail,
    'list-systemmails' => \&list_systemmails,
    'add-startmail' => \&add_startmail,
    'del-startmail' => \&del_startmail,
    'list-startmails' => \&list_startmails,
    'add-server' => \&add_server,
    'del-server' => \&del_server,
    'list-servers' => \&list_servers,
    'account-regex' => \&set_regex,
    'allow-exec' => \&allow_exex,
    'restrict-access' => \&restrict_access,
    'only-allow-explicit-data-access' => \&restrict_data_access,
    'add-data-access-authorisationid' => \&add_access,
    'del-data-access-authorisationid' => \&del_access,
    'list-data-access-authorisationids' => \&list_access,
);

my $cmd = shift;
if (exists($cmdlst{$cmd}))
{
    print $cmdlst{$cmd}->(@ARGV),"\n";
    exit;
}
else
{
    die "Command '$cmd' not recognised!\n";
}

sub set_listserver($)
{
    my $server = shift;
    return "No hostname specified!" unless ($server);
    $conf->change_config_value('servers','listserver',$server);
    return "ok";	
}

sub set_reportid($)
{
    my $id = shift;
    return "No ID specified or wrong format (integer)!" unless ($id && $id =~/^\d+$/);
    $conf->change_config_value('global','report_authorisationi',$id);
    return "ok";	
}

sub set_pwdserver($)
{
    my $server = shift;
    return "No hostname specified!" unless ($server);
    $conf->change_config_value('passwds','passwd_server',$server);
    return "ok";	
}

sub allow_exec($)
{
    my $mode = shift;
    if ($mode && $mode =~ /yes/i)
    {
	$conf->change_config_value('global','allow_exec','yes');
    }
    else
    {
	$conf->change_config_value('global','allow_exec','no');
    }
    return "ok";	
}

sub restrict_data_access($)
{
    my $mode = shift;
    if ($mode && $mode =~ /yes/i)
    {
	$conf->change_config_value('global','only_allow_explicit_data_access','yes');
    }
    else
    {
	$conf->change_config_value('global','only_allow_explicit_data_access','no');
    }
    return "ok";	
}


sub restrict_access($)
{
    my $mode = shift;
    if ($mode && $mode =~ /yes/i)
    {
	$conf->change_config_value('global','restrict_access_auth_only','yes');
    }
    else
    {
	$conf->change_config_value('global','restrict_access_auth_only','no');
    }
    return "ok";	
}

sub set_regex($)
{
    my $regex = shift;
    $conf->change_config_value('global','account_number_regexp',$regex);
    return "ok";
}

sub change_config($$$)
{
    my $section = shift;
    my $key = shift;
    my $value = shift;
    $conf->change_config_value($section,$key,$value);
    return "ok";
}

sub add_server($)
{
    my $server = shift;
    return "No hostname specified!" unless ($server);
    my @list = $conf->get_config_value('servers','server_names');
    my $found = 0;
    foreach (@list)
    {
	next unless ($_ eq $server);
	$found = 1;
	last;
    }
    unless ($found)
    {
	push @list,$server;
	$conf->change_config_value('servers','server_names',join(',',@list));
    }
    return "ok";
}

sub del_server($)
{
    my $server = shift;
    return "No hostname specified!" unless ($server);
    my @list = $conf->get_config_value('servers','server_names');
    my $found = 0;
    my @list2 = ();
    foreach (@list)
    {
	next if ($_ eq $server);
	push @list2,$_;
    }
    $conf->change_config_value('servers','server_names',join(',',@list2));
    return "ok";
}


sub add_errormail($)
{
    my $email = shift;
    return "No email specified!" unless ($email);
    my @list = $conf->get_config_value('global','errormails');
    my $found = 0;
    foreach (@list)
    {
	next unless ($_ eq $email);
	$found = 1;
	last;
    }
    unless ($found)
    {
	push @list,$email;
	$conf->change_config_value('global','errormails',join(',',@list));
    }
    return "ok";
}

sub del_errormail($)
{
    my $email = shift;
    return "No email specified!" unless ($email);
    my @list = $conf->get_config_value('global','errormails');
    my $found = 0;
    my @list2 = ();
    foreach (@list)
    {
	next if ($_ eq $email);
	push @list2,$_;
    }
    $conf->change_config_value('global','errormails',join(',',@list2));
    return "ok";
}


sub list_servers()
{
    my @list = $conf->get_config_value('servers','server_names');
    return join("\n",@list);
}


sub list_errormails()
{
    my @list = $conf->get_config_value('global','errormails');
    return join("\n",@list);
}

sub add_systemmail($)
{
    my $email = shift;
    return "No email specified!" unless ($email);
    my @list = $conf->get_config_value('admin','system_mail_users');
    my $found = 0;
    foreach (@list)
    {
	next unless ($_ eq $email);
	$found = 1;
	last;
    }
    unless ($found)
    {
	push @list,$email;
	$conf->change_config_value('admin','system_mail_users',join(',',@list));
    }
    return "ok";
}

sub del_systemmail($)
{
    my $email = shift;
    return "No email specified!" unless ($email);
    my @list = $conf->get_config_value('admin','system_mail_users');
    my $found = 0;
    my @list2 = ();
    foreach (@list)
    {
	next if ($_ eq $email);
	push @list2,$_;
    }
    $conf->change_config_value('admin','system_mail_users',join(',',@list2));
    return "ok";
}


sub list_systemmails()
{
    my @list = $conf->get_config_value('admin','system_mail_users');
    return join("\n",@list);
}


sub add_startmmail($)
{
    my $email = shift;
    return "No email specified!" unless ($email);
    my @list = $conf->get_config_value('celltest','start_test_mail_users');
    my $found = 0;
    foreach (@list)
    {
	next unless ($_ eq $email);
	$found = 1;
	last;
    }
    unless ($found)
    {
	push @list,$email;
	$conf->change_config_value('celltest','start_test_mail_users',join(',',@list));
    }
    return "ok";
}

sub del_startmail($)
{
    my $email = shift;
    return "No email specified!" unless ($email);
    my @list = $conf->get_config_value('celltest','start_test_mail_users');
    my $found = 0;
    my @list2 = ();
    foreach (@list)
    {
	next if ($_ eq $email);
	push @list2,$_;
    }
    $conf->change_config_value('celltest','start_test_mail_users',join(',',@list2));
    return "ok";
}


sub list_startmails()
{
    my @list = $conf->get_config_value('celltest','start_test_mail_users');
    return join("\n",@list);
}


sub add_access($)
{
    my $id = shift;
    return "No Id specified or not an integer!" unless ($id && $id =~ /^\d+$/);
    my @list = $conf->get_config_value('global','data_access_authorisationid');
    my $found = 0;
    foreach (@list)
    {
	next unless ($_ eq $id);
	$found = 1;
	last;
    }
    unless ($found)
    {
	push @list,$id;
	$conf->change_config_value('global','data_access_authorisationid',join(',',@list));
    }
    return "ok";
}

sub del_access($)
{
    my $id = shift;
    return "No ID specified or not an integer!" unless ($id && $id =~ /^\d+$/);
    my @list = $conf->get_config_value('global','data_access_authorisationid');
    my $found = 0;
    my @list2 = ();
    foreach (@list)
    {
	next if ($_ eq $id);
	push @list2,$_;
    }
    $conf->change_config_value('global','data_access_authorisationid',join(',',@list2));
    return "ok";
}


sub list_access()
{
    my @list = $conf->get_config_value('global','data_access_authorisationid');
    return join("\n",@list);
}
