1 |
#! perl |
2 |
|
3 |
my $pastebin_cmd; |
4 |
my $pastebin_url; |
5 |
|
6 |
sub upload_paste { |
7 |
my ($self) = @_; |
8 |
|
9 |
require Digest::MD5; |
10 |
|
11 |
my $txt = $self->selection; |
12 |
my $filename = Digest::MD5::md5_hex ($txt) . ".txt"; |
13 |
|
14 |
my $tmpfile = "/tmp/$filename"; |
15 |
|
16 |
my $msg = "uploaded $filename"; |
17 |
|
18 |
if (open my $o, ">", $tmpfile) { |
19 |
chmod 0644, $tmpfile; |
20 |
print $o $txt; |
21 |
close $o; |
22 |
} else { |
23 |
$msg = "couldn't write $tmpfile: $!"; |
24 |
} |
25 |
|
26 |
my $cmd = $pastebin_cmd; |
27 |
$cmd =~ s/%/$tmpfile/; |
28 |
|
29 |
if (system ($cmd) == 0) { |
30 |
my $url = $pastebin_url; |
31 |
$url =~ s/%/$filename/; |
32 |
|
33 |
$self->selection ($url); |
34 |
} else { |
35 |
$msg = "couldn't upload, '$cmd' failed"; |
36 |
} |
37 |
|
38 |
unlink $tmpfile; |
39 |
|
40 |
my $ov = $self->overlay (-1, 0, length $msg, 1, urxvt::OVERLAY_RSTYLE, 0); |
41 |
$ov->set (0, 0, $msg); |
42 |
|
43 |
my $timer; $timer = |
44 |
urxvt::timer |
45 |
->new |
46 |
->after (5) |
47 |
->cb (sub {undef $timer; undef $ov; }); |
48 |
} |
49 |
|
50 |
sub on_start { |
51 |
my ($self) = @_; |
52 |
$pastebin_cmd = $self->x_resource ("selection-pastebin.cmd") |
53 |
|| "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
54 |
|
55 |
$pastebin_url = $self->x_resource ("selection-pastebin.url") |
56 |
|| "http://www.ta-sa.org/files/txt/%"; |
57 |
|
58 |
push @urxvt::ext::selection_popup::hook, sub { |
59 |
("pastebin upload" => sub { $self->upload_paste }) |
60 |
}; |
61 |
|
62 |
() |
63 |
} |
64 |
|
65 |
sub on_keyboard_command { |
66 |
my ($self, $cmd) = @_; |
67 |
|
68 |
if ($cmd eq "selection-pastebin:remote-pastebin") { |
69 |
$self->upload_paste; |
70 |
} |
71 |
|
72 |
() |
73 |
} |
74 |
|