3 # This module impliments the user facing command mode for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 package DXCommandmode;
18 use POSIX qw(:math_h);
43 use Time::HiRes qw(gettimeofday tv_interval);
46 use Mojo::IOLoop::ForkCall;
50 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase %nothereslug
51 $maxbadcount $msgpolltime $default_pagelth $cmdimportdir);
53 %Cache = (); # cache of dynamically loaded routine's mod times
54 %cmd_cache = (); # cache of short names
55 $errstr = (); # error string from eval
56 %aliases = (); # aliases for (parts of) commands
57 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
58 $maxbadcount = 3; # no of bad words allowed before disconnection
59 $msgpolltime = 3600; # the time between polls for new messages
60 $cmdimportdir = "$main::root/cmd_import"; # the base directory for importing command scripts
61 # this does not exist as default, you need to create it manually
65 # obtain a new connection this is derived from dxchannel
70 my $self = DXChannel::alloc(@_);
72 # routing, this must go out here to prevent race condx
75 # my @rout = $main::routeroot->add_user($call, Route::here(1));
76 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->{conn}->peerhost], );
78 # ALWAYS output the user
79 my $ref = Route::User::get($call);
81 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref);
82 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, $ref) unless $DXProt::pc92_slug_changes;
88 # this is how a a connection starts, you get a hello message and the motd with
89 # possibly some other messages asking you to set various things up if you are
90 # new (or nearly new and slacking) user.
94 my ($self, $line, $sort) = @_;
95 my $user = $self->{user};
96 my $call = $self->{call};
97 my $name = $user->{name};
100 my $host = $self->{conn}->peerhost;
101 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
103 LogDbg('DXCommand', "$call connected from $host");
105 $self->{name} = $name ? $name : $call;
106 $self->send($self->msg('l2',$self->{name}));
107 $self->state('prompt'); # a bit of room for further expansion, passwords etc
108 $self->{priv} = $user->priv || 0;
109 $self->{lang} = $user->lang || $main::lang || 'en';
110 my $pagelth = $user->pagelth;
111 $pagelth = $default_pagelth unless defined $pagelth;
112 $self->{pagelth} = $pagelth;
113 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
114 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
115 $self->{consort} = $line; # save the connection type
117 # set some necessary flags on the user if they are connecting
118 $self->{beep} = $user->wantbeep;
119 $self->{ann} = $user->wantann;
120 $self->{wwv} = $user->wantwwv;
121 $self->{wcy} = $user->wantwcy;
122 $self->{talk} = $user->wanttalk;
123 $self->{wx} = $user->wantwx;
124 $self->{dx} = $user->wantdx;
125 $self->{logininfo} = $user->wantlogininfo;
126 $self->{ann_talk} = $user->wantann_talk;
128 $self->{prompt} = $user->prompt if $user->prompt;
129 $self->{lastmsgpoll} = 0;
131 # sort out new dx spot stuff
132 $user->wantdxcq(0) unless defined $user->{wantdxcq};
133 $user->wantdxitu(0) unless defined $user->{wantdxitu};
134 $user->wantusstate(0) unless defined $user->{wantusstate};
136 # sort out registration
137 if ($main::reqreg == 1) {
138 $self->{registered} = $user->registered;
139 } elsif ($main::reqreg == 2) {
140 $self->{registered} = !$user->registered;
142 $self->{registered} = 1;
145 # send the relevant MOTD
148 # sort out privilege reduction
149 $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
153 $nossid =~ s/-\d+$//;
155 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
156 || Filter::read_in('spots', $nossid, 0)
157 || Filter::read_in('spots', 'user_default', 0);
158 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
159 || Filter::read_in('wwv', $nossid, 0)
160 || Filter::read_in('wwv', 'user_default', 0);
161 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
162 || Filter::read_in('wcy', $nossid, 0)
163 || Filter::read_in('wcy', 'user_default', 0);
164 $self->{annfilter} = Filter::read_in('ann', $call, 0)
165 || Filter::read_in('ann', $nossid, 0)
166 || Filter::read_in('ann', 'user_default', 0) ;
168 # clean up qra locators
169 my $qra = $user->qra;
170 $qra = undef if ($qra && !DXBearing::is_qra($qra));
172 my $lat = $user->lat;
173 my $long = $user->long;
174 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
178 my $echo = $user->wantecho;
180 $self->send_now('E', "0");
181 $self->send($self->msg('echow'));
182 $self->conn->echo($echo) if $self->conn->can('echo');
185 $self->tell_login('loginu');
186 $self->tell_buddies('loginb');
188 # do we need to send a forward/opernam?
189 my $lastoper = $user->lastoper || 0;
190 my $homenode = $user->homenode || "";
191 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
192 run_cmd($main::me, "forward/opernam $call");
193 $user->lastoper($main::systime + ((int rand(10)) * 86400));
196 # run a script send the output to the punter
197 my $script = new Script(lc $call) || new Script('user_default');
198 $script->run($self) if $script;
201 my $info = Route::cluster();
202 $self->send("Cluster:$info");
204 # send prompts for qth, name and things
205 $self->send($self->msg('namee1')) if !$user->name;
206 $self->send($self->msg('qthe1')) if !$user->qth;
207 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
208 $self->send($self->msg('hnodee1')) if !$user->qth;
209 $self->send($self->msg('m9')) if DXMsg::for_me($call);
211 # send out any buddy messages for other people that are online
212 foreach my $call (@{$user->buddies}) {
213 my $ref = Route::User::get($call);
215 foreach my $node ($ref->parents) {
216 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
221 $self->lastmsgpoll($main::systime);
226 # This is the normal command prompt driver
235 # save this for them's that need it
236 my $rawline = $cmdline;
238 # remove leading and trailing spaces
239 $cmdline =~ s/^\s*(.*)\s*$/$1/;
241 if ($self->{state} eq 'page') {
242 my $i = $self->{pagelth};
243 my $ref = $self->{pagedata};
246 # abort if we get a line starting in with a
247 if ($cmdline =~ /^a/io) {
252 # send a tranche of data
253 while ($i-- > 0 && @$ref) {
254 my $line = shift @$ref;
255 $line =~ s/\s+$//o; # why am having to do this?
259 # reset state if none or else chuck out an intermediate prompt
261 $tot -= $self->{pagelth};
262 $self->send($self->msg('page', $tot));
264 $self->state('prompt');
266 } elsif ($self->{state} eq 'sysop') {
267 my $passwd = $self->{user}->passwd;
269 my @pw = grep {$_ !~ /\s/} split //, $passwd;
270 my @l = @{$self->{passwd}};
271 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
272 if ($cmdline =~ /$str/) {
273 $self->{priv} = $self->{user}->priv;
275 $self->send($self->msg('sorry'));
278 $self->send($self->msg('sorry'));
280 $self->state('prompt');
281 } elsif ($self->{state} eq 'passwd') {
282 my $passwd = $self->{user}->passwd;
283 if ($passwd && $cmdline eq $passwd) {
284 $self->send($self->msg('pw1'));
285 $self->state('passwd1');
287 $self->conn->{echo} = $self->conn->{decho};
288 delete $self->conn->{decho};
289 $self->send($self->msg('sorry'));
290 $self->state('prompt');
292 } elsif ($self->{state} eq 'passwd1') {
293 $self->{passwd} = $cmdline;
294 $self->send($self->msg('pw2'));
295 $self->state('passwd2');
296 } elsif ($self->{state} eq 'passwd2') {
297 if ($cmdline eq $self->{passwd}) {
298 $self->{user}->passwd($cmdline);
299 $self->send($self->msg('pw3'));
301 $self->send($self->msg('pw4'));
303 $self->conn->{echo} = $self->conn->{decho};
304 delete $self->conn->{decho};
305 $self->state('prompt');
306 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
307 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
308 for (@{$self->{talklist}}) {
309 if ($self->{state} eq 'talk') {
310 $self->send_talks($_, $self->msg('talkend'));
312 $self->local_send('C', $self->msg('chatend', $_));
315 $self->state('prompt');
316 delete $self->{talklist};
317 } elsif ($cmdline =~ m|^/+\w+|) {
319 my $sendit = $cmdline =~ s|^/+||;
320 my @in = $self->run_cmd($cmdline);
321 $self->send_ans(@in);
322 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
323 foreach my $l (@in) {
325 if (@bad = BadWords::check($l)) {
326 $self->badcount(($self->badcount||0) + @bad);
327 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
329 for (@{$self->{talklist}}) {
330 if ($self->{state} eq 'talk') {
331 $self->send_talks($_, $l);
333 send_chats($self, $_, $l)
339 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
340 } elsif ($self->{talklist} && @{$self->{talklist}}) {
341 # send what has been said to whoever is in this person's talk list
343 if (@bad = BadWords::check($cmdline)) {
344 $self->badcount(($self->badcount||0) + @bad);
345 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
347 for (@{$self->{talklist}}) {
348 if ($self->{state} eq 'talk') {
349 $self->send_talks($_, $rawline);
351 send_chats($self, $_, $rawline);
355 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
356 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
359 $self->state('prompt');
361 } elsif (my $func = $self->{func}) {
364 if (ref $self->{edit}) {
365 eval { @ans = $self->{edit}->$func($self, $rawline)};
367 eval { @ans = &{$self->{func}}($self, $rawline) };
370 $self->send_ans("Syserr: on stored func $self->{func}", $@);
371 delete $self->{func};
372 $self->state('prompt');
375 $self->send_ans(@ans);
377 $self->send_ans(run_cmd($self, $cmdline));
380 # check for excessive swearing
381 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
382 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
387 # send a prompt only if we are in a prompt state
388 $self->prompt() if $self->{state} =~ /^prompt/o;
391 # send out the talk messages taking into account vias and connectivity
394 my ($self, $ent, $line) = @_;
396 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
397 $to = $ent unless $to;
398 my $call = $via && $via ne '*' ? $via : $to;
399 my $clref = Route::get($call);
400 my $dxchan = $clref->dxchan if $clref;
402 $dxchan->talk($self->{call}, $to, undef, $line);
404 $self->send($self->msg('disc2', $via ? $via : $to));
405 my @l = grep { $_ ne $ent } @{$self->{talklist}};
407 $self->{talklist} = \@l;
409 delete $self->{talklist};
410 $self->state('prompt');
421 my $msgid = DXProt::nextchatmsgid();
422 $text = "#$msgid $text";
423 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
431 for (@{$self->{talklist}}) {
432 my ($to, $via) = /(\S+)>(\S+)/;
436 return $self->msg($prompt, join(',', @call));
442 return $self->special_prompt('talkprompt');
448 return $self->special_prompt('chatprompt');
452 # send a load of stuff to a command user with page prompting
460 if ($self->{pagelth} && @_ > $self->{pagelth}) {
462 for ($i = $self->{pagelth}; $i-- > 0; ) {
464 $line =~ s/\s+$//o; # why am having to do this?
467 $self->{pagedata} = [ @_ ];
468 $self->state('page');
469 $self->send($self->msg('page', scalar @_));
482 # this is the thing that runs the command, it is done like this for the
483 # benefit of remote command execution
489 my $user = $self->{user};
490 my $call = $self->{call};
494 return () if length $cmdline == 0;
496 # split the command line up into parts, the first part is the command
497 my ($cmd, $args) = split /\s+/, $cmdline, 2;
498 $args = "" unless defined $args;
503 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
504 LogDbg('DXCommand', "cmd: invalid characters in '$cmd'");
505 return $self->_error_out('e1');
508 # strip out // on command only
513 dbg("cmd: $cmd") if isdbg('command');
515 # alias it if possible
516 my $acmd = CmdAlias::get_cmd($cmd);
518 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
519 $args = "" unless defined $args;
520 dbg("cmd: aliased $cmd $args") if isdbg('command');
523 # first expand out the entry to a command
524 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
525 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
528 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
530 my $package = find_cmd_name($path, $fcmd);
533 if ($package && $self->can("${package}::handle")) {
535 dbg("cmd: package $package") if isdbg('command');
536 eval { @ans = &{"${package}::handle"}($self, $args) };
537 return (DXDebug::shortmess($@)) if $@;
539 dbg("cmd: $package not present") if isdbg('command');
540 return $self->_error_out('e1');
543 dbg("cmd: $cmd not found") if isdbg('command');
544 return $self->_error_out('e1');
550 delete $self->{errors};
552 if (++$self->{errors} > $DXChannel::maxerrors) {
553 $self->send($self->msg('e26'));
558 return map {s/([^\s])\s+$/$1/; $_} @ans;
562 # This is called from inside the main cluster processing loop and is used
563 # for despatching commands that are doing some long processing job
568 my @dxchan = DXChannel::get_all();
571 foreach $dxchan (@dxchan) {
572 next unless $dxchan->{sort} eq 'U';
574 # send a outstanding message prompt if required
575 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
576 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
577 $dxchan->lastmsgpoll($t);
580 # send a prompt if no activity out on this channel
581 if ($t >= $dxchan->t + $main::user_interval) {
582 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
587 while (my ($k, $v) = each %nothereslug) {
588 if ($main::systime >= $v + 300) {
589 delete $nothereslug{$k};
597 # finish up a user context
602 my $call = $self->call;
604 return if $self->{disconnecting}++;
606 delete $self->{senddbg};
608 my $uref = Route::User::get($call);
611 # @rout = $main::routeroot->del_user($uref);
612 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
614 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
616 # issue a pc17 to everybody interested
617 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
618 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
620 confess "trying to disconnect a non existant user $call";
623 # I was the last node visited
624 $self->user->node($main::mycall);
626 # send info to all logged in thingies
627 $self->tell_login('logoutu');
628 $self->tell_buddies('logoutb');
630 LogDbg('DXCommand', "$call disconnected");
632 $self->SUPER::disconnect;
636 # short cut to output a prompt
643 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
645 my $call = $self->call;
646 my $date = cldate($main::systime);
647 my $time = ztime($main::systime);
648 my $prompt = $self->{prompt} || $self->msg('pr');
650 $call = "($call)" unless $self->here;
651 $prompt =~ s/\%C/$call/g;
652 $prompt =~ s/\%D/$date/g;
653 $prompt =~ s/\%T/$time/g;
654 $prompt =~ s/\%M/$main::mycall/g;
656 $self->send($prompt);
659 # broadcast a message to all users [except those mentioned after buffer]
662 my $pkg = shift; # ignored
663 my $s = shift; # the line to be rebroadcast
665 foreach my $dxchan (DXChannel::get_all()) {
666 next unless $dxchan->{sort} eq 'U'; # only interested in user channels
667 next if grep $dxchan == $_, @_;
668 $dxchan->send($s); # send it
672 # gimme all the users
675 return grep {$_->{sort} eq 'U'} DXChannel::get_all();
678 # run a script for this user
682 my $silent = shift || 0;
687 # search for the command in the cache of short->long form commands
692 my ($path, $short_cmd, $suffix) = @_;
695 # commands are lower case
696 $short_cmd = lc $short_cmd;
697 dbg("command: $path $short_cmd\n") if isdbg('command');
699 # do some checking for funny characters
700 return () if $short_cmd =~ /\/$/;
702 # return immediately if we have it
703 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
704 if ($apath && $acmd) {
705 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
706 return ($apath, $acmd);
710 my @parts = split '/', $short_cmd;
714 while (my $p = shift @parts) {
715 opendir(D, $curdir) or confess "can't open $curdir $!";
719 # if this isn't the last part
722 foreach my $l (sort @ls) {
724 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
725 dbg("got dir: $curdir/$l\n") if isdbg('command');
732 # only proceed if we find the directory asked for
733 return () unless $found;
735 foreach my $l (sort @ls) {
737 next unless $l =~ /\.$suffix$/;
738 if ($p eq substr($l, 0, length $p)) {
739 $l =~ s/\.$suffix$//;
740 $dirfn = "" unless $dirfn;
741 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
742 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
743 return ($path, "$dirfn$l");
752 # clear the command name cache
757 for my $k (keys %Cache) {
758 unless ($k =~ /cmd_cache/) {
759 dbg("Undefining cmd $k") if isdbg('command');
760 undef $DXCommandmode::{"${k}::"};
764 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
768 # the persistant execution of things from the command directories
771 # This allows perl programs to call functions dynamically
773 # This has been nicked directly from the perlembed pages
775 #require Devel::Symdump;
777 sub valid_package_name {
779 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
782 return "cmd_$string";
786 # this bit of magic finds a command in the offered directory
790 my $package = valid_package_name($cmdname);
791 my $filename = "$path/$cmdname.pl";
792 my $mtime = -M $filename;
794 # return if we can't find it
796 unless (defined $mtime) {
797 $errstr = DXM::msg('e1');
801 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
802 #we have compiled this subroutine already,
803 #it has not been updated on disk, nothing left to do
804 #print STDERR "already compiled $package->handler\n";
805 dbg("find_cmd_name: $package cached") if isdbg('command');
808 my $sub = readfilestr($filename);
810 $errstr = "Syserr: can't open '$filename' $!";
814 #wrap the code into a subroutine inside our unique package
815 my $eval = qq(package DXCommandmode::$package; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
818 if ($sub =~ m|\s*sub\s+handle\n|) {
821 $eval .= qq(sub handle { $sub });
825 my @list = split /\n/, $eval;
828 dbg($_ . "\n") if isdbg('eval');
832 # get rid of any existing sub and try to compile the new one
835 if (exists $Cache{$package}) {
836 dbg("find_cmd_name: Redefining $package") if isdbg('command');
837 undef $DXCommandmode::{"${package}::"};
838 delete $Cache{$package};
840 dbg("find_cmd_name: Defining $package") if isdbg('command');
845 $Cache{$package} = {mtime => $mtime } unless $@;
848 return "DXCommandmode::$package";
856 $self->SUPER::send(dd(['cmd',$_]));
859 $self->SUPER::send(@_);
865 my ($self, $let, $buf) = @_;
866 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
867 if ($self->{enhanced}) {
868 $self->send_later($let, $buf);
877 # send a talk message here
880 my ($self, $from, $to, $via, $line, $onode) = @_;
881 $line =~ s/\\5E/\^/g;
884 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
886 $self->local_send('T', "$to de $from: $line");
889 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
890 # send a 'not here' message if required
891 unless ($self->{here} && $from ne $to) {
892 my $key = "$to$from";
893 unless (exists $nothereslug{$key}) {
895 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
896 my $name = $self->user->name || $to;
897 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
898 $nothereslug{$key} = $main::systime;
899 $dxchan->talk($to, $from, undef, $s);
916 if (!$self->{ann_talk} && $to ne $self->{call}) {
917 my $call = AnnTalk::is_talk_candidate($_[0], $text);
921 if ($self->{annfilter}) {
922 ($filter, $hops) = $self->{annfilter}->it(@_ );
923 return unless $filter;
926 unless ($self->{ann}) {
927 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
929 return if $target eq 'SYSOP' && $self->{priv} < 5;
932 $buf = dd(['ann', $to, $target, $text, @_])
934 $buf = "$to$target de $_[0]: $text";
936 $buf .= "\a\a" if $self->{beep};
938 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
952 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
954 $text =~ s/^\#\d+ //;
957 $buf = dd(['chat', $to, $target, $text, @_])
959 $buf = "$target de $_[0]: $text";
961 $buf .= "\a\a" if $self->{beep};
963 $self->local_send('C', $buf);
970 my $t = ztime($_[2]);
972 my $clth = $self->{consort} eq 'local' ? 29 : 30;
973 my $comment = substr (($_[3] || ''), 0, $clth);
974 $comment .= ' ' x ($clth - length($comment));
975 if ($self->{user}->wantgrid) {
976 my $ref = DXUser::get_current($_[4]);
978 $loc = $ref->qra || '';
979 $loc = ' ' . substr($loc, 0, 4) if $loc;
983 if ($self->{user}->wantdxitu) {
984 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
985 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
986 } elsif ($self->{user}->wantdxcq) {
987 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
988 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
989 } elsif ($self->{user}->wantusstate) {
990 $loc = ' ' . $_[13] if $_[13];
991 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
994 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1002 my $isolate = shift;
1003 return unless $self->{dx};
1005 my ($filter, $hops);
1007 if ($self->{spotsfilter}) {
1008 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1009 return unless $filter;
1012 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1015 if ($self->{ve7cc}) {
1016 $buf = VE7CC::dx_spot($self, @_);
1017 } elsif ($self->{gtk}) {
1018 my ($dxloc, $byloc);
1020 my $ref = DXUser::get_current($_[4]);
1023 $byloc = substr($byloc, 0, 4) if $byloc;
1027 $spot =~ s|/\w{1,4}$||;
1028 $ref = DXUser::get_current($spot);
1031 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1033 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1036 $buf = $self->format_dx_spot(@_);
1037 $buf .= "\a\a" if $self->{beep};
1041 $self->local_send('X', $buf);
1048 my $isolate = shift;
1049 my ($filter, $hops);
1051 return unless $self->{wwv};
1053 if ($self->{wwvfilter}) {
1054 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1055 return unless $filter;
1060 $buf = dd(['wwv', @_])
1062 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1063 $buf .= "\a\a" if $self->{beep};
1066 $self->local_send('V', $buf);
1073 my $isolate = shift;
1074 my ($filter, $hops);
1076 return unless $self->{wcy};
1078 if ($self->{wcyfilter}) {
1079 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1080 return unless $filter;
1085 $buf = dd(['wcy', @_])
1087 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1088 $buf .= "\a\a" if $self->{beep};
1090 $self->local_send('Y', $buf);
1093 # broadcast debug stuff to all interested parties
1096 my $s = shift; # the line to be rebroadcast
1098 foreach my $dxchan (DXChannel::get_all_users) {
1099 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1100 if ($dxchan->{gtk}) {
1101 $dxchan->send_later('L', dd(['db', $s]));
1103 $dxchan->send_later('L', $s);
1114 if ($self->state eq 'enterbody') {
1115 my $loc = $self->{loc} || confess "local var gone missing" ;
1116 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1118 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1120 $self->state('prompt');
1121 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1122 push @out, $self->msg('m10');
1123 delete $loc->{lines};
1124 delete $self->{loc};
1126 $self->state('prompt');
1128 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1129 # i.e. it ain't and end or abort, therefore store the line
1132 confess "Invalid state $self->{state}";
1137 sub store_startup_script
1140 my $loc = $self->{loc} || confess "local var gone missing" ;
1142 my $call = $loc->{call} || confess "callsign gone missing";
1143 confess "lines array gone missing" unless ref $loc->{lines};
1144 my $r = Script::store($call, $loc->{lines});
1147 push @out, $self->msg('m19', $call, $r);
1149 push @out, $self->msg('m20', $call);
1152 push @out, "error opening startup script $call $!";
1157 # Import any commands contained in any files in import_cmd directory
1159 # If the filename has a recogisable callsign as some delimited part
1160 # of it, then this is the user the command will be run as.
1164 # are there any to do in this directory?
1165 return unless -d $cmdimportdir;
1166 unless (opendir(DIR, $cmdimportdir)) {
1167 LogDbg('err', "can\'t open $cmdimportdir $!");
1171 my @names = readdir(DIR);
1175 return unless @names;
1177 foreach $name (@names) {
1178 next if $name =~ /^\./;
1180 my $s = Script->new($name, $cmdimportdir);
1182 LogDbg('DXCommand', "Run import cmd file $name");
1183 my @cat = split /[^A-Za-z0-9]+/, $name;
1184 my ($call) = grep {is_callsign(uc $_)} @cat;
1185 $call ||= $main::mycall;
1190 $s->inscript(0); # switch off script checks
1192 if ($call eq $main::mycall) {
1193 @out = $s->run($main::me, 1);
1195 my $dxchan = DXChannel::get($call);
1197 @out = $s->run($dxchan, 1);
1199 my $u = DXUser::get($call);
1201 $dxchan = $main::me;
1202 my $old = $dxchan->{call};
1203 my $priv = $dxchan->{priv};
1204 my $user = $dxchan->{user};
1205 $dxchan->{call} = $call;
1206 $dxchan->{priv} = $u->priv;
1207 $dxchan->{user} = $u;
1208 @out = $s->run($dxchan, 1);
1209 $dxchan->{call} = $old;
1210 $dxchan->{priv} = $priv;
1211 $dxchan->{user} = $user;
1213 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1219 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1222 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1223 unlink "$cmdimportdir/$name";
1228 sub print_find_reply
1230 my ($self, $node, $target, $flag, $ms) = @_;
1231 my $sort = $flag == 2 ? "External" : "Local";
1232 $self->send("$sort $target found at $node in $ms ms" );
1235 # send the most relevant motd
1241 unless ($self->{registered}) {
1242 $motd = "${main::motd}_nor_$self->{lang}";
1243 $motd = "${main::motd}_nor" unless -e $motd;
1245 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1246 $motd = $main::motd unless $motd && -e $motd;
1247 if ($self->conn->ax25) {
1249 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1251 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1254 $self->send_file($motd) if -e $motd;
1259 return unless isdbg('chan');
1263 my $tb = shift || [gettimeofday];
1265 my $a = int($ta->[0] * 1000) + int($ta->[1] / 1000);
1266 my $b = int($tb->[0] * 1000) + int($tb->[1] / 1000);
1267 my $msecs = $b - $a;
1269 my $s = "forkcall stats: $call '$line' ";
1274 # Punt off a long running command into a separate process
1276 # This is called from commands to run some potentially long running
1277 # function. The process forks and then runs the function and returns
1278 # the result back to the cmd.
1280 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1281 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1282 # THE CURRENT CONTEXT!!
1284 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1290 my $call = $self->{call};
1294 my $cb = delete $args{cb};
1295 my $prefix = delete $args{prefix};
1296 my $progress = delete $args{progress};
1297 my $args = delete $args{args} || [];
1298 my $t0 = [gettimeofday];
1302 my $fc = Mojo::IOLoop::ForkCall->new;
1303 $fc->serializer(\&encode_json);
1304 $fc->deserializer(\&decode_json);
1306 sub {my @args = @_; my @res = $cmdref->(@args); return @res},
1309 my ($fc, $err, @res) = @_;
1310 my $dxchan = DXChannel::get($call);
1311 return unless $dxchan;
1314 my $s = "DXCommand::spawn_cmd: call $call error $err";
1315 dbg($s) if isdbg('chan');
1320 $cb->($dxchan, @res);
1323 if (defined $prefix) {
1324 $dxchan->send(map {"$prefix$_"} @res);
1326 $dxchan->send(@res);
1329 _diffms($call, $line, $t0);