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');
136 } elsif ($self->{state} eq 'sysop') {
137 my $passwd = $self->{user}->passwd;
138 my @pw = split / */, $passwd;
140 my @l = @{$self->{passwd}};
141 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
142 if ($cmdline =~ /$str/) {
143 $self->{priv} = $self->{user}->priv;
145 $self->send($self->msg('sorry'));
148 delete $self->{passwd};
149 $self->state('prompt');
151 @ans = run_cmd($self, $cmdline); # if length $cmdline;
153 if ($self->{pagelth} && @ans > $self->{pagelth}) {
155 for ($i = $self->{pagelth}; $i-- > 0; ) {
156 my $line = shift @ans;
157 $line =~ s/\s+$//o; # why am having to do this?
160 $self->{pagedata} = \@ans;
161 $self->state('page');
162 $self->send($self->msg('page', scalar @ans));
165 s/\s+$//o; # why ?????????
171 # send a prompt only if we are in a prompt state
172 $self->prompt() if $self->{state} =~ /^prompt/o;
176 # this is the thing that runs the command, it is done like this for the
177 # benefit of remote command execution
183 my $user = $self->{user};
184 my $call = $self->{call};
189 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
190 dbg('eval', "stored func cmd = $c\n");
193 return (1, "Syserr: Eval err $errstr on stored func $self->{func}");
197 return () if length $cmdline == 0;
200 $cmdline =~ s|//|/|og;
202 # split the command line up into parts, the first part is the command
203 my ($cmd, $args) = $cmdline =~ /^([\S\/]+)\s*(.*)/o;
209 dbg('command', "cmd: $cmd");
211 # alias it if possible
212 my $acmd = CmdAlias::get_cmd($cmd);
214 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
215 dbg('command', "aliased cmd: $cmd $args");
218 # first expand out the entry to a command
219 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
220 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
223 dbg('command', "path: $cmd cmd: $fcmd");
225 my $package = find_cmd_name($path, $fcmd);
226 @ans = (0) if !$package ;
229 dbg('command', "package: $package");
231 my $c = qq{ \@ans = $package(\$self, \$args) };
232 dbg('eval', "cluster cmd = $c\n");
235 @ans = (0, "Syserr: Eval err cached $package\n$@");
239 dbg('command', "cmd: $cmd not found");
250 unshift @ans, $self->msg('e2');
252 @ans = $self->msg('e1');
259 # This is called from inside the main cluster processing loop and is used
260 # for despatching commands that are doing some long processing job
265 my @dxchan = DXChannel->get_all();
268 foreach $dxchan (@dxchan) {
269 next if $dxchan->sort ne 'U';
271 # send a prompt if no activity out on this channel
272 if ($t >= $dxchan->t + $main::user_interval) {
273 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
280 # finish up a user context
285 my $call = $self->call;
288 if (-e "$main::data/logout") {
289 open(I, "$main::data/logout") or confess;
292 $self->send_now('D', @in);
296 if ($call eq $main::myalias) { # unset the channel if it is us really
297 my $node = DXNode->get($main::mycall);
300 my $ref = DXCluster->get_exact($call);
302 # issue a pc17 to everybody interested
303 my $nchan = DXChannel->get($main::mycall);
304 my $pc17 = $nchan->pc17($self);
305 DXProt::broadcast_ak1a($pc17);
307 Log('DXCommand', "$call disconnected");
312 # short cut to output a prompt
318 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
321 # broadcast a message to all users [except those mentioned after buffer]
324 my $pkg = shift; # ignored
325 my $s = shift; # the line to be rebroadcast
326 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
327 my @list = DXChannel->get_all(); # just in case we are called from some funny object
328 my ($dxchan, $except);
330 L: foreach $dxchan (@list) {
331 next if !$dxchan->sort eq 'U'; # only interested in user channels
332 foreach $except (@except) {
333 next L if $except == $dxchan; # ignore channels in the 'except' list
335 $dxchan->send($s); # send it
339 # gimme all the users
342 my @list = DXChannel->get_all();
345 foreach $ref (@list) {
346 push @out, $ref if $ref->sort eq 'U';
352 # search for the command in the cache of short->long form commands
357 my ($path, $short_cmd, $suffix) = @_;
360 # commands are lower case
361 $short_cmd = lc $short_cmd;
362 dbg('command', "command: $path $short_cmd\n");
364 # do some checking for funny characters
365 return () if $short_cmd =~ /\/$/;
367 # return immediately if we have it
368 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
369 if ($apath && $acmd) {
370 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
371 return ($apath, $acmd);
375 my @parts = split '/', $short_cmd;
382 for ($i = 0; $i < @parts; $i++) {
384 opendir(D, $curdir) or confess "can't open $curdir $!";
388 foreach $l (sort @ls) {
390 if ($i < $#parts) { # we are dealing with directories
391 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
392 dbg('command', "got dir: $curdir/$l\n");
397 } else { # we are dealing with commands
398 @lparts = split /\./, $l;
399 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
400 if ($p eq substr($l, 0, length $p)) {
401 pop @lparts; # remove the suffix
402 $l = join '.', @lparts;
403 # chop $dirfn; # remove trailing /
404 $dirfn = "" unless $dirfn;
405 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
406 dbg('command', "got path: $path cmd: $dirfn$l\n");
407 return ($path, "$dirfn$l");
415 # clear the command name cache
422 # the persistant execution of things from the command directories
425 # This allows perl programs to call functions dynamically
427 # This has been nicked directly from the perlembed pages
430 #require Devel::Symdump;
432 sub valid_package_name {
434 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
436 #second pass only for words starting with a digit
437 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
439 #Dress it up as a real package name
440 $string =~ s/\//_/og;
441 return "Emb_" . $string;
444 #borrowed from Safe.pm
450 $pkg = "DXCommandmode::$pkg\::"; # expand to full symbol table name
451 ($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
453 if ($stem && $leaf) {
454 my $stem_symtab = *{$stem}{HASH};
455 delete $stem_symtab->{$leaf};
459 # find a cmd reference
460 # this is really for use in user written stubs
462 # use the result as a symbolic reference:-
465 # @out = &$r($self, $line);
474 # first expand out the entry to a command
475 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
476 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
478 # make sure it is loaded
479 $r = find_cmd_name($path, $fcmd);
485 # this bit of magic finds a command in the offered directory
489 my $package = valid_package_name($cmdname);
490 my $filename = "$path/$cmdname.pl";
491 my $mtime = -M $filename;
493 # return if we can't find it
495 unless (defined $mtime) {
496 $errstr = DXM::msg('e1');
500 if(defined $Cache{$package}{mtime} && $Cache{$package}{mtime } <= $mtime) {
501 #we have compiled this subroutine already,
502 #it has not been updated on disk, nothing left to do
503 #print STDERR "already compiled $package->handler\n";
506 delete_package($package) if defined $Cache{$package}{mtime};
508 my $fh = new FileHandle;
509 if (!open $fh, $filename) {
510 $errstr = "Syserr: can't open '$filename' $!";
517 #wrap the code into a subroutine inside our unique package
518 my $eval = qq{ sub $package { $sub } };
521 my @list = split /\n/, $eval;
524 dbg('eval', $_, "\n");
529 #hide our variables within this block
530 my($filename,$mtime,$package,$sub);
537 delete_package($package);
539 #cache it unless we're cleaning out each time
540 $Cache{$package}{'mtime'} = $mtime;
544 #print Devel::Symdump->rnew($package)->as_string, $/;
545 $package = "DXCommandmode::$package" if $package;
546 $package = undef if $errstr;