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->send("Capabilities: ve7cc rbn");
110 $self->state('prompt'); # a bit of room for further expansion, passwords etc
111 $self->{priv} = $user->priv || 0;
112 $self->{lang} = $user->lang || $main::lang || 'en';
113 my $pagelth = $user->pagelth;
114 $pagelth = $default_pagelth unless defined $pagelth;
115 $self->{pagelth} = $pagelth;
116 ($self->{width}) = $line =~ /\s*width=(\d+)/; $line =~ s/\s*width=\d+//;
117 $self->{enhanced} = $line =~ /\s+enhanced/; $line =~ s/\s*enhanced//;
118 if ($line =~ /host=/) {
119 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
120 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
122 ($h) = $line =~ /host=([\da..fA..F:]+)/;
123 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
125 $self->{hostname} = $h if $h;
127 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
128 $self->{consort} = $line; # save the connection type
130 LogDbg('DXCommand', "$call connected from $self->{hostname} cols $self->{width}" . ($self->{enhanced}?" enhanced":''));
132 # set some necessary flags on the user if they are connecting
133 $self->{beep} = $user->wantbeep;
134 $self->{ann} = $user->wantann;
135 $self->{wwv} = $user->wantwwv;
136 $self->{wcy} = $user->wantwcy;
137 $self->{talk} = $user->wanttalk;
138 $self->{wx} = $user->wantwx;
139 $self->{dx} = $user->wantdx;
140 $self->{logininfo} = $user->wantlogininfo;
141 $self->{ann_talk} = $user->wantann_talk;
142 $self->{wantrbn} = $user->wantrbn;
144 $self->{prompt} = $user->prompt if $user->prompt;
145 $self->{lastmsgpoll} = 0;
146 $self->{rbnseeme} = $user->rbnseeme;
147 RBN::add_seeme($call) if $self->{rbnseeme};
149 # sort out new dx spot stuff
150 $user->wantdxcq(0) unless defined $user->{wantdxcq};
151 $user->wantdxitu(0) unless defined $user->{wantdxitu};
152 $user->wantusstate(0) unless defined $user->{wantusstate};
154 # sort out registration
155 if ($main::reqreg == 2) {
156 $self->{registered} = !$user->registered;
158 $self->{registered} = $user->registered;
161 # establish slug queue, if required
162 $self->{sluggedpcs} = [];
163 $self->{isslugged} = $DXProt::pc92_slug_changes + $DXProt::last_pc92_slug + 5 if $DXProt::pc92_slug_changes;
164 $self->{isslugged} = 0 if $self->{priv} || $user->registered || ($user->homenode && $user->homenode eq $main::mycall);
166 # send the relevant MOTD
169 # sort out privilege reduction
170 $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->conn->peerhost eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
174 $nossid =~ s/-\d+$//;
176 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
177 || Filter::read_in('spots', $nossid, 0)
178 || Filter::read_in('spots', 'user_default', 0);
179 $self->{wwvfilter} = Filter::read_in('wwv', $call, 0)
180 || Filter::read_in('wwv', $nossid, 0)
181 || Filter::read_in('wwv', 'user_default', 0);
182 $self->{wcyfilter} = Filter::read_in('wcy', $call, 0)
183 || Filter::read_in('wcy', $nossid, 0)
184 || Filter::read_in('wcy', 'user_default', 0);
185 $self->{annfilter} = Filter::read_in('ann', $call, 0)
186 || Filter::read_in('ann', $nossid, 0)
187 || Filter::read_in('ann', 'user_default', 0) ;
188 $self->{rbnfilter} = Filter::read_in('rbn', $call, 0)
189 || Filter::read_in('rbn', $nossid, 0)
190 || Filter::read_in('rbn', 'user_default', 0);
192 # clean up qra locators
193 my $qra = $user->qra;
194 $qra = undef if ($qra && !DXBearing::is_qra($qra));
196 my $lat = $user->lat;
197 my $long = $user->long;
198 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
202 my $echo = $user->wantecho;
204 $self->send_now('E', "0");
205 $self->send($self->msg('echow'));
206 $self->conn->echo($echo) if $self->conn->can('echo');
209 $self->tell_login('loginu');
210 $self->tell_buddies('loginb');
212 # is this a bad ip address?
213 if (is_ipaddr($self->{hostname})) {
214 $self->{badip} = DXCIDR::find($self->{hostname});
217 # do we need to send a forward/opernam?
218 my $lastoper = $user->lastoper || 0;
219 my $homenode = $user->homenode || "";
220 if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
221 run_cmd($main::me, "forward/opernam $call");
222 $user->lastoper($main::systime + ((int rand(10)) * 86400));
225 # run a script send the output to the punter
226 my $script = new Script(lc $call) || new Script('user_default');
227 $script->run($self) if $script;
230 $self->send($self->run_cmd("show/cluster"));
232 # send prompts for qth, name and things
233 $self->send($self->msg('namee1')) if !$user->name;
234 $self->send($self->msg('qthe1')) if !$user->qth;
235 $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
236 $self->send($self->msg('hnodee1')) if !$user->qth;
237 $self->send($self->msg('m9')) if DXMsg::for_me($call);
239 # send out any buddy messages for other people that are online
240 foreach my $call (@{$user->buddies}) {
241 my $ref = Route::User::get($call);
243 foreach my $node ($ref->parents) {
244 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
249 $self->lastmsgpoll($main::systime);
254 # This is the normal command prompt driver
264 # save this for them's that need it
265 my $rawline = $cmdline;
267 # remove leading and trailing spaces
268 $cmdline =~ s/^\s*(.*)\s*$/$1/;
270 if ($self->{state} eq 'page') {
271 my $i = $self->{pagelth}-5;
272 my $ref = $self->{pagedata};
275 # abort if we get a line starting in with a
276 if ($cmdline =~ /^a/io) {
281 # send a tranche of data
282 for (; $i > 0 && @$ref; --$i) {
283 my $line = shift @$ref;
284 $line =~ s/\s+$//o; # why am having to do this?
288 # reset state if none or else chuck out an intermediate prompt
290 $tot -= $self->{pagelth};
291 $self->send($self->msg('page', $tot));
293 $self->state('prompt');
295 } elsif ($self->{state} eq 'sysop') {
296 my $passwd = $self->{user}->passwd;
298 my @pw = grep {$_ !~ /\s/} split //, $passwd;
299 my @l = @{$self->{passwd}};
300 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
301 if ($cmdline =~ /$str/) {
302 $self->{priv} = $self->{user}->priv;
304 $self->send($self->msg('sorry'));
307 $self->send($self->msg('sorry'));
309 $self->state('prompt');
310 } elsif ($self->{state} eq 'passwd') {
311 my $passwd = $self->{user}->passwd;
312 if ($passwd && $cmdline eq $passwd) {
313 $self->send($self->msg('pw1'));
314 $self->state('passwd1');
316 $self->conn->{echo} = $self->conn->{decho};
317 delete $self->conn->{decho};
318 $self->send($self->msg('sorry'));
319 $self->state('prompt');
321 } elsif ($self->{state} eq 'passwd1') {
322 $self->{passwd} = $cmdline;
323 $self->send($self->msg('pw2'));
324 $self->state('passwd2');
325 } elsif ($self->{state} eq 'passwd2') {
326 if ($cmdline eq $self->{passwd}) {
327 $self->{user}->passwd($cmdline);
328 $self->send($self->msg('pw3'));
330 $self->send($self->msg('pw4'));
332 $self->conn->{echo} = $self->conn->{decho};
333 delete $self->conn->{decho};
334 $self->state('prompt');
335 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
336 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
337 for (@{$self->{talklist}}) {
338 if ($self->{state} eq 'talk') {
339 $self->send_talks($_, $self->msg('talkend'));
341 $self->local_send('C', $self->msg('chatend', $_));
344 $self->state('prompt');
345 delete $self->{talklist};
346 } elsif ($cmdline =~ m|^/+\w+|) {
348 my $sendit = $cmdline =~ s|^/+||;
349 if (@bad = BadWords::check($cmdline)) {
350 $self->badcount(($self->badcount||0) + @bad);
351 LogDbg('DXCommand', "$self->{call} swore: '$cmdline' with badwords: '" . join(',', @bad) . "'");
353 my @in = $self->run_cmd($cmdline);
354 $self->send_ans(@in);
355 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
356 foreach my $l (@in) {
357 for (@{$self->{talklist}}) {
358 if ($self->{state} eq 'talk') {
359 $self->send_talks($_, $l);
361 send_chats($self, $_, $l)
367 $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
368 } elsif ($self->{talklist} && @{$self->{talklist}}) {
369 # send what has been said to whoever is in this person's talk list
370 if (@bad = BadWords::check($cmdline)) {
371 $self->badcount(($self->badcount||0) + @bad);
372 LogDbg('DXCommand', "$self->{call} swore: '$cmdline' with badwords: '" . join(',', @bad) . "'");
374 for (@{$self->{talklist}}) {
375 if ($self->{state} eq 'talk') {
376 $self->send_talks($_, $rawline);
378 send_chats($self, $_, $rawline);
382 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
383 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
386 $self->state('prompt');
388 } elsif (my $func = $self->{func}) {
391 if (ref $self->{edit}) {
392 eval { @ans = $self->{edit}->$func($self, $rawline)};
394 eval { @ans = &{$self->{func}}($self, $rawline) };
397 $self->send_ans("Syserr: on stored func $self->{func}", $@);
398 delete $self->{func};
399 $self->state('prompt');
402 $self->send_ans(@ans);
404 if (@bad = BadWords::check($cmdline)) {
405 $self->badcount(($self->badcount||0) + @bad);
406 LogDbg('DXCommand', "$self->{call} swore: '$cmdline' with badwords: '" . join(',', @bad) . "'");
408 $self->send_ans(run_cmd($self, $cmdline));
412 # check for excessive swearing
413 if ($maxbadcount && $self->{badcount} && $self->{badcount} >= $maxbadcount) {
414 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
419 # send a prompt only if we are in a prompt state
420 $self->prompt() if $self->{state} =~ /^prompt/o;
423 # send out the talk messages taking into account vias and connectivity
426 my ($self, $ent, $line) = @_;
428 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
429 $to = $ent unless $to;
430 my $call = $via && $via ne '*' ? $via : $to;
431 my $clref = Route::get($call);
432 my $dxchan = $clref->dxchan if $clref;
434 $dxchan->talk($self->{call}, $to, undef, $line);
436 $self->send($self->msg('disc2', $via ? $via : $to));
437 my @l = grep { $_ ne $ent } @{$self->{talklist}};
439 $self->{talklist} = \@l;
441 delete $self->{talklist};
442 $self->state('prompt');
453 my $msgid = DXProt::nextchatmsgid();
454 $text = "#$msgid $text";
455 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
463 for (@{$self->{talklist}}) {
464 my ($to, $via) = /(\S+)>(\S+)/;
468 return $self->msg($prompt, join(',', @call));
474 return $self->special_prompt('talkprompt');
480 return $self->special_prompt('chatprompt');
484 # send a load of stuff to a command user with page prompting
492 if ($self->{pagelth} && @_ > $self->{pagelth}) {
494 for ($i = $self->{pagelth}; $i-- > 0; ) {
496 $line =~ s/\s+$//o; # why am having to do this?
499 $self->{pagedata} = [ @_ ];
500 $self->state('page');
501 $self->send($self->msg('page', scalar @_));
514 # this is the thing that preps for running the command, it is done like this for the
515 # benefit of remote command execution
521 my $user = $self->{user};
522 my $call = $self->{call};
526 return () if length $cmdline == 0;
528 # split the command line up into parts, the first part is the command
529 my ($cmd, $args) = split /\s+/, $cmdline, 2;
530 $args = "" unless defined $args;
535 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
536 LogDbg('DXCommand', "cmd: $self->{call} - invalid characters in '$cmd'");
537 return $self->_error_out('e1');
540 # strip out // on command only
545 dbg("cmd: $cmd") if isdbg('command');
547 # alias it if possible
548 my $acmd = CmdAlias::get_cmd($cmd);
550 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
551 $args = "" unless defined $args;
552 dbg("cmd: aliased $cmd $args") if isdbg('command');
555 # first expand out the entry to a command
556 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
557 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
560 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
562 my $package = find_cmd_name($path, $fcmd);
565 if ($package && $self->can("${package}::handle")) {
567 dbg("cmd: package $package") if isdbg('command');
568 # Log('cmd', "$self->{call} on $self->{hostname} : '$cmd $args'");
569 my $t0 = [gettimeofday];
570 eval { @ans = &{"${package}::handle"}($self, $args) };
572 DXDebug::dbgprintring(25);
573 return (DXDebug::shortmess($@));
575 if (isdbg('progress')) {
576 my $msecs = _diffms($t0);
577 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
578 dbg($s) if $cmd !~ /^(?:echo|blank)/ || isdbg('echo'); # cut down a bit on HRD and other clients' noise
581 dbg("cmd: $package not present") if isdbg('command');
582 return $self->_error_out('e1');
585 dbg("cmd: $cmd not found") if isdbg('command');
586 return $self->_error_out('e1');
592 delete $self->{errors};
594 if (++$self->{errors} > $DXChannel::maxerrors) {
595 $self->send($self->msg('e26'));
600 return map {s/([^\s])\s+$/$1/; $_} @ans;
604 # This is called from inside the main cluster processing loop and is used
605 # for despatching commands that are doing some long processing job
610 my @dxchan = DXChannel::get_all();
614 foreach $dxchan (@dxchan) {
615 next unless $dxchan->is_user;
617 # send a outstanding message prompt if required
618 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
619 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
620 $dxchan->lastmsgpoll($t);
623 # send a prompt if no activity out on this channel
624 if ($t >= $dxchan->t + $main::user_interval) {
625 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
629 $maxusers = $users if $users > $maxusers;
631 if ($dxchan->{isslugged} && $main::systime > $dxchan->{isslugged}) {
632 foreach my $ref (@{$dxchan->{sluggedpcs}}) {
633 if ($ref->[0] == 61) {
634 Spot::add(@{$ref->[2]});
635 DXProt::send_dx_spot($dxchan, $ref->[1], @{$ref->[2]});
639 $dxchan->{isslugged} = 0;
640 $dxchan->{sluggedpcs} = [];
648 # finish up a user context
653 my $call = $self->call;
655 return if $self->{disconnecting}++;
657 delete $self->{senddbg};
658 RBN::del_seeme($call);
660 my $uref = Route::User::get($call);
663 # @rout = $main::routeroot->del_user($uref);
664 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
666 # dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
668 # issue a pc17 to everybody interested
669 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
670 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
672 confess "trying to disconnect a non existant user $call";
675 # I was the last node visited
676 $self->user->node($main::mycall);
678 # send info to all logged in thingies
679 $self->tell_login('logoutu');
680 $self->tell_buddies('logoutb');
682 LogDbg('DXCommand', "$call disconnected");
684 $self->SUPER::disconnect;
688 # short cut to output a prompt
695 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
697 my $call = $self->call;
698 my $date = cldate($main::systime);
699 my $time = ztime($main::systime);
700 my $prompt = $self->{prompt} || $self->msg('pr');
702 $call = "($call)" unless $self->here;
703 $prompt =~ s/\%C/$call/g;
704 $prompt =~ s/\%D/$date/g;
705 $prompt =~ s/\%T/$time/g;
706 $prompt =~ s/\%M/$main::mycall/g;
708 $self->send($prompt);
711 # broadcast a message to all users [except those mentioned after buffer]
714 my $pkg = shift; # ignored
715 my $s = shift; # the line to be rebroadcast
717 foreach my $dxchan (DXChannel::get_all()) {
718 next unless $dxchan->is_user; # only interested in user channels
719 next if grep $dxchan == $_, @_;
720 $dxchan->send($s); # send it
724 # gimme all the users
727 goto &DXChannel::get_all_users;
730 # run a script for this user
734 my $silent = shift || 0;
739 # search for the command in the cache of short->long form commands
744 my ($path, $short_cmd, $suffix) = @_;
747 # commands are lower case
748 $short_cmd = lc $short_cmd;
749 dbg("command: $path $short_cmd\n") if isdbg('command');
751 # do some checking for funny characters
752 return () if $short_cmd =~ /\/$/;
754 # return immediately if we have it
755 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
756 if ($apath && $acmd) {
757 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
758 return ($apath, $acmd);
762 my @parts = split '/', $short_cmd;
766 while (my $p = shift @parts) {
767 opendir(D, $curdir) or confess "can't open $curdir $!";
771 # if this isn't the last part
774 foreach my $l (sort @ls) {
776 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
777 dbg("got dir: $curdir/$l\n") if isdbg('command');
784 # only proceed if we find the directory asked for
785 return () unless $found;
787 foreach my $l (sort @ls) {
789 next unless $l =~ /\.$suffix$/;
790 if ($p eq substr($l, 0, length $p)) {
791 $l =~ s/\.$suffix$//;
792 $dirfn = "" unless $dirfn;
793 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
794 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
795 return ($path, "$dirfn$l");
804 # clear the command name cache
809 for my $k (keys %Cache) {
810 unless ($k =~ /cmd_cache/) {
811 dbg("Undefining cmd $k") if isdbg('command');
812 undef $DXCommandmode::{"${k}::"};
816 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
820 # the persistant execution of things from the command directories
823 # This allows perl programs to call functions dynamically
825 # This has been nicked directly from the perlembed pages
827 #require Devel::Symdump;
829 sub valid_package_name {
831 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
834 return "cmd_$string";
838 # this bit of magic finds a command in the offered directory
842 my $package = valid_package_name($cmdname);
843 my $filename = "$path/$cmdname.pl";
844 my $mtime = -M $filename;
846 # return if we can't find it
848 unless (defined $mtime) {
849 $errstr = DXM::msg('e1');
853 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
854 #we have compiled this subroutine already,
855 #it has not been updated on disk, nothing left to do
856 #print STDERR "already compiled $package->handler\n";
857 dbg("find_cmd_name: $package cached") if isdbg('command');
860 my $sub = readfilestr($filename);
862 $errstr = "Syserr: can't open '$filename' $!";
866 #wrap the code into a subroutine inside our unique package
867 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}; );
870 if ($sub =~ m|\s*sub\s+handle\n|) {
873 $eval .= qq(sub handle { $sub });
877 my @list = split /\n/, $eval;
880 dbg($_ . "\n") if isdbg('eval');
884 # get rid of any existing sub and try to compile the new one
887 if (exists $Cache{$package}) {
888 dbg("find_cmd_name: Redefining $package") if isdbg('command');
889 undef $DXCommandmode::{"${package}::"};
890 delete $Cache{$package};
892 dbg("find_cmd_name: Defining $package") if isdbg('command');
897 $Cache{$package} = {mtime => $mtime } unless $@;
900 return "DXCommandmode::$package";
908 $self->SUPER::send(dd(['cmd',$_]));
911 $self->SUPER::send(@_);
917 my ($self, $let, $buf) = @_;
918 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
919 if ($self->{enhanced}) {
920 $self->send_later($let, $buf);
929 # send a talk message here
932 my ($self, $from, $to, $via, $line, $onode) = @_;
933 $line =~ s/\\5E/\^/g;
936 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
938 $self->local_send('T', "$to de $from: $line");
941 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
942 # send a 'not here' message if required
943 unless ($self->{here} && $from ne $to) {
944 my $key = "$to$from";
945 unless (exists $nothereslug{$key}) {
947 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
948 my $name = $self->user->name || $to;
949 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
950 $nothereslug{$key} = $main::systime;
951 $dxchan->talk($to, $from, undef, $s);
968 if (!$self->{ann_talk} && $to ne $self->{call}) {
969 my $call = AnnTalk::is_talk_candidate($_[0], $text);
973 if ($self->{annfilter}) {
974 ($filter, $hops) = $self->{annfilter}->it(@_ );
975 return unless $filter;
978 unless ($self->{ann}) {
979 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
981 return if $target eq 'SYSOP' && $self->{priv} < 5;
984 $buf = dd(['ann', $to, $target, $text, @_])
986 $buf = "$to$target de $_[0]: $text";
988 $buf .= "\a\a" if $self->{beep};
990 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
1002 my ($filter, $hops);
1004 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
1006 $text =~ s/^\#\d+ //;
1009 $buf = dd(['chat', $to, $target, $text, @_])
1011 $buf = "$target de $_[0]: $text";
1012 #$buf =~ s/\%5E/^/g;
1013 $buf .= "\a\a" if $self->{beep};
1015 $self->local_send('C', $buf);
1022 my $t = ztime($_[2]);
1023 my ($slot1, $slot2) = ('', '');
1025 my $clth = 30 + $self->{width} - 80; # allow comment to grow according the screen width
1028 my $comment = substr (($c || ''), 0, $clth);
1029 $comment .= ' ' x ($clth - (length($comment)));
1031 if (!$slot1 && $self->{user}->wantgrid) {
1032 my $ref = DXUser::get_current($_[1]);
1033 if ($ref && $ref->qra) {
1034 $slot1 = ' ' . substr($ref->qra, 0, 4);
1037 if (!$slot1 && $self->{user}->wantusstate) {
1038 $slot1 = " $_[12]" if $_[12];
1041 if ($self->{user}->wantdxitu) {
1042 $slot1 = sprintf(" %2d", $_[8]) if defined $_[8];
1043 } elsif ($self->{user}->wantdxcq) {
1044 $slot1 = sprintf(" %2d", $_[9]) if defined $_[9];
1047 $comment = substr($comment, 0, $clth-length($slot1)) . $slot1 if $slot1;
1049 if (!$slot2 && $self->{user}->wantgrid) {
1051 $origin =~ s/-#$//; # sigh......
1052 my $ref = DXUser::get_current($origin);
1053 if ($ref && $ref->qra) {
1054 $slot2 = ' ' . substr($ref->qra, 0, 4);
1057 if (!$slot2 && $self->{user}->wantusstate) {
1058 $slot2 = " $_[13]" if $_[13];
1061 if ($self->{user}->wantdxitu) {
1062 $slot2 = sprintf(" %2d", $_[10]) if defined $_[10];
1063 } elsif ($self->{user}->wantdxcq) {
1064 $slot2 = sprintf(" %2d", $_[11]) if defined $_[11];
1068 my $o = sprintf("%-9s", $_[4] . ':');
1069 my $qrg = sprintf "%8.1f", $_[0];
1070 if (length $qrg >= 9) {
1071 while (length($o)+length($qrg) > 17 && $o =~ / $/) {
1075 my $spot = sprintf "%-12s", $_[1];
1076 my $front = "DX de $o $qrg $spot";
1077 while (length($front) > 38 && $front =~ / $/) {
1082 return sprintf "$front %-s $t$slot2", $comment;
1091 my $isolate = shift;
1092 return unless $self->{dx};
1094 my ($filter, $hops);
1096 if ($self->{spotsfilter}) {
1097 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1098 return unless $filter;
1101 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1104 if ($self->{ve7cc}) {
1105 $buf = VE7CC::dx_spot($self, @_);
1106 } elsif ($self->{gtk}) {
1107 my ($dxloc, $byloc);
1109 my $ref = DXUser::get_current($_[4]);
1112 $byloc = substr($byloc, 0, 4) if $byloc;
1116 $spot =~ s|/\w{1,4}$||;
1117 $ref = DXUser::get_current($spot);
1120 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1122 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1125 $buf = $self->format_dx_spot(@_);
1126 $buf .= "\a\a" if $self->{beep};
1127 #$buf =~ s/\%5E/^/g;
1130 $self->local_send('X', $buf);
1137 my $isolate = shift;
1138 my ($filter, $hops);
1140 return unless $self->{wwv};
1142 if ($self->{wwvfilter}) {
1143 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1144 return unless $filter;
1149 $buf = dd(['wwv', @_])
1151 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1152 $buf .= "\a\a" if $self->{beep};
1155 $self->local_send('V', $buf);
1162 my $isolate = shift;
1163 my ($filter, $hops);
1165 return unless $self->{wcy};
1167 if ($self->{wcyfilter}) {
1168 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1169 return unless $filter;
1174 $buf = dd(['wcy', @_])
1176 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1177 $buf .= "\a\a" if $self->{beep};
1179 $self->local_send('Y', $buf);
1182 # broadcast debug stuff to all interested parties
1185 my $s = shift; # the line to be rebroadcast
1187 foreach my $dxchan (DXChannel::get_all_users) {
1188 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1189 if ($dxchan->{gtk}) {
1190 $dxchan->send_later('L', dd(['db', $s]));
1192 $dxchan->send_later('L', $s);
1203 if ($self->state eq 'enterbody') {
1204 my $loc = $self->{loc} || confess "local var gone missing" ;
1205 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1207 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1209 $self->state('prompt');
1210 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1211 push @out, $self->msg('m10');
1212 delete $loc->{lines};
1213 delete $self->{loc};
1215 $self->state('prompt');
1217 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1218 # i.e. it ain't and end or abort, therefore store the line
1221 confess "Invalid state $self->{state}";
1226 sub store_startup_script
1229 my $loc = $self->{loc} || confess "local var gone missing" ;
1231 my $call = $loc->{call} || confess "callsign gone missing";
1232 confess "lines array gone missing" unless ref $loc->{lines};
1233 my $r = Script::store($call, $loc->{lines});
1236 push @out, $self->msg('m19', $call, $r);
1238 push @out, $self->msg('m20', $call);
1241 push @out, "error opening startup script $call $!";
1246 # Import any commands contained in any files in import_cmd directory
1248 # If the filename has a recogisable callsign as some delimited part
1249 # of it, then this is the user the command will be run as.
1253 # are there any to do in this directory?
1254 return unless -d $cmdimportdir;
1255 unless (opendir(DIR, $cmdimportdir)) {
1256 LogDbg('err', "can\'t open $cmdimportdir $!");
1260 my @names = readdir(DIR);
1264 return unless @names;
1266 foreach $name (@names) {
1267 next if $name =~ /^\./;
1269 my $s = Script->new($name, $cmdimportdir);
1271 LogDbg('DXCommand', "Run import cmd file $name");
1272 my @cat = split /[^A-Za-z0-9]+/, $name;
1273 my ($call) = grep {is_callsign(uc $_)} @cat;
1274 $call ||= $main::mycall;
1279 $s->inscript(0); # switch off script checks
1281 if ($call eq $main::mycall) {
1282 @out = $s->run($main::me, 1);
1284 my $dxchan = DXChannel::get($call);
1286 @out = $s->run($dxchan, 1);
1288 my $u = DXUser::get($call);
1290 $dxchan = $main::me;
1291 my $old = $dxchan->{call};
1292 my $priv = $dxchan->{priv};
1293 my $user = $dxchan->{user};
1294 $dxchan->{call} = $call;
1295 $dxchan->{priv} = $u->priv;
1296 $dxchan->{user} = $u;
1297 @out = $s->run($dxchan, 1);
1298 $dxchan->{call} = $old;
1299 $dxchan->{priv} = $priv;
1300 $dxchan->{user} = $user;
1302 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1308 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1311 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1312 unlink "$cmdimportdir/$name";
1317 sub print_find_reply
1319 my ($self, $node, $target, $flag, $ms) = @_;
1320 my $sort = $flag == 2 ? "External" : "Local";
1321 $self->send("$sort $target found at $node in $ms ms" );
1324 # send the most relevant motd
1330 unless ($self->isregistered) {
1331 $motd = "${main::motd}_nor_$self->{lang}";
1332 $motd = "${main::motd}_nor" unless -e $motd;
1334 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1335 $motd = $main::motd unless $motd && -e $motd;
1336 if ($self->conn->ax25) {
1338 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1340 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1343 $self->send_file($motd) if -e $motd;
1346 # Punt off a long running command into a separate process
1348 # This is called from commands to run some potentially long running
1349 # function. The process forks and then runs the function and returns
1350 # the result back to the cmd.
1352 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1353 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1354 # THE CURRENT CONTEXT!!
1356 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1362 my $call = $self->{call};
1366 my $cb = delete $args{cb};
1367 my $prefix = delete $args{prefix};
1368 my $progress = delete $args{progress};
1369 my $args = delete $args{args} || [];
1370 my $t0 = [gettimeofday];
1374 # just behave normally if something has set the "one-shot" _nospawn in the channel
1375 if ($self->{_nospawn}) {
1376 eval { @out = $cmdref->(@$args); };
1378 DXDebug::dbgprintring(25);
1379 push @out, DXDebug::shortmess($@);
1384 my $fc = DXSubprocess->new;
1385 # $fc->serializer(\&encode_json);
1386 # $fc->deserializer(\&decode_json);
1390 if (isdbg('progress')) {
1391 my $s = qq{$call line: "$line"};
1392 $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1396 ++$self->{_in_sub_process};
1397 dbg "\$self->{_in_sub_process} = $self->{_in_sub_process}";
1398 @out = $cmdref->(@$args);
1399 --$self->{_in_sub_process} if $self->{_in_sub_process} > 0;
1402 DXDebug::dbgprintring(25);
1403 push @out, DXDebug::shortmess($@);
1409 my ($fc, $err, @res) = @_;
1410 my $dxchan = DXChannel::get($call);
1411 return unless $dxchan;
1414 my $s = "DXProt::spawn_cmd: call $call error $err";
1415 dbg($s) if isdbg('chan');
1420 # transform output if required
1421 @res = $cb->($dxchan, @res);
1424 if (defined $prefix) {
1425 $dxchan->send(map {"$prefix$_"} @res);
1427 $dxchan->send(@res);
1430 diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
1438 return ($users, $maxusers);