3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
35 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
37 %Cache = (); # cache of dynamically loaded routine's mod times
38 %cmd_cache = (); # cache of short names
39 $errstr = (); # error string from eval
40 %aliases = (); # aliases for (parts of) commands
41 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
44 # obtain a new connection this is derived from dxchannel
49 my $self = DXChannel::alloc(@_);
53 # this is how a a connection starts, you get a hello message and the motd with
54 # possibly some other messages asking you to set various things up if you are
55 # new (or nearly new and slacking) user.
59 my ($self, $line, $sort) = @_;
60 my $user = $self->{user};
61 my $call = $self->{call};
62 my $name = $user->{name};
64 $self->{name} = $name ? $name : $call;
65 $self->send($self->msg('l2',$self->{name}));
66 $self->send_file($main::motd) if (-e $main::motd);
67 $self->state('prompt'); # a bit of room for further expansion, passwords etc
68 $self->{priv} = $user->priv || 0;
69 $self->{lang} = $user->lang || $main::lang || 'en';
70 $self->{pagelth} = $user->pagelth || 20;
71 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
72 $self->{consort} = $line; # save the connection type
74 # set some necessary flags on the user if they are connecting
75 $self->{beep} = $user->wantbeep;
76 $self->{ann} = $user->wantann;
77 $self->{wwv} = $user->wantwwv;
78 $self->{wcy} = $user->wantwcy;
79 $self->{talk} = $user->wanttalk;
80 $self->{wx} = $user->wantwx;
81 $self->{dx} = $user->wantdx;
82 $self->{logininfo} = $user->wantlogininfo;
86 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
87 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
88 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
89 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
91 # clean up qra locators
93 $qra = undef if ($qra && !DXBearing::is_qra($qra));
96 my $long = $user->long;
97 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
100 # add yourself to the database
101 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
102 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
103 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
105 # issue a pc16 to everybody interested
106 my $nchan = DXChannel->get($main::mycall);
107 my @pc16 = DXProt::pc16($nchan, $cuser);
109 DXProt::broadcast_all_ak1a($_);
111 Log('DXCommand', "$call connected");
113 # send prompts and things
114 my $info = DXCluster::cluster();
115 $self->send("Cluster:$info");
116 $self->send($self->msg('namee1')) if !$user->name;
117 $self->send($self->msg('qthe1')) if !$user->qth;
118 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
119 $self->send($self->msg('hnodee1')) if !$user->qth;
120 $self->send($self->msg('m9')) if DXMsg::for_me($call);
124 if (!$user->wantecho) {
125 $self->send_now('E', "0");
126 $self->send($self->msg('echow'));
129 $self->tell_login('loginu');
134 # This is the normal command prompt driver
143 # remove leading and trailing spaces
144 $cmdline =~ s/^\s*(.*)\s*$/$1/;
146 if ($self->{state} eq 'page') {
147 my $i = $self->{pagelth};
148 my $ref = $self->{pagedata};
151 # abort if we get a line starting in with a
152 if ($cmdline =~ /^a/io) {
157 # send a tranche of data
158 while ($i-- > 0 && @$ref) {
159 my $line = shift @$ref;
160 $line =~ s/\s+$//o; # why am having to do this?
164 # reset state if none or else chuck out an intermediate prompt
166 $tot -= $self->{pagelth};
167 $self->send($self->msg('page', $tot));
169 $self->state('prompt');
171 } elsif ($self->{state} eq 'sysop') {
172 my $passwd = $self->{user}->passwd;
173 my @pw = split / */, $passwd;
175 my @l = @{$self->{passwd}};
176 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
177 if ($cmdline =~ /$str/) {
178 $self->{priv} = $self->{user}->priv;
180 $self->send($self->msg('sorry'));
183 $self->send($self->msg('sorry'));
185 delete $self->{passwd};
186 $self->state('prompt');
187 } elsif ($self->{state} eq 'talk') {
188 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
189 for (@{$self->{talklist}}) {
190 $self->send_talks($_, $self->msg('talkend'));
192 $self->state('prompt');
193 delete $self->{talklist};
194 } elsif ($cmdline =~ m(^/\w+)) {
196 $self->send_ans(run_cmd($self, $cmdline));
197 $self->send($self->talk_prompt);
198 } elsif ($self->{talklist} && @{$self->{talklist}}) {
199 # send what has been said to whoever is in this person's talk list
200 for (@{$self->{talklist}}) {
201 $self->send_talks($_, $cmdline);
203 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
206 $self->state('prompt');
209 $self->send_ans(run_cmd($self, $cmdline));
212 # send a prompt only if we are in a prompt state
213 $self->prompt() if $self->{state} =~ /^prompt/o;
216 # send out the talk messages taking into account vias and connectivity
219 my ($self, $ent, $line) = @_;
221 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
222 $to = $ent unless $to;
223 my $call = $via ? $via : $to;
224 my $clref = DXCluster->get_exact($call);
225 my $dxchan = $clref->dxchan if $clref;
227 $dxchan->talk($self->{call}, $to, $via, $line);
229 $self->send($self->msg('disc2', $via ? $via : $to));
230 my @l = grep { $_ ne $ent } @{$self->{talklist}};
232 $self->{talklist} = \@l;
234 delete $self->{talklist};
235 $self->state('prompt');
244 for (@{$self->{talklist}}) {
245 my ($to, $via) = /(\S+)>(\S+)/;
249 return $self->msg('talkprompt', join(',', @call));
253 # send a load of stuff to a command user with page prompting
261 if ($self->{pagelth} && @_ > $self->{pagelth}) {
263 for ($i = $self->{pagelth}; $i-- > 0; ) {
265 $line =~ s/\s+$//o; # why am having to do this?
268 $self->{pagedata} = [ @_ ];
269 $self->state('page');
270 $self->send($self->msg('page', scalar @_));
273 $self->send($_) if $_;
278 # this is the thing that runs the command, it is done like this for the
279 # benefit of remote command execution
285 my $user = $self->{user};
286 my $call = $self->{call};
291 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
292 dbg('eval', "stored func cmd = $c\n");
295 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
299 return () if length $cmdline == 0;
302 $cmdline =~ s|//|/|og;
304 # split the command line up into parts, the first part is the command
305 my ($cmd, $args) = split /\s+/, $cmdline, 2;
306 $args = "" unless defined $args;
312 dbg('command', "cmd: $cmd");
314 # alias it if possible
315 my $acmd = CmdAlias::get_cmd($cmd);
317 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
318 $args = "" unless defined $args;
319 dbg('command', "aliased cmd: $cmd $args");
322 # first expand out the entry to a command
323 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
324 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
327 dbg('command', "path: $cmd cmd: $fcmd");
329 my $package = find_cmd_name($path, $fcmd);
330 @ans = (0) if !$package ;
333 dbg('command', "package: $package");
335 unless (exists $Cache{$package}->{'sub'}) {
336 $c = eval $Cache{$package}->{'eval'};
338 return DXDebug::shortmess($@);
340 $Cache{$package}->{'sub'} = $c;
342 $c = $Cache{$package}->{'sub'};
344 @ans = &{$c}($self, $args);
349 return (DXDebug::shortmess($@));
353 dbg('command', "cmd: $cmd not found");
354 return ($self->msg('e1'));
364 # This is called from inside the main cluster processing loop and is used
365 # for despatching commands that are doing some long processing job
370 my @dxchan = DXChannel->get_all();
373 foreach $dxchan (@dxchan) {
374 next if $dxchan->sort ne 'U';
376 # send a prompt if no activity out on this channel
377 if ($t >= $dxchan->t + $main::user_interval) {
378 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
385 # finish up a user context
390 my $call = $self->call;
392 # reset the redirection of messages back to 'normal' if we are the sysop
393 if ($call eq $main::myalias) {
394 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
395 $node->dxchan($DXProt::me);
398 # I was the last node visited
399 $self->user->node($main::mycall);
401 # issue a pc17 to everybody interested
402 my $nchan = DXChannel->get($main::mycall);
403 my $pc17 = $nchan->pc17($self);
404 DXProt::broadcast_all_ak1a($pc17);
406 # send info to all logged in thingies
407 $self->tell_login('logoutu');
409 Log('DXCommand', "$call disconnected");
410 my $ref = DXCluster->get_exact($call);
415 # short cut to output a prompt
421 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
424 # broadcast a message to all users [except those mentioned after buffer]
427 my $pkg = shift; # ignored
428 my $s = shift; # the line to be rebroadcast
429 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
430 my @list = DXChannel->get_all(); # just in case we are called from some funny object
431 my ($dxchan, $except);
433 L: foreach $dxchan (@list) {
434 next if !$dxchan->sort eq 'U'; # only interested in user channels
435 foreach $except (@except) {
436 next L if $except == $dxchan; # ignore channels in the 'except' list
438 $dxchan->send($s); # send it
442 # gimme all the users
445 my @list = DXChannel->get_all();
448 foreach $ref (@list) {
449 push @out, $ref if $ref->sort eq 'U';
454 # run a script for this user
458 my $silent = shift || 0;
463 # search for the command in the cache of short->long form commands
468 my ($path, $short_cmd, $suffix) = @_;
471 # commands are lower case
472 $short_cmd = lc $short_cmd;
473 dbg('command', "command: $path $short_cmd\n");
475 # do some checking for funny characters
476 return () if $short_cmd =~ /\/$/;
478 # return immediately if we have it
479 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
480 if ($apath && $acmd) {
481 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
482 return ($apath, $acmd);
486 my @parts = split '/', $short_cmd;
493 for ($i = 0; $i < @parts; $i++) {
495 opendir(D, $curdir) or confess "can't open $curdir $!";
499 foreach $l (sort @ls) {
501 if ($i < $#parts) { # we are dealing with directories
502 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
503 dbg('command', "got dir: $curdir/$l\n");
508 } else { # we are dealing with commands
509 @lparts = split /\./, $l;
510 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
511 if ($p eq substr($l, 0, length $p)) {
512 pop @lparts; # remove the suffix
513 $l = join '.', @lparts;
514 # chop $dirfn; # remove trailing /
515 $dirfn = "" unless $dirfn;
516 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
517 dbg('command', "got path: $path cmd: $dirfn$l\n");
518 return ($path, "$dirfn$l");
526 # clear the command name cache
533 # the persistant execution of things from the command directories
536 # This allows perl programs to call functions dynamically
538 # This has been nicked directly from the perlembed pages
541 #require Devel::Symdump;
543 sub valid_package_name {
545 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
547 #second pass only for words starting with a digit
548 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
550 #Dress it up as a real package name
551 $string =~ s/\//_/og;
555 # find a cmd reference
556 # this is really for use in user written stubs
558 # use the result as a symbolic reference:-
561 # @out = &$r($self, $line);
570 # first expand out the entry to a command
571 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
572 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
574 # make sure it is loaded
575 $r = find_cmd_name($path, $fcmd);
581 # this bit of magic finds a command in the offered directory
585 my $package = valid_package_name($cmdname);
586 my $filename = "$path/$cmdname.pl";
587 my $mtime = -M $filename;
589 # return if we can't find it
591 unless (defined $mtime) {
592 $errstr = DXM::msg('e1');
596 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
597 #we have compiled this subroutine already,
598 #it has not been updated on disk, nothing left to do
599 #print STDERR "already compiled $package->handler\n";
603 my $sub = readfilestr($filename);
605 $errstr = "Syserr: can't open '$filename' $!";
609 #wrap the code into a subroutine inside our unique package
610 my $eval = qq( sub { $sub } );
613 my @list = split /\n/, $eval;
616 dbg('eval', $_, "\n");
620 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
626 # send a talk message here
629 my ($self, $from, $to, $via, $line) = @_;
630 $line =~ s/\\5E/\^/g;
631 $self->send("$to de $from: $line") if $self->{talk};
632 Log('talk', $to, $from, $main::mycall, $line);