2010-03-17 07:36:54 +01:00
|
|
|
# File: Interpreter.pm
|
2010-03-22 08:33:44 +01:00
|
|
|
# Author: pragma_
|
2010-03-17 07:36:54 +01:00
|
|
|
#
|
2010-03-22 08:33:44 +01:00
|
|
|
# Purpose:
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
package PBot::Interpreter;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
use base 'PBot::Registerable';
|
|
|
|
|
2015-03-29 01:50:43 +01:00
|
|
|
use Time::HiRes qw/gettimeofday/;
|
2015-03-31 00:04:08 +02:00
|
|
|
use Time::Duration;
|
2010-04-13 06:17:54 +02:00
|
|
|
use LWP::UserAgent;
|
2010-03-22 08:33:44 +01:00
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
if(ref($_[1]) eq 'HASH') {
|
2014-05-19 23:34:24 +02:00
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 08:33:44 +01:00
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
|
|
|
$self->SUPER::initialize(%conf);
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2014-05-19 23:34:24 +02:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
2010-04-13 06:17:54 +02:00
|
|
|
|
2014-10-14 04:30:57 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'general', 'compile_blocks', $conf{compile_blocks} // 1);
|
|
|
|
$self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_channels', $conf{compile_blocks_channels} // '.*');
|
2014-05-19 23:34:24 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_ignore_channels', $conf{compile_blocks_ignore_channels} // 'none');
|
2015-05-16 02:53:13 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'general', 'paste_ratelimit', $conf{paste_ratelimit} // 60);
|
2014-05-31 03:05:47 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'interpreter', 'max_recursion', 10);
|
2015-03-29 01:50:43 +01:00
|
|
|
|
2016-07-01 22:00:20 +02:00
|
|
|
$self->{output_queue} = {};
|
|
|
|
$self->{command_queue} = {};
|
|
|
|
$self->{last_paste} = 0;
|
2015-03-29 01:50:43 +01:00
|
|
|
|
2016-07-01 22:00:20 +02:00
|
|
|
$self->{pbot}->{timer}->register(sub { $self->process_output_queue }, 1);
|
|
|
|
$self->{pbot}->{timer}->register(sub { $self->process_command_queue }, 1);
|
2012-07-22 21:22:30 +02:00
|
|
|
}
|
2010-04-13 06:17:54 +02:00
|
|
|
|
2010-03-17 07:36:54 +01:00
|
|
|
sub process_line {
|
2010-03-22 08:33:44 +01:00
|
|
|
my $self = shift;
|
2010-03-17 07:36:54 +01:00
|
|
|
my ($from, $nick, $user, $host, $text) = @_;
|
2012-07-22 21:22:30 +02:00
|
|
|
|
2014-03-14 11:05:11 +01:00
|
|
|
my $command;
|
2012-07-22 21:22:30 +02:00
|
|
|
my $has_code;
|
|
|
|
my $nick_override;
|
2015-04-19 10:55:52 +02:00
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
2015-09-08 10:37:34 +02:00
|
|
|
my $processed = 0;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
|
|
|
$from = lc $from if defined $from;
|
|
|
|
|
2014-05-19 23:34:24 +02:00
|
|
|
my $pbot = $self->{pbot};
|
2010-03-22 08:33:44 +01:00
|
|
|
|
2014-05-13 12:15:52 +02:00
|
|
|
my $message_account = $pbot->{messagehistory}->get_message_account($nick, $user, $host);
|
|
|
|
$pbot->{messagehistory}->add_message($message_account, "$nick!$user\@$host", $from, $text, $pbot->{messagehistory}->{MSG_CHAT});
|
|
|
|
|
2015-07-08 23:05:55 +02:00
|
|
|
my $flood_threshold = $pbot->{registry}->get_value($from, 'chat_flood_threshold');
|
|
|
|
my $flood_time_threshold = $pbot->{registry}->get_value($from, 'chat_flood_time_threshold');
|
|
|
|
|
|
|
|
$flood_threshold = $pbot->{registry}->get_value('antiflood', 'chat_flood_threshold') if not defined $flood_threshold;
|
|
|
|
$flood_time_threshold = $pbot->{registry}->get_value('antiflood', 'chat_flood_time_threshold') if not defined $flood_time_threshold;
|
|
|
|
|
2014-05-19 04:42:18 +02:00
|
|
|
$pbot->{antiflood}->check_flood($from, $nick, $user, $host, $text,
|
2015-07-08 23:05:55 +02:00
|
|
|
$flood_threshold, $flood_time_threshold,
|
2014-05-19 04:42:18 +02:00
|
|
|
$pbot->{messagehistory}->{MSG_CHAT}) if defined $from;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-09-08 10:37:34 +02:00
|
|
|
my $preserve_whitespace = 0;
|
|
|
|
|
2013-07-24 14:33:19 +02:00
|
|
|
$text =~ s/^\s+//;
|
|
|
|
$text =~ s/\s+$//;
|
2012-07-22 21:22:30 +02:00
|
|
|
|
2013-07-24 14:33:19 +02:00
|
|
|
my $cmd_text = $text;
|
|
|
|
$cmd_text =~ s/^\/me\s+//;
|
|
|
|
|
2015-03-23 12:27:41 +01:00
|
|
|
# get channel-specific trigger if available
|
|
|
|
my $bot_trigger = $pbot->{registry}->get_value($from, 'trigger');
|
|
|
|
|
|
|
|
if (not defined $bot_trigger) {
|
|
|
|
$bot_trigger = $pbot->{registry}->get_value('general', 'trigger');
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2015-09-04 05:56:44 +02:00
|
|
|
my $referenced;
|
|
|
|
my $count = 0;
|
|
|
|
while (++$count <= 3) {
|
|
|
|
$referenced = 0;
|
|
|
|
$command = undef;
|
|
|
|
$has_code = undef;
|
|
|
|
|
2016-01-19 15:51:40 +01:00
|
|
|
if ($cmd_text =~ s/\B$bot_trigger`([^`]+)// || $cmd_text =~ s/\B$bot_trigger\{([^}]+)//) {
|
|
|
|
my $cmd = $1;
|
|
|
|
my ($nick) = $cmd_text =~ m/^([^ ,:;]+)/;
|
2016-10-14 14:56:54 +02:00
|
|
|
$nick = $self->{pbot}->{nicklist}->is_present($from, $nick);
|
2016-01-19 15:51:40 +01:00
|
|
|
if ($nick) {
|
|
|
|
$command = "tell $nick about $cmd";
|
|
|
|
} else {
|
|
|
|
$command = $cmd;
|
|
|
|
}
|
|
|
|
$referenced = 1;
|
|
|
|
} elsif($cmd_text =~ s/^(?:$bot_trigger|$botnick.?)?\s*{\s*(.*)\s*}\s*$//) {
|
2015-09-04 05:56:44 +02:00
|
|
|
$has_code = $1 if length $1;
|
|
|
|
$preserve_whitespace = 1;
|
2015-09-08 10:37:34 +02:00
|
|
|
$processed += 100;
|
|
|
|
} elsif($cmd_text =~ s/^\s*([^,:\(\)\+\*\/ ]+)[,:]*\s*{\s*(.*)\s*}\s*$//) {
|
|
|
|
$nick_override = $1;
|
|
|
|
$has_code = $2 if length $2 and $nick_override !~ /^(?:enum|struct|union)$/;
|
|
|
|
$preserve_whitespace = 1;
|
2016-11-17 04:07:01 +01:00
|
|
|
my $nick_override = $self->{pbot}->{nicklist}->is_present($from, $nick_override);
|
2015-09-08 10:37:34 +02:00
|
|
|
$processed += 100;
|
2016-10-23 12:51:59 +02:00
|
|
|
} elsif($cmd_text =~ s/^\s*([^,:\(\)\+\*\/ ]+)[,:]?\s+$bot_trigger(.*)$//) {
|
2016-10-14 14:56:54 +02:00
|
|
|
$nick_override = $1;
|
|
|
|
$command = $2;
|
|
|
|
|
2016-10-23 12:51:59 +02:00
|
|
|
my $similar = $self->{pbot}->{nicklist}->is_present_similar($from, $nick_override);
|
|
|
|
if ($similar) {
|
|
|
|
$nick_override = $similar;
|
|
|
|
} else {
|
|
|
|
$self->{pbot}->{logger}->log("No similar nick for $nick_override\n");
|
|
|
|
return 0;
|
2016-10-14 14:56:54 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 12:51:59 +02:00
|
|
|
$processed += 100;
|
|
|
|
} elsif($cmd_text =~ s/^$bot_trigger(.*)$//) {
|
|
|
|
$command = $1;
|
2015-09-08 10:37:34 +02:00
|
|
|
$processed += 100;
|
2016-06-26 03:09:47 +02:00
|
|
|
} elsif($cmd_text =~ s/^.?$botnick.?\s*(.*?)$//i) {
|
2015-09-04 05:56:44 +02:00
|
|
|
$command = $1;
|
2015-09-08 10:37:34 +02:00
|
|
|
$processed += 100;
|
2016-06-26 03:09:47 +02:00
|
|
|
} elsif($cmd_text =~ s/^(.*?),?\s*$botnick[?!.]*$//i) {
|
2016-01-19 15:51:40 +01:00
|
|
|
$command = $1;
|
|
|
|
$processed += 100;
|
2015-09-04 05:56:44 +02:00
|
|
|
}
|
|
|
|
|
2015-09-08 10:37:34 +02:00
|
|
|
last if not defined $command and not defined $has_code;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-09-08 10:37:34 +02:00
|
|
|
if((!defined $command || $command !~ /^login /) && defined $from && $pbot->{ignorelist}->check_ignore($nick, $user, $host, $from)) {
|
2015-09-07 14:04:54 +02:00
|
|
|
my $admin = $pbot->{admins}->loggedin($from, "$nick!$user\@$host");
|
|
|
|
if (!defined $admin || $admin->{level} < 10) {
|
|
|
|
# ignored hostmask
|
2015-09-08 10:37:34 +02:00
|
|
|
return 1;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2015-09-07 14:04:54 +02:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-09-08 10:37:34 +02:00
|
|
|
if(defined $has_code) {
|
|
|
|
$processed += 100; # ensure no other plugins try to parse this message
|
2015-09-07 14:04:54 +02:00
|
|
|
if($pbot->{registry}->get_value('general', 'compile_blocks') and not $pbot->{registry}->get_value($from, 'no_compile_blocks')
|
|
|
|
and not grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_ignore_channels')
|
|
|
|
and grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_channels')) {
|
|
|
|
if (not defined $nick_override or (defined $nick_override and $self->{pbot}->{nicklist}->is_present($from, $nick_override))) {
|
|
|
|
$pbot->{factoids}->{factoidmodulelauncher}->execute_module($from, undef, $nick, $user, $host, $text, "compiler_block", (defined $nick_override ? $nick_override : $nick) . " $from $has_code }", $preserve_whitespace);
|
2015-06-20 01:16:23 +02:00
|
|
|
}
|
2014-05-19 23:34:24 +02:00
|
|
|
}
|
2015-09-07 14:04:54 +02:00
|
|
|
} else {
|
2016-10-14 14:56:54 +02:00
|
|
|
$processed++ if $self->handle_result($from, $nick, $user, $host, $text, $command, $self->interpret($from, $nick, $user, $host, 1, $command, $nick_override, $referenced), 1, $preserve_whitespace);
|
2010-04-17 21:22:22 +02:00
|
|
|
}
|
2014-03-14 11:05:11 +01:00
|
|
|
}
|
2015-09-08 10:37:34 +02:00
|
|
|
return $processed;
|
2014-03-14 11:05:11 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-03-31 00:04:08 +02:00
|
|
|
sub interpret {
|
|
|
|
my $self = shift;
|
2015-09-04 05:56:44 +02:00
|
|
|
my ($from, $nick, $user, $host, $depth, $command, $tonick, $referenced) = @_;
|
2015-03-31 00:04:08 +02:00
|
|
|
my ($keyword, $arguments) = ("", "");
|
|
|
|
my $text;
|
|
|
|
my $pbot = $self->{pbot};
|
|
|
|
|
2015-04-03 21:32:35 +02:00
|
|
|
$pbot->{logger}->log("=== Enter interpret_command: [" . (defined $from ? $from : "(undef)") . "][$nick!$user\@$host][$depth][$command]\n");
|
2015-03-31 00:04:08 +02:00
|
|
|
|
2015-04-03 21:32:35 +02:00
|
|
|
return "Too many levels of recursion, aborted." if(++$depth > $self->{pbot}->{registry}->get_value('interpreter', 'max_recursion'));
|
2015-03-31 00:04:08 +02:00
|
|
|
|
|
|
|
if(not defined $nick || not defined $user || not defined $host ||
|
|
|
|
not defined $command) {
|
|
|
|
$pbot->{logger}->log("Error 1, bad parameters to interpret_command\n");
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($command =~ /^tell\s+(.{1,20})\s+about\s+(.*?)\s+(.*)$/i)
|
|
|
|
{
|
|
|
|
($keyword, $arguments, $tonick) = ($2, $3, $1);
|
2016-09-25 09:03:37 +02:00
|
|
|
my $similar = $self->{pbot}->{nicklist}->is_present_similar($from, $tonick);
|
|
|
|
$tonick = $similar if $similar;
|
2015-03-31 00:04:08 +02:00
|
|
|
} elsif($command =~ /^tell\s+(.{1,20})\s+about\s+(.*)$/i) {
|
|
|
|
($keyword, $tonick) = ($2, $1);
|
2016-09-25 09:03:37 +02:00
|
|
|
my $similar = $self->{pbot}->{nicklist}->is_present_similar($from, $tonick);
|
|
|
|
$tonick = $similar if $similar;
|
2015-03-31 00:04:08 +02:00
|
|
|
} elsif($command =~ /^(.*?)\s+(.*)$/) {
|
|
|
|
($keyword, $arguments) = ($1, $2);
|
|
|
|
} else {
|
|
|
|
$keyword = $command;
|
|
|
|
}
|
|
|
|
|
2017-08-09 06:18:45 +02:00
|
|
|
$tonick = $nick if defined $tonick and $tonick eq 'me';
|
2017-08-02 06:34:57 +02:00
|
|
|
|
|
|
|
if ($keyword !~ /^(factadd|add|factfind|find|factshow|show|forget|factdel|factset|factchange|change|msg|tell)/) {
|
2015-03-31 00:04:08 +02:00
|
|
|
$keyword =~ s/(\w+)([?!.]+)$/$1/;
|
2017-08-02 06:34:57 +02:00
|
|
|
$arguments =~ s/(?<![\w\/\-\\])me\b/$nick/gi if defined $arguments && $depth <= 2;
|
|
|
|
$arguments =~ s/(?<![\w\/\-\\])my\b/${nick}'s/gi if defined $arguments && $depth <= 2;
|
|
|
|
$arguments =~ s/\\my\b/my/gi if defined $arguments && $depth <= 2;
|
|
|
|
$arguments =~ s/\\me\b/me/gi if defined $arguments && $depth <= 2;
|
2015-03-31 00:04:08 +02:00
|
|
|
}
|
|
|
|
|
2015-09-20 05:02:11 +02:00
|
|
|
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
|
|
|
|
|
|
|
|
if (defined $arguments && ($arguments =~ m/^(your|him|her|its|it|them|their)(self|selves)$/i || $arguments =~ m/^$botnick$/i)) {
|
2015-03-31 00:04:08 +02:00
|
|
|
my $delay = (rand 10) + 8;
|
|
|
|
my $message = {
|
|
|
|
nick => $nick, user => $user, host => $host, command => $command, checkflood => 1,
|
2015-09-20 05:02:11 +02:00
|
|
|
message => "$nick: Why would I want to do that to myself?"
|
2015-03-31 00:04:08 +02:00
|
|
|
};
|
|
|
|
$self->add_message_to_output_queue($from, $message, $delay);
|
2016-07-01 22:00:20 +02:00
|
|
|
$delay = duration($delay);
|
|
|
|
$self->{pbot}->{logger}->log("Final result ($delay delay) [$message->{message}]\n");
|
2015-03-31 00:04:08 +02:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not defined $keyword) {
|
|
|
|
$pbot->{logger}->log("Error 2, no keyword\n");
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2015-09-04 05:56:44 +02:00
|
|
|
return $self->SUPER::execute_all($from, $nick, $user, $host, $depth, $keyword, $arguments, $tonick, undef, $referenced);
|
2015-03-31 00:04:08 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 02:53:33 +01:00
|
|
|
sub truncate_result {
|
|
|
|
my ($self, $from, $nick, $text, $original_result, $result, $paste) = @_;
|
2014-05-17 22:08:19 +02:00
|
|
|
my $max_msg_len = $self->{pbot}->{registry}->get_value('irc', 'max_msg_len');
|
2014-03-15 02:53:33 +01:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
if(length $result > $max_msg_len) {
|
2014-03-15 02:53:33 +01:00
|
|
|
my $link;
|
|
|
|
if($paste) {
|
2015-05-16 02:53:13 +02:00
|
|
|
$link = $self->paste("[" . (defined $from ? $from : "stdin") . "] <$nick> $text\n\n$original_result");
|
2014-03-15 02:53:33 +01:00
|
|
|
} else {
|
|
|
|
$link = 'undef';
|
|
|
|
}
|
|
|
|
|
2015-05-07 06:15:25 +02:00
|
|
|
my $trunc = "... [truncated; ";
|
|
|
|
if ($link =~ m/^http/) {
|
|
|
|
$trunc .= "see $link for full text.]";
|
|
|
|
} else {
|
|
|
|
$trunc .= "$link]";
|
|
|
|
}
|
|
|
|
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("Message truncated -- pasted to $link\n") if $paste;
|
2014-03-15 02:53:33 +01:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
my $trunc_len = length $result < $max_msg_len ? length $result : $max_msg_len;
|
2014-03-15 02:53:33 +01:00
|
|
|
$result = substr($result, 0, $trunc_len);
|
|
|
|
substr($result, $trunc_len - length $trunc) = $trunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-03-14 11:05:11 +01:00
|
|
|
sub handle_result {
|
|
|
|
my ($self, $from, $nick, $user, $host, $text, $command, $result, $checkflood, $preserve_whitespace) = @_;
|
2012-07-22 21:22:30 +02:00
|
|
|
|
2015-03-29 01:50:43 +01:00
|
|
|
if (not defined $result or length $result == 0) {
|
2015-09-08 10:37:34 +02:00
|
|
|
return 0;
|
2014-03-14 11:05:11 +01:00
|
|
|
}
|
2012-07-22 21:22:30 +02:00
|
|
|
|
2014-03-14 11:05:11 +01:00
|
|
|
my $original_result = $result;
|
2010-03-17 07:36:54 +01:00
|
|
|
|
2015-03-29 01:50:43 +01:00
|
|
|
my $use_output_queue = 0;
|
|
|
|
|
|
|
|
if (defined $command) {
|
2014-03-14 11:05:11 +01:00
|
|
|
my ($cmd, $args) = split / /, $command, 2;
|
2015-04-04 00:33:19 +02:00
|
|
|
if (not $self->{pbot}->{commands}->exists($cmd)) {
|
2015-04-27 01:05:03 +02:00
|
|
|
my ($chan, $trigger) = $self->{pbot}->{factoids}->find_factoid($from, $cmd, $args, 1, 0, 1);
|
2015-04-04 00:33:19 +02:00
|
|
|
if(defined $trigger) {
|
|
|
|
if ($preserve_whitespace == 0) {
|
|
|
|
$preserve_whitespace = $self->{pbot}->{factoids}->{factoids}->hash->{$chan}->{$trigger}->{preserve_whitespace};
|
|
|
|
$preserve_whitespace = 0 if not defined $preserve_whitespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
$use_output_queue = $self->{pbot}->{factoids}->{factoids}->hash->{$chan}->{$trigger}->{use_output_queue};
|
|
|
|
$use_output_queue = 0 if not defined $use_output_queue;
|
2015-03-29 01:50:43 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2014-03-14 11:05:11 +01:00
|
|
|
}
|
2013-10-22 20:57:08 +02:00
|
|
|
|
2014-08-05 00:48:32 +02:00
|
|
|
my $preserve_newlines = $self->{pbot}->{registry}->get_value($from, 'preserve_newlines');
|
|
|
|
|
|
|
|
$result =~ s/[\n\r]/ /g unless $preserve_newlines;
|
2014-08-06 01:11:51 +02:00
|
|
|
$result =~ s/[ \t]+/ /g unless $preserve_whitespace;
|
2014-08-05 00:48:32 +02:00
|
|
|
|
2014-08-06 01:15:11 +02:00
|
|
|
my $max_lines = $self->{pbot}->{registry}->get_value($from, 'max_newlines');
|
|
|
|
$max_lines = 4 if not defined $max_lines;
|
2014-08-05 00:48:32 +02:00
|
|
|
my $lines = 0;
|
2014-08-06 01:15:11 +02:00
|
|
|
|
2014-08-11 09:32:24 +02:00
|
|
|
my $stripped_line;
|
|
|
|
foreach my $line (split /[\n\r]+/, $result) {
|
|
|
|
$stripped_line = $line;
|
|
|
|
$stripped_line =~ s/^\s+//;
|
|
|
|
$stripped_line =~ s/\s+$//;
|
|
|
|
next if not length $stripped_line;
|
|
|
|
|
2014-08-06 01:15:11 +02:00
|
|
|
if (++$lines >= $max_lines) {
|
2015-05-16 02:53:13 +02:00
|
|
|
my $link = $self->paste("[" . (defined $from ? $from : "stdin") . "] <$nick> $text\n\n$original_result");
|
2015-03-29 01:50:43 +01:00
|
|
|
if ($use_output_queue) {
|
|
|
|
my $message = {
|
2015-03-30 05:24:36 +02:00
|
|
|
nick => $nick, user => $user, host => $host, command => $command,
|
2015-03-29 01:50:43 +01:00
|
|
|
message => "And that's all I have to say about that. See $link for full text.",
|
|
|
|
checkflood => $checkflood
|
|
|
|
};
|
2015-03-30 05:24:36 +02:00
|
|
|
$self->add_message_to_output_queue($from, $message, 0);
|
2015-03-29 01:50:43 +01:00
|
|
|
} else {
|
|
|
|
$self->{pbot}->{conn}->privmsg($from, "And that's all I have to say about that. See $link for full text.");
|
|
|
|
}
|
2014-08-05 00:48:32 +02:00
|
|
|
last;
|
2014-03-14 11:05:11 +01:00
|
|
|
}
|
2014-08-05 00:48:32 +02:00
|
|
|
|
|
|
|
if ($preserve_newlines) {
|
|
|
|
$line = $self->truncate_result($from, $nick, $text, $line, $line, 1);
|
|
|
|
} else {
|
|
|
|
$line = $self->truncate_result($from, $nick, $text, $original_result, $line, 1);
|
2014-03-14 11:05:11 +01:00
|
|
|
}
|
2014-08-05 00:48:32 +02:00
|
|
|
|
2015-03-29 01:50:43 +01:00
|
|
|
if ($use_output_queue) {
|
2015-04-14 00:41:11 +02:00
|
|
|
my $delay = (rand 5) + 5; # initial delay for reading/processing user's message
|
2015-03-30 05:24:36 +02:00
|
|
|
$delay += (length $line) / 7; # additional delay of 7 characters per second typing speed
|
2015-03-29 01:50:43 +01:00
|
|
|
my $message = {
|
2015-03-30 05:24:36 +02:00
|
|
|
nick => $nick, user => $user, host => $host, command => $command,
|
|
|
|
message => $line, checkflood => $checkflood
|
2015-03-29 01:50:43 +01:00
|
|
|
};
|
2015-03-30 05:24:36 +02:00
|
|
|
$self->add_message_to_output_queue($from, $message, $delay);
|
2015-03-31 00:04:08 +02:00
|
|
|
$delay = duration($delay);
|
|
|
|
$self->{pbot}->{logger}->log("Final result ($delay delay) [$line]\n");
|
2014-03-14 11:05:11 +01:00
|
|
|
} else {
|
2015-03-29 01:50:43 +01:00
|
|
|
$self->output_result($from, $nick, $user, $host, $command, $line, $checkflood);
|
2015-03-31 00:04:08 +02:00
|
|
|
$self->{pbot}->{logger}->log("Final result: [$line]\n");
|
2010-03-22 08:33:44 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
2015-03-29 01:50:43 +01:00
|
|
|
$self->{pbot}->{logger}->log("---------------------------------------------\n");
|
2015-09-08 10:37:34 +02:00
|
|
|
return 1;
|
2015-03-29 01:50:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub output_result {
|
|
|
|
my ($self, $from, $nick, $user, $host, $command, $line, $checkflood) = @_;
|
2015-04-19 10:55:52 +02:00
|
|
|
my ($pbot, $botnick) = ($self->{pbot}, $self->{pbot}->{registry}->get_value('irc', 'botnick'));
|
2015-03-29 01:50:43 +01:00
|
|
|
|
|
|
|
if($line =~ s/^\/say\s+//i) {
|
2015-04-19 10:55:52 +02:00
|
|
|
$pbot->{conn}->privmsg($from, $line) if defined $from && $from !~ /\Q$botnick\E/i;
|
|
|
|
$pbot->{antiflood}->check_flood($from, $botnick, $pbot->{registry}->get_value('irc', 'username'), 'localhost', $line, 0, 0, 0) if $checkflood;
|
2015-03-29 01:50:43 +01:00
|
|
|
} elsif($line =~ s/^\/me\s+//i) {
|
2015-04-19 10:55:52 +02:00
|
|
|
$pbot->{conn}->me($from, $line) if defined $from && $from !~ /\Q$botnick\E/i;
|
|
|
|
$pbot->{antiflood}->check_flood($from, $botnick, $pbot->{registry}->get_value('irc', 'username'), 'localhost', '/me ' . $line, 0, 0, 0) if $checkflood;
|
2015-03-29 01:50:43 +01:00
|
|
|
} elsif($line =~ s/^\/msg\s+([^\s]+)\s+//i) {
|
|
|
|
my $to = $1;
|
|
|
|
if($to =~ /,/) {
|
|
|
|
$pbot->{logger}->log("[HACK] Possible HACK ATTEMPT /msg multiple users: [$nick!$user\@$host] [$command] [$line]\n");
|
|
|
|
}
|
|
|
|
elsif($to =~ /.*serv$/i) {
|
|
|
|
$pbot->{logger}->log("[HACK] Possible HACK ATTEMPT /msg *serv: [$nick!$user\@$host] [$command] [$line]\n");
|
|
|
|
}
|
|
|
|
elsif($line =~ s/^\/me\s+//i) {
|
2015-04-19 10:55:52 +02:00
|
|
|
$pbot->{conn}->me($to, $line) if $to !~ /\Q$botnick\E/i;
|
|
|
|
$pbot->{antiflood}->check_flood($to, $botnick, $pbot->{registry}->get_value('irc', 'username'), 'localhost', '/me ' . $line, 0, 0, 0) if $checkflood;
|
2015-03-29 01:50:43 +01:00
|
|
|
} else {
|
|
|
|
$line =~ s/^\/say\s+//i;
|
2015-04-19 10:55:52 +02:00
|
|
|
$pbot->{conn}->privmsg($to, $line) if $to !~ /\Q$botnick\E/i;
|
|
|
|
$pbot->{antiflood}->check_flood($to, $botnick, $pbot->{registry}->get_value('irc', 'username'), 'localhost', $line, 0, 0, 0) if $checkflood;
|
2015-03-29 01:50:43 +01:00
|
|
|
}
|
2016-11-17 04:07:01 +01:00
|
|
|
} elsif($line =~ s/^\/$self->{pbot}->{secretstuff}kick\s+//) {
|
|
|
|
$pbot->{antiflood}->check_flood($from, $botnick, $pbot->{registry}->get_value('irc', 'username'), 'localhost', '/kick ' . $line, 0, 0, 0) if $checkflood;
|
|
|
|
my ($victim, $reason) = split / /, $line, 2;
|
|
|
|
|
|
|
|
if (not defined $reason) {
|
|
|
|
if (open my $fh, '<', $self->{pbot}->{registry}->get_value('general', 'module_dir') . '/insults.txt') {
|
|
|
|
my @insults = <$fh>;
|
|
|
|
close $fh;
|
|
|
|
$reason = $insults[rand @insults];
|
|
|
|
chomp $reason;
|
|
|
|
} else {
|
|
|
|
$reason = 'Bye!';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{pbot}->{chanops}->can_gain_ops($from)) {
|
|
|
|
$self->{pbot}->{chanops}->add_op_command($from, "kick $from $victim $reason");
|
|
|
|
$self->{pbot}->{chanops}->gain_ops($from);
|
|
|
|
} else {
|
|
|
|
$pbot->{conn}->privmsg($from, "$victim: $reason") if defined $from && $from !~ /\Q$botnick\E/i;
|
|
|
|
}
|
2015-03-29 01:50:43 +01:00
|
|
|
} else {
|
2015-04-19 10:55:52 +02:00
|
|
|
$pbot->{conn}->privmsg($from, $line) if defined $from && $from !~ /\Q$botnick\E/i;
|
|
|
|
$pbot->{antiflood}->check_flood($from, $botnick, $pbot->{registry}->get_value('irc', 'username'), 'localhost', $line, 0, 0, 0) if $checkflood;
|
2015-03-29 01:50:43 +01:00
|
|
|
}
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-29 01:50:43 +01:00
|
|
|
sub add_message_to_output_queue {
|
2015-03-30 05:24:36 +02:00
|
|
|
my ($self, $channel, $message, $delay) = @_;
|
|
|
|
|
|
|
|
if (exists $self->{output_queue}->{$channel}) {
|
|
|
|
my $last_when = $self->{output_queue}->{$channel}->[-1]->{when};
|
|
|
|
$message->{when} = $last_when + $delay;
|
|
|
|
} else {
|
|
|
|
$message->{when} = gettimeofday + $delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @{$self->{output_queue}->{$channel}}, $message;
|
2015-03-29 01:50:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub process_output_queue {
|
|
|
|
my $self = shift;
|
|
|
|
|
2015-03-30 05:24:36 +02:00
|
|
|
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}) {
|
|
|
|
$self->output_result($channel, $message->{nick}, $message->{user}, $message->{host}, $message->{command}, $message->{message}, $message->{checkflood});
|
|
|
|
splice @{$self->{output_queue}->{$channel}}, $i--, 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not @{$self->{output_queue}->{$channel}}) {
|
|
|
|
delete $self->{output_queue}->{$channel};
|
2015-03-29 01:50:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:00:20 +02:00
|
|
|
sub add_to_command_queue {
|
|
|
|
my ($self, $channel, $command, $delay) = @_;
|
|
|
|
|
2016-07-01 22:29:26 +02:00
|
|
|
$command->{when} = gettimeofday + $delay;
|
2016-07-01 22:00:20 +02:00
|
|
|
|
|
|
|
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 => 'localhost',
|
|
|
|
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}) {
|
2017-08-13 08:42:56 +02:00
|
|
|
$self->handle_result($channel, $command->{nick}, $command->{user}, $command->{host}, $command->{command}, $command->{command}, $self->interpret($channel, $command->{nick}, $command->{user}, $command->{host}, 0, $command->{command}), 0, 0);
|
2016-07-01 22:00:20 +02:00
|
|
|
splice @{$self->{command_queue}->{$channel}}, $i--, 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not @{$self->{command_queue}->{$channel}}) {
|
|
|
|
delete $self->{command_queue}->{$channel};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 02:53:13 +02:00
|
|
|
sub paste {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $rate_limit = $self->{pbot}->{registry}->get_value('general', 'paste_ratelimit');
|
|
|
|
my $now = gettimeofday;
|
|
|
|
|
|
|
|
if ($now - $self->{last_paste} < $rate_limit) {
|
|
|
|
return "paste rate-limited, try again in " . ($rate_limit - int($now - $self->{last_paste})) . " seconds";
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->{last_paste} = $now;
|
|
|
|
|
|
|
|
my $text = join(' ', @_);
|
|
|
|
$text =~ s/(.{120})\s/$1\n/g;
|
|
|
|
|
|
|
|
my $result = $self->paste_sprunge($text);
|
|
|
|
|
|
|
|
if ($result =~ m/error pasting/) {
|
|
|
|
$result = $self->paste_codepad($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-03-31 00:04:08 +02:00
|
|
|
sub paste_codepad {
|
2015-05-16 02:53:13 +02:00
|
|
|
my $self = shift;
|
2015-03-31 00:04:08 +02:00
|
|
|
my $text = join(' ', @_);
|
|
|
|
|
|
|
|
$text =~ s/(.{120})\s/$1\n/g;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new();
|
|
|
|
$ua->agent("Mozilla/5.0");
|
|
|
|
push @{ $ua->requests_redirectable }, 'POST';
|
|
|
|
|
|
|
|
my %post = ( 'lang' => 'Plain Text', 'code' => $text, 'private' => 'True', 'submit' => 'Submit' );
|
|
|
|
my $response = $ua->post("http://codepad.org", \%post);
|
|
|
|
|
|
|
|
if(not $response->is_success) {
|
2015-05-16 02:53:13 +02:00
|
|
|
return "error pasting: " . $response->status_line;
|
2015-03-31 00:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response->request->uri;
|
|
|
|
}
|
|
|
|
|
2014-05-19 23:34:24 +02:00
|
|
|
sub paste_sprunge {
|
2015-05-07 06:15:25 +02:00
|
|
|
my $self = shift;
|
2014-05-19 23:34:24 +02:00
|
|
|
my $text = join(' ', @_);
|
|
|
|
|
|
|
|
$text =~ s/(.{120})\s/$1\n/g;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new();
|
|
|
|
$ua->agent("Mozilla/5.0");
|
|
|
|
$ua->requests_redirectable([ ]);
|
|
|
|
|
|
|
|
my %post = ( 'sprunge' => $text, 'submit' => 'Submit' );
|
|
|
|
my $response = $ua->post("http://sprunge.us", \%post);
|
|
|
|
|
|
|
|
if(not $response->is_success) {
|
2015-05-07 06:22:22 +02:00
|
|
|
return "error pasting: " . $response->status_line;
|
2014-05-19 23:34:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my $result = $response->content;
|
|
|
|
$result =~ s/^\s+//;
|
|
|
|
$result =~ s/\s+$//;
|
|
|
|
return $result;
|
2010-03-17 07:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|