caio.co/de/pasteit


Initial commit by Caio 12 years ago (log)

Blob pasteit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env perl
# I've tried keeping dependencies to a minimum. What this script needs
# is as follows:
#       - LWP
#       - LWP::Protocol::https
#       - Net::SSL
# HTTP::Tiny's API is WAY more sane, but LWP is installed everywhere :(
#
# For fiddling with the clipboard you need a clipboard manager
#       - Linux: xclip
#       - Mac: pbcopy/pbpaste
# Note: I don't have a mac. If it doesn't work just let me know :-)
#
# There is support for a config file. Just create a $HOME/.pasteit.conf
# with something like this:
#        %options = (
#            author => 'Caio Romao',
#            paste => 'https://pzt.me/api',
#            to_clipboard => 1,
#            lifespan => 3,
#            raw => 1,
#        );


use strict;
use warnings;
use English; # I refuse to use $^O

# IO::Socket::SSL randomly misbehaves in such a fucked up way
# that the connection just times out. I haven't dug deep here
# but note that this should have nothing to do with the
# misbehaving related to HTTPS_PROXY since I have none set up
# at home.
#
# HTTP::Tiny stated behaving at home the same after I pulled
# LWP::Protocol::https, so maybe the misbehaving is related.

use if !$ENV{SERVER_ROLE} || !$ENV{HTTPS_PROXY}, 'Net::SSL' => ();

# In case things get even worse:
# BEGIN {
#     $Net::HTTPS::SSL_SOCKET_CLASS = "Net::SSL";
#     $ENV{HTTPS_PROXY} = '';
# }

use LWP::UserAgent;
use LWP::Protocol::https;
use Encode qw/decode/;
use Data::Dumper qw/Dumper/;
use Getopt::Long qw/GetOptions/;

# Default options
my %options = (
    highlighter => 'plaintext',
    author => 'Anonymous',
    lifespan => 0,
    privacy => 0,
);

sub read_clipboard {
    $OSNAME eq 'linux' ? `xclip -o` : `pbpaste`;
}

sub write_clipboard {
    my $content = shift;
    my $cmd = '';

    # Somehow just using `echo $content |xclip -i` blocks
    # I'm guessing it's a deadlock: xclip tries to write
    # to stdin while perl is blocked waiting for xclip to
    # exit. NOTE: This might block on a mac also, so I try
    # to "make it right" by default.
    $cmd = $OSNAME eq 'linux' ? '|xclip -selection clipboard -i' : '|pbcopy';
    my $r = open (my $exe, $cmd);
    print $exe $content;
    close $exe;
}

GetOptions(
    \%options,
    'author|a=s',
    'paste|p=s',
    'lifespan|l=i',
    'highlighter|f=s',
    'privacy|x',
    'from_clipboard|f',
    'to_clipboard|t',
    'raw|r',
    'help|h',
);

if ($options{help}) {
    print q{Usage: pasteit.pl -p <URL> [OPTION]... [FILE]
Send FILE, or standard input, to pastebin.

  -p, --paste=URL          URL for the paste API
  -a, --author=NAME        Name of the author for the paste
  -f, --highlighter=TYPE   Filetype of the paste. Values: FIXME
  -x, --privacy            Whether the post is private or not
  -r, --raw                Output the link to the raw paste
  -l, --lifespan=N         When to expire the paste:
                               0 => 13 weeks
                               1 => 26 weeks
                               2 => 4 weeks
                               3 => 1 week
                               4 => never
  -f, --from_clipboard     Read input from clipboard instead of STDIN
  -t, --to_clipboard       Write result URL to clipboard
  -h, --help               What you're currently reading

Exit status:
  0  if OK,
  1  if an error occurred.

Report pasteit bugs to github@caioromao.com
};
    exit 0;
}

# Rudimentary support for a config file
eval {
    use Safe;
    no warnings 'once';
    my $comp = new Safe 'pasteit';
    $comp->rdo($ENV{HOME} . "/.pasteit.conf");
    %options = (%options, %pasteit::options);
    1;
};

if (!$options{paste}) {
    print STDERR "Don't know where to paste it. Missing --paste option\n";
    exit 1;
}

my $data;
if (!@ARGV && $options{from_clipboard}) {
    # read from clipboard
    $data = read_clipboard;
}
else {
    # Read from STDIN *or* from the filenames given in ARGV
    while (<>) {
        $data .= $_;
    }
}

# If you're not using utf-8 I'm sorry for you
$data = decode 'utf-8', $data;

my $api_data = {
    pasteEnter => $data,
    %options,
};

my $ua = LWP::UserAgent->new(
    ssl_opts => { verify_hostname => 0},
);
my $response = $ua->post($options{paste}, $api_data);

# # HTTP::Tiny supports proxies but doesn't test the NO_PROXY env
# # agains the url. proxy => undef prevents it against using a
# # proxy at all.
# my $http = HTTP::Tiny->new(proxy => undef);
# my $response = $http->post_form(PASTE, $api_data);

if ($response->is_error) {
    print STDERR "Request Error: " . $response->code . ": " .
                 $response->decoded_content . "\n";
    exit 1;
}

# AWESOME JSON2PERL TRANSFORMATION ZOMG WTFBBQ!!!11!
# I love how this API produces json but doesn't consume it :/
my $result = $response->decoded_content;
$result =~ s/"(\w+)":/$1 =>/g;
my $DANGEROUS_RESULT = eval($result);

if ($DANGEROUS_RESULT->{error}) {
    print STDERR "PasteIt Error: $DANGEROUS_RESULT->{message}\n";
    exit 1;
}

my $url = $DANGEROUS_RESULT->{url};
$url .= '@raw' if $options{raw};

write_clipboard $url if $options{to_clipboard};
print "$url\n";