+sub dochat
+{
+ my ($expect, $send) = @_;
+ dbg('connect', "CHAT \"$expect\" -> \"$send\"");
+ my $line;
+
+ alarm($timeout);
+
+ if ($expect) {
+ for (;;) {
+ if ($csort eq 'telnet') {
+ $line = $sock->get();
+ cease(11) unless $line; # the socket has gone away?
+ $line =~ s/\r\n/\n/og;
+ chomp;
+ } elsif ($csort eq 'ax25' || $csort eq 'prog') {
+ local $/ = "\r";
+ $line = <$rfh>;
+ $line =~ s/\r//og;
+ }
+ if (length $line == 0) {
+ dbg('connect', "received 0 length line, aborting...");
+ cease(11);
+ }
+ dbg('connect', "received \"$line\"");
+ if ($abort && $line =~ /$abort/i) {
+ dbg('connect', "aborted on /$abort/");
+ cease(11);
+ }
+ last if $line =~ /$expect/i;
+ }
+ }
+ if ($send) {
+ if ($csort eq 'telnet') {
+ $sock->print("$send\n");
+ } elsif ($csort eq 'ax25') {
+ local $\ = "\r";
+ $wfh->print("$send");
+ }
+ dbg('connect', "sent \"$send\"");
+ }
+}
+
+sub timeout
+{
+ dbg('connect', "timed out after $timeout seconds");
+ cease(0);
+}
+
+
+#
+# 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
+
+$pid = 0; # the pid of the child program
+$csort = ""; # the connection type
+$sock = 0; # connection socket
+
+$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;