1
0
Fork 0
mirror of https://github.com/perlbot/perlbuut synced 2025-06-07 17:25:41 -04:00
perlbuut/plugins/more.pm

90 lines
2.2 KiB
Perl

package Bot::BB3::Plugin::More;
use strict;
use LWP::UserAgent;
use JSON::MaybeXS;
sub new {
my( $class ) = @_;
my $self = bless {}, $class;
$self->{name} = 'more';
$self->{opts} = {
command => 1,
post_process => 1,
};
return $self;
}
sub initialize {
my( $self, $pm, $cache ) = @_;
$self->{cache} = $cache;
}
sub command {
my( $self, $said, $pm ) = @_;
my $text = $self->{cache}->get( "pager_$said->{name}" );
$self->{cache}->remove( "pager_$said->{name}" );
if( $text ) { return( 'handled', "...$text" ); }
else { return( 'handled', "Sorry, no more output" ); }
}
sub post_process {
my( $self, $said, $pm, $output_ref ) = @_;
return if $said->{channel} =~ /^\*/;
# Magic numbers are awesome.
# the usual max length for an irc message is around 425?
# Something like that.
# The actual max is usually 512 but you need room for nicks and command types.
if( length $$output_ref > 400 ) {
# Sanity checking, let's not store novels. yes lets
# if( length $$output_ref > 1_000 ) {
# my $new_out = $$output_ref = substr( $$output_ref, 0, 1_000 );
# $$output_ref = $new_out;
#
# warn "Sanity checking, new length: ", length $$output_ref;
# }
my $ua = LWP::UserAgent->new();
my $text = $$output_ref;
$text =~ s/\x{2424}/\n/g;
my $res = $ua->post("https://perl.bot/api/v1/paste", {
paste => $text,
description => 'More text for '.$said->{body},
username => $said->{nick},
language => 'text'
});
if ($res->is_success()) {
my $content = $res->decoded_content;
my $data = decode_json $content;
my $new_text = substr( $$output_ref, 0, 350, '' );
$$output_ref = $new_text;
$$output_ref .= "... [Output truncated. ".$data->{url}." ]";
} else {
my $new_text = substr( $$output_ref, 0, 350, '' );
$self->{cache}->set( "pager_$said->{name}", $$output_ref, "10 minutes" ); #Remainder
$$output_ref = $new_text;
$$output_ref .= "... [Output truncated. Use `more` to read more]";
}
}
}
no warnings 'void';
"Bot::BB3::Plugin::More";
__DATA__
More acts as a pager. It automatically truncates output that is too long and saves it in a buffer based on your name. Use the command `more` to access the remainder of the text.