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.6 by root, Wed Jan 18 10:31:37 2006 UTC vs.
Revision 1.23 by root, Sat May 17 13:38:23 2014 UTC

1#! perl 1#! perl
2 2
3my $timers = {}; 3#:META:RESOURCE:%.cmd:string:the command to run create a new pastebin
4my $pastebin_cmd; 4#:META:RESOURCE:%.url:string:the url template for new pastebins
5my $pastebin_url; 5
6=head1 NAME
7
8selection-pastebin - automatic pastebin upload
9
10=head1 DESCRIPTION
11
12This is a little rarely useful extension that uploads the selection as
13textfile to a remote site (or does other things). (The implementation is
14not currently secure for use in a multiuser environment as it writes to
15F</tmp> directly.).
16
17It listens to the C<selection-pastebin:remote-pastebin> keyboard command,
18i.e.
19
20 URxvt.keysym.C-M-e: perl:selection-pastebin:remote-pastebin
21
22Pressing this combination runs a command with C<%> replaced by the name of
23the textfile. This command can be set via a resource:
24
25 URxvt.selection-pastebin.cmd: rsync -apP % ruth:/var/www/www.ta-sa.org/files/txt/.
26
27And the default is likely not useful to anybody but the few people around
28here :)
29
30The name of the textfile is the hex encoded md5 sum of the selection, so
31the same content should lead to the same filename.
32
33After a successful upload the selection will be replaced by the text given
34in the C<selection-pastebin-url> resource (again, the % is the placeholder
35for the filename):
36
37 URxvt.selection-pastebin.url: http://www.ta-sa.org/files/txt/%
38
39I<Note to xrdb users:> xrdb uses the C preprocessor, which might interpret
40the double C</> characters as comment start. Use C<\057\057> instead,
41which works regardless of whether xrdb is used to parse the resource file
42or not.
43
44=cut
6 45
7sub upload_paste { 46sub upload_paste {
8 my ($self) = @_; 47 my ($self) = @_;
9 48
10 require Digest::MD5; 49 require Digest::MD5;
11 50
12 my $txt = $self->selection; 51 my $txt = $self->selection;
52
53 my $filename = $txt;
54 utf8::encode $filename;
13 my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; 55 $filename = Digest::MD5::md5_hex ($filename) . ".txt";
14 56
15 my $tmpfile = "/tmp/$filename"; 57 my $tmpfile = "/tmp/$filename";
16 58
17 my $msg = "uploaded $filename"; 59 my $msg = "uploaded as $filename";
18 60
19 if (open my $o, ">$tmpfile") { 61 if (open my $o, ">:utf8", $tmpfile) {
20 chmod 0644, $tmpfile; 62 chmod 0644, $tmpfile;
21 print $o $txt; 63 print $o $txt;
22 close $o; 64 close $o;
23 } else { 65 } else {
24 $msg = "couldn't write $tmpfile: $!"; 66 $msg = "couldn't write $tmpfile: $!";
25 } 67 }
26 68
27 my $cmd = $pastebin_cmd; 69 my $cmd = $self->{pastebin_cmd};
28 $cmd =~ s/%/$tmpfile/; 70 $cmd =~ s/%/$tmpfile/;
29 71
30 if (system ($cmd) == 0) { 72 my $pid = $self->exec_async ($cmd);
31 my $url = $pastebin_url;
32 $url =~ s/%/$filename/;
33 73
34 $self->selection ($url); 74 $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub {
35 } else { 75 my (undef, $status) = @_;
36 $msg = "couldn't upload, '$cmd' failed";
37 }
38 76
39 unlink $tmpfile; 77 delete $self->{pw};
40 78
41 my $ov = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); 79 if ($status) {
42 $ov->set (0, 0, $msg); 80 $status >>= 8;
81 $msg = "ERROR: command returned status $status";
82 } else {
83 my $url = $self->{pastebin_url};
84 $url =~ s/%/$filename/;
43 85
44 $timers->{t1} = 86 $self->selection ($url);
87 }
88
89 unlink $tmpfile;
90
91 my $ov = $self->overlay (-1, 0, $self->strwidth ($msg), 1, urxvt::OVERLAY_RSTYLE, 0);
92 $ov->set (0, 0, $msg);
93
94 $self->{timer} =
45 urxvt::timer 95 urxvt::timer
46 ->new 96 ->new
47 ->start ((int urxvt::NOW) + 5) # make sure we update "on" the second
48 ->interval (1) 97 ->after (5)
49 ->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); 98 ->cb (sub { delete $self->{timer}; undef $ov; });
99 });
50} 100}
51 101
52sub on_start { 102sub on_start {
53 my ($self) = @_; 103 my ($self) = @_;
54 $pastebin_cmd = $self->x_resource ("selection-pastebin.cmd")
55 || "scp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
56 104
57 $pastebin_url = $self->x_resource ("selection-pastebin.url") 105 $self->{pastebin_cmd} = $self->x_resource ("%.cmd")
58 || "http://www.ta-sa.org/files/txt/%"; 106 || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
59 107
60 push @urxvt::ext::selection_popup::hook, sub { 108 $self->{pastebin_url} = $self->x_resource ("%.url")
109 || "http://www.ta-sa.org/files/txt/%";
110
111 push @{ $self->{term}{selection_popup_hook} }, sub {
61 ("pastebin upload" => sub { $self->upload_paste }) 112 ("pastebin upload" => sub { $self->upload_paste })
62 }; 113 };
63 114
64 () 115 ()
65} 116}
66 117
67sub on_keyboard_command { 118sub on_user_command {
68 my ($self, $cmd) = @_; 119 my ($self, $cmd) = @_;
69 120
70 if ($cmd eq "selection-pastebin:remote-pastebin") { 121 if ($cmd eq "selection-pastebin:remote-pastebin") {
71 $self->upload_paste; 122 $self->upload_paste;
72 } 123 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines