Add support for turning off server connections

This commit is contained in:
mrkubax10 2023-09-22 21:29:33 +02:00
parent 6c2bf4aacf
commit e5e82db490
4 changed files with 105 additions and 7 deletions

View File

@ -86,6 +86,21 @@ sub verifyChannelAccess {
return 1;
}
sub enumerateServers {
my $aConnection = $_[0];
my $output = "<select name=\"server\">";
my $query = $aConnection->prepare(qq(select id, name from servers;));
$query->execute();
while(my @row = $query->fetchrow_array()) {
my $serverID = $row[0];
my $server = $row[1];
$output.="<option value=\"$serverID\">$server</option>";
}
$output.="</select>";
return $output;
}
sub enumerateChannels {
my $aConnection = $_[0];
@ -129,13 +144,13 @@ sub handlePath {
$userbar.="</form>";
}
my $query = $aConnection->prepare(qq(select channels.id, channels.name, channels.enabled, servers.name from channels inner join servers on channels.server_id=servers.id where channels.public=1;));
my $query = $aConnection->prepare(qq(select channels.id, channels.name, channels.enabled, servers.name, servers.enabled from channels inner join servers on channels.server_id=servers.id where channels.public=1;));
$query->execute();
my $table = "";
while(my @row = $query->fetchrow_array()) {
my $channelID = $row[0];
my $channelName = $row[1];
my $channelEnabled = $row[2];
my $channelEnabled = $row[2] && $row[4];
my $serverName = $row[3];
$channelName =~ s/%23/#/;
my $status = $channelEnabled?"<span style=\"color:green\">Enabled</span>":"<span style=\"color:gray\">Disabled</span>";
@ -159,12 +174,12 @@ sub handlePath {
}
while(@row = $query->fetchrow_array()) {
my $channelID = $row[0];
my $channelQuery = $aConnection->prepare(qq(select channels.name, channels.enabled, servers.name from channels inner join servers on channels.server_id=servers.id where channels.id=$channelID;));
my $channelQuery = $aConnection->prepare(qq(select channels.name, channels.enabled, servers.name, servers.enabled from channels inner join servers on channels.server_id=servers.id where channels.id=$channelID;));
$channelQuery->execute();
@row = $channelQuery->fetchrow_array();
my $channelName = $row[0];
$channelName =~ s/%23/#/;
my $channelEnabled = $row[1];
my $channelEnabled = $row[1] && $row[3];
my $serverName = $row[2];
my $status = $channelEnabled?"<span style=\"color:green\">Enabled</span>":"<span style=\"color:gray\">Disabled</span>";
$privateChannels.="<tr><td><a href=\"view_logs?channel=$channelID\">$channelName</a></td><td>$serverName</td><td>$status</td></tr>";
@ -274,6 +289,7 @@ sub handlePath {
}
my $addServer = "";
my $updateServer = "";
if($privileges==2) {
$addServer.="<h3>Add server</h3>";
$addServer.="<form action=\"add_server_action\" method=\"POST\">";
@ -282,6 +298,12 @@ sub handlePath {
$addServer.="<input name=\"port\" type=\"number\" placeholder=\"Server port (optional)\" /><br />";
$addServer.="<input type=\"submit\" value=\"Add\" />";
$addServer.="</form>";
$updateServer.="<h3>Update server</h3>";
$updateServer.="<form action=\"update_server_action\" method=\"POST\">";
$updateServer.=enumerateServers($aConnection)."<br />";
$updateServer.="<input name=\"enabled\" type=\"checkbox\" checked=\"true\" />Enabled<br />";
$updateServer.="<input type=\"submit\" value=\"Update\" />";
$updateServer.="</form>";
}
my $addChannel = "";
@ -312,7 +334,15 @@ sub handlePath {
$updateChannel.="</form>";
}
frontend::sendTemplate("templates/panel.html", $aClient, {"username"=>$session->{"username"}, "manageChannelAccess"=>$manageChannelAccess, "addUser"=>$addUser, "addServer"=>$addServer, "addChannel"=>$addChannel, "updateChannel"=>$updateChannel});
frontend::sendTemplate("templates/panel.html", $aClient, {
"username"=>$session->{"username"},
"manageChannelAccess"=>$manageChannelAccess,
"addUser"=>$addUser,
"addServer"=>$addServer,
"updateServer"=>$updateServer,
"addChannel"=>$addChannel,
"updateChannel"=>$updateChannel
});
return 1;
}
when("/change_password_action") {
@ -505,6 +535,47 @@ sub handlePath {
logger::createLogger($parameters{"name"}, $parameters{"address"}, $port, ());
return 1;
}
when("/update_server_action") {
if(!verifyRequestPrivileges($aRequest, $aClient, 2, $aConnection)) {
return 1;
}
my %parameters = frontend::parsePathParameters($aRequest->{"content"});
if(!defined($parameters{"server"}) || length($parameters{"server"})==0) {
frontend::sendBadRequest($aClient, "Server required");
return 1;
}
my $query = $aConnection->prepare(qq(select name, host, port, enabled from servers where id=?;));
$query->execute($parameters{"server"});
my @row = $query->fetchrow_array();
if(scalar(@row)==0) {
frontend::sendBadRequest($aClient, "Server with ID $parameters{'server'} doesn't exist");
return 1;
}
my $server = $row[0];
my $serverEnabled = $row[3];
if(defined($parameters{"enabled"}) && !$serverEnabled) {
my $host = $row[1];
my $port = $row[2];
$query = $aConnection->prepare(qq(select name, enabled from channels where server_id=?;));
$query->execute($parameters{"server"});
my @channels;
while(@row = $query->fetchrow_array()) {
if(!$row[1]) {
next;
}
push(@channels, $row[0]);
}
logger::createLogger($server, $host, $port, \@channels);
}
elsif($serverEnabled) {
my $actionQueue = logger::getActionQueueByServerName($server);
push(@$actionQueue, "QUIT");
}
$query = $aConnection->prepare(qq(update servers set enabled=? where id=?;));
$query->execute(defined($parameters{"enabled"})?1:0, $parameters{"server"});
frontend::redirect($aClient, "/server_updated.html");
return 1;
}
when("/add_channel_action") {
if(!verifyRequestPrivileges($aRequest, $aClient, 2, $aConnection)) {
return 1;
@ -577,7 +648,6 @@ sub handlePath {
my $actionQueue = logger::getActionQueueByServerName($row[0]);
if(defined($parameters{"enabled"}) && !$channelEnabled) {
push(@$actionQueue, "JOIN", $channel);
print("Joining\n");
}
elsif($channelEnabled) {
push(@$actionQueue, "PART", $channel);

View File

@ -361,6 +361,12 @@ sub partChannel {
$aStream->send(sprintf("PART %s\r\n", $aChannel));
}
sub quitFromServer {
my $aStream = $_[0];
$aStream->send("QUIT\r\n");
}
sub handleNames {
my $aCommand = $_[0];
my $aChannels = $_[1];
@ -413,17 +419,22 @@ sub connectionWorker {
my $buffer = "";
my @actionQueue :shared;
my $running = 1;
my @connection :shared = ($aServerName, \@actionQueue);
push(@connections, \@connection);
my %logFiles;
while($running) {
my $stream = connectToServer($aHost, $aPort, $aServerName);
my $streamSelect = IO::Select->new($stream);
while(!eof($stream)) {
while(!eof($stream) && $running) {
if(scalar(@actionQueue)>0) {
given($actionQueue[0]) {
when("JOIN") { joinChannel($stream, $actionQueue[1]); }
when("PART") { partChannel($stream, $actionQueue[1]); }
when("QUIT") {
quitFromServer($stream);
$running = 0;
}
}
@actionQueue = ();
}
@ -460,6 +471,12 @@ sub connectionWorker {
}
close($stream);
}
foreach my $i (0..scalar(@connections)-1) {
if($connections[$i][0] eq $aServerName) {
$connections[$i][0] = "";
last;
}
}
}
sub createLogger {

View File

@ -0,0 +1,10 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Server updated</title>
</head>
<body>
<p>Server successfully updated</p>
<a href="/panel">Return to user panel</a>
</body>
</html>

View File

@ -15,6 +15,7 @@
{{manageChannelAccess}}
{{addUser}}
{{addServer}}
{{updateServer}}
{{addChannel}}
{{updateChannel}}
</body>