3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
37 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %nothereslug);
39 %Cache = (); # cache of dynamically loaded routine's mod times
40 %cmd_cache = (); # cache of short names
41 $errstr = (); # error string from eval
42 %aliases = (); # aliases for (parts of) commands
43 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
44 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
46 use vars qw($VERSION $BRANCH);
47 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
48 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
49 $main::build += $VERSION;
50 $main::branch += $BRANCH;
53 # obtain a new connection this is derived from dxchannel
58 my $self = DXChannel::alloc(@_);
60 # routing, this must go out here to prevent race condx
63 my @rout = $main::routeroot->add_user($call, Route::here(1));
64 DXProt::route_pc16($DXProt::me, $main::routeroot, @rout) if @rout;
69 # this is how a a connection starts, you get a hello message and the motd with
70 # possibly some other messages asking you to set various things up if you are
71 # new (or nearly new and slacking) user.
75 my ($self, $line, $sort) = @_;
76 my $user = $self->{user};
77 my $call = $self->{call};
78 my $name = $user->{name};
81 my $host = $self->{conn}->{peerhost} || "unknown";
82 Log('DXCommand', "$call connected from $host");
84 $self->{name} = $name ? $name : $call;
85 $self->send($self->msg('l2',$self->{name}));
86 $self->send_file($main::motd) if (-e $main::motd);
87 $self->state('prompt'); # a bit of room for further expansion, passwords etc
88 $self->{priv} = $user->priv || 0;
89 $self->{lang} = $user->lang || $main::lang || 'en';
90 $self->{pagelth} = $user->pagelth || 20;
91 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
92 ($self->{width}) = $line =~ /width=(\d+)/;
93 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
94 $self->{consort} = $line; # save the connection type
96 # set some necessary flags on the user if they are connecting
97 $self->{beep} = $user->wantbeep;
98 $self->{ann} = $user->wantann;
99 $self->{wwv} = $user->wantwwv;
100 $self->{wcy} = $user->wantwcy;
101 $self->{talk} = $user->wanttalk;
102 $self->{wx} = $user->wantwx;
103 $self->{dx} = $user->wantdx;
104 $self->{logininfo} = $user->wantlogininfo;
105 $self->{ann_talk} = $user->wantann_talk;
109 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
110 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
111 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
112 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
114 # clean up qra locators
115 my $qra = $user->qra;
116 $qra = undef if ($qra && !DXBearing::is_qra($qra));
118 my $lat = $user->lat;
119 my $long = $user->long;
120 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
124 if (!$user->wantecho) {
125 $self->send_now('E', "0");
126 $self->send($self->msg('echow'));
129 $self->tell_login('loginu');
131 # do we need to send a forward/opernam?
132 my $lastoper = $user->lastoper || 0;
133 my $homenode = $user->homenode || "";
134 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
135 run_cmd($DXProt::me, "forward/opernam $call");
136 $user->lastoper($main::systime);
139 # run a script send the output to the punter
140 my $script = new Script(lc $call) || new Script('user_default');
141 $script->run($self) if $script;
144 my $info = Route::cluster();
145 $self->send("Cluster:$info");
147 # send prompts and things
148 $self->send($self->msg('namee1')) if !$user->name;
149 $self->send($self->msg('qthe1')) if !$user->qth;
150 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
151 $self->send($self->msg('hnodee1')) if !$user->qth;
152 $self->send($self->msg('m9')) if DXMsg::for_me($call);
157 # This is the normal command prompt driver
166 # remove leading and trailing spaces
167 $cmdline =~ s/^\s*(.*)\s*$/$1/;
169 if ($self->{state} eq 'page') {
170 my $i = $self->{pagelth};
171 my $ref = $self->{pagedata};
174 # abort if we get a line starting in with a
175 if ($cmdline =~ /^a/io) {
180 # send a tranche of data
181 while ($i-- > 0 && @$ref) {
182 my $line = shift @$ref;
183 $line =~ s/\s+$//o; # why am having to do this?
187 # reset state if none or else chuck out an intermediate prompt
189 $tot -= $self->{pagelth};
190 $self->send($self->msg('page', $tot));
192 $self->state('prompt');
194 } elsif ($self->{state} eq 'sysop') {
195 my $passwd = $self->{user}->passwd;
196 my @pw = split / */, $passwd;
198 my @l = @{$self->{passwd}};
199 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
200 if ($cmdline =~ /$str/) {
201 $self->{priv} = $self->{user}->priv;
203 $self->send($self->msg('sorry'));
206 $self->send($self->msg('sorry'));
208 delete $self->{passwd};
209 $self->state('prompt');
210 } elsif ($self->{state} eq 'talk') {
211 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
212 for (@{$self->{talklist}}) {
213 $self->send_talks($_, $self->msg('talkend'));
215 $self->state('prompt');
216 delete $self->{talklist};
217 } elsif ($cmdline =~ m(^/\w+)) {
219 $self->send_ans(run_cmd($self, $cmdline));
220 $self->send($self->talk_prompt);
221 } elsif ($self->{talklist} && @{$self->{talklist}}) {
222 # send what has been said to whoever is in this person's talk list
223 for (@{$self->{talklist}}) {
224 $self->send_talks($_, $cmdline);
226 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
229 $self->state('prompt');
232 $self->send_ans(run_cmd($self, $cmdline));
235 # send a prompt only if we are in a prompt state
236 $self->prompt() if $self->{state} =~ /^prompt/o;
239 # send out the talk messages taking into account vias and connectivity
242 my ($self, $ent, $line) = @_;
244 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
245 $to = $ent unless $to;
246 my $call = $via ? $via : $to;
247 my $clref = Route::get($call);
248 my $dxchan = $clref->dxchan if $clref;
250 $dxchan->talk($self->{call}, $to, $via, $line);
252 $self->send($self->msg('disc2', $via ? $via : $to));
253 my @l = grep { $_ ne $ent } @{$self->{talklist}};
255 $self->{talklist} = \@l;
257 delete $self->{talklist};
258 $self->state('prompt');
267 for (@{$self->{talklist}}) {
268 my ($to, $via) = /(\S+)>(\S+)/;
272 return $self->msg('talkprompt', join(',', @call));
276 # send a load of stuff to a command user with page prompting
284 if ($self->{pagelth} && @_ > $self->{pagelth}) {
286 for ($i = $self->{pagelth}; $i-- > 0; ) {
288 $line =~ s/\s+$//o; # why am having to do this?
291 $self->{pagedata} = [ @_ ];
292 $self->state('page');
293 $self->send($self->msg('page', scalar @_));
305 # this is the thing that runs the command, it is done like this for the
306 # benefit of remote command execution
312 my $user = $self->{user};
313 my $call = $self->{call};
318 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
319 dbg("stored func cmd = $c\n") if isdbg('eval');
322 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
326 return () if length $cmdline == 0;
329 $cmdline =~ s|//|/|og;
331 # split the command line up into parts, the first part is the command
332 my ($cmd, $args) = split /\s+/, $cmdline, 2;
333 $args = "" unless defined $args;
339 dbg("cmd: $cmd") if isdbg('command');
341 # alias it if possible
342 my $acmd = CmdAlias::get_cmd($cmd);
344 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
345 $args = "" unless defined $args;
346 dbg("aliased cmd: $cmd $args") if isdbg('command');
349 # first expand out the entry to a command
350 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
351 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
354 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
356 my $package = find_cmd_name($path, $fcmd);
357 @ans = (0) if !$package ;
360 dbg("package: $package") if isdbg('command');
362 unless (exists $Cache{$package}->{'sub'}) {
363 $c = eval $Cache{$package}->{'eval'};
365 return DXDebug::shortmess($@);
367 $Cache{$package}->{'sub'} = $c;
369 $c = $Cache{$package}->{'sub'};
371 @ans = &{$c}($self, $args);
376 return (DXDebug::shortmess($@));
380 dbg("cmd: $cmd not found") if isdbg('command');
381 if (++$self->{errors} > $maxerrors) {
382 $self->send($self->msg('e26'));
386 return ($self->msg('e1'));
394 delete $self->{errors};
396 if (++$self->{errors} > $maxerrors) {
397 $self->send($self->msg('e26'));
406 # This is called from inside the main cluster processing loop and is used
407 # for despatching commands that are doing some long processing job
412 my @dxchan = DXChannel->get_all();
415 foreach $dxchan (@dxchan) {
416 next if $dxchan->sort ne 'U';
418 # send a prompt if no activity out on this channel
419 if ($t >= $dxchan->t + $main::user_interval) {
420 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
425 while (my ($k, $v) = each %nothereslug) {
426 if ($main::systime >= $v + 300) {
427 delete $nothereslug{$k};
433 # finish up a user context
438 my $call = $self->call;
440 return if $self->{disconnecting}++;
442 delete $self->{senddbg};
444 my $uref = Route::User::get($call);
447 @rout = $main::routeroot->del_user($uref);
448 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
450 confess "trying to disconnect a non existant user $call";
453 # issue a pc17 to everybody interested
454 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
456 # I was the last node visited
457 $self->user->node($main::mycall);
459 # send info to all logged in thingies
460 $self->tell_login('logoutu');
462 Log('DXCommand', "$call disconnected");
464 $self->SUPER::disconnect;
468 # short cut to output a prompt
474 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
477 # broadcast a message to all users [except those mentioned after buffer]
480 my $pkg = shift; # ignored
481 my $s = shift; # the line to be rebroadcast
483 foreach my $dxchan (DXChannel->get_all()) {
484 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
485 next if grep $dxchan == $_, @_;
486 $dxchan->send($s); # send it
490 # gimme all the users
493 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
496 # run a script for this user
500 my $silent = shift || 0;
505 # search for the command in the cache of short->long form commands
510 my ($path, $short_cmd, $suffix) = @_;
513 # commands are lower case
514 $short_cmd = lc $short_cmd;
515 dbg("command: $path $short_cmd\n") if isdbg('command');
517 # do some checking for funny characters
518 return () if $short_cmd =~ /\/$/;
520 # return immediately if we have it
521 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
522 if ($apath && $acmd) {
523 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
524 return ($apath, $acmd);
528 my @parts = split '/', $short_cmd;
535 for ($i = 0; $i < @parts; $i++) {
537 opendir(D, $curdir) or confess "can't open $curdir $!";
541 foreach $l (sort @ls) {
543 if ($i < $#parts) { # we are dealing with directories
544 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
545 dbg("got dir: $curdir/$l\n") if isdbg('command');
550 } else { # we are dealing with commands
551 @lparts = split /\./, $l;
552 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
553 if ($p eq substr($l, 0, length $p)) {
554 pop @lparts; # remove the suffix
555 $l = join '.', @lparts;
556 # chop $dirfn; # remove trailing /
557 $dirfn = "" unless $dirfn;
558 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
559 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
560 return ($path, "$dirfn$l");
568 # clear the command name cache
575 # the persistant execution of things from the command directories
578 # This allows perl programs to call functions dynamically
580 # This has been nicked directly from the perlembed pages
583 #require Devel::Symdump;
585 sub valid_package_name {
587 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
589 #second pass only for words starting with a digit
590 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
592 #Dress it up as a real package name
593 $string =~ s/\//_/og;
597 # find a cmd reference
598 # this is really for use in user written stubs
600 # use the result as a symbolic reference:-
603 # @out = &$r($self, $line);
612 # first expand out the entry to a command
613 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
614 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
616 # make sure it is loaded
617 $r = find_cmd_name($path, $fcmd);
623 # this bit of magic finds a command in the offered directory
627 my $package = valid_package_name($cmdname);
628 my $filename = "$path/$cmdname.pl";
629 my $mtime = -M $filename;
631 # return if we can't find it
633 unless (defined $mtime) {
634 $errstr = DXM::msg('e1');
638 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
639 #we have compiled this subroutine already,
640 #it has not been updated on disk, nothing left to do
641 #print STDERR "already compiled $package->handler\n";
645 my $sub = readfilestr($filename);
647 $errstr = "Syserr: can't open '$filename' $!";
651 #wrap the code into a subroutine inside our unique package
652 my $eval = qq( sub { $sub } );
655 my @list = split /\n/, $eval;
658 dbg($_ . "\n") if isdbg('eval');
662 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
670 my ($self, $let, $buf) = @_;
671 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
672 if ($self->{enhanced}) {
673 $self->send_later($let, $buf);
682 # send a talk message here
685 my ($self, $from, $to, $via, $line) = @_;
686 $line =~ s/\\5E/\^/g;
687 $self->local_send('T', "$to de $from: $line") if $self->{talk};
688 Log('talk', $to, $from, $main::mycall, $line);
689 # send a 'not here' message if required
690 unless ($self->{here} && $from ne $to) {
691 my $key = "$to$from";
692 unless (exists $nothereslug{$key}) {
694 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
695 my $name = $self->user->name || $to;
696 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
697 $nothereslug{$key} = $main::systime;
698 $dxchan->talk($to, $from, undef, $s);
715 if (!$self->{ann_talk} && $to ne $self->{call}) {
716 my $call = AnnTalk::is_talk_candidate($_[0], $text);
720 if ($self->{annfilter}) {
721 ($filter, $hops) = $self->{annfilter}->it(@_ );
722 return unless $filter;
725 unless ($self->{ann}) {
726 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
728 return if $target eq 'SYSOP' && $self->{priv} < 5;
729 my $buf = "$to$target de $_[0]: $text";
731 $buf .= "\a\a" if $self->{beep};
732 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
743 return unless $self->{dx};
745 if ($self->{spotsfilter}) {
746 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
747 return unless $filter;
750 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
751 $buf .= "\a\a" if $self->{beep};
753 $self->local_send('X', $buf);
763 return unless $self->{wwv};
765 if ($self->{wwvfilter}) {
766 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
767 return unless $filter;
770 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
771 $buf .= "\a\a" if $self->{beep};
772 $self->local_send('V', $buf);
782 return unless $self->{wcy};
784 if ($self->{wcyfilter}) {
785 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
786 return unless $filter;
789 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
790 $buf .= "\a\a" if $self->{beep};
791 $self->local_send('Y', $buf);
794 # broadcast debug stuff to all interested parties
797 my $s = shift; # the line to be rebroadcast
799 foreach my $dxchan (DXChannel->get_all) {
800 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
801 $dxchan->send_later('L', $s);