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
45 # obtain a new connection this is derived from dxchannel
50 my $self = DXChannel::alloc(@_);
52 # routing, this must go out here to prevent race condx
55 my @rout = $main::routeroot->add_user($call, Route::here(1));
56 DXProt::route_pc16($DXProt::me, $main::routeroot, @rout) if @rout;
61 # this is how a a connection starts, you get a hello message and the motd with
62 # possibly some other messages asking you to set various things up if you are
63 # new (or nearly new and slacking) user.
67 my ($self, $line, $sort) = @_;
68 my $user = $self->{user};
69 my $call = $self->{call};
70 my $name = $user->{name};
72 $self->{name} = $name ? $name : $call;
73 $self->send($self->msg('l2',$self->{name}));
74 $self->send_file($main::motd) if (-e $main::motd);
75 $self->state('prompt'); # a bit of room for further expansion, passwords etc
76 $self->{priv} = $user->priv || 0;
77 $self->{lang} = $user->lang || $main::lang || 'en';
78 $self->{pagelth} = $user->pagelth || 20;
79 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
80 $self->{consort} = $line; # save the connection type
82 # set some necessary flags on the user if they are connecting
83 $self->{beep} = $user->wantbeep;
84 $self->{ann} = $user->wantann;
85 $self->{wwv} = $user->wantwwv;
86 $self->{wcy} = $user->wantwcy;
87 $self->{talk} = $user->wanttalk;
88 $self->{wx} = $user->wantwx;
89 $self->{dx} = $user->wantdx;
90 $self->{logininfo} = $user->wantlogininfo;
94 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
95 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
96 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
97 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
99 # clean up qra locators
100 my $qra = $user->qra;
101 $qra = undef if ($qra && !DXBearing::is_qra($qra));
103 my $lat = $user->lat;
104 my $long = $user->long;
105 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
108 Log('DXCommand', "$call connected");
110 # send prompts and things
111 my $info = Route::cluster();
112 $self->send("Cluster:$info");
113 $self->send($self->msg('namee1')) if !$user->name;
114 $self->send($self->msg('qthe1')) if !$user->qth;
115 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
116 $self->send($self->msg('hnodee1')) if !$user->qth;
117 $self->send($self->msg('m9')) if DXMsg::for_me($call);
121 if (!$user->wantecho) {
122 $self->send_now('E', "0");
123 $self->send($self->msg('echow'));
126 $self->tell_login('loginu');
131 # This is the normal command prompt driver
140 # remove leading and trailing spaces
141 $cmdline =~ s/^\s*(.*)\s*$/$1/;
143 if ($self->{state} eq 'page') {
144 my $i = $self->{pagelth};
145 my $ref = $self->{pagedata};
148 # abort if we get a line starting in with a
149 if ($cmdline =~ /^a/io) {
154 # send a tranche of data
155 while ($i-- > 0 && @$ref) {
156 my $line = shift @$ref;
157 $line =~ s/\s+$//o; # why am having to do this?
161 # reset state if none or else chuck out an intermediate prompt
163 $tot -= $self->{pagelth};
164 $self->send($self->msg('page', $tot));
166 $self->state('prompt');
168 } elsif ($self->{state} eq 'sysop') {
169 my $passwd = $self->{user}->passwd;
170 my @pw = split / */, $passwd;
172 my @l = @{$self->{passwd}};
173 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
174 if ($cmdline =~ /$str/) {
175 $self->{priv} = $self->{user}->priv;
177 $self->send($self->msg('sorry'));
180 $self->send($self->msg('sorry'));
182 delete $self->{passwd};
183 $self->state('prompt');
184 } elsif ($self->{state} eq 'talk') {
185 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
186 for (@{$self->{talklist}}) {
187 $self->send_talks($_, $self->msg('talkend'));
189 $self->state('prompt');
190 delete $self->{talklist};
191 } elsif ($cmdline =~ m(^/\w+)) {
193 $self->send_ans(run_cmd($self, $cmdline));
194 $self->send($self->talk_prompt);
195 } elsif ($self->{talklist} && @{$self->{talklist}}) {
196 # send what has been said to whoever is in this person's talk list
197 for (@{$self->{talklist}}) {
198 $self->send_talks($_, $cmdline);
200 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
203 $self->state('prompt');
206 $self->send_ans(run_cmd($self, $cmdline));
209 # send a prompt only if we are in a prompt state
210 $self->prompt() if $self->{state} =~ /^prompt/o;
213 # send out the talk messages taking into account vias and connectivity
216 my ($self, $ent, $line) = @_;
218 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
219 $to = $ent unless $to;
220 my $call = $via ? $via : $to;
221 my $clref = Route::get($call);
222 my $dxchan = $clref->dxchan if $clref;
224 $dxchan->talk($self->{call}, $to, $via, $line);
226 $self->send($self->msg('disc2', $via ? $via : $to));
227 my @l = grep { $_ ne $ent } @{$self->{talklist}};
229 $self->{talklist} = \@l;
231 delete $self->{talklist};
232 $self->state('prompt');
241 for (@{$self->{talklist}}) {
242 my ($to, $via) = /(\S+)>(\S+)/;
246 return $self->msg('talkprompt', join(',', @call));
250 # send a load of stuff to a command user with page prompting
258 if ($self->{pagelth} && @_ > $self->{pagelth}) {
260 for ($i = $self->{pagelth}; $i-- > 0; ) {
262 $line =~ s/\s+$//o; # why am having to do this?
265 $self->{pagedata} = [ @_ ];
266 $self->state('page');
267 $self->send($self->msg('page', scalar @_));
279 # this is the thing that runs the command, it is done like this for the
280 # benefit of remote command execution
286 my $user = $self->{user};
287 my $call = $self->{call};
292 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
293 dbg("stored func cmd = $c\n") if isdbg('eval');
296 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
300 return () if length $cmdline == 0;
303 $cmdline =~ s|//|/|og;
305 # split the command line up into parts, the first part is the command
306 my ($cmd, $args) = split /\s+/, $cmdline, 2;
307 $args = "" unless defined $args;
313 dbg("cmd: $cmd") if isdbg('command');
315 # alias it if possible
316 my $acmd = CmdAlias::get_cmd($cmd);
318 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
319 $args = "" unless defined $args;
320 dbg("aliased cmd: $cmd $args") if isdbg('command');
323 # first expand out the entry to a command
324 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
325 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
328 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
330 my $package = find_cmd_name($path, $fcmd);
331 @ans = (0) if !$package ;
334 dbg("package: $package") if isdbg('command');
336 unless (exists $Cache{$package}->{'sub'}) {
337 $c = eval $Cache{$package}->{'eval'};
339 return DXDebug::shortmess($@);
341 $Cache{$package}->{'sub'} = $c;
343 $c = $Cache{$package}->{'sub'};
345 @ans = &{$c}($self, $args);
350 return (DXDebug::shortmess($@));
354 dbg("cmd: $cmd not found") if isdbg('command');
355 if (++$self->{errors} > $maxerrors) {
356 $self->send($self->msg('e26'));
360 return ($self->msg('e1'));
368 delete $self->{errors};
370 if (++$self->{errors} > $maxerrors) {
371 $self->send($self->msg('e26'));
380 # This is called from inside the main cluster processing loop and is used
381 # for despatching commands that are doing some long processing job
386 my @dxchan = DXChannel->get_all();
389 foreach $dxchan (@dxchan) {
390 next if $dxchan->sort ne 'U';
392 # send a prompt if no activity out on this channel
393 if ($t >= $dxchan->t + $main::user_interval) {
394 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
399 while (my ($k, $v) = each %nothereslug) {
400 if ($main::systime >= $v + 300) {
401 delete $nothereslug{$k};
407 # finish up a user context
412 my $call = $self->call;
413 delete $self->{senddbg};
415 my @rout = $main::routeroot->del_user($call);
416 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
418 # issue a pc17 to everybody interested
419 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
421 # I was the last node visited
422 $self->user->node($main::mycall);
424 # send info to all logged in thingies
425 $self->tell_login('logoutu');
427 Log('DXCommand', "$call disconnected");
429 $self->SUPER::disconnect;
433 # short cut to output a prompt
439 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
442 # broadcast a message to all users [except those mentioned after buffer]
445 my $pkg = shift; # ignored
446 my $s = shift; # the line to be rebroadcast
448 foreach my $dxchan (DXChannel->get_all()) {
449 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
450 next if grep $dxchan == $_, @_;
451 $dxchan->send($s); # send it
455 # gimme all the users
458 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
461 # run a script for this user
465 my $silent = shift || 0;
470 # search for the command in the cache of short->long form commands
475 my ($path, $short_cmd, $suffix) = @_;
478 # commands are lower case
479 $short_cmd = lc $short_cmd;
480 dbg("command: $path $short_cmd\n") if isdbg('command');
482 # do some checking for funny characters
483 return () if $short_cmd =~ /\/$/;
485 # return immediately if we have it
486 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
487 if ($apath && $acmd) {
488 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
489 return ($apath, $acmd);
493 my @parts = split '/', $short_cmd;
500 for ($i = 0; $i < @parts; $i++) {
502 opendir(D, $curdir) or confess "can't open $curdir $!";
506 foreach $l (sort @ls) {
508 if ($i < $#parts) { # we are dealing with directories
509 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
510 dbg("got dir: $curdir/$l\n") if isdbg('command');
515 } else { # we are dealing with commands
516 @lparts = split /\./, $l;
517 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
518 if ($p eq substr($l, 0, length $p)) {
519 pop @lparts; # remove the suffix
520 $l = join '.', @lparts;
521 # chop $dirfn; # remove trailing /
522 $dirfn = "" unless $dirfn;
523 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
524 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
525 return ($path, "$dirfn$l");
533 # clear the command name cache
540 # the persistant execution of things from the command directories
543 # This allows perl programs to call functions dynamically
545 # This has been nicked directly from the perlembed pages
548 #require Devel::Symdump;
550 sub valid_package_name {
552 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
554 #second pass only for words starting with a digit
555 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
557 #Dress it up as a real package name
558 $string =~ s/\//_/og;
562 # find a cmd reference
563 # this is really for use in user written stubs
565 # use the result as a symbolic reference:-
568 # @out = &$r($self, $line);
577 # first expand out the entry to a command
578 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
579 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
581 # make sure it is loaded
582 $r = find_cmd_name($path, $fcmd);
588 # this bit of magic finds a command in the offered directory
592 my $package = valid_package_name($cmdname);
593 my $filename = "$path/$cmdname.pl";
594 my $mtime = -M $filename;
596 # return if we can't find it
598 unless (defined $mtime) {
599 $errstr = DXM::msg('e1');
603 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
604 #we have compiled this subroutine already,
605 #it has not been updated on disk, nothing left to do
606 #print STDERR "already compiled $package->handler\n";
610 my $sub = readfilestr($filename);
612 $errstr = "Syserr: can't open '$filename' $!";
616 #wrap the code into a subroutine inside our unique package
617 my $eval = qq( sub { $sub } );
620 my @list = split /\n/, $eval;
623 dbg($_ . "\n") if isdbg('eval');
627 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
635 my ($self, $let, $buf) = @_;
636 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
637 if ($self->{enhanced}) {
638 $self->send_later($let, $buf);
647 # send a talk message here
650 my ($self, $from, $to, $via, $line) = @_;
651 $line =~ s/\\5E/\^/g;
652 $self->send_later('T', "$to de $from: $line") if $self->{talk};
653 Log('talk', $to, $from, $main::mycall, $line);
654 # send a 'not here' message if required
655 unless ($self->{here} && $from ne $to) {
656 my $key = "$to$from";
657 unless (exists $nothereslug{$key}) {
659 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
660 my $name = $self->user->name || $to;
661 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
662 $nothereslug{$key} = $main::systime;
663 $dxchan->talk($to, $from, undef, $s);
680 if ($self->{annfilter}) {
681 ($filter, $hops) = $self->{annfilter}->it(@_ );
682 return unless $filter;
685 unless ($self->{ann}) {
686 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
688 return if $target eq 'SYSOP' && $self->{priv} < 5;
689 my $buf = "$to$target de $_[0]: $text";
691 $buf .= "\a\a" if $self->{beep};
692 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
703 return unless $self->{dx};
705 if ($self->{spotsfilter}) {
706 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
707 return unless $filter;
710 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
711 $buf .= "\a\a" if $self->{beep};
713 $self->local_send('X', $buf);
723 return unless $self->{wwv};
725 if ($self->{wwvfilter}) {
726 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
727 return unless $filter;
730 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
731 $buf .= "\a\a" if $self->{beep};
732 $self->local_send('V', $buf);
742 return unless $self->{wcy};
744 if ($self->{wcyfilter}) {
745 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
746 return unless $filter;
749 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
750 $buf .= "\a\a" if $self->{beep};
751 $self->local_send('Y', $buf);
754 # broadcast debug stuff to all interested parties
757 my $s = shift; # the line to be rebroadcast
759 foreach my $dxchan (DXChannel->get_all) {
760 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
761 $dxchan->send_later('L', $s);