backport mojo console.pl width/scolling changes
[spider.git] / perl / DXCommandmode.pm
index a00e0be808a9d19afb85db07f9f2b3da7c1b54a0..c8decb72e1b3b5c39e34e2fd3f283d0f23741b54 100644 (file)
@@ -32,14 +32,14 @@ use WCY;
 use Sun;
 use Internet;
 use Script;
-use Net::Telnet;
 use QSL;
 use DB_File;
 use VE7CC;
 use DXXml;
+use AsyncMsg;
 
 use strict;
-use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %nothereslug
+use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase %nothereslug
        $maxbadcount $msgpolltime $default_pagelth $cmdimportdir);
 
 %Cache = ();                                   # cache of dynamically loaded routine's mod times
@@ -47,12 +47,11 @@ use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %notheresl
 $errstr = ();                                  # error string from eval
 %aliases = ();                                 # aliases for (parts of) commands
 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
-$maxerrors = 20;                               # the maximum number of concurrent errors allowed before disconnection
 $maxbadcount = 3;                              # no of bad words allowed before disconnection
 $msgpolltime = 3600;                   # the time between polls for new messages 
 $cmdimportdir = "$main::root/cmd_import"; # the base directory for importing command scripts 
                                           # this does not exist as default, you need to create it manually
-                                         #
+#
 
 #
 # obtain a new connection this is derived from dxchannel
@@ -66,7 +65,7 @@ sub new
        my $pkg = shift;
        my $call = shift;
 #      my @rout = $main::routeroot->add_user($call, Route::here(1));
-       DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1]);
+       DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->{conn}->peerhost], );
 
        # ALWAYS output the user
        my $ref = Route::User::get($call);
@@ -90,7 +89,7 @@ sub start
        my $name = $user->{name};
        
        # log it
-       my $host = $self->{conn}->{peerhost};
+       my $host = $self->{conn}->peerhost;
        $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
        $host ||= "unknown";
        LogDbg('DXCommand', "$call connected from $host");
@@ -134,23 +133,8 @@ sub start
                $self->{registered} = 1;
        }
 
-
-       # decide which motd to send
-       my $motd;
-       unless ($self->{registered}) {
-               $motd = "${main::motd}_nor_$self->{lang}";
-               $motd = "${main::motd}_nor" unless -e $motd;
-       }
-       $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
-       $motd = $main::motd unless $motd && -e $motd;
-       if ($self->conn->{csort} eq 'ax25') {
-               if ($motd) {
-                       $motd = "${motd}_ax25" if -e "${motd}_ax25";
-               } else {
-                       $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
-               }
-       }
-       $self->send_file($motd) if -e $motd;
+       # send the relevant MOTD
+       $self->send_motd;
 
        # sort out privilege reduction
        $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
@@ -219,7 +203,7 @@ sub start
        foreach my $call (@{$user->buddies}) {
                my $ref = Route::User::get($call);
                if ($ref) {
-                       foreach my $node (@{$ref->parent}) {
+                       foreach my $node ($ref->parents) {
                                $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
                        } 
                }
@@ -484,6 +468,7 @@ sub send_ans
                }
        } 
 }
+
 # 
 # this is the thing that runs the command, it is done like this for the 
 # benefit of remote command execution
@@ -497,19 +482,22 @@ sub run_cmd
        my $cmdline = shift;
        my @ans;
        
-
        return () if length $cmdline == 0;
        
-       
        # split the command line up into parts, the first part is the command
        my ($cmd, $args) = split /\s+/, $cmdline, 2;
        $args = "" unless defined $args;
                
        if ($cmd) {
+
+               # check cmd
+               if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
+                       LogDbg('DXCommand', "cmd: invalid characters in '$cmd'");
+                       return $self->_error_out('e1');
+               }
+
                # strip out // on command only
                $cmd =~ s|//|/|g;
-               $cmd =~ s|^/||g;                # no leading / either
-               $cmd =~ s|[^-?\w/]||g;          # and no funny characters either
                                        
                my ($path, $fcmd);
                        
@@ -520,7 +508,7 @@ sub run_cmd
                if ($acmd) {
                        ($cmd, $args) = split /\s+/, "$acmd $args", 2;
                        $args = "" unless defined $args;
-                       dbg("aliased cmd: $cmd $args") if isdbg('command');
+                       dbg("cmd: aliased $cmd $args") if isdbg('command');
                }
                        
                # first expand out the entry to a command
