3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
16 use POSIX qw(:math_h);
41 use Time::HiRes qw(gettimeofday tv_interval);
44 use Mojo::IOLoop::ForkCall;
48 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase %nothereslug
49 $maxbadcount $msgpolltime $default_pagelth $cmdimportdir);
51 %Cache = (); # cache of dynamically loaded routine's mod times
52 %cmd_cache = (); # cache of short names
53 $errstr = (); # error string from eval
54 %aliases = (); # aliases for (parts of) commands
55 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
56 $maxbadcount = 3; # no of bad words allowed before disconnection
57 $msgpolltime = 3600; # the time between polls for new messages
58 $cmdimportdir = "$main::root/cmd_import"; # the base directory for importing command scripts
59 # this does not exist as default, you need to create it manually
63 # obtain a new connection this is derived from dxchannel
68 my $self = DXChannel::alloc(@_);
70 # routing, this must go out here to prevent race condx
73 # my @rout = $main::routeroot->add_user($call, Route::here(1));
74 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->{conn}->peerhost], );
76 # ALWAYS output the user
77 my $ref = Route::User::get($call);
79 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref);
80 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, $ref) unless $DXProt::pc92_slug_changes;
86 # this is how a a connection starts, you get a hello message and the motd with
87 # possibly some other messages asking you to set various things up if you are
88 # new (or nearly new and slacking) user.
92 my ($self, $line, $sort) = @_;
93 my $user = $self->{user};
94 my $call = $self->{call};
95 my $name = $user->{name};
98 my $host = $self->{conn}->peerhost;
99 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
101 LogDbg('DXCommand', "$call connected from $host");
103 $self->{name} = $name ? $name : $call;
104 $self->send($self->msg('l2',$self->{name}));
105 $self->state('prompt'); # a bit of room for further expansion, passwords etc
106 $self->{priv} = $user->priv || 0;
107 $self->{lang} = $user->lang || $main::lang || 'en';
108 my $pagelth = $user->pagelth;
109 $pagelth = $default_pagelth unless defined $pagelth;
110 $self->{pagelth} = $pagelth;
111 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
112 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
113 $self->{consort} = $line; # save the connection type
115 # set some necessary flags on the user if they are connecting
116 $self->{beep} = $user->wantbeep;
117 $self->{ann} = $user->wantann;
118 $self->{wwv} = $user->wantwwv;
119 $self->{wcy} = $user->wantwcy;
120 $self->{talk} = $user->wanttalk;
121 $self->{wx} = $user->wantwx;
122 $self->{dx} = $user->wantdx;
123 $self->{logininfo} = $user->wantlogininfo;
124 $self->{ann_talk} = $user->wantann_talk;
126 $self->{prompt} = $user->prompt if $user->prompt;
127 $self->{lastmsgpoll} = 0;
129 # sort out new dx spot stuff
130 $user->wantdxcq(0) unless defined $user->{wantdxcq};
131 $user->wantdxitu(0) unless defined $user->{wantdxitu};
132 $user->wantusstate(0) unless defined $user->{wantusstate};
134 # sort out registration
135 if ($main::reqreg == 1) {
136 $self->{registered} = $user->registered;
137 } elsif ($main::reqreg == 2) {
138 $self->{registered} = !$user->registered;
140 $self->{registered} = 1;
143 # send the relevant MOTD
146 # sort out privilege reduction
147 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
151 $nossid =~ s/-\d+$//;
153 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
154 || Filter::read_in('spots', $nossid, 0)
155 || Filter::read_in('spots', 'user_default', 0);
156 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
157 || Filter::read_in('wwv', $nossid, 0)
158 || Filter::read_in('wwv', 'user_default', 0);
159 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
160 || Filter::read_in('wcy', $nossid, 0)
161 || Filter::read_in('wcy', 'user_default', 0);
162 $self->{annfilter} = Filter::read_in('ann', $call, 0)
163 || Filter::read_in('ann', $nossid, 0)
164 || Filter::read_in('ann', 'user_default', 0) ;
166 # clean up qra locators
167 my $qra = $user->qra;
168 $qra = undef if ($qra && !DXBearing::is_qra($qra));
170 my $lat = $user->lat;
171 my $long = $user->long;
172 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
176 my $echo = $user->wantecho;
178 $self->send_now('E', "0");
179 $self->send($self->msg('echow'));
180 $self->conn->echo($echo) if $self->conn->can('echo');
183 $self->tell_login('loginu');
184 $self->tell_buddies('loginb');
186 # do we need to send a forward/opernam?
187 my $lastoper = $user->lastoper || 0;
188 my $homenode = $user->homenode || "";
189 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
190 run_cmd($main::me, "forward/opernam $call");
191 $user->lastoper($main::systime + ((int rand(10)) * 86400));
194 # run a script send the output to the punter
195 my $script = new Script(lc $call) || new Script('user_default');
196 $script->run($self) if $script;
199 my $info = Route::cluster();
200 $self->send("Cluster:$info");
202 # send prompts for qth, name and things
203 $self->send($self->msg('namee1')) if !$user->name;
204 $self->send($self->msg('qthe1')) if !$user->qth;
205 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
206 $self->send($self->msg('hnodee1')) if !$user->qth;
207 $self->send($self->msg('m9')) if DXMsg::for_me($call);
209 # send out any buddy messages for other people that are online
210 foreach my $call (@{$user->buddies}) {
211 my $ref = Route::User::get($call);
213 foreach my $node ($ref->parents) {
214 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
219 $self->lastmsgpoll($main::systime);
224 # This is the normal command prompt driver
233 # save this for them's that need it
234 my $rawline = $cmdline;
236 # remove leading and trailing spaces
237 $cmdline =~ s/^\s*(.*)\s*$/$1/;
239 if ($self->{state} eq 'page') {
240 my $i = $self->{pagelth};
241 my $ref = $self->{pagedata};
244 # abort if we get a line starting in with a
245 if ($cmdline =~ /^a/io) {
250 # send a tranche of data
251 while ($i-- > 0 && @$ref) {
252 my $line = shift @$ref;
253 $line =~ s/\s+$//o; # why am having to do this?
257 # reset state if none or else chuck out an intermediate prompt
259 $tot -= $self->{pagelth};
260 $self->send($self->msg('page', $tot));
262 $self->state('prompt');
264 } elsif ($self->{state} eq 'sysop') {
265 my $passwd = $self->{user}->passwd;
267 my @pw = grep {$_ !~ /\s/} split //, $passwd;
268 my @l = @{$self->{passwd}};
269 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
270 if ($cmdline =~ /$str/) {
271 $self->{priv} = $self->{user}->priv;
273 $self->send($self->msg('sorry'));
276 $self->send($self->msg('sorry'));
278 $self->state('prompt');
279 } elsif ($self->{state} eq 'passwd') {
280 my $passwd = $self->{user}->passwd;
281 if ($passwd && $cmdline eq $passwd) {
282 $self->send($self->msg('pw1'));
283 $self->state('passwd1');
285 $self->conn->{echo} = $self->conn->{decho};
286 delete $self->conn->{decho};
287 $self->send($self->msg('sorry'));
288 $self->state('prompt');
290 } elsif ($self->{state} eq 'passwd1') {
291 $self->{passwd} = $cmdline;
292 $self->send($self->msg('pw2'));
293 $self->state('passwd2');
294 } elsif ($self->{state} eq 'passwd2') {
295 if ($cmdline eq $self->{passwd}) {
296 $self->{user}->passwd($cmdline);
297 $self->send($self->msg('pw3'));
299 $self->send($self->msg('pw4'));
301 $self->conn->{echo} = $self->conn->{decho};
302 delete $self->conn->{decho};
303 $self->state('prompt');
304 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
305 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
306 for (@{$self->{talklist}}) {
307 if ($self->{state} eq 'talk') {
308 $self->send_talks($_, $self->msg('talkend'));
310 $self->local_send('C', $self->msg('chatend', $_));
313 $self->state('prompt');
314 delete $self->{talklist};
315 } elsif ($cmdline =~ m|^/+\w+|) {
317 my $sendit = $cmdline =~ s|^/+||;
318 my @in = $self->run_cmd($cmdline);
319 $self->send_ans(@in);
320 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
321 foreach my $l (@in) {
323 if (@bad = BadWords::check($l)) {
324 $self->badcount(($self->badcount||0) + @bad);
325 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
327 for (@{$self->{talklist}}) {
328 if ($self->{state} eq 'talk') {
329 $self->send_talks($_, $l);
331 send_chats($self, $_, $l)
337 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
338 } elsif ($self->{talklist} && @{$self->{talklist}}) {
339 # send what has been said to whoever is in this person's talk list
341 if (@bad = BadWords::check($cmdline)) {
342 $self->badcount(($self->badcount||0) + @bad);
343 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
345 for (@{$self->{talklist}}) {
346 if ($self->{state} eq 'talk') {
347 $self->send_talks($_, $rawline);
349 send_chats($self, $_, $rawline);
353 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
354 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
357 $self->state('prompt');
359 } elsif (my $func = $self->{func}) {
362 if (ref $self->{edit}) {
363 eval { @ans = $self->{edit}->$func($self, $rawline)};
365 eval { @ans = &{$self->{func}}($self, $rawline) };
368 $self->send_ans("Syserr: on stored func $self->{func}", $@);
369 delete $self->{func};
370 $self->state('prompt');
373 $self->send_ans(@ans);
375 $self->send_ans(run_cmd($self, $cmdline));
378 # check for excessive swearing
379 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
380 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
385 # send a prompt only if we are in a prompt state
386 $self->prompt() if $self->{state} =~ /^prompt/o;
389 # send out the talk messages taking into account vias and connectivity
392 my ($self, $ent, $line) = @_;
394 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
395 $to = $ent unless $to;
396 my $call = $via && $via ne '*' ? $via : $to;
397 my $clref = Route::get($call);
398 my $dxchan = $clref->dxchan if $clref;
400 $dxchan->talk($self->{call}, $to, undef, $line);
402 $self->send($self->msg('disc2', $via ? $via : $to));
403 my @l = grep { $_ ne $ent } @{$self->{talklist}};
405 $self->{talklist} = \@l;
407 delete $self->{talklist};
408 $self->state('prompt');
419 my $msgid = DXProt::nextchatmsgid();
420 $text = "#$msgid $text";
421 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
429 for (@{$self->{talklist}}) {
430 my ($to, $via) = /(\S+)>(\S+)/;
434 return $self->msg($prompt, join(',', @call));
440 return $self->special_prompt('talkprompt');
446 return $self->special_prompt('chatprompt');
450 # send a load of stuff to a command user with page prompting
458 if ($self->{pagelth} && @_ > $self->{pagelth}) {
460 for ($i = $self->{pagelth}; $i-- > 0; ) {
462 $line =~ s/\s+$//o; # why am having to do this?
465 $self->{pagedata} = [ @_ ];
466 $self->state('page');
467 $self->send($self->msg('page', scalar @_));
480 # this is the thing that runs the command, it is done like this for the
481 # benefit of remote command execution
487 my $user = $self->{user};
488 my $call = $self->{call};
492 return () if length $cmdline == 0;
494 # split the command line up into parts, the first part is the command
495 my ($cmd, $args) = split /\s+/, $cmdline, 2;
496 $args = "" unless defined $args;
501 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
502 LogDbg('DXCommand', "cmd: invalid characters in '$cmd'");
503 return $self->_error_out('e1');
506 # strip out // on command only
511 dbg("cmd: $cmd") if isdbg('command');
513 # alias it if possible
514 my $acmd = CmdAlias::get_cmd($cmd);
516 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
517 $args = "" unless defined $args;
518 dbg("cmd: aliased $cmd $args") if isdbg('command');
521 # first expand out the entry to a command
522 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
523 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
526 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
528 my $package = find_cmd_name($path, $fcmd);
531 if ($package && $self->can("${package}::handle")) {
533 dbg("cmd: package $package") if isdbg('command');
534 eval { @ans = &{"${package}::handle"}($self, $args) };
535 return (DXDebug::shortmess($@)) if $@;
537 dbg("cmd: $package not present") if isdbg('command');
538 return $self->_error_out('e1');
541 dbg("cmd: $cmd not found") if isdbg('command');
542 return $self->_error_out('e1');
548 delete $self->{errors};
550 if (++$self->{errors} > $DXChannel::maxerrors) {
551 $self->send($self->msg('e26'));
556 return map {s/([^\s])\s+$/$1/; $_} @ans;
560 # This is called from inside the main cluster processing loop and is used
561 # for despatching commands that are doing some long processing job
566 my @dxchan = DXChannel::get_all();
569 foreach $dxchan (@dxchan) {
570 next unless $dxchan->{sort} eq 'U';
572 # send a outstanding message prompt if required
573 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
574 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
575 $dxchan->lastmsgpoll($t);
578 # send a prompt if no activity out on this channel
579 if ($t >= $dxchan->t + $main::user_interval) {
580 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
585 while (my ($k, $v) = each %nothereslug) {
586 if ($main::systime >= $v + 300) {
587 delete $nothereslug{$k};
595 # finish up a user context
600 my $call = $self->call;
602 return if $self->{disconnecting}++;
604 delete $self->{senddbg};
606 my $uref = Route::User::get($call);
609 # @rout = $main::routeroot->del_user($uref);
610 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
612 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
614 # issue a pc17 to everybody interested
615 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
616 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
618 confess "trying to disconnect a non existant user $call";
621 # I was the last node visited
622 $self->user->node($main::mycall);
624 # send info to all logged in thingies
625 $self->tell_login('logoutu');
626 $self->tell_buddies('logoutb');
628 LogDbg('DXCommand', "$call disconnected");
630 $self->SUPER::disconnect;
634 # short cut to output a prompt
641 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
643 my $call = $self->call;
644 my $date = cldate($main::systime);
645 my $time = ztime($main::systime);
646 my $prompt = $self->{prompt} || $self->msg('pr');
648 $call = "($call)" unless $self->here;
649 $prompt =~ s/\%C/$call/g;
650 $prompt =~ s/\%D/$date/g;
651 $prompt =~ s/\%T/$time/g;
652 $prompt =~ s/\%M/$main::mycall/g;
654 $self->send($prompt);
657 # broadcast a message to all users [except those mentioned after buffer]
660 my $pkg = shift; # ignored
661 my $s = shift; # the line to be rebroadcast
663 foreach my $dxchan (DXChannel::get_all()) {
664 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
665 next if grep $dxchan == $_, @_;
666 $dxchan->send($s); # send it
670 # gimme all the users
673 return grep {$_->{sort} eq 'U'} DXChannel::get_all();
676 # run a script for this user
680 my $silent = shift || 0;
685 # search for the command in the cache of short->long form commands
690 my ($path, $short_cmd, $suffix) = @_;
693 # commands are lower case
694 $short_cmd = lc $short_cmd;
695 dbg("command: $path $short_cmd\n") if isdbg('command');
697 # do some checking for funny characters
698 return () if $short_cmd =~ /\/$/;
700 # return immediately if we have it
701 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
702 if ($apath && $acmd) {
703 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
704 return ($apath, $acmd);
708 my @parts = split '/', $short_cmd;
712 while (my $p = shift @parts) {
713 opendir(D, $curdir) or confess "can't open $curdir $!";
717 # if this isn't the last part
720 foreach my $l (sort @ls) {
722 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
723 dbg("got dir: $curdir/$l\n") if isdbg('command');
730 # only proceed if we find the directory asked for
731 return () unless $found;
733 foreach my $l (sort @ls) {
735 next unless $l =~ /\.$suffix$/;
736 if ($p eq substr($l, 0, length $p)) {
737 $l =~ s/\.$suffix$//;
738 $dirfn = "" unless $dirfn;
739 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
740 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
741 return ($path, "$dirfn$l");
750 # clear the command name cache
755 for my $k (keys %Cache) {
756 unless ($k =~ /cmd_cache/) {
757 dbg("Undefining cmd $k") if isdbg('command');
758 undef $DXCommandmode::{"${k}::"};
762 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
766 # the persistant execution of things from the command directories
769 # This allows perl programs to call functions dynamically
771 # This has been nicked directly from the perlembed pages
773 #require Devel::Symdump;
775 sub valid_package_name {
777 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
780 return "cmd_$string";
784 # this bit of magic finds a command in the offered directory
788 my $package = valid_package_name($cmdname);
789 my $filename = "$path/$cmdname.pl";
790 my $mtime = -M $filename;
792 # return if we can't find it
794 unless (defined $mtime) {
795 $errstr = DXM::msg('e1');
799 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
800 #we have compiled this subroutine already,
801 #it has not been updated on disk, nothing left to do
802 #print STDERR "already compiled $package->handler\n";
803 dbg("find_cmd_name: $package cached") if isdbg('command');
806 my $sub = readfilestr($filename);
808 $errstr = "Syserr: can't open '$filename' $!";
812 #wrap the code into a subroutine inside our unique package
813 my $eval = qq(package DXCommandmode::$package; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
816 if ($sub =~ m|\s*sub\s+handle\n|) {
819 $eval .= qq(sub handle { $sub });
823 my @list = split /\n/, $eval;
826 dbg($_ . "\n") if isdbg('eval');
830 # get rid of any existing sub and try to compile the new one
833 if (exists $Cache{$package}) {
834 dbg("find_cmd_name: Redefining $package") if isdbg('command');
835 undef $DXCommandmode::{"${package}::"};
836 delete $Cache{$package};
838 dbg("find_cmd_name: Defining $package") if isdbg('command');
843 $Cache{$package} = {mtime => $mtime } unless $@;
846 return "DXCommandmode::$package";
854 $self->SUPER::send(dd(['cmd',$_]));
857 $self->SUPER::send(@_);
863 my ($self, $let, $buf) = @_;
864 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
865 if ($self->{enhanced}) {
866 $self->send_later($let, $buf);
875 # send a talk message here
878 my ($self, $from, $to, $via, $line, $onode) = @_;
879 $line =~ s/\\5E/\^/g;
882 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
884 $self->local_send('T', "$to de $from: $line");
887 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
888 # send a 'not here' message if required
889 unless ($self->{here} && $from ne $to) {
890 my $key = "$to$from";
891 unless (exists $nothereslug{$key}) {
893 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
894 my $name = $self->user->name || $to;
895 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
896 $nothereslug{$key} = $main::systime;
897 $dxchan->talk($to, $from, undef, $s);
914 if (!$self->{ann_talk} && $to ne $self->{call}) {
915 my $call = AnnTalk::is_talk_candidate($_[0], $text);
919 if ($self->{annfilter}) {
920 ($filter, $hops) = $self->{annfilter}->it(@_ );
921 return unless $filter;
924 unless ($self->{ann}) {
925 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
927 return if $target eq 'SYSOP' && $self->{priv} < 5;
930 $buf = dd(['ann', $to, $target, $text, @_])
932 $buf = "$to$target de $_[0]: $text";
934 $buf .= "\a\a" if $self->{beep};
936 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
950 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
952 $text =~ s/^\#\d+ //;
955 $buf = dd(['chat', $to, $target, $text, @_])
957 $buf = "$target de $_[0]: $text";
959 $buf .= "\a\a" if $self->{beep};
961 $self->local_send('C', $buf);
968 my $t = ztime($_[2]);
970 my $clth = $self->{consort} eq 'local' ? 29 : 30;
971 my $comment = substr (($_[3] || ''), 0, $clth);
972 $comment .= ' ' x ($clth - length($comment));
973 if ($self->{user}->wantgrid) {
974 my $ref = DXUser::get_current($_[4]);
976 $loc = $ref->qra || '';
977 $loc = ' ' . substr($loc, 0, 4) if $loc;
981 if ($self->{user}->wantdxitu) {
982 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
983 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
984 } elsif ($self->{user}->wantdxcq) {
985 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
986 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
987 } elsif ($self->{user}->wantusstate) {
988 $loc = ' ' . $_[13] if $_[13];
989 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
992 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1000 my $isolate = shift;
1001 return unless $self->{dx};
1003 my ($filter, $hops);
1005 if ($self->{spotsfilter}) {
1006 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1007 return unless $filter;
1010 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1013 if ($self->{ve7cc}) {
1014 $buf = VE7CC::dx_spot($self, @_);
1015 } elsif ($self->{gtk}) {
1016 my ($dxloc, $byloc);
1018 my $ref = DXUser::get_current($_[4]);
1021 $byloc = substr($byloc, 0, 4) if $byloc;
1025 $spot =~ s|/\w{1,4}$||;
1026 $ref = DXUser::get_current($spot);
1029 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1031 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1034 $buf = $self->format_dx_spot(@_);
1035 $buf .= "\a\a" if $self->{beep};
1039 $self->local_send('X', $buf);
1046 my $isolate = shift;
1047 my ($filter, $hops);
1049 return unless $self->{wwv};
1051 if ($self->{wwvfilter}) {
1052 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1053 return unless $filter;
1058 $buf = dd(['wwv', @_])
1060 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1061 $buf .= "\a\a" if $self->{beep};
1064 $self->local_send('V', $buf);
1071 my $isolate = shift;
1072 my ($filter, $hops);
1074 return unless $self->{wcy};
1076 if ($self->{wcyfilter}) {
1077 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1078 return unless $filter;
1083 $buf = dd(['wcy', @_])
1085 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1086 $buf .= "\a\a" if $self->{beep};
1088 $self->local_send('Y', $buf);
1091 # broadcast debug stuff to all interested parties
1094 my $s = shift; # the line to be rebroadcast
1096 foreach my $dxchan (DXChannel::get_all_users) {
1097 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1098 if ($dxchan->{gtk}) {
1099 $dxchan->send_later('L', dd(['db', $s]));
1101 $dxchan->send_later('L', $s);
1112 if ($self->state eq 'enterbody') {
1113 my $loc = $self->{loc} || confess "local var gone missing" ;
1114 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1116 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1118 $self->state('prompt');
1119 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1120 push @out, $self->msg('m10');
1121 delete $loc->{lines};
1122 delete $self->{loc};
1124 $self->state('prompt');
1126 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1127 # i.e. it ain't and end or abort, therefore store the line
1130 confess "Invalid state $self->{state}";
1135 sub store_startup_script
1138 my $loc = $self->{loc} || confess "local var gone missing" ;
1140 my $call = $loc->{call} || confess "callsign gone missing";
1141 confess "lines array gone missing" unless ref $loc->{lines};
1142 my $r = Script::store($call, $loc->{lines});
1145 push @out, $self->msg('m19', $call, $r);
1147 push @out, $self->msg('m20', $call);
1150 push @out, "error opening startup script $call $!";
1155 # Import any commands contained in any files in import_cmd directory
1157 # If the filename has a recogisable callsign as some delimited part
1158 # of it, then this is the user the command will be run as.
1162 # are there any to do in this directory?
1163 return unless -d $cmdimportdir;
1164 unless (opendir(DIR, $cmdimportdir)) {
1165 LogDbg('err', "can\'t open $cmdimportdir $!");
1169 my @names = readdir(DIR);
1173 return unless @names;
1175 foreach $name (@names) {
1176 next if $name =~ /^\./;
1178 my $s = Script->new($name, $cmdimportdir);
1180 LogDbg('DXCommand', "Run import cmd file $name");
1181 my @cat = split /[^A-Za-z0-9]+/, $name;
1182 my ($call) = grep {is_callsign(uc $_)} @cat;
1183 $call ||= $main::mycall;
1188 $s->inscript(0); # switch off script checks
1190 if ($call eq $main::mycall) {
1191 @out = $s->run($main::me, 1);
1193 my $dxchan = DXChannel::get($call);
1195 @out = $s->run($dxchan, 1);
1197 my $u = DXUser::get($call);
1199 $dxchan = $main::me;
1200 my $old = $dxchan->{call};
1201 my $priv = $dxchan->{priv};
1202 my $user = $dxchan->{user};
1203 $dxchan->{call} = $call;
1204 $dxchan->{priv} = $u->priv;
1205 $dxchan->{user} = $u;
1206 @out = $s->run($dxchan, 1);
1207 $dxchan->{call} = $old;
1208 $dxchan->{priv} = $priv;
1209 $dxchan->{user} = $user;
1211 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1217 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1220 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1221 unlink "$cmdimportdir/$name";
1226 sub print_find_reply
1228 my ($self, $node, $target, $flag, $ms) = @_;
1229 my $sort = $flag == 2 ? "External" : "Local";
1230 $self->send("$sort $target found at $node in $ms ms" );
1233 # send the most relevant motd
1239 unless ($self->{registered}) {
1240 $motd = "${main::motd}_nor_$self->{lang}";
1241 $motd = "${main::motd}_nor" unless -e $motd;
1243 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1244 $motd = $main::motd unless $motd && -e $motd;
1245 if ($self->conn->ax25) {
1247 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1249 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1252 $self->send_file($motd) if -e $motd;
1257 return unless isdbg('chan');
1261 my $tb = shift || [gettimeofday];
1263 my $a = int($ta->[0] * 1000) + int($ta->[1] / 1000);
1264 my $b = int($tb->[0] * 1000) + int($tb->[1] / 1000);
1265 my $msecs = $b - $a;
1267 my $s = "forkcall stats: $call '$line' ";
1272 # Punt off a long running command into a separate process
1274 # This is called from commands to run some potentially long running
1275 # function. The process forks and then runs the function and returns
1276 # the result back to the cmd.
1278 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1279 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1280 # THE CURRENT CONTEXT!!
1282 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1288 my $call = $self->{call};
1292 my $cb = delete $args{cb};
1293 my $prefix = delete $args{prefix};
1294 my $progress = delete $args{progress};
1295 my $args = delete $args{args} || [];
1296 my $t0 = [gettimeofday];
1300 my $fc = Mojo::IOLoop::ForkCall->new;
1301 $fc->serializer(\&encode_json);
1302 $fc->deserializer(\&decode_json);
1304 sub {my @args = @_; my @res = $cmdref->(@args); return @res},
1307 my ($fc, $err, @res) = @_;
1308 my $dxchan = DXChannel::get($call);
1309 return unless $dxchan;
1312 my $s = "DXCommand::spawn_cmd: call $call error $err";
1313 dbg($s) if isdbg('chan');
1318 $cb->($dxchan, @res);
1321 if (defined $prefix) {
1322 $dxchan->send(map {"$prefix$_"} @res);
1324 $dxchan->send(@res);
1327 _diffms($call, $line, $t0);