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 $maxerrors);
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
42 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
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 $DXProt::me->conn($self->conn) if $call eq $main::myalias; # send all output for mycall to myalias
104 my @rout = $main::routeroot->add_user($call, Route::here($self->{here}));
105 dbg('route', "B/C PC16 on $main::mycall for: $call") if @rout;
106 DXProt::route_pc16($DXProt::me, $main::routeroot, @rout) if @rout;
107 Log('DXCommand', "$call connected");
109 # send prompts and things
110 my $info = Route::cluster();
111 $self->send("Cluster:$info");
112 $self->send($self->msg('namee1')) if !$user->name;
113 $self->send($self->msg('qthe1')) if !$user->qth;
114 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
115 $self->send($self->msg('hnodee1')) if !$user->qth;
116 $self->send($self->msg('m9')) if DXMsg::for_me($call);
120 if (!$user->wantecho) {
121 $self->send_now('E', "0");
122 $self->send($self->msg('echow'));
125 $self->tell_login('loginu');
130 # This is the normal command prompt driver
139 # remove leading and trailing spaces
140 $cmdline =~ s/^\s*(.*)\s*$/$1/;
142 if ($self->{state} eq 'page') {
143 my $i = $self->{pagelth};
144 my $ref = $self->{pagedata};
147 # abort if we get a line starting in with a
148 if ($cmdline =~ /^a/io) {
153 # send a tranche of data
154 while ($i-- > 0 && @$ref) {
155 my $line = shift @$ref;
156 $line =~ s/\s+$//o; # why am having to do this?
160 # reset state if none or else chuck out an intermediate prompt
162 $tot -= $self->{pagelth};
163 $self->send($self->msg('page', $tot));
165 $self->state('prompt');
167 } elsif ($self->{state} eq 'sysop') {
168 my $passwd = $self->{user}->passwd;
169 my @pw = split / */, $passwd;
171 my @l = @{$self->{passwd}};
172 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
173 if ($cmdline =~ /$str/) {
174 $self->{priv} = $self->{user}->priv;
176 $self->send($self->msg('sorry'));
179 $self->send($self->msg('sorry'));
181 delete $self->{passwd};
182 $self->state('prompt');
183 } elsif ($self->{state} eq 'talk') {
184 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
185 for (@{$self->{talklist}}) {
186 $self->send_talks($_, $self->msg('talkend'));
188 $self->state('prompt');
189 delete $self->{talklist};
190 } elsif ($cmdline =~ m(^/\w+)) {
192 $self->send_ans(run_cmd($self, $cmdline));
193 $self->send($self->talk_prompt);
194 } elsif ($self->{talklist} && @{$self->{talklist}}) {
195 # send what has been said to whoever is in this person's talk list
196 for (@{$self->{talklist}}) {
197 $self->send_talks($_, $cmdline);
199 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
202 $self->state('prompt');
205 $self->send_ans(run_cmd($self, $cmdline));
208 # send a prompt only if we are in a prompt state
209 $self->prompt() if $self->{state} =~ /^prompt/o;
212 # send out the talk messages taking into account vias and connectivity
215 my ($self, $ent, $line) = @_;
217 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
218 $to = $ent unless $to;
219 my $call = $via ? $via : $to;
220 my $clref = Route::get($call);
221 my $dxchan = $clref->dxchan if $clref;
223 $dxchan->talk($self->{call}, $to, $via, $line);
225 $self->send($self->msg('disc2', $via ? $via : $to));
226 my @l = grep { $_ ne $ent } @{$self->{talklist}};
228 $self->{talklist} = \@l;
230 delete $self->{talklist};
231 $self->state('prompt');
240 for (@{$self->{talklist}}) {
241 my ($to, $via) = /(\S+)>(\S+)/;
245 return $self->msg('talkprompt', join(',', @call));
249 # send a load of stuff to a command user with page prompting
257 if ($self->{pagelth} && @_ > $self->{pagelth}) {
259 for ($i = $self->{pagelth}; $i-- > 0; ) {
261 $line =~ s/\s+$//o; # why am having to do this?
264 $self->{pagedata} = [ @_ ];
265 $self->state('page');
266 $self->send($self->msg('page', scalar @_));
269 $self->send($_) if $_;
274 # this is the thing that runs the command, it is done like this for the
275 # benefit of remote command execution
281 my $user = $self->{user};
282 my $call = $self->{call};
287 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
288 dbg('eval', "stored func cmd = $c\n");
291 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
295 return () if length $cmdline == 0;
298 $cmdline =~ s|//|/|og;
300 # split the command line up into parts, the first part is the command
301 my ($cmd, $args) = split /\s+/, $cmdline, 2;
302 $args = "" unless defined $args;
308 dbg('command', "cmd: $cmd");
310 # alias it if possible
311 my $acmd = CmdAlias::get_cmd($cmd);
313 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
314 $args = "" unless defined $args;
315 dbg('command', "aliased cmd: $cmd $args");
318 # first expand out the entry to a command
319 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
320 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
323 dbg('command', "path: $cmd cmd: $fcmd");
325 my $package = find_cmd_name($path, $fcmd);
326 @ans = (0) if !$package ;
329 dbg('command', "package: $package");
331 unless (exists $Cache{$package}->{'sub'}) {
332 $c = eval $Cache{$package}->{'eval'};
334 return DXDebug::shortmess($@);
336 $Cache{$package}->{'sub'} = $c;
338 $c = $Cache{$package}->{'sub'};
340 @ans = &{$c}($self, $args);
345 return (DXDebug::shortmess($@));
349 dbg('command', "cmd: $cmd not found");
350 if (++$self->{errors} > $maxerrors) {
351 $self->send($self->msg('e26'));
355 return ($self->msg('e1'));
363 delete $self->{errors};
365 if (++$self->{errors} > $maxerrors) {
366 $self->send($self->msg('e26'));
375 # This is called from inside the main cluster processing loop and is used
376 # for despatching commands that are doing some long processing job
381 my @dxchan = DXChannel->get_all();
384 foreach $dxchan (@dxchan) {
385 next if $dxchan->sort ne 'U';
387 # send a prompt if no activity out on this channel
388 if ($t >= $dxchan->t + $main::user_interval) {
389 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
396 # finish up a user context
401 my $call = $self->call;
403 # reset the redirection of messages back to 'normal' if we are the sysop
404 if ($call eq $main::myalias) {
405 $DXProt::me->conn(undef);
408 my @rout = $main::routeroot->del_user($call);
409 dbg('route', "B/C PC17 on $main::mycall for: $call");
411 # issue a pc17 to everybody interested
412 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
414 # I was the last node visited
415 $self->user->node($main::mycall);
417 # send info to all logged in thingies
418 $self->tell_login('logoutu');
420 Log('DXCommand', "$call disconnected");
422 $self->SUPER::disconnect;
426 # short cut to output a prompt
432 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
435 # broadcast a message to all users [except those mentioned after buffer]
438 my $pkg = shift; # ignored
439 my $s = shift; # the line to be rebroadcast
440 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
441 my @list = DXChannel->get_all(); # just in case we are called from some funny object
442 my ($dxchan, $except);
444 L: foreach $dxchan (@list) {
445 next if !$dxchan->sort eq 'U'; # only interested in user channels
446 foreach $except (@except) {
447 next L if $except == $dxchan; # ignore channels in the 'except' list
449 $dxchan->send($s); # send it
453 # gimme all the users
456 my @list = DXChannel->get_all();
459 foreach $ref (@list) {
460 push @out, $ref if $ref->sort eq 'U';
465 # run a script for this user
469 my $silent = shift || 0;
474 # search for the command in the cache of short->long form commands
479 my ($path, $short_cmd, $suffix) = @_;
482 # commands are lower case
483 $short_cmd = lc $short_cmd;
484 dbg('command', "command: $path $short_cmd\n");
486 # do some checking for funny characters
487 return () if $short_cmd =~ /\/$/;
489 # return immediately if we have it
490 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
491 if ($apath && $acmd) {
492 dbg('command', "cached $short_cmd = ($apath, $acmd)\n");
493 return ($apath, $acmd);
497 my @parts = split '/', $short_cmd;
504 for ($i = 0; $i < @parts; $i++) {
506 opendir(D, $curdir) or confess "can't open $curdir $!";
510 foreach $l (sort @ls) {
512 if ($i < $#parts) { # we are dealing with directories
513 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
514 dbg('command', "got dir: $curdir/$l\n");
519 } else { # we are dealing with commands
520 @lparts = split /\./, $l;
521 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
522 if ($p eq substr($l, 0, length $p)) {
523 pop @lparts; # remove the suffix
524 $l = join '.', @lparts;
525 # chop $dirfn; # remove trailing /
526 $dirfn = "" unless $dirfn;
527 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
528 dbg('command', "got path: $path cmd: $dirfn$l\n");
529 return ($path, "$dirfn$l");
537 # clear the command name cache
544 # the persistant execution of things from the command directories
547 # This allows perl programs to call functions dynamically
549 # This has been nicked directly from the perlembed pages
552 #require Devel::Symdump;
554 sub valid_package_name {
556 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
558 #second pass only for words starting with a digit
559 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
561 #Dress it up as a real package name
562 $string =~ s/\//_/og;
566 # find a cmd reference
567 # this is really for use in user written stubs
569 # use the result as a symbolic reference:-
572 # @out = &$r($self, $line);
581 # first expand out the entry to a command
582 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
583 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
585 # make sure it is loaded
586 $r = find_cmd_name($path, $fcmd);
592 # this bit of magic finds a command in the offered directory
596 my $package = valid_package_name($cmdname);
597 my $filename = "$path/$cmdname.pl";
598 my $mtime = -M $filename;
600 # return if we can't find it
602 unless (defined $mtime) {
603 $errstr = DXM::msg('e1');
607 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
608 #we have compiled this subroutine already,
609 #it has not been updated on disk, nothing left to do
610 #print STDERR "already compiled $package->handler\n";
614 my $sub = readfilestr($filename);
616 $errstr = "Syserr: can't open '$filename' $!";
620 #wrap the code into a subroutine inside our unique package
621 my $eval = qq( sub { $sub } );
624 my @list = split /\n/, $eval;
627 dbg('eval', $_, "\n");
631 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
637 # send a talk message here
640 my ($self, $from, $to, $via, $line) = @_;
641 $line =~ s/\\5E/\^/g;
642 $self->send("$to de $from: $line") if $self->{talk};
643 Log('talk', $to, $from, $main::mycall, $line);