ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
Revision: 1.19
Committed: Sun Jun 10 13:58:06 2012 UTC (11 years, 11 months ago) by root
Branch: MAIN
Changes since 1.18: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 #! perl
2    
3 root 1.18 #:META:X_RESOURCE:%.cmd:string:the command to run create a new pastebin
4     #:META:X_RESOURCE:%.url:string:the url template for new pastebins
5    
6 elmex 1.1 sub upload_paste {
7     my ($self) = @_;
8    
9 root 1.2 require Digest::MD5;
10 elmex 1.1
11     my $txt = $self->selection;
12 root 1.16
13     my $filename = $txt;
14     utf8::encode $filename;
15     $filename = Digest::MD5::md5_hex ($filename) . ".txt";
16 elmex 1.1
17     my $tmpfile = "/tmp/$filename";
18    
19 root 1.12 my $msg = "uploaded as $filename";
20 elmex 1.1
21 root 1.14 if (open my $o, ">:utf8", $tmpfile) {
22 root 1.6 chmod 0644, $tmpfile;
23 elmex 1.1 print $o $txt;
24     close $o;
25     } else {
26     $msg = "couldn't write $tmpfile: $!";
27     }
28    
29 root 1.14 my $cmd = $self->{pastebin_cmd};
30 elmex 1.1 $cmd =~ s/%/$tmpfile/;
31    
32 sf-exg 1.17 my $pid = $self->exec_async ($cmd);
33 elmex 1.1
34 root 1.14 $self->{pw} = urxvt::pw->new->start ($pid)->cb (sub {
35     my (undef, $status) = @_;
36 elmex 1.4
37 root 1.14 delete $self->{pw};
38 elmex 1.1
39 root 1.14 if ($status) {
40     $status >>= 8;
41     $msg = "ERROR: command returned status $status";
42     } else {
43     my $url = $self->{pastebin_url};
44     $url =~ s/%/$filename/;
45    
46     $self->selection ($url);
47     }
48    
49     unlink $tmpfile;
50    
51     my $ov = $self->overlay (-1, 0, $self->strwidth ($msg), 1, urxvt::OVERLAY_RSTYLE, 0);
52     $ov->set (0, 0, $msg);
53    
54     $self->{timer} =
55     urxvt::timer
56     ->new
57     ->after (5)
58     ->cb (sub { delete $self->{timer}; undef $ov; });
59     });
60 elmex 1.1 }
61    
62 root 1.5 sub on_start {
63     my ($self) = @_;
64 root 1.10
65 root 1.19 $self->{pastebin_cmd} = $self->x_resource ("%.cmd")
66 root 1.14 || "rcp -p % ruth:/var/www/www.ta-sa.org/files/txt/";
67 root 1.5
68 root 1.19 $self->{pastebin_url} = $self->x_resource ("%.url")
69 root 1.14 || "http://www.ta-sa.org/files/txt/%";
70 root 1.5
71 root 1.10 push @{ $self->{term}{selection_popup_hook} }, sub {
72     ("pastebin upload" => sub { $self->upload_paste })
73     };
74    
75 root 1.5 ()
76     }
77    
78 root 1.15 sub on_user_command {
79 elmex 1.1 my ($self, $cmd) = @_;
80    
81 root 1.5 if ($cmd eq "selection-pastebin:remote-pastebin") {
82     $self->upload_paste;
83     }
84 elmex 1.1
85     ()
86     }
87