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.1 by elmex, Tue Jan 17 16:53:47 2006 UTC vs.
Revision 1.19 by root, Sun Jun 10 13:58:06 2012 UTC

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