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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
#!/usr/bin/perl -w
#
=head1 NAME
debpaste - https://paste.debian.net/ XML-RPC client
=cut
# Author: Hanno Hecker <vetinari@ankh-morp.org>
# Licence: AGPL 3.0 (https://www.fsf.org/licensing/licenses/agpl-3.0.html)
# Version: $Id: debpaste 22 2009-11-19 17:23:47Z vetinari $
# SVN: http://svn.ankh-morp.org:8080/tools/paste-dn/
#
# Required:
# deb: perl-base perl-modules
# libtimedate-perl libfrontier-rpc-perl libtext-iconv-perl
#
# ToDo:
# * "get" formatting?
# * wishlist :)
#
use strict;
use Getopt::Long;
use Pod::Usage;
my %config;
my $VERSION = '1.1 ($Rev: 22 $)';
=head1 SYNOPSIS
B<debpaste> ACTION [OPTIONS] [CODE|ID]
=head1 ACTIONS
=over 4
=item add
Usage: debpaste add [OPTIONS] [CODE]
Adds a new paste to L<https://paste.debian.net/>. If no code is given on the
command line, it will read from stdin.
Your paste infos are saved to I<~/.debpaste.history>
=item del
Usage: debpaste del [OPTIONS] ID
Deletes paste with id ID. This must be an ID which you have pasted before
(and is in your history file)
=item get
Usage: debpaste get [OPTIONS] ID
Fetches the paste with id ID from L<https://paste.debian.net>. To C<download>
a paste use something like
debpaste get --noheader ID > OUTFILE
=item lang
Usage: debpaste lang [OPTIONS]
Dumps the list of available languages for syntax highlighting, use the
B<--lang=LANG> option when B<add>ing a paste.
=item edit
Usage: debpaste edit [OPTIONS] ID
Downloads the paste with id ID, spawns an editor, and sends the edited file
as new paste.
=item expire
Usage: debpaste expire [OPTIONS] [ID]
Removes the entry ID from history file. If no ID is given it removes all
entries which are expired.
=back
=head1 OPTIONS
=over 4
=item --user=USERNAME
paste as USERNAME instead of C<anonymous>
=item --server=URL
use URL instead of https://paste.debian.net/server.pl
=item --noproxy
do not use the http proxy given in the environment variable C<http_proxy>
=item --lang=LANG
use LANG for syntax highlight ('debpaste lang' for available languages)
=item --expires=SEC
expires in SEC seconds (default: 259200 = 72h)
=item --encoding=ENC
when adding new paste, use ENC as encoding of file, default: UTF-8
=item --noheader
when B<get>ting entries, don't print header, just dump the paste to stdout.
=item --version
print version and exit
=back
=cut
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");
$0 =~ s#.*/##;
=head1 FILES
=over 4
=item ~/.debpaste.rc
The right place for setting default options like the username or expire values.
Format is C<KeyInAnYCase: value>, example:
User: Vetinari
Expires: 86400
=item ~/.debpaste.history
All info about pastes done with B<debpaste> are recorded here. This file
is used to keep a record for B<del>eting entries after pasting. Use
B<debpaste expire> to remove old entries.
=back
=cut
my $settings = $ENV{HOME}."/.debpaste.rc";
## Don't change, edit $settings file:
## KeYInAnyCaSE: value
## AnoThErKey: other-value
my $history = $ENV{HOME}."/.debpaste.history";
%config = (
server => "https://paste.debian.net/server.pl",
user => "anonymous",
lang => "",
expires => 86400 * 3, #
history_file => $history,
no_get_header => 0,
);
my $action = "help";
my %help = (
'add' => "\n"
."Usage: $0 add [OPTIONS] [CODE]\n"
." Adds a new paste to https://paste.debian.net/\n"
." If no code is given on the command line, it will read from\n"
." stdin.\n"
." Your paste infos are saved to $history\n",
'get' => "\n"
."Usage: $0 get [OPTIONS] ID\n"
." Fetches the paste with id ID from paste.debian.net\n"
." To 'download' a paste use something like\n"
." $0 get --noheader ID > OUTFILE\n",
'del' => "\n"
."Usage: $0 del [OPTIONS] ID\n"
." Deletes paste with id ID. This must be an ID which you have\n"
." pasted before (and is in your history file)\n",
'lang' => "\n"
."Usage: $0 lang [OPTIONS]\n"
." Dumps the list of available languages for syntax highlighting\n",
'edit' => "\n"
."Usage: $0 edit [OPTIONS] ID\n"
." Downloads the paste with id ID, spawns an editor (\$EDITOR)\n"
." and sends the edited file as new paste\n",
'expire' => "\n"
."Usage: $0 expire [OPTIONS] [ID]\n"
." Removes the entry ID from history file. If no ID is given,\n"
." it removes all entries which are expired.\n",
# 'help' => "FIXME: help",
);
if (@ARGV and $ARGV[0] !~ /^-/) {
$action = shift @ARGV;
}
&read_settings();
GetOptions(
"user=s" => \$config{user},
"server=s" => \$config{server},
"expires=s" => \$config{expires},
"lang=s" => \$config{lang},
"encoding=s"=> \$config{encoding},
"noheader" => \$config{no_get_header},
"help" => sub { pod2usage(-exitval => 0, -verbose => 2) },
"version" => sub { print "debpaste v$VERSION\n"; exit 0; },
)
or pod2usage(-exitval => 1, -verbose => 2);
if ($action and $action eq "help") {
$action = shift @ARGV
if (@ARGV and $ARGV[0] !~ /^-/);
&help($action);
exit 0;
}
my $paste = PasteDN->new(%config);
if ($paste->can($action) and $action ne "new" and $action !~ /^_/) {
$paste->$action();
}
else {
die "$0: err... unknown action $action...\n";
}
sub read_settings {
open SET, $settings
or return;
while (defined (my $line = <SET>)) {
next unless $line =~ /^(\w+):\s+(.*)$/;
my ($key, $value) = (lc $1, $2);
unless (exists $config{$key}) {
warn "$0: unknown config key '$key' found\n";
next;
}
($config{$key} = $value) =~ s/^\s*(.*?)\s*$/$1/;
}
close SET;
}
sub help {
my $msg = "";
($msg = $help{$_[0]}."\n") if (exists $help{$_[0]});
pod2usage(-exitval => 0, -verbose => 2, -message => $msg);
}
###################################################################
package PasteDN;
use Frontier::Client;
use Date::Parse;
use POSIX qw(strftime);
use File::Temp qw(tempfile);
use Text::Iconv;
sub new {
my $me = shift;
my %args = @_;
my $type = ref($me) || $me;
my $self = {};
bless $self, $type;
foreach (keys %args) {
$self->{$_} = $args{$_};
}
unless (exists $self->{editor}) {
$self->{editor} = $ENV{EDITOR} ?
$ENV{EDITOR} : ($ENV{VISUAL} ?
$ENV{VISUAL} : "/usr/bin/editor");
}
$self->{encoding} = "UTF-8" unless $self->{encoding};
$self->{expires} += time;
my %fc = ( url => $self->{server} );
unless ($self->{noproxy}) {
$fc{proxy} = $ENV{http_proxy} if $ENV{http_proxy};
}
$self->{_service} = Frontier::Client->new(%fc);
$self;
}
sub _to_utf8 {
my ($self,$txt) = @_;
my $enc = $self->{encoding};
return $txt if $enc eq "UTF-8";
my $i = eval { Text::Iconv->new($enc, "UTF-8"); };
die "$0: unsupported encoding $enc\n" if $@;
my $new = $i->convert($txt);
return $txt unless $new;
return $new;
}
sub _error {
my ($self, $msg) = @_;
unlink $self->{_tempfile} if $self->{_tempfile};
die "$0: $msg\n";
}
sub lang {
my $self = shift;
my $rc = $self->{_service}->call("paste.getLanguages");
die $rc->{statusmessage},"\n" if $rc->{rc};
## print $rc->{statusmessage},"\n";
print "Available syntax highlights:\n";
foreach (@{$rc->{langs}}) {
print " $_\n";
}
}
sub get {
my $self = shift;
my $id = shift @ARGV;
die "$0: no id given\n" unless $id;
my $rc = $self->{_service}->call("paste.getPaste", $id);
die $rc->{statusmessage},"\n" if $rc->{rc};
# ugly, but dates are ok then...
# FIXME: probably only works with paste.d.n's timezone:
my $stime = str2time($rc->{submitdate}, "CET") - 3600;
my $sub_date = strftime('%Y-%m-%d %H:%M:%S', localtime $stime);
my $exp_date = strftime('%Y-%m-%d %H:%M:%S',
localtime($stime + $rc->{expiredate}));
unless ($self->{no_get_header}) {
print "User: ", $rc->{submitter}, "\n",
"Date: $sub_date\n",
"Expires: $exp_date\n",
"---------------------------------\n";
}
print $rc->{code},"\n";
}
sub edit {
my $self = shift;
my $id = shift @ARGV;
die "$0: no id given\n" unless $id;
my $rc = $self->{_service}->call("paste.getPaste", $id);
die $rc->{statusmessage},"\n" if $rc->{rc};
my $new = $self->_spawn_editor($rc->{code});
if (!$new or ($new eq $rc->{code})) {
print "$0: not changed, aborting...\n";
exit 0;
}
## FIXME: text from paste.debian.net is probably UTF-8
## $new = $self->_to_utf8($new);
$rc = $self->{_service}->call("paste.addPaste", $new,
$self->{user},
$self->{expires} - time,
$self->{lang});
die $rc->{statusmessage},"\n"
if $rc->{rc};
print $rc->{statusmessage},"\n";
print "To delete this entry, use: $0 del $rc->{id}\n";
$self->_save_entry($rc);
}
sub _spawn_editor {
my ($self, $txt) = @_;
my $fh;
($fh, $self->{_tempfile}) = tempfile("debpaste.XXXXXX", DIR => "/tmp");
$self->_error("Could not create temp file: $!")
unless ($fh and $self->{_tempfile});
print $fh $txt or $self->_error("Could not print to tempfile: $!");
close $fh or $self->_error("Failed to close tempfile: $!");
if (system($self->{editor}, $self->{_tempfile}) != 0) {
$self->_error("failed to execute: $!")
if $? == -1;
$self->_error(sprintf('child died with signal %d, %s coredump',
($? & 127), ($? & 128) ? 'with' : 'without'))
if $? & 127;
$self->error(sprintf('editor exited with value %d', $? >> 8));
}
open FH, $self->{_tempfile}
or $self->_error("Failed to open temp file: $!");
{
local $/ = undef;
$txt = <FH>;
};
close FH;
unlink $self->{_tempfile};
return $txt;
}
sub delete { $_[0]->del(); }
sub del {
my $self = shift;
my %entry = ();
my $id = shift @ARGV;
die "$0: no id given\n" unless $id;
open FILE, $self->{history_file}
or die "$0: failed to open history file: $!\n";
{
local $/ = "\n\n";
while (<FILE>) {
s#^[\n\s]+##ms;
s#[\n\s]+$##ms;
next unless $_;
%entry = map { /^(\S+):\s*(.*?)\s*$/;
($1, $2 ? $2 : "") } split /\n/, $_;
last if ($entry{Entry} and $entry{Entry} eq $id);
%entry = ();
}
}
die "$0: Entry for $id not found...\n" unless $entry{Entry};
die "$0: No Digest for $id\n" unless $entry{Digest};
die "$0: Entry $id expired at ", scalar(localtime($entry{Expires})),"\n"
if ($entry{Expires} and $entry{Expires} < time);
my $rc = $self->{_service}->call("paste.deletePaste", $entry{Digest});
die $rc->{statusmessage},"\n" if $rc->{rc};
print $rc->{statusmessage},"\n",
"$0: deleted paste id ",$rc->{id},"\n";
$self->_expire($rc->{id});
}
sub expire {
my $self = shift;
my $id = shift @ARGV;
$self->_expire($id);
}
sub _expire {
my ($self, $id) = @_;
my @history = ();
my %entry;
my @ids = ();
open FILE, $self->{history_file}
or return;
{
local $/ = "\n\n";
while (<FILE>) {
s#^[\n\s]+##ms;
s#[\n\s]+$##ms;
next unless $_;
%entry = map { /^(\S+):\s*(.*?)\s*$/;
($1, $2 ? $2 : "") } split /\n/, $_;
## print "ID: $entry{Entry}\n";
if ($id) {
if ($entry{Entry} and $entry{Entry} eq $id) {
push @ids, $entry{Entry};
next;
}
}
elsif ($entry{Expires} and $entry{Expires} < time) {
push @ids, $entry{Entry};
next;
}
push @history, { %entry };
}
}
close FILE;
open FILE, ">", $self->{history_file}
or die "$0: Failed to open history file: $!\n";
foreach my $h (@history) {
foreach (keys %{$h}) {
next unless $_;
print FILE "$_: $h->{$_}\n";
}
print FILE "\n";
}
close FILE or die "$0: failed to write: $!\n";
print "$0: expired ", scalar(@ids), " entries from history",
(@ids ? ": ".join(", ", @ids) : ""), "\n";
}
sub add {
my $self = shift;
my $code = undef;
if (@ARGV) {
$code = join("\n", @ARGV);
}
else {
{ local $/ = undef; $code = <STDIN>; }
}
die "$0: no code given\n"
unless $code;
$code = $self->_to_utf8($code);
my $rc = $self->{_service}->call("paste.addPaste", $code,
$self->{user},
$self->{expires} - time,
$self->{lang});
die $rc->{statusmessage},"\n"
if $rc->{rc};
print $rc->{statusmessage},"\n";
print "To delete this entry, use: $0 del $rc->{id}\n";
$self->_save_entry($rc);
}
sub _save_entry {
my ($self, $rc) = @_;
# return unless $self->{save_pastes};
my $file = $self->{history_file}
or return;
open FILE, ">>", $file or die "$0: failed to open $file: $!\n";
seek FILE, 0, 2 or die "$0: Failed to seek: $!\n";
print FILE "Server: ", $self->{server}, "\n",
"Entry: ", $rc->{id}, "\n",
"Lang: ", $self->{lang}, "\n",
"Expires: ", $self->{expires},"\n",
"Digest: ", $rc->{digest}, "\n\n"
or die "$0: Failed to save paste: $!\n";
close FILE or die "$0: Failed to save paste: $!\n";
}
=head1 DOWNLOAD
L<http://ankh-morp.org/code/paste-dn/debpaste> or
L<SVN|http://svn.ankh-morp.org:8080/tools/paste-dn/>
=head1 NOTES
Renamed to C<debpaste> at Rev. 20
=head1 AUTHOR
Hanno Hecker <vetinari@ankh-morp.org>
=cut
# vim: ts=4 sw=4 expandtab syn=perl
|