From e2db94c354d298d12f51fe47d8240bb440673c52 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 5 Dec 2017 21:05:44 -0800 Subject: [PATCH] Add WebPaste.pm for rotating paste sites --- PBot/Interpreter.pm | 100 +--------------------- PBot/PBot.pm | 2 + PBot/WebPaste.pm | 197 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 98 deletions(-) create mode 100644 PBot/WebPaste.pm diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index 5bb99a02..6d8e0e4a 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -16,7 +16,6 @@ use base 'PBot::Registerable'; use Time::HiRes qw/gettimeofday/; use Time::Duration; -use LWP::UserAgent; use Carp (); use PBot::Utils::ValidateString; @@ -42,12 +41,10 @@ sub initialize { $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} // '.*'); $self->{pbot}->{registry}->add_default('array', 'general', 'compile_blocks_ignore_channels', $conf{compile_blocks_ignore_channels} // 'none'); - $self->{pbot}->{registry}->add_default('text', 'general', 'paste_ratelimit', $conf{paste_ratelimit} // 60); $self->{pbot}->{registry}->add_default('text', 'interpreter', 'max_recursion', 10); $self->{output_queue} = {}; $self->{command_queue} = {}; - $self->{last_paste} = 0; $self->{pbot}->{timer}->register(sub { $self->process_output_queue }, 1); $self->{pbot}->{timer}->register(sub { $self->process_command_queue }, 1); @@ -335,7 +332,7 @@ sub truncate_result { my $link; if($paste) { $original_result = substr $original_result, 0, 8000; - $link = $self->paste("[" . (defined $from ? $from : "stdin") . "] <$nick> $text\n\n$original_result"); + $link = $self->{pbot}->{webpaste}->paste("[" . (defined $from ? $from : "stdin") . "] <$nick> $text\n\n$original_result"); } else { $link = 'undef'; } @@ -450,7 +447,7 @@ sub handle_result { next if not length $stripped_line; if (++$lines >= $max_lines) { - my $link = $self->paste("[" . (defined $stuff->{from} ? $stuff->{from} : "stdin") . "] <$stuff->{nick}> $stuff->{text}\n\n$original_result"); + my $link = $self->{pbot}->{webpaste}->paste("[" . (defined $stuff->{from} ? $stuff->{from} : "stdin") . "] <$stuff->{nick}> $stuff->{text}\n\n$original_result"); if ($use_output_queue) { my $message = { nick => $stuff->{nick}, user => $stuff->{user}, host => $stuff->{host}, command => $stuff->{command}, @@ -660,97 +657,4 @@ sub process_command_queue { } } -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_ixio($text); - - if ($result =~ m/error pasting/) { - $result = $self->paste_codepad($text); - } - - return $result; -} - -sub paste_ixio { - my $self = shift; - 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'; - $ua->timeout(10); - - my %post = ('f:1' => $text); - my $response = $ua->post("http://ix.io", \%post); - - if(not $response->is_success) { - return "error pasting: " . $response->status_line; - } - - my $result = $response->content; - $result =~ s/^\s+//; - $result =~ s/\s+$//; - return $result; -} - -sub paste_codepad { - my $self = shift; - 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'; - $ua->timeout(10); - - my %post = ( 'lang' => 'Plain Text', 'code' => $text, 'private' => 'True', 'submit' => 'Submit' ); - my $response = $ua->post("http://codepad.org", \%post); - - if(not $response->is_success) { - return "error pasting: " . $response->status_line; - } - - return $response->request->uri; -} - -sub paste_sprunge { - my $self = shift; - my $text = join(' ', @_); - - $text =~ s/(.{120})\s/$1\n/g; - - my $ua = LWP::UserAgent->new(); - $ua->agent("Mozilla/5.0"); - $ua->requests_redirectable([ ]); - $ua->timeout(10); - - my %post = ( 'sprunge' => $text, 'submit' => 'Submit' ); - my $response = $ua->post("http://sprunge.us", \%post); - - if(not $response->is_success) { - return "error pasting: " . $response->status_line; - } - - my $result = $response->content; - $result =~ s/^\s+//; - $result =~ s/\s+$//; - return $result; -} - 1; diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 1a9d988b..7e591afe 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -52,6 +52,7 @@ use PBot::BlackList; use PBot::Timer; use PBot::Refresher; use PBot::Plugins; +use PBot::WebPaste; sub new { if(ref($_[1]) eq 'HASH') { @@ -123,6 +124,7 @@ sub initialize { $self->{channels} = PBot::Channels->new(pbot => $self, filename => $conf{channels_file}, %conf); $self->{chanops} = PBot::ChanOps->new(pbot => $self, %conf); $self->{nicklist} = PBot::NickList->new(pbot => $self, %conf); + $self->{webpaste} = PBot::WebPaste->new(pbot => $self, %conf); $self->{interpreter} = PBot::Interpreter->new(pbot => $self, %conf); $self->{interpreter}->register(sub { return $self->{commands}->interpreter(@_); }); diff --git a/PBot/WebPaste.pm b/PBot/WebPaste.pm new file mode 100644 index 00000000..ab6186ec --- /dev/null +++ b/PBot/WebPaste.pm @@ -0,0 +1,197 @@ +# File: WebPaste.pm +# Author: pragma_ +# +# Purpose: Pastes text to web paste sites. + +# 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/. + +package PBot::WebPaste; + +use warnings; +use strict; + +use Time::HiRes qw/gettimeofday/; +use Time::Duration; +use LWP::UserAgent; +use Carp (); + +sub new { + if(ref($_[1]) eq 'HASH') { + Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference"); + } + + my ($class, %conf) = @_; + my $self = bless {}, $class; + $self->initialize(%conf); + return $self; +} + +sub initialize { + my ($self, %conf) = @_; + + $self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__); + + $self->{paste_sites} = [ sub { $self->paste_sprunge(@_) }, + sub { $self->paste_codepad(@_) }, + sub { $self->paste_ixio(@_) }, + sub { $self->paste_ptpb(@_) }, + sub { $self->paste_gehidore(@_) }, + ]; + + $self->{current_site} = 0; +} + +sub get_paste_site { + my ($self) = @_; + + my $subref = $self->{paste_sites}->[$self->{current_site}]; + + if (++$self->{current_site} >= @{$self->{paste_sites}}) { + $self->{current_site} = 0; + } + + return $subref; +} + +sub paste { + my ($self, $text) = @_; + + $text =~ s/(.{120})\s/$1\n/g; + my $result; + + for (my $tries = 5; $tries > 0; $tries--) { + $self->{pbot}->{logger}->log("try $tries...\n"); + my $paste_site = $self->get_paste_site; + + $result = $paste_site->($text); + + if ($result !~ m/error pasting/) { + last; + } + } + + return $result; +} + +sub paste_ixio { + my $self = shift; + my $text = join(' ', @_); + + print "paste ixio?\n"; + + $text =~ s/(.{120})\s/$1\n/g; + + my $ua = LWP::UserAgent->new(); + $ua->agent("Mozilla/5.0"); + push @{ $ua->requests_redirectable }, 'POST'; + $ua->timeout(10); + + my %post = ('f:1' => $text); + my $response = $ua->post("http://ix.io", \%post); + + if(not $response->is_success) { + return "error pasting: " . $response->status_line; + } + + my $result = $response->content; + $result =~ s/^\s+//; + $result =~ s/\s+$//; + return $result; +} + +sub paste_codepad { + my $self = shift; + 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'; + $ua->timeout(10); + + my %post = ( 'lang' => 'Plain Text', 'code' => $text, 'private' => 'True', 'submit' => 'Submit' ); + my $response = $ua->post("http://codepad.org", \%post); + + if(not $response->is_success) { + return "error pasting: " . $response->status_line; + } + + return $response->request->uri; +} + +sub paste_sprunge { + my $self = shift; + my $text = join(' ', @_); + + $text =~ s/(.{120})\s/$1\n/g; + + my $ua = LWP::UserAgent->new(); + $ua->agent("Mozilla/5.0"); + $ua->requests_redirectable([ ]); + $ua->timeout(10); + + my %post = ( 'sprunge' => $text, 'submit' => 'Submit' ); + my $response = $ua->post("http://sprunge.us", \%post); + + if(not $response->is_success) { + return "error pasting: " . $response->status_line; + } + + my $result = $response->content; + $result =~ s/^\s+//; + $result =~ s/\s+$//; + return $result; +} + +sub paste_ptpb { + my $self = shift; + my $text = join(' ', @_); + + $text =~ s/(.{120})\s/$1\n/g; + + my $ua = LWP::UserAgent->new(); + $ua->agent("Mozilla/5.0"); + $ua->requests_redirectable([ ]); + $ua->timeout(10); + + my %post = ( 'c' => $text, 'submit' => 'Submit' ); + my $response = $ua->post("https://ptpb.pw/?u=1", \%post); + + if(not $response->is_success) { + return "error pasting: " . $response->status_line; + } + + my $result = $response->content; + $result =~ s/^\s+//; + $result =~ s/\s+$//; + return $result; +} + +sub paste_gehidore { + my $self = shift; + my $text = join(' ', @_); + + $text =~ s/(.{120})\s/$1\n/g; + + my $ua = LWP::UserAgent->new(); + $ua->agent("Mozilla/5.0"); + $ua->requests_redirectable([ ]); + $ua->timeout(10); + + my %post = ( 'c' => $text, 'submit' => 'Submit' ); + my $response = $ua->post("https://pb.gehidore.net/?u=1", \%post); + + if(not $response->is_success) { + return "error pasting: " . $response->status_line; + } + + my $result = $response->content; + $result =~ s/^\s+//; + $result =~ s/\s+$//; + return $result; +} + +1;