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 $suppress_ann_to_talk);
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
43 $suppress_ann_to_talk = 1; # don't announce 'to <call> ' or '<call> ' type announcements
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;
108 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
109 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
110 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
111 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
113 # clean up qra locators
114 my $qra = $user->qra;
115 $qra = undef if ($qra && !DXBearing::is_qra($qra));
117 my $lat = $user->lat;
118 my $long = $user->long;
119 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
122 # send prompts and things
123 my $info = Route::cluster();
124 $self->send("Cluster:$info");
125 $self->send($self->msg('namee1')) if !$user->name;
126 $self->send($self->msg('qthe1')) if !$user->qth;
127 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
128 $self->send($self->msg('hnodee1')) if !$user->qth;
129 $self->send($self->msg('m9')) if DXMsg::for_me($call);
133 if (!$user->wantecho) {
134 $self->send_now('E', "0");
135 $self->send($self->msg('echow'));
138 $self->tell_login('loginu');
140 # do we need to send a forward/opernam?
141 my $lastoper = $user->lastoper || 0;
142 my $homenode = $user->homenode || "";
143 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
144 run_cmd($DXProt::me, "forward/opernam $call");
145 $user->lastoper($main::systime);
150 # This is the normal command prompt driver
159 # remove leading and trailing spaces
160 $cmdline =~ s/^\s*(.*)\s*$/$1/;
162 if ($self->{state} eq 'page') {
163 my $i = $self->{pagelth};
164 my $ref = $self->{pagedata};
167 # abort if we get a line starting in with a
168 if ($cmdline =~ /^a/io) {
173 # send a tranche of data
174 while ($i-- > 0 && @$ref) {
175 my $line = shift @$ref;
176 $line =~ s/\s+$//o; # why am having to do this?
180 # reset state if none or else chuck out an intermediate prompt
182 $tot -= $self->{pagelth};
183 $self->send($self->msg('page', $tot));
185 $self->state('prompt');
187 } elsif ($self->{state} eq 'sysop') {
188 my $passwd = $self->{user}->passwd;
189 my @pw = split / */, $passwd;
191 my @l = @{$self->{passwd}};
192 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
193 if ($cmdline =~ /$str/) {
194 $self->{priv} = $self->{user}->priv;
196 $self->send($self->msg('sorry'));
199 $self->send($self->msg('sorry'));
201 delete $self->{passwd};
202 $self->state('prompt');
203 } elsif ($self->{state} eq 'talk') {
204 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
205 for (@{$self->{talklist}}) {
206 $self->send_talks($_, $self->msg('talkend'));
208 $self->state('prompt');
209 delete $self->{talklist};
210 } elsif ($cmdline =~ m(^/\w+)) {
212 $self->send_ans(run_cmd($self, $cmdline));
213 $self->send($self->talk_prompt);
214 } elsif ($self->{talklist} && @{$self->{talklist}}) {
215 # send what has been said to whoever is in this person's talk list
216 for (@{$self->{talklist}}) {
217 $self->send_talks($_, $cmdline);
219 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
222 $self->state('prompt');
225 $self->send_ans(run_cmd($self, $cmdline));
228 # send a prompt only if we are in a prompt state
229 $self->prompt() if $self->{state} =~ /^prompt/o;
232 # send out the talk messages taking into account vias and connectivity
235 my ($self, $ent, $line) = @_;
237 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
238 $to = $ent unless $to;
239 my $call = $via ? $via : $to;
240 my $clref = Route::get($call);
241 my $dxchan = $clref->dxchan if $clref;
243 $dxchan->talk($self->{call}, $to, $via, $line);
245 $self->send($self->msg('disc2', $via ? $via : $to));
246 my @l = grep { $_ ne $ent } @{$self->{talklist}};
248 $self->{talklist} = \@l;
250 delete $self->{talklist};
251 $self->state('prompt');
260 for (@{$self->{talklist}}) {
261 my ($to, $via) = /(\S+)>(\S+)/;
265 return $self->msg('talkprompt', join(',', @call));
269 # send a load of stuff to a command user with page prompting
277 if ($self->{pagelth} && @_ > $self->{pagelth}) {
279 for ($i = $self->{pagelth}; $i-- > 0; ) {
281 $line =~ s/\s+$//o; # why am having to do this?
284 $self->{pagedata} = [ @_ ];
285 $self->state('page');
286 $self->send($self->msg('page', scalar @_));
298 # this is the thing that runs the command, it is done like this for the
299 # benefit of remote command execution
305 my $user = $self->{user};
306 my $call = $self->{call};
311 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
312 dbg("stored func cmd = $c\n") if isdbg('eval');
315 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
319 return () if length $cmdline == 0;
322 $cmdline =~ s|//|/|og;
324 # split the command line up into parts, the first part is the command
325 my ($cmd, $args) = split /\s+/, $cmdline, 2;
326 $args = "" unless defined $args;
332 dbg("cmd: $cmd") if isdbg('command');
334 # alias it if possible
335 my $acmd = CmdAlias::get_cmd($cmd);
337 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
338 $args = "" unless defined $args;
339 dbg("aliased cmd: $cmd $args") if isdbg('command');
342 # first expand out the entry to a command
343 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
344 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
347 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
349 my $package = find_cmd_name($path, $fcmd);
350 @ans = (0) if !$package ;
353 dbg("package: $package") if isdbg('command');
355 unless (exists $Cache{$package}->{'sub'}) {
356 $c = eval $Cache{$package}->{'eval'};
358 return DXDebug::shortmess($@);
360 $Cache{$package}->{'sub'} = $c;
362 $c = $Cache{$package}->{'sub'};
364 @ans = &{$c}($self, $args);
369 return (DXDebug::shortmess($@));
373 dbg("cmd: $cmd not found") if isdbg('command');
374 if (++$self->{errors} > $maxerrors) {
375 $self->send($self->msg('e26'));
379 return ($self->msg('e1'));
387 delete $self->{errors};
389 if (++$self->{errors} > $maxerrors) {
390 $self->send($self->msg('e26'));
399 # This is called from inside the main cluster processing loop and is used
400 # for despatching commands that are doing some long processing job
405 my @dxchan = DXChannel->get_all();
408 foreach $dxchan (@dxchan) {
409 next if $dxchan->sort ne 'U';
411 # send a prompt if no activity out on this channel
412 if ($t >= $dxchan->t + $main::user_interval) {
413 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
418 while (my ($k, $v) = each %nothereslug) {
419 if ($main::systime >= $v + 300) {
420 delete $nothereslug{$k};
426 # finish up a user context
431 my $call = $self->call;
433 return if $self->{disconnecting}++;
435 delete $self->{senddbg};
437 my $uref = Route::User::get($call);
440 @rout = $main::routeroot->del_user($uref);
441 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
443 confess "trying to disconnect a non existant user $call";
446 # issue a pc17 to everybody interested
447 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
449 # I was the last node visited
450 $self->user->node($main::mycall);
452 # send info to all logged in thingies
453 $self->tell_login('logoutu');
455 Log('DXCommand', "$call disconnected");
457 $self->SUPER::disconnect;
461 # short cut to output a prompt
467 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
470 # broadcast a message to all users [except those mentioned after buffer]
473 my $pkg = shift; # ignored
474 my $s = shift; # the line to be rebroadcast
476 foreach my $dxchan (DXChannel->get_all()) {
477 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
478 next if grep $dxchan == $_, @_;
479 $dxchan->send($s); # send it
483 # gimme all the users
486 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
489 # run a script for this user
493 my $silent = shift || 0;
498 # search for the command in the cache of short->long form commands
503 my ($path, $short_cmd, $suffix) = @_;
506 # commands are lower case
507 $short_cmd = lc $short_cmd;
508 dbg("command: $path $short_cmd\n") if isdbg('command');
510 # do some checking for funny characters
511 return () if $short_cmd =~ /\/$/;
513 # return immediately if we have it
514 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
515 if ($apath && $acmd) {
516 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
517 return ($apath, $acmd);
521 my @parts = split '/', $short_cmd;
528 for ($i = 0; $i < @parts; $i++) {
530 opendir(D, $curdir) or confess "can't open $curdir $!";
534 foreach $l (sort @ls) {
536 if ($i < $#parts) { # we are dealing with directories
537 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
538 dbg("got dir: $curdir/$l\n") if isdbg('command');
543 } else { # we are dealing with commands
544 @lparts = split /\./, $l;
545 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
546 if ($p eq substr($l, 0, length $p)) {
547 pop @lparts; # remove the suffix
548 $l = join '.', @lparts;
549 # chop $dirfn; # remove trailing /
550 $dirfn = "" unless $dirfn;
551 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
552 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
553 return ($path, "$dirfn$l");
561 # clear the command name cache
568 # the persistant execution of things from the command directories
571 # This allows perl programs to call functions dynamically
573 # This has been nicked directly from the perlembed pages
576 #require Devel::Symdump;
578 sub valid_package_name {
580 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
582 #second pass only for words starting with a digit
583 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
585 #Dress it up as a real package name
586 $string =~ s/\//_/og;
590 # find a cmd reference
591 # this is really for use in user written stubs
593 # use the result as a symbolic reference:-
596 # @out = &$r($self, $line);
605 # first expand out the entry to a command
606 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
607 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
609 # make sure it is loaded
610 $r = find_cmd_name($path, $fcmd);
616 # this bit of magic finds a command in the offered directory
620 my $package = valid_package_name($cmdname);
621 my $filename = "$path/$cmdname.pl";
622 my $mtime = -M $filename;
624 # return if we can't find it
626 unless (defined $mtime) {
627 $errstr = DXM::msg('e1');
631 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
632 #we have compiled this subroutine already,
633 #it has not been updated on disk, nothing left to do
634 #print STDERR "already compiled $package->handler\n";
638 my $sub = readfilestr($filename);
640 $errstr = "Syserr: can't open '$filename' $!";
644 #wrap the code into a subroutine inside our unique package
645 my $eval = qq( sub { $sub } );
648 my @list = split /\n/, $eval;
651 dbg($_ . "\n") if isdbg('eval');
655 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
663 my ($self, $let, $buf) = @_;
664 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
665 if ($self->{enhanced}) {
666 $self->send_later($let, $buf);
675 # send a talk message here
678 my ($self, $from, $to, $via, $line) = @_;
679 $line =~ s/\\5E/\^/g;
680 $self->local_send('T', "$to de $from: $line") if $self->{talk};
681 Log('talk', $to, $from, $main::mycall, $line);
682 # send a 'not here' message if required
683 unless ($self->{here} && $from ne $to) {
684 my $key = "$to$from";
685 unless (exists $nothereslug{$key}) {
687 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
688 my $name = $self->user->name || $to;
689 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
690 $nothereslug{$key} = $main::systime;
691 $dxchan->talk($to, $from, undef, $s);
708 if ($suppress_ann_to_talk) {
709 my ($to, $call) = $text =~ /^\s*([\w-]+)[\s:]+([\w-]+)/;
710 return if ($to && $call && ((uc $to =~ /^TO?$/ && is_callsign(uc $call)) || is_callsign($call = uc $to)));
713 if ($self->{annfilter}) {
714 ($filter, $hops) = $self->{annfilter}->it(@_ );
715 return unless $filter;
718 unless ($self->{ann}) {
719 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
721 return if $target eq 'SYSOP' && $self->{priv} < 5;
722 my $buf = "$to$target de $_[0]: $text";
724 $buf .= "\a\a" if $self->{beep};
725 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
736 return unless $self->{dx};
738 if ($self->{spotsfilter}) {
739 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
740 return unless $filter;
743 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
744 $buf .= "\a\a" if $self->{beep};
746 $self->local_send('X', $buf);
756 return unless $self->{wwv};
758 if ($self->{wwvfilter}) {
759 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
760 return unless $filter;
763 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
764 $buf .= "\a\a" if $self->{beep};
765 $self->local_send('V', $buf);
775 return unless $self->{wcy};
777 if ($self->{wcyfilter}) {
778 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
779 return unless $filter;
782 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
783 $buf .= "\a\a" if $self->{beep};
784 $self->local_send('Y', $buf);
787 # broadcast debug stuff to all interested parties
790 my $s = shift; # the line to be rebroadcast
792 foreach my $dxchan (DXChannel->get_all) {
793 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
794 $dxchan->send_later('L', $s);