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