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