3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
26 use vars qw(%Cache %cmd_cache $errstr %aliases);
28 %Cache = (); # cache of dynamically loaded routine's mod times
29 %cmd_cache = (); # cache of short names
30 $errstr = (); # error string from eval
31 %aliases = (); # aliases for (parts of) commands
34 # obtain a new connection this is derived from dxchannel
39 my $self = DXChannel::alloc(@_);
40 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
44 # this is how a a connection starts, you get a hello message and the motd with
45 # possibly some other messages asking you to set various things up if you are
46 # new (or nearly new and slacking) user.
50 my ($self, $line, $sort) = @_;
51 my $user = $self->{user};
52 my $call = $self->{call};
53 my $name = $user->{name};
55 $self->{name} = $name ? $name : $call;
56 $self->send($self->msg('l2',$self->{name}));
57 $self->send_file($main::motd) if (-e $main::motd);
58 $self->send($self->msg('pr', $call));
59 $self->state('prompt'); # a bit of room for further expansion, passwords etc
60 $self->{priv} = $user->priv;
61 $self->{lang} = $user->lang;
62 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
63 $self->{consort} = $line; # save the connection type
65 # set some necessary flags on the user if they are connecting
66 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
67 # $self->prompt() if $self->{state} =~ /^prompt/o;
69 # add yourself to the database
70 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
71 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
72 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
74 # issue a pc16 to everybody interested
75 my $nchan = DXChannel->get($main::mycall);
76 my @pc16 = DXProt::pc16($nchan, $cuser);
77 DXProt::broadcast_ak1a(@pc16);
78 Log('DXCommand', "$call connected");
82 # This is the normal command prompt driver
90 my @ans = run_cmd($self, $cmdline);
91 $self->send(@ans) if @ans > 0;
93 # send a prompt only if we are in a prompt state
94 $self->prompt() if $self->{state} =~ /^prompt/o;
98 # this is the thing that runs the command, it is done like this for the
99 # benefit of remote command execution
105 my $user = $self->{user};
106 my $call = $self->{call};
110 # are we in stored state?
112 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
113 dbg('eval', "stored func cmd = $c\n");
116 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
120 # special case only \n input => " "
121 if ($cmdline eq " ") {
127 $cmdline =~ s|//|/|og;
129 # split the command line up into parts, the first part is the command
130 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
136 # alias it if possible
137 my $acmd = CmdAlias::get_cmd($cmd);
139 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
142 # first expand out the entry to a command
143 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
144 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
146 my $package = find_cmd_name($path, $fcmd);
147 @ans = (0) if !$package ;
150 my $c = qq{ \@ans = $package(\$self, \$args) };
151 dbg('eval', "cluster cmd = $c\n");
154 @ans = (0, "Syserr: Eval err cached $package\n$@");
165 unshift @ans, $self->msg('e2');
167 @ans = $self->msg('e1');
174 # This is called from inside the main cluster processing loop and is used
175 # for despatching commands that are doing some long processing job
180 my @chan = DXChannel->get_all();
183 foreach $chan (@chan) {
184 next if $chan->sort ne 'U';
186 # send a prompt if no activity out on this channel
187 if ($t >= $chan->t + $main::user_interval) {
188 $chan->prompt() if $chan->{state} =~ /^prompt/o;
195 # finish up a user context
200 my $call = $self->call;
202 if ($call eq $main::myalias) { # unset the channel if it is us really
203 my $node = DXNode->get($main::mycall);
206 my $ref = DXNodeuser->get($call);
208 # issue a pc17 to everybody interested
209 my $nchan = DXChannel->get($main::mycall);
210 my $pc17 = $nchan->pc17($self);
211 DXProt::broadcast_ak1a($pc17);
213 Log('DXCommand', "$call disconnected");
218 # short cut to output a prompt
224 my $call = $self->{call};
225 $self->send($self->msg('pr', $call));
226 #DXChannel::msg($self, 'pr', $call);
229 # broadcast a message to all users [except those mentioned after buffer]
232 my $pkg = shift; # ignored
233 my $s = shift; # the line to be rebroadcast
234 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
235 my @list = DXChannel->get_all(); # just in case we are called from some funny object
238 L: foreach $chan (@list) {
239 next if !$chan->sort eq 'U'; # only interested in user channels
240 foreach $except (@except) {
241 next L if $except == $chan; # ignore channels in the 'except' list
243 chan->send($s); # send it
247 # gimme all the users
250 my @list = DXChannel->get_all();
253 foreach $ref (@list) {
254 push @out, $ref if $ref->sort eq 'U';
260 # search for the command in the cache of short->long form commands
265 my ($path, $short_cmd, $suffix) = @_;
268 # commands are lower case
269 $short_cmd = lc $short_cmd;
270 dbg('command', "command: $path $short_cmd\n");
272 # return immediately if we have it
273 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
274 if ($apath && $acmd) {
275 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
276 return ($apath, $acmd);
280 my @parts = split '/', $short_cmd;
287 for ($i = 0; $i < @parts; $i++) {
289 opendir(D, $curdir) or confess "can't open $curdir $!";
293 foreach $l (sort @ls) {
295 if ($i < $#parts) { # we are dealing with directories
296 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
297 dbg('command', "got dir: $curdir/$l\n");
302 } else { # we are dealing with commands
303 @lparts = split /\./, $l;
304 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
305 if ($p eq substr($l, 0, length $p)) {
306 pop @lparts; # remove the suffix
307 $l = join '.', @lparts;
308 # chop $dirfn; # remove trailing /
309 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
310 dbg('command', "got path: $path cmd: $dirfn$l\n");
311 return ($path, "$dirfn$l");
319 # clear the command name cache
326 # the persistant execution of things from the command directories
329 # This allows perl programs to call functions dynamically
331 # This has been nicked directly from the perlembed pages
334 #require Devel::Symdump;
336 sub valid_package_name {
338 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
340 #second pass only for words starting with a digit
341 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
343 #Dress it up as a real package name
344 $string =~ s/\//_/og;
345 return "Emb_" . $string;
348 #borrowed from Safe.pm
354 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
355 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
357 if ($stem && $leaf) {
358 my $stem_symtab = *{$stem}{HASH};
359 delete $stem_symtab->{$leaf};
363 # find a cmd reference
364 # this is really for use in user written stubs
366 # use the result as a symbolic reference:-
369 # @out = &$r($self, $line);
378 # first expand out the entry to a command
379 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
380 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
382 # make sure it is loaded
383 $r = find_cmd_name($path, $fcmd);
389 # this bit of magic finds a command in the offered directory
393 my $package = valid_package_name($cmdname);
394 my $filename = "$path/$cmdname.pl";
395 my $mtime = -M $filename;
397 # return if we can't find it
400 $errstr = DXM::msg('e1');
404 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
405 #we have compiled this subroutine already,
406 #it has not been updated on disk, nothing left to do
407 #print STDERR "already compiled $package->handler\n";
410 my $fh = new FileHandle;
411 if (!open $fh, $filename) {
412 $errstr = "Syserr: can't open '$filename' $!";
419 #wrap the code into a subroutine inside our unique package
427 my @list = split /\n/, $eval;
430 dbg('eval', $_, "\n");
435 #hide our variables within this block
436 my($filename,$mtime,$package,$sub);
443 delete_package($package);
445 #cache it unless we're cleaning out each time
446 $Cache{$package}{mtime} = $mtime;
450 #print Devel::Symdump->rnew($package)->as_string, $/;
451 $package = "DXCommandmode::$package" if $package;
452 $package = undef if $errstr;