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 $self->state('prompt');
294 $self->send_ans(@ans);
296 $self->send_ans(run_cmd($self, $cmdline));
299 # check for excessive swearing
300 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
301 Log('DXCommand', "$self->{call} logged out for excessive swearing");
306 # send a prompt only if we are in a prompt state
307 $self->prompt() if $self->{state} =~ /^prompt/o;
310 # send out the talk messages taking into account vias and connectivity
313 my ($self, $ent, $line) = @_;
315 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
316 $to = $ent unless $to;
317 my $call = $via ? $via : $to;
318 my $clref = Route::get($call);
319 my $dxchan = $clref->dxchan if $clref;
321 $dxchan->talk($self->{call}, $to, $via, $line);
323 $self->send($self->msg('disc2', $via ? $via : $to));
324 my @l = grep { $_ ne $ent } @{$self->{talklist}};
326 $self->{talklist} = \@l;
328 delete $self->{talklist};
329 $self->state('prompt');
338 for (@{$self->{talklist}}) {
339 my ($to, $via) = /(\S+)>(\S+)/;
343 return $self->msg('talkprompt', join(',', @call));
347 # send a load of stuff to a command user with page prompting
355 if ($self->{pagelth} && @_ > $self->{pagelth}) {
357 for ($i = $self->{pagelth}; $i-- > 0; ) {
359 $line =~ s/\s+$//o; # why am having to do this?
362 $self->{pagedata} = [ @_ ];
363 $self->state('page');
364 $self->send($self->msg('page', scalar @_));
376 # this is the thing that runs the command, it is done like this for the
377 # benefit of remote command execution
383 my $user = $self->{user};
384 my $call = $self->{call};
389 return () if length $cmdline == 0;
392 $cmdline =~ s|//|/|og;
394 # split the command line up into parts, the first part is the command
395 my ($cmd, $args) = split /\s+/, $cmdline, 2;
396 $args = "" unless defined $args;
402 dbg("cmd: $cmd") if isdbg('command');
404 # alias it if possible
405 my $acmd = CmdAlias::get_cmd($cmd);
407 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
408 $args = "" unless defined $args;
409 dbg("aliased cmd: $cmd $args") if isdbg('command');
412 # first expand out the entry to a command
413 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
414 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
417 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
419 my $package = find_cmd_name($path, $fcmd);
424 dbg("package: $package") if isdbg('command');
425 eval { @ans = &$package($self, $args) };
426 return (DXDebug::shortmess($@)) if $@;
429 dbg("cmd: $cmd not found") if isdbg('command');
430 if (++$self->{errors} > $maxerrors) {
431 $self->send($self->msg('e26'));
435 return ($self->msg('e1'));
442 delete $self->{errors};
444 if (++$self->{errors} > $maxerrors) {
445 $self->send($self->msg('e26'));
454 # This is called from inside the main cluster processing loop and is used
455 # for despatching commands that are doing some long processing job
460 my @dxchan = DXChannel->get_all();
463 foreach $dxchan (@dxchan) {
464 next if $dxchan->sort ne 'U';
466 # send a prompt if no activity out on this channel
467 if ($t >= $dxchan->t + $main::user_interval) {
468 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
473 while (my ($k, $v) = each %nothereslug) {
474 if ($main::systime >= $v + 300) {
475 delete $nothereslug{$k};
481 # finish up a user context
486 my $call = $self->call;
488 return if $self->{disconnecting}++;
490 delete $self->{senddbg};
492 my $uref = Route::User::get($call);
495 @rout = $main::routeroot->del_user($uref);
496 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
498 confess "trying to disconnect a non existant user $call";
501 # issue a pc17 to everybody interested
502 DXProt::route_pc17($main::me, $main::routeroot, @rout) if @rout;
504 # I was the last node visited
505 $self->user->node($main::mycall);
507 # send info to all logged in thingies
508 $self->tell_login('logoutu');
510 Log('DXCommand', "$call disconnected");
512 $self->SUPER::disconnect;
516 # short cut to output a prompt
522 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
525 # broadcast a message to all users [except those mentioned after buffer]
528 my $pkg = shift; # ignored
529 my $s = shift; # the line to be rebroadcast
531 foreach my $dxchan (DXChannel->get_all()) {
532 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
533 next if grep $dxchan == $_, @_;
534 $dxchan->send($s); # send it
538 # gimme all the users
541 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
544 # run a script for this user
548 my $silent = shift || 0;
553 # search for the command in the cache of short->long form commands
558 my ($path, $short_cmd, $suffix) = @_;
561 # commands are lower case
562 $short_cmd = lc $short_cmd;
563 dbg("command: $path $short_cmd\n") if isdbg('command');
565 # do some checking for funny characters
566 return () if $short_cmd =~ /\/$/;
568 # return immediately if we have it
569 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
570 if ($apath && $acmd) {
571 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
572 return ($apath, $acmd);
576 my @parts = split '/', $short_cmd;
583 for ($i = 0; $i < @parts; $i++) {
585 opendir(D, $curdir) or confess "can't open $curdir $!";
589 foreach $l (sort @ls) {
591 if ($i < $#parts) { # we are dealing with directories
592 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
593 dbg("got dir: $curdir/$l\n") if isdbg('command');
598 } else { # we are dealing with commands
599 @lparts = split /\./, $l;
600 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
601 if ($p eq substr($l, 0, length $p)) {
602 pop @lparts; # remove the suffix
603 $l = join '.', @lparts;
604 # chop $dirfn; # remove trailing /
605 $dirfn = "" unless $dirfn;
606 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
607 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
608 return ($path, "$dirfn$l");
616 # clear the command name cache
623 dbg("Undefining cmd $_") if isdbg('command');
630 # the persistant execution of things from the command directories
633 # This allows perl programs to call functions dynamically
635 # This has been nicked directly from the perlembed pages
638 #require Devel::Symdump;
640 sub valid_package_name {
642 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
645 return "cmd_$string";
649 # this bit of magic finds a command in the offered directory
653 my $package = valid_package_name($cmdname);
654 my $filename = "$path/$cmdname.pl";
655 my $mtime = -M $filename;
657 # return if we can't find it
659 unless (defined $mtime) {
660 $errstr = DXM::msg('e1');
664 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
665 #we have compiled this subroutine already,
666 #it has not been updated on disk, nothing left to do
667 #print STDERR "already compiled $package->handler\n";
671 my $sub = readfilestr($filename);
673 $errstr = "Syserr: can't open '$filename' $!";
677 #wrap the code into a subroutine inside our unique package
678 my $eval = qq( sub $package { $sub } );
681 my @list = split /\n/, $eval;
684 dbg($_ . "\n") if isdbg('eval');
688 # get rid of any existing sub and try to compile the new one
691 if (exists $Cache{$package}) {
692 dbg("Redefining $package") if isdbg('command');
695 dbg("Defining $package") if isdbg('command');
699 $Cache{$package} = {mtime => $mtime };
708 my ($self, $let, $buf) = @_;
709 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
710 if ($self->{enhanced}) {
711 $self->send_later($let, $buf);
720 # send a talk message here
723 my ($self, $from, $to, $via, $line) = @_;
724 $line =~ s/\\5E/\^/g;
725 $self->local_send('T', "$to de $from: $line") if $self->{talk};
726 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
727 # send a 'not here' message if required
728 unless ($self->{here} && $from ne $to) {
729 my $key = "$to$from";
730 unless (exists $nothereslug{$key}) {
732 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
733 my $name = $self->user->name || $to;
734 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
735 $nothereslug{$key} = $main::systime;
736 $dxchan->talk($to, $from, undef, $s);
753 if (!$self->{ann_talk} && $to ne $self->{call}) {
754 my $call = AnnTalk::is_talk_candidate($_[0], $text);
758 if ($self->{annfilter}) {
759 ($filter, $hops) = $self->{annfilter}->it(@_ );
760 return unless $filter;
763 unless ($self->{ann}) {
764 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
766 return if $target eq 'SYSOP' && $self->{priv} < 5;
767 my $buf = "$to$target de $_[0]: $text";
769 $buf .= "\a\a" if $self->{beep};
770 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
781 return unless $self->{dx};
783 if ($self->{spotsfilter}) {
784 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
785 return unless $filter;
788 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
789 $buf .= "\a\a" if $self->{beep};
791 $self->local_send('X', $buf);
801 return unless $self->{wwv};
803 if ($self->{wwvfilter}) {
804 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
805 return unless $filter;
808 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
809 $buf .= "\a\a" if $self->{beep};
810 $self->local_send('V', $buf);
820 return unless $self->{wcy};
822 if ($self->{wcyfilter}) {
823 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
824 return unless $filter;
827 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
828 $buf .= "\a\a" if $self->{beep};
829 $self->local_send('Y', $buf);
832 # broadcast debug stuff to all interested parties
835 my $s = shift; # the line to be rebroadcast
837 foreach my $dxchan (DXChannel->get_all) {
838 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
839 $dxchan->send_later('L', $s);