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};
79 my $host = $self->{conn}->{peerhost} || "unknown";
80 Log('DXCommand', "$call connected from $host");
82 $self->{name} = $name ? $name : $call;
83 $self->send($self->msg('l2',$self->{name}));
84 $self->send_file($main::motd) if (-e $main::motd);
85 $self->state('prompt'); # a bit of room for further expansion, passwords etc
86 $self->{priv} = $user->priv || 0;
87 $self->{lang} = $user->lang || $main::lang || 'en';
88 $self->{pagelth} = $user->pagelth || 20;
89 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
90 ($self->{width}) = $line =~ /width=(\d+)/;
91 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
92 $self->{consort} = $line; # save the connection type
94 # set some necessary flags on the user if they are connecting
95 $self->{beep} = $user->wantbeep;
96 $self->{ann} = $user->wantann;
97 $self->{wwv} = $user->wantwwv;
98 $self->{wcy} = $user->wantwcy;
99 $self->{talk} = $user->wanttalk;
100 $self->{wx} = $user->wantwx;
101 $self->{dx} = $user->wantdx;
102 $self->{logininfo} = $user->wantlogininfo;
106 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
107 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
108 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
109 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
111 # clean up qra locators
112 my $qra = $user->qra;
113 $qra = undef if ($qra && !DXBearing::is_qra($qra));
115 my $lat = $user->lat;
116 my $long = $user->long;
117 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
120 # send prompts and things
121 my $info = Route::cluster();
122 $self->send("Cluster:$info");
123 $self->send($self->msg('namee1')) if !$user->name;
124 $self->send($self->msg('qthe1')) if !$user->qth;
125 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
126 $self->send($self->msg('hnodee1')) if !$user->qth;
127 $self->send($self->msg('m9')) if DXMsg::for_me($call);
131 if (!$user->wantecho) {
132 $self->send_now('E', "0");
133 $self->send($self->msg('echow'));
136 $self->tell_login('loginu');
138 # do we need to send a forward/opernam?
139 my $lastoper = $user->lastoper || 0;
140 my $homenode = $user->homenode || "";
141 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
142 run_cmd($DXProt::me, "forward/opernam $call");
143 $user->lastoper($main::systime);
148 # This is the normal command prompt driver
157 # remove leading and trailing spaces
158 $cmdline =~ s/^\s*(.*)\s*$/$1/;
160 if ($self->{state} eq 'page') {
161 my $i = $self->{pagelth};
162 my $ref = $self->{pagedata};
165 # abort if we get a line starting in with a
166 if ($cmdline =~ /^a/io) {
171 # send a tranche of data
172 while ($i-- > 0 && @$ref) {
173 my $line = shift @$ref;
174 $line =~ s/\s+$//o; # why am having to do this?
178 # reset state if none or else chuck out an intermediate prompt
180 $tot -= $self->{pagelth};
181 $self->send($self->msg('page', $tot));
183 $self->state('prompt');
185 } elsif ($self->{state} eq 'sysop') {
186 my $passwd = $self->{user}->passwd;
187 my @pw = split / */, $passwd;
189 my @l = @{$self->{passwd}};
190 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
191 if ($cmdline =~ /$str/) {
192 $self->{priv} = $self->{user}->priv;
194 $self->send($self->msg('sorry'));
197 $self->send($self->msg('sorry'));
199 delete $self->{passwd};
200 $self->state('prompt');
201 } elsif ($self->{state} eq 'talk') {
202 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
203 for (@{$self->{talklist}}) {
204 $self->send_talks($_, $self->msg('talkend'));
206 $self->state('prompt');
207 delete $self->{talklist};
208 } elsif ($cmdline =~ m(^/\w+)) {
210 $self->send_ans(run_cmd($self, $cmdline));
211 $self->send($self->talk_prompt);
212 } elsif ($self->{talklist} && @{$self->{talklist}}) {
213 # send what has been said to whoever is in this person's talk list
214 for (@{$self->{talklist}}) {
215 $self->send_talks($_, $cmdline);
217 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
220 $self->state('prompt');
223 $self->send_ans(run_cmd($self, $cmdline));
226 # send a prompt only if we are in a prompt state
227 $self->prompt() if $self->{state} =~ /^prompt/o;
230 # send out the talk messages taking into account vias and connectivity
233 my ($self, $ent, $line) = @_;
235 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
236 $to = $ent unless $to;
237 my $call = $via ? $via : $to;
238 my $clref = Route::get($call);
239 my $dxchan = $clref->dxchan if $clref;
241 $dxchan->talk($self->{call}, $to, $via, $line);
243 $self->send($self->msg('disc2', $via ? $via : $to));
244 my @l = grep { $_ ne $ent } @{$self->{talklist}};
246 $self->{talklist} = \@l;
248 delete $self->{talklist};
249 $self->state('prompt');
258 for (@{$self->{talklist}}) {
259 my ($to, $via) = /(\S+)>(\S+)/;
263 return $self->msg('talkprompt', join(',', @call));
267 # send a load of stuff to a command user with page prompting
275 if ($self->{pagelth} && @_ > $self->{pagelth}) {
277 for ($i = $self->{pagelth}; $i-- > 0; ) {
279 $line =~ s/\s+$//o; # why am having to do this?
282 $self->{pagedata} = [ @_ ];
283 $self->state('page');
284 $self->send($self->msg('page', scalar @_));
296 # this is the thing that runs the command, it is done like this for the
297 # benefit of remote command execution
303 my $user = $self->{user};
304 my $call = $self->{call};
309 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
310 dbg("stored func cmd = $c\n") if isdbg('eval');
313 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
317 return () if length $cmdline == 0;
320 $cmdline =~ s|//|/|og;
322 # split the command line up into parts, the first part is the command
323 my ($cmd, $args) = split /\s+/, $cmdline, 2;
324 $args = "" unless defined $args;
330 dbg("cmd: $cmd") if isdbg('command');
332 # alias it if possible
333 my $acmd = CmdAlias::get_cmd($cmd);
335 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
336 $args = "" unless defined $args;
337 dbg("aliased cmd: $cmd $args") if isdbg('command');
340 # first expand out the entry to a command
341 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
342 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
345 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
347 my $package = find_cmd_name($path, $fcmd);
348 @ans = (0) if !$package ;
351 dbg("package: $package") if isdbg('command');
353 unless (exists $Cache{$package}->{'sub'}) {
354 $c = eval $Cache{$package}->{'eval'};
356 return DXDebug::shortmess($@);
358 $Cache{$package}->{'sub'} = $c;
360 $c = $Cache{$package}->{'sub'};
362 @ans = &{$c}($self, $args);
367 return (DXDebug::shortmess($@));
371 dbg("cmd: $cmd not found") if isdbg('command');
372 if (++$self->{errors} > $maxerrors) {
373 $self->send($self->msg('e26'));
377 return ($self->msg('e1'));
385 delete $self->{errors};
387 if (++$self->{errors} > $maxerrors) {
388 $self->send($self->msg('e26'));
397 # This is called from inside the main cluster processing loop and is used
398 # for despatching commands that are doing some long processing job
403 my @dxchan = DXChannel->get_all();
406 foreach $dxchan (@dxchan) {
407 next if $dxchan->sort ne 'U';
409 # send a prompt if no activity out on this channel
410 if ($t >= $dxchan->t + $main::user_interval) {
411 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
416 while (my ($k, $v) = each %nothereslug) {
417 if ($main::systime >= $v + 300) {
418 delete $nothereslug{$k};
424 # finish up a user context
429 my $call = $self->call;
431 return if $self->{disconnecting}++;
433 delete $self->{senddbg};
435 my $uref = Route::User::get($call);
438 @rout = $main::routeroot->del_user($uref);
439 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
441 confess "trying to disconnect a non existant user $call";
444 # issue a pc17 to everybody interested
445 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
447 # I was the last node visited
448 $self->user->node($main::mycall);
450 # send info to all logged in thingies
451 $self->tell_login('logoutu');
453 Log('DXCommand', "$call disconnected");
455 $self->SUPER::disconnect;
459 # short cut to output a prompt
465 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
468 # broadcast a message to all users [except those mentioned after buffer]
471 my $pkg = shift; # ignored
472 my $s = shift; # the line to be rebroadcast
474 foreach my $dxchan (DXChannel->get_all()) {
475 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
476 next if grep $dxchan == $_, @_;
477 $dxchan->send($s); # send it
481 # gimme all the users
484 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
487 # run a script for this user
491 my $silent = shift || 0;
496 # search for the command in the cache of short->long form commands
501 my ($path, $short_cmd, $suffix) = @_;
504 # commands are lower case
505 $short_cmd = lc $short_cmd;
506 dbg("command: $path $short_cmd\n") if isdbg('command');
508 # do some checking for funny characters
509 return () if $short_cmd =~ /\/$/;
511 # return immediately if we have it
512 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
513 if ($apath && $acmd) {
514 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
515 return ($apath, $acmd);
519 my @parts = split '/', $short_cmd;
526 for ($i = 0; $i < @parts; $i++) {
528 opendir(D, $curdir) or confess "can't open $curdir $!";
532 foreach $l (sort @ls) {
534 if ($i < $#parts) { # we are dealing with directories
535 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
536 dbg("got dir: $curdir/$l\n") if isdbg('command');
541 } else { # we are dealing with commands
542 @lparts = split /\./, $l;
543 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
544 if ($p eq substr($l, 0, length $p)) {
545 pop @lparts; # remove the suffix
546 $l = join '.', @lparts;
547 # chop $dirfn; # remove trailing /
548 $dirfn = "" unless $dirfn;
549 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
550 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
551 return ($path, "$dirfn$l");
559 # clear the command name cache
566 # the persistant execution of things from the command directories
569 # This allows perl programs to call functions dynamically
571 # This has been nicked directly from the perlembed pages
574 #require Devel::Symdump;
576 sub valid_package_name {
578 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
580 #second pass only for words starting with a digit
581 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
583 #Dress it up as a real package name
584 $string =~ s/\//_/og;
588 # find a cmd reference
589 # this is really for use in user written stubs
591 # use the result as a symbolic reference:-
594 # @out = &$r($self, $line);
603 # first expand out the entry to a command
604 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
605 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
607 # make sure it is loaded
608 $r = find_cmd_name($path, $fcmd);
614 # this bit of magic finds a command in the offered directory
618 my $package = valid_package_name($cmdname);
619 my $filename = "$path/$cmdname.pl";
620 my $mtime = -M $filename;
622 # return if we can't find it
624 unless (defined $mtime) {
625 $errstr = DXM::msg('e1');
629 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
630 #we have compiled this subroutine already,
631 #it has not been updated on disk, nothing left to do
632 #print STDERR "already compiled $package->handler\n";
636 my $sub = readfilestr($filename);
638 $errstr = "Syserr: can't open '$filename' $!";
642 #wrap the code into a subroutine inside our unique package
643 my $eval = qq( sub { $sub } );
646 my @list = split /\n/, $eval;
649 dbg($_ . "\n") if isdbg('eval');
653 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
661 my ($self, $let, $buf) = @_;
662 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
663 if ($self->{enhanced}) {
664 $self->send_later($let, $buf);
673 # send a talk message here
676 my ($self, $from, $to, $via, $line) = @_;
677 $line =~ s/\\5E/\^/g;
678 $self->local_send('T', "$to de $from: $line") if $self->{talk};
679 Log('talk', $to, $from, $main::mycall, $line);
680 # send a 'not here' message if required
681 unless ($self->{here} && $from ne $to) {
682 my $key = "$to$from";
683 unless (exists $nothereslug{$key}) {
685 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
686 my $name = $self->user->name || $to;
687 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
688 $nothereslug{$key} = $main::systime;
689 $dxchan->talk($to, $from, undef, $s);
706 if ($self->{annfilter}) {
707 ($filter, $hops) = $self->{annfilter}->it(@_ );
708 return unless $filter;
711 unless ($self->{ann}) {
712 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
714 return if $target eq 'SYSOP' && $self->{priv} < 5;
715 my $buf = "$to$target de $_[0]: $text";
717 $buf .= "\a\a" if $self->{beep};
718 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
729 return unless $self->{dx};
731 if ($self->{spotsfilter}) {
732 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
733 return unless $filter;
736 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
737 $buf .= "\a\a" if $self->{beep};
739 $self->local_send('X', $buf);
749 return unless $self->{wwv};
751 if ($self->{wwvfilter}) {
752 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
753 return unless $filter;
756 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
757 $buf .= "\a\a" if $self->{beep};
758 $self->local_send('V', $buf);
768 return unless $self->{wcy};
770 if ($self->{wcyfilter}) {
771 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
772 return unless $filter;
775 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
776 $buf .= "\a\a" if $self->{beep};
777 $self->local_send('Y', $buf);
780 # broadcast debug stuff to all interested parties
783 my $s = shift; # the line to be rebroadcast
785 foreach my $dxchan (DXChannel->get_all) {
786 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
787 $dxchan->send_later('L', $s);