+$SIG{'HUP'} = \&sig_term;
+$SIG{'CHLD'} = \&sig_chld;
+$SIG{'ALRM'} = \&timeout;
+
+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 (iscallsign($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;
+ 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);
+ close STDIN;
+ close STDOUT;
+ close STDERR;
+}
+
+$mode = ($connsort eq 'ax25') ? 1 : 2;
+setmode();
+
+# adjust the callsign if it has an SSID, SSID <= 8 are legal > 8 are netrom connections
+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";
+}
+