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