Upgrade to Archive-Tar-1.34. Omitted re-addition of the Pod
[p5sagit/p5-mst-13.2.git] / lib / CPANPLUS / Shell / Default / Plugins / Remote.pm
1 package CPANPLUS::Shell::Default::Plugins::Remote;
2
3 use strict;
4
5 use Module::Load;
6 use Params::Check               qw[check];
7 use CPANPLUS::Error             qw[error msg];
8 use Locale::Maketext::Simple    Class => 'CPANPLUS', Style => 'gettext';
9
10 =head1 NAME
11
12 CPANPLUS::Shell::Default::Plugins::Remote
13
14 =head1 SYNOPSIS
15
16     CPAN Terminal> /connect localhost 1337 --user=foo --pass=bar
17     ...
18     CPAN Terminal@localhost> /disconnect
19
20 =head1 DESCRIPTION
21
22 This is a C<CPANPLUS::Shell::Default> plugin that allows you to connect
23 to a machine running an instance of C<CPANPLUS::Daemon>, allowing remote
24 usage of the C<CPANPLUS Shell>.
25
26 A sample session, updating all modules on a remote machine, might look
27 like this:
28
29     CPAN Terminal> /connect --user=my_user --pass=secret localhost 1337
30
31     Connection accepted
32     
33     Successfully connected to 'localhost' on port '11337'
34     
35     Note that no output will appear until a command has completed
36     -- this may take a while
37
38
39     CPAN Terminal@localhost> o; i *
40     
41     [....]
42     
43     CPAN Terminal@localhost> /disconnect
44
45     CPAN Terminal>
46
47 =cut
48
49 ### store the original prompt here, so we can restore it on disconnect
50 my $Saved_Prompt;
51
52 sub plugins { ( connect => 'connect', disconnect => 'disconnect' ) }
53
54 sub connect {
55     my $class   = shift;
56     my $shell   = shift;
57     my $cb      = shift;
58     my $cmd     = shift;
59     my $input   = shift || '';
60     my $opts    = shift || {};
61     my $conf = $cb->configure_object;
62
63     my $user; my $pass;
64     {   local $Params::Check::ALLOW_UNKNOWN = 1;
65
66         my $tmpl = {
67             user => { default => $conf->_get_daemon('username'),
68                         store => \$user },
69             pass => { default => $conf->_get_daemon('password'),
70                         store => \$pass },
71         };
72
73          check( $tmpl, $opts ) or return;
74     }
75
76     my @parts = split /\s+/, $input;
77     my $host = shift @parts || 'localhost';
78     my $port = shift @parts || $conf->_get_daemon('port');
79
80     load IO::Socket;
81
82     my $remote = IO::Socket::INET->new(
83                         Proto       => "tcp",
84                         PeerAddr    => $host,
85                         PeerPort    => $port,
86                     ) or (
87                         error( loc( "Cannot connect to port '%1' ".
88                                     "on host '%2'", $port, $host ) ),
89                         return
90                     );
91
92     my $con = {
93         connection  => $remote,
94         username    => $user,
95         password    => $pass,
96     };
97
98     ### store the connection
99     $shell->remote( $con );
100
101     my($status,$buffer) = $shell->__send_remote_command(
102                             "VERSION=$CPANPLUS::Shell::Default::VERSION");
103
104     if( $status ) {
105         print "\n$buffer\n\n";
106
107         print loc(  "Successfully connected to '%1' on port '%2'",
108                     $host, $port );
109         print "\n\n";
110         print loc(  "Note that no output will appear until a command ".
111                     "has completed\n-- this may take a while" );
112         print "\n\n";
113
114         ### save the original prompt
115         $Saved_Prompt = $shell->prompt;
116
117         $shell->prompt( $shell->brand .'@'. $host .'> ' );
118
119     } else {
120         print "\n$buffer\n\n";
121
122         print loc(  "Failed to connect to '%1' on port '%2'",
123                     $host, $port );
124         print "\n\n";
125
126         $shell->remote( undef );
127     }
128 }
129
130 sub disconnect {
131     my $class   = shift;
132     my $shell   = shift;
133
134     print "\n", ( $shell->remote
135                     ? loc( "Disconnecting from remote host" )
136                     : loc( "Not connected to remote host" )
137             ), "\n\n";
138
139     $shell->remote( undef );
140     $shell->prompt( $Saved_Prompt );
141 }
142
143 sub connect_help {
144     return loc( 
145             "    /connect [HOST PORT]   # Connect to the remote machine,\n" .
146             "                           # defaults taken from your config\n" .
147             "        --user=USER        # Optional username\n" .
148             "        --pass=PASS        # Optional password" );
149 }
150
151 sub disconnect_help {
152     return loc(
153             "    /disconnect            # Disconnect from the remote server" );
154 }
155
156 1; 
157         
158 =pod
159
160 =head1 BUG REPORTS
161
162 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
163
164 =head1 AUTHOR
165
166 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
167
168 =head1 COPYRIGHT
169
170 The CPAN++ interface (of which this module is a part of) is copyright (c) 
171 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
172
173 This library is free software; you may redistribute and/or modify it 
174 under the same terms as Perl itself.
175
176 =head1 SEE ALSO
177
178 L<CPANPLUS::Shell::Default>, L<CPANPLUS::Shell>, L<cpanp>
179
180 =cut
181
182 # Local variables:
183 # c-indentation-style: bsd
184 # c-basic-offset: 4
185 # indent-tabs-mode: nil
186 # End:
187 # vim: expandtab shiftwidth=4:
188