3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
32 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
34 %Cache = (); # cache of dynamically loaded routine's mod times
35 %cmd_cache = (); # cache of short names
36 $errstr = (); # error string from eval
37 %aliases = (); # aliases for (parts of) commands
38 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
41 # obtain a new connection this is derived from dxchannel
46 my $self = DXChannel::alloc(@_);
47 $self->{'sort'} = 'U'; # in absence of how to find out what sort of an object I am
51 # this is how a a connection starts, you get a hello message and the motd with
52 # possibly some other messages asking you to set various things up if you are
53 # new (or nearly new and slacking) user.
57 my ($self, $line, $sort) = @_;
58 my $user = $self->{user};
59 my $call = $self->{call};
60 my $name = $user->{name};
62 $self->{name} = $name ? $name : $call;
63 $self->send($self->msg('l2',$self->{name}));
64 $self->send_file($main::motd) if (-e $main::motd);
65 $self->state('prompt'); # a bit of room for further expansion, passwords etc
66 $self->{priv} = $user->priv;
67 $self->{lang} = $user->lang;
68 $self->{pagelth} = 20;
69 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
70 $self->{consort} = $line; # save the connection type
72 # set some necessary flags on the user if they are connecting
73 $self->{beep} = $self->{wwv} = $self->{wx} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
74 # $self->prompt() if $self->{state} =~ /^prompt/o;
76 # add yourself to the database
77 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
78 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
79 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
81 # issue a pc16 to everybody interested
82 my $nchan = DXChannel->get($main::mycall);
83 my @pc16 = DXProt::pc16($nchan, $cuser);
85 DXProt::broadcast_all_ak1a($_);
87 Log('DXCommand', "$call connected");
89 # send prompts and things
90 my $info = DXCluster::cluster();
91 $self->send("Cluster:$info");
92 $self->send($self->msg('namee1')) if !$user->name;
93 $self->send($self->msg('qthe1')) if !$user->qth;
94 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
95 $self->send($self->msg('hnodee1')) if !$user->qth;
96 $self->send($self->msg('m9')) if DXMsg::for_me($call);
99 $self->send($self->msg('pr', $call));
103 # This is the normal command prompt driver
112 # remove leading and trailing spaces
113 $cmdline =~ s/^\s*(.*)\s*$/$1/;
115 if ($self->{state} eq 'page') {
116 my $i = $self->{pagelth};
117 my $ref = $self->{pagedata};
120 # abort if we get a line starting in with a
121 if ($cmdline =~ /^a/io) {
126 # send a tranche of data
127 while ($i-- > 0 && @$ref) {
128 my $line = shift @$ref;
129 $line =~ s/\s+$//o; # why am having to do this?
133 # reset state if none or else chuck out an intermediate prompt
135 $tot -= $self->{pagelth};
136 $self->send($self->msg('page', $tot));
138 $self->state('prompt');
140 } elsif ($self->{state} eq 'sysop') {
141 my $passwd = $self->{user}->passwd;
142 my @pw = split / */, $passwd;
144 my @l = @{$self->{passwd}};
145 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
146 if ($cmdline =~ /$str/) {
147 $self->{priv} = $self->{user}->priv;
149 $self->send($self->msg('sorry'));
152 $self->send($self->msg('sorry'));
154 delete $self->{passwd};
155 $self->state('prompt');
157 @ans = run_cmd($self, $cmdline); # if length $cmdline;
159 if ($self->{pagelth} && @ans > $self->{pagelth}) {
161 for ($i = $self->{pagelth}; $i-- > 0; ) {
162 my $line = shift @ans;
163 $line =~ s/\s+$//o; # why am having to do this?
166 $self->{pagedata} = \@ans;
167 $self->state('page');
168 $self->send($self->msg('page', scalar @ans));
171 $self->send($_) if $_;
176 # send a prompt only if we are in a prompt state
177 $self->prompt() if $self->{state} =~ /^prompt/o;
181 # this is the thing that runs the command, it is done like this for the
182 # benefit of remote command execution
188 my $user = $self->{user};
189 my $call = $self->{call};
194 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
195 dbg('eval', "stored func cmd = $c\n");
198 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
202 return () if length $cmdline == 0;
205 $cmdline =~ s|//|/|og;
207 # split the command line up into parts, the first part is the command
208 my ($cmd, $args) = split /\s+/, $cmdline, 2;
209 $args = "" unless $args;
215 dbg('command', "cmd: $cmd");
217 # alias it if possible
218 my $acmd = CmdAlias::get_cmd($cmd);
220 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
221 $args = "" unless $args;
222 dbg('command', "aliased cmd: $cmd $args");
225 # first expand out the entry to a command
226 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
227 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
230 dbg('command', "path: $cmd cmd: $fcmd");
232 my $package = find_cmd_name($path, $fcmd);
233 @ans = (0) if !$package ;
236 dbg('command', "package: $package");
238 unless (exists $Cache{$package}->{sub}) {
239 $c = eval $Cache{$package}->{eval};
241 return ("Syserr: Syntax error in $package", $@);
243 $Cache{$package}->{sub} = $c;
245 $c = $Cache{$package}->{sub};
247 @ans = &{$c}($self, $args);
253 dbg('command', "cmd: $cmd not found");
254 return ($self->msg('e1'));
264 # This is called from inside the main cluster processing loop and is used
265 # for despatching commands that are doing some long processing job
270 my @dxchan = DXChannel->get_all();
273 foreach $dxchan (@dxchan) {
274 next if $dxchan->sort ne 'U';
276 # send a prompt if no activity out on this channel
277 if ($t >= $dxchan->t + $main::user_interval) {
278 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
285 # finish up a user context
290 my $call = $self->call;
293 if (-e "$main::data/logout") {
294 open(I, "$main::data/logout") or confess;
297 $self->send_now('D', @in);
301 if ($call eq $main::myalias) { # unset the channel if it is us really
302 my $node = DXNode->get($main::mycall);
305 my $ref = DXCluster->get_exact($call);
307 # issue a pc17 to everybody interested
308 my $nchan = DXChannel->get($main::mycall);
309 my $pc17 = $nchan->pc17($self);
310 DXProt::broadcast_all_ak1a($pc17);
312 Log('DXCommand', "$call disconnected");
317 # short cut to output a prompt
323 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
326 # broadcast a message to all users [except those mentioned after buffer]
329 my $pkg = shift; # ignored
330 my $s = shift; # the line to be rebroadcast
331 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
332 my @list = DXChannel->get_all(); # just in case we are called from some funny object
333 my ($dxchan, $except);
335 L: foreach $dxchan (@list) {
336 next if !$dxchan->sort eq 'U'; # only interested in user channels
337 foreach $except (@except) {
338 next L if $except == $dxchan; # ignore channels in the 'except' list
340 $dxchan->send($s); # send it
344 # gimme all the users
347 my @list = DXChannel->get_all();
350 foreach $ref (@list) {
351 push @out, $ref if $ref->sort eq 'U';
356 # run a script for this user
360 my $silent = shift || 0;
365 # search for the command in the cache of short->long form commands
370 my ($path, $short_cmd, $suffix) = @_;
373 # commands are lower case
374 $short_cmd = lc $short_cmd;
375 dbg('command', "command: $path $short_cmd\n");
377 # do some checking for funny characters
378 return () if $short_cmd =~ /\/$/;
380 # return immediately if we have it
381 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
382 if ($apath && $acmd) {
383 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
384 return ($apath, $acmd);
388 my @parts = split '/', $short_cmd;
395 for ($i = 0; $i < @parts; $i++) {
397 opendir(D, $curdir) or confess "can't open $curdir $!";
401 foreach $l (sort @ls) {
403 if ($i < $#parts) { # we are dealing with directories
404 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
405 dbg('command', "got dir: $curdir/$l\n");
410 } else { # we are dealing with commands
411 @lparts = split /\./, $l;
412 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
413 if ($p eq substr($l, 0, length $p)) {
414 pop @lparts; # remove the suffix
415 $l = join '.', @lparts;
416 # chop $dirfn; # remove trailing /
417 $dirfn = "" unless $dirfn;
418 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
419 dbg('command', "got path: $path cmd: $dirfn$l\n");
420 return ($path, "$dirfn$l");
428 # clear the command name cache
435 # the persistant execution of things from the command directories
438 # This allows perl programs to call functions dynamically
440 # This has been nicked directly from the perlembed pages
443 #require Devel::Symdump;
445 sub valid_package_name {
447 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
449 #second pass only for words starting with a digit
450 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
452 #Dress it up as a real package name
453 $string =~ s/\//_/og;
457 # find a cmd reference
458 # this is really for use in user written stubs
460 # use the result as a symbolic reference:-
463 # @out = &$r($self, $line);
472 # first expand out the entry to a command
473 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
474 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
476 # make sure it is loaded
477 $r = find_cmd_name($path, $fcmd);
483 # this bit of magic finds a command in the offered directory
487 my $package = valid_package_name($cmdname);
488 my $filename = "$path/$cmdname.pl";
489 my $mtime = -M $filename;
491 # return if we can't find it
493 unless (defined $mtime) {
494 $errstr = DXM::msg('e1');
498 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
499 #we have compiled this subroutine already,
500 #it has not been updated on disk, nothing left to do
501 #print STDERR "already compiled $package->handler\n";
505 my $sub = readfilestr($filename);
507 $errstr = "Syserr: can't open '$filename' $!";
511 #wrap the code into a subroutine inside our unique package
512 my $eval = qq( sub { $sub } );
515 my @list = split /\n/, $eval;
518 dbg('eval', $_, "\n");
522 $Cache{$package} = {mtime => $mtime, eval => $eval };