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.10 by root, Thu Jan 19 16:22:13 2006 UTC vs.
Revision 1.17 by sf-exg, Sat Nov 19 17:40:48 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines