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.1 by elmex, Tue Jan 17 16:53:47 2006 UTC vs.
Revision 1.26 by sf-exg, Mon Jun 9 19:54:26 2014 UTC

1#! perl 1#! perl
2use Digest::MD5 qw/md5_hex/;
3 2
4my $timers = {}; 3#:META:RESOURCE:%.cmd:string:the command to run create a new pastebin
5my $pastebin_cmd; 4#:META:RESOURCE:%.url:string:the url template for new pastebins
6my $pastebin_url;
7 5
8sub on_start { 6=head1 NAME
9 my ($self) = @_;
10 $pastebin_cmd =
11 $self->x_resource ("selection-pastebin-cmd")
12 or "scp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
13 7
14 $pastebin_url = 8selection-pastebin - automatic pastebin upload
15 $self->x_resource ("selection-pastebin-url") 9
16 or "http://www.ta-sa.org/files/txt/"; 10=head1 EXAMPLES
17 (); 11
18} 12 URxvt.keysym.C-M-e: selection-pastebin:remote-pastebin
13
14=head1 DESCRIPTION
15
16This is a little rarely useful extension that uploads the selection as
17textfile to a remote site (or does other things). (The implementation is
18not currently secure for use in a multiuser environment as it writes to
19F</tmp> directly.).
20
21It listens to the C<selection-pastebin:remote-pastebin> action, which,
22when activated, runs a command with C<%> replaced by the name of the
23textfile. 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
19 45
20sub upload_paste { 46sub upload_paste {
21 my ($self) = @_; 47 my ($self) = @_;
22 48
49 require Digest::MD5;
23 50
24 my $txt = $self->selection; 51 my $txt = $self->selection;
25 my $filename = md5_hex ($txt) . ".txt"; 52
53 my $filename = $txt;
54 utf8::encode $filename;
55 $filename = Digest::MD5::md5_hex ($filename) . ".txt";
26 56
27 my $tmpfile = "/tmp/$filename"; 57 my $tmpfile = "/tmp/$filename";
28 58
29 my $msg = "uploaded $filename"; 59 my $msg = "uploaded as $filename";
30 60
31 if (open my $o, ">" . $tmpfile) { 61 if (open my $o, ">:utf8", $tmpfile) {
62 chmod 0644, $tmpfile;
32 print $o $txt; 63 print $o $txt;
33 close $o; 64 close $o;
34 } else { 65 } else {
35 $msg = "couldn't write $tmpfile: $!"; 66 $msg = "couldn't write $tmpfile: $!";
36 } 67 }
37 68
38 my $cmd = $pastebin_cmd; 69 my $cmd = $self->{pastebin_cmd};
39 $cmd =~ s/%/$tmpfile/; 70 $cmd =~ s/%/$tmpfile/;
40 71
41 if (system ($cmd) == 0) { 72 my $pid = $self->exec_async ($cmd);
42 my $url = $pastebin_url;
43 $url =~ s/%/$filename/;
44 73
45 $self->selection ($url); 74 $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub {
46 } else { 75 my (undef, $status) = @_;
47 $msg = "couldn't upload, '$cmd' failed";
48 }
49 76
50 my $ov = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); 77 delete $self->{pw};
51 $ov->set (0, 0, $msg);
52 78
53 $timers->{t1} = 79 if ($status) {
80 $status >>= 8;
81 $msg = "ERROR: command returned status $status";
82 } else {
83 my $url = $self->{pastebin_url};
84 $url =~ s/%/$filename/;
85
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} =
54 urxvt::timer 95 urxvt::timer
55 ->new 96 ->new
56 ->start ((int urxvt::NOW) + 5) # make sure we update "on" the second
57 ->interval (1) 97 ->after (5)
58 ->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); 98 ->cb (sub { delete $self->{timer}; undef $ov; });
99 });
59} 100}
60 101
61sub on_keyboard_command { 102sub on_start {
62 my ($self, $cmd) = @_; 103 my ($self) = @_;
63 104
64 $cmd eq "selection-pastebin:remote-pastebin" 105 $self->{pastebin_cmd} = $self->x_resource ("%.cmd")
65 and upload_paste ($self); 106 || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
107
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 {
112 ("pastebin upload" => sub { $self->upload_paste })
113 };
66 114
67 () 115 ()
68} 116}
69 117
118sub on_user_command {
119 my ($self, $cmd) = @_;
120
121 if ($cmd eq "selection-pastebin:remote-pastebin") {
122 $self->upload_paste;
123 }
124
125 ()
126}
127
128sub on_action {
129 my ($self, $action) = @_;
130
131 $action eq "remote-pastebin"
132 and $self->upload_paste;
133
134 ()
135}
136

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines