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 $maxbadcount);
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
45 $maxbadcount = 3; # no of bad words allowed before disconnection
48 use vars qw($VERSION $BRANCH);
49 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
50 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
51 $main::build += $VERSION;
52 $main::branch += $BRANCH;
55 # obtain a new connection this is derived from dxchannel
60 my $self = DXChannel::alloc(@_);
62 # routing, this must go out here to prevent race condx
65 my @rout = $main::routeroot->add_user($call, Route::here(1));
66 DXProt::route_pc16($main::me, $main::routeroot, @rout) if @rout;
71 # this is how a a connection starts, you get a hello message and the motd with
72 # possibly some other messages asking you to set various things up if you are
73 # new (or nearly new and slacking) user.
77 my ($self, $line, $sort) = @_;
78 my $user = $self->{user};
79 my $call = $self->{call};
80 my $name = $user->{name};
83 my $host = $self->{conn}->{peerhost} || "unknown";
84 Log('DXCommand', "$call connected from $host");
86 $self->{name} = $name ? $name : $call;
87 $self->send($self->msg('l2',$self->{name}));
88 $self->send_file($main::motd) if (-e $main::motd);
89 $self->state('prompt'); # a bit of room for further expansion, passwords etc
90 $self->{priv} = $user->priv || 0;
91 $self->{lang} = $user->lang || $main::lang || 'en';
92 $self->{pagelth} = $user->pagelth || 20;
93 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
94 ($self->{width}) = $line =~ /width=(\d+)/;
95 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
96 $self->{consort} = $line; # save the connection type
98 # set some necessary flags on the user if they are connecting
99 $self->{beep} = $user->wantbeep;
100 $self->{ann} = $user->wantann;
101 $self->{wwv} = $user->wantwwv;
102 $self->{wcy} = $user->wantwcy;
103 $self->{talk} = $user->wanttalk;
104 $self->{wx} = $user->wantwx;
105 $self->{dx} = $user->wantdx;
106 $self->{logininfo} = $user->wantlogininfo;
107 $self->{ann_talk} = $user->wantann_talk;
111 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
112 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
113 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
114 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
116 # clean up qra locators
117 my $qra = $user->qra;
118 $qra = undef if ($qra && !DXBearing::is_qra($qra));
120 my $lat = $user->lat;
121 my $long = $user->long;
122 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
126 if (!$user->wantecho) {
127 $self->send_now('E', "0");
128 $self->send($self->msg('echow'));
131 $self->tell_login('loginu');
133 # do we need to send a forward/opernam?
134 my $lastoper = $user->lastoper || 0;
135 my $homenode = $user->homenode || "";
136 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
137 run_cmd($main::me, "forward/opernam $call");
138 $user->lastoper($main::systime);
141 # run a script send the output to the punter
142 my $script = new Script(lc $call) || new Script('user_default');
143 $script->run($self) if $script;
146 my $info = Route::cluster();
147 $self->send("Cluster:$info");
149 # send prompts and things
150 $self->send($self->msg('namee1')) if !$user->name;
151 $self->send($self->msg('qthe1')) if !$user->qth;
152 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
153 $self->send($self->msg('hnodee1')) if !$user->qth;
154 $self->send($self->msg('m9')) if DXMsg::for_me($call);
159 # This is the normal command prompt driver
168 # remove leading and trailing spaces
169 $cmdline =~ s/^\s*(.*)\s*$/$1/;
171 if ($self->{state} eq 'page') {
172 my $i = $self->{pagelth};
173 my $ref = $self->{pagedata};
176 # abort if we get a line starting in with a
177 if ($cmdline =~ /^a/io) {
182 # send a tranche of data
183 while ($i-- > 0 && @$ref) {
184 my $line = shift @$ref;
185 $line =~ s/\s+$//o; # why am having to do this?
189 # reset state if none or else chuck out an intermediate prompt
191 $tot -= $self->{pagelth};
192 $self->send($self->msg('page', $tot));
194 $self->state('prompt');
196 } elsif ($self->{state} eq 'sysop') {
197 my $passwd = $self->{user}->passwd;
198 my @pw = split / */, $passwd;
200 my @l = @{$self->{passwd}};
201 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
202 if ($cmdline =~ /$str/) {
203 $self->{priv} = $self->{user}->priv;
205 $self->send($self->msg('sorry'));
208 $self->send($self->msg('sorry'));
210 delete $self->{passwd};
211 $self->state('prompt');
212 } elsif ($self->{state} eq 'talk') {
213 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
214 for (@{$self->{talklist}}) {
215 $self->send_talks($_, $self->msg('talkend'));
217 $self->state('prompt');
218 delete $self->{talklist};
219 } elsif ($cmdline =~ m(^/\w+)) {
221 $self->send_ans(run_cmd($self, $cmdline));
222 $self->send($self->talk_prompt);
223 } elsif ($self->{talklist} && @{$self->{talklist}}) {
224 # send what has been said to whoever is in this person's talk list
225 for (@{$self->{talklist}}) {
226 $self->send_talks($_, $cmdline);
228 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
231 $self->state('prompt');
233 } elsif (my $func = $self->{func}) {
236 if (ref $self->{edit}) {
237 eval { @ans = $self->{edit}->$func($self, $cmdline)};
239 eval { @ans = &{$self->{func}}($self, $cmdline) };
241 $self->send_ans("Syserr: on stored func $self->{func}", $@) if $@;
242 $self->send_ans(@ans);
244 $self->send_ans(run_cmd($self, $cmdline));
247 # check for excessive swearing
248 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
249 Log('DXCommand', "$self->{call} logged out for excessive swearing");
254 # send a prompt only if we are in a prompt state
255 $self->prompt() if $self->{state} =~ /^prompt/o;
258 # send out the talk messages taking into account vias and connectivity
261 my ($self, $ent, $line) = @_;
263 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
264 $to = $ent unless $to;
265 my $call = $via ? $via : $to;
266 my $clref = Route::get($call);
267 my $dxchan = $clref->dxchan if $clref;
269 $dxchan->talk($self->{call}, $to, $via, $line);
271 $self->send($self->msg('disc2', $via ? $via : $to));
272 my @l = grep { $_ ne $ent } @{$self->{talklist}};
274 $self->{talklist} = \@l;
276 delete $self->{talklist};
277 $self->state('prompt');
286 for (@{$self->{talklist}}) {
287 my ($to, $via) = /(\S+)>(\S+)/;
291 return $self->msg('talkprompt', join(',', @call));
295 # send a load of stuff to a command user with page prompting
303 if ($self->{pagelth} && @_ > $self->{pagelth}) {
305 for ($i = $self->{pagelth}; $i-- > 0; ) {
307 $line =~ s/\s+$//o; # why am having to do this?
310 $self->{pagedata} = [ @_ ];
311 $self->state('page');
312 $self->send($self->msg('page', scalar @_));
324 # this is the thing that runs the command, it is done like this for the
325 # benefit of remote command execution
331 my $user = $self->{user};
332 my $call = $self->{call};
337 return () if length $cmdline == 0;
340 $cmdline =~ s|//|/|og;
342 # split the command line up into parts, the first part is the command
343 my ($cmd, $args) = split /\s+/, $cmdline, 2;
344 $args = "" unless defined $args;
350 dbg("cmd: $cmd") if isdbg('command');
352 # alias it if possible
353 my $acmd = CmdAlias::get_cmd($cmd);
355 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
356 $args = "" unless defined $args;
357 dbg("aliased cmd: $cmd $args") if isdbg('command');
360 # first expand out the entry to a command
361 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
362 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
365 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
367 my $package = find_cmd_name($path, $fcmd);
372 dbg("package: $package") if isdbg('command');
373 eval { @ans = &$package($self, $args) };
374 return (DXDebug::shortmess($@)) if $@;
377 dbg("cmd: $cmd not found") if isdbg('command');
378 if (++$self->{errors} > $maxerrors) {
379 $self->send($self->msg('e26'));
383 return ($self->msg('e1'));
390 delete $self->{errors};
392 if (++$self->{errors} > $maxerrors) {
393 $self->send($self->msg('e26'));
402 # This is called from inside the main cluster processing loop and is used
403 # for despatching commands that are doing some long processing job
408 my @dxchan = DXChannel->get_all();
411 foreach $dxchan (@dxchan) {
412 next if $dxchan->sort ne 'U';
414 # send a prompt if no activity out on this channel
415 if ($t >= $dxchan->t + $main::user_interval) {
416 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
421 while (my ($k, $v) = each %nothereslug) {
422 if ($main::systime >= $v + 300) {
423 delete $nothereslug{$k};
429 # finish up a user context
434 my $call = $self->call;
436 return if $self->{disconnecting}++;
438 delete $self->{senddbg};
440 my $uref = Route::User::get($call);
443 @rout = $main::routeroot->del_user($uref);
444 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
446 confess "trying to disconnect a non existant user $call";
449 # issue a pc17 to everybody interested
450 DXProt::route_pc17($main::me, $main::routeroot, @rout) if @rout;
452 # I was the last node visited
453 $self->user->node($main::mycall);
455 # send info to all logged in thingies
456 $self->tell_login('logoutu');
458 Log('DXCommand', "$call disconnected");
460 $self->SUPER::disconnect;
464 # short cut to output a prompt
470 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
473 # broadcast a message to all users [except those mentioned after buffer]
476 my $pkg = shift; # ignored
477 my $s = shift; # the line to be rebroadcast
479 foreach my $dxchan (DXChannel->get_all()) {
480 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
481 next if grep $dxchan == $_, @_;
482 $dxchan->send($s); # send it
486 # gimme all the users
489 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
492 # run a script for this user
496 my $silent = shift || 0;
501 # search for the command in the cache of short->long form commands
506 my ($path, $short_cmd, $suffix) = @_;
509 # commands are lower case
510 $short_cmd = lc $short_cmd;
511 dbg("command: $path $short_cmd\n") if isdbg('command');
513 # do some checking for funny characters
514 return () if $short_cmd =~ /\/$/;
516 # return immediately if we have it
517 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
518 if ($apath && $acmd) {
519 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
520 return ($apath, $acmd);
524 my @parts = split '/', $short_cmd;
531 for ($i = 0; $i < @parts; $i++) {
533 opendir(D, $curdir) or confess "can't open $curdir $!";
537 foreach $l (sort @ls) {
539 if ($i < $#parts) { # we are dealing with directories
540 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
541 dbg("got dir: $curdir/$l\n") if isdbg('command');
546 } else { # we are dealing with commands
547 @lparts = split /\./, $l;
548 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
549 if ($p eq substr($l, 0, length $p)) {
550 pop @lparts; # remove the suffix
551 $l = join '.', @lparts;
552 # chop $dirfn; # remove trailing /
553 $dirfn = "" unless $dirfn;
554 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
555 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
556 return ($path, "$dirfn$l");
564 # clear the command name cache
571 dbg("Undefining cmd $_") if isdbg('command');
578 # the persistant execution of things from the command directories
581 # This allows perl programs to call functions dynamically
583 # This has been nicked directly from the perlembed pages
586 #require Devel::Symdump;
588 sub valid_package_name {
590 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
593 return "cmd_$string";
597 # this bit of magic finds a command in the offered directory
601 my $package = valid_package_name($cmdname);
602 my $filename = "$path/$cmdname.pl";
603 my $mtime = -M $filename;
605 # return if we can't find it
607 unless (defined $mtime) {
608 $errstr = DXM::msg('e1');
612 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
613 #we have compiled this subroutine already,
614 #it has not been updated on disk, nothing left to do
615 #print STDERR "already compiled $package->handler\n";
619 my $sub = readfilestr($filename);
621 $errstr = "Syserr: can't open '$filename' $!";
625 #wrap the code into a subroutine inside our unique package
626 my $eval = qq( sub $package { $sub } );
629 my @list = split /\n/, $eval;
632 dbg($_ . "\n") if isdbg('eval');
636 # get rid of any existing sub and try to compile the new one
639 if (exists $Cache{$package}) {
640 dbg("Redefining $package") if isdbg('command');
643 dbg("Defining $package") if isdbg('command');
647 $Cache{$package} = {mtime => $mtime };
656 my ($self, $let, $buf) = @_;
657 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
658 if ($self->{enhanced}) {
659 $self->send_later($let, $buf);
668 # send a talk message here
671 my ($self, $from, $to, $via, $line) = @_;
672 $line =~ s/\\5E/\^/g;
673 $self->local_send('T', "$to de $from: $line") if $self->{talk};
674 Log('talk', $to, $from, $main::mycall, $line);
675 # send a 'not here' message if required
676 unless ($self->{here} && $from ne $to) {
677 my $key = "$to$from";
678 unless (exists $nothereslug{$key}) {
680 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
681 my $name = $self->user->name || $to;
682 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
683 $nothereslug{$key} = $main::systime;
684 $dxchan->talk($to, $from, undef, $s);
701 if (!$self->{ann_talk} && $to ne $self->{call}) {
702 my $call = AnnTalk::is_talk_candidate($_[0], $text);
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);