2017-12-06 06:05:44 +01:00
|
|
|
# File: WebPaste.pm
|
|
|
|
#
|
2021-06-05 22:20:03 +02:00
|
|
|
# Purpose: Pastes text to a cycling list of web paste sites.
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2017-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::WebPaste;
|
|
|
|
use parent 'PBot::Core::Class';
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
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
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub initialize($self, %conf) {
|
2021-06-05 22:20:03 +02:00
|
|
|
# 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} = [
|
2024-04-08 05:55:04 +02:00
|
|
|
sub { $self->paste_0x0st(@_) },
|
2024-09-19 22:56:23 +02:00
|
|
|
# sub { $self->paste_sprunge(@_) }, # dead? RIP
|
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
|
|
|
];
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{current_site} = 0;
|
2017-12-06 06:05:44 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub get_paste_site($self) {
|
2021-06-07 04:12:14 +02:00
|
|
|
# get the next paste site's subroutine reference
|
2020-02-15 23:38:32 +01:00
|
|
|
my $subref = $self->{paste_sites}->[$self->{current_site}];
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# rotate current_site
|
2021-06-05 22:20:03 +02:00
|
|
|
if (++$self->{current_site} >= @{$self->{paste_sites}}) {
|
|
|
|
$self->{current_site} = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $subref;
|
2017-12-06 06:05:44 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub paste($self, $text, %opts) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my %default_opts = (
|
|
|
|
no_split => 0,
|
|
|
|
);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
%opts = (%default_opts, %opts);
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# word-wrap text unless no_split is set
|
2022-08-08 04:05:43 +02:00
|
|
|
$text =~ s/(.{150})\s/$1\n/g unless $opts{no_split};
|
2017-12-06 06:05:44 +01:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# encode paste to utf8
|
|
|
|
$text = encode('UTF-8', $text);
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
my $response;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
for (my $tries = 3; $tries > 0; $tries--) {
|
2021-06-07 04:12:14 +02:00
|
|
|
# get the next paste site
|
2020-02-15 23:38:32 +01:00
|
|
|
my $paste_site = $self->get_paste_site;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# attempt to paste text
|
2021-06-05 22:20:03 +02:00
|
|
|
$response = $paste_site->($text);
|
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# exit loop if paste succeeded
|
2021-06-05 22:20:03 +02:00
|
|
|
last if $response->is_success;
|
|
|
|
}
|
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# all tries failed
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not $response->is_success) {
|
|
|
|
return "error pasting: " . $response->status_line;
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-06-07 04:12:14 +02:00
|
|
|
# success, return URL
|
2021-06-05 22:20:03 +02:00
|
|
|
my $result = $response->decoded_content;
|
|
|
|
|
2021-02-07 22:51:58 +01:00
|
|
|
$result =~ s/^\s+|\s+$//g;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
2017-12-06 06:05:44 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub paste_0x0st($self, $text) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $ua = LWP::UserAgent::Paranoid->new(request_timeout => 10);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
push @{$ua->requests_redirectable}, 'POST';
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
return $ua->post(
|
2021-02-07 22:33:24 +01:00
|
|
|
"https://0x0.st",
|
2021-07-25 17:24:42 +02:00
|
|
|
[ file => [ undef, "filename", Content => $text, 'Content-Type' => 'text/plain' ] ],
|
2021-02-07 22:33:24 +01:00
|
|
|
Content_Type => 'form-data'
|
|
|
|
);
|
2021-02-07 22:51:58 +01:00
|
|
|
}
|
|
|
|
|
2024-04-08 05:55:04 +02:00
|
|
|
sub paste_sprunge($self, $text) {
|
|
|
|
my $ua = LWP::UserAgent::Paranoid->new(request_timeout => 10, agent => 'Mozilla/5.0');
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-02-07 22:51:58 +01:00
|
|
|
push @{$ua->requests_redirectable}, 'POST';
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2024-04-08 05:55:04 +02:00
|
|
|
return $ua->post(
|
|
|
|
"http://sprunge.us",
|
|
|
|
Content => [(sprunge => $text)],
|
|
|
|
Content_Type => 'form-data'
|
|
|
|
);
|
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2024-04-08 05:55:04 +02:00
|
|
|
sub paste_ixio($self, $text) {
|
|
|
|
my $ua = LWP::UserAgent::Paranoid->new(request_timeout => 10);
|
|
|
|
push @{$ua->requests_redirectable}, 'POST';
|
|
|
|
my %post = ('f:1' => $text);
|
2021-06-05 22:20:03 +02:00
|
|
|
return $ua->post("http://ix.io", \%post);
|
2017-12-06 06:05:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|