1 |
#! perl |
2 |
|
3 |
sub upload_paste { |
4 |
my ($self) = @_; |
5 |
|
6 |
require Digest::MD5; |
7 |
|
8 |
my $txt = $self->selection; |
9 |
my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; |
10 |
|
11 |
my $tmpfile = "/tmp/$filename"; |
12 |
|
13 |
my $msg = "uploaded as $filename"; |
14 |
|
15 |
if (open my $o, ">:utf8", $tmpfile) { |
16 |
chmod 0644, $tmpfile; |
17 |
print $o $txt; |
18 |
close $o; |
19 |
} else { |
20 |
$msg = "couldn't write $tmpfile: $!"; |
21 |
} |
22 |
|
23 |
my $cmd = $self->{pastebin_cmd}; |
24 |
$cmd =~ s/%/$tmpfile/; |
25 |
|
26 |
my $pid = $self->exec_async ("/bin/sh", "-c", $cmd); |
27 |
|
28 |
$self->{pw} = urxvt::pw->new->start ($pid)->cb (sub { |
29 |
my (undef, $status) = @_; |
30 |
|
31 |
delete $self->{pw}; |
32 |
|
33 |
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 |
} |
55 |
|
56 |
sub on_start { |
57 |
my ($self) = @_; |
58 |
|
59 |
$self->{pastebin_cmd} = $self->x_resource ("selection-pastebin.cmd") |
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 |
}; |
68 |
|
69 |
() |
70 |
} |
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 |
|