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,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;
110 $self->{prompt} = $user->prompt if $user->prompt;
112 # sort out registration
113 if ($main::reqreg == 1) {
114 $self->{registered} = $user->registered;
115 } elsif ($main::reqreg == 2) {
116 $self->{registered} = !$user->registered;
118 $self->{registered} = 1;
122 # decide which motd to send
123 my $motd = "${main::motd}_nor" unless $self->{registered};
124 $motd = $main::motd unless $motd && -e $motd;
125 $self->send_file($motd) if -e $motd;
127 # sort out privilege reduction
128 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
131 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
132 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
133 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
134 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
136 # clean up qra locators
137 my $qra = $user->qra;
138 $qra = undef if ($qra && !DXBearing::is_qra($qra));
140 my $lat = $user->lat;
141 my $long = $user->long;
142 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
146 my $echo = $user->wantecho;
148 $self->send_now('E', "0");
149 $self->send($self->msg('echow'));
150 $self->conn->echo($echo) if $self->conn->can('echo');
153 $self->tell_login('loginu');
155 # do we need to send a forward/opernam?
156 my $lastoper = $user->lastoper || 0;
157 my $homenode = $user->homenode || "";
158 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
159 run_cmd($main::me, "forward/opernam $call");
160 $user->lastoper($main::systime + ((int rand(10)) * 86400));
163 # run a script send the output to the punter
164 my $script = new Script(lc $call) || new Script('user_default');
165 $script->run($self) if $script;
168 my $info = Route::cluster();
169 $self->send("Cluster:$info");
171 # send prompts and things
172 $self->send($self->msg('namee1')) if !$user->name;
173 $self->send($self->msg('qthe1')) if !$user->qth;
174 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
175 $self->send($self->msg('hnodee1')) if !$user->qth;
176 $self->send($self->msg('m9')) if DXMsg::for_me($call);
181 # This is the normal command prompt driver
190 # save this for them's that need it
191 my $rawline = $cmdline;
193 # remove leading and trailing spaces
194 $cmdline =~ s/^\s*(.*)\s*$/$1/;
196 if ($self->{state} eq 'page') {
197 my $i = $self->{pagelth};
198 my $ref = $self->{pagedata};
201 # abort if we get a line starting in with a
202 if ($cmdline =~ /^a/io) {
207 # send a tranche of data
208 while ($i-- > 0 && @$ref) {
209 my $line = shift @$ref;
210 $line =~ s/\s+$//o; # why am having to do this?
214 # reset state if none or else chuck out an intermediate prompt
216 $tot -= $self->{pagelth};
217 $self->send($self->msg('page', $tot));
219 $self->state('prompt');
221 } elsif ($self->{state} eq 'sysop') {
222 my $passwd = $self->{user}->passwd;
224 my @pw = grep {$_ !~ /\s/} split //, $passwd;
225 my @l = @{$self->{passwd}};
226 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
227 if ($cmdline =~ /$str/) {
228 $self->{priv} = $self->{user}->priv;
230 $self->send($self->msg('sorry'));
233 $self->send($self->msg('sorry'));
235 $self->state('prompt');
236 } elsif ($self->{state} eq 'passwd') {
237 my $passwd = $self->{user}->passwd;
238 if ($passwd && $cmdline eq $passwd) {
239 $self->send($self->msg('pw1'));
240 $self->state('passwd1');
242 $self->conn->{echo} = $self->conn->{decho};
243 delete $self->conn->{decho};
244 $self->send($self->msg('sorry'));
245 $self->state('prompt');
247 } elsif ($self->{state} eq 'passwd1') {
248 $self->{passwd} = $cmdline;
249 $self->send($self->msg('pw2'));
250 $self->state('passwd2');
251 } elsif ($self->{state} eq 'passwd2') {
252 if ($cmdline eq $self->{passwd}) {
253 $self->{user}->passwd($cmdline);
254 $self->send($self->msg('pw3'));
256 $self->send($self->msg('pw4'));
258 $self->conn->{echo} = $self->conn->{decho};
259 delete $self->conn->{decho};
260 $self->state('prompt');
261 } elsif ($self->{state} eq 'talk') {
262 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
263 for (@{$self->{talklist}}) {
264 $self->send_talks($_, $self->msg('talkend'));
266 $self->state('prompt');
267 delete $self->{talklist};
268 } elsif ($cmdline =~ m|^/+\w+|) {
270 my $sendit = $cmdline =~ s|^/+||;
271 my @in = $self->run_cmd($cmdline);
272 $self->send_ans(@in);
273 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
274 foreach my $l (@in) {
276 if (@bad = BadWords::check($l)) {
277 $self->badcount(($self->badcount||0) + @bad);
278 Log('DXCommand', "$self->{call} swore: $l");
280 for (@{$self->{talklist}}) {
281 $self->send_talks($_, $l);
286 $self->send($self->talk_prompt);
287 } elsif ($self->{talklist} && @{$self->{talklist}}) {
288 # send what has been said to whoever is in this person's talk list
290 if (@bad = BadWords::check($cmdline)) {
291 $self->badcount(($self->badcount||0) + @bad);
292 Log('DXCommand', "$self->{call} swore: $cmdline");
294 for (@{$self->{talklist}}) {
295 $self->send_talks($_, $rawline);
298 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
301 $self->state('prompt');
303 } elsif (my $func = $self->{func}) {
306 if (ref $self->{edit}) {
307 eval { @ans = $self->{edit}->$func($self, $rawline)};
309 eval { @ans = &{$self->{func}}($self, $rawline) };
312 $self->send_ans("Syserr: on stored func $self->{func}", $@);
313 delete $self->{func};
314 $self->state('prompt');
317 $self->send_ans(@ans);
319 $self->send_ans(run_cmd($self, $cmdline));
322 # check for excessive swearing
323 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
324 Log('DXCommand', "$self->{call} logged out for excessive swearing");
329 # send a prompt only if we are in a prompt state
330 $self->prompt() if $self->{state} =~ /^prompt/o;
333 # send out the talk messages taking into account vias and connectivity
336 my ($self, $ent, $line) = @_;
338 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
339 $to = $ent unless $to;
340 my $call = $via ? $via : $to;
341 my $clref = Route::get($call);
342 my $dxchan = $clref->dxchan if $clref;
344 $dxchan->talk($self->{call}, $to, $via, $line);
346 $self->send($self->msg('disc2', $via ? $via : $to));
347 my @l = grep { $_ ne $ent } @{$self->{talklist}};
349 $self->{talklist} = \@l;
351 delete $self->{talklist};
352 $self->state('prompt');
361 for (@{$self->{talklist}}) {
362 my ($to, $via) = /(\S+)>(\S+)/;
366 return $self->msg('talkprompt', join(',', @call));
370 # send a load of stuff to a command user with page prompting
378 if ($self->{pagelth} && @_ > $self->{pagelth}) {
380 for ($i = $self->{pagelth}; $i-- > 0; ) {
382 $line =~ s/\s+$//o; # why am having to do this?
385 $self->{pagedata} = [ @_ ];
386 $self->state('page');
387 $self->send($self->msg('page', scalar @_));
399 # this is the thing that runs the command, it is done like this for the
400 # benefit of remote command execution
406 my $user = $self->{user};
407 my $call = $self->{call};
412 return () if length $cmdline == 0;
415 $cmdline =~ s|//|/|og;
417 # split the command line up into parts, the first part is the command
418 my ($cmd, $args) = split /\s+/, $cmdline, 2;
419 $args = "" unless defined $args;
425 dbg("cmd: $cmd") if isdbg('command');
427 # alias it if possible
428 my $acmd = CmdAlias::get_cmd($cmd);
430 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
431 $args = "" unless defined $args;
432 dbg("aliased cmd: $cmd $args") if isdbg('command');
435 # first expand out the entry to a command
436 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
437 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
440 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
442 my $package = find_cmd_name($path, $fcmd);
447 dbg("package: $package") if isdbg('command');
448 eval { @ans = &$package($self, $args) };
449 return (DXDebug::shortmess($@)) if $@;
452 dbg("cmd: $cmd not found") if isdbg('command');
453 if (++$self->{errors} > $maxerrors) {
454 $self->send($self->msg('e26'));
458 return ($self->msg('e1'));
465 delete $self->{errors};
467 if (++$self->{errors} > $maxerrors) {
468 $self->send($self->msg('e26'));
473 return map {s/([^\s])\s+$/$1/; $_} @ans;
477 # This is called from inside the main cluster processing loop and is used
478 # for despatching commands that are doing some long processing job
483 my @dxchan = DXChannel->get_all();
486 foreach $dxchan (@dxchan) {
487 next if $dxchan->sort ne 'U';
489 # send a prompt if no activity out on this channel
490 if ($t >= $dxchan->t + $main::user_interval) {
491 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
496 while (my ($k, $v) = each %nothereslug) {
497 if ($main::systime >= $v + 300) {
498 delete $nothereslug{$k};
504 # finish up a user context
509 my $call = $self->call;
511 return if $self->{disconnecting}++;
513 delete $self->{senddbg};
515 my $uref = Route::User::get($call);
518 @rout = $main::routeroot->del_user($uref);
519 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
521 # issue a pc17 to everybody interested
522 DXProt::route_pc17($main::me, $main::routeroot, $uref);
524 confess "trying to disconnect a non existant user $call";
527 # I was the last node visited
528 $self->user->node($main::mycall);
530 # send info to all logged in thingies
531 $self->tell_login('logoutu');
533 Log('DXCommand', "$call disconnected");
535 $self->SUPER::disconnect;
539 # short cut to output a prompt
545 if ($self->{prompt}) {
546 $self->send($self->{prompt});
548 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
552 # broadcast a message to all users [except those mentioned after buffer]
555 my $pkg = shift; # ignored
556 my $s = shift; # the line to be rebroadcast
558 foreach my $dxchan (DXChannel->get_all()) {
559 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
560 next if grep $dxchan == $_, @_;
561 $dxchan->send($s); # send it
565 # gimme all the users
568 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
571 # run a script for this user
575 my $silent = shift || 0;
580 # search for the command in the cache of short->long form commands
585 my ($path, $short_cmd, $suffix) = @_;
588 # commands are lower case
589 $short_cmd = lc $short_cmd;
590 dbg("command: $path $short_cmd\n") if isdbg('command');
592 # do some checking for funny characters
593 return () if $short_cmd =~ /\/$/;
595 # return immediately if we have it
596 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
597 if ($apath && $acmd) {
598 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
599 return ($apath, $acmd);
603 my @parts = split '/', $short_cmd;
610 for ($i = 0; $i < @parts; $i++) {
612 opendir(D, $curdir) or confess "can't open $curdir $!";
616 foreach $l (sort @ls) {
618 if ($i < $#parts) { # we are dealing with directories
619 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
620 dbg("got dir: $curdir/$l\n") if isdbg('command');
625 } else { # we are dealing with commands
626 @lparts = split /\./, $l;
627 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
628 if ($p eq substr($l, 0, length $p)) {
629 pop @lparts; # remove the suffix
630 $l = join '.', @lparts;
631 # chop $dirfn; # remove trailing /
632 $dirfn = "" unless $dirfn;
633 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
634 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
635 return ($path, "$dirfn$l");
643 # clear the command name cache
649 undef *{$_} unless /cmd_cache/;
650 dbg("Undefining cmd $_") if isdbg('command');
657 # the persistant execution of things from the command directories
660 # This allows perl programs to call functions dynamically
662 # This has been nicked directly from the perlembed pages
665 #require Devel::Symdump;
667 sub valid_package_name {
669 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
672 return "cmd_$string";
676 # this bit of magic finds a command in the offered directory
680 my $package = valid_package_name($cmdname);
681 my $filename = "$path/$cmdname.pl";
682 my $mtime = -M $filename;
684 # return if we can't find it
686 unless (defined $mtime) {
687 $errstr = DXM::msg('e1');
691 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
692 #we have compiled this subroutine already,
693 #it has not been updated on disk, nothing left to do
694 #print STDERR "already compiled $package->handler\n";
698 my $sub = readfilestr($filename);
700 $errstr = "Syserr: can't open '$filename' $!";
704 #wrap the code into a subroutine inside our unique package
705 my $eval = qq( sub $package { $sub } );
708 my @list = split /\n/, $eval;
711 dbg($_ . "\n") if isdbg('eval');
715 # get rid of any existing sub and try to compile the new one
718 if (exists $Cache{$package}) {
719 dbg("Redefining $package") if isdbg('command');
722 dbg("Defining $package") if isdbg('command');
726 $Cache{$package} = {mtime => $mtime };
735 my ($self, $let, $buf) = @_;
736 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
737 if ($self->{enhanced}) {
738 $self->send_later($let, $buf);
747 # send a talk message here
750 my ($self, $from, $to, $via, $line) = @_;
751 $line =~ s/\\5E/\^/g;
752 $self->local_send('T', "$to de $from: $line") if $self->{talk};
753 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
754 # send a 'not here' message if required
755 unless ($self->{here} && $from ne $to) {
756 my $key = "$to$from";
757 unless (exists $nothereslug{$key}) {
759 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
760 my $name = $self->user->name || $to;
761 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
762 $nothereslug{$key} = $main::systime;
763 $dxchan->talk($to, $from, undef, $s);
780 if (!$self->{ann_talk} && $to ne $self->{call}) {
781 my $call = AnnTalk::is_talk_candidate($_[0], $text);
785 if ($self->{annfilter}) {
786 ($filter, $hops) = $self->{annfilter}->it(@_ );
787 return unless $filter;
790 unless ($self->{ann}) {
791 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
793 return if $target eq 'SYSOP' && $self->{priv} < 5;
794 my $buf = "$to$target de $_[0]: $text";
796 $buf .= "\a\a" if $self->{beep};
797 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
808 return unless $self->{dx};
810 if ($self->{spotsfilter}) {
811 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
812 return unless $filter;
816 my $t = ztime($_[2]);
818 my $clth = $self->{consort} eq 'local' ? 29 : 30;
819 my $comment = substr $_[3], 0, $clth;
820 $comment .= ' ' x ($clth - length($comment));
821 my $ref = DXUser->get_current($_[4]);
823 $loc = $ref->qra || '' if $self->{user}->wantgrid;
824 $loc = ' ' . substr($loc, 0, 4) if $loc;
826 $loc = "" unless $loc;
829 if ($USDB::present && $self->{user}->wantusstate) {
830 my ($city, $state) = USDB::get($_[4]);
834 ($city, $state) = USDB::get($_[1]);
836 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $state;
840 my $buf = sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
842 $buf .= "\a\a" if $self->{beep};
844 $self->local_send('X', $buf);
854 return unless $self->{wwv};
856 if ($self->{wwvfilter}) {
857 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
858 return unless $filter;
861 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
862 $buf .= "\a\a" if $self->{beep};
863 $self->local_send('V', $buf);
873 return unless $self->{wcy};
875 if ($self->{wcyfilter}) {
876 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
877 return unless $filter;
880 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
881 $buf .= "\a\a" if $self->{beep};
882 $self->local_send('Y', $buf);
885 # broadcast debug stuff to all interested parties
888 my $s = shift; # the line to be rebroadcast
890 foreach my $dxchan (DXChannel->get_all) {
891 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
892 $dxchan->send_later('L', $s);