ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
(Generate patch)

Comparing rxvt-unicode/src/perl/selection-pastebin (file contents):
Revision 1.12 by root, Thu Jan 19 19:51:08 2006 UTC vs.
Revision 1.19 by root, Sun Jun 10 13:58:06 2012 UTC

1#! perl 1#! perl
2 2
3my $pastebin_cmd; 3#:META:X_RESOURCE:%.cmd:string:the command to run create a new pastebin
4my $pastebin_url; 4#:META:X_RESOURCE:%.url:string:the url template for new pastebins
5 5
6sub upload_paste { 6sub upload_paste {
7 my ($self) = @_; 7 my ($self) = @_;
8 8
9 require Digest::MD5; 9 require Digest::MD5;
10 10
11 my $txt = $self->selection; 11 my $txt = $self->selection;
12
13 my $filename = $txt;
14 utf8::encode $filename;
12 my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; 15 $filename = Digest::MD5::md5_hex ($filename) . ".txt";
13 16
14 my $tmpfile = "/tmp/$filename"; 17 my $tmpfile = "/tmp/$filename";
15 18
16 my $msg = "uploaded as $filename"; 19 my $msg = "uploaded as $filename";
17 20
18 if (open my $o, ">", $tmpfile) { 21 if (open my $o, ">:utf8", $tmpfile) {
19 chmod 0644, $tmpfile; 22 chmod 0644, $tmpfile;
20 print $o $txt; 23 print $o $txt;
21 close $o; 24 close $o;
22 } else { 25 } else {
23 $msg = "couldn't write $tmpfile: $!"; 26 $msg = "couldn't write $tmpfile: $!";
24 } 27 }
25 28
26 my $cmd = $pastebin_cmd; 29 my $cmd = $self->{pastebin_cmd};
27 $cmd =~ s/%/$tmpfile/; 30 $cmd =~ s/%/$tmpfile/;
28 31
29 if (system ($cmd) == 0) { 32 my $pid = $self->exec_async ($cmd);
30 my $url = $pastebin_url;
31 $url =~ s/%/$filename/;
32 33
33 $self->selection ($url); 34 $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub {
34 } else { 35 my (undef, $status) = @_;
35 $msg = "couldn't upload, '$cmd' failed";
36 }
37 36
38 unlink $tmpfile; 37 delete $self->{pw};
39 38
40 my $ov = $self->overlay (-1, 0, length $msg, 1, urxvt::OVERLAY_RSTYLE, 0); 39 if ($status) {
41 $ov->set (0, 0, $msg); 40 $status >>= 8;
41 $msg = "ERROR: command returned status $status";
42 } else {
43 my $url = $self->{pastebin_url};
44 $url =~ s/%/$filename/;
42 45
43 my $timer; $timer = 46 $self->selection ($url);
47 }
48
49 unlink $tmpfile;
50
51 my $ov = $self->overlay (-1, 0, $self->strwidth ($msg), 1, urxvt::OVERLAY_RSTYLE, 0);
52 $ov->set (0, 0, $msg);
53
54 $self->{timer} =
44 urxvt::timer 55 urxvt::timer
45 ->new 56 ->new
46 ->after (5) 57 ->after (5)
47 ->cb (sub { undef $timer; undef $ov; }); 58 ->cb (sub { delete $self->{timer}; undef $ov; });
59 });
48} 60}
49 61
50sub on_start { 62sub on_start {
51 my ($self) = @_; 63 my ($self) = @_;
52 64
53 $pastebin_cmd = $self->x_resource ("selection-pastebin.cmd") 65 $self->{pastebin_cmd} = $self->x_resource ("%.cmd")
54 || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; 66 || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
55 67
56 $pastebin_url = $self->x_resource ("selection-pastebin.url") 68 $self->{pastebin_url} = $self->x_resource ("%.url")
57 || "http://www.ta-sa.org/files/txt/%"; 69 || "http://www.ta-sa.org/files/txt/%";
58 70
59 push @{ $self->{term}{selection_popup_hook} }, sub { 71 push @{ $self->{term}{selection_popup_hook} }, sub {
60 ("pastebin upload" => sub { $self->upload_paste }) 72 ("pastebin upload" => sub { $self->upload_paste })
61 }; 73 };
62 74
63 () 75 ()
64} 76}
65 77
66sub on_keyboard_command { 78sub on_user_command {
67 my ($self, $cmd) = @_; 79 my ($self, $cmd) = @_;
68 80
69 if ($cmd eq "selection-pastebin:remote-pastebin") { 81 if ($cmd eq "selection-pastebin:remote-pastebin") {
70 $self->upload_paste; 82 $self->upload_paste;
71 } 83 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines