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