3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-25 04:02:37 +01:00
pbot/PBot/WebPaste.pm

121 lines
2.6 KiB
Perl
Raw Normal View History

# File: WebPaste.pm
# Author: pragma_
#
# Purpose: Pastes text to a cycling list of 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-15 23:38:32 +01:00
use parent 'PBot::Class';
use warnings; use strict;
2019-07-11 03:40:53 +02:00
use feature 'unicode_strings';
use utf8;
2019-07-11 03:40:53 +02:00
use Time::HiRes qw/gettimeofday/;
use Time::Duration;
use LWP::UserAgent::Paranoid;
2019-07-01 05:47:49 +02:00
use Encode;
sub initialize {
2020-02-15 23:38:32 +01:00
my ($self, %conf) = @_;
# There used to be many more paste sites in this list but one by one
# many have died off. :-(
2020-02-15 23:38:32 +01:00
$self->{paste_sites} = [
sub { $self->paste_0x0st(@_) },
2021-02-07 22:51:58 +01:00
# sub { $self->paste_ixio(@_) }, # removed due to being too slow (temporarily hopefully)
2020-02-15 23:38:32 +01:00
];
2020-02-15 23:38:32 +01:00
$self->{current_site} = 0;
}
sub get_paste_site {
2020-02-15 23:38:32 +01:00
my ($self) = @_;
# get the next paste site's subroutine reference
2020-02-15 23:38:32 +01:00
my $subref = $self->{paste_sites}->[$self->{current_site}];
# rotate current_site
if (++$self->{current_site} >= @{$self->{paste_sites}}) {
$self->{current_site} = 0;
}
2020-02-15 23:38:32 +01:00
return $subref;
}
sub paste {
2020-02-15 23:38:32 +01:00
my ($self, $text, %opts) = @_;
2020-02-15 23:38:32 +01:00
my %default_opts = (
no_split => 0,
);
2020-02-15 23:38:32 +01:00
%opts = (%default_opts, %opts);
# word-wrap text unless no_split is set
2020-02-15 23:38:32 +01:00
$text =~ s/(.{120})\s/$1\n/g unless $opts{no_split};
# encode paste to utf8
$text = encode('UTF-8', $text);
my $response;
2020-02-15 23:38:32 +01:00
for (my $tries = 3; $tries > 0; $tries--) {
# get the next paste site
2020-02-15 23:38:32 +01:00
my $paste_site = $self->get_paste_site;
# attempt to paste text
$response = $paste_site->($text);
# exit loop if paste succeeded
last if $response->is_success;
}
# all tries failed
if (not $response->is_success) {
return "error pasting: " . $response->status_line;
2020-02-15 23:38:32 +01:00
}
# success, return URL
my $result = $response->decoded_content;
2021-02-07 22:51:58 +01:00
$result =~ s/^\s+|\s+$//g;
alarm 1; # LWP::UserAgent::Paranoid kills alarm
2020-02-15 23:38:32 +01:00
return $result;
}
sub paste_0x0st {
2020-02-15 23:38:32 +01:00
my ($self, $text) = @_;
2020-02-15 23:38:32 +01:00
my $ua = LWP::UserAgent::Paranoid->new(request_timeout => 10);
2020-02-15 23:38:32 +01:00
push @{$ua->requests_redirectable}, 'POST';
return $ua->post(
"https://0x0.st",
[ file => [ undef, "file", Content => $text ] ],
Content_Type => 'form-data'
);
2021-02-07 22:51:58 +01:00
}
sub paste_ixio {
my ($self, $text) = @_;
2021-02-07 22:51:58 +01:00
my $ua = LWP::UserAgent::Paranoid->new(request_timeout => 10);
2021-02-07 22:51:58 +01:00
push @{$ua->requests_redirectable}, 'POST';
my %post = ('f:1' => $text);
return $ua->post("http://ix.io", \%post);
}
1;