--- rxvt-unicode/src/perl/selection-pastebin 2006/01/17 16:53:47 1.1 +++ rxvt-unicode/src/perl/selection-pastebin 2012/06/10 13:58:06 1.19 @@ -1,68 +1,86 @@ #! perl -use Digest::MD5 qw/md5_hex/; -my $timers = {}; -my $pastebin_cmd; -my $pastebin_url; - -sub on_start { - my ($self) = @_; - $pastebin_cmd = - $self->x_resource ("selection-pastebin-cmd") - or "scp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; - - $pastebin_url = - $self->x_resource ("selection-pastebin-url") - or "http://www.ta-sa.org/files/txt/"; - (); -} +#:META:X_RESOURCE:%.cmd:string:the command to run create a new pastebin +#:META:X_RESOURCE:%.url:string:the url template for new pastebins sub upload_paste { my ($self) = @_; + require Digest::MD5; my $txt = $self->selection; - my $filename = md5_hex ($txt) . ".txt"; + + my $filename = $txt; + utf8::encode $filename; + $filename = Digest::MD5::md5_hex ($filename) . ".txt"; my $tmpfile = "/tmp/$filename"; - my $msg = "uploaded $filename"; + my $msg = "uploaded as $filename"; - if (open my $o, ">" . $tmpfile) { + if (open my $o, ">:utf8", $tmpfile) { + chmod 0644, $tmpfile; print $o $txt; close $o; } else { $msg = "couldn't write $tmpfile: $!"; } - my $cmd = $pastebin_cmd; + my $cmd = $self->{pastebin_cmd}; $cmd =~ s/%/$tmpfile/; - if (system ($cmd) == 0) { - my $url = $pastebin_url; - $url =~ s/%/$filename/; + my $pid = $self->exec_async ($cmd); - $self->selection ($url); - } else { - $msg = "couldn't upload, '$cmd' failed"; - } + $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub { + my (undef, $status) = @_; - my $ov = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); - $ov->set (0, 0, $msg); + delete $self->{pw}; - $timers->{t1} = - urxvt::timer - ->new - ->start ((int urxvt::NOW) + 5) # make sure we update "on" the second - ->interval (1) - ->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); + if ($status) { + $status >>= 8; + $msg = "ERROR: command returned status $status"; + } else { + my $url = $self->{pastebin_url}; + $url =~ s/%/$filename/; + + $self->selection ($url); + } + + unlink $tmpfile; + + my $ov = $self->overlay (-1, 0, $self->strwidth ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); + $ov->set (0, 0, $msg); + + $self->{timer} = + urxvt::timer + ->new + ->after (5) + ->cb (sub { delete $self->{timer}; undef $ov; }); + }); } -sub on_keyboard_command { +sub on_start { + my ($self) = @_; + + $self->{pastebin_cmd} = $self->x_resource ("%.cmd") + || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; + + $self->{pastebin_url} = $self->x_resource ("%.url") + || "http://www.ta-sa.org/files/txt/%"; + + push @{ $self->{term}{selection_popup_hook} }, sub { + ("pastebin upload" => sub { $self->upload_paste }) + }; + + () +} + +sub on_user_command { my ($self, $cmd) = @_; - $cmd eq "selection-pastebin:remote-pastebin" - and upload_paste ($self); + if ($cmd eq "selection-pastebin:remote-pastebin") { + $self->upload_paste; + } () }