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 %nothereslug);
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(@_);
52 # routing, this must go out here to prevent race condx
55 my @rout = $main::routeroot->add_user($call, Route::here(1));
56 DXProt::route_pc16($DXProt::me, $main::routeroot, @rout) if @rout;
61 # this is how a a connection starts, you get a hello message and the motd with
62 # possibly some other messages asking you to set various things up if you are
63 # new (or nearly new and slacking) user.
67 my ($self, $line, $sort) = @_;
68 my $user = $self->{user};
69 my $call = $self->{call};
70 my $name = $user->{name};
72 $self->{name} = $name ? $name : $call;
73 $self->send($self->msg('l2',$self->{name}));
74 $self->send_file($main::motd) if (-e $main::motd);
75 $self->state('prompt'); # a bit of room for further expansion, passwords etc
76 $self->{priv} = $user->priv || 0;
77 $self->{lang} = $user->lang || $main::lang || 'en';
78 $self->{pagelth} = $user->pagelth || 20;
79 $self->{priv} = 0 if $line =~ /^(ax|te)/; # set the connection priv to 0 - can be upgraded later
80 $self->{consort} = $line; # save the connection type
82 # set some necessary flags on the user if they are connecting
83 $self->{beep} = $user->wantbeep;
84 $self->{ann} = $user->wantann;
85 $self->{wwv} = $user->wantwwv;
86 $self->{wcy} = $user->wantwcy;
87 $self->{talk} = $user->wanttalk;
88 $self->{wx} = $user->wantwx;
89 $self->{dx} = $user->wantdx;
90 $self->{logininfo} = $user->wantlogininfo;
94 $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
95 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
96 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
97 $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
99 # clean up qra locators
100 my $qra = $user->qra;
101 $qra = undef if ($qra && !DXBearing::is_qra($qra));
103 my $lat = $user->lat;
104 my $long = $user->long;
105 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
108 Log('DXCommand', "$call connected");
110 # send prompts and things
111 my $info = Route::cluster();
112 $self->send("Cluster:$info");
113 $self->send($self->msg('namee1')) if !$user->name;
114 $self->send($self->msg('qthe1')) if !$user->qth;
115 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
116 $self->send($self->msg('hnodee1')) if !$user->qth;
117 $self->send($self->msg('m9')) if DXMsg::for_me($call);
121 if (!$user->wantecho) {
122 $self->send_now('E', "0");
123 $self->send($self->msg('echow'));
126 $self->tell_login('loginu');
131 # This is the normal command prompt driver
140 # remove leading and trailing spaces
141 $cmdline =~ s/^\s*(.*)\s*$/$1/;
143 if ($self->{state} eq 'page') {
144 my $i = $self->{pagelth};
145 my $ref = $self->{pagedata};
148 # abort if we get a line starting in with a
149 if ($cmdline =~ /^a/io) {
154 # send a tranche of data
155 while ($i-- > 0 && @$ref) {
156 my $line = shift @$ref;
157 $line =~ s/\s+$//o; # why am having to do this?
161 # reset state if none or else chuck out an intermediate prompt
163 $tot -= $self->{pagelth};
164 $self->send($self->msg('page', $tot));
166 $self->state('prompt');
168 } elsif ($self->{state} eq 'sysop') {
169 my $passwd = $self->{user}->passwd;
170 my @pw = split / */, $passwd;
172 my @l = @{$self->{passwd}};
173 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
174 if ($cmdline =~ /$str/) {
175 $self->{priv} = $self->{user}->priv;
177 $self->send($self->msg('sorry'));
180 $self->send($self->msg('sorry'));
182 delete $self->{passwd};
183 $self->state('prompt');
184 } elsif ($self->{state} eq 'talk') {
185 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
186 for (@{$self->{talklist}}) {
187 $self->send_talks($_, $self->msg('talkend'));
189 $self->state('prompt');
190 delete $self->{talklist};
191 } elsif ($cmdline =~ m(^/\w+)) {
193 $self->send_ans(run_cmd($self, $cmdline));
194 $self->send($self->talk_prompt);
195 } elsif ($self->{talklist} && @{$self->{talklist}}) {
196 # send what has been said to whoever is in this person's talk list
197 for (@{$self->{talklist}}) {
198 $self->send_talks($_, $cmdline);
200 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
203 $self->state('prompt');
206 $self->send_ans(run_cmd($self, $cmdline));
209 # send a prompt only if we are in a prompt state
210 $self->prompt() if $self->{state} =~ /^prompt/o;
213 # send out the talk messages taking into account vias and connectivity
216 my ($self, $ent, $line) = @_;
218 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
219 $to = $ent unless $to;
220 my $call = $via ? $via : $to;
221 my $clref = Route::get($call);
222 my $dxchan = $clref->dxchan if $clref;
224 $dxchan->talk($self->{call}, $to, $via, $line);
226 $self->send($self->msg('disc2', $via ? $via : $to));
227 my @l = grep { $_ ne $ent } @{$self->{talklist}};
229 $self->{talklist} = \@l;
231 delete $self->{talklist};
232 $self->state('prompt');
241 for (@{$self->{talklist}}) {
242 my ($to, $via) = /(\S+)>(\S+)/;
246 return $self->msg('talkprompt', join(',', @call));
250 # send a load of stuff to a command user with page prompting
258 if ($self->{pagelth} && @_ > $self->{pagelth}) {
260 for ($i = $self->{pagelth}; $i-- > 0; ) {
262 $line =~ s/\s+$//o; # why am having to do this?
265 $self->{pagedata} = [ @_ ];
266 $self->state('page');
267 $self->send($self->msg('page', scalar @_));
270 $self->send($_) if $_;
275 # this is the thing that runs the command, it is done like this for the
276 # benefit of remote command execution
282 my $user = $self->{user};
283 my $call = $self->{call};
288 my $c = qq{ \@ans = $self->{func}(\$self, \$cmdline) };
289 dbg("stored func cmd = $c\n") if isdbg('eval');
292 return ("Syserr: Eval err $errstr on stored func $self->{func}", $@);
296 return () if length $cmdline == 0;
299 $cmdline =~ s|//|/|og;
301 # split the command line up into parts, the first part is the command
302 my ($cmd, $args) = split /\s+/, $cmdline, 2;
303 $args = "" unless defined $args;
309 dbg("cmd: $cmd") if isdbg('command');
311 # alias it if possible
312 my $acmd = CmdAlias::get_cmd($cmd);
314 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
315 $args = "" unless defined $args;
316 dbg("aliased cmd: $cmd $args") if isdbg('command');
319 # first expand out the entry to a command
320 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
321 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
324 dbg("path: $cmd cmd: $fcmd") if isdbg('command');
326 my $package = find_cmd_name($path, $fcmd);
327 @ans = (0) if !$package ;
330 dbg("package: $package") if isdbg('command');
332 unless (exists $Cache{$package}->{'sub'}) {
333 $c = eval $Cache{$package}->{'eval'};
335 return DXDebug::shortmess($@);
337 $Cache{$package}->{'sub'} = $c;
339 $c = $Cache{$package}->{'sub'};
341 @ans = &{$c}($self, $args);
346 return (DXDebug::shortmess($@));
350 dbg("cmd: $cmd not found") if isdbg('command');
351 if (++$self->{errors} > $maxerrors) {
352 $self->send($self->msg('e26'));
356 return ($self->msg('e1'));
364 delete $self->{errors};
366 if (++$self->{errors} > $maxerrors) {
367 $self->send($self->msg('e26'));
376 # This is called from inside the main cluster processing loop and is used
377 # for despatching commands that are doing some long processing job
382 my @dxchan = DXChannel->get_all();
385 foreach $dxchan (@dxchan) {
386 next if $dxchan->sort ne 'U';
388 # send a prompt if no activity out on this channel
389 if ($t >= $dxchan->t + $main::user_interval) {
390 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
395 while (my ($k, $v) = each %nothereslug) {
396 if ($main::systime >= $v + 300) {
397 delete $nothereslug{$k};
403 # finish up a user context
408 my $call = $self->call;
410 my @rout = $main::routeroot->del_user($call);
411 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
413 # issue a pc17 to everybody interested
414 DXProt::route_pc17($DXProt::me, $main::routeroot, @rout) if @rout;
416 # I was the last node visited
417 $self->user->node($main::mycall);
419 # send info to all logged in thingies
420 $self->tell_login('logoutu');
422 Log('DXCommand', "$call disconnected");
424 $self->SUPER::disconnect;
428 # short cut to output a prompt
434 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
437 # broadcast a message to all users [except those mentioned after buffer]
440 my $pkg = shift; # ignored
441 my $s = shift; # the line to be rebroadcast
442 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
443 my @list = DXChannel->get_all(); # just in case we are called from some funny object
444 my ($dxchan, $except);
446 L: foreach $dxchan (@list) {
447 next if !$dxchan->sort eq 'U'; # only interested in user channels
448 foreach $except (@except) {
449 next L if $except == $dxchan; # ignore channels in the 'except' list
451 $dxchan->send($s); # send it
455 # gimme all the users
458 my @list = DXChannel->get_all();
461 foreach $ref (@list) {
462 push @out, $ref if $ref->sort eq 'U';
467 # run a script for this user
471 my $silent = shift || 0;
476 # search for the command in the cache of short->long form commands
481 my ($path, $short_cmd, $suffix) = @_;
484 # commands are lower case
485 $short_cmd = lc $short_cmd;
486 dbg("command: $path $short_cmd\n") if isdbg('command');
488 # do some checking for funny characters
489 return () if $short_cmd =~ /\/$/;
491 # return immediately if we have it
492 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
493 if ($apath && $acmd) {
494 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
495 return ($apath, $acmd);
499 my @parts = split '/', $short_cmd;
506 for ($i = 0; $i < @parts; $i++) {
508 opendir(D, $curdir) or confess "can't open $curdir $!";
512 foreach $l (sort @ls) {
514 if ($i < $#parts) { # we are dealing with directories
515 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
516 dbg("got dir: $curdir/$l\n") if isdbg('command');
521 } else { # we are dealing with commands
522 @lparts = split /\./, $l;
523 next if $lparts[$#lparts] ne $suffix; # only look for .$suffix files
524 if ($p eq substr($l, 0, length $p)) {
525 pop @lparts; # remove the suffix
526 $l = join '.', @lparts;
527 # chop $dirfn; # remove trailing /
528 $dirfn = "" unless $dirfn;
529 $cmd_cache{"$short_cmd"} = join(',', ($path, "$dirfn$l")); # cache it
530 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
531 return ($path, "$dirfn$l");
539 # clear the command name cache
546 # the persistant execution of things from the command directories
549 # This allows perl programs to call functions dynamically
551 # This has been nicked directly from the perlembed pages
554 #require Devel::Symdump;
556 sub valid_package_name {
558 $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
560 #second pass only for words starting with a digit
561 $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
563 #Dress it up as a real package name
564 $string =~ s/\//_/og;
568 # find a cmd reference
569 # this is really for use in user written stubs
571 # use the result as a symbolic reference:-
574 # @out = &$r($self, $line);
583 # first expand out the entry to a command
584 my ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
585 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
587 # make sure it is loaded
588 $r = find_cmd_name($path, $fcmd);
594 # this bit of magic finds a command in the offered directory
598 my $package = valid_package_name($cmdname);
599 my $filename = "$path/$cmdname.pl";
600 my $mtime = -M $filename;
602 # return if we can't find it
604 unless (defined $mtime) {
605 $errstr = DXM::msg('e1');
609 if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
610 #we have compiled this subroutine already,
611 #it has not been updated on disk, nothing left to do
612 #print STDERR "already compiled $package->handler\n";
616 my $sub = readfilestr($filename);
618 $errstr = "Syserr: can't open '$filename' $!";
622 #wrap the code into a subroutine inside our unique package
623 my $eval = qq( sub { $sub } );
626 my @list = split /\n/, $eval;
629 dbg($_ . "\n") if isdbg('eval');
633 $Cache{$package} = {mtime => $mtime, 'eval' => $eval };
639 # send a talk message here
642 my ($self, $from, $to, $via, $line) = @_;
643 $line =~ s/\\5E/\^/g;
644 $self->send("$to de $from: $line") if $self->{talk};
645 Log('talk', $to, $from, $main::mycall, $line);
646 # send a 'not here' message if required
647 unless ($self->{here} && $from ne $to) {
648 my $key = "$to$from";
649 unless (exists $nothereslug{$key}) {
651 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
652 my $name = $self->user->name || $to;
653 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
654 $nothereslug{$key} = $main::systime;
655 $dxchan->talk($to, $from, undef, $s);