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 @rout = $main::routeroot->add_user($call, Route::here(1));
69 # ALWAYS output the user
70 my $ref = Route::User::get($call);
71 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref) if $ref;
76 # this is how a a connection starts, you get a hello message and the motd with
77 # possibly some other messages asking you to set various things up if you are
78 # new (or nearly new and slacking) user.
82 my ($self, $line, $sort) = @_;
83 my $user = $self->{user};
84 my $call = $self->{call};
85 my $name = $user->{name};
88 my $host = $self->{conn}->{peerhost} || "unknown";
89 Log('DXCommand', "$call connected from $host");
91 $self->{name} = $name ? $name : $call;
92 $self->send($self->msg('l2',$self->{name}));
93 $self->state('prompt'); # a bit of room for further expansion, passwords etc
94 $self->{priv} = $user->priv || 0;
95 $self->{lang} = $user->lang || $main::lang || 'en';
96 $self->{pagelth} = $user->pagelth || 20;
97 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
98 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
99 $self->{consort} = $line; # save the connection type
101 # set some necessary flags on the user if they are connecting
102 $self->{beep} = $user->wantbeep;
103 $self->{ann} = $user->wantann;
104 $self->{wwv} = $user->wantwwv;
105 $self->{wcy} = $user->wantwcy;
106 $self->{talk} = $user->wanttalk;
107 $self->{wx} = $user->wantwx;
108 $self->{dx} = $user->wantdx;
109 $self->{logininfo} = $user->wantlogininfo;
110 $self->{ann_talk} = $user->wantann_talk;
112 $self->{prompt} = $user->prompt if $user->prompt;
114 # sort out new dx spot stuff
115 $user->wantdxcq(0) unless defined $user->{wantdxcq};
116 $user->wantdxitu(0) unless defined $user->{wantdxitu};
117 $user->wantusstate(0) unless defined $user->{wantusstate};
119 # sort out registration
120 if ($main::reqreg == 1) {
121 $self->{registered} = $user->registered;
122 } elsif ($main::reqreg == 2) {
123 $self->{registered} = !$user->registered;
125 $self->{registered} = 1;
129 # decide which motd to send
130 my $motd = "${main::motd}_nor" unless $self->{registered};
131 $motd = $main::motd unless $motd && -e $motd;
132 $self->send_file($motd) if -e $motd;
134 # sort out privilege reduction
135 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
138 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
139 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
140 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
141 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
143 # clean up qra locators
144 my $qra = $user->qra;
145 $qra = undef if ($qra && !DXBearing::is_qra($qra));
147 my $lat = $user->lat;
148 my $long = $user->long;
149 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
153 my $echo = $user->wantecho;
155 $self->send_now('E', "0");
156 $self->send($self->msg('echow'));
157 $self->conn->echo($echo) if $self->conn->can('echo');
160 $self->tell_login('loginu');
162 # do we need to send a forward/opernam?
163 my $lastoper = $user->lastoper || 0;
164 my $homenode = $user->homenode || "";
165 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
166 run_cmd($main::me, "forward/opernam $call");
167 $user->lastoper($main::systime + ((int rand(10)) * 86400));
170 # run a script send the output to the punter
171 my $script = new Script(lc $call) || new Script('user_default');
172 $script->run($self) if $script;
175 my $info = Route::cluster();
176 $self->send("Cluster:$info");
178 # send prompts and things
179 $self->send($self->msg('namee1')) if !$user->name;
180 $self->send($self->msg('qthe1')) if !$user->qth;
181 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
182 $self->send($self->msg('hnodee1')) if !$user->qth;
183 $self->send($self->msg('m9')) if DXMsg::for_me($call);
188 # This is the normal command prompt driver
197 # save this for them's that need it
198 my $rawline = $cmdline;
200 # remove leading and trailing spaces
201 $cmdline =~ s/^\s*(.*)\s*$/$1/;
203 if ($self->{state} eq 'page') {
204 my $i = $self->{pagelth};
205 my $ref = $self->{pagedata};
208 # abort if we get a line starting in with a
209 if ($cmdline =~ /^a/io) {
214 # send a tranche of data
215 while ($i-- > 0 && @$ref) {
216 my $line = shift @$ref;
217 $line =~ s/\s+$//o; # why am having to do this?
221 # reset state if none or else chuck out an intermediate prompt
223 $tot -= $self->{pagelth};
224 $self->send($self->msg('page', $tot));
226 $self->state('prompt');
228 } elsif ($self->{state} eq 'sysop') {
229 my $passwd = $self->{user}->passwd;
231 my @pw = grep {$_ !~ /\s/} split //, $passwd;
232 my @l = @{$self->{passwd}};
233 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
234 if ($cmdline =~ /$str/) {
235 $self->{priv} = $self->{user}->priv;
237 $self->send($self->msg('sorry'));
240 $self->send($self->msg('sorry'));
242 $self->state('prompt');
243 } elsif ($self->{state} eq 'passwd') {
244 my $passwd = $self->{user}->passwd;
245 if ($passwd && $cmdline eq $passwd) {
246 $self->send($self->msg('pw1'));
247 $self->state('passwd1');
249 $self->conn->{echo} = $self->conn->{decho};
250 delete $self->conn->{decho};
251 $self->send($self->msg('sorry'));
252 $self->state('prompt');
254 } elsif ($self->{state} eq 'passwd1') {
255 $self->{passwd} = $cmdline;
256 $self->send($self->msg('pw2'));
257 $self->state('passwd2');
258 } elsif ($self->{state} eq 'passwd2') {
259 if ($cmdline eq $self->{passwd}) {
260 $self->{user}->passwd($cmdline);
261 $self->send($self->msg('pw3'));
263 $self->send($self->msg('pw4'));
265 $self->conn->{echo} = $self->conn->{decho};
266 delete $self->conn->{decho};
267 $self->state('prompt');
268 } elsif ($self->{state} eq 'talk') {
269 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
270 for (@{$self->{talklist}}) {
271 $self->send_talks($_, $self->msg('talkend'));
273 $self->state('prompt');
274 delete $self->{talklist};
275 } elsif ($cmdline =~ m|^/+\w+|) {
277 my $sendit = $cmdline =~ s|^/+||;
278 my @in = $self->run_cmd($cmdline);
279 $self->send_ans(@in);
280 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
281 foreach my $l (@in) {
283 if (@bad = BadWords::check($l)) {
284 $self->badcount(($self->badcount||0) + @bad);
285 Log('DXCommand', "$self->{call} swore: $l");
287 for (@{$self->{talklist}}) {
288 $self->send_talks($_, $l);
293 $self->send($self->talk_prompt);
294 } elsif ($self->{talklist} && @{$self->{talklist}}) {
295 # send what has been said to whoever is in this person's talk list
297 if (@bad = BadWords::check($cmdline)) {
298 $self->badcount(($self->badcount||0) + @bad);
299 Log('DXCommand', "$self->{call} swore: $cmdline");
301 for (@{$self->{talklist}}) {
302 $self->send_talks($_, $rawline);
305 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
308 $self->state('prompt');
310 } elsif (my $func = $self->{func}) {
313 if (ref $self->{edit}) {
314 eval { @ans = $self->{edit}->$func($self, $rawline)};
316 eval { @ans = &{$self->{func}}($self, $rawline) };
319 $self->send_ans("Syserr: on stored func $self->{func}", $@);
320 delete $self->{func};
321 $self->state('prompt');
324 $self->send_ans(@ans);
326 $self->send_ans(run_cmd($self, $cmdline));
329 # check for excessive swearing
330 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
331 Log('DXCommand', "$self->{call} logged out for excessive swearing");
336 # send a prompt only if we are in a prompt state
337 $self->prompt() if $self->{state} =~ /^prompt/o;
340 # send out the talk messages taking into account vias and connectivity
343 my ($self, $ent, $line) = @_;
345 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
346 $to = $ent unless $to;
347 my $call = $via ? $via : $to;
348 my $clref = Route::get($call);
349 my $dxchan = $clref->dxchan if $clref;
351 $dxchan->talk($self->{call}, $to, $via, $line);
353 $self->send($self->msg('disc2', $via ? $via : $to));
354 my @l = grep { $_ ne $ent } @{$self->{talklist}};
356 $self->{talklist} = \@l;
358 delete $self->{talklist};
359 $self->state('prompt');
368 for (@{$self->{talklist}}) {
369 my ($to, $via) = /(\S+)>(\S+)/;
373 return $self->msg('talkprompt', join(',', @call));
377 # send a load of stuff to a command user with page prompting
385 if ($self->{pagelth} && @_ > $self->{pagelth}) {
387 for ($i = $self->{pagelth}; $i-- > 0; ) {
389 $line =~ s/\s+$//o; # why am having to do this?
392 $self->{pagedata} = [ @_ ];
393 $self->state('page');
394 $self->send($self->msg('page', scalar @_));
406 # this is the thing that runs the command, it is done like this for the
407 # benefit of remote command execution
413 my $user = $self->{user};
414 my $call = $self->{call};
419 return () if length $cmdline == 0;
422 $cmdline =~ s|//|/|og;
424 # split the command line up into parts, the first part is the command
425 my ($cmd, $args) = split /\s+/, $cmdline, 2;
426 $args = "" unless defined $args;
432 dbg("cmd: $cmd") if isdbg('command');
434 # alias it if possible
435 my $acmd = CmdAlias::get_cmd($cmd);
437 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
438 $args = "" unless defined $args;
439 dbg("aliased cmd: $cmd $args") if isdbg('command');
442 # first expand out the entry to a command
443 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
444 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
447 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
449 my $package = find_cmd_name($path, $fcmd);
454 dbg("package: $package") if isdbg('command');
455 eval { @ans = &$package($self, $args) };
456 return (DXDebug::shortmess($@)) if $@;
459 dbg("cmd: $cmd not found") if isdbg('command');
460 if (++$self->{errors} > $maxerrors) {
461 $self->send($self->msg('e26'));
465 return ($self->msg('e1'));
472 delete $self->{errors};
474 if (++$self->{errors} > $maxerrors) {
475 $self->send($self->msg('e26'));
480 return map {s/([^\s])\s+$/$1/; $_} @ans;
484 # This is called from inside the main cluster processing loop and is used
485 # for despatching commands that are doing some long processing job
490 my @dxchan = DXChannel->get_all();
493 foreach $dxchan (@dxchan) {
494 next if $dxchan->sort ne 'U';
496 # send a prompt if no activity out on this channel
497 if ($t >= $dxchan->t + $main::user_interval) {
498 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
503 while (my ($k, $v) = each %nothereslug) {
504 if ($main::systime >= $v + 300) {
505 delete $nothereslug{$k};
511 # finish up a user context
516 my $call = $self->call;
518 return if $self->{disconnecting}++;
520 delete $self->{senddbg};
522 my $uref = Route::User::get($call);
525 @rout = $main::routeroot->del_user($uref);
526 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
528 # issue a pc17 to everybody interested
529 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
531 confess "trying to disconnect a non existant user $call";
534 # I was the last node visited
535 $self->user->node($main::mycall);
537 # send info to all logged in thingies
538 $self->tell_login('logoutu');
540 Log('DXCommand', "$call disconnected");
542 $self->SUPER::disconnect;
546 # short cut to output a prompt
552 if ($self->{prompt}) {
553 $self->send($self->{prompt});
555 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
559 # broadcast a message to all users [except those mentioned after buffer]
562 my $pkg = shift; # ignored
563 my $s = shift; # the line to be rebroadcast
565 foreach my $dxchan (DXChannel->get_all()) {
566 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
567 next if grep $dxchan == $_, @_;
568 $dxchan->send($s); # send it
572 # gimme all the users
575 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
578 # run a script for this user
582 my $silent = shift || 0;
587 # search for the command in the cache of short->long form commands
592 my ($path, $short_cmd, $suffix) = @_;
595 # commands are lower case
596 $short_cmd = lc $short_cmd;
597 dbg("command: $path $short_cmd\n") if isdbg('command');
599 # do some checking for funny characters
600 return () if $short_cmd =~ /\/$/;
602 # return immediately if we have it
603 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
604 if ($apath && $acmd) {
605 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
606 return ($apath, $acmd);
610 my @parts = split '/', $short_cmd;
617 for ($i = 0; $i < @parts; $i++) {
619 opendir(D, $curdir) or confess "can't open $curdir $!";
623 foreach $l (sort @ls) {
625 if ($i < $#parts) { # we are dealing with directories
626 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
627 dbg("got dir: $curdir/$l\n") if isdbg('command');
632 } else { # we are dealing with commands
633 @lparts = split /\./, $l;
634 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
635 if ($p eq substr($l, 0, length $p)) {
636 pop @lparts; # remove the suffix
637 $l = join '.', @lparts;
638 # chop $dirfn; # remove trailing /
639 $dirfn = "" unless $dirfn;
640 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
641 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
642 return ($path, "$dirfn$l");
650 # clear the command name cache
656 undef *{$_} unless /cmd_cache/;
657 dbg("Undefining cmd $_") if isdbg('command');
664 # the persistant execution of things from the command directories
667 # This allows perl programs to call functions dynamically
669 # This has been nicked directly from the perlembed pages
672 #require Devel::Symdump;
674 sub valid_package_name {
676 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
679 return "cmd_$string";
683 # this bit of magic finds a command in the offered directory
687 my $package = valid_package_name($cmdname);
688 my $filename = "$path/$cmdname.pl";
689 my $mtime = -M $filename;
691 # return if we can't find it
693 unless (defined $mtime) {
694 $errstr = DXM::msg('e1');
698 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
699 #we have compiled this subroutine already,
700 #it has not been updated on disk, nothing left to do
701 #print STDERR "already compiled $package->handler\n";
705 my $sub = readfilestr($filename);
707 $errstr = "Syserr: can't open '$filename' $!";
711 #wrap the code into a subroutine inside our unique package
712 my $eval = qq( sub $package { $sub } );
715 my @list = split /\n/, $eval;
718 dbg($_ . "\n") if isdbg('eval');
722 # get rid of any existing sub and try to compile the new one
725 if (exists $Cache{$package}) {
726 dbg("Redefining $package") if isdbg('command');
729 dbg("Defining $package") if isdbg('command');
733 $Cache{$package} = {mtime => $mtime };
742 my ($self, $let, $buf) = @_;
743 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
744 if ($self->{enhanced}) {
745 $self->send_later($let, $buf);
754 # send a talk message here
757 my ($self, $from, $to, $via, $line) = @_;
758 $line =~ s/\\5E/\^/g;
759 $self->local_send('T', "$to de $from: $line") if $self->{talk};
760 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
761 # send a 'not here' message if required
762 unless ($self->{here} && $from ne $to) {
763 my $key = "$to$from";
764 unless (exists $nothereslug{$key}) {
766 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
767 my $name = $self->user->name || $to;
768 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
769 $nothereslug{$key} = $main::systime;
770 $dxchan->talk($to, $from, undef, $s);
787 if (!$self->{ann_talk} && $to ne $self->{call}) {
788 my $call = AnnTalk::is_talk_candidate($_[0], $text);
792 if ($self->{annfilter}) {
793 ($filter, $hops) = $self->{annfilter}->it(@_ );
794 return unless $filter;
797 unless ($self->{ann}) {
798 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
800 return if $target eq 'SYSOP' && $self->{priv} < 5;
801 my $buf = "$to$target de $_[0]: $text";
803 $buf .= "\a\a" if $self->{beep};
804 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
818 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
820 $text =~ s/^\#\d+ //;
821 my $buf = "$target de $_[0]: $text";
823 $buf .= "\a\a" if $self->{beep};
824 $self->local_send('C', $buf);
835 return unless $self->{dx};
837 if ($self->{spotsfilter}) {
838 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
839 return unless $filter;
843 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
845 my $t = ztime($_[2]);
847 my $clth = $self->{consort} eq 'local' ? 29 : 30;
848 my $comment = substr $_[3], 0, $clth;
849 $comment .= ' ' x ($clth - length($comment));
850 my $ref = DXUser->get_current($_[4]);
852 $loc = $ref->qra || '' if $self->{user}->wantgrid;
853 $loc = ' ' . substr($loc, 0, 4) if $loc;
855 $loc = "" unless $loc;
857 if ($self->{user}->wantdxitu) {
858 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
859 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
860 } elsif ($self->{user}->wantdxcq) {
861 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
862 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
863 } elsif ($self->{user}->wantusstate) {
864 $loc = ' ' . $_[13] if $_[13];
865 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
868 my $buf = sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
870 $buf .= "\a\a" if $self->{beep};
872 $self->local_send('X', $buf);
882 return unless $self->{wwv};
884 if ($self->{wwvfilter}) {
885 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
886 return unless $filter;
889 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
890 $buf .= "\a\a" if $self->{beep};
891 $self->local_send('V', $buf);
901 return unless $self->{wcy};
903 if ($self->{wcyfilter}) {
904 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
905 return unless $filter;
908 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
909 $buf .= "\a\a" if $self->{beep};
910 $self->local_send('Y', $buf);
913 # broadcast debug stuff to all interested parties
916 my $s = shift; # the line to be rebroadcast
918 foreach my $dxchan (DXChannel->get_all) {
919 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
920 $dxchan->send_later('L', $s);