ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
Revision: 1.4
Committed: Tue Jan 17 17:04:18 2006 UTC (18 years, 4 months ago) by elmex
Branch: MAIN
Changes since 1.3: +2 -0 lines
Log Message:
fixed a bug, the textfile is now deleted after upload.

File Contents

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