Frontend: Implement channel adding support
This commit is contained in:
92
logger.pm
92
logger.pm
@@ -17,10 +17,12 @@
|
||||
package logger;
|
||||
|
||||
use IO::Socket;
|
||||
use IO::Select;
|
||||
use List::Util;
|
||||
use Time::Piece;
|
||||
use File::Path;
|
||||
use threads;
|
||||
use threads::shared;
|
||||
|
||||
use lib ".";
|
||||
use configuration;
|
||||
@@ -34,13 +36,35 @@ sub connectToServer {
|
||||
my $aPort = $_[1];
|
||||
my $aServerName = $_[2];
|
||||
|
||||
my $socket = new IO::Socket::INET(PeerAddr=>$aServer, PeerPort=>$aPort, Proto=>"tcp");
|
||||
my $socket = IO::Socket::INET->new(PeerAddr=>$aServer, PeerPort=>$aPort, Proto=>"tcp");
|
||||
$socket->send(sprintf("PASS %s\r\n", $configuration::botPassword));
|
||||
$socket->send(sprintf("NICK %s\r\n", $configuration::botNick));
|
||||
$socket->send(sprintf("USER %s %s %s :%s\r\n", $configuration::botUsername, $configuration::botHostname, $aServerName, $configuration::botName));
|
||||
return $socket;
|
||||
}
|
||||
|
||||
sub readLineFromBuffer {
|
||||
my $aBuffer = $_[0];
|
||||
|
||||
my $output = "";
|
||||
my $bufferLength = length($aBuffer);
|
||||
foreach my $i (0..$bufferLength-1) {
|
||||
my $char = substr($aBuffer, $i, 1);
|
||||
if($char eq "\n" || ($char eq "\r" && $i+1<$bufferLength && substr($aBuffer, $i+1, 1) eq "\n")) {
|
||||
my $outputLength = length($output);
|
||||
if($char eq "\r" && $i+1<$bufferLength && substr($aBuffer, $i+1, 1) eq "\n") {
|
||||
$outputLength+=2;
|
||||
}
|
||||
else {
|
||||
$outputLength++;
|
||||
}
|
||||
return ($output, substr($aBuffer, $outputLength, $bufferLength-$outputLength));
|
||||
}
|
||||
$output.=$char;
|
||||
}
|
||||
return ("", $aBuffer);
|
||||
}
|
||||
|
||||
sub stripPrefix {
|
||||
my $aLine = $_[0];
|
||||
|
||||
@@ -257,43 +281,75 @@ sub joinChannels {
|
||||
}
|
||||
}
|
||||
|
||||
our @connections :shared;
|
||||
|
||||
sub connectionWorker {
|
||||
my $aHost = $_[0];
|
||||
my $aPort = $_[1];
|
||||
my $aServerName = $_[2];
|
||||
my $aChannels = $_[3];
|
||||
my $aConnection = $_[4];
|
||||
|
||||
my %logFiles;
|
||||
my $stream = connectToServer($aHost, $aPort, $aServerName);
|
||||
while(!eof($stream) && $aConnection->[1]) {
|
||||
my $line = readline($stream);
|
||||
my @command = parseIRCCommand($line);
|
||||
printf(":: Server -> %s", $line);
|
||||
my $streamSelect = IO::Select->new($stream);
|
||||
my $buffer = "";
|
||||
my @actionQueue :shared;
|
||||
my @connection :shared = ($aServerName, \@actionQueue);
|
||||
push(@connections, \@connection);
|
||||
while(!eof($stream)) {
|
||||
if(scalar(@actionQueue)>0) {
|
||||
given($actionQueue[0]) {
|
||||
when("JOIN") {
|
||||
joinChannel($stream, $actionQueue[1]);
|
||||
}
|
||||
}
|
||||
@actionQueue = ();
|
||||
}
|
||||
|
||||
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("PART") { handlePart(\@command, $aServerName, \%logFiles); }
|
||||
when("376") { joinChannels($stream, $aChannels); } # end of MOTD
|
||||
my @canRead = $streamSelect->can_read(0);
|
||||
if(scalar(@canRead)==0) {
|
||||
next;
|
||||
}
|
||||
my $tempBuffer;
|
||||
$stream->recv($tempBuffer, 512);
|
||||
$buffer.=$tempBuffer;
|
||||
my ($line, $remaining) = readLineFromBuffer($buffer);
|
||||
$buffer = $remaining;
|
||||
while(length($line)>0) {
|
||||
my @command = parseIRCCommand($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("PART") { handlePart(\@command, $aServerName, \%logFiles); }
|
||||
when("376") { joinChannels($stream, $aChannels); } # end of MOTD
|
||||
}
|
||||
($line, $remaining) = readLineFromBuffer($buffer);
|
||||
$buffer = $remaining;
|
||||
}
|
||||
}
|
||||
close($stream);
|
||||
}
|
||||
|
||||
our @connections;
|
||||
|
||||
sub createLogger {
|
||||
my $aName = $_[0];
|
||||
my $aHost = $_[1];
|
||||
my $aPort = $_[2];
|
||||
my $aChannels = $_[3];
|
||||
|
||||
my @connection = ($aName, 1);
|
||||
push(@connection, threads->create("connectionWorker", $aHost, $aPort, $aName, $aChannels, \@connection));
|
||||
push(@connections, @connection);
|
||||
threads->create("connectionWorker", $aHost, $aPort, $aName, $aChannels);
|
||||
}
|
||||
|
||||
sub getActionQueueByServerName {
|
||||
my $aServerName = $_[0];
|
||||
|
||||
foreach my $connection (@connections) {
|
||||
if($connection->[0] eq $aServerName) {
|
||||
return $connection->[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
my $db = DBI->connect("DBI:SQLite:dbname=$configuration::database", "", "", {RaiseError=>1});
|
||||
|
||||
Reference in New Issue
Block a user