summaryrefslogtreecommitdiff
path: root/conmux/console
blob: f031e5b2c594ce1eb88ad31355a11fc9dfdcc6b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/perl
#
# console <host>/<machine> -- interactive client interface
#
# The main interactive client interace to conmux.  Allows direct
# interaction with the payload, as well as allowing break out
# to the conmux menu to envoke defined commands; for example
# hardreset.
#
# (C) Copyright IBM Corp. 2004, 2005, 2006
# Author: Andy Whitcroft <andyw@uk.ibm.com>
#
# The Console Multiplexor is released under the GNU Public License V2
#
$| = 1;

our $P = 'console';

use FindBin;
use POSIX qw(errno_h BUFSIZ);
use IO::Socket;
use Getopt::Long qw(:config no_auto_abbrev);

my $CONMUX = $FindBin::Bin;
my $CONMUX = $ENV{'CONMUX_ROOT'} if ($ENV{'CONMUX_ROOT'});

# Find our internal libraries.
use lib $FindBin::Bin;
use lib "$FindBin::Bin/../lib/";
use lib "$FindBin::Bin/lib/";
use Conmux;

# Basic terminal handling.
sub termRaw {
	$termSettings = `stty -g`;
	system "stty raw -echo opost onlret";
}
sub termRestore {
	system "stty $termSettings";
}

my $bot;
my $list;
my $status;
GetOptions(
        'b|bot=s'	=> \$bot,
	'l|list'	=> \$list,
	's|status'	=> \$status,
);
sub usage {
	warn "Usage: $P <service>\n";
	warn "       $P <registry>/<service>\n";
	warn "       $P <host>:<port>\n";
	warn "       $P --status <service>\n";
	die  "       $P --list [<registry>]\n";
}

my $id;
if ($bot) {
	$id = 'bot:' . $bot;
} else {
	$id = 'user:' . $ENV{'LOGNAME'};
}

#
# MODE: registry list.
#
if ($list) {
	if ($#ARGV == -1) {
		print Conmux::Registry::list('-');

	} elsif ($#ARGV == 0) {
		print Conmux::Registry::list($ARGV[0]);

	} else {
		usage();
	}
	exit 0
}

#
# COMMAND: payload status command
#
if ($status) {
	usage() if ($#ARGV != 0);

	my $sock;
	eval {
		$sock = Conmux::connect($ARGV[0]);
	};
	if ($@) {
		print "unavailable\n";
		exit 0
	}
	my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id,
		'to' => 'console', 'hide' => 1 });
	if ($r{'status'} ne 'OK') {
		print "unavailable\n";

	} elsif ($r{'state'}) {
		print "$r{'state'}\n";

	} else {
		print "unknown\n";
	}
	exit 0;
}


#
# COMMAND: general payload connect.
#
if ($#ARGV != 0) {
	usage();
}

# Connect to the host/port specified on the command line,
# or localhost:23
my $sock = Conmux::connect($ARGV[0]);

my %r = Conmux::sendCmd($sock, 'CONNECT', { 'id' => $id, 'to' => 'console' });
die "$P: $ARGV[0]: connect failed - $r{'status'}\n" if ($r{'status'} ne 'OK');

print "Connected to $r{'title'} (~\$quit to exit)\n";
#display message of the day passed by the server e.g. who is already connected
print "$r{'motd'}";

my $rin = $win = $ein = '';
vec($rin, fileno(STDIN), 1) = 1;
vec($rin, fileno($sock), 1) = 1;
my $ein = $rin | $win;

# We want to buffer output to the terminal.  This prevents the program
# from blocking if the user hits CTRL-S for example.
# XXX ^^^

termRaw();

# Loop waiting for input from the user, or from the server.
while (1) {
	$nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
	if ($nfound < 0) {
		if ($! == EINTR || $! == EAGAIN) {
			next;
		}
		die "$P: select failed - $!\n";
	}

	if (vec($rout, fileno(STDIN), 1)) {
		$len = sysread(STDIN, $data, 4096);
		if ($len <= 0) {
			if ($len < 0) {
				if ($! == EINTR || $! == EAGAIN) {
					next;
				}
				die "$P: STDIN: read failed - $!\n";
			}
			print STDERR "Connection Closed (client)\n";
			last;
		}
		print $sock $data;
	}
	if (vec($rout, fileno($sock), 1)) {
		$len = sysread($sock, $data, 4096);
		if ($len <= 0) {
			if ($len < 0) {
				if ($! == EINTR || $! == EAGAIN) {
					next;
				}
				die "$P: server: read failed - $!\n";
			}
			print STDERR "Connection Closed (server)\n";
			last;
		}
		print $data;
	}
}

termRestore();