3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
36 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase);
38 %Cache = (); # cache of dynamically loaded routine's mod times
39 %cmd_cache = (); # cache of short names
40 $errstr = (); # error string from eval
41 %aliases = (); # aliases for (parts of) commands
42 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
45 # obtain a new connection this is derived from dxchannel
50 my $self = DXChannel::alloc(@_);
54 # this is how a a connection starts, you get a hello message and the motd with
55 # possibly some other messages asking you to set various things up if you are
56 # new (or nearly new and slacking) user.
60 my ($self, $line, $sort) = @_;
61 my $user = $self->{user};
62 my $call = $self->{call};
63 my $name = $user->{name};
65 $self->{name} = $name ? $name : $call;
66 $self->send($self->msg('l2',$self->{name}));
67 $self->send_file($main::motd) if (-e $main::motd);
68 $self->state('prompt'); # a bit of room for further expansion, passwords etc
69 $self->{priv} = $user->priv || 0;
70 $self->{lang} = $user->lang || $main::lang || 'en';
71 $self->{pagelth} = $user->pagelth || 20;
72 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
73 $self->{consort} = $line; # save the connection type
75 # set some necessary flags on the user if they are connecting
76 $self->{beep} = $user->wantbeep;
77 $self->{ann} = $user->wantann;
78 $self->{wwv} = $user->wantwwv;
79 $self->{wcy} = $user->wantwcy;
80 $self->{talk} = $user->wanttalk;
81 $self->{wx} = $user->wantwx;
82 $self->{dx} = $user->wantdx;
83 $self->{logininfo} = $user->wantlogininfo;
87 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
88 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
89 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
90 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
92 # clean up qra locators
94 $qra = undef if ($qra && !DXBearing::is_qra($qra));
97 my $long = $user->long;
98 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
101 # add yourself to the database
102 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
103 my $cuser = DXNodeuser->new($self, $node, $call, 0, 1);
104 $node->dxchan($self) if $call eq $main::myalias; # send all output for mycall to myalias
106 # issue a pc16 to everybody interested
107 my $nchan = DXChannel->get($main::mycall);
108 my @pc16 = DXProt::pc16($nchan, $cuser);
110 DXProt::broadcast_all_ak1a($_);
112 Log('DXCommand', "$call connected");
114 # send prompts and things
115 my $info = DXCluster::cluster();
116 $self->send("Cluster:$info");
117 $self->send($self->msg('namee1')) if !$user->name;
118 $self->send($self->msg('qthe1')) if !$user->qth;
119 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
120 $self->send($self->msg('hnodee1')) if !$user->qth;
121 $self->send($self->msg('m9')) if DXMsg::for_me($call);
125 if (!$user->wantecho) {
126 $self->send_now('E', "0");
127 $self->send($self->msg('echow'));
130 $self->tell_login('loginu');
135 # This is the normal command prompt driver
144 # remove leading and trailing spaces
145 $cmdline =~ s/^\s*(.*)\s*$/$1/;
147 if ($self->{state} eq 'page') {
148 my $i = $self->{pagelth};
149 my $ref = $self->{pagedata};
152 # abort if we get a line starting in with a
153 if ($cmdline =~ /^a/io) {
158 # send a tranche of data
159 while ($i-- > 0 && @$ref) {
160 my $line = shift @$ref;
161 $line =~ s/\s+$//o; # why am having to do this?
165 # reset state if none or else chuck out an intermediate prompt
167 $tot -= $self->{pagelth};
168 $self->send($self->msg('page', $tot));
170 $self->state('prompt');
172 } elsif ($self->{state} eq 'sysop') {
173 my $passwd = $self->{user}->passwd;
174 my @pw = split / */, $passwd;
176 my @l = @{$self->{passwd}};
177 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
178 if ($cmdline =~ /$str/) {
179 $self->{priv} = $self->{user}->priv;
181 $self->send($self->msg('sorry'));
184 $self->send($self->msg('sorry'));
186 delete $self->{passwd};
187 $self->state('prompt');
188 } elsif ($self->{state} eq 'talk') {
189 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
190 for (@{$self->{talklist}}) {
191 $self->send_talks($_, $self->msg('talkend'));
193 $self->state('prompt');
194 delete $self->{talklist};
195 } elsif ($cmdline =~ m(^/\w+)) {
197 $self->send_ans(run_cmd($self, $cmdline));
198 $self->send($self->talk_prompt);
199 } elsif ($self->{talklist} && @{$self->{talklist}}) {
200 # send what has been said to whoever is in this person's talk list
201 for (@{$self->{talklist}}) {
202 $self->send_talks($_, $cmdline);
204 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
207 $self->state('prompt');
210 $self->send_ans(run_cmd($self, $cmdline));
213 # send a prompt only if we are in a prompt state
214 $self->prompt() if $self->{state} =~ /^prompt/o;
217 # send out the talk messages taking into account vias and connectivity
220 my ($self, $ent, $line) = @_;
222 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
223 $to = $ent unless $to;
224 my $call = $via ? $via : $to;
225 my $clref = DXCluster->get_exact($call);
226 my $dxchan = $clref->dxchan if $clref;
228 $dxchan->talk($self->{call}, $to, $via, $line);
230 $self->send($self->msg('disc2', $via ? $via : $to));
231 my @l = grep { $_ ne $ent } @{$self->{talklist}};
233 $self->{talklist} = \@l;
235 delete $self->{talklist};
236 $self->state('prompt');
245 for (@{$self->{talklist}}) {
246 my ($to, $via) = /(\S+)>(\S+)/;
250 return $self->msg('talkprompt', join(',', @call));
254 # send a load of stuff to a command user with page prompting
262 if ($self->{pagelth} && @_ > $self->{pagelth}) {
264 for ($i = $self->{pagelth}; $i-- > 0; ) {
266 $line =~ s/\s+$//o; # why am having to do this?
269 $self->{pagedata} = [ @_ ];
270 $self->state('page');
271 $self->send($self->msg('page', scalar @_));
274 $self->send($_) if $_;
279 # this is the thing that runs the command, it is done like this for the
280 # benefit of remote command execution
286 my $user = $self->{user};
287 my $call = $self->{call};
292 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
293 dbg('eval', "stored func cmd = $c\n");
296 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
300 return () if length $cmdline == 0;
303 $cmdline =~ s|//|/|og;
305 # split the command line up into parts, the first part is the command
306 my ($cmd, $args) = split /\s+/, $cmdline, 2;
307 $args = "" unless defined $args;
313 dbg('command', "cmd: $cmd");
315 # alias it if possible
316 my $acmd = CmdAlias::get_cmd($cmd);
318 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
319 $args = "" unless defined $args;
320 dbg('command', "aliased cmd: $cmd $args");
323 # first expand out the entry to a command
324 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
325 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
328 dbg('command', "path: $cmd cmd: $fcmd");
330 my $package = find_cmd_name($path, $fcmd);
331 @ans = (0) if !$package ;
334 dbg('command', "package: $package");
336 unless (exists $Cache{$package}->{'sub'}) {
337 $c = eval $Cache{$package}->{'eval'};
339 return DXDebug::shortmess($@);
341 $Cache{$package}->{'sub'} = $c;
343 $c = $Cache{$package}->{'sub'};
345 @ans = &{$c}($self, $args);
350 return (DXDebug::shortmess($@));
354 dbg('command', "cmd: $cmd not found");
355 return ($self->msg('e1'));
365 # This is called from inside the main cluster processing loop and is used
366 # for despatching commands that are doing some long processing job
371 my @dxchan = DXChannel->get_all();
374 foreach $dxchan (@dxchan) {
375 next if $dxchan->sort ne 'U';
377 # send a prompt if no activity out on this channel
378 if ($t >= $dxchan->t + $main::user_interval) {
379 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
386 # finish up a user context
391 my $call = $self->call;
393 # reset the redirection of messages back to 'normal' if we are the sysop
394 if ($call eq $main::myalias) {
395 my $node = DXNode->get($main::mycall) or die "$main::mycall not allocated in DXNode database";
396 $node->dxchan($DXProt::me);
399 # I was the last node visited
400 $self->user->node($main::mycall);
402 # issue a pc17 to everybody interested
403 my $nchan = DXChannel->get($main::mycall);
404 my $pc17 = $nchan->pc17($self);
405 DXProt::broadcast_all_ak1a($pc17);
407 # send info to all logged in thingies
408 $self->tell_login('logoutu');
410 Log('DXCommand', "$call disconnected");
411 my $ref = DXCluster->get_exact($call);
414 $self->SUPER::disconnect;
418 # short cut to output a prompt
424 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
427 # broadcast a message to all users [except those mentioned after buffer]
430 my $pkg = shift; # ignored
431 my $s = shift; # the line to be rebroadcast
432 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
433 my @list = DXChannel->get_all(); # just in case we are called from some funny object
434 my ($dxchan, $except);
436 L: foreach $dxchan (@list) {
437 next if !$dxchan->sort eq 'U'; # only interested in user channels
438 foreach $except (@except) {
439 next L if $except == $dxchan; # ignore channels in the 'except' list
441 $dxchan->send($s); # send it
445 # gimme all the users
448 my @list = DXChannel->get_all();
451 foreach $ref (@list) {
452 push @out, $ref if $ref->sort eq 'U';
457 # run a script for this user
461 my $silent = shift || 0;
466 # search for the command in the cache of short->long form commands
471 my ($path, $short_cmd, $suffix) = @_;
474 # commands are lower case
475 $short_cmd = lc $short_cmd;
476 dbg('command', "command: $path $short_cmd\n");
478 # do some checking for funny characters
479 return () if $short_cmd =~ /\/$/;
481 # return immediately if we have it
482 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
483 if ($apath && $acmd) {
484 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
485 return ($apath, $acmd);
489 my @parts = split '/', $short_cmd;
496 for ($i = 0; $i < @parts; $i++) {
498 opendir(D, $curdir) or confess "can't open $curdir $!";
502 foreach $l (sort @ls) {
504 if ($i < $#parts) { # we are dealing with directories
505 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
506 dbg('command', "got dir: $curdir/$l\n");
511 } else { # we are dealing with commands
512 @lparts = split /\./, $l;
513 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
514 if ($p eq substr($l, 0, length $p)) {
515 pop @lparts; # remove the suffix
516 $l = join '.', @lparts;
517 # chop $dirfn; # remove trailing /
518 $dirfn = "" unless $dirfn;
519 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
520 dbg('command', "got path: $path cmd: $dirfn$l\n");
521 return ($path, "$dirfn$l");
529 # clear the command name cache
536 # the persistant execution of things from the command directories
539 # This allows perl programs to call functions dynamically
541 # This has been nicked directly from the perlembed pages
544 #require Devel::Symdump;
546 sub valid_package_name {
548 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
550 #second pass only for words starting with a digit
551 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
553 #Dress it up as a real package name
554 $string =~ s/\//_/og;
558 # find a cmd reference
559 # this is really for use in user written stubs
561 # use the result as a symbolic reference:-
564 # @out = &$r($self, $line);
573 # first expand out the entry to a command
574 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
575 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
577 # make sure it is loaded
578 $r = find_cmd_name($path, $fcmd);
584 # this bit of magic finds a command in the offered directory
588 my $package = valid_package_name($cmdname);
589 my $filename = "$path/$cmdname.pl";
590 my $mtime = -M $filename;
592 # return if we can't find it
594 unless (defined $mtime) {
595 $errstr = DXM::msg('e1');
599 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
600 #we have compiled this subroutine already,
601 #it has not been updated on disk, nothing left to do
602 #print STDERR "already compiled $package->handler\n";
606 my $sub = readfilestr($filename);
608 $errstr = "Syserr: can't open '$filename' $!";
612 #wrap the code into a subroutine inside our unique package
613 my $eval = qq( sub { $sub } );
616 my @list = split /\n/, $eval;
619 dbg('eval', $_, "\n");
623 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
629 # send a talk message here
632 my ($self, $from, $to, $via, $line) = @_;
633 $line =~ s/\\5E/\^/g;
634 $self->send("$to de $from: $line") if $self->{talk};
635 Log('talk', $to, $from, $main::mycall, $line);