Move CPANPLUS from lib/ to ext/
[p5sagit/p5-mst-13.2.git] / ext / CPANPLUS / 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   => 'cpanpd',    store => \$user },
68             pass => { required  => 1,           store => \$pass },
69         };
70
71          check( $tmpl, $opts ) or return;
72     }
73
74     my @parts = split /\s+/, $input;
75     my $host = shift @parts || 'localhost';
76     my $port = shift @parts || '1337';
77
78     load IO::Socket;
79
80     my $remote = IO::Socket::INET->new(
81                         Proto       => "tcp",
82                         PeerAddr    => $host,
83                         PeerPort    => $port,
84                     ) or (
85                         error( loc( "Cannot connect to port '%1' ".
86                                     "on host '%2'", $port, $host ) ),
87                         return
88                     );
89
90     my $con = {
91         connection  => $remote,
92         username    => $user,
93         password    => $pass,
94     };
95
96     ### store the connection
97     $shell->remote( $con );
98
99     my($status,$buffer) = $shell->__send_remote_command(
100                             "VERSION=$CPANPLUS::Shell::Default::VERSION");
101
102     if( $status ) {
103         print "\n$buffer\n\n";
104
105         print loc(  "Successfully connected to '%1' on port '%2'",
106                     $host, $port );
107         print "\n\n";
108         print loc(  "Note that no output will appear until a command ".
109                     "has completed\n-- this may take a while" );
110         print "\n\n";
111
112         ### save the original prompt
113         $Saved_Prompt = $shell->prompt;
114
115         $shell->prompt( $shell->brand .'@'. $host .':'. $port .'> ' );
116
117     } else {
118         print "\n$buffer\n\n";
119
120         print loc(  "Failed to connect to '%1' on port '%2'",
121                     $host, $port );
122         print "\n\n";
123
124         $shell->remote( undef );
125     }
126 }
127
128 sub disconnect {
129     my $class   = shift;
130     my $shell   = shift;
131
132     print "\n", ( $shell->remote
133                     ? loc( "Disconnecting from remote host" )
134                     : loc( "Not connected to remote host" )
135             ), "\n\n";
136
137     $shell->remote( undef );
138     $shell->prompt( $Saved_Prompt );
139 }
140
141 sub connect_help {
142     return loc( 
143             "    /connect [HOST PORT]   # Connect to the remote machine,\n" .
144             "                           # defaults taken from your config\n" .
145             "        --user=USER        # Optional username\n" .
146             "        --pass=PASS        # Optional password" );
147 }
148
149 sub disconnect_help {
150     return loc(
151             "    /disconnect            # Disconnect from the remote server" );
152 }
153
154 1; 
155         
156 =pod
157
158 =head1 BUG REPORTS
159
160 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
161
162 =head1 AUTHOR
163
164 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
165
166 =head1 COPYRIGHT
167
168 The CPAN++ interface (of which this module is a part of) is copyright (c) 
169 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
170
171 This library is free software; you may redistribute and/or modify it 
172 under the same terms as Perl itself.
173
174 =head1 SEE ALSO
175
176 L<CPANPLUS::Shell::Default>, L<CPANPLUS::Shell>, L<cpanp>
177
178 =cut
179
180 # Local variables:
181 # c-indentation-style: bsd
182 # c-basic-offset: 4
183 # indent-tabs-mode: nil
184 # End:
185 # vim: expandtab shiftwidth=4:
186