ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
Revision: 1.1
Committed: Tue Jan 17 16:53:47 2006 UTC (18 years, 4 months ago) by elmex
Branch: MAIN
Log Message:
moved pastebin code from selection to src/perl/selection-pastebin and corrected documentation

File Contents

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