diff --git a/database_settings.sql b/database_settings.sql index 1e8919c..9929ff6 100644 --- a/database_settings.sql +++ b/database_settings.sql @@ -1,7 +1,8 @@ create table channels(id int primary key not null, server_id int not null, -- foreign key in servers table name text not null, - public int not null + public int not null, + enabled int not null ); create table users(id int primary key not null, @@ -13,7 +14,8 @@ create table users(id int primary key not null, create table servers(id int primary key not null, name text not null, host text not null, - port int not null + port int not null, + enabled int not null ); create table accessors(user_id int not null, -- foreign key in users table diff --git a/frontend_routes.pm b/frontend_routes.pm index 0a577a3..3726440 100644 --- a/frontend_routes.pm +++ b/frontend_routes.pm @@ -129,16 +129,17 @@ sub handlePath { $userbar.=""; } - my $query = $aConnection->prepare(qq(select channels.id, channels.name, 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 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 $serverName = $row[2]; + my $channelEnabled = $row[2]; + my $serverName = $row[3]; $channelName =~ s/%23/#/; - - $table.="$channelName$serverName"; + my $status = $channelEnabled?"Enabled":"Disabled"; + $table.="$channelName$serverName$status"; } my $privateChannels = ""; @@ -158,13 +159,15 @@ sub handlePath { } while(@row = $query->fetchrow_array()) { my $channelID = $row[0]; - my $channelQuery = $aConnection->prepare(qq(select channels.name, 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 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 $serverName = $row[1]; - $privateChannels.="$channelName$serverName"; + my $channelEnabled = $row[1]; + my $serverName = $row[2]; + my $status = $channelEnabled?"Enabled":"Disabled"; + $privateChannels.="$channelName$serverName$status"; } } @@ -304,6 +307,7 @@ sub handlePath { $updateChannel.="
"; $updateChannel.=enumerateChannels($aConnection)."
"; $updateChannel.="Public
"; + $updateChannel.="Enabled
"; $updateChannel.=""; $updateChannel.="
"; } @@ -495,7 +499,7 @@ sub handlePath { $lastID = $row[0]+1; } - $query = $aConnection->prepare(qq(insert into servers values($lastID, ?, ?, ?);)); + $query = $aConnection->prepare(qq(insert into servers values($lastID, ?, ?, ?, 1);)); $query->execute($parameters{"name"}, $parameters{"address"}, $port); frontend::redirect($aClient, "/server_added.html"); logger::createLogger($parameters{"name"}, $parameters{"address"}, $port, ()); @@ -541,7 +545,7 @@ sub handlePath { $lastID = $row[0]+1; } - $query = $aConnection->prepare(qq(insert into channels values($lastID, ?, ?, ?);)); + $query = $aConnection->prepare(qq(insert into channels values($lastID, ?, ?, ?, 1);)); $query->execute($parameters{"server"}, $parameters{"channel"}, defined($parameters{"public"})?1:0); my $actionQueue = logger::getActionQueueByServerName($serverName); push(@$actionQueue, "JOIN", $parameters{"channel"}); @@ -558,15 +562,28 @@ sub handlePath { frontend::sendBadRequest($aClient, "Channel required"); return 1; } - my $query = $aConnection->prepare(qq(select id from channels where id=?;)); + my $query = $aConnection->prepare(qq(select name, server_id, enabled from channels where id=?;)); $query->execute($parameters{"channel"}); my @row = $query->fetchrow_array(); if(scalar(@row)==0) { frontend::sendBadRequest($aClient, "Channel with ID $parameters{'channel'} doesn't exist"); return 1; } - $query = $aConnection->prepare(qq(update channels set public=? where id=?;)); - $query->execute(defined($parameters{"public"})?1:0, $parameters{"channel"}); + my $channel = $row[0]; + my $channelEnabled = $row[2]; + $query = $aConnection->prepare(qq(select name from servers where id=?;)); + $query->execute($row[1]); + @row = $query->fetchrow_array(); + my $actionQueue = logger::getActionQueueByServerName($row[0]); + if(defined($parameters{"enabled"}) && !$channelEnabled) { + push(@$actionQueue, "JOIN", $channel); + print("Joining\n"); + } + elsif($channelEnabled) { + push(@$actionQueue, "PART", $channel); + } + $query = $aConnection->prepare(qq(update channels set public=?, enabled=? where id=?;)); + $query->execute(defined($parameters{"public"})?1:0, defined($parameters{"enabled"})?1:0, $parameters{"channel"}); frontend::redirect($aClient, "/channel_updated.html"); return 1; } diff --git a/logger.pm b/logger.pm index bc8f8f0..b42ea99 100644 --- a/logger.pm +++ b/logger.pm @@ -354,6 +354,13 @@ sub joinChannels { } } +sub partChannel { + my $aStream = $_[0]; + my $aChannel = $_[1]; + + $aStream->send(sprintf("PART %s\r\n", $aChannel)); +} + sub handleNames { my $aCommand = $_[0]; my $aChannels = $_[1]; @@ -415,9 +422,8 @@ sub connectionWorker { while(!eof($stream)) { if(scalar(@actionQueue)>0) { given($actionQueue[0]) { - when("JOIN") { - joinChannel($stream, $actionQueue[1]); - } + when("JOIN") { joinChannel($stream, $actionQueue[1]); } + when("PART") { partChannel($stream, $actionQueue[1]); } } @actionQueue = (); } @@ -483,12 +489,20 @@ while(my @row = $query->fetchrow_array()) { my $name = $row[1]; my $host = $row[2]; my $port = $row[3]; + my $enabled = $row[4]; - my $channelQuery = $db->prepare(qq(select name from channels where server_id=$id;)); + if(!$enabled) { + next; + } + my $channelQuery = $db->prepare(qq(select name, enabled from channels where server_id=$id;)); $channelQuery->execute(); my @channels; while(my @channelsRow = $channelQuery->fetchrow_array()) { my $name = $channelsRow[0]; + my $enabled = $channelsRow[1]; + if(!$enabled) { + next; + } push(@channels, $name); } createLogger($name, $host, $port, \@channels); diff --git a/templates/index.html b/templates/index.html index d21f42f..076c88d 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,7 +7,7 @@ {{userbar}}

Channel list

- + {{publicChannels}} {{privateChannels}}
ChannelNetwork
ChannelNetworkStatus