re-arrange ExtMsg a bit to cope with $sock->peerhost and port failures
[spider.git] / perl / ExtMsg.pm
index 3f8159aa54699357bff0ac074a4a9235928a2698..4efb3484c0621d58c90631ab247ff6c67d87304a 100644 (file)
@@ -90,7 +90,9 @@ sub dequeue
                        } elsif ($conn->{state} eq 'WL' ) {
                                $msg = uc $msg;
                                if (is_callsign($msg)) {
-                                       $conn->to_connected($msg, 'A', $conn->{csort});
+                                       my $sort = $conn->{csort};
+                                       $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
+                                       $conn->to_connected($msg, 'A', $sort);
                                } else {
                                        $conn->send_now("Sorry $msg is an invalid callsign");
                                        $conn->disconnect;
@@ -127,28 +129,35 @@ sub new_client {
                $conn->{sock} = $sock;
                Msg::blocking($sock, 0);
                $conn->{blocking} = 0;
-               
-               my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
-               dbg('connll', "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}");
-               if ($eproc) {
-                       $conn->{eproc} = $eproc;
-                       Msg::set_event_handler ($sock, "error" => $eproc);
-               }
-               if ($rproc) {
-                       $conn->{rproc} = $rproc;
-                       my $callback = sub {$conn->_rcv};
-                       Msg::set_event_handler ($sock, "read" => $callback);
-                       # send login prompt
-                       $conn->{state} = 'WL';
-                       #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
-                       #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
-                       #               $conn->send_raw("\xFF\xFC\x01");
-                       $conn->_send_file("$main::data/issue");
-                       $conn->send_raw("login: ");
-                       $conn->_dotimeout(60);
-               } else { 
-                       &{$conn->{eproc}}() if $conn->{eproc};
-                       $conn->disconnect();
+               eval {$conn->{peerhost} = $sock->peerhost};
+               if ($@) {
+                       dbg('conn', $@);
+                       $conn->disconnect;
+               } else {
+                       eval {$conn->{peerport} = $sock->peerport};
+                       $conn->{peerport} = 0 if $@;
+                       my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
+                       dbg('connll', "accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}");
+                       if ($eproc) {
+                               $conn->{eproc} = $eproc;
+                               Msg::set_event_handler ($sock, "error" => $eproc);
+                       }
+                       if ($rproc) {
+                               $conn->{rproc} = $rproc;
+                               my $callback = sub {$conn->_rcv};
+                               Msg::set_event_handler ($sock, "read" => $callback);
+                               # send login prompt
+                               $conn->{state} = 'WL';
+                               #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
+                               #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
+                               #               $conn->send_raw("\xFF\xFC\x01");
+                               $conn->_send_file("$main::data/issue");
+                               $conn->send_raw("login: ");
+                               $conn->_dotimeout(60);
+                       } else { 
+                               &{$conn->{eproc}}() if $conn->{eproc};
+                               $conn->disconnect();
+                       }
                }
        } else {
                dbg('err', "ExtMsg: error on accept ($!)");