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