+# handle callsign and connection type firtling
+sub doclient
+{
+ my $line = shift;
+ my @f = split /\s+/, $line;
+ $call = uc $f[0] if $f[0];
+ $csort = $f[1] if $f[1];
+}
+
+#
+# initialisation
+#
+
+$mode = 2; # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
+$call = ""; # the callsign being used
+$conn = 0; # the connection object for the cluster
+$lastbit = ""; # the last bit of an incomplete input line
+$mynl = "\n"; # standard terminator
+$lasttime = time; # lasttime something happened on the interface
+$outqueue = ""; # the output queue
+$client_buffer_lth = 200; # how many characters are buffered up on outqueue
+$buffered = 1; # buffer output
+$savenl = ""; # an NL that has been saved from last time
+$timeout = 60; # default timeout for connects
+$abort = ""; # the current abort string
+$cpath = "$root/connect"; # the basic connect directory
+$maxecho = 5; # length of max echo queue
+
+$pid = 0; # the pid of the child program
+$csort = ""; # the connection type
+$sock = 0; # connection socket
+$out_lineend = $mynl; # connection lineending (used for outgoing connects)
+
+$stdin = *STDIN;
+$stdout = *STDOUT;
+$rfh = 0;
+$wfh = 0;
+
+$waitedpid = 0;
+
+#
+# deal with args
+#
+
+$call = uc shift @ARGV if @ARGV;
+$call = uc $myalias if !$call;
+$connsort = lc shift @ARGV if @ARGV;
+$connsort = 'local' if !$connsort;
+
+$loginreq = $call eq 'LOGIN';
+
+# we will do this again later 'cos things may have changed
+$mode = ($connsort eq 'ax25') ? 1 : 2;
+setmode();
+
+if ($call eq $mycall) {
+ print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
+ cease(0);
+}
+
+$stdout->autoflush(1);
+
+unless ($^O =~ /^MS/i) {
+ $SIG{'INT'} = \&sig_term;
+ $SIG{'TERM'} = \&sig_term;
+ $SIG{'HUP'} = \&sig_term;
+ $SIG{'CHLD'} = \&sig_chld;
+}
+
+dbgadd('connect');
+
+# do we need to do a login and password job?
+if ($loginreq) {
+ my $user;
+ my $s;
+
+ $connsort = 'telnet' if $connsort eq 'local';
+ setmode();
+
+ if (-e "$data/issue") {
+ open(I, "$data/issue") or die;
+ local $/ = undef;
+ $issue = <I>;
+ close(I);
+ $issue = s/\n/\r/og if $mode == 1;
+ local $/ = $nl;
+ $stdout->print($issue) if $issue;
+ }
+
+ # allow a login from an existing user. I could create a user but
+ # I want to check for valid callsigns and I don't have the
+ # necessary info / regular expression yet
+ alarm($timeout);
+
+ $stdout->print('login: ');
+ $stdout->flush();
+ local $\ = $mynl;
+ $s = $stdin->getline();
+ chomp $s;
+ $s =~ s/\s+//og;
+ $s =~ s/-\d+$//o; # no ssids!
+ cease(0) unless $s && $s gt ' ';
+ unless (is_callsign($s)) {
+ $stdout->print("Sorry, $s is an invalid callsign");
+ cease(0);
+ }
+ $call = uc $s;
+ alarm(0);
+}
+
+# is this an out going connection?
+if ($connsort eq "connect") {
+ my $mcall = lc $call;
+
+ open(IN, "$cpath/$mcall") or cease(2);
+ @in = <IN>;
+ close IN;
+
+ alarm($timeout);
+
+ for (@in) {
+ chomp;
+ next if /^\s*\#/o;
+ next if /^\s*$/o;
+ doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
+ doabort($1) if /^\s*a\w*\s+(.*)/io;
+ dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
+ dolineend($1) if /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/;
+ dochat($1, $2) if /^\s*\'(.*)\'\s+\'(.*)\'/io;
+
+ if (/^\s*cl\w+\s+(.*)/io) {
+ doclient($1);
+ last;
+ }
+ }
+
+ dbg('connect', "Connected to $call ($csort), starting normal protocol");
+ dbgsub('connect');
+
+ # if we get here we are connected
+ if ($csort eq 'ax25' || $csort eq 'prog') {
+ # open(STDIN, "<&R");
+ # open(STDOUT, ">&W");
+ # close R;
+ # close W;
+ $stdin = $rfh;
+ $stdout = $wfh;
+ $csort = 'telnet' if $csort eq 'prog';
+ } elsif ($csort eq 'telnet') {
+ # open(STDIN, "<&$sock");
+ # open(STDOUT, ">&$sock");
+ # close $sock;
+ $stdin = $sock;
+ $stdout = $sock;
+ }
+ alarm(0);
+ $outbound = 1;
+ $connsort = $csort;
+ $stdout->autoflush(1);
+ $mode = ($connsort eq 'ax25') ? 1 : $mode;
+ close STDIN;
+ close STDOUT;
+ close STDERR;
+}
+
+setmode();
+
+# adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
+$call =~ s/-0$//; # strip off -0 as this is equiv to just call on its own
+my ($scall, $ssid) = split /-/, $call;
+$ssid = undef unless $ssid && $ssid =~ /^\d+$/;
+if ($ssid) {
+ $ssid = 15 if $ssid > 15;
+ if ($connsort eq 'ax25') {
+ if ($ssid > 8) {
+ $ssid = 15 - $ssid;
+ }
+ }
+ $call = "$scall-$ssid";
+}
+
+
+$conn = IntMsg->connect("$clusteraddr", $clusterport, \&rec_socket);
+if (! $conn) {
+ if (-r "$data/offline") {
+ open IN, "$data/offline" or die;
+ while (<IN>) {
+ s/\n/\r/og if $mode == 1;
+ print $stdout $_;
+ }
+ close IN;
+ } else {
+ print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
+ }
+ cease(0);
+}
+
+$let = $outbound ? 'O' : 'A';
+$conn->send_now("$let$call|$connsort");
+Msg->set_event_handler($stdin, "read" => \&rec_stdin);
+
+for (;;) {
+ my $t;
+ Msg->event_loop(1, 0.1);
+ $t = time;
+ if ($t > $lasttime) {
+ if ($outqueue) {
+ print $stdout $outqueue;
+ $outqueue = "";
+ }
+ $lasttime = $t;
+ }
+}