3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
29 use vars qw(%Cache %cmd_cache $errstr %aliases);
31 %Cache = (); # cache of dynamically loaded routine's mod times
32 %cmd_cache = (); # cache of short names
33 $errstr = (); # error string from eval
34 %aliases = (); # aliases for (parts of) commands
37 # obtain a new connection this is derived from dxchannel
42 my $self = DXChannel::alloc(@_);
43 $self->{'sort'} = 'U'; # in absence of how to find out what sort of an object I am
47 # this is how a a connection starts, you get a hello message and the motd with
48 # possibly some other messages asking you to set various things up if you are
49 # new (or nearly new and slacking) user.
53 my ($self, $line, $sort) = @_;
54 my $user = $self->{user};
55 my $call = $self->{call};
56 my $name = $user->{name};
58 $self->{name} = $name ? $name : $call;
59 $self->send($self->msg('l2',$self->{name}));
60 $self->send_file($main::motd) if (-e $main::motd);
61 $self->state('prompt'); # a bit of room for further expansion, passwords etc
62 $self->{priv} = $user->priv;
63 $self->{lang} = $user->lang;
64 $self->{pagelth} = 20;
65 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
66 $self->{consort} = $line; # save the connection type
68 # set some necessary flags on the user if they are connecting
69 $self->{beep} = $self->{wwv} = $self->{wx} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
70 # $self->prompt() if $self->{state} =~ /^prompt/o;
72 # add yourself to the database
73 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
74 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
75 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
77 # issue a pc16 to everybody interested
78 my $nchan = DXChannel->get($main::mycall);
79 my @pc16 = DXProt::pc16($nchan, $cuser);
80 DXProt::broadcast_ak1a(@pc16);
81 Log('DXCommand', "$call connected");
83 # send prompts and things
84 my $info = DXCluster::cluster();
85 $self->send("Cluster:$info");
86 $self->send($self->msg('namee1')) if !$user->name;
87 $self->send($self->msg('qthe1')) if !$user->qth;
88 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
89 $self->send($self->msg('hnodee1')) if !$user->qth;
90 $self->send($self->msg('msgnew')) if DXMsg::for_me($call);
93 $self->{spotfilter} = Filter::read_in('spots', $call);
95 $self->send($self->msg('pr', $call));
99 # This is the normal command prompt driver
108 # remove leading and trailing spaces
109 $cmdline =~ s/^\s*(.*)\s*$/$1/;
111 if ($self->{state} eq 'page') {
112 my $i = $self->{pagelth};
113 my $ref = $self->{pagedata};
116 # abort if we get a line starting in with a
117 if ($cmdline =~ /^a/io) {
122 # send a tranche of data
123 while ($i-- > 0 && @$ref) {
124 my $line = shift @$ref;
125 $line =~ s/\s+$//o; # why am having to do this?
129 # reset state if none or else chuck out an intermediate prompt
131 $tot -= $self->{pagelth};
132 $self->send($self->msg('page', $tot));
134 $self->state('prompt');
137 @ans = run_cmd($self, $cmdline); # if length $cmdline;
139 if ($self->{pagelth} && @ans > $self->{pagelth}) {
141 for ($i = $self->{pagelth}; $i-- > 0; ) {
142 my $line = shift @ans;
143 $line =~ s/\s+$//o; # why am having to do this?
146 $self->{pagedata} = \@ans;
147 $self->state('page');
148 $self->send($self->msg('page', scalar @ans));
151 s/\s+$//o; # why ?????????
157 # send a prompt only if we are in a prompt state
158 $self->prompt() if $self->{state} =~ /^prompt/o;
162 # this is the thing that runs the command, it is done like this for the
163 # benefit of remote command execution
169 my $user = $self->{user};
170 my $call = $self->{call};
175 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
176 dbg('eval', "stored func cmd = $c\n");
179 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
183 return () if length $cmdline == 0;
186 $cmdline =~ s|//|/|og;
188 # split the command line up into parts, the first part is the command
189 my ($cmd, $args) = $cmdline =~ /^([\S\/]+)\s*(.*)/o;
195 dbg('command', "cmd: $cmd");
197 # alias it if possible
198 my $acmd = CmdAlias::get_cmd($cmd);
200 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
201 dbg('command', "aliased cmd: $cmd $args");
204 # first expand out the entry to a command
205 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
206 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
209 dbg('command', "path: $cmd cmd: $fcmd");
211 my $package = find_cmd_name($path, $fcmd);
212 @ans = (0) if !$package ;
215 dbg('command', "package: $package");
217 my $c = qq{ \@ans = $package(\$self, \$args) };
218 dbg('eval', "cluster cmd = $c\n");
221 @ans = (0, "Syserr: Eval err cached $package\n$@");
225 dbg('command', "cmd: $cmd not found");
236 unshift @ans, $self->msg('e2');
238 @ans = $self->msg('e1');
245 # This is called from inside the main cluster processing loop and is used
246 # for despatching commands that are doing some long processing job
251 my @dxchan = DXChannel->get_all();
254 foreach $dxchan (@dxchan) {
255 next if $dxchan->sort ne 'U';
257 # send a prompt if no activity out on this channel
258 if ($t >= $dxchan->t + $main::user_interval) {
259 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
266 # finish up a user context
271 my $call = $self->call;
274 if (-e "$main::data/logout") {
275 open(I, "$main::data/logout") or confess;
278 $self->send_now('D', @in);
282 if ($call eq $main::myalias) { # unset the channel if it is us really
283 my $node = DXNode->get($main::mycall);
286 my $ref = DXCluster->get_exact($call);
288 # issue a pc17 to everybody interested
289 my $nchan = DXChannel->get($main::mycall);
290 my $pc17 = $nchan->pc17($self);
291 DXProt::broadcast_ak1a($pc17);
293 Log('DXCommand', "$call disconnected");
298 # short cut to output a prompt
304 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
307 # broadcast a message to all users [except those mentioned after buffer]
310 my $pkg = shift; # ignored
311 my $s = shift; # the line to be rebroadcast
312 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
313 my @list = DXChannel->get_all(); # just in case we are called from some funny object
314 my ($dxchan, $except);
316 L: foreach $dxchan (@list) {
317 next if !$dxchan->sort eq 'U'; # only interested in user channels
318 foreach $except (@except) {
319 next L if $except == $dxchan; # ignore channels in the 'except' list
321 $dxchan->send($s); # send it
325 # gimme all the users
328 my @list = DXChannel->get_all();
331 foreach $ref (@list) {
332 push @out, $ref if $ref->sort eq 'U';
338 # search for the command in the cache of short->long form commands
343 my ($path, $short_cmd, $suffix) = @_;
346 # commands are lower case
347 $short_cmd = lc $short_cmd;
348 dbg('command', "command: $path $short_cmd\n");
350 # do some checking for funny characters
351 return () if $short_cmd =~ /\/$/;
353 # return immediately if we have it
354 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
355 if ($apath && $acmd) {
356 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
357 return ($apath, $acmd);
361 my @parts = split '/', $short_cmd;
368 for ($i = 0; $i < @parts; $i++) {
370 opendir(D, $curdir) or confess "can't open $curdir $!";
374 foreach $l (sort @ls) {
376 if ($i < $#parts) { # we are dealing with directories
377 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
378 dbg('command', "got dir: $curdir/$l\n");
383 } else { # we are dealing with commands
384 @lparts = split /\./, $l;
385 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
386 if ($p eq substr($l, 0, length $p)) {
387 pop @lparts; # remove the suffix
388 $l = join '.', @lparts;
389 # chop $dirfn; # remove trailing /
390 $dirfn = "" unless $dirfn;
391 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
392 dbg('command', "got path: $path cmd: $dirfn$l\n");
393 return ($path, "$dirfn$l");
401 # clear the command name cache
408 # the persistant execution of things from the command directories
411 # This allows perl programs to call functions dynamically
413 # This has been nicked directly from the perlembed pages
416 #require Devel::Symdump;
418 sub valid_package_name {
420 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
422 #second pass only for words starting with a digit
423 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
425 #Dress it up as a real package name
426 $string =~ s/\//_/og;
427 return "Emb_" . $string;
430 #borrowed from Safe.pm
436 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
437 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
439 if ($stem && $leaf) {
440 my $stem_symtab = *{$stem}{HASH};
441 delete $stem_symtab->{$leaf};
445 # find a cmd reference
446 # this is really for use in user written stubs
448 # use the result as a symbolic reference:-
451 # @out = &$r($self, $line);
460 # first expand out the entry to a command
461 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
462 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
464 # make sure it is loaded
465 $r = find_cmd_name($path, $fcmd);
471 # this bit of magic finds a command in the offered directory
475 my $package = valid_package_name($cmdname);
476 my $filename = "$path/$cmdname.pl";
477 my $mtime = -M $filename;
479 # return if we can't find it
481 unless (defined $mtime) {
482 $errstr = DXM::msg('e1');
486 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
487 #we have compiled this subroutine already,
488 #it has not been updated on disk, nothing left to do
489 #print STDERR "already compiled $package->handler\n";
492 delete_package($package) if defined $Cache{$package}{mtime};
494 my $fh = new FileHandle;
495 if (!open $fh, $filename) {
496 $errstr = "Syserr: can't open '$filename' $!";
503 #wrap the code into a subroutine inside our unique package
504 my $eval = qq{ sub $package { $sub } };
507 my @list = split /\n/, $eval;
510 dbg('eval', $_, "\n");
515 #hide our variables within this block
516 my($filename,$mtime,$package,$sub);
523 delete_package($package);
525 #cache it unless we're cleaning out each time
526 $Cache{$package}{'mtime'} = $mtime;
530 #print Devel::Symdump->rnew($package)->as_string, $/;
531 $package = "DXCommandmode::$package" if $package;
532 $package = undef if $errstr;