ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
Revision: 1.17
Committed: Sat Nov 19 17:40:48 2011 UTC (12 years, 6 months ago) by sf-exg
Branch: MAIN
CVS Tags: rel-9_14, rxvt-unicode-rel-9_15
Changes since 1.16: +1 -1 lines
Log Message:
Spawn selection-pastebin command directly rather than via /bin/sh.

File Contents

# User Rev Content
1 elmex 1.1 #! perl
2    
3     sub upload_paste {
4     my ($self) = @_;
5    
6 root 1.2 require Digest::MD5;
7 elmex 1.1
8     my $txt = $self->selection;
9 root 1.16
10     my $filename = $txt;
11     utf8::encode $filename;
12     $filename = Digest::MD5::md5_hex ($filename) . ".txt";
13 elmex 1.1
14     my $tmpfile = "/tmp/$filename";
15    
16 root 1.12 my $msg = "uploaded as $filename";
17 elmex 1.1
18 root 1.14 if (open my $o, ">:utf8", $tmpfile) {
19 root 1.6 chmod 0644, $tmpfile;
20 elmex 1.1 print $o $txt;
21     close $o;
22     } else {
23     $msg = "couldn't write $tmpfile: $!";
24     }
25    
26 root 1.14 my $cmd = $self->{pastebin_cmd};
27 elmex 1.1 $cmd =~ s/%/$tmpfile/;
28    
29 sf-exg 1.17 my $pid = $self->exec_async ($cmd);
30 elmex 1.1
31 root 1.14 $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub {
32     my (undef, $status) = @_;
33 elmex 1.4
34 root 1.14 delete $self->{pw};
35 elmex 1.1
36 root 1.14 if ($status) {
37     $status >>= 8;
38     $msg = "ERROR: command returned status $status";
39     } else {
40     my $url = $self->{pastebin_url};
41     $url =~ s/%/$filename/;
42    
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} =
52     urxvt::timer
53     ->new
54     ->after (5)
55     ->cb (sub { delete $self->{timer}; undef $ov; });
56     });
57 elmex 1.1 }
58    
59 root 1.5 sub on_start {
60     my ($self) = @_;
61 root 1.10
62 root 1.14 $self->{pastebin_cmd} = $self->x_resource ("selection-pastebin.cmd")
63     || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
64 root 1.5
65 root 1.14 $self->{pastebin_url} = $self->x_resource ("selection-pastebin.url")
66     || "http://www.ta-sa.org/files/txt/%";
67 root 1.5
68 root 1.10 push @{ $self->{term}{selection_popup_hook} }, sub {
69     ("pastebin upload" => sub { $self->upload_paste })
70     };
71    
72 root 1.5 ()
73     }
74    
75 root 1.15 sub on_user_command {
76 elmex 1.1 my ($self, $cmd) = @_;
77    
78 root 1.5 if ($cmd eq "selection-pastebin:remote-pastebin") {
79     $self->upload_paste;
80     }
81 elmex 1.1
82     ()
83     }
84