From f998f974c06773997730a28848aee1a3a25d5d38 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 15 Mar 2014 01:53:33 +0000 Subject: [PATCH] Truncate module launcher piped result --- PBot/FactoidModuleLauncher.pm | 2 ++ PBot/Interpreter.pm | 36 +++++++++++++++++++++++------------ PBot/VERSION.pm | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/PBot/FactoidModuleLauncher.pm b/PBot/FactoidModuleLauncher.pm index 7e79764d..0610b2e8 100644 --- a/PBot/FactoidModuleLauncher.pm +++ b/PBot/FactoidModuleLauncher.pm @@ -177,6 +177,8 @@ sub execute_module { sub module_pipe_reader { my ($self, $buf) = @_; my ($channel, $text) = split / /, $buf, 2; + return if not defined $text or not length $text; + $text = $self->{pbot}->interpreter->truncate_result($channel, $self->{pbot}->{botnick}, 'undef', $text, $text, 0); $self->{pbot}->antiflood->check_flood($channel, $self->{pbot}->{botnick}, $self->{pbot}->{username}, 'localhost', $text, 0, 0, 0); } diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index c88c4b99..d18e9edf 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -142,6 +142,28 @@ sub process_line { } } +sub truncate_result { + my ($self, $from, $nick, $text, $original_result, $result, $paste) = @_; + + if(length $result > $self->{pbot}->max_msg_len) { + my $link; + if($paste) { + $link = paste_sprunge("[" . (defined $from ? $from : "stdin") . "] <$nick> $text\n\n$original_result"); + } else { + $link = 'undef'; + } + + my $trunc = "... [truncated; see $link for full text.]"; + $self->{pbot}->logger->log("Message truncated -- pasted to $link\n") if $paste; + + my $trunc_len = length $result < $self->{pbot}->max_msg_len ? length $result : $self->{pbot}->max_msg_len; + $result = substr($result, 0, $trunc_len); + substr($result, $trunc_len - length $trunc) = $trunc; + } + + return $result; +} + sub handle_result { my ($self, $from, $nick, $user, $host, $text, $command, $result, $checkflood, $preserve_whitespace) = @_; my ($pbot, $mynick) = ($self->{pbot}, $self->{pbot}->{botnick}); @@ -151,7 +173,7 @@ sub handle_result { } my $original_result = $result; - $result =~ s/[\n\r]+/ /g; + $result =~ s/[\n\r]/ /g; if($preserve_whitespace == 0 && defined $command) { my ($cmd, $args) = split / /, $command, 2; @@ -164,17 +186,7 @@ sub handle_result { } $result =~ s/\s+/ /g unless $preserve_whitespace; - - if(length $result > $pbot->max_msg_len) { - my $link = paste_sprunge("[" . (defined $from ? $from : "stdin") . "] <$nick> $text\n\n$original_result"); - my $trunc = "... [truncated; see $link for full text.]"; - $pbot->logger->log("Message truncated -- pasted to $link\n"); - - my $trunc_len = length $result < $pbot->max_msg_len ? length $result : $pbot->max_msg_len; - $result = substr($result, 0, $trunc_len); - substr($result, $trunc_len - length $trunc) = $trunc; - } - + $result = $self->truncate_result($from, $nick, $text, $original_result, $result, 1); $pbot->logger->log("Final result: [$result]\n"); if($result =~ s/^\/say\s+//i) { diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 49577e17..fcd3d57a 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 525, + BUILD_REVISION => 526, BUILD_DATE => "2014-03-14", };