Logger: Detect from which channels user left after quit

This commit is contained in:
mrkubax10 2023-09-17 20:16:19 +02:00
parent b0fc9ff2a5
commit 73419f5a9c

View File

@ -165,7 +165,8 @@ sub prepareLogFile {
open(my $file, ">>", $outputFilePath);
if($file) {
printf(":: Logger -> Outputting channel '%s' at '%s' to '%s'\n", $aChannelName, $aServerName, $outputFilePath);
$aLogFiles->{$aChannelName} = $file;
$aLogFiles->{$aChannelName}{"file"} = $file;
$aLogFiles->{$aChannelName}{"names"} = [];
}
else {
print(":: Logger -> Failed to open '$outputFilePath' for writing\n");
@ -203,8 +204,8 @@ sub handlePrivMsg {
if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) {
return;
}
$aLogFiles->{$aCommand->[1]}->print(sprintf("(%s) %s: %s\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[3]), $aCommand->[2]));
$aLogFiles->{$aCommand->[1]}->flush();
$aLogFiles->{$aCommand->[1]}{"file"}->print(sprintf("(%s) %s: %s\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[3]), $aCommand->[2]));
$aLogFiles->{$aCommand->[1]}{"file"}->flush();
}
sub handleJoin {
@ -220,15 +221,14 @@ sub handleJoin {
if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) {
return;
}
$aLogFiles->{$aCommand->[1]}->print(sprintf("(%s) %s has joined %s\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[2]), $aCommand->[1]));
$aLogFiles->{$aCommand->[1]}->flush();
$aLogFiles->{$aCommand->[1]}{"file"}->print(sprintf("(%s) %s has joined %s\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[2]), $aCommand->[1]));
$aLogFiles->{$aCommand->[1]}{"file"}->flush();
}
sub handleQuit {
my $aCommand = $_[0];
my $aServerName = $_[1];
my $aJoinedChannels = $_[2];
my $aLogFiles = $_[3];
my $aLogFiles = $_[2];
my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=3 && $aCommandLength!=2) {
@ -239,12 +239,20 @@ sub handleQuit {
if($aCommandLength==3) {
$reason = $aCommand->[1];
}
foreach my $channel (@$aJoinedChannels) {
if(!prepareLogFile($aLogFiles, $aServerName, $channel)) {
my $username = getUsernameFromHost($aCommand->[$aCommandLength-1]);
foreach my $channel (keys(%$aLogFiles)) {
my $found = 0;
foreach my $name (@{$aLogFiles->{$channel}{"names"}}) {
if($name eq $username) {
$found = 1;
last;
}
}
if(!$found || !prepareLogFile($aLogFiles, $aServerName, $channel)) {
next;
}
$aLogFiles->{$channel}->print(sprintf("(%s) %s has quit (%s)\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[2]), $reason));
$aLogFiles->{$channel}->flush();
$aLogFiles->{$channel}{"file"}->print(sprintf("(%s) %s has quit (%s)\n", localtime->strftime("%H:%M:%S"), $username, $reason));
$aLogFiles->{$channel}{"file"}->flush();
}
}
@ -261,8 +269,8 @@ sub handlePart {
if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) {
return;
}
$aLogFiles->{$aCommand->[1]}->print(sprintf("(%s) %s has left %s\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[2]), $aCommand->[1]));
$aLogFiles->{$aCommand->[1]}->flush();
$aLogFiles->{$aCommand->[1]}{"file"}->print(sprintf("(%s) %s has left %s\n", localtime->strftime("%H:%M:%S"), getUsernameFromHost($aCommand->[2]), $aCommand->[1]));
$aLogFiles->{$aCommand->[1]}{"file"}->flush();
}
sub joinChannel {
@ -281,6 +289,23 @@ sub joinChannels {
}
}
sub handleNames {
my $aCommand = $_[0];
my $aChannels = $_[1];
my $aLogFiles = $_[2];
my $aCommandLength = scalar(@$aCommand);
if($aCommandLength!=6) {
print("Encountered invalid NAMES command (6 arguments expected, $aCommandLength provided)\n");
return;
}
my @names = split(" ", $aCommand->[4]);
if(!defined($aLogFiles->{$aCommand->[3]})) {
return;
}
push(@{$aLogFiles->{$aCommand->[3]}{"names"}}, @names);
}
our @connections :shared;
our $running :shared = 1;
@ -319,14 +344,15 @@ sub connectionWorker {
$buffer = $remaining;
while(length($line)>0) {
my @command = parseIRCCommand($line);
#printf(":: Server -> %s\n", $line);
printf(":: Server -> %s\n", $line);
given($command[0]) {
when("PING") { handlePing($stream, \@command); }
when("PRIVMSG") { handlePrivMsg($stream, \@command, $aServerName, $aChannels, \%logFiles); }
when("JOIN") { handleJoin(\@command, $aServerName, \%logFiles); }
when("QUIT") { handleQuit(\@command, $aServerName, $aChannels, \%logFiles); }
when("QUIT") { handleQuit(\@command, $aServerName, \%logFiles); }
when("PART") { handlePart(\@command, $aServerName, \%logFiles); }
when("376") { joinChannels($stream, $aChannels); } # end of MOTD
when("353") { handleNames(\@command, $aChannels, \%logFiles); } # NAMES reply
}
($line, $remaining) = readLineFromBuffer($buffer);
$buffer = $remaining;