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 |
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 = $timers->{ov} = $self->overlay (-1, 0, length ($msg), 1, urxvt::OVERLAY_RSTYLE, 0); |
41 |
$ov->set (0, 0, $msg); |
42 |
|
43 |
$timers->{t1} = |
44 |
urxvt::timer |
45 |
->new |
46 |
->start ((int urxvt::NOW) + 5) # make sure we update "on" the second |
47 |
->interval (1) |
48 |
->cb (sub { delete $timers->{ov}; delete $timers->{t1}; }); |
49 |
} |
50 |
|
51 |
sub on_start { |
52 |
my ($self) = @_; |
53 |
$pastebin_cmd = $self->x_resource ("selection-pastebin.cmd") |
54 |
|| "scp -p % ruth:/var/www/www.ta-sa.org/files/txt/"; |
55 |
|
56 |
$pastebin_url = $self->x_resource ("selection-pastebin.url") |
57 |
|| "http://www.ta-sa.org/files/txt/%"; |
58 |
|
59 |
push @urxvt::ext::selection_popup::hook, sub { |
60 |
("pastebin upload" => sub { $self->upload_paste }) |
61 |
}; |
62 |
|
63 |
() |
64 |
} |
65 |
|
66 |
sub on_keyboard_command { |
67 |
my ($self, $cmd) = @_; |
68 |
|
69 |
if ($cmd eq "selection-pastebin:remote-pastebin") { |
70 |
$self->upload_paste; |
71 |
} |
72 |
|
73 |
() |
74 |
} |
75 |
|