@@ -528,26 +516,23 @@ sub run_cmd
                ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
 
                if ($path && $cmd) {
-                       dbg("path: $cmd cmd: $fcmd") if isdbg('command');
+                       dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
                        
                        my $package = find_cmd_name($path, $fcmd);
                        return ($@) if $@;
                                
-                       if ($package) {
+                       if ($package && $self->can("${package}::handle")) {
                                no strict 'refs';
-                               dbg("package: $package") if isdbg('command');
-                               eval { @ans = &$package($self, $args) };
+                               dbg("cmd: package $package") if isdbg('command');
+                               eval { @ans = &{"${package}::handle"}($self, $args) };
                                return (DXDebug::shortmess($@)) if $@;
+                       } else {
+                               dbg("cmd: $package not present") if isdbg('command');
+                               return $self->_error_out('e1');
                        }
                } else {
                        dbg("cmd: $cmd not found") if isdbg('command');
-                       if (++$self->{errors} > $maxerrors) {
-                               $self->send($self->msg('e26'));
-                               $self->disconnect;
-                               return ();
-                       } else {
-                               return ($self->msg('e1'));
-                       }
+                       return $self->_error_out('e1');
                }
        }
        
@@ -555,7 +540,7 @@ sub run_cmd
        if ($ok) {
                delete $self->{errors};
        } else {
-               if (++$self->{errors} > $maxerrors) {
+               if (++$self->{errors} > $DXChannel::maxerrors) {
                        $self->send($self->msg('e26'));
                        $self->disconnect;
                        return ();
@@ -760,12 +745,14 @@ sub clear_cmd_cache
 {
        no strict 'refs';
        
-       for (keys %Cache) {
-               undef *{$_} unless /cmd_cache/;
-               dbg("Undefining cmd $_") if isdbg('command');
+       for my $k (keys %Cache) {
+               unless ($k =~ /cmd_cache/) {
+                       dbg("Undefining cmd $k") if isdbg('command');
+                       undef $DXCommandmode::{"${k}::"};
+               }
        }
        %cmd_cache = ();
-       %Cache = ();
+       %Cache = ( cmd_clear_cmd_cache  => $Cache{cmd_clear_cmd_cache} );
 }
 
 #
@@ -776,11 +763,10 @@ sub clear_cmd_cache
 # 
 # This has been nicked directly from the perlembed pages
 #
-
 #require Devel::Symdump;  
 
 sub valid_package_name {
-       my($string) = @_;
+       my $string = shift;
        $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
        
        $string =~ s|/|_|g;
@@ -803,7 +789,7 @@ sub find_cmd_name {
                return undef;
        }
        
-       if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
+       if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
                #we have compiled this subroutine already,
                #it has not been updated on disk, nothing left to do
                #print STDERR "already compiled $package->handler\n";
@@ -817,7 +803,14 @@ sub find_cmd_name {
                };
                
                #wrap the code into a subroutine inside our unique package
-               my $eval = qq( sub $package { $sub } );
+               my $eval = qq(package DXCommandmode::$package; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
+
+
+               if ($sub =~ m|\s*sub\s+handle\n|) {
+                       $eval .= $sub;
+               } else {
+                       $eval .= qq(sub handle { $sub });
+               }
                
                if (isdbg('eval')) {
                        my @list = split /\n/, $eval;
@@ -831,18 +824,19 @@ sub find_cmd_name {
                no strict 'refs';
 
                if (exists $Cache{$package}) {
-                       dbg("Redefining $package") if isdbg('command');
-                       undef *$package;
+                       dbg("find_cmd_name: Redefining $package") if isdbg('command');
+                       undef $DXCommandmode::{"${package}::"};
+                       delete $Cache{$package};
                } else {
-                       dbg("Defining $package") if isdbg('command');
+                       dbg("find_cmd_name: Defining $package") if isdbg('command');
                }
+
                eval $eval;
-               
-               $Cache{$package} = {mtime => $mtime };
-           
+
+               $Cache{$package} = {mtime => $mtime } unless $@;
        }
 
-       return $package;
+       return "DXCommandmode::$package";
 }
 
 sub send
@@ -966,29 +960,40 @@ sub format_dx_spot
 
        my $t = ztime($_[2]);
        my $loc = '';
-       my $clth = $self->{consort} eq 'local' ? 29 : 30;
+
+       my $clth = 30 + $self->{width} - 80;    # allow comment to grow according the screen width 
+       #       --$clth if $self->{consort} eq 'local';
+       
        my $comment = substr (($_[3] || ''), 0, $clth);
-       $comment .= ' ' x ($clth - length($comment));
-       if ($self->{user}->wantgrid) {
-               my $ref = DXUser->get_current($_[4]);
-               if ($ref) {
-                       $loc = $ref->qra || '';
-                       $loc = ' ' . substr($loc, 0, 4) if $loc;
+       $comment =~ s/\t/ /g;
+       
+       $comment .= ' ' x ($clth - (length($comment)));
+       
+    if ($self->{user}->wantgrid) {
+               my $ref = DXUser::get_current($_[1]);
+               if ($ref && $ref->qra) {
+                       my $cloc = ' ' . substr($ref->qra, 0, 4);
+                       $comment = substr $comment, 0,  ($clth - (length($comment)+length($cloc)));
+                       $comment .= $cloc;
                }
-       }
-
-       if ($self->{user}->wantdxitu) {
+               my $origin = $_[4];
+               $origin =~ s/-#$//;                     # sigh......
+               $ref = DXUser::get_current($origin);
+               if ($ref && $ref->qra) {
+                       $loc = ' ' . substr($ref->qra, 0, 4);
+               }
+       } elsif ($self->{user}->wantdxitu) {
                $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
-               $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8]; 
+               $comment = substr($comment, 0,  $clth-3) . ' ' . sprintf("%2d", $_[8]) if defined $_[8]; 
        } elsif ($self->{user}->wantdxcq) {
                $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
-               $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9]; 
+               $comment = substr($comment, 0, $clth-3) . ' ' . sprintf("%2d", $_[9]) if defined $_[9]; 
        } elsif ($self->{user}->wantusstate) {
                $loc = ' ' . $_[13] if $_[13];
-               $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12]; 
+               $comment = substr($comment, 0,  $clth-3) . ' ' . $_[12] if $_[12]; 
        }
 
-       return sprintf "DX de %-7.7s%11.1f  %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
+       return sprintf "DX de %-8.8s%10.1f  %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
 }
 
 # send a dx spot
@@ -1014,7 +1019,7 @@ sub dx_spot
        } elsif ($self->{gtk}) {
                my ($dxloc, $byloc);
 
-               my $ref = DXUser->get_current($_[4]);
+               my $ref = DXUser::get_current($_[4]);
                if ($ref) {
                        $byloc = $ref->qra;
                        $byloc = substr($byloc, 0, 4) if $byloc;
@@ -1022,7 +1027,7 @@ sub dx_spot
 
                my $spot = $_[1];
                $spot =~ s|/\w{1,4}$||;
-               $ref = DXUser->get_current($spot);
+               $ref = DXUser::get_current($spot);
                if ($ref) {
                        $dxloc = $ref->qra;
                        $dxloc = substr($dxloc, 0, 4) if $dxloc;
@@ -1190,7 +1195,7 @@ sub import_cmd
                            if ($dxchan) {
                                        @out = $s->run($dxchan, 1);
                                } else {
-                                       my $u = DXUser->get($call);
+                                       my $u = DXUser::get($call);
                                        if ($u) {
                                                $dxchan = $main::me;
                                                my $old = $dxchan->{call};
@@ -1200,7 +1205,7 @@ sub import_cmd
                                                $dxchan->{priv} = $u->priv;
                                                $dxchan->{user} = $u;
                                                @out = $s->run($dxchan, 1);
-                                               $dxchan->{call} = $call;
+                                               $dxchan->{call} = $old;
                                                $dxchan->{priv} = $priv;
                                                $dxchan->{user} = $user;
                                        } else {
@@ -1225,5 +1230,27 @@ sub print_find_reply
        my $sort = $flag == 2 ? "External" : "Local";
        $self->send("$sort $target found at $node in $ms ms" );
 }
+
+# send the most relevant motd
+sub send_motd
+{
+       my $self = shift;
+       my $motd;
+
+       unless ($self->{registered}) {
+               $motd = "${main::motd}_nor_$self->{lang}";
+               $motd = "${main::motd}_nor" unless -e $motd;
+       }
+       $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
+       $motd = $main::motd unless $motd && -e $motd;
+       if ($self->conn->ax25) {
+               if ($motd) {
+                       $motd = "${motd}_ax25" if -e "${motd}_ax25";
+               } else {
+                       $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
+               }
+       }
+       $self->send_file($motd) if -e $motd;
+}
 1;
 __END__