Initial commit
- Id
- b3d73ea661a27b5a886cc62444415f43838c6e26
- Author
- Caio
- Commit time
- 2012-08-04T21:10:02+02:00
Created README
+PasteIt is a simple command line app for the Knoxious-Open-Pastebin[1]
+with some neat nittle features to fit my needs, mostly:
+
+ * Read/Write clipboard support
+ * Configuration file
+ * Input from STDIN or filename arguments
+
+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,
+ );
+
+[1]: https://github.com/xanmanning/Knoxious-Open-Pastebin
Created pasteit
+#!/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";
Created pasteit.conf
+# vim:set ft=perl:
+
+%options = (
+ paste => 'http://yourpaste.uri/api',
+ author => 'John Doe',
+ to_clipboard => 1,
+ lifespan => 3,
+ raw => 1,
+);