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 new dx spot stuff
113 $user->wantdxcq(0) unless defined $user->{wantdxcq};
114 $user->wantdxitu(0) unless defined $user->{wantdxitu};
115 $user->wantusstate(0) unless defined $user->{wantusstate};
117 # sort out registration
118 if ($main::reqreg == 1) {
119 $self->{registered} = $user->registered;
120 } elsif ($main::reqreg == 2) {
121 $self->{registered} = !$user->registered;
123 $self->{registered} = 1;
127 # decide which motd to send
128 my $motd = "${main::motd}_nor" unless $self->{registered};
129 $motd = $main::motd unless $motd && -e $motd;
130 $self->send_file($motd) if -e $motd;
132 # sort out privilege reduction
133 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
136 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
137 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
138 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
139 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
141 # clean up qra locators
142 my $qra = $user->qra;
143 $qra = undef if ($qra && !DXBearing::is_qra($qra));
145 my $lat = $user->lat;
146 my $long = $user->long;
147 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
151 my $echo = $user->wantecho;
153 $self->send_now('E', "0");
154 $self->send($self->msg('echow'));
155 $self->conn->echo($echo) if $self->conn->can('echo');
158 $self->tell_login('loginu');
160 # do we need to send a forward/opernam?
161 my $lastoper = $user->lastoper || 0;
162 my $homenode = $user->homenode || "";
163 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
164 run_cmd($main::me, "forward/opernam $call");
165 $user->lastoper($main::systime + ((int rand(10)) * 86400));
168 # run a script send the output to the punter
169 my $script = new Script(lc $call) || new Script('user_default');
170 $script->run($self) if $script;
173 my $info = Route::cluster();
174 $self->send("Cluster:$info");
176 # send prompts and things
177 $self->send($self->msg('namee1')) if !$user->name;
178 $self->send($self->msg('qthe1')) if !$user->qth;
179 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
180 $self->send($self->msg('hnodee1')) if !$user->qth;
181 $self->send($self->msg('m9')) if DXMsg::for_me($call);
186 # This is the normal command prompt driver
195 # save this for them's that need it
196 my $rawline = $cmdline;
198 # remove leading and trailing spaces
199 $cmdline =~ s/^\s*(.*)\s*$/$1/;
201 if ($self->{state} eq 'page') {
202 my $i = $self->{pagelth};
203 my $ref = $self->{pagedata};
206 # abort if we get a line starting in with a
207 if ($cmdline =~ /^a/io) {
212 # send a tranche of data
213 while ($i-- > 0 && @$ref) {
214 my $line = shift @$ref;
215 $line =~ s/\s+$//o; # why am having to do this?
219 # reset state if none or else chuck out an intermediate prompt
221 $tot -= $self->{pagelth};
222 $self->send($self->msg('page', $tot));
224 $self->state('prompt');
226 } elsif ($self->{state} eq 'sysop') {
227 my $passwd = $self->{user}->passwd;
229 my @pw = grep {$_ !~ /\s/} split //, $passwd;
230 my @l = @{$self->{passwd}};
231 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
232 if ($cmdline =~ /$str/) {
233 $self->{priv} = $self->{user}->priv;
235 $self->send($self->msg('sorry'));
238 $self->send($self->msg('sorry'));
240 $self->state('prompt');
241 } elsif ($self->{state} eq 'passwd') {
242 my $passwd = $self->{user}->passwd;
243 if ($passwd && $cmdline eq $passwd) {
244 $self->send($self->msg('pw1'));
245 $self->state('passwd1');
247 $self->conn->{echo} = $self->conn->{decho};
248 delete $self->conn->{decho};
249 $self->send($self->msg('sorry'));
250 $self->state('prompt');
252 } elsif ($self->{state} eq 'passwd1') {
253 $self->{passwd} = $cmdline;
254 $self->send($self->msg('pw2'));
255 $self->state('passwd2');
256 } elsif ($self->{state} eq 'passwd2') {
257 if ($cmdline eq $self->{passwd}) {
258 $self->{user}->passwd($cmdline);
259 $self->send($self->msg('pw3'));
261 $self->send($self->msg('pw4'));
263 $self->conn->{echo} = $self->conn->{decho};
264 delete $self->conn->{decho};
265 $self->state('prompt');
266 } elsif ($self->{state} eq 'talk') {
267 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
268 for (@{$self->{talklist}}) {
269 $self->send_talks($_, $self->msg('talkend'));
271 $self->state('prompt');
272 delete $self->{talklist};
273 } elsif ($cmdline =~ m|^/+\w+|) {
275 my $sendit = $cmdline =~ s|^/+||;
276 my @in = $self->run_cmd($cmdline);
277 $self->send_ans(@in);
278 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
279 foreach my $l (@in) {
281 if (@bad = BadWords::check($l)) {
282 $self->badcount(($self->badcount||0) + @bad);
283 Log('DXCommand', "$self->{call} swore: $l");
285 for (@{$self->{talklist}}) {
286 $self->send_talks($_, $l);
291 $self->send($self->talk_prompt);
292 } elsif ($self->{talklist} && @{$self->{talklist}}) {
293 # send what has been said to whoever is in this person's talk list
295 if (@bad = BadWords::check($cmdline)) {
296 $self->badcount(($self->badcount||0) + @bad);
297 Log('DXCommand', "$self->{call} swore: $cmdline");
299 for (@{$self->{talklist}}) {
300 $self->send_talks($_, $rawline);
303 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
306 $self->state('prompt');
308 } elsif (my $func = $self->{func}) {
311 if (ref $self->{edit}) {
312 eval { @ans = $self->{edit}->$func($self, $rawline)};
314 eval { @ans = &{$self->{func}}($self, $rawline) };
317 $self->send_ans("Syserr: on stored func $self->{func}", $@);
318 delete $self->{func};
319 $self->state('prompt');
322 $self->send_ans(@ans);
324 $self->send_ans(run_cmd($self, $cmdline));
327 # check for excessive swearing
328 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
329 Log('DXCommand', "$self->{call} logged out for excessive swearing");
334 # send a prompt only if we are in a prompt state
335 $self->prompt() if $self->{state} =~ /^prompt/o;
338 # send out the talk messages taking into account vias and connectivity
341 my ($self, $ent, $line) = @_;
343 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
344 $to = $ent unless $to;
345 my $call = $via ? $via : $to;
346 my $clref = Route::get($call);
347 my $dxchan = $clref->dxchan if $clref;
349 $dxchan->talk($self->{call}, $to, $via, $line);
351 $self->send($self->msg('disc2', $via ? $via : $to));
352 my @l = grep { $_ ne $ent } @{$self->{talklist}};
354 $self->{talklist} = \@l;
356 delete $self->{talklist};
357 $self->state('prompt');
366 for (@{$self->{talklist}}) {
367 my ($to, $via) = /(\S+)>(\S+)/;
371 return $self->msg('talkprompt', join(',', @call));
375 # send a load of stuff to a command user with page prompting
383 if ($self->{pagelth} && @_ > $self->{pagelth}) {
385 for ($i = $self->{pagelth}; $i-- > 0; ) {
387 $line =~ s/\s+$//o; # why am having to do this?
390 $self->{pagedata} = [ @_ ];
391 $self->state('page');
392 $self->send($self->msg('page', scalar @_));
404 # this is the thing that runs the command, it is done like this for the
405 # benefit of remote command execution
411 my $user = $self->{user};
412 my $call = $self->{call};
417 return () if length $cmdline == 0;
420 $cmdline =~ s|//|/|og;
422 # split the command line up into parts, the first part is the command
423 my ($cmd, $args) = split /\s+/, $cmdline, 2;
424 $args = "" unless defined $args;
430 dbg("cmd: $cmd") if isdbg('command');
432 # alias it if possible
433 my $acmd = CmdAlias::get_cmd($cmd);
435 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
436 $args = "" unless defined $args;
437 dbg("aliased cmd: $cmd $args") if isdbg('command');
440 # first expand out the entry to a command
441 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
442 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
445 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
447 my $package = find_cmd_name($path, $fcmd);
452 dbg("package: $package") if isdbg('command');
453 eval { @ans = &$package($self, $args) };
454 return (DXDebug::shortmess($@)) if $@;
457 dbg("cmd: $cmd not found") if isdbg('command');
458 if (++$self->{errors} > $maxerrors) {
459 $self->send($self->msg('e26'));
463 return ($self->msg('e1'));
470 delete $self->{errors};
472 if (++$self->{errors} > $maxerrors) {
473 $self->send($self->msg('e26'));
478 return map {s/([^\s])\s+$/$1/; $_} @ans;
482 # This is called from inside the main cluster processing loop and is used
483 # for despatching commands that are doing some long processing job
488 my @dxchan = DXChannel->get_all();
491 foreach $dxchan (@dxchan) {
492 next if $dxchan->sort ne 'U';
494 # send a prompt if no activity out on this channel
495 if ($t >= $dxchan->t + $main::user_interval) {
496 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
501 while (my ($k, $v) = each %nothereslug) {
502 if ($main::systime >= $v + 300) {
503 delete $nothereslug{$k};
509 # finish up a user context
514 my $call = $self->call;
516 return if $self->{disconnecting}++;
518 delete $self->{senddbg};
520 my $uref = Route::User::get($call);
523 @rout = $main::routeroot->del_user($uref);
524 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
526 # issue a pc17 to everybody interested
527 DXProt::route_pc17($main::me, $main::routeroot, $uref);
529 confess "trying to disconnect a non existant user $call";
532 # I was the last node visited
533 $self->user->node($main::mycall);
535 # send info to all logged in thingies
536 $self->tell_login('logoutu');
538 Log('DXCommand', "$call disconnected");
540 $self->SUPER::disconnect;
544 # short cut to output a prompt
550 if ($self->{prompt}) {
551 $self->send($self->{prompt});
553 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
557 # broadcast a message to all users [except those mentioned after buffer]
560 my $pkg = shift; # ignored
561 my $s = shift; # the line to be rebroadcast
563 foreach my $dxchan (DXChannel->get_all()) {
564 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
565 next if grep $dxchan == $_, @_;
566 $dxchan->send($s); # send it
570 # gimme all the users
573 return grep {$_->{sort} eq 'U'} DXChannel->get_all();
576 # run a script for this user
580 my $silent = shift || 0;
585 # search for the command in the cache of short->long form commands
590 my ($path, $short_cmd, $suffix) = @_;
593 # commands are lower case
594 $short_cmd = lc $short_cmd;
595 dbg("command: $path $short_cmd\n") if isdbg('command');
597 # do some checking for funny characters
598 return () if $short_cmd =~ /\/$/;
600 # return immediately if we have it
601 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
602 if ($apath && $acmd) {
603 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
604 return ($apath, $acmd);
608 my @parts = split '/', $short_cmd;
615 for ($i = 0; $i < @parts; $i++) {
617 opendir(D, $curdir) or confess "can't open $curdir $!";
621 foreach $l (sort @ls) {
623 if ($i < $#parts) { # we are dealing with directories
624 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
625 dbg("got dir: $curdir/$l\n") if isdbg('command');
630 } else { # we are dealing with commands
631 @lparts = split /\./, $l;
632 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
633 if ($p eq substr($l, 0, length $p)) {
634 pop @lparts; # remove the suffix
635 $l = join '.', @lparts;
636 # chop $dirfn; # remove trailing /
637 $dirfn = "" unless $dirfn;
638 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
639 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
640 return ($path, "$dirfn$l");
648 # clear the command name cache
654 undef *{$_} unless /cmd_cache/;
655 dbg("Undefining cmd $_") if isdbg('command');
662 # the persistant execution of things from the command directories
665 # This allows perl programs to call functions dynamically
667 # This has been nicked directly from the perlembed pages
670 #require Devel::Symdump;
672 sub valid_package_name {
674 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
677 return "cmd_$string";
681 # this bit of magic finds a command in the offered directory
685 my $package = valid_package_name($cmdname);
686 my $filename = "$path/$cmdname.pl";
687 my $mtime = -M $filename;
689 # return if we can't find it
691 unless (defined $mtime) {
692 $errstr = DXM::msg('e1');
696 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
697 #we have compiled this subroutine already,
698 #it has not been updated on disk, nothing left to do
699 #print STDERR "already compiled $package->handler\n";
703 my $sub = readfilestr($filename);
705 $errstr = "Syserr: can't open '$filename' $!";
709 #wrap the code into a subroutine inside our unique package
710 my $eval = qq( sub $package { $sub } );
713 my @list = split /\n/, $eval;
716 dbg($_ . "\n") if isdbg('eval');
720 # get rid of any existing sub and try to compile the new one
723 if (exists $Cache{$package}) {
724 dbg("Redefining $package") if isdbg('command');
727 dbg("Defining $package") if isdbg('command');
731 $Cache{$package} = {mtime => $mtime };
740 my ($self, $let, $buf) = @_;
741 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
742 if ($self->{enhanced}) {
743 $self->send_later($let, $buf);
752 # send a talk message here
755 my ($self, $from, $to, $via, $line) = @_;
756 $line =~ s/\\5E/\^/g;
757 $self->local_send('T', "$to de $from: $line") if $self->{talk};
758 Log('talk', $to, $from, $via?$via:$main::mycall, $line);
759 # send a 'not here' message if required
760 unless ($self->{here} && $from ne $to) {
761 my $key = "$to$from";
762 unless (exists $nothereslug{$key}) {
764 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
765 my $name = $self->user->name || $to;
766 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
767 $nothereslug{$key} = $main::systime;
768 $dxchan->talk($to, $from, undef, $s);
785 if (!$self->{ann_talk} && $to ne $self->{call}) {
786 my $call = AnnTalk::is_talk_candidate($_[0], $text);
790 if ($self->{annfilter}) {
791 ($filter, $hops) = $self->{annfilter}->it(@_ );
792 return unless $filter;
795 unless ($self->{ann}) {
796 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
798 return if $target eq 'SYSOP' && $self->{priv} < 5;
799 my $buf = "$to$target de $_[0]: $text";
801 $buf .= "\a\a" if $self->{beep};
802 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
813 return unless $self->{dx};
815 if ($self->{spotsfilter}) {
816 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
817 return unless $filter;
821 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
823 my $t = ztime($_[2]);
825 my $clth = $self->{consort} eq 'local' ? 29 : 30;
826 my $comment = substr $_[3], 0, $clth;
827 $comment .= ' ' x ($clth - length($comment));
828 my $ref = DXUser->get_current($_[4]);
830 $loc = $ref->qra || '' if $self->{user}->wantgrid;
831 $loc = ' ' . substr($loc, 0, 4) if $loc;
833 $loc = "" unless $loc;
835 if ($self->{user}->wantdxitu) {
836 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
837 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
838 } elsif ($self->{user}->wantdxcq) {
839 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
840 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
841 } elsif ($self->{user}->wantusstate) {
842 $loc = ' ' . $_[13] if $_[13];
843 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
846 my $buf = sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
848 $buf .= "\a\a" if $self->{beep};
850 $self->local_send('X', $buf);
860 return unless $self->{wwv};
862 if ($self->{wwvfilter}) {
863 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
864 return unless $filter;
867 my $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
868 $buf .= "\a\a" if $self->{beep};
869 $self->local_send('V', $buf);
879 return unless $self->{wcy};
881 if ($self->{wcyfilter}) {
882 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
883 return unless $filter;
886 my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
887 $buf .= "\a\a" if $self->{beep};
888 $self->local_send('Y', $buf);
891 # broadcast debug stuff to all interested parties
894 my $s = shift; # the line to be rebroadcast
896 foreach my $dxchan (DXChannel->get_all) {
897 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
898 $dxchan->send_later('L', $s);