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:
parent
74b3f35d3e
commit
f5927c8761
@ -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_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('array', 'general', 'compile_blocks_ignore_channels', $conf{compile_blocks_ignore_channels} // 'none');
|
||||||
$self->{pbot}->{registry}->add_default('text', 'interpreter', 'max_recursion', 10);
|
$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 {
|
sub process_line {
|
||||||
@ -945,25 +939,10 @@ sub output_result {
|
|||||||
sub add_message_to_output_queue {
|
sub add_message_to_output_queue {
|
||||||
my ($self, $channel, $message, $delay) = @_;
|
my ($self, $channel, $message, $delay) = @_;
|
||||||
|
|
||||||
if ($delay > 0 and exists $self->{output_queue}->{$channel}) {
|
$self->{pbot}->{timer}->enqueue_event(
|
||||||
my $last_when = $self->{output_queue}->{$channel}->[-1]->{when};
|
sub {
|
||||||
$message->{when} = $last_when + $delay;
|
my ($self) = @_;
|
||||||
} else {
|
|
||||||
$message->{when} = gettimeofday + $delay;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = {
|
my $stuff = {
|
||||||
from => $channel,
|
from => $channel,
|
||||||
nick => $message->{nick},
|
nick => $message->{nick},
|
||||||
@ -974,43 +953,18 @@ sub process_output_queue {
|
|||||||
checkflood => $message->{checkflood}
|
checkflood => $message->{checkflood}
|
||||||
};
|
};
|
||||||
|
|
||||||
$self->output_result($stuff);
|
$self->{pbot}->{interpreter}->output_result($stuff);
|
||||||
splice @{$self->{output_queue}->{$channel}}, $i--, 1;
|
},
|
||||||
}
|
$delay, "output $channel $message->{message}"
|
||||||
}
|
);
|
||||||
|
|
||||||
if (not @{$self->{output_queue}->{$channel}}) { delete $self->{output_queue}->{$channel}; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub add_to_command_queue {
|
sub add_to_command_queue {
|
||||||
my ($self, $channel, $command, $delay) = @_;
|
my ($self, $channel, $command, $delay) = @_;
|
||||||
|
|
||||||
$command->{when} = gettimeofday + $delay;
|
$self->{pbot}->{timer}->enqueue_event(
|
||||||
|
sub {
|
||||||
push @{$self->{command_queue}->{$channel}}, $command;
|
my ($self) = @_;
|
||||||
}
|
|
||||||
|
|
||||||
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}) {
|
|
||||||
my $stuff = {
|
my $stuff = {
|
||||||
from => $channel,
|
from => $channel,
|
||||||
nick => $command->{nick},
|
nick => $command->{nick},
|
||||||
@ -1027,15 +981,25 @@ sub process_command_queue {
|
|||||||
$stuff->{'cap-override'} = $command->{'cap-override'};
|
$stuff->{'cap-override'} = $command->{'cap-override'};
|
||||||
}
|
}
|
||||||
|
|
||||||
my $result = $self->interpret($stuff);
|
my $result = $self->{pbot}->{interpreter}->interpret($stuff);
|
||||||
$stuff->{result} = $result;
|
$stuff->{result} = $result;
|
||||||
$self->handle_result($stuff, $result);
|
$self->{pbot}->{interpreter}->handle_result($stuff, $result);
|
||||||
splice @{$self->{command_queue}->{$channel}}, $i--, 1;
|
},
|
||||||
}
|
$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;
|
1;
|
||||||
|
Loading…
Reference in New Issue
Block a user