3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
35 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %nothereslug);
37 %Cache = (); # cache of dynamically loaded routine's mod times
38 %cmd_cache = (); # cache of short names
39 $errstr = (); # error string from eval
40 %aliases = (); # aliases for (parts of) commands
41 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
42 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
44 use vars qw($VERSION $BRANCH);
45 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
46 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
47 $main::build += $VERSION;
48 $main::branch += $BRANCH;
51 # obtain a new connection this is derived from dxchannel
56 my $self = DXChannel::alloc(@_);
58 # routing, this must go out here to prevent race condx
61 my @rout = $main::routeroot->add_user($call, Route::here(1));
62 DXProt::route_pc16($DXProt::me, $main::routeroot, @rout) if @rout;
67 # this is how a a connection starts, you get a hello message and the motd with
68 # possibly some other messages asking you to set various things up if you are
69 # new (or nearly new and slacking) user.
73 my ($self, $line, $sort) = @_;
74 my $user = $self->{user};
75 my $call = $self->{call};
76 my $name = $user->{name};
78 $self->{name} = $name ? $name : $call;
79 $self->send($self->msg('l2',$self->{name}));
80 $self->send_file($main::motd) if (-e $main::motd);
81 $self->state('prompt'); # a bit of room for further expansion, passwords etc
82 $self->{priv} = $user->priv || 0;
83 $self->{lang} = $user->lang || $main::lang || 'en';
84 $self->{pagelth} = $user->pagelth || 20;
85 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
86 ($self->{width}) = $line =~ /width=(\d+)/;
87 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
88 $self->{consort} = $line; # save the connection type
90 # set some necessary flags on the user if they are connecting
91 $self->{beep} = $user->wantbeep;
92 $self->{ann} = $user->wantann;
93 $self->{wwv} = $user->wantwwv;
94 $self->{wcy} = $user->wantwcy;
95 $self->{talk} = $user->wanttalk;
96 $self->{wx} = $user->wantwx;
97 $self->{dx} = $user->wantdx;
98 $self->{logininfo} = $user->wantlogininfo;
102 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
103 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
104 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
105 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
107 # clean up qra locators
108 my $qra = $user->qra;
109 $qra = undef if ($qra && !DXBearing::is_qra($qra));
111 my $lat = $user->lat;
112 my $long = $user->long;
113 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
116 Log('DXCommand', "$call connected");
118 # send prompts and things
119 my $info = Route::cluster();
120 $self->send("Cluster:$info");
121 $self->send($self->msg('namee1')) if !$user->name;
122 $self->send($self->msg('qthe1')) if !$user->qth;
123 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
124 $self->send($self->msg('hnodee1')) if !$user->qth;
125 $self->send($self->msg('m9')) if DXMsg::for_me($call);
129 if (!$user->wantecho) {
130 $self->send_now('E', "0");
131 $self->send($self->msg('echow'));
134 $self->tell_login('loginu');
136 # do we need to send a forward/opernam?
137 my $lastoper = $user->lastoper || 0;
138 my $homenode = $user->homenode || "";
139 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
140 run_cmd($DXProt::me, "forward/opernam $call");
141 $user->lastoper($main::systime);
146 # This is the normal command prompt driver
155 # remove leading and trailing spaces
156 $cmdline =~ s/^\s*(.*)\s*$/$1/;
158 if ($self->{state} eq 'page') {
159 my $i = $self->{pagelth};
160 my $ref = $self->{pagedata};
163 # abort if we get a line starting in with a
164 if ($cmdline =~ /^a/io) {
169 # send a tranche of data
170 while ($i-- > 0 && @$ref) {
171 my $line = shift @$ref;
172 $line =~ s/\s+$//o; # why am having to do this?
176 # reset state if none or else chuck out an intermediate prompt
178 $tot -= $self->{pagelth};
179 $self->send($self->msg('page', $tot));
181 $self->state('prompt');
183 } elsif ($self->{state} eq 'sysop') {
184 my $passwd = $self->{user}->passwd;
185 my @pw = split / */, $passwd;
187 my @l = @{$self->{passwd}};
188 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
189 if ($cmdline =~ /$str/) {
190 $self->{priv} = $self->{user}->priv;
192 $self->send($self->msg('sorry'));
195 $self->send($self->msg('sorry'));
197 delete $self->{passwd};
198 $self->state('prompt');
199 } elsif ($self->{state} eq 'talk') {
200 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
201 for (@{$self->{talklist}}) {
202 $self->send_talks($_, $self->msg('talkend'));
204 $self->state('prompt');
205 delete $self->{talklist};
206 } elsif ($cmdline =~ m(^/\w+)) {
208 $self->send_ans(run_cmd($self, $cmdline));
209 $self->send($self->talk_prompt);
210 } elsif ($self->{talklist} && @{$self->{talklist}}) {
211 # send what has been said to whoever is in this person's talk list
212 for (@{$self->{talklist}}) {
213 $self->send_talks($_, $cmdline);
215 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
218 $self->state('prompt');
221 $self->send_ans(run_cmd($self, $cmdline));
224 # send a prompt only if we are in a prompt state
225 $self->prompt() if $self->{state} =~ /^prompt/o;
228 # send out the talk messages taking into account vias and connectivity
231 my ($self, $ent, $line) = @_;
233 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
234 $to = $ent unless $to;
235 my $call = $via ? $via : $to;
236 my $clref = Route::get($call);
237 my $dxchan = $clref->dxchan if $clref;
239 $dxchan->talk($self->{call}, $to, $via, $line);
241 $self->send($self->msg('disc2', $via ? $via : $to));
242 my @l = grep { $_ ne $ent } @{$self->{talklist}};
244 $self->{talklist} = \@l;
246 delete $self->{talklist};
247 $self->state('prompt');
256 for (@{$self->{talklist}}) {
257 my ($to, $via) = /(\S+)>(\S+)/;
261 return $self->msg('talkprompt', join(',', @call));
265 # send a load of stuff to a command user with page prompting
273 if ($self->{pagelth} && @_ > $self->{pagelth}) {
275 for ($i = $self->{pagelth}; $i-- > 0; ) {
277 $line =~ s/\s+$//o; # why am having to do this?
280 $self->{pagedata} = [ @_ ];
281 $self->state('page');
282 $self->send($self->msg('page', scalar @_));
294 # this is the thing that runs the command, it is done like this for the
295 # benefit of remote command execution
301 my $user = $self->{user};
302 my $call = $self->{call};
307 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
308 dbg("stored func cmd = $c\n") if isdbg('eval');
311 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
315 return () if length $cmdline == 0;
318 $cmdline =~ s|//|/|og;
320 # split the command line up into parts, the first part is the command
321 my ($cmd, $args) = split /\s+/, $cmdline, 2;
322 $args = "" unless defined $args;
328 dbg("cmd: $cmd") if isdbg('command');
330 # alias it if possible
331 my $acmd = CmdAlias::get_cmd($cmd);
333 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
334 $args = "" unless defined $args;
335 dbg("aliased cmd: $cmd $args") if isdbg('command');
338 # first expand out the entry to a command
339 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
340 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
343 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
345 my $package = find_cmd_name($path, $fcmd);
346 @ans = (0) if !$package ;
349 dbg("package: $package") if isdbg('command');
351 unless (exists $Cache{$package}->{'sub'}) {
352 $c = eval $Cache{$package}->{'eval'};
354 return DXDebug::shortmess($@);
356 $Cache{$package}->{'sub'} = $c;
358 $c = $Cache{$package}->{'sub'};
360 @ans = &{$c}($self, $args);
365 return (DXDebug::shortmess($@));
369 dbg("cmd: $cmd not found") if isdbg('command');
370 if (++$self->{errors} > $maxerrors) {
371 $self->send($self->msg('e26'));
375 return ($self->msg('e1'));
383 delete $self->{errors};
385 if (++$self->{errors} > $maxerrors) {
386 $self->send($self->msg('e26'));
395 # This is called from inside the main cluster processing loop and is used
396 # for despatching commands that are doing some long processing job
401 my @dxchan = DXChannel->get_all();
404 foreach $dxchan (@dxchan) {
405 next if $dxchan->sort ne 'U';
407 # send a prompt if no activity out on this channel
408 if ($t >= $dxchan->t + $main::user_interval) {
409 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
414 while (my ($k, $v) = each %nothereslug) {
415 if ($main::systime >= $v + 300) {
416 delete $nothereslug{$k};
422 # finish up a user context
427 my $call = $self->call;
429 return if $self->{disconnecting}++;
431 delete $self->{senddbg};
433 my $uref = Route::User::get($call);
436 @rout = $main::routeroot->del_user($uref);
437 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
439 confess "trying to disconnect a non existant user $call";
442 # issue a pc17 to everybody interested
443 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
445 # I was the last node visited
446 $self->user->node($main::mycall);
448 # send info to all logged in thingies
449 $self->tell_login('logoutu');
451 Log('DXCommand', "$call disconnected");
453 $self->SUPER::disconnect;
457 # short cut to output a prompt
463 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
466 # broadcast a message to all users [except those mentioned after buffer]
469 my $pkg = shift; # ignored
470 my $s = shift; # the line to be rebroadcast
472 foreach my $dxchan (DXChannel->get_all()) {
473 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
474 next if grep $dxchan == $_, @_;
475 $dxchan->send($s); # send it
479 # gimme all the users
482 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
485 # run a script for this user
489 my $silent = shift || 0;
494 # search for the command in the cache of short->long form commands
499 my ($path, $short_cmd, $suffix) = @_;
502 # commands are lower case
503 $short_cmd = lc $short_cmd;
504 dbg("command: $path $short_cmd\n") if isdbg('command');
506 # do some checking for funny characters
507 return () if $short_cmd =~ /\/$/;
509 # return immediately if we have it
510 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
511 if ($apath && $acmd) {
512 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
513 return ($apath, $acmd);
517 my @parts = split '/', $short_cmd;
524 for ($i = 0; $i < @parts; $i++) {
526 opendir(D, $curdir) or confess "can't open $curdir $!";
530 foreach $l (sort @ls) {
532 if ($i < $#parts) { # we are dealing with directories
533 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
534 dbg("got dir: $curdir/$l\n") if isdbg('command');
539 } else { # we are dealing with commands
540 @lparts = split /\./, $l;
541 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
542 if ($p eq substr($l, 0, length $p)) {
543 pop @lparts; # remove the suffix
544 $l = join '.', @lparts;
545 # chop $dirfn; # remove trailing /
546 $dirfn = "" unless $dirfn;
547 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
548 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
549 return ($path, "$dirfn$l");
557 # clear the command name cache
564 # the persistant execution of things from the command directories
567 # This allows perl programs to call functions dynamically
569 # This has been nicked directly from the perlembed pages
572 #require Devel::Symdump;
574 sub valid_package_name {
576 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
578 #second pass only for words starting with a digit
579 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
581 #Dress it up as a real package name
582 $string =~ s/\//_/og;
586 # find a cmd reference
587 # this is really for use in user written stubs
589 # use the result as a symbolic reference:-
592 # @out = &$r($self, $line);
601 # first expand out the entry to a command
602 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
603 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
605 # make sure it is loaded
606 $r = find_cmd_name($path, $fcmd);
612 # this bit of magic finds a command in the offered directory
616 my $package = valid_package_name($cmdname);
617 my $filename = "$path/$cmdname.pl";
618 my $mtime = -M $filename;
620 # return if we can't find it
622 unless (defined $mtime) {
623 $errstr = DXM::msg('e1');
627 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
628 #we have compiled this subroutine already,
629 #it has not been updated on disk, nothing left to do
630 #print STDERR "already compiled $package->handler\n";
634 my $sub = readfilestr($filename);
636 $errstr = "Syserr: can't open '$filename' $!";
640 #wrap the code into a subroutine inside our unique package
641 my $eval = qq( sub { $sub } );
644 my @list = split /\n/, $eval;
647 dbg($_ . "\n") if isdbg('eval');
651 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
659 my ($self, $let, $buf) = @_;
660 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
661 if ($self->{enhanced}) {
662 $self->send_later($let, $buf);
671 # send a talk message here
674 my ($self, $from, $to, $via, $line) = @_;
675 $line =~ s/\\5E/\^/g;
676 $self->local_send('T', "$to de $from: $line") if $self->{talk};
677 Log('talk', $to, $from, $main::mycall, $line);
678 # send a 'not here' message if required
679 unless ($self->{here} && $from ne $to) {
680 my $key = "$to$from";
681 unless (exists $nothereslug{$key}) {
683 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
684 my $name = $self->user->name || $to;
685 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
686 $nothereslug{$key} = $main::systime;
687 $dxchan->talk($to, $from, undef, $s);
704 if ($self->{annfilter}) {
705 ($filter, $hops) = $self->{annfilter}->it(@_ );
706 return unless $filter;
709 unless ($self->{ann}) {
710 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
712 return if $target eq 'SYSOP' && $self->{priv} < 5;
713 my $buf = "$to$target de $_[0]: $text";
715 $buf .= "\a\a" if $self->{beep};
716 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
727 return unless $self->{dx};
729 if ($self->{spotsfilter}) {
730 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
731 return unless $filter;
734 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
735 $buf .= "\a\a" if $self->{beep};
737 $self->local_send('X', $buf);
747 return unless $self->{wwv};
749 if ($self->{wwvfilter}) {
750 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
751 return unless $filter;
754 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
755 $buf .= "\a\a" if $self->{beep};
756 $self->local_send('V', $buf);
766 return unless $self->{wcy};
768 if ($self->{wcyfilter}) {
769 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
770 return unless $filter;
773 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
774 $buf .= "\a\a" if $self->{beep};
775 $self->local_send('Y', $buf);
778 # broadcast debug stuff to all interested parties
781 my $s = shift; # the line to be rebroadcast
783 foreach my $dxchan (DXChannel->get_all) {
784 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
785 $dxchan->send_later('L', $s);