3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
34 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
36 %Cache = (); # cache of dynamically loaded routine's mod times
37 %cmd_cache = (); # cache of short names
38 $errstr = (); # error string from eval
39 %aliases = (); # aliases for (parts of) commands
40 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
43 # obtain a new connection this is derived from dxchannel
48 my $self = DXChannel::alloc(@_);
52 # this is how a a connection starts, you get a hello message and the motd with
53 # possibly some other messages asking you to set various things up if you are
54 # new (or nearly new and slacking) user.
58 my ($self, $line, $sort) = @_;
59 my $user = $self->{user};
60 my $call = $self->{call};
61 my $name = $user->{name};
63 $self->{name} = $name ? $name : $call;
64 $self->send($self->msg('l2',$self->{name}));
65 $self->send_file($main::motd) if (-e $main::motd);
66 $self->state('prompt'); # a bit of room for further expansion, passwords etc
67 $self->{priv} = $user->priv || 0;
68 $self->{lang} = $user->lang || 'en';
69 $self->{pagelth} = $user->pagelth || 20;
70 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
71 $self->{consort} = $line; # save the connection type
73 # set some necessary flags on the user if they are connecting
74 $self->{beep} = $user->wantbeep;
75 $self->{ann} = $user->wantann;
76 $self->{wwv} = $user->wantwwv;
77 $self->{wcy} = $user->wantwcy;
78 $self->{talk} = $user->wanttalk;
79 $self->{wx} = $user->wantwx;
80 $self->{dx} = $user->wantdx;
81 $self->{logininfo} = $user->wantlogininfo;
85 $self->{spotfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
86 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
87 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
88 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
90 # clean up qra locators
92 $qra = undef if ($qra && !DXBearing::is_qra($qra));
95 my $long = $user->long;
96 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
99 # add yourself to the database
100 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
101 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
102 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
104 # issue a pc16 to everybody interested
105 my $nchan = DXChannel->get($main::mycall);
106 my @pc16 = DXProt::pc16($nchan, $cuser);
108 DXProt::broadcast_all_ak1a($_);
110 Log('DXCommand', "$call connected");
112 # send prompts and things
113 my $info = DXCluster::cluster();
114 $self->send("Cluster:$info");
115 $self->send($self->msg('namee1')) if !$user->name;
116 $self->send($self->msg('qthe1')) if !$user->qth;
117 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
118 $self->send($self->msg('hnodee1')) if !$user->qth;
119 $self->send($self->msg('m9')) if DXMsg::for_me($call);
120 $self->send($self->msg('pr', $call));
123 if (!$user->wantecho) {
124 $self->send_now('E', "0");
125 $self->send($self->msg('echow'));
128 $self->tell_login('loginu');
133 # This is the normal command prompt driver
142 # remove leading and trailing spaces
143 $cmdline =~ s/^\s*(.*)\s*$/$1/;
145 if ($self->{state} eq 'page') {
146 my $i = $self->{pagelth};
147 my $ref = $self->{pagedata};
150 # abort if we get a line starting in with a
151 if ($cmdline =~ /^a/io) {
156 # send a tranche of data
157 while ($i-- > 0 && @$ref) {
158 my $line = shift @$ref;
159 $line =~ s/\s+$//o; # why am having to do this?
163 # reset state if none or else chuck out an intermediate prompt
165 $tot -= $self->{pagelth};
166 $self->send($self->msg('page', $tot));
168 $self->state('prompt');
170 } elsif ($self->{state} eq 'sysop') {
171 my $passwd = $self->{user}->passwd;
172 my @pw = split / */, $passwd;
174 my @l = @{$self->{passwd}};
175 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
176 if ($cmdline =~ /$str/) {
177 $self->{priv} = $self->{user}->priv;
179 $self->send($self->msg('sorry'));
182 $self->send($self->msg('sorry'));
184 delete $self->{passwd};
185 $self->state('prompt');
186 } elsif ($self->{state} eq 'talk') {
187 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
188 for (@{$self->{talklist}}) {
189 $self->send_talks($_, $self->msg('talkend'));
191 $self->state('prompt');
192 delete $self->{talklist};
193 } elsif ($cmdline =~ m(^/\w+)) {
195 $self->send_ans(run_cmd($self, $cmdline));
196 $self->send($self->talk_prompt);
197 } elsif ($self->{talklist} && @{$self->{talklist}}) {
198 # send what has been said to whoever is in this person's talk list
199 for (@{$self->{talklist}}) {
200 $self->send_talks($_, $cmdline);
202 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
205 $self->state('prompt');
208 $self->send_ans(run_cmd($self, $cmdline));
211 # send a prompt only if we are in a prompt state
212 $self->prompt() if $self->{state} =~ /^prompt/o;
215 # send out the talk messages taking into account vias and connectivity
218 my ($self, $ent, $line) = @_;
220 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
221 $to = $ent unless $to;
222 my $call = $via ? $via : $to;
223 my $clref = DXCluster->get_exact($call);
224 my $dxchan = $clref->dxchan if $clref;
226 $dxchan->talk($self->{call}, $to, $via, $line);
228 $self->send($self->msg('disc2', $via ? $via : $to));
229 my @l = grep { $_ ne $ent } @{$self->{talklist}};
231 $self->{talklist} = \@l;
233 delete $self->{talklist};
234 $self->state('prompt');
243 for (@{$self->{talklist}}) {
244 my ($to, $via) = /(\S+)>(\S+)/;
248 return $self->msg('talkprompt', join(',', @call));
252 # send a load of stuff to a command user with page prompting
260 if ($self->{pagelth} && @_ > $self->{pagelth}) {
262 for ($i = $self->{pagelth}; $i-- > 0; ) {
264 $line =~ s/\s+$//o; # why am having to do this?
267 $self->{pagedata} = [ @_ ];
268 $self->state('page');
269 $self->send($self->msg('page', scalar @_));
272 $self->send($_) if $_;
277 # this is the thing that runs the command, it is done like this for the
278 # benefit of remote command execution
284 my $user = $self->{user};
285 my $call = $self->{call};
290 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
291 dbg('eval', "stored func cmd = $c\n");
294 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
298 return () if length $cmdline == 0;
301 $cmdline =~ s|//|/|og;
303 # split the command line up into parts, the first part is the command
304 my ($cmd, $args) = split /\s+/, $cmdline, 2;
305 $args = "" unless $args;
311 dbg('command', "cmd: $cmd");
313 # alias it if possible
314 my $acmd = CmdAlias::get_cmd($cmd);
316 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
317 $args = "" unless $args;
318 dbg('command', "aliased cmd: $cmd $args");
321 # first expand out the entry to a command
322 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
323 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
326 dbg('command', "path: $cmd cmd: $fcmd");
328 my $package = find_cmd_name($path, $fcmd);
329 @ans = (0) if !$package ;
332 dbg('command', "package: $package");
334 unless (exists $Cache{$package}->{'sub'}) {
335 $c = eval $Cache{$package}->{'eval'};
337 return DXDebug::shortmess($@);
339 $Cache{$package}->{'sub'} = $c;
341 $c = $Cache{$package}->{'sub'};
343 @ans = &{$c}($self, $args);
348 return (DXDebug::shortmess($@));
352 dbg('command', "cmd: $cmd not found");
353 return ($self->msg('e1'));
363 # This is called from inside the main cluster processing loop and is used
364 # for despatching commands that are doing some long processing job
369 my @dxchan = DXChannel->get_all();
372 foreach $dxchan (@dxchan) {
373 next if $dxchan->sort ne 'U';
375 # send a prompt if no activity out on this channel
376 if ($t >= $dxchan->t + $main::user_interval) {
377 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
384 # finish up a user context
390 my $call = $self->call;
392 # I was the last node visited
393 $self->user->node($main::mycall);
396 if ($conn && -e "$main::data/logout") {
397 open(I, "$main::data/logout") or confess;
400 $self->send_now('D', @in);
404 if ($call eq $main::myalias) { # unset the channel if it is us really
405 my $node = DXNode->get($main::mycall);
409 # issue a pc17 to everybody interested
410 my $nchan = DXChannel->get($main::mycall);
411 my $pc17 = $nchan->pc17($self);
412 DXProt::broadcast_all_ak1a($pc17);
414 # send info to all logged in thingies
415 $self->tell_login('logoutu');
417 Log('DXCommand', "$call disconnected");
418 my $ref = DXCluster->get_exact($call);
423 # short cut to output a prompt
429 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
432 # broadcast a message to all users [except those mentioned after buffer]
435 my $pkg = shift; # ignored
436 my $s = shift; # the line to be rebroadcast
437 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
438 my @list = DXChannel->get_all(); # just in case we are called from some funny object
439 my ($dxchan, $except);
441 L: foreach $dxchan (@list) {
442 next if !$dxchan->sort eq 'U'; # only interested in user channels
443 foreach $except (@except) {
444 next L if $except == $dxchan; # ignore channels in the 'except' list
446 $dxchan->send($s); # send it
450 # gimme all the users
453 my @list = DXChannel->get_all();
456 foreach $ref (@list) {
457 push @out, $ref if $ref->sort eq 'U';
462 # run a script for this user
466 my $silent = shift || 0;
471 # search for the command in the cache of short->long form commands
476 my ($path, $short_cmd, $suffix) = @_;
479 # commands are lower case
480 $short_cmd = lc $short_cmd;
481 dbg('command', "command: $path $short_cmd\n");
483 # do some checking for funny characters
484 return () if $short_cmd =~ /\/$/;
486 # return immediately if we have it
487 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
488 if ($apath && $acmd) {
489 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
490 return ($apath, $acmd);
494 my @parts = split '/', $short_cmd;
501 for ($i = 0; $i < @parts; $i++) {
503 opendir(D, $curdir) or confess "can't open $curdir $!";
507 foreach $l (sort @ls) {
509 if ($i < $#parts) { # we are dealing with directories
510 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
511 dbg('command', "got dir: $curdir/$l\n");
516 } else { # we are dealing with commands
517 @lparts = split /\./, $l;
518 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
519 if ($p eq substr($l, 0, length $p)) {
520 pop @lparts; # remove the suffix
521 $l = join '.', @lparts;
522 # chop $dirfn; # remove trailing /
523 $dirfn = "" unless $dirfn;
524 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
525 dbg('command', "got path: $path cmd: $dirfn$l\n");
526 return ($path, "$dirfn$l");
534 # clear the command name cache
541 # the persistant execution of things from the command directories
544 # This allows perl programs to call functions dynamically
546 # This has been nicked directly from the perlembed pages
549 #require Devel::Symdump;
551 sub valid_package_name {
553 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
555 #second pass only for words starting with a digit
556 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
558 #Dress it up as a real package name
559 $string =~ s/\//_/og;
563 # find a cmd reference
564 # this is really for use in user written stubs
566 # use the result as a symbolic reference:-
569 # @out = &$r($self, $line);
578 # first expand out the entry to a command
579 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
580 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
582 # make sure it is loaded
583 $r = find_cmd_name($path, $fcmd);
589 # this bit of magic finds a command in the offered directory
593 my $package = valid_package_name($cmdname);
594 my $filename = "$path/$cmdname.pl";
595 my $mtime = -M $filename;
597 # return if we can't find it
599 unless (defined $mtime) {
600 $errstr = DXM::msg('e1');
604 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
605 #we have compiled this subroutine already,
606 #it has not been updated on disk, nothing left to do
607 #print STDERR "already compiled $package->handler\n";
611 my $sub = readfilestr($filename);
613 $errstr = "Syserr: can't open '$filename' $!";
617 #wrap the code into a subroutine inside our unique package
618 my $eval = qq( sub { $sub } );
621 my @list = split /\n/, $eval;
624 dbg('eval', $_, "\n");
628 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
634 # send a talk message here
637 my ($self, $from, $to, $via, $line) = @_;
638 $line =~ s/\\5E/\^/g;
639 $self->send("$to de $from: $line") if $self->{talk};
640 Log('talk', $to, $from, $main::mycall, $line);