2017-12-06 06:05:44 +01:00
|
|
|
# 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;
|
2020-02-08 20:04:13 +01:00
|
|
|
use parent 'PBot::Class';
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2020-02-08 20:04:13 +01:00
|
|
|
use warnings; use strict;
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2017-12-06 06:05:44 +01:00
|
|
|
use Time::HiRes qw/gettimeofday/;
|
|
|
|
use Time::Duration;
|
2020-02-03 18:46:44 +01:00
|
|
|
use LWP::UserAgent::Paranoid;
|
2019-07-01 05:47:49 +02:00
|
|
|
use Encode;
|
2017-12-06 06:05:44 +01:00
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2018-02-28 20:14:19 +01:00
|
|
|
$self->{paste_sites} = [
|
|
|
|
sub { $self->paste_ixio(@_) },
|
|
|
|
];
|
2017-12-06 06:05:44 +01:00
|
|
|
|
|
|
|
$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 {
|
2019-08-07 03:23:13 +02:00
|
|
|
my ($self, $text, %opts) = @_;
|
|
|
|
my %default_opts = (
|
|
|
|
no_split => 0,
|
|
|
|
);
|
|
|
|
%opts = (%default_opts, %opts);
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2019-08-07 03:23:13 +02:00
|
|
|
$text =~ s/(.{120})\s/$1\n/g unless $opts{no_split};
|
2019-07-01 05:47:49 +02:00
|
|
|
$text = encode('UTF-8', $text);
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2019-07-01 05:47:49 +02:00
|
|
|
my $result;
|
2020-02-03 18:46:44 +01:00
|
|
|
for (my $tries = 3; $tries > 0; $tries--) {
|
2017-12-06 06:05:44 +01:00
|
|
|
my $paste_site = $self->get_paste_site;
|
|
|
|
$result = $paste_site->($text);
|
2020-02-08 20:04:13 +01:00
|
|
|
last if $result !~ m/error pasting/;
|
2017-12-06 06:05:44 +01:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub paste_ixio {
|
2019-08-07 03:23:13 +02:00
|
|
|
my ($self, $text) = @_;
|
2020-02-03 21:27:45 +01:00
|
|
|
my $ua = LWP::UserAgent::Paranoid->new(request_timeout => 10);
|
2017-12-06 06:05:44 +01:00
|
|
|
$ua->agent("Mozilla/5.0");
|
|
|
|
push @{ $ua->requests_redirectable }, 'POST';
|
|
|
|
my %post = ('f:1' => $text);
|
|
|
|
my $response = $ua->post("http://ix.io", \%post);
|
2020-02-04 12:13:44 +01:00
|
|
|
alarm 1; # LWP::UserAgent::Paranoid kills alarm
|
2020-02-03 21:27:45 +01:00
|
|
|
return "error pasting: " . $response->status_line if not $response->is_success;
|
2017-12-06 06:05:44 +01:00
|
|
|
my $result = $response->content;
|
|
|
|
$result =~ s/^\s+//;
|
|
|
|
$result =~ s/\s+$//;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|