1 | #! perl |
1 | #! perl |
2 | |
|
|
3 | my $timers = {}; |
|
|
4 | my $pastebin_cmd; |
|
|
5 | my $pastebin_url; |
|
|
6 | |
|
|
7 | sub on_start { |
|
|
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 | |
|
|
13 | $pastebin_url = |
|
|
14 | $self->x_resource ("selection-pastebin.url") |
|
|
15 | or "http://www.ta-sa.org/files/txt/"; |
|
|
16 | |
|
|
17 | () |
|
|
18 | } |
|
|
19 | |
2 | |
20 | sub upload_paste { |
3 | sub upload_paste { |
21 | my ($self) = @_; |
4 | my ($self) = @_; |
22 | |
5 | |
23 | require Digest::MD5; |
6 | require Digest::MD5; |
… | |
… | |
25 | my $txt = $self->selection; |
8 | my $txt = $self->selection; |
26 | my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; |
9 | my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; |
27 | |
10 | |
28 | my $tmpfile = "/tmp/$filename"; |
11 | my $tmpfile = "/tmp/$filename"; |
29 | |
12 | |
30 | my $msg = "uploaded $filename"; |
13 | my $msg = "uploaded as $filename"; |
31 | |
14 | |
32 | if (open my $o, ">" . $tmpfile) { |
15 | if (open my $o, ">:utf8", $tmpfile) { |
|
|
16 | chmod 0644, $tmpfile; |
33 | print $o $txt; |
17 | print $o $txt; |
34 | close $o; |
18 | close $o; |
35 | } else { |
19 | } else { |
36 | $msg = "couldn't write $tmpfile: $!"; |
20 | $msg = "couldn't write $tmpfile: $!"; |
37 | } |
21 | } |
38 | |
22 | |
39 | my $cmd = $pastebin_cmd; |
23 | my $cmd = $self->{pastebin_cmd}; |
40 | $cmd =~ s/%/$tmpfile/; |
24 | $cmd =~ s/%/$tmpfile/; |
41 | |
25 | |
42 | if (system ($cmd) == 0) { |
26 | my $pid = $self->exec_async ("/bin/sh", "-c", $cmd); |
43 | my $url = $pastebin_url; |
|
|
44 | $url =~ s/%/$filename/; |
|
|
45 | |
27 | |
46 | $self->selection ($url); |
28 | $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub { |
47 | } else { |
29 | my (undef, $status) = @_; |
48 | $msg = "couldn't upload, '$cmd' failed"; |
|
|
49 | } |
|
|
50 | |
30 | |
51 | unlink $tmpfile; |
31 | delete $self->{pw}; |
52 | |
32 | |
53 | my $ov = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); |
33 | if ($status) { |
54 | $ov->set (0, 0, $msg); |
34 | $status >>= 8; |
|
|
35 | $msg = "ERROR: command returned status $status"; |
|
|
36 | } else { |
|
|
37 | my $url = $self->{pastebin_url}; |
|
|
38 | $url =~ s/%/$filename/; |
55 | |
39 | |
56 | $timers->{t1} = |
40 | $self->selection ($url); |
|
|
41 | } |
|
|
42 | |
|
|
43 | unlink $tmpfile; |
|
|
44 | |
|
|
45 | my $ov = $self->overlay (-1, 0, $self->strwidth ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); |
|
|
46 | $ov->set (0, 0, $msg); |
|
|
47 | |
|
|
48 | $self->{timer} = |
57 | urxvt::timer |
49 | urxvt::timer |
58 | ->new |
50 | ->new |
59 | ->start ((int urxvt::NOW) + 5) # make sure we update "on" the second |
|
|
60 | ->interval (1) |
51 | ->after (5) |
61 | ->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); |
52 | ->cb (sub { delete $self->{timer}; undef $ov; }); |
|
|
53 | }); |
62 | } |
54 | } |
63 | |
55 | |
64 | sub on_keyboard_command { |
56 | sub on_start { |
65 | my ($self, $cmd) = @_; |
57 | my ($self) = @_; |
66 | |
58 | |
67 | $cmd eq "selection-pastebin:remote-pastebin" |
59 | $self->{pastebin_cmd} = $self->x_resource ("selection-pastebin.cmd") |
68 | and upload_paste ($self); |
60 | || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
|
|
61 | |
|
|
62 | $self->{pastebin_url} = $self->x_resource ("selection-pastebin.url") |
|
|
63 | || "http://www.ta-sa.org/files/txt/%"; |
|
|
64 | |
|
|
65 | push @{ $self->{term}{selection_popup_hook} }, sub { |
|
|
66 | ("pastebin upload" => sub { $self->upload_paste }) |
|
|
67 | }; |
69 | |
68 | |
70 | () |
69 | () |
71 | } |
70 | } |
72 | |
71 | |
|
|
72 | sub on_user_command { |
|
|
73 | my ($self, $cmd) = @_; |
|
|
74 | |
|
|
75 | if ($cmd eq "selection-pastebin:remote-pastebin") { |
|
|
76 | $self->upload_paste; |
|
|
77 | } |
|
|
78 | |
|
|
79 | () |
|
|
80 | } |
|
|
81 | |