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
226 if (@bad = BadWords::check($cmdline)) {
227 $self->badcount(($self->badcount||0) + @bad);
228 Log('DXCommand', "$self->{call} swore: $cmdline");
230 for (@{$self->{talklist}}) {
231 $self->send_talks($_, $cmdline);
234 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
237 $self->state('prompt');
239 } elsif (my $func = $self->{func}) {
242 if (ref $self->{edit}) {
243 eval { @ans = $self->{edit}->$func($self, $cmdline)};
245 eval { @ans = &{$self->{func}}($self, $cmdline) };
247 $self->send_ans("Syserr: on stored func $self->{func}", $@) if $@;
248 $self->send_ans(@ans);
250 $self->send_ans(run_cmd($self, $cmdline));
253 # check for excessive swearing
254 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
255 Log('DXCommand', "$self->{call} logged out for excessive swearing");
260 # send a prompt only if we are in a prompt state
261 $self->prompt() if $self->{state} =~ /^prompt/o;
264 # send out the talk messages taking into account vias and connectivity
267 my ($self, $ent, $line) = @_;
269 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
270 $to = $ent unless $to;
271 my $call = $via ? $via : $to;
272 my $clref = Route::get($call);
273 my $dxchan = $clref->dxchan if $clref;
275 $dxchan->talk($self->{call}, $to, $via, $line);
277 $self->send($self->msg('disc2', $via ? $via : $to));
278 my @l = grep { $_ ne $ent } @{$self->{talklist}};
280 $self->{talklist} = \@l;
282 delete $self->{talklist};
283 $self->state('prompt');
292 for (@{$self->{talklist}}) {
293 my ($to, $via) = /(\S+)>(\S+)/;
297 return $self->msg('talkprompt', join(',', @call));
301 # send a load of stuff to a command user with page prompting
309 if ($self->{pagelth} && @_ > $self->{pagelth}) {
311 for ($i = $self->{pagelth}; $i-- > 0; ) {
313 $line =~ s/\s+$//o; # why am having to do this?
316 $self->{pagedata} = [ @_ ];
317 $self->state('page');
318 $self->send($self->msg('page', scalar @_));
330 # this is the thing that runs the command, it is done like this for the
331 # benefit of remote command execution
337 my $user = $self->{user};
338 my $call = $self->{call};
343 return () if length $cmdline == 0;
346 $cmdline =~ s|//|/|og;
348 # split the command line up into parts, the first part is the command
349 my ($cmd, $args) = split /\s+/, $cmdline, 2;
350 $args = "" unless defined $args;
356 dbg("cmd: $cmd") if isdbg('command');
358 # alias it if possible
359 my $acmd = CmdAlias::get_cmd($cmd);
361 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
362 $args = "" unless defined $args;
363 dbg("aliased cmd: $cmd $args") if isdbg('command');
366 # first expand out the entry to a command
367 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
368 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
371 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
373 my $package = find_cmd_name($path, $fcmd);
378 dbg("package: $package") if isdbg('command');
379 eval { @ans = &$package($self, $args) };
380 return (DXDebug::shortmess($@)) if $@;
383 dbg("cmd: $cmd not found") if isdbg('command');
384 if (++$self->{errors} > $maxerrors) {
385 $self->send($self->msg('e26'));
389 return ($self->msg('e1'));
396 delete $self->{errors};
398 if (++$self->{errors} > $maxerrors) {
399 $self->send($self->msg('e26'));
408 # This is called from inside the main cluster processing loop and is used
409 # for despatching commands that are doing some long processing job
414 my @dxchan = DXChannel->get_all();
417 foreach $dxchan (@dxchan) {
418 next if $dxchan->sort ne 'U';
420 # send a prompt if no activity out on this channel
421 if ($t >= $dxchan->t + $main::user_interval) {
422 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
427 while (my ($k, $v) = each %nothereslug) {
428 if ($main::systime >= $v + 300) {
429 delete $nothereslug{$k};
435 # finish up a user context
440 my $call = $self->call;
442 return if $self->{disconnecting}++;
444 delete $self->{senddbg};
446 my $uref = Route::User::get($call);
449 @rout = $main::routeroot->del_user($uref);
450 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
452 confess "trying to disconnect a non existant user $call";
455 # issue a pc17 to everybody interested
456 DXProt::route_pc17($main::me, $main::routeroot, @rout) if @rout;
458 # I was the last node visited
459 $self->user->node($main::mycall);
461 # send info to all logged in thingies
462 $self->tell_login('logoutu');
464 Log('DXCommand', "$call disconnected");
466 $self->SUPER::disconnect;
470 # short cut to output a prompt
476 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
479 # broadcast a message to all users [except those mentioned after buffer]
482 my $pkg = shift; # ignored
483 my $s = shift; # the line to be rebroadcast
485 foreach my $dxchan (DXChannel->get_all()) {
486 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
487 next if grep $dxchan == $_, @_;
488 $dxchan->send($s); # send it
492 # gimme all the users
495 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
498 # run a script for this user
502 my $silent = shift || 0;
507 # search for the command in the cache of short->long form commands
512 my ($path, $short_cmd, $suffix) = @_;
515 # commands are lower case
516 $short_cmd = lc $short_cmd;
517 dbg("command: $path $short_cmd\n") if isdbg('command');
519 # do some checking for funny characters
520 return () if $short_cmd =~ /\/$/;
522 # return immediately if we have it
523 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
524 if ($apath && $acmd) {
525 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
526 return ($apath, $acmd);
530 my @parts = split '/', $short_cmd;
537 for ($i = 0; $i < @parts; $i++) {
539 opendir(D, $curdir) or confess "can't open $curdir $!";
543 foreach $l (sort @ls) {
545 if ($i < $#parts) { # we are dealing with directories
546 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
547 dbg("got dir: $curdir/$l\n") if isdbg('command');
552 } else { # we are dealing with commands
553 @lparts = split /\./, $l;
554 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
555 if ($p eq substr($l, 0, length $p)) {
556 pop @lparts; # remove the suffix
557 $l = join '.', @lparts;
558 # chop $dirfn; # remove trailing /
559 $dirfn = "" unless $dirfn;
560 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
561 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
562 return ($path, "$dirfn$l");
570 # clear the command name cache
577 dbg("Undefining cmd $_") if isdbg('command');
584 # the persistant execution of things from the command directories
587 # This allows perl programs to call functions dynamically
589 # This has been nicked directly from the perlembed pages
592 #require Devel::Symdump;
594 sub valid_package_name {
596 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
599 return "cmd_$string";
603 # this bit of magic finds a command in the offered directory
607 my $package = valid_package_name($cmdname);
608 my $filename = "$path/$cmdname.pl";
609 my $mtime = -M $filename;
611 # return if we can't find it
613 unless (defined $mtime) {
614 $errstr = DXM::msg('e1');
618 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
619 #we have compiled this subroutine already,
620 #it has not been updated on disk, nothing left to do
621 #print STDERR "already compiled $package->handler\n";
625 my $sub = readfilestr($filename);
627 $errstr = "Syserr: can't open '$filename' $!";
631 #wrap the code into a subroutine inside our unique package
632 my $eval = qq( sub $package { $sub } );
635 my @list = split /\n/, $eval;
638 dbg($_ . "\n") if isdbg('eval');
642 # get rid of any existing sub and try to compile the new one
645 if (exists $Cache{$package}) {
646 dbg("Redefining $package") if isdbg('command');
649 dbg("Defining $package") if isdbg('command');
653 $Cache{$package} = {mtime => $mtime };
662 my ($self, $let, $buf) = @_;
663 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
664 if ($self->{enhanced}) {
665 $self->send_later($let, $buf);
674 # send a talk message here
677 my ($self, $from, $to, $via, $line) = @_;
678 $line =~ s/\\5E/\^/g;
679 $self->local_send('T', "$to de $from: $line") if $self->{talk};
680 Log('talk', $to, $from, $main::mycall, $line);
681 # send a 'not here' message if required
682 unless ($self->{here} && $from ne $to) {
683 my $key = "$to$from";
684 unless (exists $nothereslug{$key}) {
686 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
687 my $name = $self->user->name || $to;
688 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
689 $nothereslug{$key} = $main::systime;
690 $dxchan->talk($to, $from, undef, $s);
707 if (!$self->{ann_talk} && $to ne $self->{call}) {
708 my $call = AnnTalk::is_talk_candidate($_[0], $text);
712 if ($self->{annfilter}) {
713 ($filter, $hops) = $self->{annfilter}->it(@_ );
714 return unless $filter;
717 unless ($self->{ann}) {
718 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
720 return if $target eq 'SYSOP' && $self->{priv} < 5;
721 my $buf = "$to$target de $_[0]: $text";
723 $buf .= "\a\a" if $self->{beep};
724 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
735 return unless $self->{dx};
737 if ($self->{spotsfilter}) {
738 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
739 return unless $filter;
742 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
743 $buf .= "\a\a" if $self->{beep};
745 $self->local_send('X', $buf);
755 return unless $self->{wwv};
757 if ($self->{wwvfilter}) {
758 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
759 return unless $filter;
762 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
763 $buf .= "\a\a" if $self->{beep};
764 $self->local_send('V', $buf);
774 return unless $self->{wcy};
776 if ($self->{wcyfilter}) {
777 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
778 return unless $filter;
781 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
782 $buf .= "\a\a" if $self->{beep};
783 $self->local_send('Y', $buf);
786 # broadcast debug stuff to all interested parties
789 my $s = shift; # the line to be rebroadcast
791 foreach my $dxchan (DXChannel->get_all) {
792 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
793 $dxchan->send_later('L', $s);