3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
28 use vars qw(%Cache %cmd_cache $errstr %aliases);
30 %Cache = (); # cache of dynamically loaded routine's mod times
31 %cmd_cache = (); # cache of short names
32 $errstr = (); # error string from eval
33 %aliases = (); # aliases for (parts of) commands
36 # obtain a new connection this is derived from dxchannel
41 my $self = DXChannel::alloc(@_);
42 $self->{'sort'} = 'U'; # in absence of how to find out what sort of an object I am
46 # this is how a a connection starts, you get a hello message and the motd with
47 # possibly some other messages asking you to set various things up if you are
48 # new (or nearly new and slacking) user.
52 my ($self, $line, $sort) = @_;
53 my $user = $self->{user};
54 my $call = $self->{call};
55 my $name = $user->{name};
57 $self->{name} = $name ? $name : $call;
58 $self->send($self->msg('l2',$self->{name}));
59 $self->send_file($main::motd) if (-e $main::motd);
60 $self->state('prompt'); # a bit of room for further expansion, passwords etc
61 $self->{priv} = $user->priv;
62 $self->{lang} = $user->lang;
63 $self->{pagelth} = 20;
64 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
65 $self->{consort} = $line; # save the connection type
67 # set some necessary flags on the user if they are connecting
68 $self->{beep} = $self->{wwv} = $self->{wx} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
69 # $self->prompt() if $self->{state} =~ /^prompt/o;
71 # add yourself to the database
72 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
73 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
74 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
76 # issue a pc16 to everybody interested
77 my $nchan = DXChannel->get($main::mycall);
78 my @pc16 = DXProt::pc16($nchan, $cuser);
79 DXProt::broadcast_ak1a(@pc16);
80 Log('DXCommand', "$call connected");
82 # send prompts and things
83 my $info = DXCluster::cluster();
84 $self->send("Cluster:$info");
85 $self->send($self->msg('namee1')) if !$user->name;
86 $self->send($self->msg('qthe1')) if !$user->qth;
87 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
88 $self->send($self->msg('hnodee1')) if !$user->qth;
89 $self->send($self->msg('msgnew')) if DXMsg::for_me($call);
91 $self->send($self->msg('pr', $call));
95 # This is the normal command prompt driver
104 # remove leading and trailing spaces
105 $cmdline =~ s/^\s*(.*)\s*$/$1/;
107 if ($self->{state} eq 'page') {
108 my $i = $self->{pagelth};
109 my $ref = $self->{pagedata};
112 # abort if we get a line starting in with a
113 if ($cmdline =~ /^a/io) {
118 # send a tranche of data
119 while ($i-- > 0 && @$ref) {
120 my $line = shift @$ref;
121 $line =~ s/\s+$//o; # why am having to do this?
125 # reset state if none or else chuck out an intermediate prompt
127 $tot -= $self->{pagelth};
128 $self->send($self->msg('page', $tot));
130 $self->state('prompt');
133 @ans = run_cmd($self, $cmdline); # if length $cmdline;
135 if ($self->{pagelth} && @ans > $self->{pagelth}) {
137 for ($i = $self->{pagelth}; $i-- > 0; ) {
138 my $line = shift @ans;
139 $line =~ s/\s+$//o; # why am having to do this?
142 $self->{pagedata} = \@ans;
143 $self->state('page');
144 $self->send($self->msg('page', scalar @ans));
147 s/\s+$//o; # why ?????????
153 # send a prompt only if we are in a prompt state
154 $self->prompt() if $self->{state} =~ /^prompt/o;
158 # this is the thing that runs the command, it is done like this for the
159 # benefit of remote command execution
165 my $user = $self->{user};
166 my $call = $self->{call};
171 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
172 dbg('eval', "stored func cmd = $c\n");
175 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
179 return () if length $cmdline == 0;
182 $cmdline =~ s|//|/|og;
184 # split the command line up into parts, the first part is the command
185 my ($cmd, $args) = $cmdline =~ /^([\S\/]+)\s*(.*)/o;
191 dbg('command', "cmd: $cmd");
193 # alias it if possible
194 my $acmd = CmdAlias::get_cmd($cmd);
196 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
197 dbg('command', "aliased cmd: $cmd $args");
200 # first expand out the entry to a command
201 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
202 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
205 dbg('command', "path: $cmd cmd: $fcmd");
207 my $package = find_cmd_name($path, $fcmd);
208 @ans = (0) if !$package ;
211 dbg('command', "package: $package");
213 my $c = qq{ \@ans = $package(\$self, \$args) };
214 dbg('eval', "cluster cmd = $c\n");
217 @ans = (0, "Syserr: Eval err cached $package\n$@");
221 dbg('command', "cmd: $cmd not found");
232 unshift @ans, $self->msg('e2');
234 @ans = $self->msg('e1');
241 # This is called from inside the main cluster processing loop and is used
242 # for despatching commands that are doing some long processing job
247 my @dxchan = DXChannel->get_all();
250 foreach $dxchan (@dxchan) {
251 next if $dxchan->sort ne 'U';
253 # send a prompt if no activity out on this channel
254 if ($t >= $dxchan->t + $main::user_interval) {
255 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
262 # finish up a user context
267 my $call = $self->call;
270 if (-e "$main::data/logout") {
271 open(I, "$main::data/logout") or confess;
274 $self->send_now('D', @in);
278 if ($call eq $main::myalias) { # unset the channel if it is us really
279 my $node = DXNode->get($main::mycall);
282 my $ref = DXCluster->get_exact($call);
284 # issue a pc17 to everybody interested
285 my $nchan = DXChannel->get($main::mycall);
286 my $pc17 = $nchan->pc17($self);
287 DXProt::broadcast_ak1a($pc17);
289 Log('DXCommand', "$call disconnected");
294 # short cut to output a prompt
300 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
303 # broadcast a message to all users [except those mentioned after buffer]
306 my $pkg = shift; # ignored
307 my $s = shift; # the line to be rebroadcast
308 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
309 my @list = DXChannel->get_all(); # just in case we are called from some funny object
310 my ($dxchan, $except);
312 L: foreach $dxchan (@list) {
313 next if !$dxchan->sort eq 'U'; # only interested in user channels
314 foreach $except (@except) {
315 next L if $except == $dxchan; # ignore channels in the 'except' list
317 $dxchan->send($s); # send it
321 # gimme all the users
324 my @list = DXChannel->get_all();
327 foreach $ref (@list) {
328 push @out, $ref if $ref->sort eq 'U';
334 # search for the command in the cache of short->long form commands
339 my ($path, $short_cmd, $suffix) = @_;
342 # commands are lower case
343 $short_cmd = lc $short_cmd;
344 dbg('command', "command: $path $short_cmd\n");
346 # do some checking for funny characters
347 return () if $short_cmd =~ /\/$/;
349 # return immediately if we have it
350 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
351 if ($apath && $acmd) {
352 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
353 return ($apath, $acmd);
357 my @parts = split '/', $short_cmd;
364 for ($i = 0; $i < @parts; $i++) {
366 opendir(D, $curdir) or confess "can't open $curdir $!";
370 foreach $l (sort @ls) {
372 if ($i < $#parts) { # we are dealing with directories
373 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
374 dbg('command', "got dir: $curdir/$l\n");
379 } else { # we are dealing with commands
380 @lparts = split /\./, $l;
381 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
382 if ($p eq substr($l, 0, length $p)) {
383 pop @lparts; # remove the suffix
384 $l = join '.', @lparts;
385 # chop $dirfn; # remove trailing /
386 $dirfn = "" unless $dirfn;
387 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
388 dbg('command', "got path: $path cmd: $dirfn$l\n");
389 return ($path, "$dirfn$l");
397 # clear the command name cache
404 # the persistant execution of things from the command directories
407 # This allows perl programs to call functions dynamically
409 # This has been nicked directly from the perlembed pages
412 #require Devel::Symdump;
414 sub valid_package_name {
416 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
418 #second pass only for words starting with a digit
419 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
421 #Dress it up as a real package name
422 $string =~ s/\//_/og;
423 return "Emb_" . $string;
426 #borrowed from Safe.pm
432 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
433 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
435 if ($stem && $leaf) {
436 my $stem_symtab = *{$stem}{HASH};
437 delete $stem_symtab->{$leaf};
441 # find a cmd reference
442 # this is really for use in user written stubs
444 # use the result as a symbolic reference:-
447 # @out = &$r($self, $line);
456 # first expand out the entry to a command
457 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
458 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
460 # make sure it is loaded
461 $r = find_cmd_name($path, $fcmd);
467 # this bit of magic finds a command in the offered directory
471 my $package = valid_package_name($cmdname);
472 my $filename = "$path/$cmdname.pl";
473 my $mtime = -M $filename;
475 # return if we can't find it
477 unless (defined $mtime) {
478 $errstr = DXM::msg('e1');
482 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
483 #we have compiled this subroutine already,
484 #it has not been updated on disk, nothing left to do
485 #print STDERR "already compiled $package->handler\n";
488 delete_package($package) if defined $Cache{$package}{mtime};
490 my $fh = new FileHandle;
491 if (!open $fh, $filename) {
492 $errstr = "Syserr: can't open '$filename' $!";
499 #wrap the code into a subroutine inside our unique package
500 my $eval = qq{ sub $package { $sub } };
503 my @list = split /\n/, $eval;
506 dbg('eval', $_, "\n");
511 #hide our variables within this block
512 my($filename,$mtime,$package,$sub);
519 delete_package($package);
521 #cache it unless we're cleaning out each time
522 $Cache{$package}{'mtime'} = $mtime;
526 #print Devel::Symdump->rnew($package)->as_string, $/;
527 $package = "DXCommandmode::$package" if $package;
528 $package = undef if $errstr;