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));
67 # ALWAYS output the user
68 my $ref = Route::User::get($call);
69 DXProt::route_pc16($main::me, $main::routeroot, $ref) if $ref;
74 # this is how a a connection starts, you get a hello message and the motd with
75 # possibly some other messages asking you to set various things up if you are
76 # new (or nearly new and slacking) user.
80 my ($self, $line, $sort) = @_;
81 my $user = $self->{user};
82 my $call = $self->{call};
83 my $name = $user->{name};
86 my $host = $self->{conn}->{peerhost} || "unknown";
87 Log('DXCommand', "$call connected from $host");
89 $self->{name} = $name ? $name : $call;
90 $self->send($self->msg('l2',$self->{name}));
91 $self->state('prompt'); # a bit of room for further expansion, passwords etc
92 $self->{priv} = $user->priv || 0;
93 $self->{lang} = $user->lang || $main::lang || 'en';
94 $self->{pagelth} = $user->pagelth || 20;
95 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
96 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
97 $self->{consort} = $line; # save the connection type
99 # set some necessary flags on the user if they are connecting
100 $self->{beep} = $user->wantbeep;
101 $self->{ann} = $user->wantann;
102 $self->{wwv} = $user->wantwwv;
103 $self->{wcy} = $user->wantwcy;
104 $self->{talk} = $user->wanttalk;
105 $self->{wx} = $user->wantwx;
106 $self->{dx} = $user->wantdx;
107 $self->{logininfo} = $user->wantlogininfo;
108 $self->{ann_talk} = $user->wantann_talk;
111 # sort out registration
112 if ($main::reqreg == 1) {
113 $self->{registered} = $user->registered;
114 } elsif ($main::reqreg == 2) {
115 $self->{registered} = !$user->registered;
117 $self->{registered} = 1;
121 # decide which motd to send
122 my $motd = "${main::motd}_nor" unless $self->{registered};
123 $motd = $main::motd unless $motd && -e $motd;
124 $self->send_file($motd) if -e $motd;
126 # sort out privilege reduction
127 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
130 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
131 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
132 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
133 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
135 # clean up qra locators
136 my $qra = $user->qra;
137 $qra = undef if ($qra && !DXBearing::is_qra($qra));
139 my $lat = $user->lat;
140 my $long = $user->long;
141 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
145 my $echo = $user->wantecho;
147 $self->send_now('E', "0");
148 $self->send($self->msg('echow'));
149 $self->conn->echo($echo) if $self->conn->can('echo');
152 $self->tell_login('loginu');
154 # do we need to send a forward/opernam?
155 my $lastoper = $user->lastoper || 0;
156 my $homenode = $user->homenode || "";
157 if ($homenode eq $main::mycall && $lastoper + $DXUser::lastoperinterval < $main::systime) {
158 run_cmd($main::me, "forward/opernam $call");
159 $user->lastoper($main::systime);
162 # run a script send the output to the punter
163 my $script = new Script(lc $call) || new Script('user_default');
164 $script->run($self) if $script;
167 my $info = Route::cluster();
168 $self->send("Cluster:$info");
170 # send prompts and things
171 $self->send($self->msg('namee1')) if !$user->name;
172 $self->send($self->msg('qthe1')) if !$user->qth;
173 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
174 $self->send($self->msg('hnodee1')) if !$user->qth;
175 $self->send($self->msg('m9')) if DXMsg::for_me($call);
180 # This is the normal command prompt driver
189 # remove leading and trailing spaces
190 $cmdline =~ s/^\s*(.*)\s*$/$1/;
192 if ($self->{state} eq 'page') {
193 my $i = $self->{pagelth};
194 my $ref = $self->{pagedata};
197 # abort if we get a line starting in with a
198 if ($cmdline =~ /^a/io) {
203 # send a tranche of data
204 while ($i-- > 0 && @$ref) {
205 my $line = shift @$ref;
206 $line =~ s/\s+$//o; # why am having to do this?
210 # reset state if none or else chuck out an intermediate prompt
212 $tot -= $self->{pagelth};
213 $self->send($self->msg('page', $tot));
215 $self->state('prompt');
217 } elsif ($self->{state} eq 'sysop') {
218 my $passwd = $self->{user}->passwd;
220 my @pw = grep {$_ !~ /\s/} split //, $passwd;
221 my @l = @{$self->{passwd}};
222 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
223 if ($cmdline =~ /$str/) {
224 $self->{priv} = $self->{user}->priv;
226 $self->send($self->msg('sorry'));
229 $self->send($self->msg('sorry'));
231 $self->state('prompt');
232 } elsif ($self->{state} eq 'passwd') {
233 my $passwd = $self->{user}->passwd;
234 if ($passwd && $cmdline eq $passwd) {
235 $self->send($self->msg('pw1'));
236 $self->state('passwd1');
238 $self->conn->{echo} = $self->conn->{decho};
239 delete $self->conn->{decho};
240 $self->send($self->msg('sorry'));
241 $self->state('prompt');
243 } elsif ($self->{state} eq 'passwd1') {
244 $self->{passwd} = $cmdline;
245 $self->send($self->msg('pw2'));
246 $self->state('passwd2');
247 } elsif ($self->{state} eq 'passwd2') {
248 if ($cmdline eq $self->{passwd}) {
249 $self->{user}->passwd($cmdline);
250 $self->send($self->msg('pw3'));
252 $self->send($self->msg('pw4'));
254 $self->conn->{echo} = $self->conn->{decho};
255 delete $self->conn->{decho};
256 $self->state('prompt');
257 } elsif ($self->{state} eq 'talk') {
258 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
259 for (@{$self->{talklist}}) {
260 $self->send_talks($_, $self->msg('talkend'));
262 $self->state('prompt');
263 delete $self->{talklist};
264 } elsif ($cmdline =~ m(^/\w+)) {
266 $self->send_ans(run_cmd($self, $cmdline));
267 $self->send($self->talk_prompt);
268 } elsif ($self->{talklist} && @{$self->{talklist}}) {
269 # send what has been said to whoever is in this person's talk list
271 if (@bad = BadWords::check($cmdline)) {
272 $self->badcount(($self->badcount||0) + @bad);
273 Log('DXCommand', "$self->{call} swore: $cmdline");
275 for (@{$self->{talklist}}) {
276 $self->send_talks($_, $cmdline);
279 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
282 $self->state('prompt');
284 } elsif (my $func = $self->{func}) {
287 if (ref $self->{edit}) {
288 eval { @ans = $self->{edit}->$func($self, $cmdline)};
290 eval { @ans = &{$self->{func}}($self, $cmdline) };
293 $self->send_ans("Syserr: on stored func $self->{func}", $@);
294 delete $self->{func};
295 $self->state('prompt');
298 $self->send_ans(@ans);
300 $self->send_ans(run_cmd($self, $cmdline));
303 # check for excessive swearing
304 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
305 Log('DXCommand', "$self->{call} logged out for excessive swearing");
310 # send a prompt only if we are in a prompt state
311 $self->prompt() if $self->{state} =~ /^prompt/o;
314 # send out the talk messages taking into account vias and connectivity
317 my ($self, $ent, $line) = @_;
319 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
320 $to = $ent unless $to;
321 my $call = $via ? $via : $to;
322 my $clref = Route::get($call);
323 my $dxchan = $clref->dxchan if $clref;
325 $dxchan->talk($self->{call}, $to, $via, $line);
327 $self->send($self->msg('disc2', $via ? $via : $to));
328 my @l = grep { $_ ne $ent } @{$self->{talklist}};
330 $self->{talklist} = \@l;
332 delete $self->{talklist};
333 $self->state('prompt');
342 for (@{$self->{talklist}}) {
343 my ($to, $via) = /(\S+)>(\S+)/;
347 return $self->msg('talkprompt', join(',', @call));
351 # send a load of stuff to a command user with page prompting
359 if ($self->{pagelth} && @_ > $self->{pagelth}) {
361 for ($i = $self->{pagelth}; $i-- > 0; ) {
363 $line =~ s/\s+$//o; # why am having to do this?
366 $self->{pagedata} = [ @_ ];
367 $self->state('page');
368 $self->send($self->msg('page', scalar @_));
380 # this is the thing that runs the command, it is done like this for the
381 # benefit of remote command execution
387 my $user = $self->{user};
388 my $call = $self->{call};
393 return () if length $cmdline == 0;
396 $cmdline =~ s|//|/|og;
398 # split the command line up into parts, the first part is the command
399 my ($cmd, $args) = split /\s+/, $cmdline, 2;
400 $args = "" unless defined $args;
406 dbg("cmd: $cmd") if isdbg('command');
408 # alias it if possible
409 my $acmd = CmdAlias::get_cmd($cmd);
411 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
412 $args = "" unless defined $args;
413 dbg("aliased cmd: $cmd $args") if isdbg('command');
416 # first expand out the entry to a command
417 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
418 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
421 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
423 my $package = find_cmd_name($path, $fcmd);
428 dbg("package: $package") if isdbg('command');
429 eval { @ans = &$package($self, $args) };
430 return (DXDebug::shortmess($@)) if $@;
433 dbg("cmd: $cmd not found") if isdbg('command');
434 if (++$self->{errors} > $maxerrors) {
435 $self->send($self->msg('e26'));
439 return ($self->msg('e1'));
446 delete $self->{errors};
448 if (++$self->{errors} > $maxerrors) {
449 $self->send($self->msg('e26'));
458 # This is called from inside the main cluster processing loop and is used
459 # for despatching commands that are doing some long processing job
464 my @dxchan = DXChannel->get_all();
467 foreach $dxchan (@dxchan) {
468 next if $dxchan->sort ne 'U';
470 # send a prompt if no activity out on this channel
471 if ($t >= $dxchan->t + $main::user_interval) {
472 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
477 while (my ($k, $v) = each %nothereslug) {
478 if ($main::systime >= $v + 300) {
479 delete $nothereslug{$k};
485 # finish up a user context
490 my $call = $self->call;
492 return if $self->{disconnecting}++;
494 delete $self->{senddbg};
496 my $uref = Route::User::get($call);
499 @rout = $main::routeroot->del_user($uref);
500 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
502 # issue a pc17 to everybody interested
503 DXProt::route_pc17($main::me, $main::routeroot, $uref);
505 confess "trying to disconnect a non existant user $call";
508 # I was the last node visited
509 $self->user->node($main::mycall);
511 # send info to all logged in thingies
512 $self->tell_login('logoutu');
514 Log('DXCommand', "$call disconnected");
516 $self->SUPER::disconnect;
520 # short cut to output a prompt
526 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
529 # broadcast a message to all users [except those mentioned after buffer]
532 my $pkg = shift; # ignored
533 my $s = shift; # the line to be rebroadcast
535 foreach my $dxchan (DXChannel->get_all()) {
536 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
537 next if grep $dxchan == $_, @_;
538 $dxchan->send($s); # send it
542 # gimme all the users
545 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
548 # run a script for this user
552 my $silent = shift || 0;
557 # search for the command in the cache of short->long form commands
562 my ($path, $short_cmd, $suffix) = @_;
565 # commands are lower case
566 $short_cmd = lc $short_cmd;
567 dbg("command: $path $short_cmd\n") if isdbg('command');
569 # do some checking for funny characters
570 return () if $short_cmd =~ /\/$/;
572 # return immediately if we have it
573 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
574 if ($apath && $acmd) {
575 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
576 return ($apath, $acmd);
580 my @parts = split '/', $short_cmd;
587 for ($i = 0; $i < @parts; $i++) {
589 opendir(D, $curdir) or confess "can't open $curdir $!";
593 foreach $l (sort @ls) {
595 if ($i < $#parts) { # we are dealing with directories
596 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
597 dbg("got dir: $curdir/$l\n") if isdbg('command');
602 } else { # we are dealing with commands
603 @lparts = split /\./, $l;
604 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
605 if ($p eq substr($l, 0, length $p)) {
606 pop @lparts; # remove the suffix
607 $l = join '.', @lparts;
608 # chop $dirfn; # remove trailing /
609 $dirfn = "" unless $dirfn;
610 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
611 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
612 return ($path, "$dirfn$l");
620 # clear the command name cache
627 dbg("Undefining cmd $_") if isdbg('command');
634 # the persistant execution of things from the command directories
637 # This allows perl programs to call functions dynamically
639 # This has been nicked directly from the perlembed pages
642 #require Devel::Symdump;
644 sub valid_package_name {
646 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
649 return "cmd_$string";
653 # this bit of magic finds a command in the offered directory
657 my $package = valid_package_name($cmdname);
658 my $filename = "$path/$cmdname.pl";
659 my $mtime = -M $filename;
661 # return if we can't find it
663 unless (defined $mtime) {
664 $errstr = DXM::msg('e1');
668 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
669 #we have compiled this subroutine already,
670 #it has not been updated on disk, nothing left to do
671 #print STDERR "already compiled $package->handler\n";
675 my $sub = readfilestr($filename);
677 $errstr = "Syserr: can't open '$filename' $!";
681 #wrap the code into a subroutine inside our unique package
682 my $eval = qq( sub $package { $sub } );
685 my @list = split /\n/, $eval;
688 dbg($_ . "\n") if isdbg('eval');
692 # get rid of any existing sub and try to compile the new one
695 if (exists $Cache{$package}) {
696 dbg("Redefining $package") if isdbg('command');
699 dbg("Defining $package") if isdbg('command');
703 $Cache{$package} = {mtime => $mtime };
712 my ($self, $let, $buf) = @_;
713 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
714 if ($self->{enhanced}) {
715 $self->send_later($let, $buf);
724 # send a talk message here
727 my ($self, $from, $to, $via, $line) = @_;
728 $line =~ s/\\5E/\^/g;
729 $self->local_send('T', "$to de $from: $line") if $self->{talk};
730 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
731 # send a 'not here' message if required
732 unless ($self->{here} && $from ne $to) {
733 my $key = "$to$from";
734 unless (exists $nothereslug{$key}) {
736 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
737 my $name = $self->user->name || $to;
738 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
739 $nothereslug{$key} = $main::systime;
740 $dxchan->talk($to, $from, undef, $s);
757 if (!$self->{ann_talk} && $to ne $self->{call}) {
758 my $call = AnnTalk::is_talk_candidate($_[0], $text);
762 if ($self->{annfilter}) {
763 ($filter, $hops) = $self->{annfilter}->it(@_ );
764 return unless $filter;
767 unless ($self->{ann}) {
768 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
770 return if $target eq 'SYSOP' && $self->{priv} < 5;
771 my $buf = "$to$target de $_[0]: $text";
773 $buf .= "\a\a" if $self->{beep};
774 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
785 return unless $self->{dx};
787 if ($self->{spotsfilter}) {
788 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
789 return unless $filter;
793 my $t = ztime($_[2]);
794 my $ref = DXUser->get_current($_[4]);
795 my $loc = $ref->qra if $ref && $ref->qra && $self->{user}->wantgrid;
796 $loc = ' ' . substr($loc, 0, 4) if $loc;
797 $loc = "" unless $loc;
798 my $buf = sprintf "DX de %-7.7s%11.1f %-12.12s %-*s $t$loc", "$_[4]:", $_[0], $_[1], $self->{consort} eq 'local' ? 29 : 30, $_[3];
799 $buf .= "\a\a" if $self->{beep};
801 $self->local_send('X', $buf);
811 return unless $self->{wwv};
813 if ($self->{wwvfilter}) {
814 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
815 return unless $filter;
818 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
819 $buf .= "\a\a" if $self->{beep};
820 $self->local_send('V', $buf);
830 return unless $self->{wcy};
832 if ($self->{wcyfilter}) {
833 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
834 return unless $filter;
837 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
838 $buf .= "\a\a" if $self->{beep};
839 $self->local_send('Y', $buf);
842 # broadcast debug stuff to all interested parties
845 my $s = shift; # the line to be rebroadcast
847 foreach my $dxchan (DXChannel->get_all) {
848 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
849 $dxchan->send_later('L', $s);