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->state('prompt'); # a bit of room for further expansion, passwords etc
89 $self->{priv} = $user->priv || 0;
90 $self->{lang} = $user->lang || $main::lang || 'en';
91 $self->{pagelth} = $user->pagelth || 20;
92 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
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;
105 $self->{ann_talk} = $user->wantann_talk;
108 # sort out registration
109 if ($main::reqreg == 1) {
110 $self->{registered} = $user->registered;
111 } elsif ($main::reqreg == 2) {
112 $self->{registered} = !$user->registered;
114 $self->{registered} = 1;
118 # decide which motd to send
119 my $motd = "${main::motd}_nor" unless $self->{registered};
120 $motd = $main::motd unless $motd && -e $motd;
121 $self->send_file($motd) if -e $motd;
123 # sort out privilege reduction
124 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
127 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
128 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
129 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
130 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
132 # clean up qra locators
133 my $qra = $user->qra;
134 $qra = undef if ($qra && !DXBearing::is_qra($qra));
136 my $lat = $user->lat;
137 my $long = $user->long;
138 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
142 my $echo = $user->wantecho;
144 $self->send_now('E', "0");
145 $self->send($self->msg('echow'));
146 $self->conn->echo($echo) if $self->conn->can('echo');
149 $self->tell_login('loginu');
151 # do we need to send a forward/opernam?
152 my $lastoper = $user->lastoper || 0;
153 my $homenode = $user->homenode || "";
154 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
155 run_cmd($main::me, "forward/opernam $call");
156 $user->lastoper($main::systime);
159 # run a script send the output to the punter
160 my $script = new Script(lc $call) || new Script('user_default');
161 $script->run($self) if $script;
164 my $info = Route::cluster();
165 $self->send("Cluster:$info");
167 # send prompts and things
168 $self->send($self->msg('namee1')) if !$user->name;
169 $self->send($self->msg('qthe1')) if !$user->qth;
170 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
171 $self->send($self->msg('hnodee1')) if !$user->qth;
172 $self->send($self->msg('m9')) if DXMsg::for_me($call);
177 # This is the normal command prompt driver
186 # remove leading and trailing spaces
187 $cmdline =~ s/^\s*(.*)\s*$/$1/;
189 if ($self->{state} eq 'page') {
190 my $i = $self->{pagelth};
191 my $ref = $self->{pagedata};
194 # abort if we get a line starting in with a
195 if ($cmdline =~ /^a/io) {
200 # send a tranche of data
201 while ($i-- > 0 && @$ref) {
202 my $line = shift @$ref;
203 $line =~ s/\s+$//o; # why am having to do this?
207 # reset state if none or else chuck out an intermediate prompt
209 $tot -= $self->{pagelth};
210 $self->send($self->msg('page', $tot));
212 $self->state('prompt');
214 } elsif ($self->{state} eq 'sysop') {
215 my $passwd = $self->{user}->passwd;
217 my @pw = grep {$_ !~ /\s/} split //, $passwd;
218 my @l = @{$self->{passwd}};
219 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
220 if ($cmdline =~ /$str/) {
221 $self->{priv} = $self->{user}->priv;
223 $self->send($self->msg('sorry'));
226 $self->send($self->msg('sorry'));
228 $self->state('prompt');
229 } elsif ($self->{state} eq 'passwd') {
230 my $passwd = $self->{user}->passwd;
231 if ($passwd && $cmdline eq $passwd) {
232 $self->send($self->msg('pw1'));
233 $self->state('passwd1');
235 $self->conn->{echo} = $self->conn->{decho};
236 delete $self->conn->{decho};
237 $self->send($self->msg('sorry'));
238 $self->state('prompt');
240 } elsif ($self->{state} eq 'passwd1') {
241 $self->{passwd} = $cmdline;
242 $self->send($self->msg('pw2'));
243 $self->state('passwd2');
244 } elsif ($self->{state} eq 'passwd2') {
245 if ($cmdline eq $self->{passwd}) {
246 $self->{user}->passwd($cmdline);
247 $self->send($self->msg('pw3'));
249 $self->send($self->msg('pw4'));
251 $self->conn->{echo} = $self->conn->{decho};
252 delete $self->conn->{decho};
253 $self->state('prompt');
254 } elsif ($self->{state} eq 'talk') {
255 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
256 for (@{$self->{talklist}}) {
257 $self->send_talks($_, $self->msg('talkend'));
259 $self->state('prompt');
260 delete $self->{talklist};
261 } elsif ($cmdline =~ m(^/\w+)) {
263 $self->send_ans(run_cmd($self, $cmdline));
264 $self->send($self->talk_prompt);
265 } elsif ($self->{talklist} && @{$self->{talklist}}) {
266 # send what has been said to whoever is in this person's talk list
268 if (@bad = BadWords::check($cmdline)) {
269 $self->badcount(($self->badcount||0) + @bad);
270 Log('DXCommand', "$self->{call} swore: $cmdline");
272 for (@{$self->{talklist}}) {
273 $self->send_talks($_, $cmdline);
276 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
279 $self->state('prompt');
281 } elsif (my $func = $self->{func}) {
284 if (ref $self->{edit}) {
285 eval { @ans = $self->{edit}->$func($self, $cmdline)};
287 eval { @ans = &{$self->{func}}($self, $cmdline) };
290 $self->send_ans("Syserr: on stored func $self->{func}", $@);
291 delete $self->{func};
292 $self->state('prompt');
295 $self->send_ans(@ans);
297 $self->send_ans(run_cmd($self, $cmdline));
300 # check for excessive swearing
301 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
302 Log('DXCommand', "$self->{call} logged out for excessive swearing");
307 # send a prompt only if we are in a prompt state
308 $self->prompt() if $self->{state} =~ /^prompt/o;
311 # send out the talk messages taking into account vias and connectivity
314 my ($self, $ent, $line) = @_;
316 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
317 $to = $ent unless $to;
318 my $call = $via ? $via : $to;
319 my $clref = Route::get($call);
320 my $dxchan = $clref->dxchan if $clref;
322 $dxchan->talk($self->{call}, $to, $via, $line);
324 $self->send($self->msg('disc2', $via ? $via : $to));
325 my @l = grep { $_ ne $ent } @{$self->{talklist}};
327 $self->{talklist} = \@l;
329 delete $self->{talklist};
330 $self->state('prompt');
339 for (@{$self->{talklist}}) {
340 my ($to, $via) = /(\S+)>(\S+)/;
344 return $self->msg('talkprompt', join(',', @call));
348 # send a load of stuff to a command user with page prompting
356 if ($self->{pagelth} && @_ > $self->{pagelth}) {
358 for ($i = $self->{pagelth}; $i-- > 0; ) {
360 $line =~ s/\s+$//o; # why am having to do this?
363 $self->{pagedata} = [ @_ ];
364 $self->state('page');
365 $self->send($self->msg('page', scalar @_));
377 # this is the thing that runs the command, it is done like this for the
378 # benefit of remote command execution
384 my $user = $self->{user};
385 my $call = $self->{call};
390 return () if length $cmdline == 0;
393 $cmdline =~ s|//|/|og;
395 # split the command line up into parts, the first part is the command
396 my ($cmd, $args) = split /\s+/, $cmdline, 2;
397 $args = "" unless defined $args;
403 dbg("cmd: $cmd") if isdbg('command');
405 # alias it if possible
406 my $acmd = CmdAlias::get_cmd($cmd);
408 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
409 $args = "" unless defined $args;
410 dbg("aliased cmd: $cmd $args") if isdbg('command');
413 # first expand out the entry to a command
414 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
415 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
418 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
420 my $package = find_cmd_name($path, $fcmd);
425 dbg("package: $package") if isdbg('command');
426 eval { @ans = &$package($self, $args) };
427 return (DXDebug::shortmess($@)) if $@;
430 dbg("cmd: $cmd not found") if isdbg('command');
431 if (++$self->{errors} > $maxerrors) {
432 $self->send($self->msg('e26'));
436 return ($self->msg('e1'));
443 delete $self->{errors};
445 if (++$self->{errors} > $maxerrors) {
446 $self->send($self->msg('e26'));
455 # This is called from inside the main cluster processing loop and is used
456 # for despatching commands that are doing some long processing job
461 my @dxchan = DXChannel->get_all();
464 foreach $dxchan (@dxchan) {
465 next if $dxchan->sort ne 'U';
467 # send a prompt if no activity out on this channel
468 if ($t >= $dxchan->t + $main::user_interval) {
469 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
474 while (my ($k, $v) = each %nothereslug) {
475 if ($main::systime >= $v + 300) {
476 delete $nothereslug{$k};
482 # finish up a user context
487 my $call = $self->call;
489 return if $self->{disconnecting}++;
491 delete $self->{senddbg};
493 my $uref = Route::User::get($call);
496 @rout = $main::routeroot->del_user($uref);
497 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
499 confess "trying to disconnect a non existant user $call";
502 # issue a pc17 to everybody interested
503 DXProt::route_pc17($main::me, $main::routeroot, @rout) if @rout;
505 # I was the last node visited
506 $self->user->node($main::mycall);
508 # send info to all logged in thingies
509 $self->tell_login('logoutu');
511 Log('DXCommand', "$call disconnected");
513 $self->SUPER::disconnect;
517 # short cut to output a prompt
523 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
526 # broadcast a message to all users [except those mentioned after buffer]
529 my $pkg = shift; # ignored
530 my $s = shift; # the line to be rebroadcast
532 foreach my $dxchan (DXChannel->get_all()) {
533 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
534 next if grep $dxchan == $_, @_;
535 $dxchan->send($s); # send it
539 # gimme all the users
542 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
545 # run a script for this user
549 my $silent = shift || 0;
554 # search for the command in the cache of short->long form commands
559 my ($path, $short_cmd, $suffix) = @_;
562 # commands are lower case
563 $short_cmd = lc $short_cmd;
564 dbg("command: $path $short_cmd\n") if isdbg('command');
566 # do some checking for funny characters
567 return () if $short_cmd =~ /\/$/;
569 # return immediately if we have it
570 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
571 if ($apath && $acmd) {
572 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
573 return ($apath, $acmd);
577 my @parts = split '/', $short_cmd;
584 for ($i = 0; $i < @parts; $i++) {
586 opendir(D, $curdir) or confess "can't open $curdir $!";
590 foreach $l (sort @ls) {
592 if ($i < $#parts) { # we are dealing with directories
593 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
594 dbg("got dir: $curdir/$l\n") if isdbg('command');
599 } else { # we are dealing with commands
600 @lparts = split /\./, $l;
601 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
602 if ($p eq substr($l, 0, length $p)) {
603 pop @lparts; # remove the suffix
604 $l = join '.', @lparts;
605 # chop $dirfn; # remove trailing /
606 $dirfn = "" unless $dirfn;
607 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
608 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
609 return ($path, "$dirfn$l");
617 # clear the command name cache
624 dbg("Undefining cmd $_") if isdbg('command');
631 # the persistant execution of things from the command directories
634 # This allows perl programs to call functions dynamically
636 # This has been nicked directly from the perlembed pages
639 #require Devel::Symdump;
641 sub valid_package_name {
643 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
646 return "cmd_$string";
650 # this bit of magic finds a command in the offered directory
654 my $package = valid_package_name($cmdname);
655 my $filename = "$path/$cmdname.pl";
656 my $mtime = -M $filename;
658 # return if we can't find it
660 unless (defined $mtime) {
661 $errstr = DXM::msg('e1');
665 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
666 #we have compiled this subroutine already,
667 #it has not been updated on disk, nothing left to do
668 #print STDERR "already compiled $package->handler\n";
672 my $sub = readfilestr($filename);
674 $errstr = "Syserr: can't open '$filename' $!";
678 #wrap the code into a subroutine inside our unique package
679 my $eval = qq( sub $package { $sub } );
682 my @list = split /\n/, $eval;
685 dbg($_ . "\n") if isdbg('eval');
689 # get rid of any existing sub and try to compile the new one
692 if (exists $Cache{$package}) {
693 dbg("Redefining $package") if isdbg('command');
696 dbg("Defining $package") if isdbg('command');
700 $Cache{$package} = {mtime => $mtime };
709 my ($self, $let, $buf) = @_;
710 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
711 if ($self->{enhanced}) {
712 $self->send_later($let, $buf);
721 # send a talk message here
724 my ($self, $from, $to, $via, $line) = @_;
725 $line =~ s/\\5E/\^/g;
726 $self->local_send('T', "$to de $from: $line") if $self->{talk};
727 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
728 # send a 'not here' message if required
729 unless ($self->{here} && $from ne $to) {
730 my $key = "$to$from";
731 unless (exists $nothereslug{$key}) {
733 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
734 my $name = $self->user->name || $to;
735 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
736 $nothereslug{$key} = $main::systime;
737 $dxchan->talk($to, $from, undef, $s);
754 if (!$self->{ann_talk} && $to ne $self->{call}) {
755 my $call = AnnTalk::is_talk_candidate($_[0], $text);
759 if ($self->{annfilter}) {
760 ($filter, $hops) = $self->{annfilter}->it(@_ );
761 return unless $filter;
764 unless ($self->{ann}) {
765 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
767 return if $target eq 'SYSOP' && $self->{priv} < 5;
768 my $buf = "$to$target de $_[0]: $text";
770 $buf .= "\a\a" if $self->{beep};
771 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
782 return unless $self->{dx};
784 if ($self->{spotsfilter}) {
785 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
786 return unless $filter;
790 my $t = ztime($_[2]);
791 my $ref = DXUser->get_current($_[4]);
792 my $loc = $ref->qra if $ref && $ref->qra && $self->{user}->wantgrid;
793 $loc = ' ' . substr($loc, 0, 4) if $loc;
794 $loc = "" unless $loc;
795 my $buf = sprintf "DX de %-7.7s%11.1f %-12.12s %-*s $t$loc", "$_[4]:", $_[0], $_[1], $self->{consort} eq 'local' ? 29 : 30, $_[3];
796 $buf .= "\a\a" if $self->{beep};
798 $self->local_send('X', $buf);
808 return unless $self->{wwv};
810 if ($self->{wwvfilter}) {
811 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
812 return unless $filter;
815 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
816 $buf .= "\a\a" if $self->{beep};
817 $self->local_send('V', $buf);
827 return unless $self->{wcy};
829 if ($self->{wcyfilter}) {
830 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
831 return unless $filter;
834 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
835 $buf .= "\a\a" if $self->{beep};
836 $self->local_send('Y', $buf);
839 # broadcast debug stuff to all interested parties
842 my $s = shift; # the line to be rebroadcast
844 foreach my $dxchan (DXChannel->get_all) {
845 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
846 $dxchan->send_later('L', $s);