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