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);
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;
139 $self->{wantrbn} = $user->wantrbn;
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 # establish slug queue, if required
159 $self->{sluggedpc11s} = [];
160 $self->{isslugged} = $DXProt::pc92_slug_changes + $main::systime if $DXProt::pc92_slug_changes;
162 # send the relevant MOTD
165 # sort out privilege reduction
166 $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
170 $nossid =~ s/-\d+$//;
172 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
173 || Filter::read_in('spots', $nossid, 0)
174 || Filter::read_in('spots', 'user_default', 0);
175 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
176 || Filter::read_in('wwv', $nossid, 0)
177 || Filter::read_in('wwv', 'user_default', 0);
178 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
179 || Filter::read_in('wcy', $nossid, 0)
180 || Filter::read_in('wcy', 'user_default', 0);
181 $self->{annfilter} = Filter::read_in('ann', $call, 0)
182 || Filter::read_in('ann', $nossid, 0)
183 || Filter::read_in('ann', 'user_default', 0) ;
184 $self->{rbnfilter} = Filter::read_in('rbn', $call, 0)
185 || Filter::read_in('rbn', $nossid, 0)
186 || Filter::read_in('rbn', 'user_default', 0);
188 # clean up qra locators
189 my $qra = $user->qra;
190 $qra = undef if ($qra && !DXBearing::is_qra($qra));
192 my $lat = $user->lat;
193 my $long = $user->long;
194 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
198 my $echo = $user->wantecho;
200 $self->send_now('E', "0");
201 $self->send($self->msg('echow'));
202 $self->conn->echo($echo) if $self->conn->can('echo');
205 $self->tell_login('loginu');
206 $self->tell_buddies('loginb');
208 # do we need to send a forward/opernam?
209 my $lastoper = $user->lastoper || 0;
210 my $homenode = $user->homenode || "";
211 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
212 run_cmd($main::me, "forward/opernam $call");
213 $user->lastoper($main::systime + ((int rand(10)) * 86400));
216 # run a script send the output to the punter
217 my $script = new Script(lc $call) || new Script('user_default');
218 $script->run($self) if $script;
221 $self->send($self->run_cmd("show/cluster"));
223 # send prompts for qth, name and things
224 $self->send($self->msg('namee1')) if !$user->name;
225 $self->send($self->msg('qthe1')) if !$user->qth;
226 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
227 $self->send($self->msg('hnodee1')) if !$user->qth;
228 $self->send($self->msg('m9')) if DXMsg::for_me($call);
230 # send out any buddy messages for other people that are online
231 foreach my $call (@{$user->buddies}) {
232 my $ref = Route::User::get($call);
234 foreach my $node ($ref->parents) {
235 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
240 $self->lastmsgpoll($main::systime);
245 # This is the normal command prompt driver
254 # save this for them's that need it
255 my $rawline = $cmdline;
257 # remove leading and trailing spaces
258 $cmdline =~ s/^\s*(.*)\s*$/$1/;
260 if ($self->{state} eq 'page') {
261 my $i = $self->{pagelth};
262 my $ref = $self->{pagedata};
265 # abort if we get a line starting in with a
266 if ($cmdline =~ /^a/io) {
271 # send a tranche of data
272 while ($i-- > 0 && @$ref) {
273 my $line = shift @$ref;
274 $line =~ s/\s+$//o; # why am having to do this?
278 # reset state if none or else chuck out an intermediate prompt
280 $tot -= $self->{pagelth};
281 $self->send($self->msg('page', $tot));
283 $self->state('prompt');
285 } elsif ($self->{state} eq 'sysop') {
286 my $passwd = $self->{user}->passwd;
288 my @pw = grep {$_ !~ /\s/} split //, $passwd;
289 my @l = @{$self->{passwd}};
290 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
291 if ($cmdline =~ /$str/) {
292 $self->{priv} = $self->{user}->priv;
294 $self->send($self->msg('sorry'));
297 $self->send($self->msg('sorry'));
299 $self->state('prompt');
300 } elsif ($self->{state} eq 'passwd') {
301 my $passwd = $self->{user}->passwd;
302 if ($passwd && $cmdline eq $passwd) {
303 $self->send($self->msg('pw1'));
304 $self->state('passwd1');
306 $self->conn->{echo} = $self->conn->{decho};
307 delete $self->conn->{decho};
308 $self->send($self->msg('sorry'));
309 $self->state('prompt');
311 } elsif ($self->{state} eq 'passwd1') {
312 $self->{passwd} = $cmdline;
313 $self->send($self->msg('pw2'));
314 $self->state('passwd2');
315 } elsif ($self->{state} eq 'passwd2') {
316 if ($cmdline eq $self->{passwd}) {
317 $self->{user}->passwd($cmdline);
318 $self->send($self->msg('pw3'));
320 $self->send($self->msg('pw4'));
322 $self->conn->{echo} = $self->conn->{decho};
323 delete $self->conn->{decho};
324 $self->state('prompt');
325 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
326 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
327 for (@{$self->{talklist}}) {
328 if ($self->{state} eq 'talk') {
329 $self->send_talks($_, $self->msg('talkend'));
331 $self->local_send('C', $self->msg('chatend', $_));
334 $self->state('prompt');
335 delete $self->{talklist};
336 } elsif ($cmdline =~ m|^/+\w+|) {
338 my $sendit = $cmdline =~ s|^/+||;
339 my @in = $self->run_cmd($cmdline);
340 $self->send_ans(@in);
341 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
342 foreach my $l (@in) {
344 if (@bad = BadWords::check($l)) {
345 $self->badcount(($self->badcount||0) + @bad);
346 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
348 for (@{$self->{talklist}}) {
349 if ($self->{state} eq 'talk') {
350 $self->send_talks($_, $l);
352 send_chats($self, $_, $l)
358 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
359 } elsif ($self->{talklist} && @{$self->{talklist}}) {
360 # send what has been said to whoever is in this person's talk list
362 if (@bad = BadWords::check($cmdline)) {
363 $self->badcount(($self->badcount||0) + @bad);
364 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
366 for (@{$self->{talklist}}) {
367 if ($self->{state} eq 'talk') {
368 $self->send_talks($_, $rawline);
370 send_chats($self, $_, $rawline);
374 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
375 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
378 $self->state('prompt');
380 } elsif (my $func = $self->{func}) {
383 if (ref $self->{edit}) {
384 eval { @ans = $self->{edit}->$func($self, $rawline)};
386 eval { @ans = &{$self->{func}}($self, $rawline) };
389 $self->send_ans("Syserr: on stored func $self->{func}", $@);
390 delete $self->{func};
391 $self->state('prompt');
394 $self->send_ans(@ans);
396 $self->send_ans(run_cmd($self, $cmdline));
399 # check for excessive swearing
400 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
401 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
406 # send a prompt only if we are in a prompt state
407 $self->prompt() if $self->{state} =~ /^prompt/o;
410 # send out the talk messages taking into account vias and connectivity
413 my ($self, $ent, $line) = @_;
415 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
416 $to = $ent unless $to;
417 my $call = $via && $via ne '*' ? $via : $to;
418 my $clref = Route::get($call);
419 my $dxchan = $clref->dxchan if $clref;
421 $dxchan->talk($self->{call}, $to, undef, $line);
423 $self->send($self->msg('disc2', $via ? $via : $to));
424 my @l = grep { $_ ne $ent } @{$self->{talklist}};
426 $self->{talklist} = \@l;
428 delete $self->{talklist};
429 $self->state('prompt');
440 my $msgid = DXProt::nextchatmsgid();
441 $text = "#$msgid $text";
442 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
450 for (@{$self->{talklist}}) {
451 my ($to, $via) = /(\S+)>(\S+)/;
455 return $self->msg($prompt, join(',', @call));
461 return $self->special_prompt('talkprompt');
467 return $self->special_prompt('chatprompt');
471 # send a load of stuff to a command user with page prompting
479 if ($self->{pagelth} && @_ > $self->{pagelth}) {
481 for ($i = $self->{pagelth}; $i-- > 0; ) {
483 $line =~ s/\s+$//o; # why am having to do this?
486 $self->{pagedata} = [ @_ ];
487 $self->state('page');
488 $self->send($self->msg('page', scalar @_));
501 # this is the thing that preps for running the command, it is done like this for the
502 # benefit of remote command execution
508 my $user = $self->{user};
509 my $call = $self->{call};
513 return () if length $cmdline == 0;
515 # split the command line up into parts, the first part is the command
516 my ($cmd, $args) = split /\s+/, $cmdline, 2;
517 $args = "" unless defined $args;
522 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
523 LogDbg('DXCommand', "cmd: $self->{call} - invalid characters in '$cmd'");
524 return $self->_error_out('e1');
527 # strip out // on command only
532 dbg("cmd: $cmd") if isdbg('command');
534 # alias it if possible
535 my $acmd = CmdAlias::get_cmd($cmd);
537 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
538 $args = "" unless defined $args;
539 dbg("cmd: aliased $cmd $args") if isdbg('command');
542 # first expand out the entry to a command
543 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
544 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
547 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
549 my $package = find_cmd_name($path, $fcmd);
552 if ($package && $self->can("${package}::handle")) {
554 dbg("cmd: package $package") if isdbg('command');
555 my $t0 = [gettimeofday];
556 eval { @ans = &{"${package}::handle"}($self, $args) };
558 DXDebug::dbgprintring(25);
559 return (DXDebug::shortmess($@));
561 if (isdbg('progress')) {
562 my $msecs = _diffms($t0);
563 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
564 dbg($s) if $cmd !~ /^(?:echo|blank)/ || isdbg('echo'); # cut down a bit on HRD and other clients' noise
567 dbg("cmd: $package not present") if isdbg('command');
568 return $self->_error_out('e1');
571 dbg("cmd: $cmd not found") if isdbg('command');
572 return $self->_error_out('e1');
578 delete $self->{errors};
580 if (++$self->{errors} > $DXChannel::maxerrors) {
581 $self->send($self->msg('e26'));
586 return map {s/([^\s])\s+$/$1/; $_} @ans;
590 # This is called from inside the main cluster processing loop and is used
591 # for despatching commands that are doing some long processing job
596 my @dxchan = DXChannel::get_all();
600 foreach $dxchan (@dxchan) {
601 next unless $dxchan->is_user;
603 # send a outstanding message prompt if required
604 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
605 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
606 $dxchan->lastmsgpoll($t);
609 # send a prompt if no activity out on this channel
610 if ($t >= $dxchan->t + $main::user_interval) {
611 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
615 $maxusers = $users if $users > $maxusers;
618 while (my ($k, $v) = each %nothereslug) {
619 if ($main::systime >= $v + 300) {
620 delete $nothereslug{$k};
628 # finish up a user context
633 my $call = $self->call;
635 return if $self->{disconnecting}++;
637 delete $self->{senddbg};
639 my $uref = Route::User::get($call);
642 # @rout = $main::routeroot->del_user($uref);
643 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
645 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
647 # issue a pc17 to everybody interested
648 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
649 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
651 confess "trying to disconnect a non existant user $call";
654 # I was the last node visited
655 $self->user->node($main::mycall);
657 # send info to all logged in thingies
658 $self->tell_login('logoutu');
659 $self->tell_buddies('logoutb');
661 LogDbg('DXCommand', "$call disconnected");
663 $self->SUPER::disconnect;
667 # short cut to output a prompt
674 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
676 my $call = $self->call;
677 my $date = cldate($main::systime);
678 my $time = ztime($main::systime);
679 my $prompt = $self->{prompt} || $self->msg('pr');
681 $call = "($call)" unless $self->here;
682 $prompt =~ s/\%C/$call/g;
683 $prompt =~ s/\%D/$date/g;
684 $prompt =~ s/\%T/$time/g;
685 $prompt =~ s/\%M/$main::mycall/g;
687 $self->send($prompt);
690 # broadcast a message to all users [except those mentioned after buffer]
693 my $pkg = shift; # ignored
694 my $s = shift; # the line to be rebroadcast
696 foreach my $dxchan (DXChannel::get_all()) {
697 next unless $dxchan->is_user; # only interested in user channels
698 next if grep $dxchan == $_, @_;
699 $dxchan->send($s); # send it
703 # gimme all the users
706 goto &DXChannel::get_all_users;
709 # run a script for this user
713 my $silent = shift || 0;
718 # search for the command in the cache of short->long form commands
723 my ($path, $short_cmd, $suffix) = @_;
726 # commands are lower case
727 $short_cmd = lc $short_cmd;
728 dbg("command: $path $short_cmd\n") if isdbg('command');
730 # do some checking for funny characters
731 return () if $short_cmd =~ /\/$/;
733 # return immediately if we have it
734 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
735 if ($apath && $acmd) {
736 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
737 return ($apath, $acmd);
741 my @parts = split '/', $short_cmd;
745 while (my $p = shift @parts) {
746 opendir(D, $curdir) or confess "can't open $curdir $!";
750 # if this isn't the last part
753 foreach my $l (sort @ls) {
755 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
756 dbg("got dir: $curdir/$l\n") if isdbg('command');
763 # only proceed if we find the directory asked for
764 return () unless $found;
766 foreach my $l (sort @ls) {
768 next unless $l =~ /\.$suffix$/;
769 if ($p eq substr($l, 0, length $p)) {
770 $l =~ s/\.$suffix$//;
771 $dirfn = "" unless $dirfn;
772 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
773 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
774 return ($path, "$dirfn$l");
783 # clear the command name cache
788 for my $k (keys %Cache) {
789 unless ($k =~ /cmd_cache/) {
790 dbg("Undefining cmd $k") if isdbg('command');
791 undef $DXCommandmode::{"${k}::"};
795 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
799 # the persistant execution of things from the command directories
802 # This allows perl programs to call functions dynamically
804 # This has been nicked directly from the perlembed pages
806 #require Devel::Symdump;
808 sub valid_package_name {
810 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
813 return "cmd_$string";
817 # this bit of magic finds a command in the offered directory
821 my $package = valid_package_name($cmdname);
822 my $filename = "$path/$cmdname.pl";
823 my $mtime = -M $filename;
825 # return if we can't find it
827 unless (defined $mtime) {
828 $errstr = DXM::msg('e1');
832 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
833 #we have compiled this subroutine already,
834 #it has not been updated on disk, nothing left to do
835 #print STDERR "already compiled $package->handler\n";
836 dbg("find_cmd_name: $package cached") if isdbg('command');
839 my $sub = readfilestr($filename);
841 $errstr = "Syserr: can't open '$filename' $!";
845 #wrap the code into a subroutine inside our unique package
846 my $eval = qq(package DXCommandmode::$package; use 5.10.1; use POSIX qw{:math_h}; use DXLog; use DXDebug; use DXUser; use DXUtil; our \@ISA = qw{DXCommandmode}; );
849 if ($sub =~ m|\s*sub\s+handle\n|) {
852 $eval .= qq(sub handle { $sub });
856 my @list = split /\n/, $eval;
859 dbg($_ . "\n") if isdbg('eval');
863 # get rid of any existing sub and try to compile the new one
866 if (exists $Cache{$package}) {
867 dbg("find_cmd_name: Redefining $package") if isdbg('command');
868 undef $DXCommandmode::{"${package}::"};
869 delete $Cache{$package};
871 dbg("find_cmd_name: Defining $package") if isdbg('command');
876 $Cache{$package} = {mtime => $mtime } unless $@;
879 return "DXCommandmode::$package";
887 $self->SUPER::send(dd(['cmd',$_]));
890 $self->SUPER::send(@_);
896 my ($self, $let, $buf) = @_;
897 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
898 if ($self->{enhanced}) {
899 $self->send_later($let, $buf);
908 # send a talk message here
911 my ($self, $from, $to, $via, $line, $onode) = @_;
912 $line =~ s/\\5E/\^/g;
915 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
917 $self->local_send('T', "$to de $from: $line");
920 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
921 # send a 'not here' message if required
922 unless ($self->{here} && $from ne $to) {
923 my $key = "$to$from";
924 unless (exists $nothereslug{$key}) {
926 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
927 my $name = $self->user->name || $to;
928 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
929 $nothereslug{$key} = $main::systime;
930 $dxchan->talk($to, $from, undef, $s);
947 if (!$self->{ann_talk} && $to ne $self->{call}) {
948 my $call = AnnTalk::is_talk_candidate($_[0], $text);
952 if ($self->{annfilter}) {
953 ($filter, $hops) = $self->{annfilter}->it(@_ );
954 return unless $filter;
957 unless ($self->{ann}) {
958 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
960 return if $target eq 'SYSOP' && $self->{priv} < 5;
963 $buf = dd(['ann', $to, $target, $text, @_])
965 $buf = "$to$target de $_[0]: $text";
967 $buf .= "\a\a" if $self->{beep};
969 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
983 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
985 $text =~ s/^\#\d+ //;
988 $buf = dd(['chat', $to, $target, $text, @_])
990 $buf = "$target de $_[0]: $text";
992 $buf .= "\a\a" if $self->{beep};
994 $self->local_send('C', $buf);
1001 my $t = ztime($_[2]);
1003 my $clth = 31 + $self->{width} - 80; # allow comment to grow according the screen width
1004 # --$clth if $self->{consort} eq 'local';
1006 my $comment = substr (($_[3] || ''), 0, $clth);
1007 $comment =~ s/\t/ /g;
1009 $comment .= ' ' x ($clth - (length($comment)));
1011 if ($self->{user}->wantgrid) {
1012 my $ref = DXUser::get_current($_[1]);
1013 if ($ref && $ref->qra) {
1014 my $cloc = ' ' . substr($ref->qra, 0, 4);
1015 $comment = substr $comment, 0, ($clth - (length($comment)+length($cloc)));
1019 $origin =~ s/-#$//; # sigh......
1020 $ref = DXUser::get_current($origin);
1021 if ($ref && $ref->qra) {
1022 $loc = ' ' . substr($ref->qra, 0, 4);
1024 } elsif ($self->{user}->wantdxitu) {
1025 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
1026 $comment = substr($comment, 0, $clth-3) . ' ' . sprintf("%2d", $_[8]) if defined $_[8];
1027 } elsif ($self->{user}->wantdxcq) {
1028 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
1029 $comment = substr($comment, 0, $clth-3) . ' ' . sprintf("%2d", $_[9]) if defined $_[9];
1030 } elsif ($self->{user}->wantusstate) {
1031 $loc = ' ' . $_[13] if $_[13];
1032 $comment = substr($comment, 0, $clth-3) . ' ' . $_[12] if $_[12];
1035 return sprintf "DX de %-9.9s%10.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1043 my $isolate = shift;
1044 return unless $self->{dx};
1046 my ($filter, $hops);
1048 if ($self->{spotsfilter}) {
1049 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1050 return unless $filter;
1053 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1056 if ($self->{ve7cc}) {
1057 $buf = VE7CC::dx_spot($self, @_);
1058 } elsif ($self->{gtk}) {
1059 my ($dxloc, $byloc);
1061 my $ref = DXUser::get_current($_[4]);
1064 $byloc = substr($byloc, 0, 4) if $byloc;
1068 $spot =~ s|/\w{1,4}$||;
1069 $ref = DXUser::get_current($spot);
1072 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1074 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1077 $buf = $self->format_dx_spot(@_);
1078 $buf .= "\a\a" if $self->{beep};
1079 #$buf =~ s/\%5E/^/g;
1082 $self->local_send('X', $buf);
1089 my $isolate = shift;
1090 my ($filter, $hops);
1092 return unless $self->{wwv};
1094 if ($self->{wwvfilter}) {
1095 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1096 return unless $filter;
1101 $buf = dd(['wwv', @_])
1103 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1104 $buf .= "\a\a" if $self->{beep};
1107 $self->local_send('V', $buf);
1114 my $isolate = shift;
1115 my ($filter, $hops);
1117 return unless $self->{wcy};
1119 if ($self->{wcyfilter}) {
1120 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1121 return unless $filter;
1126 $buf = dd(['wcy', @_])
1128 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1129 $buf .= "\a\a" if $self->{beep};
1131 $self->local_send('Y', $buf);
1134 # broadcast debug stuff to all interested parties
1137 my $s = shift; # the line to be rebroadcast
1139 foreach my $dxchan (DXChannel::get_all_users) {
1140 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1141 if ($dxchan->{gtk}) {
1142 $dxchan->send_later('L', dd(['db', $s]));
1144 $dxchan->send_later('L', $s);
1155 if ($self->state eq 'enterbody') {
1156 my $loc = $self->{loc} || confess "local var gone missing" ;
1157 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1159 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1161 $self->state('prompt');
1162 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1163 push @out, $self->msg('m10');
1164 delete $loc->{lines};
1165 delete $self->{loc};
1167 $self->state('prompt');
1169 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1170 # i.e. it ain't and end or abort, therefore store the line
1173 confess "Invalid state $self->{state}";
1178 sub store_startup_script
1181 my $loc = $self->{loc} || confess "local var gone missing" ;
1183 my $call = $loc->{call} || confess "callsign gone missing";
1184 confess "lines array gone missing" unless ref $loc->{lines};
1185 my $r = Script::store($call, $loc->{lines});
1188 push @out, $self->msg('m19', $call, $r);
1190 push @out, $self->msg('m20', $call);
1193 push @out, "error opening startup script $call $!";
1198 # Import any commands contained in any files in import_cmd directory
1200 # If the filename has a recogisable callsign as some delimited part
1201 # of it, then this is the user the command will be run as.
1205 # are there any to do in this directory?
1206 return unless -d $cmdimportdir;
1207 unless (opendir(DIR, $cmdimportdir)) {
1208 LogDbg('err', "can\'t open $cmdimportdir $!");
1212 my @names = readdir(DIR);
1216 return unless @names;
1218 foreach $name (@names) {
1219 next if $name =~ /^\./;
1221 my $s = Script->new($name, $cmdimportdir);
1223 LogDbg('DXCommand', "Run import cmd file $name");
1224 my @cat = split /[^A-Za-z0-9]+/, $name;
1225 my ($call) = grep {is_callsign(uc $_)} @cat;
1226 $call ||= $main::mycall;
1231 $s->inscript(0); # switch off script checks
1233 if ($call eq $main::mycall) {
1234 @out = $s->run($main::me, 1);
1236 my $dxchan = DXChannel::get($call);
1238 @out = $s->run($dxchan, 1);
1240 my $u = DXUser::get($call);
1242 $dxchan = $main::me;
1243 my $old = $dxchan->{call};
1244 my $priv = $dxchan->{priv};
1245 my $user = $dxchan->{user};
1246 $dxchan->{call} = $call;
1247 $dxchan->{priv} = $u->priv;
1248 $dxchan->{user} = $u;
1249 @out = $s->run($dxchan, 1);
1250 $dxchan->{call} = $old;
1251 $dxchan->{priv} = $priv;
1252 $dxchan->{user} = $user;
1254 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1260 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1263 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1264 unlink "$cmdimportdir/$name";
1269 sub print_find_reply
1271 my ($self, $node, $target, $flag, $ms) = @_;
1272 my $sort = $flag == 2 ? "External" : "Local";
1273 $self->send("$sort $target found at $node in $ms ms" );
1276 # send the most relevant motd
1282 unless ($self->{registered}) {
1283 $motd = "${main::motd}_nor_$self->{lang}";
1284 $motd = "${main::motd}_nor" unless -e $motd;
1286 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1287 $motd = $main::motd unless $motd && -e $motd;
1288 if ($self->conn->ax25) {
1290 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1292 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1295 $self->send_file($motd) if -e $motd;
1299 # Punt off a long running command into a separate process
1301 # This is called from commands to run some potentially long running
1302 # function. The process forks and then runs the function and returns
1303 # the result back to the cmd.
1305 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1306 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1307 # THE CURRENT CONTEXT!!
1309 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1315 my $call = $self->{call};
1319 my $cb = delete $args{cb};
1320 my $prefix = delete $args{prefix};
1321 my $progress = delete $args{progress};
1322 my $args = delete $args{args} || [];
1323 my $t0 = [gettimeofday];
1327 # just behave normally if something has set the "one-shot" _nospawn in the channel
1328 if ($self->{_nospawn}) {
1329 eval { @out = $cmdref->(@$args); };
1331 DXDebug::dbgprintring(25);
1332 push @out, DXDebug::shortmess($@);
1337 my $fc = DXSubprocess->new;
1338 # $fc->serializer(\&encode_json);
1339 # $fc->deserializer(\&decode_json);
1343 if (isdbg('progress')) {
1344 my $s = qq{$call line: "$line"};
1345 $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1349 ++$self->{_in_sub_process};
1350 dbg "\$self->{_in_sub_process} = $self->{_in_sub_process}";
1351 @out = $cmdref->(@$args);
1352 --$self->{_in_sub_process} if $self->{_in_sub_process} > 0;
1355 DXDebug::dbgprintring(25);
1356 push @out, DXDebug::shortmess($@);
1362 my ($fc, $err, @res) = @_;
1363 my $dxchan = DXChannel::get($call);
1364 return unless $dxchan;
1367 my $s = "DXProt::spawn_cmd: call $call error $err";
1368 dbg($s) if isdbg('chan');
1373 # transform output if required
1374 @res = $cb->($dxchan, @res);
1377 if (defined $prefix) {
1378 $dxchan->send(map {"$prefix$_"} @res);
1380 $dxchan->send(@res);
1383 diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
1391 return ($users, $maxusers);