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 my $echo = $user->wantecho;
128 $self->send_now('E', "0");
129 $self->send($self->msg('echow'));
130 $self->conn->echo($echo) if $self->conn->can('echo');
133 $self->tell_login('loginu');
135 # do we need to send a forward/opernam?
136 my $lastoper = $user->lastoper || 0;
137 my $homenode = $user->homenode || "";
138 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
139 run_cmd($main::me, "forward/opernam $call");
140 $user->lastoper($main::systime);
143 # run a script send the output to the punter
144 my $script = new Script(lc $call) || new Script('user_default');
145 $script->run($self) if $script;
148 my $info = Route::cluster();
149 $self->send("Cluster:$info");
151 # send prompts and things
152 $self->send($self->msg('namee1')) if !$user->name;
153 $self->send($self->msg('qthe1')) if !$user->qth;
154 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
155 $self->send($self->msg('hnodee1')) if !$user->qth;
156 $self->send($self->msg('m9')) if DXMsg::for_me($call);
161 # This is the normal command prompt driver
170 # remove leading and trailing spaces
171 $cmdline =~ s/^\s*(.*)\s*$/$1/;
173 if ($self->{state} eq 'page') {
174 my $i = $self->{pagelth};
175 my $ref = $self->{pagedata};
178 # abort if we get a line starting in with a
179 if ($cmdline =~ /^a/io) {
184 # send a tranche of data
185 while ($i-- > 0 && @$ref) {
186 my $line = shift @$ref;
187 $line =~ s/\s+$//o; # why am having to do this?
191 # reset state if none or else chuck out an intermediate prompt
193 $tot -= $self->{pagelth};
194 $self->send($self->msg('page', $tot));
196 $self->state('prompt');
198 } elsif ($self->{state} eq 'sysop') {
199 my $passwd = $self->{user}->passwd;
200 my @pw = split / */, $passwd;
202 my @l = @{$self->{passwd}};
203 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
204 if ($cmdline =~ /$str/) {
205 $self->{priv} = $self->{user}->priv;
207 $self->send($self->msg('sorry'));
210 $self->send($self->msg('sorry'));
212 delete $self->{passwd};
213 $self->state('prompt');
214 } elsif ($self->{state} eq 'talk') {
215 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
216 for (@{$self->{talklist}}) {
217 $self->send_talks($_, $self->msg('talkend'));
219 $self->state('prompt');
220 delete $self->{talklist};
221 } elsif ($cmdline =~ m(^/\w+)) {
223 $self->send_ans(run_cmd($self, $cmdline));
224 $self->send($self->talk_prompt);
225 } elsif ($self->{talklist} && @{$self->{talklist}}) {
226 # send what has been said to whoever is in this person's talk list
228 if (@bad = BadWords::check($cmdline)) {
229 $self->badcount(($self->badcount||0) + @bad);
230 Log('DXCommand', "$self->{call} swore: $cmdline");
232 for (@{$self->{talklist}}) {
233 $self->send_talks($_, $cmdline);
236 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
239 $self->state('prompt');
241 } elsif (my $func = $self->{func}) {
244 if (ref $self->{edit}) {
245 eval { @ans = $self->{edit}->$func($self, $cmdline)};
247 eval { @ans = &{$self->{func}}($self, $cmdline) };
249 $self->send_ans("Syserr: on stored func $self->{func}", $@) if $@;
250 $self->send_ans(@ans);
252 $self->send_ans(run_cmd($self, $cmdline));
255 # check for excessive swearing
256 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
257 Log('DXCommand', "$self->{call} logged out for excessive swearing");
262 # send a prompt only if we are in a prompt state
263 $self->prompt() if $self->{state} =~ /^prompt/o;
266 # send out the talk messages taking into account vias and connectivity
269 my ($self, $ent, $line) = @_;
271 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
272 $to = $ent unless $to;
273 my $call = $via ? $via : $to;
274 my $clref = Route::get($call);
275 my $dxchan = $clref->dxchan if $clref;
277 $dxchan->talk($self->{call}, $to, $via, $line);
279 $self->send($self->msg('disc2', $via ? $via : $to));
280 my @l = grep { $_ ne $ent } @{$self->{talklist}};
282 $self->{talklist} = \@l;
284 delete $self->{talklist};
285 $self->state('prompt');
294 for (@{$self->{talklist}}) {
295 my ($to, $via) = /(\S+)>(\S+)/;
299 return $self->msg('talkprompt', join(',', @call));
303 # send a load of stuff to a command user with page prompting
311 if ($self->{pagelth} && @_ > $self->{pagelth}) {
313 for ($i = $self->{pagelth}; $i-- > 0; ) {
315 $line =~ s/\s+$//o; # why am having to do this?
318 $self->{pagedata} = [ @_ ];
319 $self->state('page');
320 $self->send($self->msg('page', scalar @_));
332 # this is the thing that runs the command, it is done like this for the
333 # benefit of remote command execution
339 my $user = $self->{user};
340 my $call = $self->{call};
345 return () if length $cmdline == 0;
348 $cmdline =~ s|//|/|og;
350 # split the command line up into parts, the first part is the command
351 my ($cmd, $args) = split /\s+/, $cmdline, 2;
352 $args = "" unless defined $args;
358 dbg("cmd: $cmd") if isdbg('command');
360 # alias it if possible
361 my $acmd = CmdAlias::get_cmd($cmd);
363 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
364 $args = "" unless defined $args;
365 dbg("aliased cmd: $cmd $args") if isdbg('command');
368 # first expand out the entry to a command
369 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
370 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
373 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
375 my $package = find_cmd_name($path, $fcmd);
380 dbg("package: $package") if isdbg('command');
381 eval { @ans = &$package($self, $args) };
382 return (DXDebug::shortmess($@)) if $@;
385 dbg("cmd: $cmd not found") if isdbg('command');
386 if (++$self->{errors} > $maxerrors) {
387 $self->send($self->msg('e26'));
391 return ($self->msg('e1'));
398 delete $self->{errors};
400 if (++$self->{errors} > $maxerrors) {
401 $self->send($self->msg('e26'));
410 # This is called from inside the main cluster processing loop and is used
411 # for despatching commands that are doing some long processing job
416 my @dxchan = DXChannel->get_all();
419 foreach $dxchan (@dxchan) {
420 next if $dxchan->sort ne 'U';
422 # send a prompt if no activity out on this channel
423 if ($t >= $dxchan->t + $main::user_interval) {
424 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
429 while (my ($k, $v) = each %nothereslug) {
430 if ($main::systime >= $v + 300) {
431 delete $nothereslug{$k};
437 # finish up a user context
442 my $call = $self->call;
444 return if $self->{disconnecting}++;
446 delete $self->{senddbg};
448 my $uref = Route::User::get($call);
451 @rout = $main::routeroot->del_user($uref);
452 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
454 confess "trying to disconnect a non existant user $call";
457 # issue a pc17 to everybody interested
458 DXProt::route_pc17($main::me, $main::routeroot, @rout) if @rout;
460 # I was the last node visited
461 $self->user->node($main::mycall);
463 # send info to all logged in thingies
464 $self->tell_login('logoutu');
466 Log('DXCommand', "$call disconnected");
468 $self->SUPER::disconnect;
472 # short cut to output a prompt
478 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
481 # broadcast a message to all users [except those mentioned after buffer]
484 my $pkg = shift; # ignored
485 my $s = shift; # the line to be rebroadcast
487 foreach my $dxchan (DXChannel->get_all()) {
488 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
489 next if grep $dxchan == $_, @_;
490 $dxchan->send($s); # send it
494 # gimme all the users
497 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
500 # run a script for this user
504 my $silent = shift || 0;
509 # search for the command in the cache of short->long form commands
514 my ($path, $short_cmd, $suffix) = @_;
517 # commands are lower case
518 $short_cmd = lc $short_cmd;
519 dbg("command: $path $short_cmd\n") if isdbg('command');
521 # do some checking for funny characters
522 return () if $short_cmd =~ /\/$/;
524 # return immediately if we have it
525 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
526 if ($apath && $acmd) {
527 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
528 return ($apath, $acmd);
532 my @parts = split '/', $short_cmd;
539 for ($i = 0; $i < @parts; $i++) {
541 opendir(D, $curdir) or confess "can't open $curdir $!";
545 foreach $l (sort @ls) {
547 if ($i < $#parts) { # we are dealing with directories
548 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
549 dbg("got dir: $curdir/$l\n") if isdbg('command');
554 } else { # we are dealing with commands
555 @lparts = split /\./, $l;
556 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
557 if ($p eq substr($l, 0, length $p)) {
558 pop @lparts; # remove the suffix
559 $l = join '.', @lparts;
560 # chop $dirfn; # remove trailing /
561 $dirfn = "" unless $dirfn;
562 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
563 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
564 return ($path, "$dirfn$l");
572 # clear the command name cache
579 dbg("Undefining cmd $_") if isdbg('command');
586 # the persistant execution of things from the command directories
589 # This allows perl programs to call functions dynamically
591 # This has been nicked directly from the perlembed pages
594 #require Devel::Symdump;
596 sub valid_package_name {
598 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
601 return "cmd_$string";
605 # this bit of magic finds a command in the offered directory
609 my $package = valid_package_name($cmdname);
610 my $filename = "$path/$cmdname.pl";
611 my $mtime = -M $filename;
613 # return if we can't find it
615 unless (defined $mtime) {
616 $errstr = DXM::msg('e1');
620 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
621 #we have compiled this subroutine already,
622 #it has not been updated on disk, nothing left to do
623 #print STDERR "already compiled $package->handler\n";
627 my $sub = readfilestr($filename);
629 $errstr = "Syserr: can't open '$filename' $!";
633 #wrap the code into a subroutine inside our unique package
634 my $eval = qq( sub $package { $sub } );
637 my @list = split /\n/, $eval;
640 dbg($_ . "\n") if isdbg('eval');
644 # get rid of any existing sub and try to compile the new one
647 if (exists $Cache{$package}) {
648 dbg("Redefining $package") if isdbg('command');
651 dbg("Defining $package") if isdbg('command');
655 $Cache{$package} = {mtime => $mtime };
664 my ($self, $let, $buf) = @_;
665 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
666 if ($self->{enhanced}) {
667 $self->send_later($let, $buf);
676 # send a talk message here
679 my ($self, $from, $to, $via, $line) = @_;
680 $line =~ s/\\5E/\^/g;
681 $self->local_send('T', "$to de $from: $line") if $self->{talk};
682 Log('talk', $to, $from, $main::mycall, $line);
683 # send a 'not here' message if required
684 unless ($self->{here} && $from ne $to) {
685 my $key = "$to$from";
686 unless (exists $nothereslug{$key}) {
688 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
689 my $name = $self->user->name || $to;
690 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
691 $nothereslug{$key} = $main::systime;
692 $dxchan->talk($to, $from, undef, $s);
709 if (!$self->{ann_talk} && $to ne $self->{call}) {
710 my $call = AnnTalk::is_talk_candidate($_[0], $text);
714 if ($self->{annfilter}) {
715 ($filter, $hops) = $self->{annfilter}->it(@_ );
716 return unless $filter;
719 unless ($self->{ann}) {
720 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
722 return if $target eq 'SYSOP' && $self->{priv} < 5;
723 my $buf = "$to$target de $_[0]: $text";
725 $buf .= "\a\a" if $self->{beep};
726 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
737 return unless $self->{dx};
739 if ($self->{spotsfilter}) {
740 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
741 return unless $filter;
744 my $buf = Spot::formatb($self->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
745 $buf .= "\a\a" if $self->{beep};
747 $self->local_send('X', $buf);
757 return unless $self->{wwv};
759 if ($self->{wwvfilter}) {
760 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
761 return unless $filter;
764 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
765 $buf .= "\a\a" if $self->{beep};
766 $self->local_send('V', $buf);
776 return unless $self->{wcy};
778 if ($self->{wcyfilter}) {
779 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
780 return unless $filter;
783 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
784 $buf .= "\a\a" if $self->{beep};
785 $self->local_send('Y', $buf);
788 # broadcast debug stuff to all interested parties
791 my $s = shift; # the line to be rebroadcast
793 foreach my $dxchan (DXChannel->get_all) {
794 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
795 $dxchan->send_later('L', $s);