3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
39 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %nothereslug $maxbadcount);
41 %Cache = (); # cache of dynamically loaded routine's mod times
42 %cmd_cache = (); # cache of short names
43 $errstr = (); # error string from eval
44 %aliases = (); # aliases for (parts of) commands
45 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
46 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
47 $maxbadcount = 3; # no of bad words allowed before disconnection
50 use vars qw($VERSION $BRANCH);
51 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
52 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
53 $main::build += $VERSION;
54 $main::branch += $BRANCH;
57 # obtain a new connection this is derived from dxchannel
62 my $self = DXChannel::alloc(@_);
64 # routing, this must go out here to prevent race condx
67 my $uref = Route::User::get($call) || Route::User->new($call);
70 $main::routeroot->add_user($uref);
72 # ALWAYS output the user
73 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $uref);
78 # this is how a a connection starts, you get a hello message and the motd with
79 # possibly some other messages asking you to set various things up if you are
80 # new (or nearly new and slacking) user.
84 my ($self, $line, $sort) = @_;
85 my $user = $self->{user};
86 my $call = $self->{call};
87 my $name = $user->{name};
90 my $host = $self->{conn}->{peerhost} || "unknown";
91 Log('DXCommand', "$call connected from $host");
93 $self->{name} = $name ? $name : $call;
94 $self->send($self->msg('l2',$self->{name}));
95 $self->state('prompt'); # a bit of room for further expansion, passwords etc
96 $self->{priv} = $user->priv || 0;
97 $self->{lang} = $user->lang || $main::lang || 'en';
98 $self->{pagelth} = $user->pagelth || 20;
99 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
100 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
101 $self->{consort} = $line; # save the connection type
103 # set some necessary flags on the user if they are connecting
104 $self->{beep} = $user->wantbeep;
105 $self->{ann} = $user->wantann;
106 $self->{wwv} = $user->wantwwv;
107 $self->{wcy} = $user->wantwcy;
108 $self->{talk} = $user->wanttalk;
109 $self->{wx} = $user->wantwx;
110 $self->{dx} = $user->wantdx;
111 $self->{logininfo} = $user->wantlogininfo;
112 $self->{ann_talk} = $user->wantann_talk;
114 $self->{prompt} = $user->prompt if $user->prompt;
116 # sort out new dx spot stuff
117 $user->wantdxcq(0) unless defined $user->{wantdxcq};
118 $user->wantdxitu(0) unless defined $user->{wantdxitu};
119 $user->wantusstate(0) unless defined $user->{wantusstate};
121 # sort out registration
122 if ($main::reqreg == 1) {
123 $self->{registered} = $user->registered;
124 } elsif ($main::reqreg == 2) {
125 $self->{registered} = !$user->registered;
127 $self->{registered} = 1;
131 # decide which motd to send
132 my $motd = "${main::motd}_nor" unless $self->{registered};
133 $motd = $main::motd unless $motd && -e $motd;
134 $self->send_file($motd) if -e $motd;
136 # sort out privilege reduction
137 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
140 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
141 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
142 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
143 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
145 # clean up qra locators
146 my $qra = $user->qra;
147 $qra = undef if ($qra && !DXBearing::is_qra($qra));
149 my $lat = $user->lat;
150 my $long = $user->long;
151 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
155 my $echo = $user->wantecho;
157 $self->send_now('E', "0");
158 $self->send($self->msg('echow'));
159 $self->conn->echo($echo) if $self->conn->can('echo');
162 $self->tell_login('loginu');
164 # do we need to send a forward/opernam?
165 my $lastoper = $user->lastoper || 0;
166 my $homenode = $user->homenode || "";
167 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
168 run_cmd($main::me, "forward/opernam $call");
169 $user->lastoper($main::systime + ((int rand(10)) * 86400));
172 # run a script send the output to the punter
173 my $script = new Script(lc $call) || new Script('user_default');
174 $script->run($self) if $script;
177 my $info = Route::cluster();
178 $self->send("Cluster:$info");
180 # send prompts and things
181 $self->send($self->msg('namee1')) if !$user->name;
182 $self->send($self->msg('qthe1')) if !$user->qth;
183 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
184 $self->send($self->msg('hnodee1')) if !$user->qth;
185 $self->send($self->msg('m9')) if DXMsg::for_me($call);
190 # This is the normal command prompt driver
199 # save this for them's that need it
200 my $rawline = $cmdline;
202 # remove leading and trailing spaces
203 $cmdline =~ s/^\s*(.*)\s*$/$1/;
205 if ($self->{state} eq 'page') {
206 my $i = $self->{pagelth};
207 my $ref = $self->{pagedata};
210 # abort if we get a line starting in with a
211 if ($cmdline =~ /^a/io) {
216 # send a tranche of data
217 while ($i-- > 0 && @$ref) {
218 my $line = shift @$ref;
219 $line =~ s/\s+$//o; # why am having to do this?
223 # reset state if none or else chuck out an intermediate prompt
225 $tot -= $self->{pagelth};
226 $self->send($self->msg('page', $tot));
228 $self->state('prompt');
230 } elsif ($self->{state} eq 'sysop') {
231 my $passwd = $self->{user}->passwd;
233 my @pw = grep {$_ !~ /\s/} split //, $passwd;
234 my @l = @{$self->{passwd}};
235 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
236 if ($cmdline =~ /$str/) {
237 $self->{priv} = $self->{user}->priv;
239 $self->send($self->msg('sorry'));
242 $self->send($self->msg('sorry'));
244 $self->state('prompt');
245 } elsif ($self->{state} eq 'passwd') {
246 my $passwd = $self->{user}->passwd;
247 if ($passwd && $cmdline eq $passwd) {
248 $self->send($self->msg('pw1'));
249 $self->state('passwd1');
251 $self->conn->{echo} = $self->conn->{decho};
252 delete $self->conn->{decho};
253 $self->send($self->msg('sorry'));
254 $self->state('prompt');
256 } elsif ($self->{state} eq 'passwd1') {
257 $self->{passwd} = $cmdline;
258 $self->send($self->msg('pw2'));
259 $self->state('passwd2');
260 } elsif ($self->{state} eq 'passwd2') {
261 if ($cmdline eq $self->{passwd}) {
262 $self->{user}->passwd($cmdline);
263 $self->send($self->msg('pw3'));
265 $self->send($self->msg('pw4'));
267 $self->conn->{echo} = $self->conn->{decho};
268 delete $self->conn->{decho};
269 $self->state('prompt');
270 } elsif ($self->{state} eq 'talk') {
271 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
272 for (@{$self->{talklist}}) {
273 $self->send_talks($_, $self->msg('talkend'));
275 $self->state('prompt');
276 delete $self->{talklist};
277 } elsif ($cmdline =~ m|^/+\w+|) {
279 my $sendit = $cmdline =~ s|^/+||;
280 my @in = $self->run_cmd($cmdline);
281 $self->send_ans(@in);
282 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
283 foreach my $l (@in) {
285 if (@bad = BadWords::check($l)) {
286 $self->badcount(($self->badcount||0) + @bad);
287 Log('DXCommand', "$self->{call} swore: $l");
289 for (@{$self->{talklist}}) {
290 $self->send_talks($_, $l);
295 $self->send($self->talk_prompt);
296 } elsif ($self->{talklist} && @{$self->{talklist}}) {
297 # send what has been said to whoever is in this person's talk list
299 if (@bad = BadWords::check($cmdline)) {
300 $self->badcount(($self->badcount||0) + @bad);
301 Log('DXCommand', "$self->{call} swore: $cmdline");
303 for (@{$self->{talklist}}) {
304 $self->send_talks($_, $rawline);
307 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
310 $self->state('prompt');
312 } elsif (my $func = $self->{func}) {
315 if (ref $self->{edit}) {
316 eval { @ans = $self->{edit}->$func($self, $rawline)};
318 eval { @ans = &{$self->{func}}($self, $rawline) };
321 $self->send_ans("Syserr: on stored func $self->{func}", $@);
322 delete $self->{func};
323 $self->state('prompt');
326 $self->send_ans(@ans);
328 $self->send_ans(run_cmd($self, $cmdline));
331 # check for excessive swearing
332 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
333 Log('DXCommand', "$self->{call} logged out for excessive swearing");
338 # send a prompt only if we are in a prompt state
339 $self->prompt() if $self->{state} =~ /^prompt/o;
342 # send out the talk messages taking into account vias and connectivity
345 my ($self, $ent, $line) = @_;
347 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
348 $to = $ent unless $to;
349 my $call = $via ? $via : $to;
350 my $clref = Route::get($call);
351 my $dxchan = $clref->bestdxchan if $clref;
353 $dxchan->talk($self->{call}, $to, $via, $line);
355 $self->send($self->msg('disc2', $via ? $via : $to));
356 my @l = grep { $_ ne $ent } @{$self->{talklist}};
358 $self->{talklist} = \@l;
360 delete $self->{talklist};
361 $self->state('prompt');
370 for (@{$self->{talklist}}) {
371 my ($to, $via) = /(\S+)>(\S+)/;
375 return $self->msg('talkprompt', join(',', @call));
379 # send a load of stuff to a command user with page prompting
387 if ($self->{pagelth} && @_ > $self->{pagelth}) {
389 for ($i = $self->{pagelth}; $i-- > 0; ) {
391 $line =~ s/\s+$//o; # why am having to do this?
394 $self->{pagedata} = [ @_ ];
395 $self->state('page');
396 $self->send($self->msg('page', scalar @_));
408 # this is the thing that runs the command, it is done like this for the
409 # benefit of remote command execution
415 my $user = $self->{user};
416 my $call = $self->{call};
421 return () if length $cmdline == 0;
424 $cmdline =~ s|//|/|og;
426 # split the command line up into parts, the first part is the command
427 my ($cmd, $args) = split /\s+/, $cmdline, 2;
428 $args = "" unless defined $args;
434 dbg("cmd: $cmd") if isdbg('command');
436 # alias it if possible
437 my $acmd = CmdAlias::get_cmd($cmd);
439 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
440 $args = "" unless defined $args;
441 dbg("aliased cmd: $cmd $args") if isdbg('command');
444 # first expand out the entry to a command
445 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
446 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
449 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
451 my $package = find_cmd_name($path, $fcmd);
456 dbg("package: $package") if isdbg('command');
457 eval { @ans = &$package($self, $args) };
458 return (DXDebug::shortmess($@)) if $@;
461 dbg("cmd: $cmd not found") if isdbg('command');
462 if (++$self->{errors} > $maxerrors) {
463 $self->send($self->msg('e26'));
467 return ($self->msg('e1'));
474 delete $self->{errors};
476 if (++$self->{errors} > $maxerrors) {
477 $self->send($self->msg('e26'));
482 return map {s/([^\s])\s+$/$1/; $_} @ans;
486 # This is called from inside the main cluster processing loop and is used
487 # for despatching commands that are doing some long processing job
492 my @dxchan = DXChannel->get_all();
495 foreach $dxchan (@dxchan) {
496 next if $dxchan->sort ne 'U';
498 # send a prompt if no activity out on this channel
499 if ($t >= $dxchan->t + $main::user_interval) {
500 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
505 while (my ($k, $v) = each %nothereslug) {
506 if ($main::systime >= $v + 300) {
507 delete $nothereslug{$k};
513 # finish up a user context
518 my $call = $self->call;
520 return if $self->{disconnecting}++;
522 delete $self->{senddbg};
524 my $uref = Route::User::get($call);
527 @rout = $main::routeroot->del_user($uref);
528 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
530 # issue a pc17 to everybody interested
531 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
533 confess "trying to disconnect a non existant user $call";
536 # I was the last node visited
537 $self->user->node($main::mycall);
539 # send info to all logged in thingies
540 $self->tell_login('logoutu');
542 Log('DXCommand', "$call disconnected");
544 $self->SUPER::disconnect;
548 # short cut to output a prompt
554 if ($self->{prompt}) {
555 $self->send($self->{prompt});
557 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
561 # broadcast a message to all users [except those mentioned after buffer]
564 my $pkg = shift; # ignored
565 my $s = shift; # the line to be rebroadcast
567 foreach my $dxchan (DXChannel->get_all()) {
568 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
569 next if grep $dxchan == $_, @_;
570 $dxchan->send($s); # send it
574 # gimme all the users
577 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
580 # run a script for this user
584 my $silent = shift || 0;
589 # search for the command in the cache of short->long form commands
594 my ($path, $short_cmd, $suffix) = @_;
597 # commands are lower case
598 $short_cmd = lc $short_cmd;
599 dbg("command: $path $short_cmd\n") if isdbg('command');
601 # do some checking for funny characters
602 return () if $short_cmd =~ /\/$/;
604 # return immediately if we have it
605 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
606 if ($apath && $acmd) {
607 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
608 return ($apath, $acmd);
612 my @parts = split '/', $short_cmd;
619 for ($i = 0; $i < @parts; $i++) {
621 opendir(D, $curdir) or confess "can't open $curdir $!";
625 foreach $l (sort @ls) {
627 if ($i < $#parts) { # we are dealing with directories
628 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
629 dbg("got dir: $curdir/$l\n") if isdbg('command');
634 } else { # we are dealing with commands
635 @lparts = split /\./, $l;
636 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
637 if ($p eq substr($l, 0, length $p)) {
638 pop @lparts; # remove the suffix
639 $l = join '.', @lparts;
640 # chop $dirfn; # remove trailing /
641 $dirfn = "" unless $dirfn;
642 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
643 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
644 return ($path, "$dirfn$l");
652 # clear the command name cache
658 undef *{$_} unless /cmd_cache/;
659 dbg("Undefining cmd $_") if isdbg('command');
666 # the persistant execution of things from the command directories
669 # This allows perl programs to call functions dynamically
671 # This has been nicked directly from the perlembed pages
674 #require Devel::Symdump;
676 sub valid_package_name {
678 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
681 return "cmd_$string";
685 # this bit of magic finds a command in the offered directory
689 my $package = valid_package_name($cmdname);
690 my $filename = "$path/$cmdname.pl";
691 my $mtime = -M $filename;
693 # return if we can't find it
695 unless (defined $mtime) {
696 $errstr = DXM::msg('e1');
700 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
701 #we have compiled this subroutine already,
702 #it has not been updated on disk, nothing left to do
703 #print STDERR "already compiled $package->handler\n";
707 my $sub = readfilestr($filename);
709 $errstr = "Syserr: can't open '$filename' $!";
713 #wrap the code into a subroutine inside our unique package
714 my $eval = qq( sub $package { $sub } );
717 my @list = split /\n/, $eval;
720 dbg($_ . "\n") if isdbg('eval');
724 # get rid of any existing sub and try to compile the new one
727 if (exists $Cache{$package}) {
728 dbg("Redefining $package") if isdbg('command');
731 dbg("Defining $package") if isdbg('command');
735 $Cache{$package} = {mtime => $mtime };
744 my ($self, $let, $buf) = @_;
745 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
746 if ($self->{enhanced}) {
747 $self->send_later($let, $buf);
756 # send a talk message here
759 my ($self, $from, $to, $via, $line) = @_;
760 $line =~ s/\\5E/\^/g;
761 $self->local_send('T', "$to de $from: $line") if $self->{talk};
762 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
763 # send a 'not here' message if required
764 unless ($self->{here} && $from ne $to) {
765 my $key = "$to$from";
766 unless (exists $nothereslug{$key}) {
768 if (($ref = Route::get($from)) && ($dxchan = $ref->bestdxchan)) {
769 my $name = $self->user->name || $to;
770 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
771 $nothereslug{$key} = $main::systime;
772 $dxchan->talk($to, $from, undef, $s);
789 if (!$self->{ann_talk} && $to ne $self->{call}) {
790 my $call = AnnTalk::is_talk_candidate($_[0], $text);
794 if ($self->{annfilter}) {
795 ($filter, $hops) = $self->{annfilter}->it(@_ );
796 return unless $filter;
799 unless ($self->{ann}) {
800 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
802 return if $target eq 'SYSOP' && $self->{priv} < 5;
803 my $buf = "$to$target de $_[0]: $text";
805 $buf .= "\a\a" if $self->{beep};
806 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
820 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
822 $text =~ s/^\#\d+ //;
823 my $buf = "$target de $_[0]: $text";
825 $buf .= "\a\a" if $self->{beep};
826 $self->local_send('C', $buf);
833 my $t = ztime($_[2]);
835 my $clth = $self->{consort} eq 'local' ? 29 : 30;
836 my $comment = substr $_[3], 0, $clth;
837 $comment .= ' ' x ($clth - length($comment));
838 my $ref = DXUser->get_current($_[4]);
840 $loc = $ref->qra || '' if $self->{user}->wantgrid;
841 $loc = ' ' . substr($loc, 0, 4) if $loc;
843 $loc = "" unless $loc;
845 if ($self->{user}->wantdxitu) {
846 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
847 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
848 } elsif ($self->{user}->wantdxcq) {
849 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
850 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
851 } elsif ($self->{user}->wantusstate) {
852 $loc = ' ' . $_[13] if $_[13];
853 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
856 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
867 return unless $self->{dx};
869 if ($self->{spotsfilter}) {
870 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
871 return unless $filter;
874 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
876 my $buf = $self->format_dx_spot(@_);
877 $buf .= "\a\a" if $self->{beep};
879 $self->local_send('X', $buf);
889 return unless $self->{wwv};
891 if ($self->{wwvfilter}) {
892 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
893 return unless $filter;
896 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
897 $buf .= "\a\a" if $self->{beep};
898 $self->local_send('V', $buf);
908 return unless $self->{wcy};
910 if ($self->{wcyfilter}) {
911 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
912 return unless $filter;
915 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
916 $buf .= "\a\a" if $self->{beep};
917 $self->local_send('Y', $buf);
920 # broadcast debug stuff to all interested parties
923 my $s = shift; # the line to be rebroadcast
925 foreach my $dxchan (DXChannel->get_all) {
926 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
927 $dxchan->send_later('L', $s);