1 | #! perl |
1 | #! perl |
|
|
2 | |
|
|
3 | #:META:RESOURCE:%.cmd:string:the command to run create a new pastebin |
|
|
4 | #:META:RESOURCE:%.url:string:the url template for new pastebins |
|
|
5 | |
|
|
6 | =head1 NAME |
|
|
7 | |
|
|
8 | selection-pastebin - automatic pastebin upload |
|
|
9 | |
|
|
10 | =head1 DESCRIPTION |
|
|
11 | |
|
|
12 | This is a little rarely useful extension that uploads the selection as |
|
|
13 | textfile to a remote site (or does other things). (The implementation is |
|
|
14 | not currently secure for use in a multiuser environment as it writes to |
|
|
15 | F</tmp> directly.). |
|
|
16 | |
|
|
17 | It listens to the C<selection-pastebin:remote-pastebin> keyboard command, |
|
|
18 | i.e. |
|
|
19 | |
|
|
20 | URxvt.keysym.C-M-e: perl:selection-pastebin:remote-pastebin |
|
|
21 | |
|
|
22 | Pressing this combination runs a command with C<%> replaced by the name of |
|
|
23 | the 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 | |
|
|
27 | And the default is likely not useful to anybody but the few people around |
|
|
28 | here :) |
|
|
29 | |
|
|
30 | The name of the textfile is the hex encoded md5 sum of the selection, so |
|
|
31 | the same content should lead to the same filename. |
|
|
32 | |
|
|
33 | After a successful upload the selection will be replaced by the text given |
|
|
34 | in the C<selection-pastebin-url> resource (again, the % is the placeholder |
|
|
35 | for the filename): |
|
|
36 | |
|
|
37 | URxvt.selection-pastebin.url: http://www.ta-sa.org/files/txt/% |
|
|
38 | |
|
|
39 | I<Note to xrdb users:> xrdb uses the C preprocessor, which might interpret |
|
|
40 | the double C</> characters as comment start. Use C<\057\057> instead, |
|
|
41 | which works regardless of whether xrdb is used to parse the resource file |
|
|
42 | or not. |
|
|
43 | |
|
|
44 | =cut |
2 | |
45 | |
3 | sub upload_paste { |
46 | sub upload_paste { |
4 | my ($self) = @_; |
47 | my ($self) = @_; |
5 | |
48 | |
6 | require Digest::MD5; |
49 | require Digest::MD5; |
7 | |
50 | |
8 | my $txt = $self->selection; |
51 | my $txt = $self->selection; |
|
|
52 | |
|
|
53 | my $filename = $txt; |
|
|
54 | utf8::encode $filename; |
9 | my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; |
55 | $filename = Digest::MD5::md5_hex ($filename) . ".txt"; |
10 | |
56 | |
11 | my $tmpfile = "/tmp/$filename"; |
57 | my $tmpfile = "/tmp/$filename"; |
12 | |
58 | |
13 | my $msg = "uploaded as $filename"; |
59 | my $msg = "uploaded as $filename"; |
14 | |
60 | |
… | |
… | |
21 | } |
67 | } |
22 | |
68 | |
23 | my $cmd = $self->{pastebin_cmd}; |
69 | my $cmd = $self->{pastebin_cmd}; |
24 | $cmd =~ s/%/$tmpfile/; |
70 | $cmd =~ s/%/$tmpfile/; |
25 | |
71 | |
26 | my $pid = $self->exec_async ("/bin/sh", "-c", $cmd); |
72 | my $pid = $self->exec_async ($cmd); |
27 | |
73 | |
28 | $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub { |
74 | $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub { |
29 | my (undef, $status) = @_; |
75 | my (undef, $status) = @_; |
30 | |
76 | |
31 | delete $self->{pw}; |
77 | delete $self->{pw}; |
… | |
… | |
54 | } |
100 | } |
55 | |
101 | |
56 | sub on_start { |
102 | sub on_start { |
57 | my ($self) = @_; |
103 | my ($self) = @_; |
58 | |
104 | |
59 | $self->{pastebin_cmd} = $self->x_resource ("selection-pastebin.cmd") |
105 | $self->{pastebin_cmd} = $self->x_resource ("%.cmd") |
60 | || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
106 | || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
61 | |
107 | |
62 | $self->{pastebin_url} = $self->x_resource ("selection-pastebin.url") |
108 | $self->{pastebin_url} = $self->x_resource ("%.url") |
63 | || "http://www.ta-sa.org/files/txt/%"; |
109 | || "http://www.ta-sa.org/files/txt/%"; |
64 | |
110 | |
65 | push @{ $self->{term}{selection_popup_hook} }, sub { |
111 | push @{ $self->{term}{selection_popup_hook} }, sub { |
66 | ("pastebin upload" => sub { $self->upload_paste }) |
112 | ("pastebin upload" => sub { $self->upload_paste }) |
67 | }; |
113 | }; |
68 | |
114 | |
69 | () |
115 | () |
70 | } |
116 | } |
71 | |
117 | |
72 | sub on_user_command { |
118 | sub on_action { |
73 | my ($self, $cmd) = @_; |
119 | my ($self, $action) = @_; |
74 | |
120 | |
75 | if ($cmd eq "selection-pastebin:remote-pastebin") { |
121 | $action eq "remote-pastebin" |
76 | $self->upload_paste; |
122 | and $self->upload_paste; |
77 | } |
|
|
78 | |
123 | |
79 | () |
124 | () |
80 | } |
125 | } |
81 | |
126 | |