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