1 |
elmex |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
sub upload_paste { |
4 |
|
|
my ($self) = @_; |
5 |
|
|
|
6 |
root |
1.2 |
require Digest::MD5; |
7 |
elmex |
1.1 |
|
8 |
|
|
my $txt = $self->selection; |
9 |
root |
1.2 |
my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; |
10 |
elmex |
1.1 |
|
11 |
|
|
my $tmpfile = "/tmp/$filename"; |
12 |
|
|
|
13 |
root |
1.12 |
my $msg = "uploaded as $filename"; |
14 |
elmex |
1.1 |
|
15 |
root |
1.14 |
if (open my $o, ">:utf8", $tmpfile) { |
16 |
root |
1.6 |
chmod 0644, $tmpfile; |
17 |
elmex |
1.1 |
print $o $txt; |
18 |
|
|
close $o; |
19 |
|
|
} else { |
20 |
|
|
$msg = "couldn't write $tmpfile: $!"; |
21 |
|
|
} |
22 |
|
|
|
23 |
root |
1.14 |
my $cmd = $self->{pastebin_cmd}; |
24 |
elmex |
1.1 |
$cmd =~ s/%/$tmpfile/; |
25 |
|
|
|
26 |
root |
1.14 |
my $pid = $self->exec_async ("/bin/sh", "-c", $cmd); |
27 |
elmex |
1.1 |
|
28 |
root |
1.14 |
$self->{pw} = urxvt::pw->new->start ($pid)->cb (sub { |
29 |
|
|
my (undef, $status) = @_; |
30 |
elmex |
1.4 |
|
31 |
root |
1.14 |
delete $self->{pw}; |
32 |
elmex |
1.1 |
|
33 |
root |
1.14 |
if ($status) { |
34 |
|
|
$status >>= 8; |
35 |
|
|
$msg = "ERROR: command returned status $status"; |
36 |
|
|
} else { |
37 |
|
|
my $url = $self->{pastebin_url}; |
38 |
|
|
$url =~ s/%/$filename/; |
39 |
|
|
|
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} = |
49 |
|
|
urxvt::timer |
50 |
|
|
->new |
51 |
|
|
->after (5) |
52 |
|
|
->cb (sub { delete $self->{timer}; undef $ov; }); |
53 |
|
|
}); |
54 |
elmex |
1.1 |
} |
55 |
|
|
|
56 |
root |
1.5 |
sub on_start { |
57 |
|
|
my ($self) = @_; |
58 |
root |
1.10 |
|
59 |
root |
1.14 |
$self->{pastebin_cmd} = $self->x_resource ("selection-pastebin.cmd") |
60 |
|
|
|| "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
61 |
root |
1.5 |
|
62 |
root |
1.14 |
$self->{pastebin_url} = $self->x_resource ("selection-pastebin.url") |
63 |
|
|
|| "http://www.ta-sa.org/files/txt/%"; |
64 |
root |
1.5 |
|
65 |
root |
1.10 |
push @{ $self->{term}{selection_popup_hook} }, sub { |
66 |
|
|
("pastebin upload" => sub { $self->upload_paste }) |
67 |
|
|
}; |
68 |
|
|
|
69 |
root |
1.5 |
() |
70 |
|
|
} |
71 |
|
|
|
72 |
elmex |
1.1 |
sub on_keyboard_command { |
73 |
|
|
my ($self, $cmd) = @_; |
74 |
|
|
|
75 |
root |
1.5 |
if ($cmd eq "selection-pastebin:remote-pastebin") { |
76 |
|
|
$self->upload_paste; |
77 |
|
|
} |
78 |
elmex |
1.1 |
|
79 |
|
|
() |
80 |
|
|
} |
81 |
|
|
|