3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 02:49:49 +01:00

Interpreter: use event queue for output queue and command queue

This commit is contained in:
Pragmatic Software 2020-03-06 13:30:09 -08:00
parent 74b3f35d3e
commit f5927c8761

View File

@ -27,12 +27,6 @@ sub initialize {
$self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_channels', $conf{compile_blocks_channels} // '.*');
$self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_ignore_channels', $conf{compile_blocks_ignore_channels} // 'none');
$self->{pbot}->{registry}->add_default('text', 'interpreter', 'max_recursion', 10);
$self->{output_queue} = {};
$self->{command_queue} = {};
$self->{pbot}->{timer}->register(sub { $self->process_output_queue }, 1);
$self->{pbot}->{timer}->register(sub { $self->process_command_queue }, 1);
}
sub process_line {
@ -945,25 +939,10 @@ sub output_result {
sub add_message_to_output_queue {
my ($self, $channel, $message, $delay) = @_;
if ($delay > 0 and exists $self->{output_queue}->{$channel}) {
my $last_when = $self->{output_queue}->{$channel}->[-1]->{when};
$message->{when} = $last_when + $delay;
} else {
$message->{when} = gettimeofday + $delay;
}
$self->{pbot}->{timer}->enqueue_event(
sub {
my ($self) = @_;
push @{$self->{output_queue}->{$channel}}, $message;
$self->process_output_queue if $delay <= 0;
}
sub process_output_queue {
my $self = shift;
foreach my $channel (keys %{$self->{output_queue}}) {
for (my $i = 0; $i < @{$self->{output_queue}->{$channel}}; $i++) {
my $message = $self->{output_queue}->{$channel}->[$i];
if (gettimeofday >= $message->{when}) {
my $stuff = {
from => $channel,
nick => $message->{nick},
@ -974,43 +953,18 @@ sub process_output_queue {
checkflood => $message->{checkflood}
};
$self->output_result($stuff);
splice @{$self->{output_queue}->{$channel}}, $i--, 1;
}
}
if (not @{$self->{output_queue}->{$channel}}) { delete $self->{output_queue}->{$channel}; }
}
$self->{pbot}->{interpreter}->output_result($stuff);
},
$delay, "output $channel $message->{message}"
);
}
sub add_to_command_queue {
my ($self, $channel, $command, $delay) = @_;
$command->{when} = gettimeofday + $delay;
push @{$self->{command_queue}->{$channel}}, $command;
}
sub add_botcmd_to_command_queue {
my ($self, $channel, $command, $delay) = @_;
my $botcmd = {
nick => $self->{pbot}->{registry}->get_value('irc', 'botnick'),
user => 'stdin',
host => 'pbot',
command => $command
};
$self->add_to_command_queue($channel, $botcmd, $delay);
}
sub process_command_queue {
my $self = shift;
foreach my $channel (keys %{$self->{command_queue}}) {
for (my $i = 0; $i < @{$self->{command_queue}->{$channel}}; $i++) {
my $command = $self->{command_queue}->{$channel}->[$i];
if (gettimeofday >= $command->{when}) {
$self->{pbot}->{timer}->enqueue_event(
sub {
my ($self) = @_;
my $stuff = {
from => $channel,
nick => $command->{nick},
@ -1027,15 +981,25 @@ sub process_command_queue {
$stuff->{'cap-override'} = $command->{'cap-override'};
}
my $result = $self->interpret($stuff);
my $result = $self->{pbot}->{interpreter}->interpret($stuff);
$stuff->{result} = $result;
$self->handle_result($stuff, $result);
splice @{$self->{command_queue}->{$channel}}, $i--, 1;
}
}
$self->{pbot}->{interpreter}->handle_result($stuff, $result);
},
$delay, "command $channel $command->{command}"
);
}
if (not @{$self->{command_queue}->{$channel}}) { delete $self->{command_queue}->{$channel}; }
}
sub add_botcmd_to_command_queue {
my ($self, $channel, $command, $delay) = @_;
my $botcmd = {
nick => $self->{pbot}->{registry}->get_value('irc', 'botnick'),
user => 'stdin',
host => 'pbot',
command => $command
};
$self->add_to_command_queue($channel, $botcmd, $delay);
}
1;