diff --git a/frontend.pm b/frontend.pm index 36a9b50..4b18946 100644 --- a/frontend.pm +++ b/frontend.pm @@ -310,7 +310,6 @@ sub sendTemplate { my $response = getBaseResponse(200, "OK"); $response.="Content-Length: $length\r\n\r\n"; $response.=$content; - print("$response\n"); $aClient->send($response); } @@ -338,7 +337,7 @@ sub handlePath { when("/view_logs") { my $channelID = $aRequest->{"path"}{"parameters"}{"channel"}; if(!defined($channelID)) { - sendBadRequest($aClient, "view_log requires channel URL parameter"); + sendBadRequest($aClient, "view_logs requires channel URL parameter"); return 1; } @@ -362,12 +361,49 @@ sub handlePath { my $table = ""; foreach my $entry (@entries) { - $table.="$entry"; + $table.="$entry"; } sendTemplate("templates/view_logs.html", $aClient, {"channel"=>$channelName, "server"=>$serverName, "logs"=>$table}); return 1; } + when("/view_log") { + my $channelID = $aRequest->{"path"}{"parameters"}{"channel"}; + if(!defined($channelID)) { + sendBadRequest($aClient, "view_log requires channel URL parameter"); + return 1; + } + my $logFile = $aRequest->{"path"}{"parameters"}{"file"}; + if(!defined($channelID)) { + sendBadRequest($aClient, "view_log requires file URL parameter"); + return 1; + } + + my $query = $aConnection->prepare(qq(select channels.name, servers.name from channels inner join servers on channels.server_id=servers.id where channels.id=?;)); + $query->execute($channelID); + my @row = $query->fetchrow_array(); + if(scalar(@row)==0) { + sendBadRequest($aClient, "Unknown channel with ID $channelID"); + return 1; + } + my $channelName = $row[0]; + my $serverName = $row[1]; + my $logFilePath = "logs/".$serverName."/".$channelName."/".$logFile; + + my $result = open(my $file, "<", $logFilePath); + if(!$result) { + sendBadRequest($aClient, "No log file $logFile for channel $channelName at $serverName"); + return 1; + } + my $content = readFullFile($file); + close($file); + + my $response = getBaseResponse(200, "OK"); + $response.="Content-Type: text/plain;charset=utf-8\r\n"; + $response.="Content-Length: ".length($content)."\r\n\r\n"; + $response.=$content; + $aClient->send($response); + } } return 0; }