3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
34 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
36 %Cache = (); # cache of dynamically loaded routine's mod times
37 %cmd_cache = (); # cache of short names
38 $errstr = (); # error string from eval
39 %aliases = (); # aliases for (parts of) commands
40 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
43 # obtain a new connection this is derived from dxchannel
48 my $self = DXChannel::alloc(@_);
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} = $user->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->{wcy} = $user->wantwcy;
78 $self->{talk} = $user->wanttalk;
79 $self->{wx} = $user->wantwx;
80 $self->{dx} = $user->wantdx;
81 $self->{logininfo} = $user->wantlogininfo;
85 # add yourself to the database
86 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
87 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
88 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
90 # issue a pc16 to everybody interested
91 my $nchan = DXChannel->get($main::mycall);
92 my @pc16 = DXProt::pc16($nchan, $cuser);
94 DXProt::broadcast_all_ak1a($_);
96 Log('DXCommand', "$call connected");
98 # send prompts and things
99 my $info = DXCluster::cluster();
100 $self->send("Cluster:$info");
101 $self->send($self->msg('namee1')) if !$user->name;
102 $self->send($self->msg('qthe1')) if !$user->qth;
103 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
104 $self->send($self->msg('hnodee1')) if !$user->qth;
105 $self->send($self->msg('m9')) if DXMsg::for_me($call);
106 $self->send($self->msg('pr', $call));
109 if (!$user->wantecho) {
110 $self->send_now('E', "0");
111 $self->send($self->msg('echow'));
114 $self->tell_login('loginu');
119 # This is the normal command prompt driver
128 # remove leading and trailing spaces
129 $cmdline =~ s/^\s*(.*)\s*$/$1/;
131 if ($self->{state} eq 'page') {
132 my $i = $self->{pagelth};
133 my $ref = $self->{pagedata};
136 # abort if we get a line starting in with a
137 if ($cmdline =~ /^a/io) {
142 # send a tranche of data
143 while ($i-- > 0 && @$ref) {
144 my $line = shift @$ref;
145 $line =~ s/\s+$//o; # why am having to do this?
149 # reset state if none or else chuck out an intermediate prompt
151 $tot -= $self->{pagelth};
152 $self->send($self->msg('page', $tot));
154 $self->state('prompt');
156 } elsif ($self->{state} eq 'sysop') {
157 my $passwd = $self->{user}->passwd;
158 my @pw = split / */, $passwd;
160 my @l = @{$self->{passwd}};
161 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
162 if ($cmdline =~ /$str/) {
163 $self->{priv} = $self->{user}->priv;
165 $self->send($self->msg('sorry'));
168 $self->send($self->msg('sorry'));
170 delete $self->{passwd};
171 $self->state('prompt');
173 @ans = run_cmd($self, $cmdline); # if length $cmdline;
175 if ($self->{pagelth} && @ans > $self->{pagelth}) {
177 for ($i = $self->{pagelth}; $i-- > 0; ) {
178 my $line = shift @ans;
179 $line =~ s/\s+$//o; # why am having to do this?
182 $self->{pagedata} = \@ans;
183 $self->state('page');
184 $self->send($self->msg('page', scalar @ans));
187 $self->send($_) if $_;
192 # send a prompt only if we are in a prompt state
193 $self->prompt() if $self->{state} =~ /^prompt/o;
197 # this is the thing that runs the command, it is done like this for the
198 # benefit of remote command execution
204 my $user = $self->{user};
205 my $call = $self->{call};
210 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
211 dbg('eval', "stored func cmd = $c\n");
214 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
218 return () if length $cmdline == 0;
221 $cmdline =~ s|//|/|og;
223 # split the command line up into parts, the first part is the command
224 my ($cmd, $args) = split /\s+/, $cmdline, 2;
225 $args = "" unless $args;
231 dbg('command', "cmd: $cmd");
233 # alias it if possible
234 my $acmd = CmdAlias::get_cmd($cmd);
236 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
237 $args = "" unless $args;
238 dbg('command', "aliased cmd: $cmd $args");
241 # first expand out the entry to a command
242 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
243 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
246 dbg('command', "path: $cmd cmd: $fcmd");
248 my $package = find_cmd_name($path, $fcmd);
249 @ans = (0) if !$package ;
252 dbg('command', "package: $package");
254 unless (exists $Cache{$package}->{'sub'}) {
255 $c = eval $Cache{$package}->{'eval'};
257 return DXDebug::shortmess($@);
259 $Cache{$package}->{'sub'} = $c;
261 $c = $Cache{$package}->{'sub'};
263 @ans = &{$c}($self, $args);
268 return (DXDebug::shortmess($@));
272 dbg('command', "cmd: $cmd not found");
273 return ($self->msg('e1'));
283 # This is called from inside the main cluster processing loop and is used
284 # for despatching commands that are doing some long processing job
289 my @dxchan = DXChannel->get_all();
292 foreach $dxchan (@dxchan) {
293 next if $dxchan->sort ne 'U';
295 # send a prompt if no activity out on this channel
296 if ($t >= $dxchan->t + $main::user_interval) {
297 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
304 # finish up a user context
309 my $call = $self->call;
311 # I was the last node visited
312 $self->user->node($main::mycall);
315 if (-e "$main::data/logout") {
316 open(I, "$main::data/logout") or confess;
319 $self->send_now('D', @in);
323 if ($call eq $main::myalias) { # unset the channel if it is us really
324 my $node = DXNode->get($main::mycall);
328 # issue a pc17 to everybody interested
329 my $nchan = DXChannel->get($main::mycall);
330 my $pc17 = $nchan->pc17($self);
331 DXProt::broadcast_all_ak1a($pc17);
333 # send info to all logged in thingies
334 $self->tell_login('logoutu');
336 Log('DXCommand', "$call disconnected");
337 my $ref = DXCluster->get_exact($call);
342 # short cut to output a prompt
348 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
351 # broadcast a message to all users [except those mentioned after buffer]
354 my $pkg = shift; # ignored
355 my $s = shift; # the line to be rebroadcast
356 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
357 my @list = DXChannel->get_all(); # just in case we are called from some funny object
358 my ($dxchan, $except);
360 L: foreach $dxchan (@list) {
361 next if !$dxchan->sort eq 'U'; # only interested in user channels
362 foreach $except (@except) {
363 next L if $except == $dxchan; # ignore channels in the 'except' list
365 $dxchan->send($s); # send it
369 # gimme all the users
372 my @list = DXChannel->get_all();
375 foreach $ref (@list) {
376 push @out, $ref if $ref->sort eq 'U';
381 # run a script for this user
385 my $silent = shift || 0;
390 # search for the command in the cache of short->long form commands
395 my ($path, $short_cmd, $suffix) = @_;
398 # commands are lower case
399 $short_cmd = lc $short_cmd;
400 dbg('command', "command: $path $short_cmd\n");
402 # do some checking for funny characters
403 return () if $short_cmd =~ /\/$/;
405 # return immediately if we have it
406 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
407 if ($apath && $acmd) {
408 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
409 return ($apath, $acmd);
413 my @parts = split '/', $short_cmd;
420 for ($i = 0; $i < @parts; $i++) {
422 opendir(D, $curdir) or confess "can't open $curdir $!";
426 foreach $l (sort @ls) {
428 if ($i < $#parts) { # we are dealing with directories
429 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
430 dbg('command', "got dir: $curdir/$l\n");
435 } else { # we are dealing with commands
436 @lparts = split /\./, $l;
437 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
438 if ($p eq substr($l, 0, length $p)) {
439 pop @lparts; # remove the suffix
440 $l = join '.', @lparts;
441 # chop $dirfn; # remove trailing /
442 $dirfn = "" unless $dirfn;
443 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
444 dbg('command', "got path: $path cmd: $dirfn$l\n");
445 return ($path, "$dirfn$l");
453 # clear the command name cache
460 # the persistant execution of things from the command directories
463 # This allows perl programs to call functions dynamically
465 # This has been nicked directly from the perlembed pages
468 #require Devel::Symdump;
470 sub valid_package_name {
472 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
474 #second pass only for words starting with a digit
475 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
477 #Dress it up as a real package name
478 $string =~ s/\//_/og;
482 # find a cmd reference
483 # this is really for use in user written stubs
485 # use the result as a symbolic reference:-
488 # @out = &$r($self, $line);
497 # first expand out the entry to a command
498 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
499 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
501 # make sure it is loaded
502 $r = find_cmd_name($path, $fcmd);
508 # this bit of magic finds a command in the offered directory
512 my $package = valid_package_name($cmdname);
513 my $filename = "$path/$cmdname.pl";
514 my $mtime = -M $filename;
516 # return if we can't find it
518 unless (defined $mtime) {
519 $errstr = DXM::msg('e1');
523 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
524 #we have compiled this subroutine already,
525 #it has not been updated on disk, nothing left to do
526 #print STDERR "already compiled $package->handler\n";
530 my $sub = readfilestr($filename);
532 $errstr = "Syserr: can't open '$filename' $!";
536 #wrap the code into a subroutine inside our unique package
537 my $eval = qq( sub { $sub } );
540 my @list = split /\n/, $eval;
543 dbg('eval', $_, "\n");
547 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };