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;
80 $self->{logininfo} = $user->wantlogininfo;
83 # add yourself to the database
84 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
85 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
86 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
88 # issue a pc16 to everybody interested
89 my $nchan = DXChannel->get($main::mycall);
90 my @pc16 = DXProt::pc16($nchan, $cuser);
92 DXProt::broadcast_all_ak1a($_);
94 Log('DXCommand', "$call connected");
96 # send prompts and things
97 my $info = DXCluster::cluster();
98 $self->send("Cluster:$info");
99 $self->send($self->msg('namee1')) if !$user->name;
100 $self->send($self->msg('qthe1')) if !$user->qth;
101 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
102 $self->send($self->msg('hnodee1')) if !$user->qth;
103 $self->send($self->msg('m9')) if DXMsg::for_me($call);
104 $self->send($self->msg('pr', $call));
106 $self->tell_login('loginu');
111 # This is the normal command prompt driver
120 # remove leading and trailing spaces
121 $cmdline =~ s/^\s*(.*)\s*$/$1/;
123 if ($self->{state} eq 'page') {
124 my $i = $self->{pagelth};
125 my $ref = $self->{pagedata};
128 # abort if we get a line starting in with a
129 if ($cmdline =~ /^a/io) {
134 # send a tranche of data
135 while ($i-- > 0 && @$ref) {
136 my $line = shift @$ref;
137 $line =~ s/\s+$//o; # why am having to do this?
141 # reset state if none or else chuck out an intermediate prompt
143 $tot -= $self->{pagelth};
144 $self->send($self->msg('page', $tot));
146 $self->state('prompt');
148 } elsif ($self->{state} eq 'sysop') {
149 my $passwd = $self->{user}->passwd;
150 my @pw = split / */, $passwd;
152 my @l = @{$self->{passwd}};
153 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
154 if ($cmdline =~ /$str/) {
155 $self->{priv} = $self->{user}->priv;
157 $self->send($self->msg('sorry'));
160 $self->send($self->msg('sorry'));
162 delete $self->{passwd};
163 $self->state('prompt');
165 @ans = run_cmd($self, $cmdline); # if length $cmdline;
167 if ($self->{pagelth} && @ans > $self->{pagelth}) {
169 for ($i = $self->{pagelth}; $i-- > 0; ) {
170 my $line = shift @ans;
171 $line =~ s/\s+$//o; # why am having to do this?
174 $self->{pagedata} = \@ans;
175 $self->state('page');
176 $self->send($self->msg('page', scalar @ans));
179 $self->send($_) if $_;
184 # send a prompt only if we are in a prompt state
185 $self->prompt() if $self->{state} =~ /^prompt/o;
189 # this is the thing that runs the command, it is done like this for the
190 # benefit of remote command execution
196 my $user = $self->{user};
197 my $call = $self->{call};
202 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
203 dbg('eval', "stored func cmd = $c\n");
206 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
210 return () if length $cmdline == 0;
213 $cmdline =~ s|//|/|og;
215 # split the command line up into parts, the first part is the command
216 my ($cmd, $args) = split /\s+/, $cmdline, 2;
217 $args = "" unless $args;
223 dbg('command', "cmd: $cmd");
225 # alias it if possible
226 my $acmd = CmdAlias::get_cmd($cmd);
228 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
229 $args = "" unless $args;
230 dbg('command', "aliased cmd: $cmd $args");
233 # first expand out the entry to a command
234 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
235 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
238 dbg('command', "path: $cmd cmd: $fcmd");
240 my $package = find_cmd_name($path, $fcmd);
241 @ans = (0) if !$package ;
244 dbg('command', "package: $package");
246 unless (exists $Cache{$package}->{'sub'}) {
247 $c = eval $Cache{$package}->{'eval'};
249 return ("Syserr: Syntax error in $package", $@);
251 $Cache{$package}->{'sub'} = $c;
253 $c = $Cache{$package}->{'sub'};
255 @ans = &{$c}($self, $args);
261 dbg('command', "cmd: $cmd not found");
262 return ($self->msg('e1'));
272 # This is called from inside the main cluster processing loop and is used
273 # for despatching commands that are doing some long processing job
278 my @dxchan = DXChannel->get_all();
281 foreach $dxchan (@dxchan) {
282 next if $dxchan->sort ne 'U';
284 # send a prompt if no activity out on this channel
285 if ($t >= $dxchan->t + $main::user_interval) {
286 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
293 # finish up a user context
298 my $call = $self->call;
300 # I was the last node visited
301 $self->user->node($main::mycall);
304 if (-e "$main::data/logout") {
305 open(I, "$main::data/logout") or confess;
308 $self->send_now('D', @in);
312 if ($call eq $main::myalias) { # unset the channel if it is us really
313 my $node = DXNode->get($main::mycall);
317 # issue a pc17 to everybody interested
318 my $nchan = DXChannel->get($main::mycall);
319 my $pc17 = $nchan->pc17($self);
320 DXProt::broadcast_all_ak1a($pc17);
322 # send info to all logged in thingies
323 $self->tell_login('logoutu');
325 Log('DXCommand', "$call disconnected");
326 my $ref = DXCluster->get_exact($call);
331 # short cut to output a prompt
337 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
340 # broadcast a message to all users [except those mentioned after buffer]
343 my $pkg = shift; # ignored
344 my $s = shift; # the line to be rebroadcast
345 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
346 my @list = DXChannel->get_all(); # just in case we are called from some funny object
347 my ($dxchan, $except);
349 L: foreach $dxchan (@list) {
350 next if !$dxchan->sort eq 'U'; # only interested in user channels
351 foreach $except (@except) {
352 next L if $except == $dxchan; # ignore channels in the 'except' list
354 $dxchan->send($s); # send it
358 # gimme all the users
361 my @list = DXChannel->get_all();
364 foreach $ref (@list) {
365 push @out, $ref if $ref->sort eq 'U';
370 # run a script for this user
374 my $silent = shift || 0;
379 # search for the command in the cache of short->long form commands
384 my ($path, $short_cmd, $suffix) = @_;
387 # commands are lower case
388 $short_cmd = lc $short_cmd;
389 dbg('command', "command: $path $short_cmd\n");
391 # do some checking for funny characters
392 return () if $short_cmd =~ /\/$/;
394 # return immediately if we have it
395 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
396 if ($apath && $acmd) {
397 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
398 return ($apath, $acmd);
402 my @parts = split '/', $short_cmd;
409 for ($i = 0; $i < @parts; $i++) {
411 opendir(D, $curdir) or confess "can't open $curdir $!";
415 foreach $l (sort @ls) {
417 if ($i < $#parts) { # we are dealing with directories
418 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
419 dbg('command', "got dir: $curdir/$l\n");
424 } else { # we are dealing with commands
425 @lparts = split /\./, $l;
426 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
427 if ($p eq substr($l, 0, length $p)) {
428 pop @lparts; # remove the suffix
429 $l = join '.', @lparts;
430 # chop $dirfn; # remove trailing /
431 $dirfn = "" unless $dirfn;
432 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
433 dbg('command', "got path: $path cmd: $dirfn$l\n");
434 return ($path, "$dirfn$l");
442 # clear the command name cache
449 # the persistant execution of things from the command directories
452 # This allows perl programs to call functions dynamically
454 # This has been nicked directly from the perlembed pages
457 #require Devel::Symdump;
459 sub valid_package_name {
461 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
463 #second pass only for words starting with a digit
464 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
466 #Dress it up as a real package name
467 $string =~ s/\//_/og;
471 # find a cmd reference
472 # this is really for use in user written stubs
474 # use the result as a symbolic reference:-
477 # @out = &$r($self, $line);
486 # first expand out the entry to a command
487 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
488 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
490 # make sure it is loaded
491 $r = find_cmd_name($path, $fcmd);
497 # this bit of magic finds a command in the offered directory
501 my $package = valid_package_name($cmdname);
502 my $filename = "$path/$cmdname.pl";
503 my $mtime = -M $filename;
505 # return if we can't find it
507 unless (defined $mtime) {
508 $errstr = DXM::msg('e1');
512 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
513 #we have compiled this subroutine already,
514 #it has not been updated on disk, nothing left to do
515 #print STDERR "already compiled $package->handler\n";
519 my $sub = readfilestr($filename);
521 $errstr = "Syserr: can't open '$filename' $!";
525 #wrap the code into a subroutine inside our unique package
526 my $eval = qq( sub { $sub } );
529 my @list = split /\n/, $eval;
532 dbg('eval', $_, "\n");
536 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };