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
45 $maxbadcount $msgpolltime $default_pagelth);
47 %Cache = (); # cache of dynamically loaded routine's mod times
48 %cmd_cache = (); # cache of short names
49 $errstr = (); # error string from eval
50 %aliases = (); # aliases for (parts of) commands
51 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
52 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
53 $maxbadcount = 3; # no of bad words allowed before disconnection
54 $msgpolltime = 3600; # the time between polls for new messages
55 $default_pagelth = 20; # the default page length 0 = unlimited
59 use vars qw($VERSION $BRANCH);
61 main::mkver($VERSION = q$Revision$);
64 # obtain a new connection this is derived from dxchannel
69 my $self = DXChannel::alloc(@_);
71 # routing, this must go out here to prevent race condx
74 my @rout = $main::routeroot->add_user($call, 1);
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 my $pagelth = $user->pagelth;
103 $pagelth = $default_pagelth unless defined $pagelth;
104 $self->{pagelth} = $pagelth;
105 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
106 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
107 $self->{consort} = $line; # save the connection type
109 # set some necessary flags on the user if they are connecting
110 $self->{beep} = $user->wantbeep;
111 $self->{ann} = $user->wantann;
112 $self->{wwv} = $user->wantwwv;
113 $self->{wcy} = $user->wantwcy;
114 $self->{talk} = $user->wanttalk;
115 $self->{wx} = $user->wantwx;
116 $self->{dx} = $user->wantdx;
117 $self->{logininfo} = $user->wantlogininfo;
118 $self->{ann_talk} = $user->wantann_talk;
120 $self->{prompt} = $user->prompt if $user->prompt;
122 # sort out new dx spot stuff
123 $user->wantdxcq(0) unless defined $user->{wantdxcq};
124 $user->wantdxitu(0) unless defined $user->{wantdxitu};
125 $user->wantusstate(0) unless defined $user->{wantusstate};
127 # sort out registration
128 if ($main::reqreg == 1) {
129 $self->{registered} = $user->registered;
130 } elsif ($main::reqreg == 2) {
131 $self->{registered} = !$user->registered;
133 $self->{registered} = 1;
137 # decide which motd to send
139 unless ($self->{registered}) {
140 $motd = "${main::motd}_nor_$self->{lang}";
141 $motd = "${main::motd}_nor" unless -e $motd;
143 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
144 $motd = $main::motd unless $motd && -e $motd;
145 $self->send_file($motd) if -e $motd;
147 # sort out privilege reduction
148 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
151 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
152 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
153 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
154 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
156 # clean up qra locators
157 my $qra = $user->qra;
158 $qra = undef if ($qra && !DXBearing::is_qra($qra));
160 my $lat = $user->lat;
161 my $long = $user->long;
162 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
166 my $echo = $user->wantecho;
168 $self->send_now('E', "0");
169 $self->send($self->msg('echow'));
170 $self->conn->echo($echo) if $self->conn->can('echo');
173 $self->tell_login('loginu');
175 # do we need to send a forward/opernam?
176 my $lastoper = $user->lastoper || 0;
177 my $homenode = $user->homenode || "";
178 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
179 run_cmd($main::me, "forward/opernam $call");
180 $user->lastoper($main::systime + ((int rand(10)) * 86400));
183 # ALWAYS output the user
184 my $thing = Thingy::Hello->new(user => $call, h => $self->{here});
185 $thing->broadcast($self);
186 $self->lasthello($main::systime);
188 # run a script send the output to the punter
189 my $script = new Script(lc $call) || new Script('user_default');
190 $script->run($self) if $script;
193 my $info = Route::cluster();
194 $self->send("Cluster:$info");
196 # send prompts and things
197 $self->send($self->msg('namee1')) if !$user->name;
198 $self->send($self->msg('qthe1')) if !$user->qth;
199 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
200 $self->send($self->msg('hnodee1')) if !$user->qth;
201 $self->send($self->msg('m9')) if DXMsg::for_me($call);
202 $self->lastmsgpoll($main::systime);
207 # This is the normal command prompt driver
216 # save this for them's that need it
217 my $rawline = $cmdline;
219 # remove leading and trailing spaces
220 $cmdline =~ s/^\s*(.*)\s*$/$1/;
222 if ($self->{state} eq 'page') {
223 my $i = $self->{pagelth};
224 my $ref = $self->{pagedata};
227 # abort if we get a line starting in with a
228 if ($cmdline =~ /^a/io) {
233 # send a tranche of data
234 while ($i-- > 0 && @$ref) {
235 my $line = shift @$ref;
236 $line =~ s/\s+$//o; # why am having to do this?
240 # reset state if none or else chuck out an intermediate prompt
242 $tot -= $self->{pagelth};
243 $self->send($self->msg('page', $tot));
245 $self->state('prompt');
247 } elsif ($self->{state} eq 'sysop') {
248 my $passwd = $self->{user}->passwd;
250 my @pw = grep {$_ !~ /\s/} split //, $passwd;
251 my @l = @{$self->{passwd}};
252 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
253 if ($cmdline =~ /$str/) {
254 $self->{priv} = $self->{user}->priv;
256 $self->send($self->msg('sorry'));
259 $self->send($self->msg('sorry'));
261 $self->state('prompt');
262 } elsif ($self->{state} eq 'passwd') {
263 my $passwd = $self->{user}->passwd;
264 if ($passwd && $cmdline eq $passwd) {
265 $self->send($self->msg('pw1'));
266 $self->state('passwd1');
268 $self->conn->{echo} = $self->conn->{decho};
269 delete $self->conn->{decho};
270 $self->send($self->msg('sorry'));
271 $self->state('prompt');
273 } elsif ($self->{state} eq 'passwd1') {
274 $self->{passwd} = $cmdline;
275 $self->send($self->msg('pw2'));
276 $self->state('passwd2');
277 } elsif ($self->{state} eq 'passwd2') {
278 if ($cmdline eq $self->{passwd}) {
279 $self->{user}->passwd($cmdline);
280 $self->send($self->msg('pw3'));
282 $self->send($self->msg('pw4'));
284 $self->conn->{echo} = $self->conn->{decho};
285 delete $self->conn->{decho};
286 $self->state('prompt');
287 } elsif ($self->{state} eq 'talk') {
288 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
289 for (@{$self->{talklist}}) {
290 $self->send_talks($_, $self->msg('talkend'));
292 $self->state('prompt');
293 delete $self->{talklist};
294 } elsif ($cmdline =~ m|^/+\w+|) {
296 my $sendit = $cmdline =~ s|^/+||;
297 my @in = $self->run_cmd($cmdline);
298 $self->send_ans(@in);
299 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
300 foreach my $l (@in) {
302 if (@bad = BadWords::check($l)) {
303 $self->badcount(($self->badcount||0) + @bad);
304 Log('DXCommand', "$self->{call} swore: $l");
306 for (@{$self->{talklist}}) {
307 $self->send_talks($_, $l);
312 $self->send($self->talk_prompt);
313 } elsif ($self->{talklist} && @{$self->{talklist}}) {
314 # send what has been said to whoever is in this person's talk list
316 if (@bad = BadWords::check($cmdline)) {
317 $self->badcount(($self->badcount||0) + @bad);
318 Log('DXCommand', "$self->{call} swore: $cmdline");
320 for (@{$self->{talklist}}) {
321 $self->send_talks($_, $rawline);
324 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
327 $self->state('prompt');
329 } elsif (my $func = $self->{func}) {
332 if (ref $self->{edit}) {
333 eval { @ans = $self->{edit}->$func($self, $rawline)};
335 eval { @ans = &{$self->{func}}($self, $rawline) };
338 $self->send_ans("Syserr: on stored func $self->{func}", $@);
339 delete $self->{func};
340 $self->state('prompt');
343 $self->send_ans(@ans);
345 $self->send_ans(run_cmd($self, $cmdline));
348 # check for excessive swearing
349 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
350 Log('DXCommand', "$self->{call} logged out for excessive swearing");
355 # send a prompt only if we are in a prompt state
356 $self->prompt() if $self->{state} =~ /^prompt/o;
359 # send out the talk messages taking into account vias and connectivity
362 my ($self, $ent, $line) = @_;
364 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
365 $to = $ent unless $to;
366 my $call = $via ? $via : $to;
367 my $clref = Route::get($call);
368 my $dxchan = $clref->dxchan if $clref;
370 $dxchan->talk($self->{call}, $to, $via, $line);
372 $self->send($self->msg('disc2', $via ? $via : $to));
373 my @l = grep { $_ ne $ent } @{$self->{talklist}};
375 $self->{talklist} = \@l;
377 delete $self->{talklist};
378 $self->state('prompt');
387 for (@{$self->{talklist}}) {
388 my ($to, $via) = /(\S+)>(\S+)/;
392 return $self->msg('talkprompt', join(',', @call));
396 # send a load of stuff to a command user with page prompting
404 if ($self->{pagelth} && @_ > $self->{pagelth}) {
406 for ($i = $self->{pagelth}; $i-- > 0; ) {
408 $line =~ s/\s+$//o; # why am having to do this?
411 $self->{pagedata} = [ @_ ];
412 $self->state('page');
413 $self->send($self->msg('page', scalar @_));
425 # this is the thing that runs the command, it is done like this for the
426 # benefit of remote command execution
432 my $user = $self->{user};
433 my $call = $self->{call};
438 return () if length $cmdline == 0;
440 # split the command line up into parts, the first part is the command
441 my ($cmd, $args) = split /\s+/, $cmdline, 2;
442 $args = "" unless defined $args;
445 # strip out // and .. on command only
447 $cmd =~ s|^/||g; # no leading / either
448 $cmd =~ s|[^-?\w/]||g; # and no funny characters
452 dbg("cmd: $cmd") if isdbg('command');
454 # alias it if possible
455 my $acmd = CmdAlias::get_cmd($cmd);
457 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
458 $args = "" unless defined $args;
459 dbg("aliased cmd: $cmd $args") if isdbg('command');
462 # first expand out the entry to a command
463 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
464 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
467 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
469 my $package = find_cmd_name($path, $fcmd);
474 dbg("package: $package") if isdbg('command');
475 eval { @ans = &$package($self, $args) };
476 return (DXDebug::shortmess($@)) if $@;
479 dbg("cmd: $cmd not found") if isdbg('command');
480 if (++$self->{errors} > $maxerrors) {
481 $self->send($self->msg('e26'));
485 return ($self->msg('e1'));
492 delete $self->{errors};
494 if (++$self->{errors} > $maxerrors) {
495 $self->send($self->msg('e26'));
500 return map {s/([^\s])\s+$/$1/; $_} @ans;
504 # This is called from inside the main cluster processing loop and is used
505 # for despatching commands that are doing some long processing job
510 my @dxchan = DXChannel::get_all();
513 foreach $dxchan (@dxchan) {
514 next if $dxchan->sort ne 'U';
516 # send a outstanding message prompt if required
517 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
518 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
519 $dxchan->lastmsgpoll($t);
522 # send a prompt if no activity out on this channel
523 if ($t >= $dxchan->t + $main::user_interval) {
524 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
529 while (my ($k, $v) = each %nothereslug) {
530 if ($main::systime >= $v + 300) {
531 delete $nothereslug{$k};
537 # finish up a user context
542 my $call = $self->call;
544 return if $self->{disconnecting}++;
546 delete $self->{senddbg};
548 my $uref = Route::User::get($call);
551 @rout = $main::routeroot->del_user($uref);
552 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
554 # issue a pc17 to everybody interested
555 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
557 my $thing = Thingy::Bye->new(user=>$call);
558 $thing->broadcast($self);
560 confess "trying to disconnect a non existant user $call";
563 # I was the last node visited
564 $self->user->node($main::mycall);
566 # send info to all logged in thingies
567 $self->tell_login('logoutu');
569 # remove any outstanding pings I have sent
570 Thingy::Ping::forget($call);
572 Log('DXCommand', "$call disconnected");
574 $self->SUPER::disconnect;
578 # short cut to output a prompt
584 my $call = $self->call;
585 my $date = cldate($main::systime);
586 my $time = ztime($main::systime);
587 my $prompt = $self->{prompt} || $self->msg('pr');
589 $call = "($call)" unless $self->here;
590 $prompt =~ s/\%C/$call/g;
591 $prompt =~ s/\%D/$date/g;
592 $prompt =~ s/\%T/$time/g;
593 $prompt =~ s/\%M/$main::mycall/g;
595 $self->send($prompt);
598 # broadcast a message to all users [except those mentioned after buffer]
601 my $pkg = shift; # ignored
602 my $s = shift; # the line to be rebroadcast
604 foreach my $dxchan (DXChannel::get_all()) {
605 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
606 next if grep $dxchan == $_, @_;
607 $dxchan->send($s); # send it
611 # gimme all the users
614 return grep {$_->{sort} eq 'U'} DXChannel::get_all();
617 # run a script for this user
621 my $silent = shift || 0;
626 # search for the command in the cache of short->long form commands
631 my ($path, $short_cmd, $suffix) = @_;
634 # commands are lower case
635 $short_cmd = lc $short_cmd;
636 dbg("command: $path $short_cmd\n") if isdbg('command');
638 # do some checking for funny characters
639 return () if $short_cmd =~ /\/$/;
641 # return immediately if we have it
642 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
643 if ($apath && $acmd) {
644 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
645 return ($apath, $acmd);
649 my @parts = split '/', $short_cmd;
653 while (my $p = shift @parts) {
654 opendir(D, $curdir) or confess "can't open $curdir $!";
658 # if this isn't the last part
661 foreach my $l (sort @ls) {
663 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
664 dbg("got dir: $curdir/$l\n") if isdbg('command');
671 # only proceed if we find the directory asked for
672 return () unless $found;
674 foreach my $l (sort @ls) {
676 next unless $l =~ /\.$suffix$/;
677 if ($p eq substr($l, 0, length $p)) {
678 $l =~ s/\.$suffix$//;
679 $dirfn = "" unless $dirfn;
680 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
681 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
682 return ($path, "$dirfn$l");
690 # clear the command name cache
696 undef *{$_} unless /cmd_cache/;
697 dbg("Undefining cmd $_") if isdbg('command');
704 # the persistant execution of things from the command directories
707 # This allows perl programs to call functions dynamically
709 # This has been nicked directly from the perlembed pages
712 #require Devel::Symdump;
714 sub valid_package_name {
716 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
719 return "cmd_$string";
723 # this bit of magic finds a command in the offered directory
727 my $package = valid_package_name($cmdname);
728 my $filename = "$path/$cmdname.pl";
729 my $mtime = -M $filename;
731 # return if we can't find it
733 unless (defined $mtime) {
734 $errstr = DXM::msg('e1');
738 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
739 #we have compiled this subroutine already,
740 #it has not been updated on disk, nothing left to do
741 #print STDERR "already compiled $package->handler\n";
745 my $sub = readfilestr($filename);
747 $errstr = "Syserr: can't open '$filename' $!";
751 #wrap the code into a subroutine inside our unique package
752 my $eval = qq( sub $package { $sub } );
755 my @list = split /\n/, $eval;
758 dbg($_ . "\n") if isdbg('eval');
762 # get rid of any existing sub and try to compile the new one
765 if (exists $Cache{$package}) {
766 dbg("Redefining $package") if isdbg('command');
769 dbg("Defining $package") if isdbg('command');
773 $Cache{$package} = {mtime => $mtime };
782 my ($self, $let, $buf) = @_;
783 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
784 if ($self->{enhanced}) {
785 $self->send_later($let, $buf);
794 # send a talk message here
797 my ($self, $from, $to, $via, $line) = @_;
798 $line =~ s/\\5E/\^/g;
799 $self->local_send('T', "$to de $from: $line") if $self->{talk};
800 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
801 # send a 'not here' message if required
802 unless ($self->{here} && $from ne $to) {
803 my $key = "$to$from";
804 unless (exists $nothereslug{$key}) {
806 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
807 my $name = $self->user->name || $to;
808 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
809 $nothereslug{$key} = $main::systime;
810 $dxchan->talk($to, $from, undef, $s);
827 if (!$self->{ann_talk} && $to ne $self->{call}) {
828 my $call = AnnTalk::is_talk_candidate($_[0], $text);
832 if ($self->{annfilter}) {
833 ($filter, $hops) = $self->{annfilter}->it(@_ );
834 return unless $filter;
837 unless ($self->{ann}) {
838 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
840 return if $target eq 'SYSOP' && $self->{priv} < 5;
841 my $buf = "$to$target de $_[0]: $text";
843 $buf .= "\a\a" if $self->{beep};
844 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
858 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
860 $text =~ s/^\#\d+ //;
861 my $buf = "$target de $_[0]: $text";
863 $buf .= "\a\a" if $self->{beep};
864 $self->local_send('C', $buf);
875 return unless $self->{wwv};
877 if ($self->{wwvfilter}) {
878 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
879 return unless $filter;
882 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
883 $buf .= "\a\a" if $self->{beep};
884 $self->local_send('V', $buf);
894 return unless $self->{wcy};
896 if ($self->{wcyfilter}) {
897 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
898 return unless $filter;
901 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
902 $buf .= "\a\a" if $self->{beep};
903 $self->local_send('Y', $buf);
906 # broadcast debug stuff to all interested parties
909 my $s = shift; # the line to be rebroadcast
911 foreach my $dxchan (DXChannel::get_all) {
912 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
913 $dxchan->send_later('L', $s);
923 if ($self->state eq 'enterbody') {
924 my $loc = $self->{loc} || confess "local var gone missing" ;
925 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
927 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
929 $self->state('prompt');
930 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
931 push @out, $self->msg('m10');
932 delete $loc->{lines};
935 $self->state('prompt');
937 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
938 # i.e. it ain't and end or abort, therefore store the line
941 confess "Invalid state $self->{state}";
946 sub store_startup_script
949 my $loc = $self->{loc} || confess "local var gone missing" ;
951 my $call = $loc->{call} || confess "callsign gone missing";
952 confess "lines array gone missing" unless ref $loc->{lines};
953 my $r = Script::store($call, $loc->{lines});
956 push @out, $self->msg('m19', $call, $r);
958 push @out, $self->msg('m20', $call);
961 push @out, "error opening startup script $call $!";