3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
32 use vars qw(%Cache %cmd_cache $errstr %aliases);
34 %Cache = (); # cache of dynamically loaded routine's mod times
35 %cmd_cache = (); # cache of short names
36 $errstr = (); # error string from eval
37 %aliases = (); # aliases for (parts of) commands
40 # obtain a new connection this is derived from dxchannel
45 my $self = DXChannel::alloc(@_);
46 $self->{'sort'} = 'U'; # in absence of how to find out what sort of an object I am
50 # this is how a a connection starts, you get a hello message and the motd with
51 # possibly some other messages asking you to set various things up if you are
52 # new (or nearly new and slacking) user.
56 my ($self, $line, $sort) = @_;
57 my $user = $self->{user};
58 my $call = $self->{call};
59 my $name = $user->{name};
61 $self->{name} = $name ? $name : $call;
62 $self->send($self->msg('l2',$self->{name}));
63 $self->send_file($main::motd) if (-e $main::motd);
64 $self->state('prompt'); # a bit of room for further expansion, passwords etc
65 $self->{priv} = $user->priv;
66 $self->{lang} = $user->lang;
67 $self->{pagelth} = 20;
68 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
69 $self->{consort} = $line; # save the connection type
71 # set some necessary flags on the user if they are connecting
72 $self->{beep} = $self->{wwv} = $self->{wx} = $self->{talk} = $self->{ann} = $self->{here} = $self->{dx} = 1;
73 # $self->prompt() if $self->{state} =~ /^prompt/o;
75 # add yourself to the database
76 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
77 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
78 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
80 # issue a pc16 to everybody interested
81 my $nchan = DXChannel->get($main::mycall);
82 my @pc16 = DXProt::pc16($nchan, $cuser);
84 DXProt::broadcast_all_ak1a($_);
86 Log('DXCommand', "$call connected");
88 # send prompts and things
89 my $info = DXCluster::cluster();
90 $self->send("Cluster:$info");
91 $self->send($self->msg('namee1')) if !$user->name;
92 $self->send($self->msg('qthe1')) if !$user->qth;
93 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
94 $self->send($self->msg('hnodee1')) if !$user->qth;
95 $self->send($self->msg('m9')) if DXMsg::for_me($call);
98 $self->send($self->msg('pr', $call));
102 # This is the normal command prompt driver
111 # remove leading and trailing spaces
112 $cmdline =~ s/^\s*(.*)\s*$/$1/;
114 if ($self->{state} eq 'page') {
115 my $i = $self->{pagelth};
116 my $ref = $self->{pagedata};
119 # abort if we get a line starting in with a
120 if ($cmdline =~ /^a/io) {
125 # send a tranche of data
126 while ($i-- > 0 && @$ref) {
127 my $line = shift @$ref;
128 $line =~ s/\s+$//o; # why am having to do this?
132 # reset state if none or else chuck out an intermediate prompt
134 $tot -= $self->{pagelth};
135 $self->send($self->msg('page', $tot));
137 $self->state('prompt');
139 } elsif ($self->{state} eq 'sysop') {
140 my $passwd = $self->{user}->passwd;
141 my @pw = split / */, $passwd;
143 my @l = @{$self->{passwd}};
144 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
145 if ($cmdline =~ /$str/) {
146 $self->{priv} = $self->{user}->priv;
148 $self->send($self->msg('sorry'));
151 $self->send($self->msg('sorry'));
153 delete $self->{passwd};
154 $self->state('prompt');
156 @ans = run_cmd($self, $cmdline); # if length $cmdline;
158 if ($self->{pagelth} && @ans > $self->{pagelth}) {
160 for ($i = $self->{pagelth}; $i-- > 0; ) {
161 my $line = shift @ans;
162 $line =~ s/\s+$//o; # why am having to do this?
165 $self->{pagedata} = \@ans;
166 $self->state('page');
167 $self->send($self->msg('page', scalar @ans));
170 s/\s+$//o; # why ?????????
176 # send a prompt only if we are in a prompt state
177 $self->prompt() if $self->{state} =~ /^prompt/o;
181 # this is the thing that runs the command, it is done like this for the
182 # benefit of remote command execution
188 my $user = $self->{user};
189 my $call = $self->{call};
194 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
195 dbg('eval', "stored func cmd = $c\n");
198 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
202 return () if length $cmdline == 0;
205 $cmdline =~ s|//|/|og;
207 # split the command line up into parts, the first part is the command
208 my ($cmd, $args) = $cmdline =~ /^([\S\/]+)\s*(.*)/o;
214 dbg('command', "cmd: $cmd");
216 # alias it if possible
217 my $acmd = CmdAlias::get_cmd($cmd);
219 ($cmd, $args) = "$acmd $args" =~ /^([\w\/]+)\s*(.*)/o;
220 dbg('command', "aliased cmd: $cmd $args");
223 # first expand out the entry to a command
224 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
225 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
228 dbg('command', "path: $cmd cmd: $fcmd");
230 my $package = find_cmd_name($path, $fcmd);
231 @ans = (0) if !$package ;
234 dbg('command', "package: $package");
236 unless (exists $Cache{$package}->{sub}) {
237 $c = eval $Cache{$package}->{eval};
239 return ("Syserr: Syntax error in $package", $@);
241 $Cache{$package}->{sub} = $c;
243 $c = $Cache{$package}->{sub};
244 @ans = &{$c}($self, $args);
247 dbg('command', "cmd: $cmd not found");
248 return ($self->msg('e1'));
258 # This is called from inside the main cluster processing loop and is used
259 # for despatching commands that are doing some long processing job
264 my @dxchan = DXChannel->get_all();
267 foreach $dxchan (@dxchan) {
268 next if $dxchan->sort ne 'U';
270 # send a prompt if no activity out on this channel
271 if ($t >= $dxchan->t + $main::user_interval) {
272 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
279 # finish up a user context
284 my $call = $self->call;
287 if (-e "$main::data/logout") {
288 open(I, "$main::data/logout") or confess;
291 $self->send_now('D', @in);
295 if ($call eq $main::myalias) { # unset the channel if it is us really
296 my $node = DXNode->get($main::mycall);
299 my $ref = DXCluster->get_exact($call);
301 # issue a pc17 to everybody interested
302 my $nchan = DXChannel->get($main::mycall);
303 my $pc17 = $nchan->pc17($self);
304 DXProt::broadcast_all_ak1a($pc17);
306 Log('DXCommand', "$call disconnected");
311 # short cut to output a prompt
317 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call));
320 # broadcast a message to all users [except those mentioned after buffer]
323 my $pkg = shift; # ignored
324 my $s = shift; # the line to be rebroadcast
325 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
326 my @list = DXChannel->get_all(); # just in case we are called from some funny object
327 my ($dxchan, $except);
329 L: foreach $dxchan (@list) {
330 next if !$dxchan->sort eq 'U'; # only interested in user channels
331 foreach $except (@except) {
332 next L if $except == $dxchan; # ignore channels in the 'except' list
334 $dxchan->send($s); # send it
338 # gimme all the users
341 my @list = DXChannel->get_all();
344 foreach $ref (@list) {
345 push @out, $ref if $ref->sort eq 'U';
351 # search for the command in the cache of short->long form commands
356 my ($path, $short_cmd, $suffix) = @_;
359 # commands are lower case
360 $short_cmd = lc $short_cmd;
361 dbg('command', "command: $path $short_cmd\n");
363 # do some checking for funny characters
364 return () if $short_cmd =~ /\/$/;
366 # return immediately if we have it
367 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
368 if ($apath && $acmd) {
369 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
370 return ($apath, $acmd);
374 my @parts = split '/', $short_cmd;
381 for ($i = 0; $i < @parts; $i++) {
383 opendir(D, $curdir) or confess "can't open $curdir $!";
387 foreach $l (sort @ls) {
389 if ($i < $#parts) { # we are dealing with directories
390 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
391 dbg('command', "got dir: $curdir/$l\n");
396 } else { # we are dealing with commands
397 @lparts = split /\./, $l;
398 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
399 if ($p eq substr($l, 0, length $p)) {
400 pop @lparts; # remove the suffix
401 $l = join '.', @lparts;
402 # chop $dirfn; # remove trailing /
403 $dirfn = "" unless $dirfn;
404 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
405 dbg('command', "got path: $path cmd: $dirfn$l\n");
406 return ($path, "$dirfn$l");
414 # clear the command name cache
421 # the persistant execution of things from the command directories
424 # This allows perl programs to call functions dynamically
426 # This has been nicked directly from the perlembed pages
429 #require Devel::Symdump;
431 sub valid_package_name {
433 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
435 #second pass only for words starting with a digit
436 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
438 #Dress it up as a real package name
439 $string =~ s/\//_/og;
443 # find a cmd reference
444 # this is really for use in user written stubs
446 # use the result as a symbolic reference:-
449 # @out = &$r($self, $line);
458 # first expand out the entry to a command
459 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
460 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
462 # make sure it is loaded
463 $r = find_cmd_name($path, $fcmd);
469 # this bit of magic finds a command in the offered directory
473 my $package = valid_package_name($cmdname);
474 my $filename = "$path/$cmdname.pl";
475 my $mtime = -M $filename;
477 # return if we can't find it
479 unless (defined $mtime) {
480 $errstr = DXM::msg('e1');
484 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
485 #we have compiled this subroutine already,
486 #it has not been updated on disk, nothing left to do
487 #print STDERR "already compiled $package->handler\n";
491 my $fh = new IO::File;
492 if (!open $fh, $filename) {
493 $errstr = "Syserr: can't open '$filename' $!";
500 #wrap the code into a subroutine inside our unique package
501 my $eval = qq( sub { $sub } );
504 my @list = split /\n/, $eval;
507 dbg('eval', $_, "\n");
511 $Cache{$package} = {mtime => $mtime, eval => $eval };