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