3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
33 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
35 %Cache = (); # cache of dynamically loaded routine's mod times
36 %cmd_cache = (); # cache of short names
37 $errstr = (); # error string from eval
38 %aliases = (); # aliases for (parts of) commands
39 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
42 # obtain a new connection this is derived from dxchannel
47 my $self = DXChannel::alloc(@_);
48 $self->{'sort'} = 'U'; # in absence of how to find out what sort of an object I am
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;
68 $self->{lang} = $user->lang;
69 $self->{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->{talk} = $user->wanttalk;
78 $self->{wx} = $user->wantwx;
79 $self->{dx} = $user->wantdx;
80 $self->{logininfo} = $user->wantlogininfo;
83 # add yourself to the database
84 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
85 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
86 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
88 # issue a pc16 to everybody interested
89 my $nchan = DXChannel->get($main::mycall);
90 my @pc16 = DXProt::pc16($nchan, $cuser);
92 DXProt::broadcast_all_ak1a($_);
94 Log('DXCommand', "$call connected");
96 # send prompts and things
97 my $info = DXCluster::cluster();
98 $self->send("Cluster:$info");
99 $self->send($self->msg('namee1')) if !$user->name;
100 $self->send($self->msg('qthe1')) if !$user->qth;
101 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
102 $self->send($self->msg('hnodee1')) if !$user->qth;
103 $self->send($self->msg('m9')) if DXMsg::for_me($call);
104 $self->send($self->msg('pr', $call));
106 $self->tell_login('loginu');
111 # This is the normal command prompt driver
120 # remove leading and trailing spaces
121 $cmdline =~ s/^\s*(.*)\s*$/$1/;
123 if ($self->{state} eq 'page') {
124 my $i = $self->{pagelth};
125 my $ref = $self->{pagedata};
128 # abort if we get a line starting in with a
129 if ($cmdline =~ /^a/io) {
134 # send a tranche of data
135 while ($i-- > 0 && @$ref) {
136 my $line = shift @$ref;
137 $line =~ s/\s+$//o; # why am having to do this?
141 # reset state if none or else chuck out an intermediate prompt
143 $tot -= $self->{pagelth};
144 $self->send($self->msg('page', $tot));
146 $self->state('prompt');
148 } elsif ($self->{state} eq 'sysop') {
149 my $passwd = $self->{user}->passwd;
150 my @pw = split / */, $passwd;
152 my @l = @{$self->{passwd}};
153 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
154 if ($cmdline =~ /$str/) {
155 $self->{priv} = $self->{user}->priv;
157 $self->send($self->msg('sorry'));
160 $self->send($self->msg('sorry'));
162 delete $self->{passwd};
163 $self->state('prompt');
165 @ans = run_cmd($self, $cmdline); # if length $cmdline;
167 if ($self->{pagelth} && @ans > $self->{pagelth}) {
169 for ($i = $self->{pagelth}; $i-- > 0; ) {
170 my $line = shift @ans;
171 $line =~ s/\s+$//o; # why am having to do this?
174 $self->{pagedata} = \@ans;
175 $self->state('page');
176 $self->send($self->msg('page', scalar @ans));
179 $self->send($_) if $_;
184 # send a prompt only if we are in a prompt state
185 $self->prompt() if $self->{state} =~ /^prompt/o;
189 # this is the thing that runs the command, it is done like this for the
190 # benefit of remote command execution
196 my $user = $self->{user};
197 my $call = $self->{call};
202 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
203 dbg('eval', "stored func cmd = $c\n");
206 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
210 return () if length $cmdline == 0;
213 $cmdline =~ s|//|/|og;
215 # split the command line up into parts, the first part is the command
216 my ($cmd, $args) = split /\s+/, $cmdline, 2;
217 $args = "" unless $args;
223 dbg('command', "cmd: $cmd");
225 # alias it if possible
226 my $acmd = CmdAlias::get_cmd($cmd);
228 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
229 $args = "" unless $args;
230 dbg('command', "aliased cmd: $cmd $args");
233 # first expand out the entry to a command
234 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
235 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
238 dbg('command', "path: $cmd cmd: $fcmd");
240 my $package = find_cmd_name($path, $fcmd);
241 @ans = (0) if !$package ;
244 dbg('command', "package: $package");
246 unless (exists $Cache{$package}->{'sub'}) {
247 $c = eval $Cache{$package}->{'eval'};
249 return ("Syserr: Syntax error in $package", $@);
251 $Cache{$package}->{'sub'} = $c;
253 $c = $Cache{$package}->{'sub'};
255 @ans = &{$c}($self, $args);
261 dbg('command', "cmd: $cmd not found");
262 return ($self->msg('e1'));
272 # This is called from inside the main cluster processing loop and is used
273 # for despatching commands that are doing some long processing job
278 my @dxchan = DXChannel->get_all();
281 foreach $dxchan (@dxchan) {
282 next if $dxchan->sort ne 'U';
284 # send a prompt if no activity out on this channel
285 if ($t >= $dxchan->t + $main::user_interval) {
286 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
293 # finish up a user context
298 my $call = $self->call;
301 if (-e "$main::data/logout") {
302 open(I, "$main::data/logout") or confess;
305 $self->send_now('D', @in);
309 if ($call eq $main::myalias) { # unset the channel if it is us really
310 my $node = DXNode->get($main::mycall);
314 # issue a pc17 to everybody interested
315 my $nchan = DXChannel->get($main::mycall);
316 my $pc17 = $nchan->pc17($self);
317 DXProt::broadcast_all_ak1a($pc17);
319 # send info to all logged in thingies
320 $self->tell_login('logoutu');
322 Log('DXCommand', "$call disconnected");
323 my $ref = DXCluster->get_exact($call);
328 # short cut to output a prompt
334 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
337 # broadcast a message to all users [except those mentioned after buffer]
340 my $pkg = shift; # ignored
341 my $s = shift; # the line to be rebroadcast
342 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
343 my @list = DXChannel->get_all(); # just in case we are called from some funny object
344 my ($dxchan, $except);
346 L: foreach $dxchan (@list) {
347 next if !$dxchan->sort eq 'U'; # only interested in user channels
348 foreach $except (@except) {
349 next L if $except == $dxchan; # ignore channels in the 'except' list
351 $dxchan->send($s); # send it
355 # gimme all the users
358 my @list = DXChannel->get_all();
361 foreach $ref (@list) {
362 push @out, $ref if $ref->sort eq 'U';
367 # run a script for this user
371 my $silent = shift || 0;
376 # search for the command in the cache of short->long form commands
381 my ($path, $short_cmd, $suffix) = @_;
384 # commands are lower case
385 $short_cmd = lc $short_cmd;
386 dbg('command', "command: $path $short_cmd\n");
388 # do some checking for funny characters
389 return () if $short_cmd =~ /\/$/;
391 # return immediately if we have it
392 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
393 if ($apath && $acmd) {
394 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
395 return ($apath, $acmd);
399 my @parts = split '/', $short_cmd;
406 for ($i = 0; $i < @parts; $i++) {
408 opendir(D, $curdir) or confess "can't open $curdir $!";
412 foreach $l (sort @ls) {
414 if ($i < $#parts) { # we are dealing with directories
415 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
416 dbg('command', "got dir: $curdir/$l\n");
421 } else { # we are dealing with commands
422 @lparts = split /\./, $l;
423 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
424 if ($p eq substr($l, 0, length $p)) {
425 pop @lparts; # remove the suffix
426 $l = join '.', @lparts;
427 # chop $dirfn; # remove trailing /
428 $dirfn = "" unless $dirfn;
429 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
430 dbg('command', "got path: $path cmd: $dirfn$l\n");
431 return ($path, "$dirfn$l");
439 # clear the command name cache
446 # the persistant execution of things from the command directories
449 # This allows perl programs to call functions dynamically
451 # This has been nicked directly from the perlembed pages
454 #require Devel::Symdump;
456 sub valid_package_name {
458 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
460 #second pass only for words starting with a digit
461 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
463 #Dress it up as a real package name
464 $string =~ s/\//_/og;
468 # find a cmd reference
469 # this is really for use in user written stubs
471 # use the result as a symbolic reference:-
474 # @out = &$r($self, $line);
483 # first expand out the entry to a command
484 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
485 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
487 # make sure it is loaded
488 $r = find_cmd_name($path, $fcmd);
494 # this bit of magic finds a command in the offered directory
498 my $package = valid_package_name($cmdname);
499 my $filename = "$path/$cmdname.pl";
500 my $mtime = -M $filename;
502 # return if we can't find it
504 unless (defined $mtime) {
505 $errstr = DXM::msg('e1');
509 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
510 #we have compiled this subroutine already,
511 #it has not been updated on disk, nothing left to do
512 #print STDERR "already compiled $package->handler\n";
516 my $sub = readfilestr($filename);
518 $errstr = "Syserr: can't open '$filename' $!";
522 #wrap the code into a subroutine inside our unique package
523 my $eval = qq( sub { $sub } );
526 my @list = split /\n/, $eval;
529 dbg('eval', $_, "\n");
533 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };