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);
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 =~ /\s*width=(\d+)/; $line =~ s/\s*width=\d+//;
116 $self->{enhanced} = $line =~ /\s+enhanced/; $line =~ s/\s*enhanced//;
117 if ($line =~ /host=/) {
118 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
119 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
121 ($h) = $line =~ /host=([\da..fA..F:]+)/;
122 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
124 $self->{hostname} = $h if $h;
126 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
127 $self->{consort} = $line; # save the connection type
129 LogDbg('DXCommand', "$call connected from $self->{hostname} cols $self->{width}" . ($self->{enhanced}?" enhanced":''));
131 # set some necessary flags on the user if they are connecting
132 $self->{beep} = $user->wantbeep;
133 $self->{ann} = $user->wantann;
134 $self->{wwv} = $user->wantwwv;
135 $self->{wcy} = $user->wantwcy;
136 $self->{talk} = $user->wanttalk;
137 $self->{wx} = $user->wantwx;
138 $self->{dx} = $user->wantdx;
139 $self->{logininfo} = $user->wantlogininfo;
140 $self->{ann_talk} = $user->wantann_talk;
141 $self->{wantrbn} = $user->wantrbn;
143 $self->{prompt} = $user->prompt if $user->prompt;
144 $self->{lastmsgpoll} = 0;
146 # sort out new dx spot stuff
147 $user->wantdxcq(0) unless defined $user->{wantdxcq};
148 $user->wantdxitu(0) unless defined $user->{wantdxitu};
149 $user->wantusstate(0) unless defined $user->{wantusstate};
151 # sort out registration
152 if ($main::reqreg == 2) {
153 $self->{registered} = !$user->registered;
155 $self->{registered} = $user->registered;
158 # establish slug queue, if required
159 $self->{sluggedpcs} = [];
160 $self->{isslugged} = $DXProt::pc92_slug_changes + $DXProt::last_pc92_slug + 5 if $DXProt::pc92_slug_changes;
161 $self->{isslugged} = 0 if $self->{priv} || $user->registered || ($user->homenode && $user->homenode eq $main::mycall);
163 # send the relevant MOTD
166 # sort out privilege reduction
167 $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
171 $nossid =~ s/-\d+$//;
173 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
174 || Filter::read_in('spots', $nossid, 0)
175 || Filter::read_in('spots', 'user_default', 0);
176 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
177 || Filter::read_in('wwv', $nossid, 0)
178 || Filter::read_in('wwv', 'user_default', 0);
179 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
180 || Filter::read_in('wcy', $nossid, 0)
181 || Filter::read_in('wcy', 'user_default', 0);
182 $self->{annfilter} = Filter::read_in('ann', $call, 0)
183 || Filter::read_in('ann', $nossid, 0)
184 || Filter::read_in('ann', 'user_default', 0) ;
185 $self->{rbnfilter} = Filter::read_in('rbn', $call, 0)
186 || Filter::read_in('rbn', $nossid, 0)
187 || Filter::read_in('rbn', 'user_default', 0);
189 # clean up qra locators
190 my $qra = $user->qra;
191 $qra = undef if ($qra && !DXBearing::is_qra($qra));
193 my $lat = $user->lat;
194 my $long = $user->long;
195 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
199 my $echo = $user->wantecho;
201 $self->send_now('E', "0");
202 $self->send($self->msg('echow'));
203 $self->conn->echo($echo) if $self->conn->can('echo');
206 $self->tell_login('loginu');
207 $self->tell_buddies('loginb');
209 # is this a bad ip address?
210 if (is_ipaddr($self->{hostname})) {
211 $self->{badip} = DXCIDR->find($self->{hostname});
214 # do we need to send a forward/opernam?
215 my $lastoper = $user->lastoper || 0;
216 my $homenode = $user->homenode || "";
217 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
218 run_cmd($main::me, "forward/opernam $call");
219 $user->lastoper($main::systime + ((int rand(10)) * 86400));
222 # run a script send the output to the punter
223 my $script = new Script(lc $call) || new Script('user_default');
224 $script->run($self) if $script;
227 $self->send($self->run_cmd("show/cluster"));
229 # send prompts for qth, name and things
230 $self->send($self->msg('namee1')) if !$user->name;
231 $self->send($self->msg('qthe1')) if !$user->qth;
232 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
233 $self->send($self->msg('hnodee1')) if !$user->qth;
234 $self->send($self->msg('m9')) if DXMsg::for_me($call);
236 # send out any buddy messages for other people that are online
237 foreach my $call (@{$user->buddies}) {
238 my $ref = Route::User::get($call);
240 foreach my $node ($ref->parents) {
241 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
246 $self->lastmsgpoll($main::systime);
251 # This is the normal command prompt driver
260 # save this for them's that need it
261 my $rawline = $cmdline;
263 # remove leading and trailing spaces
264 $cmdline =~ s/^\s*(.*)\s*$/$1/;
266 if ($self->{state} eq 'page') {
267 my $i = $self->{pagelth}-5;
268 my $ref = $self->{pagedata};
271 # abort if we get a line starting in with a
272 if ($cmdline =~ /^a/io) {
277 # send a tranche of data
278 for (; $i > 0 && @$ref; --$i) {
279 my $line = shift @$ref;
280 $line =~ s/\s+$//o; # why am having to do this?
284 # reset state if none or else chuck out an intermediate prompt
286 $tot -= $self->{pagelth};
287 $self->send($self->msg('page', $tot));
289 $self->state('prompt');
291 } elsif ($self->{state} eq 'sysop') {
292 my $passwd = $self->{user}->passwd;
294 my @pw = grep {$_ !~ /\s/} split //, $passwd;
295 my @l = @{$self->{passwd}};
296 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
297 if ($cmdline =~ /$str/) {
298 $self->{priv} = $self->{user}->priv;
300 $self->send($self->msg('sorry'));
303 $self->send($self->msg('sorry'));
305 $self->state('prompt');
306 } elsif ($self->{state} eq 'passwd') {
307 my $passwd = $self->{user}->passwd;
308 if ($passwd && $cmdline eq $passwd) {
309 $self->send($self->msg('pw1'));
310 $self->state('passwd1');
312 $self->conn->{echo} = $self->conn->{decho};
313 delete $self->conn->{decho};
314 $self->send($self->msg('sorry'));
315 $self->state('prompt');
317 } elsif ($self->{state} eq 'passwd1') {
318 $self->{passwd} = $cmdline;
319 $self->send($self->msg('pw2'));
320 $self->state('passwd2');
321 } elsif ($self->{state} eq 'passwd2') {
322 if ($cmdline eq $self->{passwd}) {
323 $self->{user}->passwd($cmdline);
324 $self->send($self->msg('pw3'));
326 $self->send($self->msg('pw4'));
328 $self->conn->{echo} = $self->conn->{decho};
329 delete $self->conn->{decho};
330 $self->state('prompt');
331 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
332 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
333 for (@{$self->{talklist}}) {
334 if ($self->{state} eq 'talk') {
335 $self->send_talks($_, $self->msg('talkend'));
337 $self->local_send('C', $self->msg('chatend', $_));
340 $self->state('prompt');
341 delete $self->{talklist};
342 } elsif ($cmdline =~ m|^/+\w+|) {
344 my $sendit = $cmdline =~ s|^/+||;
345 my @in = $self->run_cmd($cmdline);
346 $self->send_ans(@in);
347 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
348 foreach my $l (@in) {
350 if (@bad = BadWords::check($l)) {
351 $self->badcount(($self->badcount||0) + @bad);
352 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
354 for (@{$self->{talklist}}) {
355 if ($self->{state} eq 'talk') {
356 $self->send_talks($_, $l);
358 send_chats($self, $_, $l)
364 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
365 } elsif ($self->{talklist} && @{$self->{talklist}}) {
366 # send what has been said to whoever is in this person's talk list
368 if (@bad = BadWords::check($cmdline)) {
369 $self->badcount(($self->badcount||0) + @bad);
370 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
372 for (@{$self->{talklist}}) {
373 if ($self->{state} eq 'talk') {
374 $self->send_talks($_, $rawline);
376 send_chats($self, $_, $rawline);
380 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
381 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
384 $self->state('prompt');
386 } elsif (my $func = $self->{func}) {
389 if (ref $self->{edit}) {
390 eval { @ans = $self->{edit}->$func($self, $rawline)};
392 eval { @ans = &{$self->{func}}($self, $rawline) };
395 $self->send_ans("Syserr: on stored func $self->{func}", $@);
396 delete $self->{func};
397 $self->state('prompt');
400 $self->send_ans(@ans);
402 $self->send_ans(run_cmd($self, $cmdline));
405 # check for excessive swearing
406 if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
407 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
412 # send a prompt only if we are in a prompt state
413 $self->prompt() if $self->{state} =~ /^prompt/o;
416 # send out the talk messages taking into account vias and connectivity
419 my ($self, $ent, $line) = @_;
421 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
422 $to = $ent unless $to;
423 my $call = $via && $via ne '*' ? $via : $to;
424 my $clref = Route::get($call);
425 my $dxchan = $clref->dxchan if $clref;
427 $dxchan->talk($self->{call}, $to, undef, $line);
429 $self->send($self->msg('disc2', $via ? $via : $to));
430 my @l = grep { $_ ne $ent } @{$self->{talklist}};
432 $self->{talklist} = \@l;
434 delete $self->{talklist};
435 $self->state('prompt');
446 my $msgid = DXProt::nextchatmsgid();
447 $text = "#$msgid $text";
448 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
456 for (@{$self->{talklist}}) {
457 my ($to, $via) = /(\S+)>(\S+)/;
461 return $self->msg($prompt, join(',', @call));
467 return $self->special_prompt('talkprompt');
473 return $self->special_prompt('chatprompt');
477 # send a load of stuff to a command user with page prompting
485 if ($self->{pagelth} && @_ > $self->{pagelth}) {
487 for ($i = $self->{pagelth}; $i-- > 0; ) {
489 $line =~ s/\s+$//o; # why am having to do this?
492 $self->{pagedata} = [ @_ ];
493 $self->state('page');
494 $self->send($self->msg('page', scalar @_));
507 # this is the thing that preps for running the command, it is done like this for the
508 # benefit of remote command execution
514 my $user = $self->{user};
515 my $call = $self->{call};
519 return () if length $cmdline == 0;
521 # split the command line up into parts, the first part is the command
522 my ($cmd, $args) = split /\s+/, $cmdline, 2;
523 $args = "" unless defined $args;
528 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
529 LogDbg('DXCommand', "cmd: $self->{call} - invalid characters in '$cmd'");
530 return $self->_error_out('e1');
533 # strip out // on command only
538 dbg("cmd: $cmd") if isdbg('command');
540 # alias it if possible
541 my $acmd = CmdAlias::get_cmd($cmd);
543 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
544 $args = "" unless defined $args;
545 dbg("cmd: aliased $cmd $args") if isdbg('command');
548 # first expand out the entry to a command
549 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
550 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
553 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
555 my $package = find_cmd_name($path, $fcmd);
558 if ($package && $self->can("${package}::handle")) {
560 dbg("cmd: package $package") if isdbg('command');
561 # Log('cmd', "$self->{call} on $self->{hostname} : '$cmd $args'");
562 my $t0 = [gettimeofday];
563 eval { @ans = &{"${package}::handle"}($self, $args) };
565 DXDebug::dbgprintring(25);
566 return (DXDebug::shortmess($@));
568 if (isdbg('progress')) {
569 my $msecs = _diffms($t0);
570 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
571 dbg($s) if $cmd !~ /^(?:echo|blank)/ || isdbg('echo'); # cut down a bit on HRD and other clients' noise
574 dbg("cmd: $package not present") if isdbg('command');
575 return $self->_error_out('e1');
578 dbg("cmd: $cmd not found") if isdbg('command');
579 return $self->_error_out('e1');
585 delete $self->{errors};
587 if (++$self->{errors} > $DXChannel::maxerrors) {
588 $self->send($self->msg('e26'));
593 return map {s/([^\s])\s+$/$1/; $_} @ans;
597 # This is called from inside the main cluster processing loop and is used
598 # for despatching commands that are doing some long processing job
603 my @dxchan = DXChannel::get_all();
607 foreach $dxchan (@dxchan) {
608 next unless $dxchan->is_user;
610 # send a outstanding message prompt if required
611 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
612 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
613 $dxchan->lastmsgpoll($t);
616 # send a prompt if no activity out on this channel
617 if ($t >= $dxchan->t + $main::user_interval) {
618 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
622 $maxusers = $users if $users > $maxusers;
624 if ($dxchan->{isslugged} && $main::systime > $dxchan->{isslugged}) {
625 foreach my $ref (@{$dxchan->{sluggedpcs}}) {
626 if ($ref->[0] == 61) {
627 Spot::add(@{$ref->[2]});
628 DXProt::send_dx_spot($dxchan, $ref->[1], @{$ref->[2]});
632 $dxchan->{isslugged} = 0;
633 $dxchan->{sluggedpcs} = [];
641 # finish up a user context
646 my $call = $self->call;
648 return if $self->{disconnecting}++;
650 delete $self->{senddbg};
652 my $uref = Route::User::get($call);
655 # @rout = $main::routeroot->del_user($uref);
656 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
658 # dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
660 # issue a pc17 to everybody interested
661 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
662 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
664 confess "trying to disconnect a non existant user $call";
667 # I was the last node visited
668 $self->user->node($main::mycall);
670 # send info to all logged in thingies
671 $self->tell_login('logoutu');
672 $self->tell_buddies('logoutb');
674 LogDbg('DXCommand', "$call disconnected");
676 $self->SUPER::disconnect;
680 # short cut to output a prompt
687 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
689 my $call = $self->call;
690 my $date = cldate($main::systime);
691 my $time = ztime($main::systime);
692 my $prompt = $self->{prompt} || $self->msg('pr');
694 $call = "($call)" unless $self->here;
695 $prompt =~ s/\%C/$call/g;
696 $prompt =~ s/\%D/$date/g;
697 $prompt =~ s/\%T/$time/g;
698 $prompt =~ s/\%M/$main::mycall/g;
700 $self->send($prompt);
703 # broadcast a message to all users [except those mentioned after buffer]
706 my $pkg = shift; # ignored
707 my $s = shift; # the line to be rebroadcast
709 foreach my $dxchan (DXChannel::get_all()) {
710 next unless $dxchan->is_user; # only interested in user channels
711 next if grep $dxchan == $_, @_;
712 $dxchan->send($s); # send it
716 # gimme all the users
719 goto &DXChannel::get_all_users;
722 # run a script for this user
726 my $silent = shift || 0;
731 # search for the command in the cache of short->long form commands
736 my ($path, $short_cmd, $suffix) = @_;
739 # commands are lower case
740 $short_cmd = lc $short_cmd;
741 dbg("command: $path $short_cmd\n") if isdbg('command');
743 # do some checking for funny characters
744 return () if $short_cmd =~ /\/$/;
746 # return immediately if we have it
747 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
748 if ($apath && $acmd) {
749 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
750 return ($apath, $acmd);
754 my @parts = split '/', $short_cmd;
758 while (my $p = shift @parts) {
759 opendir(D, $curdir) or confess "can't open $curdir $!";
763 # if this isn't the last part
766 foreach my $l (sort @ls) {
768 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
769 dbg("got dir: $curdir/$l\n") if isdbg('command');
776 # only proceed if we find the directory asked for
777 return () unless $found;
779 foreach my $l (sort @ls) {
781 next unless $l =~ /\.$suffix$/;
782 if ($p eq substr($l, 0, length $p)) {
783 $l =~ s/\.$suffix$//;
784 $dirfn = "" unless $dirfn;
785 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
786 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
787 return ($path, "$dirfn$l");
796 # clear the command name cache
801 for my $k (keys %Cache) {
802 unless ($k =~ /cmd_cache/) {
803 dbg("Undefining cmd $k") if isdbg('command');
804 undef $DXCommandmode::{"${k}::"};
808 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
812 # the persistant execution of things from the command directories
815 # This allows perl programs to call functions dynamically
817 # This has been nicked directly from the perlembed pages
819 #require Devel::Symdump;
821 sub valid_package_name {
823 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
826 return "cmd_$string";
830 # this bit of magic finds a command in the offered directory
834 my $package = valid_package_name($cmdname);
835 my $filename = "$path/$cmdname.pl";
836 my $mtime = -M $filename;
838 # return if we can't find it
840 unless (defined $mtime) {
841 $errstr = DXM::msg('e1');
845 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
846 #we have compiled this subroutine already,
847 #it has not been updated on disk, nothing left to do
848 #print STDERR "already compiled $package->handler\n";
849 dbg("find_cmd_name: $package cached") if isdbg('command');
852 my $sub = readfilestr($filename);
854 $errstr = "Syserr: can't open '$filename' $!";
858 #wrap the code into a subroutine inside our unique package
859 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}; );
862 if ($sub =~ m|\s*sub\s+handle\n|) {
865 $eval .= qq(sub handle { $sub });
869 my @list = split /\n/, $eval;
872 dbg($_ . "\n") if isdbg('eval');
876 # get rid of any existing sub and try to compile the new one
879 if (exists $Cache{$package}) {
880 dbg("find_cmd_name: Redefining $package") if isdbg('command');
881 undef $DXCommandmode::{"${package}::"};
882 delete $Cache{$package};
884 dbg("find_cmd_name: Defining $package") if isdbg('command');
889 $Cache{$package} = {mtime => $mtime } unless $@;
892 return "DXCommandmode::$package";
900 $self->SUPER::send(dd(['cmd',$_]));
903 $self->SUPER::send(@_);
909 my ($self, $let, $buf) = @_;
910 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
911 if ($self->{enhanced}) {
912 $self->send_later($let, $buf);
921 # send a talk message here
924 my ($self, $from, $to, $via, $line, $onode) = @_;
925 $line =~ s/\\5E/\^/g;
928 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
930 $self->local_send('T', "$to de $from: $line");
933 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
934 # send a 'not here' message if required
935 unless ($self->{here} && $from ne $to) {
936 my $key = "$to$from";
937 unless (exists $nothereslug{$key}) {
939 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
940 my $name = $self->user->name || $to;
941 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
942 $nothereslug{$key} = $main::systime;
943 $dxchan->talk($to, $from, undef, $s);
960 if (!$self->{ann_talk} && $to ne $self->{call}) {
961 my $call = AnnTalk::is_talk_candidate($_[0], $text);
965 if ($self->{annfilter}) {
966 ($filter, $hops) = $self->{annfilter}->it(@_ );
967 return unless $filter;
970 unless ($self->{ann}) {
971 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
973 return if $target eq 'SYSOP' && $self->{priv} < 5;
976 $buf = dd(['ann', $to, $target, $text, @_])
978 $buf = "$to$target de $_[0]: $text";
980 $buf .= "\a\a" if $self->{beep};
982 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
996 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
998 $text =~ s/^\#\d+ //;
1001 $buf = dd(['chat', $to, $target, $text, @_])
1003 $buf = "$target de $_[0]: $text";
1004 #$buf =~ s/\%5E/^/g;
1005 $buf .= "\a\a" if $self->{beep};
1007 $self->local_send('C', $buf);
1014 my $t = ztime($_[2]);
1015 my ($slot1, $slot2) = ('', '');
1017 my $clth = 30 + $self->{width} - 80; # allow comment to grow according the screen width
1020 my $comment = substr (($c || ''), 0, $clth);
1021 $comment .= ' ' x ($clth - (length($comment)));
1023 if (!$slot1 && $self->{user}->wantgrid) {
1024 my $ref = DXUser::get_current($_[1]);
1025 if ($ref && $ref->qra) {
1026 $slot1 = ' ' . substr($ref->qra, 0, 4);
1029 if (!$slot1 && $self->{user}->wantusstate) {
1030 $slot1 = " $_[12]" if $_[12];
1033 if ($self->{user}->wantdxitu) {
1034 $slot1 = sprintf(" %2d", $_[8]) if defined $_[8];
1035 } elsif ($self->{user}->wantdxcq) {
1036 $slot1 = sprintf(" %2d", $_[9]) if defined $_[9];
1039 $comment = substr($comment, 0, $clth-length($slot1)) . $slot1 if $slot1;
1041 if (!$slot2 && $self->{user}->wantgrid) {
1043 $origin =~ s/-#$//; # sigh......
1044 my $ref = DXUser::get_current($origin);
1045 if ($ref && $ref->qra) {
1046 $slot2 = ' ' . substr($ref->qra, 0, 4);
1049 if (!$slot2 && $self->{user}->wantusstate) {
1050 $slot2 = " $_[13]" if $_[13];
1053 if ($self->{user}->wantdxitu) {
1054 $slot2 = sprintf(" %2d", $_[10]) if defined $_[10];
1055 } elsif ($self->{user}->wantdxcq) {
1056 $slot2 = sprintf(" %2d", $_[11]) if defined $_[11];
1060 return sprintf "DX de %-8.8s%10.1f %-12.12s %-s $t$slot2", "$_[4]:", $_[0], $_[1], $comment;
1069 my $isolate = shift;
1070 return unless $self->{dx};
1072 my ($filter, $hops);
1074 if ($self->{spotsfilter}) {
1075 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1076 return unless $filter;
1079 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1082 if ($self->{ve7cc}) {
1083 $buf = VE7CC::dx_spot($self, @_);
1084 } elsif ($self->{gtk}) {
1085 my ($dxloc, $byloc);
1087 my $ref = DXUser::get_current($_[4]);
1090 $byloc = substr($byloc, 0, 4) if $byloc;
1094 $spot =~ s|/\w{1,4}$||;
1095 $ref = DXUser::get_current($spot);
1098 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1100 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1103 $buf = $self->format_dx_spot(@_);
1104 $buf .= "\a\a" if $self->{beep};
1105 #$buf =~ s/\%5E/^/g;
1108 $self->local_send('X', $buf);
1115 my $isolate = shift;
1116 my ($filter, $hops);
1118 return unless $self->{wwv};
1120 if ($self->{wwvfilter}) {
1121 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1122 return unless $filter;
1127 $buf = dd(['wwv', @_])
1129 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1130 $buf .= "\a\a" if $self->{beep};
1133 $self->local_send('V', $buf);
1140 my $isolate = shift;
1141 my ($filter, $hops);
1143 return unless $self->{wcy};
1145 if ($self->{wcyfilter}) {
1146 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1147 return unless $filter;
1152 $buf = dd(['wcy', @_])
1154 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1155 $buf .= "\a\a" if $self->{beep};
1157 $self->local_send('Y', $buf);
1160 # broadcast debug stuff to all interested parties
1163 my $s = shift; # the line to be rebroadcast
1165 foreach my $dxchan (DXChannel::get_all_users) {
1166 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1167 if ($dxchan->{gtk}) {
1168 $dxchan->send_later('L', dd(['db', $s]));
1170 $dxchan->send_later('L', $s);
1181 if ($self->state eq 'enterbody') {
1182 my $loc = $self->{loc} || confess "local var gone missing" ;
1183 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1185 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1187 $self->state('prompt');
1188 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1189 push @out, $self->msg('m10');
1190 delete $loc->{lines};
1191 delete $self->{loc};
1193 $self->state('prompt');
1195 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1196 # i.e. it ain't and end or abort, therefore store the line
1199 confess "Invalid state $self->{state}";
1204 sub store_startup_script
1207 my $loc = $self->{loc} || confess "local var gone missing" ;
1209 my $call = $loc->{call} || confess "callsign gone missing";
1210 confess "lines array gone missing" unless ref $loc->{lines};
1211 my $r = Script::store($call, $loc->{lines});
1214 push @out, $self->msg('m19', $call, $r);
1216 push @out, $self->msg('m20', $call);
1219 push @out, "error opening startup script $call $!";
1224 # Import any commands contained in any files in import_cmd directory
1226 # If the filename has a recogisable callsign as some delimited part
1227 # of it, then this is the user the command will be run as.
1231 # are there any to do in this directory?
1232 return unless -d $cmdimportdir;
1233 unless (opendir(DIR, $cmdimportdir)) {
1234 LogDbg('err', "can\'t open $cmdimportdir $!");
1238 my @names = readdir(DIR);
1242 return unless @names;
1244 foreach $name (@names) {
1245 next if $name =~ /^\./;
1247 my $s = Script->new($name, $cmdimportdir);
1249 LogDbg('DXCommand', "Run import cmd file $name");
1250 my @cat = split /[^A-Za-z0-9]+/, $name;
1251 my ($call) = grep {is_callsign(uc $_)} @cat;
1252 $call ||= $main::mycall;
1257 $s->inscript(0); # switch off script checks
1259 if ($call eq $main::mycall) {
1260 @out = $s->run($main::me, 1);
1262 my $dxchan = DXChannel::get($call);
1264 @out = $s->run($dxchan, 1);
1266 my $u = DXUser::get($call);
1268 $dxchan = $main::me;
1269 my $old = $dxchan->{call};
1270 my $priv = $dxchan->{priv};
1271 my $user = $dxchan->{user};
1272 $dxchan->{call} = $call;
1273 $dxchan->{priv} = $u->priv;
1274 $dxchan->{user} = $u;
1275 @out = $s->run($dxchan, 1);
1276 $dxchan->{call} = $old;
1277 $dxchan->{priv} = $priv;
1278 $dxchan->{user} = $user;
1280 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1286 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1289 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1290 unlink "$cmdimportdir/$name";
1295 sub print_find_reply
1297 my ($self, $node, $target, $flag, $ms) = @_;
1298 my $sort = $flag == 2 ? "External" : "Local";
1299 $self->send("$sort $target found at $node in $ms ms" );
1302 # send the most relevant motd
1308 unless ($self->isregistered) {
1309 $motd = "${main::motd}_nor_$self->{lang}";
1310 $motd = "${main::motd}_nor" unless -e $motd;
1312 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1313 $motd = $main::motd unless $motd && -e $motd;
1314 if ($self->conn->ax25) {
1316 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1318 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1321 $self->send_file($motd) if -e $motd;
1324 # Punt off a long running command into a separate process
1326 # This is called from commands to run some potentially long running
1327 # function. The process forks and then runs the function and returns
1328 # the result back to the cmd.
1330 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1331 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1332 # THE CURRENT CONTEXT!!
1334 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1340 my $call = $self->{call};
1344 my $cb = delete $args{cb};
1345 my $prefix = delete $args{prefix};
1346 my $progress = delete $args{progress};
1347 my $args = delete $args{args} || [];
1348 my $t0 = [gettimeofday];
1352 # just behave normally if something has set the "one-shot" _nospawn in the channel
1353 if ($self->{_nospawn}) {
1354 eval { @out = $cmdref->(@$args); };
1356 DXDebug::dbgprintring(25);
1357 push @out, DXDebug::shortmess($@);
1362 my $fc = DXSubprocess->new;
1363 # $fc->serializer(\&encode_json);
1364 # $fc->deserializer(\&decode_json);
1368 if (isdbg('progress')) {
1369 my $s = qq{$call line: "$line"};
1370 $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1374 ++$self->{_in_sub_process};
1375 dbg "\$self->{_in_sub_process} = $self->{_in_sub_process}";
1376 @out = $cmdref->(@$args);
1377 --$self->{_in_sub_process} if $self->{_in_sub_process} > 0;
1380 DXDebug::dbgprintring(25);
1381 push @out, DXDebug::shortmess($@);
1387 my ($fc, $err, @res) = @_;
1388 my $dxchan = DXChannel::get($call);
1389 return unless $dxchan;
1392 my $s = "DXProt::spawn_cmd: call $call error $err";
1393 dbg($s) if isdbg('chan');
1398 # transform output if required
1399 @res = $cb->($dxchan, @res);
1402 if (defined $prefix) {
1403 $dxchan->send(map {"$prefix$_"} @res);
1405 $dxchan->send(@res);
1408 diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
1416 return ($users, $maxusers);