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->{consort} = $line; # save the connection type
88 # set some necessary flags on the user if they are connecting
89 $self->{beep} = $user->wantbeep;
90 $self->{ann} = $user->wantann;
91 $self->{wwv} = $user->wantwwv;
92 $self->{wcy} = $user->wantwcy;
93 $self->{talk} = $user->wanttalk;
94 $self->{wx} = $user->wantwx;
95 $self->{dx} = $user->wantdx;
96 $self->{logininfo} = $user->wantlogininfo;
100 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
101 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
102 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
103 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
105 # clean up qra locators
106 my $qra = $user->qra;
107 $qra = undef if ($qra && !DXBearing::is_qra($qra));
109 my $lat = $user->lat;
110 my $long = $user->long;
111 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
114 Log('DXCommand', "$call connected");
116 # send prompts and things
117 my $info = Route::cluster();
118 $self->send("Cluster:$info");
119 $self->send($self->msg('namee1')) if !$user->name;
120 $self->send($self->msg('qthe1')) if !$user->qth;
121 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
122 $self->send($self->msg('hnodee1')) if !$user->qth;
123 $self->send($self->msg('m9')) if DXMsg::for_me($call);
127 if (!$user->wantecho) {
128 $self->send_now('E', "0");
129 $self->send($self->msg('echow'));
132 $self->tell_login('loginu');
134 # do we need to send a forward/opernam?
135 my $lastoper = $user->lastoper || 0;
136 my $homenode = $user->homenode || "";
137 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
138 run_cmd($DXProt::me, "forward/opernam $call");
139 $user->lastoper($main::systime);
144 # This is the normal command prompt driver
153 # remove leading and trailing spaces
154 $cmdline =~ s/^\s*(.*)\s*$/$1/;
156 if ($self->{state} eq 'page') {
157 my $i = $self->{pagelth};
158 my $ref = $self->{pagedata};
161 # abort if we get a line starting in with a
162 if ($cmdline =~ /^a/io) {
167 # send a tranche of data
168 while ($i-- > 0 && @$ref) {
169 my $line = shift @$ref;
170 $line =~ s/\s+$//o; # why am having to do this?
174 # reset state if none or else chuck out an intermediate prompt
176 $tot -= $self->{pagelth};
177 $self->send($self->msg('page', $tot));
179 $self->state('prompt');
181 } elsif ($self->{state} eq 'sysop') {
182 my $passwd = $self->{user}->passwd;
183 my @pw = split / */, $passwd;
185 my @l = @{$self->{passwd}};
186 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
187 if ($cmdline =~ /$str/) {
188 $self->{priv} = $self->{user}->priv;
190 $self->send($self->msg('sorry'));
193 $self->send($self->msg('sorry'));
195 delete $self->{passwd};
196 $self->state('prompt');
197 } elsif ($self->{state} eq 'talk') {
198 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
199 for (@{$self->{talklist}}) {
200 $self->send_talks($_, $self->msg('talkend'));
202 $self->state('prompt');
203 delete $self->{talklist};
204 } elsif ($cmdline =~ m(^/\w+)) {
206 $self->send_ans(run_cmd($self, $cmdline));
207 $self->send($self->talk_prompt);
208 } elsif ($self->{talklist} && @{$self->{talklist}}) {
209 # send what has been said to whoever is in this person's talk list
210 for (@{$self->{talklist}}) {
211 $self->send_talks($_, $cmdline);
213 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
216 $self->state('prompt');
219 $self->send_ans(run_cmd($self, $cmdline));
222 # send a prompt only if we are in a prompt state
223 $self->prompt() if $self->{state} =~ /^prompt/o;
226 # send out the talk messages taking into account vias and connectivity
229 my ($self, $ent, $line) = @_;
231 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
232 $to = $ent unless $to;
233 my $call = $via ? $via : $to;
234 my $clref = Route::get($call);
235 my $dxchan = $clref->dxchan if $clref;
237 $dxchan->talk($self->{call}, $to, $via, $line);
239 $self->send($self->msg('disc2', $via ? $via : $to));
240 my @l = grep { $_ ne $ent } @{$self->{talklist}};
242 $self->{talklist} = \@l;
244 delete $self->{talklist};
245 $self->state('prompt');
254 for (@{$self->{talklist}}) {
255 my ($to, $via) = /(\S+)>(\S+)/;
259 return $self->msg('talkprompt', join(',', @call));
263 # send a load of stuff to a command user with page prompting
271 if ($self->{pagelth} && @_ > $self->{pagelth}) {
273 for ($i = $self->{pagelth}; $i-- > 0; ) {
275 $line =~ s/\s+$//o; # why am having to do this?
278 $self->{pagedata} = [ @_ ];
279 $self->state('page');
280 $self->send($self->msg('page', scalar @_));
292 # this is the thing that runs the command, it is done like this for the
293 # benefit of remote command execution
299 my $user = $self->{user};
300 my $call = $self->{call};
305 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
306 dbg("stored func cmd = $c\n") if isdbg('eval');
309 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
313 return () if length $cmdline == 0;
316 $cmdline =~ s|//|/|og;
318 # split the command line up into parts, the first part is the command
319 my ($cmd, $args) = split /\s+/, $cmdline, 2;
320 $args = "" unless defined $args;
326 dbg("cmd: $cmd") if isdbg('command');
328 # alias it if possible
329 my $acmd = CmdAlias::get_cmd($cmd);
331 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
332 $args = "" unless defined $args;
333 dbg("aliased cmd: $cmd $args") if isdbg('command');
336 # first expand out the entry to a command
337 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
338 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
341 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
343 my $package = find_cmd_name($path, $fcmd);
344 @ans = (0) if !$package ;
347 dbg("package: $package") if isdbg('command');
349 unless (exists $Cache{$package}->{'sub'}) {
350 $c = eval $Cache{$package}->{'eval'};
352 return DXDebug::shortmess($@);
354 $Cache{$package}->{'sub'} = $c;
356 $c = $Cache{$package}->{'sub'};
358 @ans = &{$c}($self, $args);
363 return (DXDebug::shortmess($@));
367 dbg("cmd: $cmd not found") if isdbg('command');
368 if (++$self->{errors} > $maxerrors) {
369 $self->send($self->msg('e26'));
373 return ($self->msg('e1'));
381 delete $self->{errors};
383 if (++$self->{errors} > $maxerrors) {
384 $self->send($self->msg('e26'));
393 # This is called from inside the main cluster processing loop and is used
394 # for despatching commands that are doing some long processing job
399 my @dxchan = DXChannel->get_all();
402 foreach $dxchan (@dxchan) {
403 next if $dxchan->sort ne 'U';
405 # send a prompt if no activity out on this channel
406 if ($t >= $dxchan->t + $main::user_interval) {
407 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
412 while (my ($k, $v) = each %nothereslug) {
413 if ($main::systime >= $v + 300) {
414 delete $nothereslug{$k};
420 # finish up a user context
425 my $call = $self->call;
426 delete $self->{senddbg};
428 my @rout = $main::routeroot->del_user($call);
429 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
431 # issue a pc17 to everybody interested
432 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
434 # I was the last node visited
435 $self->user->node($main::mycall);
437 # send info to all logged in thingies
438 $self->tell_login('logoutu');
440 Log('DXCommand', "$call disconnected");
442 $self->SUPER::disconnect;
446 # short cut to output a prompt
452 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
455 # broadcast a message to all users [except those mentioned after buffer]
458 my $pkg = shift; # ignored
459 my $s = shift; # the line to be rebroadcast
461 foreach my $dxchan (DXChannel->get_all()) {
462 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
463 next if grep $dxchan == $_, @_;
464 $dxchan->send($s); # send it
468 # gimme all the users
471 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
474 # run a script for this user
478 my $silent = shift || 0;
483 # search for the command in the cache of short->long form commands
488 my ($path, $short_cmd, $suffix) = @_;
491 # commands are lower case
492 $short_cmd = lc $short_cmd;
493 dbg("command: $path $short_cmd\n") if isdbg('command');
495 # do some checking for funny characters
496 return () if $short_cmd =~ /\/$/;
498 # return immediately if we have it
499 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
500 if ($apath && $acmd) {
501 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
502 return ($apath, $acmd);
506 my @parts = split '/', $short_cmd;
513 for ($i = 0; $i < @parts; $i++) {
515 opendir(D, $curdir) or confess "can't open $curdir $!";
519 foreach $l (sort @ls) {
521 if ($i < $#parts) { # we are dealing with directories
522 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
523 dbg("got dir: $curdir/$l\n") if isdbg('command');
528 } else { # we are dealing with commands
529 @lparts = split /\./, $l;
530 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
531 if ($p eq substr($l, 0, length $p)) {
532 pop @lparts; # remove the suffix
533 $l = join '.', @lparts;
534 # chop $dirfn; # remove trailing /
535 $dirfn = "" unless $dirfn;
536 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
537 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
538 return ($path, "$dirfn$l");
546 # clear the command name cache
553 # the persistant execution of things from the command directories
556 # This allows perl programs to call functions dynamically
558 # This has been nicked directly from the perlembed pages
561 #require Devel::Symdump;
563 sub valid_package_name {
565 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
567 #second pass only for words starting with a digit
568 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
570 #Dress it up as a real package name
571 $string =~ s/\//_/og;
575 # find a cmd reference
576 # this is really for use in user written stubs
578 # use the result as a symbolic reference:-
581 # @out = &$r($self, $line);
590 # first expand out the entry to a command
591 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
592 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
594 # make sure it is loaded
595 $r = find_cmd_name($path, $fcmd);
601 # this bit of magic finds a command in the offered directory
605 my $package = valid_package_name($cmdname);
606 my $filename = "$path/$cmdname.pl";
607 my $mtime = -M $filename;
609 # return if we can't find it
611 unless (defined $mtime) {
612 $errstr = DXM::msg('e1');
616 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
617 #we have compiled this subroutine already,
618 #it has not been updated on disk, nothing left to do
619 #print STDERR "already compiled $package->handler\n";
623 my $sub = readfilestr($filename);
625 $errstr = "Syserr: can't open '$filename' $!";
629 #wrap the code into a subroutine inside our unique package
630 my $eval = qq( sub { $sub } );
633 my @list = split /\n/, $eval;
636 dbg($_ . "\n") if isdbg('eval');
640 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
648 my ($self, $let, $buf) = @_;
649 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
650 if ($self->{enhanced}) {
651 $self->send_later($let, $buf);
660 # send a talk message here
663 my ($self, $from, $to, $via, $line) = @_;
664 $line =~ s/\\5E/\^/g;
665 $self->local_send('T', "$to de $from: $line") if $self->{talk};
666 Log('talk', $to, $from, $main::mycall, $line);
667 # send a 'not here' message if required
668 unless ($self->{here} && $from ne $to) {
669 my $key = "$to$from";
670 unless (exists $nothereslug{$key}) {
672 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
673 my $name = $self->user->name || $to;
674 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
675 $nothereslug{$key} = $main::systime;
676 $dxchan->talk($to, $from, undef, $s);
693 if ($self->{annfilter}) {
694 ($filter, $hops) = $self->{annfilter}->it(@_ );
695 return unless $filter;
698 unless ($self->{ann}) {
699 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
701 return if $target eq 'SYSOP' && $self->{priv} < 5;
702 my $buf = "$to$target de $_[0]: $text";
704 $buf .= "\a\a" if $self->{beep};
705 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
716 return unless $self->{dx};
718 if ($self->{spotsfilter}) {
719 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
720 return unless $filter;
723 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
724 $buf .= "\a\a" if $self->{beep};
726 $self->local_send('X', $buf);
736 return unless $self->{wwv};
738 if ($self->{wwvfilter}) {
739 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
740 return unless $filter;
743 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
744 $buf .= "\a\a" if $self->{beep};
745 $self->local_send('V', $buf);
755 return unless $self->{wcy};
757 if ($self->{wcyfilter}) {
758 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
759 return unless $filter;
762 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
763 $buf .= "\a\a" if $self->{beep};
764 $self->local_send('Y', $buf);
767 # broadcast debug stuff to all interested parties
770 my $s = shift; # the line to be rebroadcast
772 foreach my $dxchan (DXChannel->get_all) {
773 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
774 $dxchan->send_later('L', $s);