fix longstanding issues with console EOL handling
[spider.git] / perl / DXCommandmode.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the user facing command mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 #
8
9
10 package DXCommandmode;
11
12 #use POSIX;
13
14 @ISA = qw(DXChannel);
15
16 use 5.10.1;
17
18 use POSIX qw(:math_h);
19 use DXUtil;
20 use DXChannel;
21 use DXUser;
22 use DXVars;
23 use DXDebug;
24 use DXM;
25 use DXLog;
26 use DXLogPrint;
27 use DXBearing;
28 use CmdAlias;
29 use Filter;
30 use Minimuf;
31 use DXDb;
32 use AnnTalk;
33 use WCY;
34 use Sun;
35 use Internet;
36 use Script;
37 use QSL;
38 use DB_File;
39 use VE7CC;
40 use DXXml;
41 use AsyncMsg;
42 use JSON;
43 use Time::HiRes qw(gettimeofday tv_interval);
44
45 use Mojo::IOLoop;
46 use DXSubprocess;
47 use Mojo::UserAgent;
48
49 use strict;
50 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase %nothereslug
51         $maxbadcount $msgpolltime $default_pagelth $cmdimportdir $users $maxusers);
52
53 %Cache = ();                                    # cache of dynamically loaded routine's mod times
54 %cmd_cache = ();                                # cache of short names
55 $errstr = ();                                   # error string from eval
56 %aliases = ();                                  # aliases for (parts of) commands
57 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
58 $maxbadcount = 3;                               # no of bad words allowed before disconnection
59 $msgpolltime = 3600;                    # the time between polls for new messages 
60 $cmdimportdir = "$main::root/cmd_import"; # the base directory for importing command scripts 
61                                           # this does not exist as default, you need to create it manually
62 $users = 0;                                       # no of users on this node currently
63 $maxusers = 0;                            # max no users on this node for this run
64
65 #
66 # obtain a new connection this is derived from dxchannel
67 #
68
69 sub new 
70 {
71         my $self = DXChannel::alloc(@_);
72
73         # routing, this must go out here to prevent race condx
74         my $pkg = shift;
75         my $call = shift;
76 #       my @rout = $main::routeroot->add_user($call, Route::here(1));
77         DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
78
79         # ALWAYS output the user
80         my $ref = Route::User::get($call);
81         if ($ref) {
82                 $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref);
83                 $main::me->route_pc92a($main::mycall, undef, $main::routeroot, $ref) unless $DXProt::pc92_slug_changes;
84         }
85
86         return $self;
87 }
88
89 # this is how a a connection starts, you get a hello message and the motd with
90 # possibly some other messages asking you to set various things up if you are
91 # new (or nearly new and slacking) user.
92
93 sub start
94
95         my ($self, $line, $sort) = @_;
96         my $user = $self->{user};
97         my $call = $self->{call};
98         my $name = $user->{name};
99         
100         # log it
101         my $host = $self->{conn}->peerhost;
102         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
103         $host ||= "unknown";
104         $self->{hostname} = $host;
105
106         $self->{name} = $name ? $name : $call;
107         $self->send($self->msg('l2',$self->{name}));
108         $self->state('prompt');         # a bit of room for further expansion, passwords etc
109         $self->{priv} = $user->priv || 0;
110         $self->{lang} = $user->lang || $main::lang || 'en';
111         my $pagelth = $user->pagelth;
112         $pagelth = $default_pagelth unless defined $pagelth;
113         $self->{pagelth} = $pagelth;
114         ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
115         if ($line =~ /host=/) {
116                 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
117                 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
118                 unless ($h) {
119                         ($h) = $line =~ /host=([\da..fA..F:]+)/;
120                         $line =~ s/\s*host=[\da..fA..F:]+// if $h;
121                 }
122                 $self->{hostname} = $h if $h;
123         }
124         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
125         $self->{consort} = $line;       # save the connection type
126
127         LogDbg('DXCommand', "$call connected from $self->{hostname}");
128
129         # set some necessary flags on the user if they are connecting
130         $self->{beep} = $user->wantbeep;
131         $self->{ann} = $user->wantann;
132         $self->{wwv} = $user->wantwwv;
133         $self->{wcy} = $user->wantwcy;
134         $self->{talk} = $user->wanttalk;
135         $self->{wx} = $user->wantwx;
136         $self->{dx} = $user->wantdx;
137         $self->{logininfo} = $user->wantlogininfo;
138         $self->{ann_talk} = $user->wantann_talk;
139         $self->{wantrbn} = $user->wantrbn;
140         $self->{here} = 1;
141         $self->{prompt} = $user->prompt if $user->prompt;
142         $self->{lastmsgpoll} = 0;
143
144         # sort out new dx spot stuff
145         $user->wantdxcq(0) unless defined $user->{wantdxcq};
146         $user->wantdxitu(0) unless defined $user->{wantdxitu};  
147         $user->wantusstate(0) unless defined $user->{wantusstate};
148
149         # sort out registration
150         if ($main::reqreg == 1) {
151                 $self->{registered} = $user->registered;
152         } elsif ($main::reqreg == 2) {
153                 $self->{registered} = !$user->registered;
154         } else {
155                 $self->{registered} = 1;
156         }
157
158         # send the relevant MOTD
159         $self->send_motd;
160
161         # sort out privilege reduction
162         $self->{priv} = 0 unless $self->{hostname} eq '127.0.0.1' || $self->{hostname} eq '::1' || $self->conn->{usedpasswd};
163
164         # get the filters
165         my $nossid = $call;
166         $nossid =~ s/-\d+$//;
167         
168         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) 
169                 || Filter::read_in('spots', $nossid, 0)
170                         || Filter::read_in('spots', 'user_default', 0);
171         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) 
172                 || Filter::read_in('wwv', $nossid, 0) 
173                         || Filter::read_in('wwv', 'user_default', 0);
174         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) 
175                 || Filter::read_in('wcy', $nossid, 0) 
176                         || Filter::read_in('wcy', 'user_default', 0);
177         $self->{annfilter} = Filter::read_in('ann', $call, 0) 
178                 || Filter::read_in('ann', $nossid, 0) 
179                         || Filter::read_in('ann', 'user_default', 0) ;
180         $self->{rbnfilter} = Filter::read_in('rbn', $call, 0) 
181                 || Filter::read_in('rbn', $nossid, 0)
182                 || Filter::read_in('rbn', 'user_default', 0);
183         
184         # clean up qra locators
185         my $qra = $user->qra;
186         $qra = undef if ($qra && !DXBearing::is_qra($qra));
187         unless ($qra) {
188                 my $lat = $user->lat;
189                 my $long = $user->long;
190                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
191         }
192
193         # decide on echo
194         my $echo = $user->wantecho;
195         unless ($echo) {
196                 $self->send_now('E', "0");
197                 $self->send($self->msg('echow'));
198                 $self->conn->echo($echo) if $self->conn->can('echo');
199         }
200         
201         $self->tell_login('loginu');
202         $self->tell_buddies('loginb');
203         
204         # do we need to send a forward/opernam?
205         my $lastoper = $user->lastoper || 0;
206         my $homenode = $user->homenode || ""; 
207         if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
208                 run_cmd($main::me, "forward/opernam $call");
209                 $user->lastoper($main::systime + ((int rand(10)) * 86400));
210         }
211
212         # run a script send the output to the punter
213         my $script = new Script(lc $call) || new Script('user_default');
214         $script->run($self) if $script;
215
216         # send cluster info
217         $self->send($self->run_cmd("show/cluster"));
218
219         # send prompts for qth, name and things
220         $self->send($self->msg('namee1')) if !$user->name;
221         $self->send($self->msg('qthe1')) if !$user->qth;
222         $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
223         $self->send($self->msg('hnodee1')) if !$user->qth;
224         $self->send($self->msg('m9')) if DXMsg::for_me($call);
225
226         # send out any buddy messages for other people that are online
227         foreach my $call (@{$user->buddies}) {
228                 my $ref = Route::User::get($call);
229                 if ($ref) {
230                         foreach my $node ($ref->parents) {
231                                 $self->send($self->msg($node eq $main::mycall ? 'loginb' : 'loginbn', $call, $node));
232                         } 
233                 }
234         }
235
236         $self->lastmsgpoll($main::systime);
237         $self->prompt;
238 }
239
240 #
241 # This is the normal command prompt driver
242 #
243
244 sub normal
245 {
246         my $self = shift;
247         my $cmdline = shift;
248         my @ans;
249
250         # save this for them's that need it
251         my $rawline = $cmdline;
252         
253         # remove leading and trailing spaces
254         $cmdline =~ s/^\s*(.*)\s*$/$1/;
255         
256         if ($self->{state} eq 'page') {
257                 my $i = $self->{pagelth};
258                 my $ref = $self->{pagedata};
259                 my $tot = @$ref;
260                 
261                 # abort if we get a line starting in with a
262                 if ($cmdline =~ /^a/io) {
263                         undef $ref;
264                         $i = 0;
265                 }
266         
267                 # send a tranche of data
268                 while ($i-- > 0 && @$ref) {
269                         my $line = shift @$ref;
270                         $line =~ s/\s+$//o;     # why am having to do this? 
271                         $self->send($line);
272                 }
273                 
274                 # reset state if none or else chuck out an intermediate prompt
275                 if ($ref && @$ref) {
276                         $tot -= $self->{pagelth};
277                         $self->send($self->msg('page', $tot));
278                 } else {
279                         $self->state('prompt');
280                 }
281         } elsif ($self->{state} eq 'sysop') {
282                 my $passwd = $self->{user}->passwd;
283                 if ($passwd) {
284                         my @pw = grep {$_ !~ /\s/} split //, $passwd;
285                         my @l = @{$self->{passwd}};
286                         my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
287                         if ($cmdline =~ /$str/) {
288                                 $self->{priv} = $self->{user}->priv;
289                         } else {
290                                 $self->send($self->msg('sorry'));
291                         }
292                 } else {
293                         $self->send($self->msg('sorry'));
294                 }
295                 $self->state('prompt');
296         } elsif ($self->{state} eq 'passwd') {
297                 my $passwd = $self->{user}->passwd;
298                 if ($passwd && $cmdline eq $passwd) {
299                         $self->send($self->msg('pw1'));
300                         $self->state('passwd1');
301                 } else {
302                         $self->conn->{echo} = $self->conn->{decho};
303                         delete $self->conn->{decho};
304                         $self->send($self->msg('sorry'));
305                         $self->state('prompt');
306                 }
307         } elsif ($self->{state} eq 'passwd1') {
308                 $self->{passwd} = $cmdline;
309                 $self->send($self->msg('pw2'));
310                 $self->state('passwd2');
311         } elsif ($self->{state} eq 'passwd2') {
312                 if ($cmdline eq $self->{passwd}) {
313                         $self->{user}->passwd($cmdline);
314                         $self->send($self->msg('pw3'));
315                 } else {
316                         $self->send($self->msg('pw4'));
317                 }
318                 $self->conn->{echo} = $self->conn->{decho};
319                 delete $self->conn->{decho};
320                 $self->state('prompt');
321         } elsif ($self->{state} eq 'talk' || $self->{state} eq 'chat') {
322                 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
323                         for (@{$self->{talklist}}) {
324                                 if ($self->{state} eq 'talk') {
325                                         $self->send_talks($_,  $self->msg('talkend'));
326                                 } else {
327                                         $self->local_send('C', $self->msg('chatend', $_));
328                                 }
329                         }
330                         $self->state('prompt');
331                         delete $self->{talklist};
332                 } elsif ($cmdline =~ m|^/+\w+|) {
333                         $cmdline =~ s|^/||;
334                         my $sendit = $cmdline =~ s|^/+||;
335                         my @in = $self->run_cmd($cmdline);
336                         $self->send_ans(@in);
337                         if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
338                                 foreach my $l (@in) {
339                                         my @bad;
340                                         if (@bad = BadWords::check($l)) {
341                                                 $self->badcount(($self->badcount||0) + @bad);
342                                                 LogDbg('DXCommand', "$self->{call} swore: $l with words:" . join(',', @bad) . ")");
343                                         } else {
344                                                 for (@{$self->{talklist}}) {
345                                                         if ($self->{state} eq 'talk') {
346                                                                 $self->send_talks($_, $l);
347                                                         } else {
348                                                                 send_chats($self, $_, $l)
349                                                         }
350                                                 }
351                                         }
352                                 }
353                         }
354                         $self->send($self->{state} eq 'talk' ? $self->talk_prompt : $self->chat_prompt);
355                 } elsif ($self->{talklist} && @{$self->{talklist}}) {
356                         # send what has been said to whoever is in this person's talk list
357                         my @bad;
358                         if (@bad = BadWords::check($cmdline)) {
359                                 $self->badcount(($self->badcount||0) + @bad);
360                                 LogDbg('DXCommand', "$self->{call} swore: $cmdline with words:" . join(',', @bad) . ")");
361                         } else {
362                                 for (@{$self->{talklist}}) {
363                                         if ($self->{state} eq 'talk') {
364                                                 $self->send_talks($_, $rawline);
365                                         } else {
366                                                 send_chats($self, $_, $rawline);
367                                         }
368                                 }
369                         }
370                         $self->send($self->talk_prompt) if $self->{state} eq 'talk';
371                         $self->send($self->chat_prompt) if $self->{state} eq 'chat';
372                 } else {
373                         # for safety
374                         $self->state('prompt');
375                 }
376         } elsif (my $func = $self->{func}) {
377                 no strict 'refs';
378                 my @ans;
379                 if (ref $self->{edit}) {
380                         eval { @ans = $self->{edit}->$func($self, $rawline)};
381                 } else {
382                         eval {  @ans = &{$self->{func}}($self, $rawline) };
383                 }
384                 if ($@) {
385                         $self->send_ans("Syserr: on stored func $self->{func}", $@);
386                         delete $self->{func};
387                         $self->state('prompt');
388                         undef $@;
389                 }
390                 $self->send_ans(@ans);
391         } else {
392                 $self->send_ans(run_cmd($self, $cmdline));
393         } 
394
395         # check for excessive swearing
396         if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
397                 LogDbg('DXCommand', "$self->{call} logged out for excessive swearing");
398                 $self->disconnect;
399                 return;
400         }
401
402         # send a prompt only if we are in a prompt state
403         $self->prompt() if $self->{state} =~ /^prompt/o;
404 }
405
406 # send out the talk messages taking into account vias and connectivity
407 sub send_talks
408 {
409         my ($self, $ent, $line) = @_;
410         
411         my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
412         $to = $ent unless $to;
413         my $call = $via && $via ne '*' ? $via : $to;
414         my $clref = Route::get($call);
415         my $dxchan = $clref->dxchan if $clref;
416         if ($dxchan) {
417                 $dxchan->talk($self->{call}, $to, undef, $line);
418         } else {
419                 $self->send($self->msg('disc2', $via ? $via : $to));
420                 my @l = grep { $_ ne $ent } @{$self->{talklist}};
421                 if (@l) {
422                         $self->{talklist} = \@l;
423                 } else {
424                         delete $self->{talklist};
425                         $self->state('prompt');
426                 }
427         }
428 }
429
430 sub send_chats
431 {
432         my $self = shift;
433         my $target = shift;
434         my $text = shift;
435
436         my $msgid = DXProt::nextchatmsgid();
437         $text = "#$msgid $text";
438         $main::me->normal(DXProt::pc93($target, $self->{call}, undef, $text));
439 }
440
441 sub special_prompt
442 {
443         my $self = shift;
444         my $prompt = shift;
445         my @call;
446         for (@{$self->{talklist}}) {
447                 my ($to, $via) = /(\S+)>(\S+)/;
448                 $to = $_ unless $to;
449                 push @call, $to;
450         }
451         return $self->msg($prompt, join(',', @call));
452 }
453
454 sub talk_prompt
455 {
456         my $self = shift;
457         return $self->special_prompt('talkprompt');
458 }
459
460 sub chat_prompt
461 {
462         my $self = shift;
463         return $self->special_prompt('chatprompt');
464 }
465
466 #
467 # send a load of stuff to a command user with page prompting
468 # and stuff
469 #
470
471 sub send_ans
472 {
473         my $self = shift;
474         
475         if ($self->{pagelth} && @_ > $self->{pagelth}) {
476                 my $i;
477                 for ($i = $self->{pagelth}; $i-- > 0; ) {
478                         my $line = shift @_;
479                         $line =~ s/\s+$//o;     # why am having to do this? 
480                         $self->send($line);
481                 }
482                 $self->{pagedata} =  [ @_ ];
483                 $self->state('page');
484                 $self->send($self->msg('page', scalar @_));
485         } else {
486                 for (@_) {
487                         if (defined $_) {
488                                 $self->send($_);
489                         } else {
490                                 $self->send('');
491                         }
492                 }
493         } 
494 }
495
496
497 # this is the thing that preps for running the command, it is done like this for the 
498 # benefit of remote command execution
499 #
500
501 sub run_cmd
502 {
503         my $self = shift;
504         my $user = $self->{user};
505         my $call = $self->{call};
506         my $cmdline = shift;
507         my @ans;
508         
509         return () if length $cmdline == 0;
510         
511         # split the command line up into parts, the first part is the command
512         my ($cmd, $args) = split /\s+/, $cmdline, 2;
513         $args = "" unless defined $args;
514                 
515         if ($cmd) {
516
517                 # check cmd
518                 if ($cmd =~ m|^/| || $cmd =~ m|[^-?\w/]|) {
519                         LogDbg('DXCommand', "cmd: invalid characters in '$cmd'");
520                         return $self->_error_out('e1');
521                 }
522
523                 # strip out // on command only
524                 $cmd =~ s|//|/|g;
525                                         
526                 my ($path, $fcmd);
527                         
528                 dbg("cmd: $cmd") if isdbg('command');
529                         
530                 # alias it if possible
531                 my $acmd = CmdAlias::get_cmd($cmd);
532                 if ($acmd) {
533                         ($cmd, $args) = split /\s+/, "$acmd $args", 2;
534                         $args = "" unless defined $args;
535                         dbg("cmd: aliased $cmd $args") if isdbg('command');
536                 }
537                         
538                 # first expand out the entry to a command
539                 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
540                 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
541
542                 if ($path && $cmd) {
543                         dbg("cmd: path $cmd cmd: $fcmd") if isdbg('command');
544                         
545                         my $package = find_cmd_name($path, $fcmd);
546                         return ($@) if $@;
547                                 
548                         if ($package && $self->can("${package}::handle")) {
549                                 no strict 'refs';
550                                 dbg("cmd: package $package") if isdbg('command');
551                                 my $t0 = [gettimeofday];
552                                 eval { @ans = &{"${package}::handle"}($self, $args) };
553                                 if ($@) {
554                                         DXDebug::dbgprintring(25);
555                                         return (DXDebug::shortmess($@));
556                                 }
557                                 if (isdbg('progress')) {
558                                         my $msecs = _diffms($t0);
559                                         my $s = "CMD: '$cmd $args' by $call ip: $self->{hostname} ${msecs}mS";
560                                         dbg($s) if $cmd !~ /^(?:echo|blank)/ || isdbg('echo');     # cut down a bit on HRD and other clients' noise
561                                 }
562                         } else {
563                                 dbg("cmd: $package not present") if isdbg('command');
564                                 return $self->_error_out('e1');
565                         }
566                 } else {
567                         dbg("cmd: $cmd not found") if isdbg('command');
568                         return $self->_error_out('e1');
569                 }
570         }
571         
572         my $ok = shift @ans;
573         if ($ok) {
574                 delete $self->{errors};
575         } else {
576                 if (++$self->{errors} > $DXChannel::maxerrors) {
577                         $self->send($self->msg('e26'));
578                         $self->disconnect;
579                         return ();
580                 }
581         }
582         return map {s/([^\s])\s+$/$1/; $_} @ans;
583 }
584
585 #
586 # This is called from inside the main cluster processing loop and is used
587 # for despatching commands that are doing some long processing job
588 #
589 sub process
590 {
591         my $t = time;
592         my @dxchan = DXChannel::get_all();
593         my $dxchan;
594
595         $users = 0;
596         foreach $dxchan (@dxchan) {
597                 next unless $dxchan->is_user;  
598         
599                 # send a outstanding message prompt if required
600                 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
601                         $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
602                         $dxchan->lastmsgpoll($t);
603                 }
604                 
605                 # send a prompt if no activity out on this channel
606                 if ($t >= $dxchan->t + $main::user_interval) {
607                         $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
608                         $dxchan->t($t);
609                 }
610                 ++$users;
611                 $maxusers = $users if $users > $maxusers;
612         }
613
614         while (my ($k, $v) = each %nothereslug) {
615                 if ($main::systime >= $v + 300) {
616                         delete $nothereslug{$k};
617                 }
618         }
619
620         import_cmd();
621 }
622
623 #
624 # finish up a user context
625 #
626 sub disconnect
627 {
628         my $self = shift;
629         my $call = $self->call;
630
631         return if $self->{disconnecting}++;
632
633         delete $self->{senddbg};
634
635         my $uref = Route::User::get($call);
636         my @rout;
637         if ($uref) {
638 #               @rout = $main::routeroot->del_user($uref);
639                 @rout = DXProt::_del_thingy($main::routeroot, [$call, 0]);
640
641                 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
642
643                 # issue a pc17 to everybody interested
644                 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
645                 $main::me->route_pc92d($main::mycall, undef, $main::routeroot, $uref) unless $DXProt::pc92_slug_changes;
646         } else {
647                 confess "trying to disconnect a non existant user $call";
648         }
649
650         # I was the last node visited
651     $self->user->node($main::mycall);
652                 
653         # send info to all logged in thingies
654         $self->tell_login('logoutu');
655         $self->tell_buddies('logoutb');
656
657         LogDbg('DXCommand', "$call disconnected");
658
659         $self->SUPER::disconnect;
660 }
661
662 #
663 # short cut to output a prompt
664 #
665
666 sub prompt
667 {
668         my $self = shift;
669
670         return if $self->{gtk};         # 'cos prompts are not a concept that applies here
671         
672         my $call = $self->call;
673         my $date = cldate($main::systime);
674         my $time = ztime($main::systime);
675         my $prompt = $self->{prompt} || $self->msg('pr');
676
677         $call = "($call)" unless $self->here;
678         $prompt =~ s/\%C/$call/g;
679         $prompt =~ s/\%D/$date/g;
680         $prompt =~ s/\%T/$time/g;
681         $prompt =~ s/\%M/$main::mycall/g;
682         
683         $self->send($prompt);
684 }
685
686 # broadcast a message to all users [except those mentioned after buffer]
687 sub broadcast
688 {
689         my $pkg = shift;                        # ignored
690         my $s = shift;                          # the line to be rebroadcast
691         
692     foreach my $dxchan (DXChannel::get_all()) {
693                 next unless $dxchan->is_user; # only interested in user channels  
694                 next if grep $dxchan == $_, @_;
695                 $dxchan->send($s);                      # send it
696         }
697 }
698
699 # gimme all the users
700 sub get_all
701 {
702         goto &DXChannel::get_all_users;
703 }
704
705 # run a script for this user
706 sub run_script
707 {
708         my $self = shift;
709         my $silent = shift || 0;
710         
711 }
712
713 #
714 # search for the command in the cache of short->long form commands
715 #
716
717 sub search
718 {
719         my ($path, $short_cmd, $suffix) = @_;
720         my ($apath, $acmd);
721         
722         # commands are lower case
723         $short_cmd = lc $short_cmd;
724         dbg("command: $path $short_cmd\n") if isdbg('command');
725
726         # do some checking for funny characters
727         return () if $short_cmd =~ /\/$/;
728
729         # return immediately if we have it
730         ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
731         if ($apath && $acmd) {
732                 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
733                 return ($apath, $acmd);
734         }
735         
736         # if not guess
737         my @parts = split '/', $short_cmd;
738         my $dirfn;
739         my $curdir = $path;
740         
741         while (my $p = shift @parts) {
742                 opendir(D, $curdir) or confess "can't open $curdir $!";
743                 my @ls = readdir D;
744                 closedir D;
745
746                 # if this isn't the last part
747                 if (@parts) {
748                         my $found;
749                         foreach my $l (sort @ls) {
750                                 next if $l =~ /^\./;
751                                 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
752                                         dbg("got dir: $curdir/$l\n") if isdbg('command');
753                                         $dirfn .= "$l/";
754                                         $curdir .= "/$l";
755                                         $found++;
756                                         last;
757                                 }
758                         }
759                         # only proceed if we find the directory asked for
760                         return () unless $found;
761                 } else {
762                         foreach my $l (sort @ls) {
763                                 next if $l =~ /^\./;
764                                 next unless $l =~ /\.$suffix$/;
765                                 if ($p eq substr($l, 0, length $p)) {
766                                         $l =~ s/\.$suffix$//;
767                                         $dirfn = "" unless $dirfn;
768                                         $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
769                                         dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
770                                         return ($path, "$dirfn$l");
771                                 }
772                         }
773                 }
774         }
775
776         return ();  
777 }  
778
779 # clear the command name cache
780 sub clear_cmd_cache
781 {
782         no strict 'refs';
783         
784         for my $k (keys %Cache) {
785                 unless ($k =~ /cmd_cache/) {
786                         dbg("Undefining cmd $k") if isdbg('command');
787                         undef $DXCommandmode::{"${k}::"};
788                 }
789         }
790         %cmd_cache = ();
791         %Cache = ( cmd_clear_cmd_cache  => $Cache{cmd_clear_cmd_cache} );
792 }
793
794 #
795 # the persistant execution of things from the command directories
796 #
797 #
798 # This allows perl programs to call functions dynamically
799
800 # This has been nicked directly from the perlembed pages
801 #
802 #require Devel::Symdump;  
803
804 sub valid_package_name {
805         my $string = shift;
806         $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
807         
808         $string =~ s|/|_|g;
809         return "cmd_$string";
810 }
811
812
813 # this bit of magic finds a command in the offered directory
814 sub find_cmd_name {
815         my $path = shift;
816         my $cmdname = shift;
817         my $package = valid_package_name($cmdname);
818         my $filename = "$path/$cmdname.pl";
819         my $mtime = -M $filename;
820         
821         # return if we can't find it
822         $errstr = undef;
823         unless (defined $mtime) {
824                 $errstr = DXM::msg('e1');
825                 return undef;
826         }
827         
828         if(exists $Cache{$package} && exists $Cache{$package}->{mtime} && $Cache{$package}->{mtime} <= $mtime) {
829                 #we have compiled this subroutine already,
830                 #it has not been updated on disk, nothing left to do
831                 #print STDERR "already compiled $package->handler\n";
832                 dbg("find_cmd_name: $package cached") if isdbg('command');
833         } else {
834
835                 my $sub = readfilestr($filename);
836                 unless ($sub) {
837                         $errstr = "Syserr: can't open '$filename' $!";
838                         return undef;
839                 };
840                 
841                 #wrap the code into a subroutine inside our unique package
842                 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}; );
843
844
845                 if ($sub =~ m|\s*sub\s+handle\n|) {
846                         $eval .= $sub;
847                 } else {
848                         $eval .= qq(sub handle { $sub });
849                 }
850                 
851                 if (isdbg('eval')) {
852                         my @list = split /\n/, $eval;
853                         my $line;
854                         for (@list) {
855                                 dbg($_ . "\n") if isdbg('eval');
856                         }
857                 }
858                 
859                 # get rid of any existing sub and try to compile the new one
860                 no strict 'refs';
861
862                 if (exists $Cache{$package}) {
863                         dbg("find_cmd_name: Redefining $package") if isdbg('command');
864                         undef $DXCommandmode::{"${package}::"};
865                         delete $Cache{$package};
866                 } else {
867                         dbg("find_cmd_name: Defining $package") if isdbg('command');
868                 }
869
870                 eval $eval;
871
872                 $Cache{$package} = {mtime => $mtime } unless $@;
873         }
874
875         return "DXCommandmode::$package";
876 }
877
878 sub send
879 {
880         my $self = shift;
881         if ($self->{gtk}) {
882                 for (@_) {
883                         $self->SUPER::send(dd(['cmd',$_]));
884                 }
885         } else {
886                 $self->SUPER::send(@_);
887         }
888 }
889
890 sub local_send
891 {
892         my ($self, $let, $buf) = @_;
893         if ($self->{state} eq 'prompt' || $self->{state} eq 'talk' || $self->{state} eq 'chat') {
894                 if ($self->{enhanced}) {
895                         $self->send_later($let, $buf);
896                 } else {
897                         $self->send($buf);
898                 }
899         } else {
900                 $self->delay($buf);
901         }
902 }
903
904 # send a talk message here
905 sub talk
906 {
907         my ($self, $from, $to, $via, $line, $onode) = @_;
908         $line =~ s/\\5E/\^/g;
909         if ($self->{talk}) {
910                 if ($self->{gtk}) {
911                         $self->local_send('T', dd(['talk',$to,$from,$via,$line]));
912                 } else {
913                         $self->local_send('T', "$to de $from: $line");
914                 }
915         }
916         Log('talk', $to, $from, '<' . ($onode || '*'), $line);
917         # send a 'not here' message if required
918         unless ($self->{here} && $from ne $to) {
919                 my $key = "$to$from";
920                 unless (exists $nothereslug{$key}) {
921                         my ($ref, $dxchan);
922                         if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
923                                 my $name = $self->user->name || $to;
924                                 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
925                                 $nothereslug{$key} = $main::systime;
926                                 $dxchan->talk($to, $from, undef, $s);
927                         }
928                 }
929         }
930 }
931
932 # send an announce
933 sub announce
934 {
935         my $self = shift;
936         my $line = shift;
937         my $isolate = shift;
938         my $to = shift;
939         my $target = shift;
940         my $text = shift;
941         my ($filter, $hops);
942
943         if (!$self->{ann_talk} && $to ne $self->{call}) {
944                 my $call = AnnTalk::is_talk_candidate($_[0], $text);
945                 return if $call;
946         }
947
948         if ($self->{annfilter}) {
949                 ($filter, $hops) = $self->{annfilter}->it(@_ );
950                 return unless $filter;
951         }
952
953         unless ($self->{ann}) {
954                 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
955         }
956         return if $target eq 'SYSOP' && $self->{priv} < 5;
957         my $buf;
958         if ($self->{gtk}) {
959                 $buf = dd(['ann', $to, $target, $text, @_])
960         } else {
961                 $buf = "$to$target de $_[0]: $text";
962                 $buf =~ s/\%5E/^/g;
963                 $buf .= "\a\a" if $self->{beep};
964         }
965         $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
966 }
967
968 # send a chat
969 sub chat
970 {
971         my $self = shift;
972         my $line = shift;
973         my $isolate = shift;
974         my $target = shift;
975         my $to = shift;
976         my $text = shift;
977         my ($filter, $hops);
978
979         return unless grep uc $_ eq $target, @{$self->{user}->{group}};
980         
981         $text =~ s/^\#\d+ //;
982         my $buf;
983         if ($self->{gtk}) {
984                 $buf = dd(['chat', $to, $target, $text, @_])
985         } else {
986                 $buf = "$target de $_[0]: $text";
987                 $buf =~ s/\%5E/^/g;
988                 $buf .= "\a\a" if $self->{beep};
989         }
990         $self->local_send('C', $buf);
991 }
992
993 sub format_dx_spot
994 {
995         my $self = shift;
996
997         my $t = ztime($_[2]);
998         my $loc = '';
999         my $clth = 30;
1000         --$clth if $self->{consort} eq 'local';
1001         
1002         my $comment = substr (($_[3] || ''), 0, $clth);
1003         $comment .= ' ' x ($clth - (length($comment)));
1004         
1005         if ($self->{user}->wantgrid) {
1006                 my $ref = DXUser::get_current($_[4]);
1007                 if ($ref && $ref->qra) {
1008                         $loc = ' ' . substr($ref->qra, 0, 4);
1009                 }
1010         }
1011
1012     if ($self->{user}->wantgrid) {
1013                 my $ref = DXUser::get_current($_[1]);
1014                 if ($ref && $ref->qra) {
1015                         $loc = ' ' . substr($ref->qra, 0, 4);
1016                         $comment = substr $comment, 0,  ($clth - (length($comment)+length($loc)));
1017                         $comment .= $loc;
1018                         $loc = '';
1019                 }
1020         } elsif ($self->{user}->wantdxitu) {
1021                 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
1022                 $comment = substr($comment, 0,  $clth-3) . ' ' . sprintf("%2d", $_[8]) if defined $_[8]; 
1023         } elsif ($self->{user}->wantdxcq) {
1024                 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
1025                 $comment = substr($comment, 0, $clth-3) . ' ' . sprintf("%2d", $_[9]) if defined $_[9]; 
1026         } elsif ($self->{user}->wantusstate) {
1027                 $loc = ' ' . $_[13] if $_[13];
1028                 $comment = substr($comment, 0,  $clth-3) . ' ' . $_[12] if $_[12]; 
1029         }
1030
1031         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
1032 }
1033
1034 # send a dx spot
1035 sub dx_spot
1036 {
1037         my $self = shift;
1038         my $line = shift;
1039         my $isolate = shift;
1040         return unless $self->{dx};
1041
1042         my ($filter, $hops);
1043
1044         if ($self->{spotsfilter}) {
1045                 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
1046                 return unless $filter;
1047         }
1048
1049         dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
1050
1051         my $buf;
1052         if ($self->{ve7cc}) {
1053                 $buf = VE7CC::dx_spot($self, @_);
1054         } elsif ($self->{gtk}) {
1055                 my ($dxloc, $byloc);
1056
1057                 my $ref = DXUser::get_current($_[4]);
1058                 if ($ref) {
1059                         $byloc = $ref->qra;
1060                         $byloc = substr($byloc, 0, 4) if $byloc;
1061                 }
1062
1063                 my $spot = $_[1];
1064                 $spot =~ s|/\w{1,4}$||;
1065                 $ref = DXUser::get_current($spot);
1066                 if ($ref) {
1067                         $dxloc = $ref->qra;
1068                         $dxloc = substr($dxloc, 0, 4) if $dxloc;
1069                 }
1070                 $buf = dd(['dx', @_, ($dxloc||''), ($byloc||'')]);
1071                 
1072         } else {
1073                 $buf = $self->format_dx_spot(@_);
1074                 $buf .= "\a\a" if $self->{beep};
1075                 $buf =~ s/\%5E/^/g;
1076         }
1077
1078         $self->local_send('X', $buf);
1079 }
1080
1081 sub wwv
1082 {
1083         my $self = shift;
1084         my $line = shift;
1085         my $isolate = shift;
1086         my ($filter, $hops);
1087
1088         return unless $self->{wwv};
1089         
1090         if ($self->{wwvfilter}) {
1091                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_] );
1092                 return unless $filter;
1093         }
1094
1095         my $buf;
1096         if ($self->{gtk}) {
1097                 $buf = dd(['wwv', @_])
1098         } else {
1099                 $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1100                 $buf .= "\a\a" if $self->{beep};
1101         }
1102         
1103         $self->local_send('V', $buf);
1104 }
1105
1106 sub wcy
1107 {
1108         my $self = shift;
1109         my $line = shift;
1110         my $isolate = shift;
1111         my ($filter, $hops);
1112
1113         return unless $self->{wcy};
1114         
1115         if ($self->{wcyfilter}) {
1116                 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
1117                 return unless $filter;
1118         }
1119
1120         my $buf;
1121         if ($self->{gtk}) {
1122                 $buf = dd(['wcy', @_])
1123         } else {
1124                 $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1125                 $buf .= "\a\a" if $self->{beep};
1126         }
1127         $self->local_send('Y', $buf);
1128 }
1129
1130 # broadcast debug stuff to all interested parties
1131 sub broadcast_debug
1132 {
1133         my $s = shift;                          # the line to be rebroadcast
1134         
1135         foreach my $dxchan (DXChannel::get_all_users) {
1136                 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
1137                 if ($dxchan->{gtk}) {
1138                         $dxchan->send_later('L', dd(['db', $s]));
1139                 } else {
1140                         $dxchan->send_later('L', $s);
1141                 }
1142         }
1143 }
1144
1145 sub do_entry_stuff
1146 {
1147         my $self = shift;
1148         my $line = shift;
1149         my @out;
1150         
1151         if ($self->state eq 'enterbody') {
1152                 my $loc = $self->{loc} || confess "local var gone missing" ;
1153                 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1154                         no strict 'refs';
1155                         push @out, &{$loc->{endaction}}($self);          # like this for < 5.8.0
1156                         $self->func(undef);
1157                         $self->state('prompt');
1158                 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1159                         push @out, $self->msg('m10');
1160                         delete $loc->{lines};
1161                         delete $self->{loc};
1162                         $self->func(undef);
1163                         $self->state('prompt');
1164                 } else {
1165                         push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1166                         # i.e. it ain't and end or abort, therefore store the line
1167                 }
1168         } else {
1169                 confess "Invalid state $self->{state}";
1170         }
1171         return @out;
1172 }
1173
1174 sub store_startup_script
1175 {
1176         my $self = shift;
1177         my $loc = $self->{loc} || confess "local var gone missing" ;
1178         my @out;
1179         my $call = $loc->{call} || confess "callsign gone missing";
1180         confess "lines array gone missing" unless ref $loc->{lines};
1181         my $r = Script::store($call, $loc->{lines});
1182         if (defined $r) {
1183                 if ($r) {
1184                         push @out, $self->msg('m19', $call, $r);
1185                 } else {
1186                         push @out, $self->msg('m20', $call);
1187                 }
1188         } else {
1189                 push @out, "error opening startup script $call $!";
1190         } 
1191         return @out;
1192 }
1193
1194 # Import any commands contained in any files in import_cmd directory
1195 #
1196 # If the filename has a recogisable callsign as some delimited part
1197 # of it, then this is the user the command will be run as. 
1198 #
1199 sub import_cmd
1200 {
1201         # are there any to do in this directory?
1202         return unless -d $cmdimportdir;
1203         unless (opendir(DIR, $cmdimportdir)) {
1204                 LogDbg('err', "can\'t open $cmdimportdir $!");
1205                 return;
1206         } 
1207
1208         my @names = readdir(DIR);
1209         closedir(DIR);
1210         my $name;
1211
1212         return unless @names;
1213         
1214         foreach $name (@names) {
1215                 next if $name =~ /^\./;
1216
1217                 my $s = Script->new($name, $cmdimportdir);
1218                 if ($s) {
1219                         LogDbg('DXCommand', "Run import cmd file $name");
1220                         my @cat = split /[^A-Za-z0-9]+/, $name;
1221                         my ($call) = grep {is_callsign(uc $_)} @cat;
1222                         $call ||= $main::mycall;
1223                         $call = uc $call;
1224                         my @out;
1225                         
1226                         
1227                         $s->inscript(0);        # switch off script checks
1228                         
1229                         if ($call eq $main::mycall) {
1230                                 @out = $s->run($main::me, 1);
1231                         } else {
1232                                 my $dxchan = DXChannel::get($call);
1233                             if ($dxchan) {
1234                                         @out = $s->run($dxchan, 1);
1235                                 } else {
1236                                         my $u = DXUser::get($call);
1237                                         if ($u) {
1238                                                 $dxchan = $main::me;
1239                                                 my $old = $dxchan->{call};
1240                                                 my $priv = $dxchan->{priv};
1241                                                 my $user = $dxchan->{user};
1242                                                 $dxchan->{call} = $call;
1243                                                 $dxchan->{priv} = $u->priv;
1244                                                 $dxchan->{user} = $u;
1245                                                 @out = $s->run($dxchan, 1);
1246                                                 $dxchan->{call} = $old;
1247                                                 $dxchan->{priv} = $priv;
1248                                                 $dxchan->{user} = $user;
1249                                         } else {
1250                                                 LogDbg('err', "Trying to run import cmd for non-existant user $call");
1251                                         }
1252                                 }
1253                         }
1254                         $s->erase;
1255                         for (@out) {
1256                                 LogDbg('DXCommand', "Import cmd $name/$call: $_");
1257                         }
1258                 } else {
1259                         LogDbg('err', "Failed to open $cmdimportdir/$name $!");
1260                         unlink "$cmdimportdir/$name";
1261                 }
1262         }
1263 }
1264
1265 sub print_find_reply
1266 {
1267         my ($self, $node, $target, $flag, $ms) = @_;
1268         my $sort = $flag == 2 ? "External" : "Local";
1269         $self->send("$sort $target found at $node in $ms ms" );
1270 }
1271
1272 # send the most relevant motd
1273 sub send_motd
1274 {
1275         my $self = shift;
1276         my $motd;
1277
1278         unless ($self->{registered}) {
1279                 $motd = "${main::motd}_nor_$self->{lang}";
1280                 $motd = "${main::motd}_nor" unless -e $motd;
1281         }
1282         $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
1283         $motd = $main::motd unless $motd && -e $motd;
1284         if ($self->conn->ax25) {
1285                 if ($motd) {
1286                         $motd = "${motd}_ax25" if -e "${motd}_ax25";
1287                 } else {
1288                         $motd = "${main::motd}_ax25" if -e "${main::motd}_ax25";
1289                 }
1290         }
1291         $self->send_file($motd) if -e $motd;
1292 }
1293
1294
1295 # Punt off a long running command into a separate process
1296 #
1297 # This is called from commands to run some potentially long running
1298 # function. The process forks and then runs the function and returns
1299 # the result back to the cmd. 
1300 #
1301 # NOTE: this merely forks the current process and then runs the cmd in that (current) context.
1302 #       IT DOES NOT START UP SOME NEW PROGRAM AND RELIES ON THE FACT THAT IT IS RUNNING DXSPIDER 
1303 #       THE CURRENT CONTEXT!!
1304
1305 # call: $self->spawn_cmd($original_cmd_line, \<function>, [cb => sub{...}], [prefix => "cmd> "], [progress => 0|1], [args => [...]]);
1306 sub spawn_cmd
1307 {
1308         my $self = shift;
1309         my $line = shift;
1310         my $cmdref = shift;
1311         my $call = $self->{call};
1312         my %args = @_;
1313         my @out;
1314         
1315         my $cb = delete $args{cb};
1316         my $prefix = delete $args{prefix};
1317         my $progress = delete $args{progress};
1318         my $args = delete $args{args} || [];
1319         my $t0 = [gettimeofday];
1320
1321         no strict 'refs';
1322
1323         # just behave normally if something has set the "one-shot" _nospawn in the channel
1324         if ($self->{_nospawn}) {
1325                 eval { @out = $cmdref->(@$args); };
1326                 if ($@) {
1327                         DXDebug::dbgprintring(25);
1328                         push @out, DXDebug::shortmess($@);
1329                 }
1330                 return @out;
1331         }
1332         
1333         my $fc = DXSubprocess->new;
1334 #       $fc->serializer(\&encode_json);
1335 #       $fc->deserializer(\&decode_json);
1336         $fc->run(
1337                          sub {
1338                                  my $subpro = shift;
1339                                  if (isdbg('progress')) {
1340                                          my $s = qq{line: "$line"};
1341                                          $s .= ", args: " . join(', ', map { defined $_ ? qq{'$_'} : q{'undef'} } @$args) if $args && @$args;
1342                                          dbg($s);
1343                                  }
1344                                  eval { @out = $cmdref->(@$args); };
1345                                  if ($@) {
1346                                          DXDebug::dbgprintring(25);
1347                                          push @out, DXDebug::shortmess($@);
1348                                  }
1349                                  return @out;
1350                          },
1351 #                        $args,
1352                          sub {
1353                                  my ($fc, $err, @res) = @_; 
1354                                  my $dxchan = DXChannel::get($call);
1355                                  return unless $dxchan;
1356
1357                                  if ($err) {
1358                                          my $s = "DXProt::spawn_cmd: call $call error $err";
1359                                          dbg($s) if isdbg('chan');
1360                                          $dxchan->send($s);
1361                                          return;
1362                                  }
1363                                  if ($cb) {
1364                                          # transform output if required
1365                                          @res = $cb->($dxchan, @res);
1366                                  }
1367                                  if (@res) {
1368                                          if (defined $prefix) {
1369                                                  $dxchan->send(map {"$prefix$_"} @res);
1370                                          } else {
1371                                                  $dxchan->send(@res);
1372                                          }
1373                                  }
1374                                  diffms("by $call", $line, $t0, scalar @res) if isdbg('progress');
1375                          });
1376         
1377         return @out;
1378 }
1379
1380 sub user_count
1381 {
1382         return ($users, $maxusers);
1383 }
1384 1;
1385 __END__