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::Subprocess;
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->hostname], );
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 $self->{hostname} = $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 if ($line =~ /host=/) {
115 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
116 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
118 ($h) = $line =~ /host=([\da..fA..F:]+)/;
119 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
121 $self->{hostname} = $h if $h;
123 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
124 $self->{consort} = $line; # save the connection type
126 LogDbg('DXCommand', "$call connected from $self->{hostname}");
128 # set some necessary flags on the user if they are connecting
129 $self->{beep} = $user->wantbeep;
130 $self->{ann} = $user->wantann;
131 $self->{wwv} = $user->wantwwv;
132 $self->{wcy} = $user->wantwcy;
133 $self->{talk} = $user->wanttalk;
134 $self->{wx} = $user->wantwx;
135 $self->{dx} = $user->wantdx;
136 $self->{logininfo} = $user->wantlogininfo;
137 $self->{ann_talk} = $user->wantann_talk;
139 $self->{prompt} = $user->prompt if $user->prompt;
140 $self->{lastmsgpoll} = 0;
142 # sort out new dx spot stuff
143 $user->wantdxcq(0) unless defined $user->{wantdxcq};
144 $user->wantdxitu(0) unless defined $user->{wantdxitu};
145 $user->wantusstate(0) unless defined $user->{wantusstate};
147 # sort out registration
148 if ($main::reqreg == 1) {
149 $self->{registered} = $user->registered;
150 } elsif ($main::reqreg == 2) {
151 $self->{registered} = !$user->registered;
153 $self->{registered} = 1;
156 # send the relevant MOTD
159 # sort out privilege reduction
160 $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
164 $nossid =~ s/-\d+$//;
166 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
167 || Filter::read_in('spots', $nossid, 0)
168 || Filter::read_in('spots', 'user_default', 0);
169 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
170 || Filter::read_in('wwv', $nossid, 0)
171 || Filter::read_in('wwv', 'user_default', 0);
172 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
173 || Filter::read_in('wcy', $nossid, 0)
174 || Filter::read_in('wcy', 'user_default', 0);
175 $self->{annfilter} = Filter::read_in('ann', $call, 0)
176 || Filter::read_in('ann', $nossid, 0)
177 || Filter::read_in('ann', 'user_default', 0) ;
179 # clean up qra locators
180 my $qra = $user->qra;
181 $qra = undef if ($qra && !DXBearing::is_qra($qra));
183 my $lat = $user->lat;
184 my $long = $user->long;
185 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
189 my $echo = $user->wantecho;
191 $self->send_now('E', "0");
192 $self->send($self->msg('echow'));
193 $self->conn->echo($echo) if $self->conn->can('echo');
196 $self->tell_login('loginu');
197 $self->tell_buddies('loginb');
199 # do we need to send a forward/opernam?
200 my $lastoper = $user->lastoper || 0;
201 my $homenode = $user->homenode || "";
202 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
203 run_cmd($main::me, "forward/opernam $call");
204 $user->lastoper($main::systime + ((int rand(10)) * 86400));
207 # run a script send the output to the punter
208 my $script = new Script(lc $call) || new Script('user_default');
209 $script->run($self) if $script;
212 my $info = Route::cluster();
213 $self->send("Cluster:$info");
215 # send prompts for qth, name and things
216 $self->send($self->msg('namee1')) if !$user->name;
217 $self->send($self->msg('qthe1')) if !$user->qth;
218 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
219 $self->send($self->msg('hnodee1')) if !$user->qth;
220 $self->send($self->msg('m9')) if DXMsg::for_me($call);
222 # send out any buddy messages for other people that are online
223 foreach my $call (@{$user->buddies}) {
224 my $ref = Route::User::get($call);
226 foreach my $node ($ref->parents) {
227 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
232 $self->lastmsgpoll($main::systime);
237 # This is the normal command prompt driver
246 # save this for them's that need it
247 my $rawline = $cmdline;
249 # remove leading and trailing spaces
250 $cmdline =~ s/^\s*(.*)\s*$/$1/;
252 if ($self->{state} eq 'page') {
253 my $i = $self->{pagelth};
254 my $ref = $self->{pagedata};
257 # abort if we get a line starting in with a
258 if ($cmdline =~ /^a/io) {
263 # send a tranche of data
264 while ($i-- > 0 && @$ref) {
265 my $line = shift @$ref;
266 $line =~ s/\s+$//o; # why am having to do this?
270 # reset state if none or else chuck out an intermediate prompt
272 $tot -= $self->{pagelth};
273 $self->send($self->msg('page', $tot));
275 $self->state('prompt');
277 } elsif ($self->{state} eq 'sysop') {
278 my $passwd = $self->{user}->passwd;
280 my @pw = grep {$_ !~ /\s/} split //, $passwd;
281 my @l = @{$self->{passwd}};
282 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
283 if ($cmdline =~ /$str/) {
284 $self->{priv} = $self->{user}->priv;
286 $self->send($self->msg('sorry'));
289 $self->send($self->msg('sorry'));
291 $self->state('prompt');
292 } elsif ($self->{state} eq 'passwd') {
293 my $passwd = $self->{user}->passwd;
294 if ($passwd && $cmdline eq $passwd) {
295 $self->send($self->msg('pw1'));
296 $self->state('passwd1');
298 $self->conn->{echo} = $self->conn->{decho};
299 delete $self->conn->{decho};
300 $self->send($self->msg('sorry'));
301 $self->state('prompt');
303 } elsif ($self->{state} eq 'passwd1') {
304 $self->{passwd} = $cmdline;
305 $self->send($self->msg('pw2'));
306 $self->state('passwd2');
307 } elsif ($self->{state} eq 'passwd2') {
308 if ($cmdline eq $self->{passwd}) {
309 $self->{user}->passwd($cmdline);
310 $self->send($self->msg('pw3'));
312 $self->send($self->msg('pw4'));
314 $self->conn->{echo} = $self->conn->{decho};
315 delete $self->conn->{decho};
316 $self->state('prompt');
317 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
318 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
319 for (@{$self->{talklist}}) {
320 if ($self->{state} eq 'talk') {
321 $self->send_talks($_, $self->msg('talkend'));
323 $self->local_send('C', $self->msg('chatend', $_));
326 $self->state('prompt');
327 delete $self->{talklist};
328 } elsif ($cmdline =~ m|^/+\w+|) {
330 my $sendit = $cmdline =~ s|^/+||;
331 my @in = $self->run_cmd($cmdline);
332 $self->send_ans(@in);
333 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
334 foreach my $l (@in) {
336 if (@bad = BadWords::check($l)) {
337 $self->badcount(($self->badcount||0) + @bad);
338 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
340 for (@{$self->{talklist}}) {
341 if ($self->{state} eq 'talk') {
342 $self->send_talks($_, $l);
344 send_chats($self, $_, $l)
350 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
351 } elsif ($self->{talklist} && @{$self->{talklist}}) {
352 # send what has been said to whoever is in this person's talk list
354 if (@bad = BadWords::check($cmdline)) {
355 $self->badcount(($self->badcount||0) + @bad);
356 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
358 for (@{$self->{talklist}}) {
359 if ($self->{state} eq 'talk') {
360 $self->send_talks($_, $rawline);
362 send_chats($self, $_, $rawline);
366 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
367 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
370 $self->state('prompt');
372 } elsif (my $func = $self->{func}) {
375 if (ref $self->{edit}) {
376 eval { @ans = $self->{edit}->$func($self, $rawline)};
378 eval { @ans = &{$self->{func}}($self, $rawline) };
381 $self->send_ans("Syserr: on stored func $self->{func}", $@);
382 delete $self->{func};
383 $self->state('prompt');
386 $self->send_ans(@ans);
388 $self->send_ans(run_cmd($self, $cmdline));
391 # check for excessive swearing
392 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
393 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
398 # send a prompt only if we are in a prompt state
399 $self->prompt() if $self->{state} =~ /^prompt/o;
402 # send out the talk messages taking into account vias and connectivity
405 my ($self, $ent, $line) = @_;
407 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
408 $to = $ent unless $to;
409 my $call = $via && $via ne '*' ? $via : $to;
410 my $clref = Route::get($call);
411 my $dxchan = $clref->dxchan if $clref;
413 $dxchan->talk($self->{call}, $to, undef, $line);
415 $self->send($self->msg('disc2', $via ? $via : $to));
416 my @l = grep { $_ ne $ent } @{$self->{talklist}};
418 $self->{talklist} = \@l;
420 delete $self->{talklist};
421 $self->state('prompt');
432 my $msgid = DXProt::nextchatmsgid();
433 $text = "#$msgid $text";
434 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
442 for (@{$self->{talklist}}) {
443 my ($to, $via) = /(\S+)>(\S+)/;
447 return $self->msg($prompt, join(',', @call));
453 return $self->special_prompt('talkprompt');
459 return $self->special_prompt('chatprompt');
463 # send a load of stuff to a command user with page prompting
471 if ($self->{pagelth} && @_ > $self->{pagelth}) {
473 for ($i = $self->{pagelth}; $i-- > 0; ) {
475 $line =~ s/\s+$//o; # why am having to do this?
478 $self->{pagedata} = [ @_ ];
479 $self->state('page');
480 $self->send($self->msg('page', scalar @_));
493 # this is the thing that preps for running the command, it is done like this for the
494 # benefit of remote command execution
500 my $user = $self->{user};
501 my $call = $self->{call};
505 return () if length $cmdline == 0;
507 # split the command line up into parts, the first part is the command
508 my ($cmd, $args) = split /\s+/, $cmdline, 2;
509 $args = "" unless defined $args;
514 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
515 LogDbg('DXCommand', "cmd: invalid characters in '$cmd'");
516 return $self->_error_out('e1');
519 # strip out // on command only
524 dbg("cmd: $cmd") if isdbg('command');
526 # alias it if possible
527 my $acmd = CmdAlias::get_cmd($cmd);
529 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
530 $args = "" unless defined $args;
531 dbg("cmd: aliased $cmd $args") if isdbg('command');
534 # first expand out the entry to a command
535 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
536 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
539 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
541 my $package = find_cmd_name($path, $fcmd);
544 if ($package && $self->can("${package}::handle")) {
546 dbg("cmd: package $package") if isdbg('command');
547 if (isdbg('progress')) {
548 my $s = "CMD: '$cmd' by $call ip $self->{hostname}";
550 my $t0 = [gettimeofday];
551 eval { @ans = &{"${package}::handle"}($self, $args) };
552 return (DXDebug::shortmess($@)) if $@;
553 if (isdbg('progress')) {
554 my $msecs = _diffms($t0);
555 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
559 dbg("cmd: $package not present") if isdbg('command');
560 return $self->_error_out('e1');
563 dbg("cmd: $cmd not found") if isdbg('command');
564 return $self->_error_out('e1');
570 delete $self->{errors};
572 if (++$self->{errors} > $DXChannel::maxerrors) {
573 $self->send($self->msg('e26'));
578 return map {s/([^\s])\s+$/$1/; $_} @ans;
582 # This is called from inside the main cluster processing loop and is used
583 # for despatching commands that are doing some long processing job
588 my @dxchan = DXChannel::get_all();
591 foreach $dxchan (@dxchan) {
592 next unless $dxchan->is_user;
594 # send a outstanding message prompt if required
595 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
596 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
597 $dxchan->lastmsgpoll($t);
600 # send a prompt if no activity out on this channel
601 if ($t >= $dxchan->t + $main::user_interval) {
602 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
607 while (my ($k, $v) = each %nothereslug) {
608 if ($main::systime >= $v + 300) {
609 delete $nothereslug{$k};
617 # finish up a user context
622 my $call = $self->call;
624 return if $self->{disconnecting}++;
626 delete $self->{senddbg};
628 my $uref = Route::User::get($call);
631 # @rout = $main::routeroot->del_user($uref);
632 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
634 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
636 # issue a pc17 to everybody interested
637 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
638 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
640 confess "trying to disconnect a non existant user $call";
643 # I was the last node visited
644 $self->user->node($main::mycall);
646 # send info to all logged in thingies
647 $self->tell_login('logoutu');
648 $self->tell_buddies('logoutb');
650 LogDbg('DXCommand', "$call disconnected");
652 $self->SUPER::disconnect;
656 # short cut to output a prompt
663 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
665 my $call = $self->call;
666 my $date = cldate($main::systime);
667 my $time = ztime($main::systime);
668 my $prompt = $self->{prompt} || $self->msg('pr');
670 $call = "($call)" unless $self->here;
671 $prompt =~ s/\%C/$call/g;
672 $prompt =~ s/\%D/$date/g;
673 $prompt =~ s/\%T/$time/g;
674 $prompt =~ s/\%M/$main::mycall/g;
676 $self->send($prompt);
679 # broadcast a message to all users [except those mentioned after buffer]
682 my $pkg = shift; # ignored
683 my $s = shift; # the line to be rebroadcast
685 foreach my $dxchan (DXChannel::get_all()) {
686 next unless $dxchan->is_user; # only interested in user channels
687 next if grep $dxchan == $_, @_;
688 $dxchan->send($s); # send it
692 # gimme all the users
695 return grep {$_->is_user} DXChannel::get_all();
698 # run a script for this user
702 my $silent = shift || 0;
707 # search for the command in the cache of short->long form commands
712 my ($path, $short_cmd, $suffix) = @_;
715 # commands are lower case
716 $short_cmd = lc $short_cmd;
717 dbg("command: $path $short_cmd\n") if isdbg('command');
719 # do some checking for funny characters
720 return () if $short_cmd =~ /\/$/;
722 # return immediately if we have it
723 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
724 if ($apath && $acmd) {
725 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
726 return ($apath, $acmd);
730 my @parts = split '/', $short_cmd;
734 while (my $p = shift @parts) {
735 opendir(D, $curdir) or confess "can't open $curdir $!";
739 # if this isn't the last part
742 foreach my $l (sort @ls) {
744 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
745 dbg("got dir: $curdir/$l\n") if isdbg('command');
752 # only proceed if we find the directory asked for
753 return () unless $found;
755 foreach my $l (sort @ls) {
757 next unless $l =~ /\.$suffix$/;
758 if ($p eq substr($l, 0, length $p)) {
759 $l =~ s/\.$suffix$//;
760 $dirfn = "" unless $dirfn;
761 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
762 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
763 return ($path, "$dirfn$l");
772 # clear the command name cache
777 for my $k (keys %Cache) {
778 unless ($k =~ /cmd_cache/) {
779 dbg("Undefining cmd $k") if isdbg('command');
780 undef $DXCommandmode::{"${k}::"};
784 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
788 # the persistant execution of things from the command directories
791 # This allows perl programs to call functions dynamically
793 # This has been nicked directly from the perlembed pages
795 #require Devel::Symdump;
797 sub valid_package_name {
799 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
802 return "cmd_$string";
806 # this bit of magic finds a command in the offered directory
810 my $package = valid_package_name($cmdname);
811 my $filename = "$path/$cmdname.pl";
812 my $mtime = -M $filename;
814 # return if we can't find it
816 unless (defined $mtime) {
817 $errstr = DXM::msg('e1');
821 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
822 #we have compiled this subroutine already,
823 #it has not been updated on disk, nothing left to do
824 #print STDERR "already compiled $package->handler\n";
825 dbg("find_cmd_name: $package cached") if isdbg('command');
828 my $sub = readfilestr($filename);
830 $errstr = "Syserr: can't open '$filename' $!";
834 #wrap the code into a subroutine inside our unique package
835 my $eval = qq(package DXCommandmode::$package; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
838 if ($sub =~ m|\s*sub\s+handle\n|) {
841 $eval .= qq(sub handle { $sub });
845 my @list = split /\n/, $eval;
848 dbg($_ . "\n") if isdbg('eval');
852 # get rid of any existing sub and try to compile the new one
855 if (exists $Cache{$package}) {
856 dbg("find_cmd_name: Redefining $package") if isdbg('command');
857 undef $DXCommandmode::{"${package}::"};
858 delete $Cache{$package};
860 dbg("find_cmd_name: Defining $package") if isdbg('command');
865 $Cache{$package} = {mtime => $mtime } unless $@;
868 return "DXCommandmode::$package";
876 $self->SUPER::send(dd(['cmd',$_]));
879 $self->SUPER::send(@_);
885 my ($self, $let, $buf) = @_;
886 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
887 if ($self->{enhanced}) {
888 $self->send_later($let, $buf);
897 # send a talk message here
900 my ($self, $from, $to, $via, $line, $onode) = @_;
901 $line =~ s/\\5E/\^/g;
904 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
906 $self->local_send('T', "$to de $from: $line");
909 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
910 # send a 'not here' message if required
911 unless ($self->{here} && $from ne $to) {
912 my $key = "$to$from";
913 unless (exists $nothereslug{$key}) {
915 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
916 my $name = $self->user->name || $to;
917 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
918 $nothereslug{$key} = $main::systime;
919 $dxchan->talk($to, $from, undef, $s);
936 if (!$self->{ann_talk} && $to ne $self->{call}) {
937 my $call = AnnTalk::is_talk_candidate($_[0], $text);
941 if ($self->{annfilter}) {
942 ($filter, $hops) = $self->{annfilter}->it(@_ );
943 return unless $filter;
946 unless ($self->{ann}) {
947 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
949 return if $target eq 'SYSOP' && $self->{priv} < 5;
952 $buf = dd(['ann', $to, $target, $text, @_])
954 $buf = "$to$target de $_[0]: $text";
956 $buf .= "\a\a" if $self->{beep};
958 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
972 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
974 $text =~ s/^\#\d+ //;
977 $buf = dd(['chat', $to, $target, $text, @_])
979 $buf = "$target de $_[0]: $text";
981 $buf .= "\a\a" if $self->{beep};
983 $self->local_send('C', $buf);
990 my $t = ztime($_[2]);
992 my $clth = $self->{consort} eq 'local' ? 29 : 30;
993 my $comment = substr (($_[3] || ''), 0, $clth);
994 $comment .= ' ' x ($clth - length($comment));
995 if ($self->{user}->wantgrid) {
996 my $ref = DXUser::get_current($_[4]);
998 $loc = $ref->qra || '';
999 $loc = ' ' . substr($loc, 0, 4) if $loc;
1003 if ($self->{user}->wantdxitu) {
1004 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
1005 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
1006 } elsif ($self->{user}->wantdxcq) {
1007 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
1008 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
1009 } elsif ($self->{user}->wantusstate) {
1010 $loc = ' ' . $_[13] if $_[13];
1011 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
1014 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1022 my $isolate = shift;
1023 return unless $self->{dx};
1025 my ($filter, $hops);
1027 if ($self->{spotsfilter}) {
1028 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1029 return unless $filter;
1032 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1035 if ($self->{ve7cc}) {
1036 $buf = VE7CC::dx_spot($self, @_);
1037 } elsif ($self->{gtk}) {
1038 my ($dxloc, $byloc);
1040 my $ref = DXUser::get_current($_[4]);
1043 $byloc = substr($byloc, 0, 4) if $byloc;
1047 $spot =~ s|/\w{1,4}$||;
1048 $ref = DXUser::get_current($spot);
1051 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1053 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1056 $buf = $self->format_dx_spot(@_);
1057 $buf .= "\a\a" if $self->{beep};
1061 $self->local_send('X', $buf);
1068 my $isolate = shift;
1069 my ($filter, $hops);
1071 return unless $self->{wwv};
1073 if ($self->{wwvfilter}) {
1074 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1075 return unless $filter;
1080 $buf = dd(['wwv', @_])
1082 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1083 $buf .= "\a\a" if $self->{beep};
1086 $self->local_send('V', $buf);
1093 my $isolate = shift;
1094 my ($filter, $hops);
1096 return unless $self->{wcy};
1098 if ($self->{wcyfilter}) {
1099 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1100 return unless $filter;
1105 $buf = dd(['wcy', @_])
1107 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1108 $buf .= "\a\a" if $self->{beep};
1110 $self->local_send('Y', $buf);
1113 # broadcast debug stuff to all interested parties
1116 my $s = shift; # the line to be rebroadcast
1118 foreach my $dxchan (DXChannel::get_all_users) {
1119 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1120 if ($dxchan->{gtk}) {
1121 $dxchan->send_later('L', dd(['db', $s]));
1123 $dxchan->send_later('L', $s);
1134 if ($self->state eq 'enterbody') {
1135 my $loc = $self->{loc} || confess "local var gone missing" ;
1136 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1138 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1140 $self->state('prompt');
1141 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1142 push @out, $self->msg('m10');
1143 delete $loc->{lines};
1144 delete $self->{loc};
1146 $self->state('prompt');
1148 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1149 # i.e. it ain't and end or abort, therefore store the line
1152 confess "Invalid state $self->{state}";
1157 sub store_startup_script
1160 my $loc = $self->{loc} || confess "local var gone missing" ;
1162 my $call = $loc->{call} || confess "callsign gone missing";
1163 confess "lines array gone missing" unless ref $loc->{lines};
1164 my $r = Script::store($call, $loc->{lines});
1167 push @out, $self->msg('m19', $call, $r);
1169 push @out, $self->msg('m20', $call);
1172 push @out, "error opening startup script $call $!";
1177 # Import any commands contained in any files in import_cmd directory
1179 # If the filename has a recogisable callsign as some delimited part
1180 # of it, then this is the user the command will be run as.
1184 # are there any to do in this directory?
1185 return unless -d $cmdimportdir;
1186 unless (opendir(DIR, $cmdimportdir)) {
1187 LogDbg('err', "can\'t open $cmdimportdir $!");
1191 my @names = readdir(DIR);
1195 return unless @names;
1197 foreach $name (@names) {
1198 next if $name =~ /^\./;
1200 my $s = Script->new($name, $cmdimportdir);
1202 LogDbg('DXCommand', "Run import cmd file $name");
1203 my @cat = split /[^A-Za-z0-9]+/, $name;
1204 my ($call) = grep {is_callsign(uc $_)} @cat;
1205 $call ||= $main::mycall;
1210 $s->inscript(0); # switch off script checks
1212 if ($call eq $main::mycall) {
1213 @out = $s->run($main::me, 1);
1215 my $dxchan = DXChannel::get($call);
1217 @out = $s->run($dxchan, 1);
1219 my $u = DXUser::get($call);
1221 $dxchan = $main::me;
1222 my $old = $dxchan->{call};
1223 my $priv = $dxchan->{priv};
1224 my $user = $dxchan->{user};
1225 $dxchan->{call} = $call;
1226 $dxchan->{priv} = $u->priv;
1227 $dxchan->{user} = $u;
1228 @out = $s->run($dxchan, 1);
1229 $dxchan->{call} = $old;
1230 $dxchan->{priv} = $priv;
1231 $dxchan->{user} = $user;
1233 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1239 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1242 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1243 unlink "$cmdimportdir/$name";
1248 sub print_find_reply
1250 my ($self, $node, $target, $flag, $ms) = @_;
1251 my $sort = $flag == 2 ? "External" : "Local";
1252 $self->send("$sort $target found at $node in $ms ms" );
1255 # send the most relevant motd
1261 unless ($self->{registered}) {
1262 $motd = "${main::motd}_nor_$self->{lang}";
1263 $motd = "${main::motd}_nor" unless -e $motd;
1265 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1266 $motd = $main::motd unless $motd && -e $motd;
1267 if ($self->conn->ax25) {
1269 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1271 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1274 $self->send_file($motd) if -e $motd;
1278 # Punt off a long running command into a separate process
1280 # This is called from commands to run some potentially long running
1281 # function. The process forks and then runs the function and returns
1282 # the result back to the cmd.
1284 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1285 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1286 # THE CURRENT CONTEXT!!
1288 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1294 my $call = $self->{call};
1298 my $cb = delete $args{cb};
1299 my $prefix = delete $args{prefix};
1300 my $progress = delete $args{progress};
1301 my $args = delete $args{args} || [];
1302 my $t0 = [gettimeofday];
1306 # just behave normally if something has set the "one-shot" _nospawn in the channel
1307 return ($cmdref->(@$args)) if $self->{_nospawn};
1309 my $fc = Mojo::IOLoop::Subprocess->new;
1310 # $fc->serializer(\&encode_json);
1311 # $fc->deserializer(\&decode_json);
1315 if (isdbg('spawn_cmd')) {
1316 my $s = "line: $line";
1317 $s .= ", args: " . join(', ', @$args) if $args && @$args;
1319 my @res = $cmdref->(@$args);
1320 # diffms("rcmd from $call 1", $line, $t0, scalar @res) if isdbg('chan');
1325 my ($fc, $err, @res) = @_;
1326 my $dxchan = DXChannel::get($call);
1327 return unless $dxchan;
1330 my $s = "DXProt::spawn_cmd: call $call error $err";
1331 dbg($s) if isdbg('chan');
1336 # transform output if required
1337 @res = $cb->($dxchan, @res);
1340 if (defined $prefix) {
1341 $dxchan->send(map {"$prefix$_"} @res);
1343 $dxchan->send(@res);
1346 diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');