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 $users $maxusers);
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
62 $users = 0; # no of users on this node currently
63 $maxusers = 0; # max no users on this node for this run
66 # obtain a new connection this is derived from dxchannel
71 my $self = DXChannel::alloc(@_);
73 # routing, this must go out here to prevent race condx
76 # my @rout = $main::routeroot->add_user($call, Route::here(1));
77 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
79 # ALWAYS output the user
80 my $ref = Route::User::get($call);
82 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref);
83 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, $ref) unless $DXProt::pc92_slug_changes;
89 # this is how a a connection starts, you get a hello message and the motd with
90 # possibly some other messages asking you to set various things up if you are
91 # new (or nearly new and slacking) user.
95 my ($self, $line, $sort) = @_;
96 my $user = $self->{user};
97 my $call = $self->{call};
98 my $name = $user->{name};
101 my $host = $self->{conn}->peerhost;
102 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
104 $self->{hostname} = $host;
106 $self->{name} = $name ? $name : $call;
107 $self->send($self->msg('l2',$self->{name}));
108 $self->state('prompt'); # a bit of room for further expansion, passwords etc
109 $self->{priv} = $user->priv || 0;
110 $self->{lang} = $user->lang || $main::lang || 'en';
111 my $pagelth = $user->pagelth;
112 $pagelth = $default_pagelth unless defined $pagelth;
113 $self->{pagelth} = $pagelth;
114 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
115 if ($line =~ /host=/) {
116 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
117 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
119 ($h) = $line =~ /host=([\da..fA..F:]+)/;
120 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
122 $self->{hostname} = $h if $h;
124 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
125 $self->{consort} = $line; # save the connection type
127 LogDbg('DXCommand', "$call connected from $self->{hostname}");
129 # set some necessary flags on the user if they are connecting
130 $self->{beep} = $user->wantbeep;
131 $self->{ann} = $user->wantann;
132 $self->{wwv} = $user->wantwwv;
133 $self->{wcy} = $user->wantwcy;
134 $self->{talk} = $user->wanttalk;
135 $self->{wx} = $user->wantwx;
136 $self->{dx} = $user->wantdx;
137 $self->{logininfo} = $user->wantlogininfo;
138 $self->{ann_talk} = $user->wantann_talk;
140 $self->{prompt} = $user->prompt if $user->prompt;
141 $self->{lastmsgpoll} = 0;
143 # sort out new dx spot stuff
144 $user->wantdxcq(0) unless defined $user->{wantdxcq};
145 $user->wantdxitu(0) unless defined $user->{wantdxitu};
146 $user->wantusstate(0) unless defined $user->{wantusstate};
148 # sort out registration
149 if ($main::reqreg == 1) {
150 $self->{registered} = $user->registered;
151 } elsif ($main::reqreg == 2) {
152 $self->{registered} = !$user->registered;
154 $self->{registered} = 1;
157 # send the relevant MOTD
160 # sort out privilege reduction
161 $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
165 $nossid =~ s/-\d+$//;
167 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
168 || Filter::read_in('spots', $nossid, 0)
169 || Filter::read_in('spots', 'user_default', 0);
170 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
171 || Filter::read_in('wwv', $nossid, 0)
172 || Filter::read_in('wwv', 'user_default', 0);
173 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
174 || Filter::read_in('wcy', $nossid, 0)
175 || Filter::read_in('wcy', 'user_default', 0);
176 $self->{annfilter} = Filter::read_in('ann', $call, 0)
177 || Filter::read_in('ann', $nossid, 0)
178 || Filter::read_in('ann', 'user_default', 0) ;
180 # clean up qra locators
181 my $qra = $user->qra;
182 $qra = undef if ($qra && !DXBearing::is_qra($qra));
184 my $lat = $user->lat;
185 my $long = $user->long;
186 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
190 my $echo = $user->wantecho;
192 $self->send_now('E', "0");
193 $self->send($self->msg('echow'));
194 $self->conn->echo($echo) if $self->conn->can('echo');
197 $self->tell_login('loginu');
198 $self->tell_buddies('loginb');
200 # do we need to send a forward/opernam?
201 my $lastoper = $user->lastoper || 0;
202 my $homenode = $user->homenode || "";
203 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
204 run_cmd($main::me, "forward/opernam $call");
205 $user->lastoper($main::systime + ((int rand(10)) * 86400));
208 # run a script send the output to the punter
209 my $script = new Script(lc $call) || new Script('user_default');
210 $script->run($self) if $script;
213 my $info = Route::cluster();
214 $self->send("Cluster:$info");
216 # send prompts for qth, name and things
217 $self->send($self->msg('namee1')) if !$user->name;
218 $self->send($self->msg('qthe1')) if !$user->qth;
219 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
220 $self->send($self->msg('hnodee1')) if !$user->qth;
221 $self->send($self->msg('m9')) if DXMsg::for_me($call);
223 # send out any buddy messages for other people that are online
224 foreach my $call (@{$user->buddies}) {
225 my $ref = Route::User::get($call);
227 foreach my $node ($ref->parents) {
228 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
233 $self->lastmsgpoll($main::systime);
238 # This is the normal command prompt driver
247 # save this for them's that need it
248 my $rawline = $cmdline;
250 # remove leading and trailing spaces
251 $cmdline =~ s/^\s*(.*)\s*$/$1/;
253 if ($self->{state} eq 'page') {
254 my $i = $self->{pagelth};
255 my $ref = $self->{pagedata};
258 # abort if we get a line starting in with a
259 if ($cmdline =~ /^a/io) {
264 # send a tranche of data
265 while ($i-- > 0 && @$ref) {
266 my $line = shift @$ref;
267 $line =~ s/\s+$//o; # why am having to do this?
271 # reset state if none or else chuck out an intermediate prompt
273 $tot -= $self->{pagelth};
274 $self->send($self->msg('page', $tot));
276 $self->state('prompt');
278 } elsif ($self->{state} eq 'sysop') {
279 my $passwd = $self->{user}->passwd;
281 my @pw = grep {$_ !~ /\s/} split //, $passwd;
282 my @l = @{$self->{passwd}};
283 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
284 if ($cmdline =~ /$str/) {
285 $self->{priv} = $self->{user}->priv;
287 $self->send($self->msg('sorry'));
290 $self->send($self->msg('sorry'));
292 $self->state('prompt');
293 } elsif ($self->{state} eq 'passwd') {
294 my $passwd = $self->{user}->passwd;
295 if ($passwd && $cmdline eq $passwd) {
296 $self->send($self->msg('pw1'));
297 $self->state('passwd1');
299 $self->conn->{echo} = $self->conn->{decho};
300 delete $self->conn->{decho};
301 $self->send($self->msg('sorry'));
302 $self->state('prompt');
304 } elsif ($self->{state} eq 'passwd1') {
305 $self->{passwd} = $cmdline;
306 $self->send($self->msg('pw2'));
307 $self->state('passwd2');
308 } elsif ($self->{state} eq 'passwd2') {
309 if ($cmdline eq $self->{passwd}) {
310 $self->{user}->passwd($cmdline);
311 $self->send($self->msg('pw3'));
313 $self->send($self->msg('pw4'));
315 $self->conn->{echo} = $self->conn->{decho};
316 delete $self->conn->{decho};
317 $self->state('prompt');
318 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
319 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
320 for (@{$self->{talklist}}) {
321 if ($self->{state} eq 'talk') {
322 $self->send_talks($_, $self->msg('talkend'));
324 $self->local_send('C', $self->msg('chatend', $_));
327 $self->state('prompt');
328 delete $self->{talklist};
329 } elsif ($cmdline =~ m|^/+\w+|) {
331 my $sendit = $cmdline =~ s|^/+||;
332 my @in = $self->run_cmd($cmdline);
333 $self->send_ans(@in);
334 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
335 foreach my $l (@in) {
337 if (@bad = BadWords::check($l)) {
338 $self->badcount(($self->badcount||0) + @bad);
339 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
341 for (@{$self->{talklist}}) {
342 if ($self->{state} eq 'talk') {
343 $self->send_talks($_, $l);
345 send_chats($self, $_, $l)
351 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
352 } elsif ($self->{talklist} && @{$self->{talklist}}) {
353 # send what has been said to whoever is in this person's talk list
355 if (@bad = BadWords::check($cmdline)) {
356 $self->badcount(($self->badcount||0) + @bad);
357 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
359 for (@{$self->{talklist}}) {
360 if ($self->{state} eq 'talk') {
361 $self->send_talks($_, $rawline);
363 send_chats($self, $_, $rawline);
367 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
368 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
371 $self->state('prompt');
373 } elsif (my $func = $self->{func}) {
376 if (ref $self->{edit}) {
377 eval { @ans = $self->{edit}->$func($self, $rawline)};
379 eval { @ans = &{$self->{func}}($self, $rawline) };
382 $self->send_ans("Syserr: on stored func $self->{func}", $@);
383 delete $self->{func};
384 $self->state('prompt');
387 $self->send_ans(@ans);
389 $self->send_ans(run_cmd($self, $cmdline));
392 # check for excessive swearing
393 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
394 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
399 # send a prompt only if we are in a prompt state
400 $self->prompt() if $self->{state} =~ /^prompt/o;
403 # send out the talk messages taking into account vias and connectivity
406 my ($self, $ent, $line) = @_;
408 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
409 $to = $ent unless $to;
410 my $call = $via && $via ne '*' ? $via : $to;
411 my $clref = Route::get($call);
412 my $dxchan = $clref->dxchan if $clref;
414 $dxchan->talk($self->{call}, $to, undef, $line);
416 $self->send($self->msg('disc2', $via ? $via : $to));
417 my @l = grep { $_ ne $ent } @{$self->{talklist}};
419 $self->{talklist} = \@l;
421 delete $self->{talklist};
422 $self->state('prompt');
433 my $msgid = DXProt::nextchatmsgid();
434 $text = "#$msgid $text";
435 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
443 for (@{$self->{talklist}}) {
444 my ($to, $via) = /(\S+)>(\S+)/;
448 return $self->msg($prompt, join(',', @call));
454 return $self->special_prompt('talkprompt');
460 return $self->special_prompt('chatprompt');
464 # send a load of stuff to a command user with page prompting
472 if ($self->{pagelth} && @_ > $self->{pagelth}) {
474 for ($i = $self->{pagelth}; $i-- > 0; ) {
476 $line =~ s/\s+$//o; # why am having to do this?
479 $self->{pagedata} = [ @_ ];
480 $self->state('page');
481 $self->send($self->msg('page', scalar @_));
494 # this is the thing that preps for running the command, it is done like this for the
495 # benefit of remote command execution
501 my $user = $self->{user};
502 my $call = $self->{call};
506 return () if length $cmdline == 0;
508 # split the command line up into parts, the first part is the command
509 my ($cmd, $args) = split /\s+/, $cmdline, 2;
510 $args = "" unless defined $args;
515 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
516 LogDbg('DXCommand', "cmd: invalid characters in '$cmd'");
517 return $self->_error_out('e1');
520 # strip out // on command only
525 dbg("cmd: $cmd") if isdbg('command');
527 # alias it if possible
528 my $acmd = CmdAlias::get_cmd($cmd);
530 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
531 $args = "" unless defined $args;
532 dbg("cmd: aliased $cmd $args") if isdbg('command');
535 # first expand out the entry to a command
536 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
537 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
540 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
542 my $package = find_cmd_name($path, $fcmd);
545 if ($package && $self->can("${package}::handle")) {
547 dbg("cmd: package $package") if isdbg('command');
548 if (isdbg('progress')) {
549 my $s = "CMD: '$cmd' by $call ip $self->{hostname}";
551 my $t0 = [gettimeofday];
552 eval { @ans = &{"${package}::handle"}($self, $args) };
555 return (DXDebug::shortmess($@));
557 if (isdbg('progress')) {
558 my $msecs = _diffms($t0);
559 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
563 dbg("cmd: $package not present") if isdbg('command');
564 return $self->_error_out('e1');
567 dbg("cmd: $cmd not found") if isdbg('command');
568 return $self->_error_out('e1');
574 delete $self->{errors};
576 if (++$self->{errors} > $DXChannel::maxerrors) {
577 $self->send($self->msg('e26'));
582 return map {s/([^\s])\s+$/$1/; $_} @ans;
586 # This is called from inside the main cluster processing loop and is used
587 # for despatching commands that are doing some long processing job
592 my @dxchan = DXChannel::get_all();
596 foreach $dxchan (@dxchan) {
597 next unless $dxchan->is_user;
599 # send a outstanding message prompt if required
600 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
601 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
602 $dxchan->lastmsgpoll($t);
605 # send a prompt if no activity out on this channel
606 if ($t >= $dxchan->t + $main::user_interval) {
607 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
611 $maxusers = $users if $users > $maxusers;
614 while (my ($k, $v) = each %nothereslug) {
615 if ($main::systime >= $v + 300) {
616 delete $nothereslug{$k};
624 # finish up a user context
629 my $call = $self->call;
631 return if $self->{disconnecting}++;
633 delete $self->{senddbg};
635 my $uref = Route::User::get($call);
638 # @rout = $main::routeroot->del_user($uref);
639 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
641 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
643 # issue a pc17 to everybody interested
644 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
645 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
647 confess "trying to disconnect a non existant user $call";
650 # I was the last node visited
651 $self->user->node($main::mycall);
653 # send info to all logged in thingies
654 $self->tell_login('logoutu');
655 $self->tell_buddies('logoutb');
657 LogDbg('DXCommand', "$call disconnected");
659 $self->SUPER::disconnect;
663 # short cut to output a prompt
670 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
672 my $call = $self->call;
673 my $date = cldate($main::systime);
674 my $time = ztime($main::systime);
675 my $prompt = $self->{prompt} || $self->msg('pr');
677 $call = "($call)" unless $self->here;
678 $prompt =~ s/\%C/$call/g;
679 $prompt =~ s/\%D/$date/g;
680 $prompt =~ s/\%T/$time/g;
681 $prompt =~ s/\%M/$main::mycall/g;
683 $self->send($prompt);
686 # broadcast a message to all users [except those mentioned after buffer]
689 my $pkg = shift; # ignored
690 my $s = shift; # the line to be rebroadcast
692 foreach my $dxchan (DXChannel::get_all()) {
693 next unless $dxchan->is_user; # only interested in user channels
694 next if grep $dxchan == $_, @_;
695 $dxchan->send($s); # send it
699 # gimme all the users
702 goto &DXChannel::get_all_users;
705 # run a script for this user
709 my $silent = shift || 0;
714 # search for the command in the cache of short->long form commands
719 my ($path, $short_cmd, $suffix) = @_;
722 # commands are lower case
723 $short_cmd = lc $short_cmd;
724 dbg("command: $path $short_cmd\n") if isdbg('command');
726 # do some checking for funny characters
727 return () if $short_cmd =~ /\/$/;
729 # return immediately if we have it
730 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
731 if ($apath && $acmd) {
732 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
733 return ($apath, $acmd);
737 my @parts = split '/', $short_cmd;
741 while (my $p = shift @parts) {
742 opendir(D, $curdir) or confess "can't open $curdir $!";
746 # if this isn't the last part
749 foreach my $l (sort @ls) {
751 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
752 dbg("got dir: $curdir/$l\n") if isdbg('command');
759 # only proceed if we find the directory asked for
760 return () unless $found;
762 foreach my $l (sort @ls) {
764 next unless $l =~ /\.$suffix$/;
765 if ($p eq substr($l, 0, length $p)) {
766 $l =~ s/\.$suffix$//;
767 $dirfn = "" unless $dirfn;
768 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
769 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
770 return ($path, "$dirfn$l");
779 # clear the command name cache
784 for my $k (keys %Cache) {
785 unless ($k =~ /cmd_cache/) {
786 dbg("Undefining cmd $k") if isdbg('command');
787 undef $DXCommandmode::{"${k}::"};
791 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
795 # the persistant execution of things from the command directories
798 # This allows perl programs to call functions dynamically
800 # This has been nicked directly from the perlembed pages
802 #require Devel::Symdump;
804 sub valid_package_name {
806 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
809 return "cmd_$string";
813 # this bit of magic finds a command in the offered directory
817 my $package = valid_package_name($cmdname);
818 my $filename = "$path/$cmdname.pl";
819 my $mtime = -M $filename;
821 # return if we can't find it
823 unless (defined $mtime) {
824 $errstr = DXM::msg('e1');
828 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
829 #we have compiled this subroutine already,
830 #it has not been updated on disk, nothing left to do
831 #print STDERR "already compiled $package->handler\n";
832 dbg("find_cmd_name: $package cached") if isdbg('command');
835 my $sub = readfilestr($filename);
837 $errstr = "Syserr: can't open '$filename' $!";
841 #wrap the code into a subroutine inside our unique package
842 my $eval = qq(package DXCommandmode::$package; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
845 if ($sub =~ m|\s*sub\s+handle\n|) {
848 $eval .= qq(sub handle { $sub });
852 my @list = split /\n/, $eval;
855 dbg($_ . "\n") if isdbg('eval');
859 # get rid of any existing sub and try to compile the new one
862 if (exists $Cache{$package}) {
863 dbg("find_cmd_name: Redefining $package") if isdbg('command');
864 undef $DXCommandmode::{"${package}::"};
865 delete $Cache{$package};
867 dbg("find_cmd_name: Defining $package") if isdbg('command');
872 $Cache{$package} = {mtime => $mtime } unless $@;
875 return "DXCommandmode::$package";
883 $self->SUPER::send(dd(['cmd',$_]));
886 $self->SUPER::send(@_);
892 my ($self, $let, $buf) = @_;
893 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
894 if ($self->{enhanced}) {
895 $self->send_later($let, $buf);
904 # send a talk message here
907 my ($self, $from, $to, $via, $line, $onode) = @_;
908 $line =~ s/\\5E/\^/g;
911 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
913 $self->local_send('T', "$to de $from: $line");
916 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
917 # send a 'not here' message if required
918 unless ($self->{here} && $from ne $to) {
919 my $key = "$to$from";
920 unless (exists $nothereslug{$key}) {
922 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
923 my $name = $self->user->name || $to;
924 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
925 $nothereslug{$key} = $main::systime;
926 $dxchan->talk($to, $from, undef, $s);
943 if (!$self->{ann_talk} && $to ne $self->{call}) {
944 my $call = AnnTalk::is_talk_candidate($_[0], $text);
948 if ($self->{annfilter}) {
949 ($filter, $hops) = $self->{annfilter}->it(@_ );
950 return unless $filter;
953 unless ($self->{ann}) {
954 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
956 return if $target eq 'SYSOP' && $self->{priv} < 5;
959 $buf = dd(['ann', $to, $target, $text, @_])
961 $buf = "$to$target de $_[0]: $text";
963 $buf .= "\a\a" if $self->{beep};
965 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
979 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
981 $text =~ s/^\#\d+ //;
984 $buf = dd(['chat', $to, $target, $text, @_])
986 $buf = "$target de $_[0]: $text";
988 $buf .= "\a\a" if $self->{beep};
990 $self->local_send('C', $buf);
997 my $t = ztime($_[2]);
999 my $clth = $self->{consort} eq 'local' ? 29 : 30;
1000 my $comment = substr (($_[3] || ''), 0, $clth);
1001 $comment .= ' ' x ($clth - length($comment));
1002 if ($self->{user}->wantgrid) {
1003 my $ref = DXUser::get_current($_[4]);
1005 $loc = $ref->qra || '';
1006 $loc = ' ' . substr($loc, 0, 4) if $loc;
1010 if ($self->{user}->wantdxitu) {
1011 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
1012 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
1013 } elsif ($self->{user}->wantdxcq) {
1014 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
1015 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
1016 } elsif ($self->{user}->wantusstate) {
1017 $loc = ' ' . $_[13] if $_[13];
1018 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
1021 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1029 my $isolate = shift;
1030 return unless $self->{dx};
1032 my ($filter, $hops);
1034 if ($self->{spotsfilter}) {
1035 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1036 return unless $filter;
1039 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1042 if ($self->{ve7cc}) {
1043 $buf = VE7CC::dx_spot($self, @_);
1044 } elsif ($self->{gtk}) {
1045 my ($dxloc, $byloc);
1047 my $ref = DXUser::get_current($_[4]);
1050 $byloc = substr($byloc, 0, 4) if $byloc;
1054 $spot =~ s|/\w{1,4}$||;
1055 $ref = DXUser::get_current($spot);
1058 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1060 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1063 $buf = $self->format_dx_spot(@_);
1064 $buf .= "\a\a" if $self->{beep};
1068 $self->local_send('X', $buf);
1075 my $isolate = shift;
1076 my ($filter, $hops);
1078 return unless $self->{wwv};
1080 if ($self->{wwvfilter}) {
1081 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1082 return unless $filter;
1087 $buf = dd(['wwv', @_])
1089 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1090 $buf .= "\a\a" if $self->{beep};
1093 $self->local_send('V', $buf);
1100 my $isolate = shift;
1101 my ($filter, $hops);
1103 return unless $self->{wcy};
1105 if ($self->{wcyfilter}) {
1106 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1107 return unless $filter;
1112 $buf = dd(['wcy', @_])
1114 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1115 $buf .= "\a\a" if $self->{beep};
1117 $self->local_send('Y', $buf);
1120 # broadcast debug stuff to all interested parties
1123 my $s = shift; # the line to be rebroadcast
1125 foreach my $dxchan (DXChannel::get_all_users) {
1126 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1127 if ($dxchan->{gtk}) {
1128 $dxchan->send_later('L', dd(['db', $s]));
1130 $dxchan->send_later('L', $s);
1141 if ($self->state eq 'enterbody') {
1142 my $loc = $self->{loc} || confess "local var gone missing" ;
1143 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1145 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1147 $self->state('prompt');
1148 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1149 push @out, $self->msg('m10');
1150 delete $loc->{lines};
1151 delete $self->{loc};
1153 $self->state('prompt');
1155 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1156 # i.e. it ain't and end or abort, therefore store the line
1159 confess "Invalid state $self->{state}";
1164 sub store_startup_script
1167 my $loc = $self->{loc} || confess "local var gone missing" ;
1169 my $call = $loc->{call} || confess "callsign gone missing";
1170 confess "lines array gone missing" unless ref $loc->{lines};
1171 my $r = Script::store($call, $loc->{lines});
1174 push @out, $self->msg('m19', $call, $r);
1176 push @out, $self->msg('m20', $call);
1179 push @out, "error opening startup script $call $!";
1184 # Import any commands contained in any files in import_cmd directory
1186 # If the filename has a recogisable callsign as some delimited part
1187 # of it, then this is the user the command will be run as.
1191 # are there any to do in this directory?
1192 return unless -d $cmdimportdir;
1193 unless (opendir(DIR, $cmdimportdir)) {
1194 LogDbg('err', "can\'t open $cmdimportdir $!");
1198 my @names = readdir(DIR);
1202 return unless @names;
1204 foreach $name (@names) {
1205 next if $name =~ /^\./;
1207 my $s = Script->new($name, $cmdimportdir);
1209 LogDbg('DXCommand', "Run import cmd file $name");
1210 my @cat = split /[^A-Za-z0-9]+/, $name;
1211 my ($call) = grep {is_callsign(uc $_)} @cat;
1212 $call ||= $main::mycall;
1217 $s->inscript(0); # switch off script checks
1219 if ($call eq $main::mycall) {
1220 @out = $s->run($main::me, 1);
1222 my $dxchan = DXChannel::get($call);
1224 @out = $s->run($dxchan, 1);
1226 my $u = DXUser::get($call);
1228 $dxchan = $main::me;
1229 my $old = $dxchan->{call};
1230 my $priv = $dxchan->{priv};
1231 my $user = $dxchan->{user};
1232 $dxchan->{call} = $call;
1233 $dxchan->{priv} = $u->priv;
1234 $dxchan->{user} = $u;
1235 @out = $s->run($dxchan, 1);
1236 $dxchan->{call} = $old;
1237 $dxchan->{priv} = $priv;
1238 $dxchan->{user} = $user;
1240 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1246 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1249 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1250 unlink "$cmdimportdir/$name";
1255 sub print_find_reply
1257 my ($self, $node, $target, $flag, $ms) = @_;
1258 my $sort = $flag == 2 ? "External" : "Local";
1259 $self->send("$sort $target found at $node in $ms ms" );
1262 # send the most relevant motd
1268 unless ($self->{registered}) {
1269 $motd = "${main::motd}_nor_$self->{lang}";
1270 $motd = "${main::motd}_nor" unless -e $motd;
1272 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1273 $motd = $main::motd unless $motd && -e $motd;
1274 if ($self->conn->ax25) {
1276 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1278 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1281 $self->send_file($motd) if -e $motd;
1285 # Punt off a long running command into a separate process
1287 # This is called from commands to run some potentially long running
1288 # function. The process forks and then runs the function and returns
1289 # the result back to the cmd.
1291 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1292 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1293 # THE CURRENT CONTEXT!!
1295 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1301 my $call = $self->{call};
1305 my $cb = delete $args{cb};
1306 my $prefix = delete $args{prefix};
1307 my $progress = delete $args{progress};
1308 my $args = delete $args{args} || [];
1309 my $t0 = [gettimeofday];
1313 # just behave normally if something has set the "one-shot" _nospawn in the channel
1314 if ($self->{_nospawn}) {
1315 eval { @out = $cmdref->(@$args); };
1317 DXDebug::dbgprintring(25);
1318 push @out, DXDebug::shortmess($@);
1323 my $fc = Mojo::IOLoop::Subprocess->new;
1324 # $fc->serializer(\&encode_json);
1325 # $fc->deserializer(\&decode_json);
1329 if (isdbg('progress')) {
1330 my $s = "line: $line";
1331 $s .= ", args: " . join(', ', @$args) if $args && @$args;
1334 eval { @out = $cmdref->(@$args); };
1336 DXDebug::dbgprintring(25);
1337 push @out, DXDebug::shortmess($@);
1343 my ($fc, $err, @res) = @_;
1344 my $dxchan = DXChannel::get($call);
1345 return unless $dxchan;
1348 my $s = "DXProt::spawn_cmd: call $call error $err";
1349 dbg($s) if isdbg('chan');
1354 # transform output if required
1355 @res = $cb->($dxchan, @res);
1358 if (defined $prefix) {
1359 $dxchan->send(map {"$prefix$_"} @res);
1361 $dxchan->send(@res);
1364 diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
1372 return ($users, $maxusers);