3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
44 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %nothereslug $maxbadcount $msgpolltime);
46 %Cache = (); # cache of dynamically loaded routine's mod times
47 %cmd_cache = (); # cache of short names
48 $errstr = (); # error string from eval
49 %aliases = (); # aliases for (parts of) commands
50 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
51 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
52 $maxbadcount = 3; # no of bad words allowed before disconnection
53 $msgpolltime = 3600; # the time between polls for new messages
56 use vars qw($VERSION $BRANCH);
58 main::mkver($VERSION = q$Revision$);
61 # obtain a new connection this is derived from dxchannel
66 my $self = DXChannel::alloc(@_);
68 # routing, this must go out here to prevent race condx
71 my @rout = $main::routeroot->add_user($call, Route::here(1));
73 # ALWAYS output the user
74 my $thing = Thingy::Hello->new(user => $call);
75 $thing->broadcast($self);
77 my $ref = Route::User::get($call);
78 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref) if $ref;
82 # this is how a a connection starts, you get a hello message and the motd with
83 # possibly some other messages asking you to set various things up if you are
84 # new (or nearly new and slacking) user.
88 my ($self, $line, $sort) = @_;
89 my $user = $self->{user};
90 my $call = $self->{call};
91 my $name = $user->{name};
94 my $host = $self->{conn}->{peerhost} || "unknown";
95 Log('DXCommand', "$call connected from $host");
97 $self->{name} = $name ? $name : $call;
98 $self->send($self->msg('l2',$self->{name}));
99 $self->state('prompt'); # a bit of room for further expansion, passwords etc
100 $self->{priv} = $user->priv || 0;
101 $self->{lang} = $user->lang || $main::lang || 'en';
102 $self->{pagelth} = $user->pagelth || 20;
103 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
104 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
105 $self->{consort} = $line; # save the connection type
107 # set some necessary flags on the user if they are connecting
108 $self->{beep} = $user->wantbeep;
109 $self->{ann} = $user->wantann;
110 $self->{wwv} = $user->wantwwv;
111 $self->{wcy} = $user->wantwcy;
112 $self->{talk} = $user->wanttalk;
113 $self->{wx} = $user->wantwx;
114 $self->{dx} = $user->wantdx;
115 $self->{logininfo} = $user->wantlogininfo;
116 $self->{ann_talk} = $user->wantann_talk;
118 $self->{prompt} = $user->prompt if $user->prompt;
120 # sort out new dx spot stuff
121 $user->wantdxcq(0) unless defined $user->{wantdxcq};
122 $user->wantdxitu(0) unless defined $user->{wantdxitu};
123 $user->wantusstate(0) unless defined $user->{wantusstate};
125 # sort out registration
126 if ($main::reqreg == 1) {
127 $self->{registered} = $user->registered;
128 } elsif ($main::reqreg == 2) {
129 $self->{registered} = !$user->registered;
131 $self->{registered} = 1;
135 # decide which motd to send
137 unless ($self->{registered}) {
138 $motd = "${main::motd}_nor_$self->{lang}";
139 $motd = "${main::motd}_nor" unless -e $motd;
141 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
142 $motd = $main::motd unless $motd && -e $motd;
143 $self->send_file($motd) if -e $motd;
145 # sort out privilege reduction
146 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
149 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
150 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
151 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
152 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
154 # clean up qra locators
155 my $qra = $user->qra;
156 $qra = undef if ($qra && !DXBearing::is_qra($qra));
158 my $lat = $user->lat;
159 my $long = $user->long;
160 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
164 my $echo = $user->wantecho;
166 $self->send_now('E', "0");
167 $self->send($self->msg('echow'));
168 $self->conn->echo($echo) if $self->conn->can('echo');
171 $self->tell_login('loginu');
173 # do we need to send a forward/opernam?
174 my $lastoper = $user->lastoper || 0;
175 my $homenode = $user->homenode || "";
176 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
177 run_cmd($main::me, "forward/opernam $call");
178 $user->lastoper($main::systime + ((int rand(10)) * 86400));
181 # run a script send the output to the punter
182 my $script = new Script(lc $call) || new Script('user_default');
183 $script->run($self) if $script;
186 my $info = Route::cluster();
187 $self->send("Cluster:$info");
189 # send prompts and things
190 $self->send($self->msg('namee1')) if !$user->name;
191 $self->send($self->msg('qthe1')) if !$user->qth;
192 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
193 $self->send($self->msg('hnodee1')) if !$user->qth;
194 $self->send($self->msg('m9')) if DXMsg::for_me($call);
195 $self->lastmsgpoll($main::systime);
200 # This is the normal command prompt driver
209 # save this for them's that need it
210 my $rawline = $cmdline;
212 # remove leading and trailing spaces
213 $cmdline =~ s/^\s*(.*)\s*$/$1/;
215 if ($self->{state} eq 'page') {
216 my $i = $self->{pagelth};
217 my $ref = $self->{pagedata};
220 # abort if we get a line starting in with a
221 if ($cmdline =~ /^a/io) {
226 # send a tranche of data
227 while ($i-- > 0 && @$ref) {
228 my $line = shift @$ref;
229 $line =~ s/\s+$//o; # why am having to do this?
233 # reset state if none or else chuck out an intermediate prompt
235 $tot -= $self->{pagelth};
236 $self->send($self->msg('page', $tot));
238 $self->state('prompt');
240 } elsif ($self->{state} eq 'sysop') {
241 my $passwd = $self->{user}->passwd;
243 my @pw = grep {$_ !~ /\s/} split //, $passwd;
244 my @l = @{$self->{passwd}};
245 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
246 if ($cmdline =~ /$str/) {
247 $self->{priv} = $self->{user}->priv;
249 $self->send($self->msg('sorry'));
252 $self->send($self->msg('sorry'));
254 $self->state('prompt');
255 } elsif ($self->{state} eq 'passwd') {
256 my $passwd = $self->{user}->passwd;
257 if ($passwd && $cmdline eq $passwd) {
258 $self->send($self->msg('pw1'));
259 $self->state('passwd1');
261 $self->conn->{echo} = $self->conn->{decho};
262 delete $self->conn->{decho};
263 $self->send($self->msg('sorry'));
264 $self->state('prompt');
266 } elsif ($self->{state} eq 'passwd1') {
267 $self->{passwd} = $cmdline;
268 $self->send($self->msg('pw2'));
269 $self->state('passwd2');
270 } elsif ($self->{state} eq 'passwd2') {
271 if ($cmdline eq $self->{passwd}) {
272 $self->{user}->passwd($cmdline);
273 $self->send($self->msg('pw3'));
275 $self->send($self->msg('pw4'));
277 $self->conn->{echo} = $self->conn->{decho};
278 delete $self->conn->{decho};
279 $self->state('prompt');
280 } elsif ($self->{state} eq 'talk') {
281 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
282 for (@{$self->{talklist}}) {
283 $self->send_talks($_, $self->msg('talkend'));
285 $self->state('prompt');
286 delete $self->{talklist};
287 } elsif ($cmdline =~ m|^/+\w+|) {
289 my $sendit = $cmdline =~ s|^/+||;
290 my @in = $self->run_cmd($cmdline);
291 $self->send_ans(@in);
292 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
293 foreach my $l (@in) {
295 if (@bad = BadWords::check($l)) {
296 $self->badcount(($self->badcount||0) + @bad);
297 Log('DXCommand', "$self->{call} swore: $l");
299 for (@{$self->{talklist}}) {
300 $self->send_talks($_, $l);
305 $self->send($self->talk_prompt);
306 } elsif ($self->{talklist} && @{$self->{talklist}}) {
307 # send what has been said to whoever is in this person's talk list
309 if (@bad = BadWords::check($cmdline)) {
310 $self->badcount(($self->badcount||0) + @bad);
311 Log('DXCommand', "$self->{call} swore: $cmdline");
313 for (@{$self->{talklist}}) {
314 $self->send_talks($_, $rawline);
317 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
320 $self->state('prompt');
322 } elsif (my $func = $self->{func}) {
325 if (ref $self->{edit}) {
326 eval { @ans = $self->{edit}->$func($self, $rawline)};
328 eval { @ans = &{$self->{func}}($self, $rawline) };
331 $self->send_ans("Syserr: on stored func $self->{func}", $@);
332 delete $self->{func};
333 $self->state('prompt');
336 $self->send_ans(@ans);
338 $self->send_ans(run_cmd($self, $cmdline));
341 # check for excessive swearing
342 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
343 Log('DXCommand', "$self->{call} logged out for excessive swearing");
348 # send a prompt only if we are in a prompt state
349 $self->prompt() if $self->{state} =~ /^prompt/o;
352 # send out the talk messages taking into account vias and connectivity
355 my ($self, $ent, $line) = @_;
357 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
358 $to = $ent unless $to;
359 my $call = $via ? $via : $to;
360 my $clref = Route::get($call);
361 my $dxchan = $clref->dxchan if $clref;
363 $dxchan->talk($self->{call}, $to, $via, $line);
365 $self->send($self->msg('disc2', $via ? $via : $to));
366 my @l = grep { $_ ne $ent } @{$self->{talklist}};
368 $self->{talklist} = \@l;
370 delete $self->{talklist};
371 $self->state('prompt');
380 for (@{$self->{talklist}}) {
381 my ($to, $via) = /(\S+)>(\S+)/;
385 return $self->msg('talkprompt', join(',', @call));
389 # send a load of stuff to a command user with page prompting
397 if ($self->{pagelth} && @_ > $self->{pagelth}) {
399 for ($i = $self->{pagelth}; $i-- > 0; ) {
401 $line =~ s/\s+$//o; # why am having to do this?
404 $self->{pagedata} = [ @_ ];
405 $self->state('page');
406 $self->send($self->msg('page', scalar @_));
418 # this is the thing that runs the command, it is done like this for the
419 # benefit of remote command execution
425 my $user = $self->{user};
426 my $call = $self->{call};
431 return () if length $cmdline == 0;
433 # split the command line up into parts, the first part is the command
434 my ($cmd, $args) = split /\s+/, $cmdline, 2;
435 $args = "" unless defined $args;
438 # strip out // on command only
443 dbg("cmd: $cmd") if isdbg('command');
445 # alias it if possible
446 my $acmd = CmdAlias::get_cmd($cmd);
448 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
449 $args = "" unless defined $args;
450 dbg("aliased cmd: $cmd $args") if isdbg('command');
453 # first expand out the entry to a command
454 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
455 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
458 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
460 my $package = find_cmd_name($path, $fcmd);
465 dbg("package: $package") if isdbg('command');
466 eval { @ans = &$package($self, $args) };
467 return (DXDebug::shortmess($@)) if $@;
470 dbg("cmd: $cmd not found") if isdbg('command');
471 if (++$self->{errors} > $maxerrors) {
472 $self->send($self->msg('e26'));
476 return ($self->msg('e1'));
483 delete $self->{errors};
485 if (++$self->{errors} > $maxerrors) {
486 $self->send($self->msg('e26'));
491 return map {s/([^\s])\s+$/$1/; $_} @ans;
495 # This is called from inside the main cluster processing loop and is used
496 # for despatching commands that are doing some long processing job
501 my @dxchan = DXChannel->get_all();
504 foreach $dxchan (@dxchan) {
505 next if $dxchan->sort ne 'U';
507 # send a outstanding message prompt if required
508 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
509 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
510 $dxchan->lastmsgpoll($t);
513 # send a prompt if no activity out on this channel
514 if ($t >= $dxchan->t + $main::user_interval) {
515 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
520 while (my ($k, $v) = each %nothereslug) {
521 if ($main::systime >= $v + 300) {
522 delete $nothereslug{$k};
528 # finish up a user context
533 my $call = $self->call;
535 return if $self->{disconnecting}++;
537 delete $self->{senddbg};
539 my $uref = Route::User::get($call);
542 @rout = $main::routeroot->del_user($uref);
543 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
545 # issue a pc17 to everybody interested
546 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
548 my $thing = Thingy::Bye->new(user=>$call);
549 $thing->broadcast($self);
551 confess "trying to disconnect a non existant user $call";
554 # I was the last node visited
555 $self->user->node($main::mycall);
557 # send info to all logged in thingies
558 $self->tell_login('logoutu');
560 Log('DXCommand', "$call disconnected");
562 $self->SUPER::disconnect;
566 # short cut to output a prompt
572 my $call = $self->call;
573 my $date = cldate($main::systime);
574 my $time = ztime($main::systime);
575 my $prompt = $self->{prompt} || $self->msg('pr');
577 $call = "($call)" unless $self->here;
578 $prompt =~ s/\%C/$call/g;
579 $prompt =~ s/\%D/$date/g;
580 $prompt =~ s/\%T/$time/g;
581 $prompt =~ s/\%M/$main::mycall/g;
583 $self->send($prompt);
586 # broadcast a message to all users [except those mentioned after buffer]
589 my $pkg = shift; # ignored
590 my $s = shift; # the line to be rebroadcast
592 foreach my $dxchan (DXChannel->get_all()) {
593 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
594 next if grep $dxchan == $_, @_;
595 $dxchan->send($s); # send it
599 # gimme all the users
602 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
605 # run a script for this user
609 my $silent = shift || 0;
614 # search for the command in the cache of short->long form commands
619 my ($path, $short_cmd, $suffix) = @_;
622 # commands are lower case
623 $short_cmd = lc $short_cmd;
624 dbg("command: $path $short_cmd\n") if isdbg('command');
626 # do some checking for funny characters
627 return () if $short_cmd =~ /\/$/;
629 # return immediately if we have it
630 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
631 if ($apath && $acmd) {
632 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
633 return ($apath, $acmd);
637 my @parts = split '/', $short_cmd;
644 for ($i = 0; $i < @parts; $i++) {
646 opendir(D, $curdir) or confess "can't open $curdir $!";
650 foreach $l (sort @ls) {
652 if ($i < $#parts) { # we are dealing with directories
653 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
654 dbg("got dir: $curdir/$l\n") if isdbg('command');
659 } else { # we are dealing with commands
660 @lparts = split /\./, $l;
661 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
662 if ($p eq substr($l, 0, length $p)) {
663 pop @lparts; # remove the suffix
664 $l = join '.', @lparts;
665 # chop $dirfn; # remove trailing /
666 $dirfn = "" unless $dirfn;
667 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
668 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
669 return ($path, "$dirfn$l");
677 # clear the command name cache
683 undef *{$_} unless /cmd_cache/;
684 dbg("Undefining cmd $_") if isdbg('command');
691 # the persistant execution of things from the command directories
694 # This allows perl programs to call functions dynamically
696 # This has been nicked directly from the perlembed pages
699 #require Devel::Symdump;
701 sub valid_package_name {
703 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
706 return "cmd_$string";
710 # this bit of magic finds a command in the offered directory
714 my $package = valid_package_name($cmdname);
715 my $filename = "$path/$cmdname.pl";
716 my $mtime = -M $filename;
718 # return if we can't find it
720 unless (defined $mtime) {
721 $errstr = DXM::msg('e1');
725 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
726 #we have compiled this subroutine already,
727 #it has not been updated on disk, nothing left to do
728 #print STDERR "already compiled $package->handler\n";
732 my $sub = readfilestr($filename);
734 $errstr = "Syserr: can't open '$filename' $!";
738 #wrap the code into a subroutine inside our unique package
739 my $eval = qq( sub $package { $sub } );
742 my @list = split /\n/, $eval;
745 dbg($_ . "\n") if isdbg('eval');
749 # get rid of any existing sub and try to compile the new one
752 if (exists $Cache{$package}) {
753 dbg("Redefining $package") if isdbg('command');
756 dbg("Defining $package") if isdbg('command');
760 $Cache{$package} = {mtime => $mtime };
769 my ($self, $let, $buf) = @_;
770 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
771 if ($self->{enhanced}) {
772 $self->send_later($let, $buf);
781 # send a talk message here
784 my ($self, $from, $to, $via, $line) = @_;
785 $line =~ s/\\5E/\^/g;
786 $self->local_send('T', "$to de $from: $line") if $self->{talk};
787 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
788 # send a 'not here' message if required
789 unless ($self->{here} && $from ne $to) {
790 my $key = "$to$from";
791 unless (exists $nothereslug{$key}) {
793 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
794 my $name = $self->user->name || $to;
795 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
796 $nothereslug{$key} = $main::systime;
797 $dxchan->talk($to, $from, undef, $s);
814 if (!$self->{ann_talk} && $to ne $self->{call}) {
815 my $call = AnnTalk::is_talk_candidate($_[0], $text);
819 if ($self->{annfilter}) {
820 ($filter, $hops) = $self->{annfilter}->it(@_ );
821 return unless $filter;
824 unless ($self->{ann}) {
825 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
827 return if $target eq 'SYSOP' && $self->{priv} < 5;
828 my $buf = "$to$target de $_[0]: $text";
830 $buf .= "\a\a" if $self->{beep};
831 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
845 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
847 $text =~ s/^\#\d+ //;
848 my $buf = "$target de $_[0]: $text";
850 $buf .= "\a\a" if $self->{beep};
851 $self->local_send('C', $buf);
862 return unless $self->{wwv};
864 if ($self->{wwvfilter}) {
865 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
866 return unless $filter;
869 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
870 $buf .= "\a\a" if $self->{beep};
871 $self->local_send('V', $buf);
881 return unless $self->{wcy};
883 if ($self->{wcyfilter}) {
884 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
885 return unless $filter;
888 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
889 $buf .= "\a\a" if $self->{beep};
890 $self->local_send('Y', $buf);
893 # broadcast debug stuff to all interested parties
896 my $s = shift; # the line to be rebroadcast
898 foreach my $dxchan (DXChannel->get_all) {
899 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
900 $dxchan->send_later('L', $s);
910 if ($self->state eq 'enterbody') {
911 my $loc = $self->{loc} || confess "local var gone missing" ;
912 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
914 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
916 $self->state('prompt');
917 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
918 push @out, $self->msg('m10');
919 delete $loc->{lines};
922 $self->state('prompt');
924 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
925 # i.e. it ain't and end or abort, therefore store the line
928 confess "Invalid state $self->{state}";
933 sub store_startup_script
936 my $loc = $self->{loc} || confess "local var gone missing" ;
938 my $call = $loc->{call} || confess "callsign gone missing";
939 confess "lines array gone missing" unless ref $loc->{lines};
940 my $r = Script::store($call, $loc->{lines});
943 push @out, $self->msg('m19', $call, $r);
945 push @out, $self->msg('m20', $call);
948 push @out, "error opening startup script $call $!";