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
263 # save this for them's that need it
264 my $rawline = $cmdline;
266 # remove leading and trailing spaces
267 $cmdline =~ s/^\s*(.*)\s*$/$1/;
269 if ($self->{state} eq 'page') {
270 my $i = $self->{pagelth}-5;
271 my $ref = $self->{pagedata};
274 # abort if we get a line starting in with a
275 if ($cmdline =~ /^a/io) {
280 # send a tranche of data
281 for (; $i > 0 && @$ref; --$i) {
282 my $line = shift @$ref;
283 $line =~ s/\s+$//o; # why am having to do this?
287 # reset state if none or else chuck out an intermediate prompt
289 $tot -= $self->{pagelth};
290 $self->send($self->msg('page', $tot));
292 $self->state('prompt');
294 } elsif ($self->{state} eq 'sysop') {
295 my $passwd = $self->{user}->passwd;
297 my @pw = grep {$_ !~ /\s/} split //, $passwd;
298 my @l = @{$self->{passwd}};
299 my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
300 if ($cmdline =~ /$str/) {
301 $self->{priv} = $self->{user}->priv;
303 $self->send($self->msg('sorry'));
306 $self->send($self->msg('sorry'));
308 $self->state('prompt');
309 } elsif ($self->{state} eq 'passwd') {
310 my $passwd = $self->{user}->passwd;
311 if ($passwd && $cmdline eq $passwd) {
312 $self->send($self->msg('pw1'));
313 $self->state('passwd1');
315 $self->conn->{echo} = $self->conn->{decho};
316 delete $self->conn->{decho};
317 $self->send($self->msg('sorry'));
318 $self->state('prompt');
320 } elsif ($self->{state} eq 'passwd1') {
321 $self->{passwd} = $cmdline;
322 $self->send($self->msg('pw2'));
323 $self->state('passwd2');
324 } elsif ($self->{state} eq 'passwd2') {
325 if ($cmdline eq $self->{passwd}) {
326 $self->{user}->passwd($cmdline);
327 $self->send($self->msg('pw3'));
329 $self->send($self->msg('pw4'));
331 $self->conn->{echo} = $self->conn->{decho};
332 delete $self->conn->{decho};
333 $self->state('prompt');
334 } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
335 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
336 for (@{$self->{talklist}}) {
337 if ($self->{state} eq 'talk') {
338 $self->send_talks($_, $self->msg('talkend'));
340 $self->local_send('C', $self->msg('chatend', $_));
343 $self->state('prompt');
344 delete $self->{talklist};
345 } elsif ($cmdline =~ m|^/+\w+|) {
347 my $sendit = $cmdline =~ s|^/+||;
348 my @in = $self->run_cmd($cmdline);
349 $self->send_ans(@in);
350 if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
351 foreach my $l (@in) {
353 if (@bad = BadWords::check($l)) {
354 $self->badcount(($self->badcount||0) + @bad);
355 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
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
371 if (@bad = BadWords::check($cmdline)) {
372 $self->badcount(($self->badcount||0) + @bad);
373 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
375 for (@{$self->{talklist}}) {
376 if ($self->{state} eq 'talk') {
377 $self->send_talks($_, $rawline);
379 send_chats($self, $_, $rawline);
383 $self->send($self->talk_prompt) if $self->{state} eq 'talk';
384 $self->send($self->chat_prompt) if $self->{state} eq 'chat';
387 $self->state('prompt');
389 } elsif (my $func = $self->{func}) {
392 if (ref $self->{edit}) {
393 eval { @ans = $self->{edit}->$func($self, $rawline)};
395 eval { @ans = &{$self->{func}}($self, $rawline) };
398 $self->send_ans("Syserr: on stored func $self->{func}", $@);
399 delete $self->{func};
400 $self->state('prompt');
403 $self->send_ans(@ans);
405 $self->send_ans(run_cmd($self, $cmdline));
408 # check for excessive swearing
409 if ($maxbadcount && $self->{badcount} && $self->{badcount} >= $maxbadcount) {
410 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
415 # send a prompt only if we are in a prompt state
416 $self->prompt() if $self->{state} =~ /^prompt/o;
419 # send out the talk messages taking into account vias and connectivity
422 my ($self, $ent, $line) = @_;
424 my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
425 $to = $ent unless $to;
426 my $call = $via && $via ne '*' ? $via : $to;
427 my $clref = Route::get($call);
428 my $dxchan = $clref->dxchan if $clref;
430 $dxchan->talk($self->{call}, $to, undef, $line);
432 $self->send($self->msg('disc2', $via ? $via : $to));
433 my @l = grep { $_ ne $ent } @{$self->{talklist}};
435 $self->{talklist} = \@l;
437 delete $self->{talklist};
438 $self->state('prompt');
449 my $msgid = DXProt::nextchatmsgid();
450 $text = "#$msgid $text";
451 $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
459 for (@{$self->{talklist}}) {
460 my ($to, $via) = /(\S+)>(\S+)/;
464 return $self->msg($prompt, join(',', @call));
470 return $self->special_prompt('talkprompt');
476 return $self->special_prompt('chatprompt');
480 # send a load of stuff to a command user with page prompting
488 if ($self->{pagelth} && @_ > $self->{pagelth}) {
490 for ($i = $self->{pagelth}; $i-- > 0; ) {
492 $line =~ s/\s+$//o; # why am having to do this?
495 $self->{pagedata} = [ @_ ];
496 $self->state('page');
497 $self->send($self->msg('page', scalar @_));
510 # this is the thing that preps for running the command, it is done like this for the
511 # benefit of remote command execution
517 my $user = $self->{user};
518 my $call = $self->{call};
522 return () if length $cmdline == 0;
524 # split the command line up into parts, the first part is the command
525 my ($cmd, $args) = split /\s+/, $cmdline, 2;
526 $args = "" unless defined $args;
531 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
532 LogDbg('DXCommand', "cmd: $self->{call} - invalid characters in '$cmd'");
533 return $self->_error_out('e1');
536 # strip out // on command only
541 dbg("cmd: $cmd") if isdbg('command');
543 # alias it if possible
544 my $acmd = CmdAlias::get_cmd($cmd);
546 ($cmd, $args) = split /\s+/, "$acmd $args", 2;
547 $args = "" unless defined $args;
548 dbg("cmd: aliased $cmd $args") if isdbg('command');
551 # first expand out the entry to a command
552 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
553 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
556 dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
558 my $package = find_cmd_name($path, $fcmd);
561 if ($package && $self->can("${package}::handle")) {
563 dbg("cmd: package $package") if isdbg('command');
564 # Log('cmd', "$self->{call} on $self->{hostname} : '$cmd $args'");
565 my $t0 = [gettimeofday];
566 eval { @ans = &{"${package}::handle"}($self, $args) };
568 DXDebug::dbgprintring(25);
569 return (DXDebug::shortmess($@));
571 if (isdbg('progress')) {
572 my $msecs = _diffms($t0);
573 my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
574 dbg($s) if $cmd !~ /^(?:echo|blank)/ || isdbg('echo'); # cut down a bit on HRD and other clients' noise
577 dbg("cmd: $package not present") if isdbg('command');
578 return $self->_error_out('e1');
581 dbg("cmd: $cmd not found") if isdbg('command');
582 return $self->_error_out('e1');
588 delete $self->{errors};
590 if (++$self->{errors} > $DXChannel::maxerrors) {
591 $self->send($self->msg('e26'));
596 return map {s/([^\s])\s+$/$1/; $_} @ans;
600 # This is called from inside the main cluster processing loop and is used
601 # for despatching commands that are doing some long processing job
606 my @dxchan = DXChannel::get_all();
610 foreach $dxchan (@dxchan) {
611 next unless $dxchan->is_user;
613 # send a outstanding message prompt if required
614 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
615 $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
616 $dxchan->lastmsgpoll($t);
619 # send a prompt if no activity out on this channel
620 if ($t >= $dxchan->t + $main::user_interval) {
621 $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
625 $maxusers = $users if $users > $maxusers;
627 if ($dxchan->{isslugged} && $main::systime > $dxchan->{isslugged}) {
628 foreach my $ref (@{$dxchan->{sluggedpcs}}) {
629 if ($ref->[0] == 61) {
630 Spot::add(@{$ref->[2]});
631 DXProt::send_dx_spot($dxchan, $ref->[1], @{$ref->[2]});
635 $dxchan->{isslugged} = 0;
636 $dxchan->{sluggedpcs} = [];
644 # finish up a user context
649 my $call = $self->call;
651 return if $self->{disconnecting}++;
653 delete $self->{senddbg};
654 RBN::del_seeme($call);
656 my $uref = Route::User::get($call);
659 # @rout = $main::routeroot->del_user($uref);
660 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
662 # dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
664 # issue a pc17 to everybody interested
665 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
666 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
668 confess "trying to disconnect a non existant user $call";
671 # I was the last node visited
672 $self->user->node($main::mycall);
674 # send info to all logged in thingies
675 $self->tell_login('logoutu');
676 $self->tell_buddies('logoutb');
678 LogDbg('DXCommand', "$call disconnected");
680 $self->SUPER::disconnect;
684 # short cut to output a prompt
691 return if $self->{gtk}; # 'cos prompts are not a concept that applies here
693 my $call = $self->call;
694 my $date = cldate($main::systime);
695 my $time = ztime($main::systime);
696 my $prompt = $self->{prompt} || $self->msg('pr');
698 $call = "($call)" unless $self->here;
699 $prompt =~ s/\%C/$call/g;
700 $prompt =~ s/\%D/$date/g;
701 $prompt =~ s/\%T/$time/g;
702 $prompt =~ s/\%M/$main::mycall/g;
704 $self->send($prompt);
707 # broadcast a message to all users [except those mentioned after buffer]
710 my $pkg = shift; # ignored
711 my $s = shift; # the line to be rebroadcast
713 foreach my $dxchan (DXChannel::get_all()) {
714 next unless $dxchan->is_user; # only interested in user channels
715 next if grep $dxchan == $_, @_;
716 $dxchan->send($s); # send it
720 # gimme all the users
723 goto &DXChannel::get_all_users;
726 # run a script for this user
730 my $silent = shift || 0;
735 # search for the command in the cache of short->long form commands
740 my ($path, $short_cmd, $suffix) = @_;
743 # commands are lower case
744 $short_cmd = lc $short_cmd;
745 dbg("command: $path $short_cmd\n") if isdbg('command');
747 # do some checking for funny characters
748 return () if $short_cmd =~ /\/$/;
750 # return immediately if we have it
751 ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
752 if ($apath && $acmd) {
753 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
754 return ($apath, $acmd);
758 my @parts = split '/', $short_cmd;
762 while (my $p = shift @parts) {
763 opendir(D, $curdir) or confess "can't open $curdir $!";
767 # if this isn't the last part
770 foreach my $l (sort @ls) {
772 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
773 dbg("got dir: $curdir/$l\n") if isdbg('command');
780 # only proceed if we find the directory asked for
781 return () unless $found;
783 foreach my $l (sort @ls) {
785 next unless $l =~ /\.$suffix$/;
786 if ($p eq substr($l, 0, length $p)) {
787 $l =~ s/\.$suffix$//;
788 $dirfn = "" unless $dirfn;
789 $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
790 dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
791 return ($path, "$dirfn$l");
800 # clear the command name cache
805 for my $k (keys %Cache) {
806 unless ($k =~ /cmd_cache/) {
807 dbg("Undefining cmd $k") if isdbg('command');
808 undef $DXCommandmode::{"${k}::"};
812 %Cache = ( cmd_clear_cmd_cache => $Cache{cmd_clear_cmd_cache} );
816 # the persistant execution of things from the command directories
819 # This allows perl programs to call functions dynamically
821 # This has been nicked directly from the perlembed pages
823 #require Devel::Symdump;
825 sub valid_package_name {
827 $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
830 return "cmd_$string";
834 # this bit of magic finds a command in the offered directory
838 my $package = valid_package_name($cmdname);
839 my $filename = "$path/$cmdname.pl";
840 my $mtime = -M $filename;
842 # return if we can't find it
844 unless (defined $mtime) {
845 $errstr = DXM::msg('e1');
849 if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
850 #we have compiled this subroutine already,
851 #it has not been updated on disk, nothing left to do
852 #print STDERR "already compiled $package->handler\n";
853 dbg("find_cmd_name: $package cached") if isdbg('command');
856 my $sub = readfilestr($filename);
858 $errstr = "Syserr: can't open '$filename' $!";
862 #wrap the code into a subroutine inside our unique package
863 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}; );
866 if ($sub =~ m|\s*sub\s+handle\n|) {
869 $eval .= qq(sub handle { $sub });
873 my @list = split /\n/, $eval;
876 dbg($_ . "\n") if isdbg('eval');
880 # get rid of any existing sub and try to compile the new one
883 if (exists $Cache{$package}) {
884 dbg("find_cmd_name: Redefining $package") if isdbg('command');
885 undef $DXCommandmode::{"${package}::"};
886 delete $Cache{$package};
888 dbg("find_cmd_name: Defining $package") if isdbg('command');
893 $Cache{$package} = {mtime => $mtime } unless $@;
896 return "DXCommandmode::$package";
904 $self->SUPER::send(dd(['cmd',$_]));
907 $self->SUPER::send(@_);
913 my ($self, $let, $buf) = @_;
914 if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
915 if ($self->{enhanced}) {
916 $self->send_later($let, $buf);
925 # send a talk message here
928 my ($self, $from, $to, $via, $line, $onode) = @_;
929 $line =~ s/\\5E/\^/g;
932 $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
934 $self->local_send('T', "$to de $from: $line");
937 Log('talk', $to, $from, '<' . ($onode || '*'), $line);
938 # send a 'not here' message if required
939 unless ($self->{here} && $from ne $to) {
940 my $key = "$to$from";
941 unless (exists $nothereslug{$key}) {
943 if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
944 my $name = $self->user->name || $to;
945 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
946 $nothereslug{$key} = $main::systime;
947 $dxchan->talk($to, $from, undef, $s);
964 if (!$self->{ann_talk} && $to ne $self->{call}) {
965 my $call = AnnTalk::is_talk_candidate($_[0], $text);
969 if ($self->{annfilter}) {
970 ($filter, $hops) = $self->{annfilter}->it(@_ );
971 return unless $filter;
974 unless ($self->{ann}) {
975 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
977 return if $target eq 'SYSOP' && $self->{priv} < 5;
980 $buf = dd(['ann', $to, $target, $text, @_])
982 $buf = "$to$target de $_[0]: $text";
984 $buf .= "\a\a" if $self->{beep};
986 $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
1000 return unless grep uc $_ eq $target, @{$self->{user}->{group}};
1002 $text =~ s/^\#\d+ //;
1005 $buf = dd(['chat', $to, $target, $text, @_])
1007 $buf = "$target de $_[0]: $text";
1008 #$buf =~ s/\%5E/^/g;
1009 $buf .= "\a\a" if $self->{beep};
1011 $self->local_send('C', $buf);
1018 my $t = ztime($_[2]);
1019 my ($slot1, $slot2) = ('', '');
1021 my $clth = 30 + $self->{width} - 80; # allow comment to grow according the screen width
1024 my $comment = substr (($c || ''), 0, $clth);
1025 $comment .= ' ' x ($clth - (length($comment)));
1027 if (!$slot1 && $self->{user}->wantgrid) {
1028 my $ref = DXUser::get_current($_[1]);
1029 if ($ref && $ref->qra) {
1030 $slot1 = ' ' . substr($ref->qra, 0, 4);
1033 if (!$slot1 && $self->{user}->wantusstate) {
1034 $slot1 = " $_[12]" if $_[12];
1037 if ($self->{user}->wantdxitu) {
1038 $slot1 = sprintf(" %2d", $_[8]) if defined $_[8];
1039 } elsif ($self->{user}->wantdxcq) {
1040 $slot1 = sprintf(" %2d", $_[9]) if defined $_[9];
1043 $comment = substr($comment, 0, $clth-length($slot1)) . $slot1 if $slot1;
1045 if (!$slot2 && $self->{user}->wantgrid) {
1047 $origin =~ s/-#$//; # sigh......
1048 my $ref = DXUser::get_current($origin);
1049 if ($ref && $ref->qra) {
1050 $slot2 = ' ' . substr($ref->qra, 0, 4);
1053 if (!$slot2 && $self->{user}->wantusstate) {
1054 $slot2 = " $_[13]" if $_[13];
1057 if ($self->{user}->wantdxitu) {
1058 $slot2 = sprintf(" %2d", $_[10]) if defined $_[10];
1059 } elsif ($self->{user}->wantdxcq) {
1060 $slot2 = sprintf(" %2d", $_[11]) if defined $_[11];
1064 my $o = sprintf("%-9s", $_[4] . ':');
1065 my $qrg = sprintf "%8.1f", $_[0];
1066 if (length $qrg >= 9) {
1067 while (length($o)+length($qrg) > 17 && $o =~ / $/) {
1071 my $spot = sprintf "%-12s", $_[1];
1072 my $front = "DX de $o $qrg $spot";
1073 while (length($front) > 38 && $front =~ / $/) {
1078 return sprintf "$front %-s $t$slot2", $comment;
1087 my $isolate = shift;
1088 return unless $self->{dx};
1090 my ($filter, $hops);
1092 if ($self->{spotsfilter}) {
1093 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1094 return unless $filter;
1097 dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1100 if ($self->{ve7cc}) {
1101 $buf = VE7CC::dx_spot($self, @_);
1102 } elsif ($self->{gtk}) {
1103 my ($dxloc, $byloc);
1105 my $ref = DXUser::get_current($_[4]);
1108 $byloc = substr($byloc, 0, 4) if $byloc;
1112 $spot =~ s|/\w{1,4}$||;
1113 $ref = DXUser::get_current($spot);
1116 $dxloc = substr($dxloc, 0, 4) if $dxloc;
1118 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1121 $buf = $self->format_dx_spot(@_);
1122 $buf .= "\a\a" if $self->{beep};
1123 #$buf =~ s/\%5E/^/g;
1126 $self->local_send('X', $buf);
1133 my $isolate = shift;
1134 my ($filter, $hops);
1136 return unless $self->{wwv};
1138 if ($self->{wwvfilter}) {
1139 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1140 return unless $filter;
1145 $buf = dd(['wwv', @_])
1147 $buf = "WWV de $_[6] <$_[1]>: SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1148 $buf .= "\a\a" if $self->{beep};
1151 $self->local_send('V', $buf);
1158 my $isolate = shift;
1159 my ($filter, $hops);
1161 return unless $self->{wcy};
1163 if ($self->{wcyfilter}) {
1164 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1165 return unless $filter;
1170 $buf = dd(['wcy', @_])
1172 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1173 $buf .= "\a\a" if $self->{beep};
1175 $self->local_send('Y', $buf);
1178 # broadcast debug stuff to all interested parties
1181 my $s = shift; # the line to be rebroadcast
1183 foreach my $dxchan (DXChannel::get_all_users) {
1184 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1185 if ($dxchan->{gtk}) {
1186 $dxchan->send_later('L', dd(['db', $s]));
1188 $dxchan->send_later('L', $s);
1199 if ($self->state eq 'enterbody') {
1200 my $loc = $self->{loc} || confess "local var gone missing" ;
1201 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1203 push @out, &{$loc->{endaction}}($self); # like this for < 5.8.0
1205 $self->state('prompt');
1206 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1207 push @out, $self->msg('m10');
1208 delete $loc->{lines};
1209 delete $self->{loc};
1211 $self->state('prompt');
1213 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1214 # i.e. it ain't and end or abort, therefore store the line
1217 confess "Invalid state $self->{state}";
1222 sub store_startup_script
1225 my $loc = $self->{loc} || confess "local var gone missing" ;
1227 my $call = $loc->{call} || confess "callsign gone missing";
1228 confess "lines array gone missing" unless ref $loc->{lines};
1229 my $r = Script::store($call, $loc->{lines});
1232 push @out, $self->msg('m19', $call, $r);
1234 push @out, $self->msg('m20', $call);
1237 push @out, "error opening startup script $call $!";
1242 # Import any commands contained in any files in import_cmd directory
1244 # If the filename has a recogisable callsign as some delimited part
1245 # of it, then this is the user the command will be run as.
1249 # are there any to do in this directory?
1250 return unless -d $cmdimportdir;
1251 unless (opendir(DIR, $cmdimportdir)) {
1252 LogDbg('err', "can\'t open $cmdimportdir $!");
1256 my @names = readdir(DIR);
1260 return unless @names;
1262 foreach $name (@names) {
1263 next if $name =~ /^\./;
1265 my $s = Script->new($name, $cmdimportdir);
1267 LogDbg('DXCommand', "Run import cmd file $name");
1268 my @cat = split /[^A-Za-z0-9]+/, $name;
1269 my ($call) = grep {is_callsign(uc $_)} @cat;
1270 $call ||= $main::mycall;
1275 $s->inscript(0); # switch off script checks
1277 if ($call eq $main::mycall) {
1278 @out = $s->run($main::me, 1);
1280 my $dxchan = DXChannel::get($call);
1282 @out = $s->run($dxchan, 1);
1284 my $u = DXUser::get($call);
1286 $dxchan = $main::me;
1287 my $old = $dxchan->{call};
1288 my $priv = $dxchan->{priv};
1289 my $user = $dxchan->{user};
1290 $dxchan->{call} = $call;
1291 $dxchan->{priv} = $u->priv;
1292 $dxchan->{user} = $u;
1293 @out = $s->run($dxchan, 1);
1294 $dxchan->{call} = $old;
1295 $dxchan->{priv} = $priv;
1296 $dxchan->{user} = $user;
1298 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1304 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1307 LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1308 unlink "$cmdimportdir/$name";
1313 sub print_find_reply
1315 my ($self, $node, $target, $flag, $ms) = @_;
1316 my $sort = $flag == 2 ? "External" : "Local";
1317 $self->send("$sort $target found at $node in $ms ms" );
1320 # send the most relevant motd
1326 unless ($self->isregistered) {
1327 $motd = "${main::motd}_nor_$self->{lang}";
1328 $motd = "${main::motd}_nor" unless -e $motd;
1330 $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1331 $motd = $main::motd unless $motd && -e $motd;
1332 if ($self->conn->ax25) {
1334 $motd = "${motd}_ax25" if -e "${motd}_ax25";
1336 $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1339 $self->send_file($motd) if -e $motd;
1342 # Punt off a long running command into a separate process
1344 # This is called from commands to run some potentially long running
1345 # function. The process forks and then runs the function and returns
1346 # the result back to the cmd.
1348 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1349 # IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER
1350 # THE CURRENT CONTEXT!!
1352 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1358 my $call = $self->{call};
1362 my $cb = delete $args{cb};
1363 my $prefix = delete $args{prefix};
1364 my $progress = delete $args{progress};
1365 my $args = delete $args{args} || [];
1366 my $t0 = [gettimeofday];
1370 # just behave normally if something has set the "one-shot" _nospawn in the channel
1371 if ($self->{_nospawn}) {
1372 eval { @out = $cmdref->(@$args); };
1374 DXDebug::dbgprintring(25);
1375 push @out, DXDebug::shortmess($@);
1380 my $fc = DXSubprocess->new;
1381 # $fc->serializer(\&encode_json);
1382 # $fc->deserializer(\&decode_json);
1386 if (isdbg('progress')) {
1387 my $s = qq{$call line: "$line"};
1388 $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1392 ++$self->{_in_sub_process};
1393 dbg "\$self->{_in_sub_process} = $self->{_in_sub_process}";
1394 @out = $cmdref->(@$args);
1395 --$self->{_in_sub_process} if $self->{_in_sub_process} > 0;
1398 DXDebug::dbgprintring(25);
1399 push @out, DXDebug::shortmess($@);
1405 my ($fc, $err, @res) = @_;
1406 my $dxchan = DXChannel::get($call);
1407 return unless $dxchan;
1410 my $s = "DXProt::spawn_cmd: call $call error $err";
1411 dbg($s) if isdbg('chan');
1416 # transform output if required
1417 @res = $cb->($dxchan, @res);
1420 if (defined $prefix) {
1421 $dxchan->send(map {"$prefix$_"} @res);
1423 $dxchan->send(@res);
1426 diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
1434 return ($users, $maxusers);