3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
23 use vars qw(%Cache %cmd_cache);
25 %Cache = (); # cache of dynamically loaded routine's mod times
26 %cmd_cache = (); # cache of short names
29 # obtain a new connection this is derived from dxchannel
34 my $self = DXChannel::alloc(@_);
35 $self->{sort} = 'U'; # in absence of how to find out what sort of an object I am
39 # this is how a a connection starts, you get a hello message and the motd with
40 # possibly some other messages asking you to set various things up if you are
41 # new (or nearly new and slacking) user.
45 my ($self, $line) = @_;
46 my $user = $self->{user};
47 my $call = $self->{call};
48 my $name = $user->{name};
50 $self->{name} = $name ? $name : $call;
51 $self->msg('l2',$self->{name});
52 $self->send_file($main::motd) if (-e $main::motd);
53 $self->msg('pr', $call);
54 $self->state('prompt'); # a bit of room for further expansion, passwords etc
55 $self->{priv} = $user->priv;
56 $self->{lang} = $user->lang;
57 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
58 $self->{consort} = $line; # save the connection type
60 # set some necessary flags on the user if they are connecting
61 $self->{wwv} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
62 $self->prompt() if $self->{state} =~ /^prompt/o;
64 # add yourself to the database
65 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
66 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
67 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
69 # issue a pc16 to everybody interested
70 my $nchan = DXChannel->get($main::mycall);
71 my @pc16 = DXProt::pc16($nchan, $cuser);
72 DXProt::broadcast_ak1a(@pc16);
76 # This is the normal command prompt driver
81 my $user = $self->{user};
82 my $call = $self->{call};
86 $cmdline =~ s|//|/|og;
88 # split the command line up into parts, the first part is the command
89 my ($cmd, $args) = $cmdline =~ /^([\w\/]+)\s*(.*)/o;
95 # first expand out the entry to a command
96 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
97 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
99 my @ans = $self->eval_file($path, $fcmd, $args) if $path && $fcmd;
100 # @ans = $self->eval_file($main::cmd, $cmd, $args) if !$ans[0];
103 $self->send(@ans) if @ans > 0;
107 $self->msg('e2', @ans);
116 # send a prompt only if we are in a prompt state
117 $self->prompt() if $self->{state} =~ /^prompt/o;
121 # This is called from inside the main cluster processing loop and is used
122 # for despatching commands that are doing some long processing job
127 my @chan = DXChannel->get_all();
130 foreach $chan (@chan) {
131 next if $chan->sort ne 'U';
133 # send a prompt if no activity out on this channel
134 if ($t >= $chan->t + $main::user_interval) {
135 $chan->prompt() if $chan->{state} =~ /^prompt/o;
142 # finish up a user context
147 my $call = $self->call;
149 if ($call eq $main::myalias) { # unset the channel if it is us really
150 my $node = DXNode->get($main::mycall);
153 my $ref = DXNodeuser->get($call);
155 # issue a pc17 to everybody interested
156 my $nchan = DXChannel->get($main::mycall);
157 my $pc17 = $nchan->pc17($self);
158 DXProt::broadcast_ak1a($pc17);
164 # short cut to output a prompt
170 my $call = $self->{call};
171 DXChannel::msg($self, 'pr', $call);
174 # broadcast a message to all users [except those mentioned after buffer]
177 my $pkg = shift; # ignored
178 my $s = shift; # the line to be rebroadcast
179 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
180 my @list = DXChannel->get_all(); # just in case we are called from some funny object
183 L: foreach $chan (@list) {
184 next if !$chan->sort eq 'U'; # only interested in user channels
185 foreach $except (@except) {
186 next L if $except == $chan; # ignore channels in the 'except' list
188 chan->send($s); # send it
192 # gimme all the users
195 my @list = DXChannel->get_all();
198 foreach $ref (@list) {
199 push @out, $ref if $ref->sort eq 'U';
205 # search for the command in the cache of short->long form commands
210 my ($path, $short_cmd, $suffix) = @_;
213 # commands are lower case
214 $short_cmd = lc $short_cmd;
215 dbg('command', "command: $path $short_cmd\n");
217 # return immediately if we have it
218 my ($apath, $acmd) = split ',', $cmd_cache{$short_cmd};
219 if ($apath && $acmd) {
220 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
221 return ($apath, $acmd);
225 my @parts = split '/', $short_cmd;
232 for ($i = 0; $i < @parts; $i++) {
234 opendir(D, $curdir) or confess "can't open $curdir $!";
238 foreach $l (sort @ls) {
240 if ($i < $#parts) { # we are dealing with directories
241 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
242 dbg('command', "got dir: $curdir/$l\n");
247 } else { # we are dealing with commands
248 @lparts = split /\./, $l;
249 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
250 if ($p eq substr($l, 0, length $p)) {
251 pop @lparts; # remove the suffix
252 $l = join '.', @lparts;
253 # chop $dirfn; # remove trailing /
254 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
255 dbg('command', "got path: $path cmd: $dirfn$l\n");
256 return ($path, "$dirfn$l");
264 # clear the command name cache
271 # the persistant execution of things from the command directories
274 # This allows perl programs to call functions dynamically
276 # This has been nicked directly from the perlembed pages
279 #require Devel::Symdump;
281 sub valid_package_name {
283 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
285 #second pass only for words starting with a digit
286 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
288 #Dress it up as a real package name
290 return "Emb_" . $string;
293 #borrowed from Safe.pm
299 $pkg = "DXChannel::$pkg\::"; # expand to full symbol table name
300 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
302 my $stem_symtab = *{$stem}{HASH};
304 delete $stem_symtab->{$leaf};
311 my $package = valid_package_name($cmdname);
312 my $filename = "$path/$cmdname.pl";
313 my $mtime = -M $filename;
315 # return if we can't find it
316 return (0, DXM::msg('e1')) if !defined $mtime;
318 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
319 #we have compiled this subroutine already,
320 #it has not been updated on disk, nothing left to do
321 #print STDERR "already compiled $package->handler\n";
325 if (!open FH, $filename) {
326 return (0, "Syserr: can't open '$filename' $!");
332 #wrap the code into a subroutine inside our unique package
333 my $eval = qq{package DXChannel; sub $package { $sub; }};
335 my @list = split /\n/, $eval;
338 dbg('eval', $_, "\n");
341 #print "eval $eval\n";
343 #hide our variables within this block
344 my($filename,$mtime,$package,$sub);
348 delete_package($package);
349 return (1, "Syserr: Eval err $@ on $package");
352 #cache it unless we're cleaning out each time
353 $Cache{$package}{mtime} = $mtime;
357 my $c = qq{ \@r = \$self->$package(\@_); };
358 dbg('eval', "cluster cmd = $c\n");
361 delete_package($package);
362 return (1, "Syserr: Eval err $@ on cached $package");
365 #take a look if you want
366 #print Devel::Symdump->rnew($package)->as_string, $/;