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+)/;
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
110 $self->{registered} = $user->registered;
112 $self->{registered} = 1;
116 # decide which motd to send
117 my $motd = "${main::motd}_nor" unless $self->{registered};
118 $motd = $main::motd unless $motd && -e $motd;
119 $self->send_file($motd) if -e $motd;
121 # sort out privilege reduction
122 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
125 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
126 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
127 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
128 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
130 # clean up qra locators
131 my $qra = $user->qra;
132 $qra = undef if ($qra && !DXBearing::is_qra($qra));
134 my $lat = $user->lat;
135 my $long = $user->long;
136 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
140 my $echo = $user->wantecho;
142 $self->send_now('E', "0");
143 $self->send($self->msg('echow'));
144 $self->conn->echo($echo) if $self->conn->can('echo');
147 $self->tell_login('loginu');
149 # do we need to send a forward/opernam?
150 my $lastoper = $user->lastoper || 0;
151 my $homenode = $user->homenode || "";
152 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
153 run_cmd($main::me, "forward/opernam $call");
154 $user->lastoper($main::systime);
157 # run a script send the output to the punter
158 my $script = new Script(lc $call) || new Script('user_default');
159 $script->run($self) if $script;
162 my $info = Route::cluster();
163 $self->send("Cluster:$info");
165 # send prompts and things
166 $self->send($self->msg('namee1')) if !$user->name;
167 $self->send($self->msg('qthe1')) if !$user->qth;
168 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
169 $self->send($self->msg('hnodee1')) if !$user->qth;
170 $self->send($self->msg('m9')) if DXMsg::for_me($call);
175 # This is the normal command prompt driver
184 # remove leading and trailing spaces
185 $cmdline =~ s/^\s*(.*)\s*$/$1/;
187 if ($self->{state} eq 'page') {
188 my $i = $self->{pagelth};
189 my $ref = $self->{pagedata};
192 # abort if we get a line starting in with a
193 if ($cmdline =~ /^a/io) {
198 # send a tranche of data
199 while ($i-- > 0 && @$ref) {
200 my $line = shift @$ref;
201 $line =~ s/\s+$//o; # why am having to do this?
205 # reset state if none or else chuck out an intermediate prompt
207 $tot -= $self->{pagelth};
208 $self->send($self->msg('page', $tot));
210 $self->state('prompt');
212 } elsif ($self->{state} eq 'sysop') {
213 my $passwd = $self->{user}->passwd;
215 my @pw = grep {$_ !~ /\s/} split //, $passwd;
216 my @l = @{$self->{passwd}};
217 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
218 if ($cmdline =~ /$str/) {
219 $self->{priv} = $self->{user}->priv;
221 $self->send($self->msg('sorry'));
224 $self->send($self->msg('sorry'));
226 $self->state('prompt');
227 } elsif ($self->{state} eq 'passwd') {
228 my $passwd = $self->{user}->passwd;
229 if ($passwd && $cmdline eq $passwd) {
230 $self->send($self->msg('pw1'));
231 $self->state('passwd1');
233 $self->conn->{echo} = $self->conn->{decho};
234 delete $self->conn->{decho};
235 $self->send($self->msg('sorry'));
236 $self->state('prompt');
238 } elsif ($self->{state} eq 'passwd1') {
239 $self->{passwd} = $cmdline;
240 $self->send($self->msg('pw2'));
241 $self->state('passwd2');
242 } elsif ($self->{state} eq 'passwd2') {
243 if ($cmdline eq $self->{passwd}) {
244 $self->{user}->passwd($cmdline);
245 $self->send($self->msg('pw3'));
247 $self->send($self->msg('pw4'));
249 $self->conn->{echo} = $self->conn->{decho};
250 delete $self->conn->{decho};
251 $self->state('prompt');
252 } elsif ($self->{state} eq 'talk') {
253 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
254 for (@{$self->{talklist}}) {
255 $self->send_talks($_, $self->msg('talkend'));
257 $self->state('prompt');
258 delete $self->{talklist};
259 } elsif ($cmdline =~ m(^/\w+)) {
261 $self->send_ans(run_cmd($self, $cmdline));
262 $self->send($self->talk_prompt);
263 } elsif ($self->{talklist} && @{$self->{talklist}}) {
264 # send what has been said to whoever is in this person's talk list
266 if (@bad = BadWords::check($cmdline)) {
267 $self->badcount(($self->badcount||0) + @bad);
268 Log('DXCommand', "$self->{call} swore: $cmdline");
270 for (@{$self->{talklist}}) {
271 $self->send_talks($_, $cmdline);
274 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
277 $self->state('prompt');
279 } elsif (my $func = $self->{func}) {
282 if (ref $self->{edit}) {
283 eval { @ans = $self->{edit}->$func($self, $cmdline)};
285 eval { @ans = &{$self->{func}}($self, $cmdline) };
287 $self->send_ans("Syserr: on stored func $self->{func}", $@) if $@;
288 $self->send_ans(@ans);
290 $self->send_ans(run_cmd($self, $cmdline));
293 # check for excessive swearing
294 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
295 Log('DXCommand', "$self->{call} logged out for excessive swearing");
300 # send a prompt only if we are in a prompt state
301 $self->prompt() if $self->{state} =~ /^prompt/o;
304 # send out the talk messages taking into account vias and connectivity
307 my ($self, $ent, $line) = @_;
309 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
310 $to = $ent unless $to;
311 my $call = $via ? $via : $to;
312 my $clref = Route::get($call);
313 my $dxchan = $clref->dxchan if $clref;
315 $dxchan->talk($self->{call}, $to, $via, $line);
317 $self->send($self->msg('disc2', $via ? $via : $to));
318 my @l = grep { $_ ne $ent } @{$self->{talklist}};
320 $self->{talklist} = \@l;
322 delete $self->{talklist};
323 $self->state('prompt');
332 for (@{$self->{talklist}}) {
333 my ($to, $via) = /(\S+)>(\S+)/;
337 return $self->msg('talkprompt', join(',', @call));
341 # send a load of stuff to a command user with page prompting
349 if ($self->{pagelth} && @_ > $self->{pagelth}) {
351 for ($i = $self->{pagelth}; $i-- > 0; ) {
353 $line =~ s/\s+$//o; # why am having to do this?
356 $self->{pagedata} = [ @_ ];
357 $self->state('page');
358 $self->send($self->msg('page', scalar @_));
370 # this is the thing that runs the command, it is done like this for the
371 # benefit of remote command execution
377 my $user = $self->{user};
378 my $call = $self->{call};
383 return () if length $cmdline == 0;
386 $cmdline =~ s|//|/|og;
388 # split the command line up into parts, the first part is the command
389 my ($cmd, $args) = split /\s+/, $cmdline, 2;
390 $args = "" unless defined $args;
396 dbg("cmd: $cmd") if isdbg('command');
398 # alias it if possible
399 my $acmd = CmdAlias::get_cmd($cmd);
401 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
402 $args = "" unless defined $args;
403 dbg("aliased cmd: $cmd $args") if isdbg('command');
406 # first expand out the entry to a command
407 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
408 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
411 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
413 my $package = find_cmd_name($path, $fcmd);
418 dbg("package: $package") if isdbg('command');
419 eval { @ans = &$package($self, $args) };
420 return (DXDebug::shortmess($@)) if $@;
423 dbg("cmd: $cmd not found") if isdbg('command');
424 if (++$self->{errors} > $maxerrors) {
425 $self->send($self->msg('e26'));
429 return ($self->msg('e1'));
436 delete $self->{errors};
438 if (++$self->{errors} > $maxerrors) {
439 $self->send($self->msg('e26'));
448 # This is called from inside the main cluster processing loop and is used
449 # for despatching commands that are doing some long processing job
454 my @dxchan = DXChannel->get_all();
457 foreach $dxchan (@dxchan) {
458 next if $dxchan->sort ne 'U';
460 # send a prompt if no activity out on this channel
461 if ($t >= $dxchan->t + $main::user_interval) {
462 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
467 while (my ($k, $v) = each %nothereslug) {
468 if ($main::systime >= $v + 300) {
469 delete $nothereslug{$k};
475 # finish up a user context
480 my $call = $self->call;
482 return if $self->{disconnecting}++;
484 delete $self->{senddbg};
486 my $uref = Route::User::get($call);
489 @rout = $main::routeroot->del_user($uref);
490 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
492 confess "trying to disconnect a non existant user $call";
495 # issue a pc17 to everybody interested
496 DXProt::route_pc17($main::me, $main::routeroot, @rout) if @rout;
498 # I was the last node visited
499 $self->user->node($main::mycall);
501 # send info to all logged in thingies
502 $self->tell_login('logoutu');
504 Log('DXCommand', "$call disconnected");
506 $self->SUPER::disconnect;
510 # short cut to output a prompt
516 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
519 # broadcast a message to all users [except those mentioned after buffer]
522 my $pkg = shift; # ignored
523 my $s = shift; # the line to be rebroadcast
525 foreach my $dxchan (DXChannel->get_all()) {
526 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
527 next if grep $dxchan == $_, @_;
528 $dxchan->send($s); # send it
532 # gimme all the users
535 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
538 # run a script for this user
542 my $silent = shift || 0;
547 # search for the command in the cache of short->long form commands
552 my ($path, $short_cmd, $suffix) = @_;
555 # commands are lower case
556 $short_cmd = lc $short_cmd;
557 dbg("command: $path $short_cmd\n") if isdbg('command');
559 # do some checking for funny characters
560 return () if $short_cmd =~ /\/$/;
562 # return immediately if we have it
563 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
564 if ($apath && $acmd) {
565 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
566 return ($apath, $acmd);
570 my @parts = split '/', $short_cmd;
577 for ($i = 0; $i < @parts; $i++) {
579 opendir(D, $curdir) or confess "can't open $curdir $!";
583 foreach $l (sort @ls) {
585 if ($i < $#parts) { # we are dealing with directories
586 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
587 dbg("got dir: $curdir/$l\n") if isdbg('command');
592 } else { # we are dealing with commands
593 @lparts = split /\./, $l;
594 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
595 if ($p eq substr($l, 0, length $p)) {
596 pop @lparts; # remove the suffix
597 $l = join '.', @lparts;
598 # chop $dirfn; # remove trailing /
599 $dirfn = "" unless $dirfn;
600 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
601 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
602 return ($path, "$dirfn$l");
610 # clear the command name cache
617 dbg("Undefining cmd $_") if isdbg('command');
624 # the persistant execution of things from the command directories
627 # This allows perl programs to call functions dynamically
629 # This has been nicked directly from the perlembed pages
632 #require Devel::Symdump;
634 sub valid_package_name {
636 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
639 return "cmd_$string";
643 # this bit of magic finds a command in the offered directory
647 my $package = valid_package_name($cmdname);
648 my $filename = "$path/$cmdname.pl";
649 my $mtime = -M $filename;
651 # return if we can't find it
653 unless (defined $mtime) {
654 $errstr = DXM::msg('e1');
658 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
659 #we have compiled this subroutine already,
660 #it has not been updated on disk, nothing left to do
661 #print STDERR "already compiled $package->handler\n";
665 my $sub = readfilestr($filename);
667 $errstr = "Syserr: can't open '$filename' $!";
671 #wrap the code into a subroutine inside our unique package
672 my $eval = qq( sub $package { $sub } );
675 my @list = split /\n/, $eval;
678 dbg($_ . "\n") if isdbg('eval');
682 # get rid of any existing sub and try to compile the new one
685 if (exists $Cache{$package}) {
686 dbg("Redefining $package") if isdbg('command');
689 dbg("Defining $package") if isdbg('command');
693 $Cache{$package} = {mtime => $mtime };
702 my ($self, $let, $buf) = @_;
703 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
704 if ($self->{enhanced}) {
705 $self->send_later($let, $buf);
714 # send a talk message here
717 my ($self, $from, $to, $via, $line) = @_;
718 $line =~ s/\\5E/\^/g;
719 $self->local_send('T', "$to de $from: $line") if $self->{talk};
720 Log('talk', $to, $from, $main::mycall, $line);
721 # send a 'not here' message if required
722 unless ($self->{here} && $from ne $to) {
723 my $key = "$to$from";
724 unless (exists $nothereslug{$key}) {
726 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
727 my $name = $self->user->name || $to;
728 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
729 $nothereslug{$key} = $main::systime;
730 $dxchan->talk($to, $from, undef, $s);
747 if (!$self->{ann_talk} && $to ne $self->{call}) {
748 my $call = AnnTalk::is_talk_candidate($_[0], $text);
752 if ($self->{annfilter}) {
753 ($filter, $hops) = $self->{annfilter}->it(@_ );
754 return unless $filter;
757 unless ($self->{ann}) {
758 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
760 return if $target eq 'SYSOP' && $self->{priv} < 5;
761 my $buf = "$to$target de $_[0]: $text";
763 $buf .= "\a\a" if $self->{beep};
764 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
775 return unless $self->{dx};
777 if ($self->{spotsfilter}) {
778 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
779 return unless $filter;
782 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
783 $buf .= "\a\a" if $self->{beep};
785 $self->local_send('X', $buf);
795 return unless $self->{wwv};
797 if ($self->{wwvfilter}) {
798 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
799 return unless $filter;
802 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
803 $buf .= "\a\a" if $self->{beep};
804 $self->local_send('V', $buf);
814 return unless $self->{wcy};
816 if ($self->{wcyfilter}) {
817 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
818 return unless $filter;
821 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
822 $buf .= "\a\a" if $self->{beep};
823 $self->local_send('Y', $buf);
826 # broadcast debug stuff to all interested parties
829 my $s = shift; # the line to be rebroadcast
831 foreach my $dxchan (DXChannel->get_all) {
832 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
833 $dxchan->send_later('L', $s);