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);
47 use Mojo::IOLoop::Subprocess;
51 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase %nothereslug
52 $maxbadcount $msgpolltime $default_pagelth $cmdimportdir $users $maxusers);
54 %Cache = (); # cache of dynamically loaded routine's mod times
55 %cmd_cache = (); # cache of short names
56 $errstr = (); # error string from eval
57 %aliases = (); # aliases for (parts of) commands
58 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
59 $maxbadcount = 3; # no of bad words allowed before disconnection
60 $msgpolltime = 3600; # the time between polls for new messages
61 $cmdimportdir = "$main::root/cmd_import"; # the base directory for importing command scripts
62 # this does not exist as default, you need to create it manually
63 $users = 0; # no of users on this node currently
64 $maxusers = 0; # max no users on this node for this run
67 # obtain a new connection this is derived from dxchannel
72 my $self = DXChannel::alloc(@_);
74 # routing, this must go out here to prevent race condx
77 # my @rout = $main::routeroot->add_user($call, Route::here(1));
78 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
80 # ALWAYS output the user
81 my $ref = Route::User::get($call);
83 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref);
84 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, $ref) unless $DXProt::pc92_slug_changes;
90 # this is how a a connection starts, you get a hello message and the motd with
91 # possibly some other messages asking you to set various things up if you are
92 # new (or nearly new and slacking) user.
96 my ($self, $line, $sort) = @_;
97 my $user = $self->{user};
98 my $call = $self->{call};
99 my $name = $user->{name};
102 my $host = $self->{conn}->peerhost;
103 $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
105 $self->{hostname} = $host;
107 $self->{name} = $name ? $name : $call;
108 $self->send($self->msg('l2',$self->{name}));
109 $self->state('prompt'); # a bit of room for further expansion, passwords etc
110 $self->{priv} = $user->priv || 0;
111 $self->{lang} = $user->lang || $main::lang || 'en';
112 my $pagelth = $user->pagelth;
113 $pagelth = $default_pagelth unless defined $pagelth;
114 $self->{pagelth} = $pagelth;
115 ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
116 if ($line =~ /host=/) {
117 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
118 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
120 ($h) = $line =~ /host=([\da..fA..F:]+)/;
121 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
123 $self->{hostname} = $h if $h;
125 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
126 $self->{consort} = $line; # save the connection type
128 LogDbg('DXCommand', "$call connected from $self->{hostname}");
130 # set some necessary flags on the user if they are connecting
131 $self->{beep} = $user->wantbeep;
132 $self->{ann} = $user->wantann;
133 $self->{wwv} = $user->wantwwv;
134 $self->{wcy} = $user->wantwcy;
135 $self->{talk} = $user->wanttalk;
136 $self->{wx} = $user->wantwx;
137 $self->{dx} = $user->wantdx;
138 $self->{logininfo} = $user->wantlogininfo;
139 $self->{ann_talk} = $user->wantann_talk;
141 $self->{prompt} = $user->prompt if $user->prompt;
142 $self->{lastmsgpoll} = 0;
144 # sort out new dx spot stuff
145 $user->wantdxcq(0) unless defined $user->{wantdxcq};
146 $user->wantdxitu(0) unless defined $user->{wantdxitu};
147 $user->wantusstate(0) unless defined $user->{wantusstate};
149 # sort out registration
150 if ($main::reqreg == 1) {
151 $self->{registered} = $user->registered;
152 } elsif ($main::reqreg == 2) {
153 $self->{registered} = !$user->registered;
155 $self->{registered} = 1;
158 # send the relevant MOTD
161 # sort out privilege reduction
162 $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
166 $nossid =~ s/-\d+$//;
168 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
169 || Filter::read_in('spots', $nossid, 0)
170 || Filter::read_in('spots', 'user_default', 0);
171 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
172 || Filter::read_in('wwv', $nossid, 0)
173 || Filter::read_in('wwv', 'user_default', 0);
174 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
175 || Filter::read_in('wcy', $nossid, 0)
176 || Filter::read_in('wcy', 'user_default', 0);
177 $self->{annfilter} = Filter::read_in('ann', $call, 0)
178 || Filter::read_in('ann', $nossid, 0)
179 || Filter::read_in('ann', 'user_default', 0) ;
181 # clean up qra locators
182 my $qra = $user->qra;
183 $qra = undef if ($qra && !DXBearing::is_qra($qra));
185 my $lat = $user->lat;
186 my $long = $user->long;
187 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
191 my $echo = $user->wantecho;
193 $self->send_now('E', "0");
194 $self->send($self->msg('echow'));
195 $self->conn->echo($echo) if $self->conn->can('echo');
198 $self->tell_login('loginu');
199 $self->tell_buddies('loginb');
201 # do we need to send a forward/opernam?
202 my $lastoper = $user->lastoper || 0;
203 my $homenode = $user->homenode || "";
204 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
205 run_cmd($main::me, "forward/opernam $call");
206 $user->lastoper($main::systime + ((int rand(10)) * 86400));
209 # run a script send the output to the punter
210 my $script = new Script(lc $call) || new Script('user_default');
211 $script->run($self) if $script;
214 $self->send($self->run_cmd("show/cluster"));
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 my $t0 = [gettimeofday];
549 eval { @ans = &{"${package}::handle"}($self, $args) };
551 DXDebug::dbgprintring(25);
552 return (DXDebug::shortmess($@));
554 if (isdbg('progress')) {
555 my $msecs = _diffms($t0);
556 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
557 dbg($s) if $cmd !~ /^(?:echo|blank)/ || isdbg('echo'); # cut down a bit on HRD and other clients' noise
560 dbg("cmd: $package not present") if isdbg('command');
561 return $self->_error_out('e1');
564 dbg("cmd: $cmd not found") if isdbg('command');
565 return $self->_error_out('e1');
571 delete $self->{errors};
573 if (++$self->{errors} > $DXChannel::maxerrors) {
574 $self->send($self->msg('e26'));
579 return map {s/([^\s])\s+$/$1/; $_} @ans;
583 # This is called from inside the main cluster processing loop and is used
584 # for despatching commands that are doing some long processing job
589 my @dxchan = DXChannel::get_all();
593 foreach $dxchan (@dxchan) {
594 next unless $dxchan->is_user;
596 # send a outstanding message prompt if required
597 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
598 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
599 $dxchan->lastmsgpoll($t);
602 # send a prompt if no activity out on this channel
603 if ($t >= $dxchan->t + $main::user_interval) {
604 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
608 $maxusers = $users if $users > $maxusers;
611 while (my ($k, $v) = each %nothereslug) {
612 if ($main::systime >= $v + 300) {
613 delete $nothereslug{$k};
621 # finish up a user context
626 my $call = $self->call;
628 return if $self->{disconnecting}++;
630 delete $self->{senddbg};
632 my $uref = Route::User::get($call);
635 # @rout = $main::routeroot->del_user($uref);
636 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
638 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
640 # issue a pc17 to everybody interested
641 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
642 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
644 confess "trying to disconnect a non existant user $call";
647 # I was the last node visited
648 $self->user->node($main::mycall);
650 # send info to all logged in thingies
651 $self->tell_login('logoutu');
652 $self->tell_buddies('logoutb');
654 LogDbg('DXCommand', "$call disconnected");
656 $self->SUPER::disconnect;
660 # short cut to output a prompt
667 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
669 my $call = $self->call;
670 my $date = cldate($main::systime);
671 my $time = ztime($main::systime);
672 my $prompt = $self->{prompt} || $self->msg('pr');
674 $call = "($call)" unless $self->here;
675 $prompt =~ s/\%C/$call/g;
676 $prompt =~ s/\%D/$date/g;
677 $prompt =~ s/\%T/$time/g;
678 $prompt =~ s/\%M/$main::mycall/g;
680 $self->send($prompt);
683 # broadcast a message to all users [except those mentioned after buffer]
686 my $pkg = shift; # ignored
687 my $s = shift; # the line to be rebroadcast
689 foreach my $dxchan (DXChannel::get_all()) {
690 next unless $dxchan->is_user; # only interested in user channels
691 next if grep $dxchan == $_, @_;
692 $dxchan->send($s); # send it
696 # gimme all the users
699 goto &DXChannel::get_all_users;
702 # run a script for this user
706 my $silent = shift || 0;
711 # search for the command in the cache of short->long form commands
716 my ($path, $short_cmd, $suffix) = @_;
719 # commands are lower case
720 $short_cmd = lc $short_cmd;
721 dbg("command: $path $short_cmd\n") if isdbg('command');
723 # do some checking for funny characters
724 return () if $short_cmd =~ /\/$/;
726 # return immediately if we have it
727 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
728 if ($apath && $acmd) {
729 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
730 return ($apath, $acmd);
734 my @parts = split '/', $short_cmd;
738 while (my $p = shift @parts) {
739 opendir(D, $curdir) or confess "can't open $curdir $!";
743 # if this isn't the last part
746 foreach my $l (sort @ls) {
748 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
749 dbg("got dir: $curdir/$l\n") if isdbg('command');
756 # only proceed if we find the directory asked for
757 return () unless $found;
759 foreach my $l (sort @ls) {
761 next unless $l =~ /\.$suffix$/;
762 if ($p eq substr($l, 0, length $p)) {
763 $l =~ s/\.$suffix$//;
764 $dirfn = "" unless $dirfn;
765 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
766 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
767 return ($path, "$dirfn$l");
776 # clear the command name cache
781 for my $k (keys %Cache) {
782 unless ($k =~ /cmd_cache/) {
783 dbg("Undefining cmd $k") if isdbg('command');
784 undef $DXCommandmode::{"${k}::"};
788 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
792 # the persistant execution of things from the command directories
795 # This allows perl programs to call functions dynamically
797 # This has been nicked directly from the perlembed pages
799 #require Devel::Symdump;
801 sub valid_package_name {
803 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
806 return "cmd_$string";
810 # this bit of magic finds a command in the offered directory
814 my $package = valid_package_name($cmdname);
815 my $filename = "$path/$cmdname.pl";
816 my $mtime = -M $filename;
818 # return if we can't find it
820 unless (defined $mtime) {
821 $errstr = DXM::msg('e1');
825 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
826 #we have compiled this subroutine already,
827 #it has not been updated on disk, nothing left to do
828 #print STDERR "already compiled $package->handler\n";
829 dbg("find_cmd_name: $package cached") if isdbg('command');
832 my $sub = readfilestr($filename);
834 $errstr = "Syserr: can't open '$filename' $!";
838 #wrap the code into a subroutine inside our unique package
839 my $eval = qq(package DXCommandmode::$package; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
842 if ($sub =~ m|\s*sub\s+handle\n|) {
845 $eval .= qq(sub handle { $sub });
849 my @list = split /\n/, $eval;
852 dbg($_ . "\n") if isdbg('eval');
856 # get rid of any existing sub and try to compile the new one
859 if (exists $Cache{$package}) {
860 dbg("find_cmd_name: Redefining $package") if isdbg('command');
861 undef $DXCommandmode::{"${package}::"};
862 delete $Cache{$package};
864 dbg("find_cmd_name: Defining $package") if isdbg('command');
869 $Cache{$package} = {mtime => $mtime } unless $@;
872 return "DXCommandmode::$package";
880 $self->SUPER::send(dd(['cmd',$_]));
883 $self->SUPER::send(@_);
889 my ($self, $let, $buf) = @_;
890 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
891 if ($self->{enhanced}) {
892 $self->send_later($let, $buf);
901 # send a talk message here
904 my ($self, $from, $to, $via, $line, $onode) = @_;
905 $line =~ s/\\5E/\^/g;
908 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
910 $self->local_send('T', "$to de $from: $line");
913 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
914 # send a 'not here' message if required
915 unless ($self->{here} && $from ne $to) {
916 my $key = "$to$from";
917 unless (exists $nothereslug{$key}) {
919 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
920 my $name = $self->user->name || $to;
921 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
922 $nothereslug{$key} = $main::systime;
923 $dxchan->talk($to, $from, undef, $s);
940 if (!$self->{ann_talk} && $to ne $self->{call}) {
941 my $call = AnnTalk::is_talk_candidate($_[0], $text);
945 if ($self->{annfilter}) {
946 ($filter, $hops) = $self->{annfilter}->it(@_ );
947 return unless $filter;
950 unless ($self->{ann}) {
951 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
953 return if $target eq 'SYSOP' && $self->{priv} < 5;
956 $buf = dd(['ann', $to, $target, $text, @_])
958 $buf = "$to$target de $_[0]: $text";
960 $buf .= "\a\a" if $self->{beep};
962 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
976 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
978 $text =~ s/^\#\d+ //;
981 $buf = dd(['chat', $to, $target, $text, @_])
983 $buf = "$target de $_[0]: $text";
985 $buf .= "\a\a" if $self->{beep};
987 $self->local_send('C', $buf);
994 my $t = ztime($_[2]);
996 my $clth = $self->{consort} eq 'local' ? 29 : 30;
997 my $comment = substr (($_[3] || ''), 0, $clth);
998 $comment .= ' ' x ($clth - length($comment));
999 if ($self->{user}->wantgrid) {
1000 my $ref = DXUser::get_current($_[4]);
1002 $loc = $ref->qra || '';
1003 $loc = ' ' . substr($loc, 0, 4) if $loc;
1007 if ($self->{user}->wantdxitu) {
1008 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
1009 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
1010 } elsif ($self->{user}->wantdxcq) {
1011 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
1012 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
1013 } elsif ($self->{user}->wantusstate) {
1014 $loc = ' ' . $_[13] if $_[13];
1015 $comment = substr($comment, 0, $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12];
1018 return sprintf "DX de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1026 my $isolate = shift;
1027 return unless $self->{dx};
1029 my ($filter, $hops);
1031 if ($self->{spotsfilter}) {
1032 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1033 return unless $filter;
1036 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1039 if ($self->{ve7cc}) {
1040 $buf = VE7CC::dx_spot($self, @_);
1041 } elsif ($self->{gtk}) {
1042 my ($dxloc, $byloc);
1044 my $ref = DXUser::get_current($_[4]);
1047 $byloc = substr($byloc, 0, 4) if $byloc;
1051 $spot =~ s|/\w{1,4}$||;
1052 $ref = DXUser::get_current($spot);
1055 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1057 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1060 $buf = $self->format_dx_spot(@_);
1061 $buf .= "\a\a" if $self->{beep};
1065 $self->local_send('X', $buf);
1072 my $isolate = shift;
1073 my ($filter, $hops);
1075 return unless $self->{wwv};
1077 if ($self->{wwvfilter}) {
1078 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1079 return unless $filter;
1084 $buf = dd(['wwv', @_])
1086 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1087 $buf .= "\a\a" if $self->{beep};
1090 $self->local_send('V', $buf);
1097 my $isolate = shift;
1098 my ($filter, $hops);
1100 return unless $self->{wcy};
1102 if ($self->{wcyfilter}) {
1103 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1104 return unless $filter;
1109 $buf = dd(['wcy', @_])
1111 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1112 $buf .= "\a\a" if $self->{beep};
1114 $self->local_send('Y', $buf);
1117 # broadcast debug stuff to all interested parties
1120 my $s = shift; # the line to be rebroadcast
1122 foreach my $dxchan (DXChannel::get_all_users) {
1123 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1124 if ($dxchan->{gtk}) {
1125 $dxchan->send_later('L', dd(['db', $s]));
1127 $dxchan->send_later('L', $s);
1138 if ($self->state eq 'enterbody') {
1139 my $loc = $self->{loc} || confess "local var gone missing" ;
1140 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1142 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1144 $self->state('prompt');
1145 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1146 push @out, $self->msg('m10');
1147 delete $loc->{lines};
1148 delete $self->{loc};
1150 $self->state('prompt');
1152 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1153 # i.e. it ain't and end or abort, therefore store the line
1156 confess "Invalid state $self->{state}";
1161 sub store_startup_script
1164 my $loc = $self->{loc} || confess "local var gone missing" ;
1166 my $call = $loc->{call} || confess "callsign gone missing";
1167 confess "lines array gone missing" unless ref $loc->{lines};
1168 my $r = Script::store($call, $loc->{lines});
1171 push @out, $self->msg('m19', $call, $r);
1173 push @out, $self->msg('m20', $call);
1176 push @out, "error opening startup script $call $!";
1181 # Import any commands contained in any files in import_cmd directory
1183 # If the filename has a recogisable callsign as some delimited part
1184 # of it, then this is the user the command will be run as.
1188 # are there any to do in this directory?
1189 return unless -d $cmdimportdir;
1190 unless (opendir(DIR, $cmdimportdir)) {
1191 LogDbg('err', "can\'t open $cmdimportdir $!");
1195 my @names = readdir(DIR);
1199 return unless @names;
1201 foreach $name (@names) {
1202 next if $name =~ /^\./;
1204 my $s = Script->new($name, $cmdimportdir);
1206 LogDbg('DXCommand', "Run import cmd file $name");
1207 my @cat = split /[^A-Za-z0-9]+/, $name;
1208 my ($call) = grep {is_callsign(uc $_)} @cat;
1209 $call ||= $main::mycall;
1214 $s->inscript(0); # switch off script checks
1216 if ($call eq $main::mycall) {
1217 @out = $s->run($main::me, 1);
1219 my $dxchan = DXChannel::get($call);
1221 @out = $s->run($dxchan, 1);
1223 my $u = DXUser::get($call);
1225 $dxchan = $main::me;
1226 my $old = $dxchan->{call};
1227 my $priv = $dxchan->{priv};
1228 my $user = $dxchan->{user};
1229 $dxchan->{call} = $call;
1230 $dxchan->{priv} = $u->priv;
1231 $dxchan->{user} = $u;
1232 @out = $s->run($dxchan, 1);
1233 $dxchan->{call} = $old;
1234 $dxchan->{priv} = $priv;
1235 $dxchan->{user} = $user;
1237 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1243 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1246 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1247 unlink "$cmdimportdir/$name";
1252 sub print_find_reply
1254 my ($self, $node, $target, $flag, $ms) = @_;
1255 my $sort = $flag == 2 ? "External" : "Local";
1256 $self->send("$sort $target found at $node in $ms ms" );
1259 # send the most relevant motd
1265 unless ($self->{registered}) {
1266 $motd = "${main::motd}_nor_$self->{lang}";
1267 $motd = "${main::motd}_nor" unless -e $motd;
1269 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1270 $motd = $main::motd unless $motd && -e $motd;
1271 if ($self->conn->ax25) {
1273 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1275 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1278 $self->send_file($motd) if -e $motd;
1282 # Punt off a long running command into a separate process
1284 # This is called from commands to run some potentially long running
1285 # function. The process forks and then runs the function and returns
1286 # the result back to the cmd.
1288 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1289 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1290 # THE CURRENT CONTEXT!!
1292 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1298 my $call = $self->{call};
1302 my $cb = delete $args{cb};
1303 my $prefix = delete $args{prefix};
1304 my $progress = delete $args{progress};
1305 my $args = delete $args{args} || [];
1306 my $t0 = [gettimeofday];
1310 # just behave normally if something has set the "one-shot" _nospawn in the channel
1311 if ($self->{_nospawn}) {
1312 eval { @out = $cmdref->(@$args); };
1314 DXDebug::dbgprintring(25);
1315 push @out, DXDebug::shortmess($@);
1320 my $fc = DXSubprocess->new;
1321 # $fc->serializer(\&encode_json);
1322 # $fc->deserializer(\&decode_json);
1326 if (isdbg('spawn')) {
1327 my $s = __PACKAGE__ . qq{ line: "$line"};
1328 $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1331 eval { @out = $cmdref->(@$args); };
1333 DXDebug::dbgprintring(25);
1334 push @out, DXDebug::shortmess($@);
1340 my ($fc, $err, @res) = @_;
1341 my $dxchan = DXChannel::get($call);
1342 return unless $dxchan;
1345 my $s = "DXProt::spawn_cmd: call $call error $err";
1346 dbg($s) if isdbg('chan');
1351 # transform output if required
1352 @res = $cb->($dxchan, @res);
1355 if (defined $prefix) {
1356 $dxchan->send(map {"$prefix$_"} @res);
1358 $dxchan->send(@res);
1361 diffms(__PACKAGE__, "by $call", $line, $t0, scalar @res) if isdbg('progress');
1369 return ($users, $maxusers);