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