ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/perl/selection-pastebin
(Generate patch)

Comparing rxvt-unicode/src/perl/selection-pastebin (file contents):
Revision 1.2 by root, Tue Jan 17 16:57:07 2006 UTC vs.
Revision 1.19 by root, Sun Jun 10 13:58:06 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines