3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
33 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
35 %Cache = (); # cache of dynamically loaded routine's mod times
36 %cmd_cache = (); # cache of short names
37 $errstr = (); # error string from eval
38 %aliases = (); # aliases for (parts of) commands
39 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
42 # obtain a new connection this is derived from dxchannel
47 my $self = DXChannel::alloc(@_);
48 $self->{'sort'} = 'U'; # in absence of how to find out what sort of an object I am
52 # this is how a a connection starts, you get a hello message and the motd with
53 # possibly some other messages asking you to set various things up if you are
54 # new (or nearly new and slacking) user.
58 my ($self, $line, $sort) = @_;
59 my $user = $self->{user};
60 my $call = $self->{call};
61 my $name = $user->{name};
63 $self->{name} = $name ? $name : $call;
64 $self->send($self->msg('l2',$self->{name}));
65 $self->send_file($main::motd) if (-e $main::motd);
66 $self->state('prompt'); # a bit of room for further expansion, passwords etc
67 $self->{priv} = $user->priv;
68 $self->{lang} = $user->lang;
69 $self->{pagelth} = 20;
70 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
71 $self->{consort} = $line; # save the connection type
73 # set some necessary flags on the user if they are connecting
74 $self->{beep} = $user->wantbeep;
75 $self->{ann} = $user->wantann;
76 $self->{wwv} = $user->wantwwv;
77 $self->{talk} = $user->wanttalk;
78 $self->{wx} = $user->wantwx;
79 $self->{dx} = $user->wantdx;
82 # add yourself to the database
83 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
84 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
85 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
87 # issue a pc16 to everybody interested
88 my $nchan = DXChannel->get($main::mycall);
89 my @pc16 = DXProt::pc16($nchan, $cuser);
91 DXProt::broadcast_all_ak1a($_);
93 Log('DXCommand', "$call connected");
95 # send prompts and things
96 my $info = DXCluster::cluster();
97 $self->send("Cluster:$info");
98 $self->send($self->msg('namee1')) if !$user->name;
99 $self->send($self->msg('qthe1')) if !$user->qth;
100 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
101 $self->send($self->msg('hnodee1')) if !$user->qth;
102 $self->send($self->msg('m9')) if DXMsg::for_me($call);
105 $self->send($self->msg('pr', $call));
109 # This is the normal command prompt driver
118 # remove leading and trailing spaces
119 $cmdline =~ s/^\s*(.*)\s*$/$1/;
121 if ($self->{state} eq 'page') {
122 my $i = $self->{pagelth};
123 my $ref = $self->{pagedata};
126 # abort if we get a line starting in with a
127 if ($cmdline =~ /^a/io) {
132 # send a tranche of data
133 while ($i-- > 0 && @$ref) {
134 my $line = shift @$ref;
135 $line =~ s/\s+$//o; # why am having to do this?
139 # reset state if none or else chuck out an intermediate prompt
141 $tot -= $self->{pagelth};
142 $self->send($self->msg('page', $tot));
144 $self->state('prompt');
146 } elsif ($self->{state} eq 'sysop') {
147 my $passwd = $self->{user}->passwd;
148 my @pw = split / */, $passwd;
150 my @l = @{$self->{passwd}};
151 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
152 if ($cmdline =~ /$str/) {
153 $self->{priv} = $self->{user}->priv;
155 $self->send($self->msg('sorry'));
158 $self->send($self->msg('sorry'));
160 delete $self->{passwd};
161 $self->state('prompt');
163 @ans = run_cmd($self, $cmdline); # if length $cmdline;
165 if ($self->{pagelth} && @ans > $self->{pagelth}) {
167 for ($i = $self->{pagelth}; $i-- > 0; ) {
168 my $line = shift @ans;
169 $line =~ s/\s+$//o; # why am having to do this?
172 $self->{pagedata} = \@ans;
173 $self->state('page');
174 $self->send($self->msg('page', scalar @ans));
177 $self->send($_) if $_;
182 # send a prompt only if we are in a prompt state
183 $self->prompt() if $self->{state} =~ /^prompt/o;
187 # this is the thing that runs the command, it is done like this for the
188 # benefit of remote command execution
194 my $user = $self->{user};
195 my $call = $self->{call};
200 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
201 dbg('eval', "stored func cmd = $c\n");
204 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
208 return () if length $cmdline == 0;
211 $cmdline =~ s|//|/|og;
213 # split the command line up into parts, the first part is the command
214 my ($cmd, $args) = split /\s+/, $cmdline, 2;
215 $args = "" unless $args;
221 dbg('command', "cmd: $cmd");
223 # alias it if possible
224 my $acmd = CmdAlias::get_cmd($cmd);
226 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
227 $args = "" unless $args;
228 dbg('command', "aliased cmd: $cmd $args");
231 # first expand out the entry to a command
232 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
233 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
236 dbg('command', "path: $cmd cmd: $fcmd");
238 my $package = find_cmd_name($path, $fcmd);
239 @ans = (0) if !$package ;
242 dbg('command', "package: $package");
244 unless (exists $Cache{$package}->{'sub'}) {
245 $c = eval $Cache{$package}->{'eval'};
247 return ("Syserr: Syntax error in $package", $@);
249 $Cache{$package}->{'sub'} = $c;
251 $c = $Cache{$package}->{'sub'};
253 @ans = &{$c}($self, $args);
259 dbg('command', "cmd: $cmd not found");
260 return ($self->msg('e1'));
270 # This is called from inside the main cluster processing loop and is used
271 # for despatching commands that are doing some long processing job
276 my @dxchan = DXChannel->get_all();
279 foreach $dxchan (@dxchan) {
280 next if $dxchan->sort ne 'U';
282 # send a prompt if no activity out on this channel
283 if ($t >= $dxchan->t + $main::user_interval) {
284 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
291 # finish up a user context
296 my $call = $self->call;
299 if (-e "$main::data/logout") {
300 open(I, "$main::data/logout") or confess;
303 $self->send_now('D', @in);
307 if ($call eq $main::myalias) { # unset the channel if it is us really
308 my $node = DXNode->get($main::mycall);
311 my $ref = DXCluster->get_exact($call);
313 # issue a pc17 to everybody interested
314 my $nchan = DXChannel->get($main::mycall);
315 my $pc17 = $nchan->pc17($self);
316 DXProt::broadcast_all_ak1a($pc17);
318 Log('DXCommand', "$call disconnected");
323 # short cut to output a prompt
329 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
332 # broadcast a message to all users [except those mentioned after buffer]
335 my $pkg = shift; # ignored
336 my $s = shift; # the line to be rebroadcast
337 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
338 my @list = DXChannel->get_all(); # just in case we are called from some funny object
339 my ($dxchan, $except);
341 L: foreach $dxchan (@list) {
342 next if !$dxchan->sort eq 'U'; # only interested in user channels
343 foreach $except (@except) {
344 next L if $except == $dxchan; # ignore channels in the 'except' list
346 $dxchan->send($s); # send it
350 # gimme all the users
353 my @list = DXChannel->get_all();
356 foreach $ref (@list) {
357 push @out, $ref if $ref->sort eq 'U';
362 # run a script for this user
366 my $silent = shift || 0;
371 # search for the command in the cache of short->long form commands
376 my ($path, $short_cmd, $suffix) = @_;
379 # commands are lower case
380 $short_cmd = lc $short_cmd;
381 dbg('command', "command: $path $short_cmd\n");
383 # do some checking for funny characters
384 return () if $short_cmd =~ /\/$/;
386 # return immediately if we have it
387 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
388 if ($apath && $acmd) {
389 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
390 return ($apath, $acmd);
394 my @parts = split '/', $short_cmd;
401 for ($i = 0; $i < @parts; $i++) {
403 opendir(D, $curdir) or confess "can't open $curdir $!";
407 foreach $l (sort @ls) {
409 if ($i < $#parts) { # we are dealing with directories
410 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
411 dbg('command', "got dir: $curdir/$l\n");
416 } else { # we are dealing with commands
417 @lparts = split /\./, $l;
418 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
419 if ($p eq substr($l, 0, length $p)) {
420 pop @lparts; # remove the suffix
421 $l = join '.', @lparts;
422 # chop $dirfn; # remove trailing /
423 $dirfn = "" unless $dirfn;
424 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
425 dbg('command', "got path: $path cmd: $dirfn$l\n");
426 return ($path, "$dirfn$l");
434 # clear the command name cache
441 # the persistant execution of things from the command directories
444 # This allows perl programs to call functions dynamically
446 # This has been nicked directly from the perlembed pages
449 #require Devel::Symdump;
451 sub valid_package_name {
453 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
455 #second pass only for words starting with a digit
456 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
458 #Dress it up as a real package name
459 $string =~ s/\//_/og;
463 # find a cmd reference
464 # this is really for use in user written stubs
466 # use the result as a symbolic reference:-
469 # @out = &$r($self, $line);
478 # first expand out the entry to a command
479 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
480 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
482 # make sure it is loaded
483 $r = find_cmd_name($path, $fcmd);
489 # this bit of magic finds a command in the offered directory
493 my $package = valid_package_name($cmdname);
494 my $filename = "$path/$cmdname.pl";
495 my $mtime = -M $filename;
497 # return if we can't find it
499 unless (defined $mtime) {
500 $errstr = DXM::msg('e1');
504 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
505 #we have compiled this subroutine already,
506 #it has not been updated on disk, nothing left to do
507 #print STDERR "already compiled $package->handler\n";
511 my $sub = readfilestr($filename);
513 $errstr = "Syserr: can't open '$filename' $!";
517 #wrap the code into a subroutine inside our unique package
518 my $eval = qq( sub { $sub } );
521 my @list = split /\n/, $eval;
524 dbg('eval', $_, "\n");
528 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };