add more code gradually
[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 # $Id$
8
9
10 package DXCommandmode;
11
12 use POSIX;
13
14 @ISA = qw(DXChannel);
15
16 use DXUtil;
17 use DXChannel;
18 use DXUser;
19 use DXVars;
20 use DXDebug;
21 use DXM;
22 use DXLog;
23 use DXLogPrint;
24 use DXBearing;
25 use CmdAlias;
26 use Filter;
27 use Minimuf;
28 use DXDb;
29 use AnnTalk;
30 use WCY;
31 use Sun;
32 use Internet;
33 use Script;
34 use Net::Telnet;
35 use QSL;
36 use DB_File;
37 use Thingy;
38
39 use strict;
40 use vars qw(%Cache %cmd_cache $errstr %aliases $scriptbase $maxerrors %nothereslug $maxbadcount);
41
42 %Cache = ();                                    # cache of dynamically loaded routine's mod times
43 %cmd_cache = ();                                # cache of short names
44 $errstr = ();                                   # error string from eval
45 %aliases = ();                                  # aliases for (parts of) commands
46 $scriptbase = "$main::root/scripts"; # the place where all users start scripts go
47 $maxerrors = 20;                                # the maximum number of concurrent errors allowed before disconnection
48 $maxbadcount = 3;                               # no of bad words allowed before disconnection
49
50
51 use vars qw($VERSION $BRANCH);
52 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
53 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/,(0,0));
54 $main::build += $VERSION;
55 $main::branch += $BRANCH;
56
57 #
58 # obtain a new connection this is derived from dxchannel
59 #
60
61 sub new 
62 {
63         my $self = DXChannel::alloc(@_);
64
65         # routing, this must go out here to prevent race condx
66         my $pkg = shift;
67         my $call = shift;
68         my @rout = $main::routeroot->add_user($call, Route::here(1));
69
70         # ALWAYS output the user
71         my $ref = Route::User::get($call);
72         $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref) if $ref;
73
74         return $self;
75 }
76
77 # this is how a a connection starts, you get a hello message and the motd with
78 # possibly some other messages asking you to set various things up if you are
79 # new (or nearly new and slacking) user.
80
81 sub start
82
83         my ($self, $line, $sort) = @_;
84         my $user = $self->{user};
85         my $call = $self->{call};
86         my $name = $user->{name};
87         
88         # log it
89         my $host = $self->{conn}->{peerhost} || "unknown";
90         Log('DXCommand', "$call connected from $host");
91
92         $self->{name} = $name ? $name : $call;
93         $self->send($self->msg('l2',$self->{name}));
94         $self->state('prompt');         # a bit of room for further expansion, passwords etc
95         $self->{priv} = $user->priv || 0;
96         $self->{lang} = $user->lang || $main::lang || 'en';
97         $self->{pagelth} = $user->pagelth || 20;
98         ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
99         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
100         $self->{consort} = $line;       # save the connection type
101         
102         # set some necessary flags on the user if they are connecting
103         $self->{beep} = $user->wantbeep;
104         $self->{ann} = $user->wantann;
105         $self->{wwv} = $user->wantwwv;
106         $self->{wcy} = $user->wantwcy;
107         $self->{talk} = $user->wanttalk;
108         $self->{wx} = $user->wantwx;
109         $self->{dx} = $user->wantdx;
110         $self->{logininfo} = $user->wantlogininfo;
111         $self->{ann_talk} = $user->wantann_talk;
112         $self->{here} = 1;
113         $self->{prompt} = $user->prompt if $user->prompt;
114
115         # sort out new dx spot stuff
116         $user->wantdxcq(0) unless defined $user->{wantdxcq};
117         $user->wantdxitu(0) unless defined $user->{wantdxitu};  
118         $user->wantusstate(0) unless defined $user->{wantusstate};
119
120         # sort out registration
121         if ($main::reqreg == 1) {
122                 $self->{registered} = $user->registered;
123         } elsif ($main::reqreg == 2) {
124                 $self->{registered} = !$user->registered;
125         } else {
126                 $self->{registered} = 1;
127         }
128
129
130         # decide which motd to send
131         my $motd = "${main::motd}_nor" unless $self->{registered};
132         $motd = $main::motd unless $motd && -e $motd;
133         $self->send_file($motd) if -e $motd;
134
135         # sort out privilege reduction
136         $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
137
138         # get the filters
139         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
140         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
141         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
142         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
143
144         # clean up qra locators
145         my $qra = $user->qra;
146         $qra = undef if ($qra && !DXBearing::is_qra($qra));
147         unless ($qra) {
148                 my $lat = $user->lat;
149                 my $long = $user->long;
150                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
151         }
152
153         # decide on echo
154         my $echo = $user->wantecho;
155         unless ($echo) {
156                 $self->send_now('E', "0");
157                 $self->send($self->msg('echow'));
158                 $self->conn->echo($echo) if $self->conn->can('echo');
159         }
160         
161         $self->tell_login('loginu');
162         
163         # do we need to send a forward/opernam?
164         my $lastoper = $user->lastoper || 0;
165         my $homenode = $user->homenode || ""; 
166         if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
167                 run_cmd($main::me, "forward/opernam $call");
168                 $user->lastoper($main::systime + ((int rand(10)) * 86400));
169         }
170
171         # run a script send the output to the punter
172         my $script = new Script(lc $call) || new Script('user_default');
173         $script->run($self) if $script;
174
175         # send cluster info
176         my $info = Route::cluster();
177         $self->send("Cluster:$info");
178
179         # send prompts and things
180         $self->send($self->msg('namee1')) if !$user->name;
181         $self->send($self->msg('qthe1')) if !$user->qth;
182         $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
183         $self->send($self->msg('hnodee1')) if !$user->qth;
184         $self->send($self->msg('m9')) if DXMsg::for_me($call);
185         $self->prompt;
186 }
187
188 #
189 # This is the normal command prompt driver
190 #
191
192 sub normal
193 {
194         my $self = shift;
195         my $cmdline = shift;
196         my @ans;
197
198         # save this for them's that need it
199         my $rawline = $cmdline;
200         
201         # remove leading and trailing spaces
202         $cmdline =~ s/^\s*(.*)\s*$/$1/;
203         
204         if ($self->{state} eq 'page') {
205                 my $i = $self->{pagelth};
206                 my $ref = $self->{pagedata};
207                 my $tot = @$ref;
208                 
209                 # abort if we get a line starting in with a
210                 if ($cmdline =~ /^a/io) {
211                         undef $ref;
212                         $i = 0;
213                 }
214         
215                 # send a tranche of data
216                 while ($i-- > 0 && @$ref) {
217                         my $line = shift @$ref;
218                         $line =~ s/\s+$//o;     # why am having to do this? 
219                         $self->send($line);
220                 }
221                 
222                 # reset state if none or else chuck out an intermediate prompt
223                 if ($ref && @$ref) {
224                         $tot -= $self->{pagelth};
225                         $self->send($self->msg('page', $tot));
226                 } else {
227                         $self->state('prompt');
228                 }
229         } elsif ($self->{state} eq 'sysop') {
230                 my $passwd = $self->{user}->passwd;
231                 if ($passwd) {
232                         my @pw = grep {$_ !~ /\s/} split //, $passwd;
233                         my @l = @{$self->{passwd}};
234                         my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
235                         if ($cmdline =~ /$str/) {
236                                 $self->{priv} = $self->{user}->priv;
237                         } else {
238                                 $self->send($self->msg('sorry'));
239                         }
240                 } else {
241                         $self->send($self->msg('sorry'));
242                 }
243                 $self->state('prompt');
244         } elsif ($self->{state} eq 'passwd') {
245                 my $passwd = $self->{user}->passwd;
246                 if ($passwd && $cmdline eq $passwd) {
247                         $self->send($self->msg('pw1'));
248                         $self->state('passwd1');
249                 } else {
250                         $self->conn->{echo} = $self->conn->{decho};
251                         delete $self->conn->{decho};
252                         $self->send($self->msg('sorry'));
253                         $self->state('prompt');
254                 }
255         } elsif ($self->{state} eq 'passwd1') {
256                 $self->{passwd} = $cmdline;
257                 $self->send($self->msg('pw2'));
258                 $self->state('passwd2');
259         } elsif ($self->{state} eq 'passwd2') {
260                 if ($cmdline eq $self->{passwd}) {
261                         $self->{user}->passwd($cmdline);
262                         $self->send($self->msg('pw3'));
263                 } else {
264                         $self->send($self->msg('pw4'));
265                 }
266                 $self->conn->{echo} = $self->conn->{decho};
267                 delete $self->conn->{decho};
268                 $self->state('prompt');
269         } elsif ($self->{state} eq 'talk') {
270                 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
271                         for (@{$self->{talklist}}) {
272                                 $self->send_talks($_,  $self->msg('talkend'));
273                         }
274                         $self->state('prompt');
275                         delete $self->{talklist};
276                 } elsif ($cmdline =~ m|^/+\w+|) {
277                         $cmdline =~ s|^/||;
278                         my $sendit = $cmdline =~ s|^/+||;
279                         my @in = $self->run_cmd($cmdline);
280                         $self->send_ans(@in);
281                         if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
282                                 foreach my $l (@in) {
283                                         my @bad;
284                                         if (@bad = BadWords::check($l)) {
285                                                 $self->badcount(($self->badcount||0) + @bad);
286                                                 Log('DXCommand', "$self->{call} swore: $l");
287                                         } else {
288                                                 for (@{$self->{talklist}}) {
289                                                         $self->send_talks($_, $l);
290                                                 }
291                                         }
292                                 }
293                         }
294                         $self->send($self->talk_prompt);
295                 } elsif ($self->{talklist} && @{$self->{talklist}}) {
296                         # send what has been said to whoever is in this person's talk list
297                         my @bad;
298                         if (@bad = BadWords::check($cmdline)) {
299                                 $self->badcount(($self->badcount||0) + @bad);
300                                 Log('DXCommand', "$self->{call} swore: $cmdline");
301                         } else {
302                                 for (@{$self->{talklist}}) {
303                                         $self->send_talks($_, $rawline);
304                                 }
305                         }
306                         $self->send($self->talk_prompt) if $self->{state} eq 'talk';
307                 } else {
308                         # for safety
309                         $self->state('prompt');
310                 }
311         } elsif (my $func = $self->{func}) {
312                 no strict 'refs';
313                 my @ans;
314                 if (ref $self->{edit}) {
315                         eval { @ans = $self->{edit}->$func($self, $rawline)};
316                 } else {
317                         eval {  @ans = &{$self->{func}}($self, $rawline) };
318                 }
319                 if ($@) {
320                         $self->send_ans("Syserr: on stored func $self->{func}", $@);
321                         delete $self->{func};
322                         $self->state('prompt');
323                         undef $@;
324                 }
325                 $self->send_ans(@ans);
326         } else {
327                 $self->send_ans(run_cmd($self, $cmdline));
328         } 
329
330         # check for excessive swearing
331         if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
332                 Log('DXCommand', "$self->{call} logged out for excessive swearing");
333                 $self->disconnect;
334                 return;
335         }
336
337         # send a prompt only if we are in a prompt state
338         $self->prompt() if $self->{state} =~ /^prompt/o;
339 }
340
341 # send out the talk messages taking into account vias and connectivity
342 sub send_talks
343 {
344         my ($self, $ent, $line) = @_;
345         
346         my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
347         $to = $ent unless $to;
348         my $call = $via ? $via : $to;
349         my $clref = Route::get($call);
350         my $dxchan = $clref->dxchan if $clref;
351         if ($dxchan) {
352                 $dxchan->talk($self->{call}, $to, $via, $line);
353         } else {
354                 $self->send($self->msg('disc2', $via ? $via : $to));
355                 my @l = grep { $_ ne $ent } @{$self->{talklist}};
356                 if (@l) {
357                         $self->{talklist} = \@l;
358                 } else {
359                         delete $self->{talklist};
360                         $self->state('prompt');
361                 }
362         }
363 }
364
365 sub talk_prompt
366 {
367         my $self = shift;
368         my @call;
369         for (@{$self->{talklist}}) {
370                 my ($to, $via) = /(\S+)>(\S+)/;
371                 $to = $_ unless $to;
372                 push @call, $to;
373         }
374         return $self->msg('talkprompt', join(',', @call));
375 }
376
377 #
378 # send a load of stuff to a command user with page prompting
379 # and stuff
380 #
381
382 sub send_ans
383 {
384         my $self = shift;
385         
386         if ($self->{pagelth} && @_ > $self->{pagelth}) {
387                 my $i;
388                 for ($i = $self->{pagelth}; $i-- > 0; ) {
389                         my $line = shift @_;
390                         $line =~ s/\s+$//o;     # why am having to do this? 
391                         $self->send($line);
392                 }
393                 $self->{pagedata} =  [ @_ ];
394                 $self->state('page');
395                 $self->send($self->msg('page', scalar @_));
396         } else {
397                 for (@_) {
398                         if (defined $_) {
399                                 $self->send($_);
400                         } else {
401                                 $self->send('');
402                         }
403                 }
404         } 
405 }
406
407 # this is the thing that runs the command, it is done like this for the 
408 # benefit of remote command execution
409 #
410
411 sub run_cmd
412 {
413         my $self = shift;
414         my $user = $self->{user};
415         my $call = $self->{call};
416         my $cmdline = shift;
417         my @ans;
418         
419
420         return () if length $cmdline == 0;
421                 
422         # strip out //
423         $cmdline =~ s|//|/|og;
424                 
425         # split the command line up into parts, the first part is the command
426         my ($cmd, $args) = split /\s+/, $cmdline, 2;
427         $args = "" unless defined $args;
428                 
429         if ($cmd) {
430                         
431                 my ($path, $fcmd);
432                         
433                 dbg("cmd: $cmd") if isdbg('command');
434                         
435                 # alias it if possible
436                 my $acmd = CmdAlias::get_cmd($cmd);
437                 if ($acmd) {
438                         ($cmd, $args) = split /\s+/, "$acmd $args", 2;
439                         $args = "" unless defined $args;
440                         dbg("aliased cmd: $cmd $args") if isdbg('command');
441                 }
442                         
443                 # first expand out the entry to a command
444                 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
445                 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
446
447                 if ($path && $cmd) {
448                         dbg("path: $cmd cmd: $fcmd") if isdbg('command');
449                         
450                         my $package = find_cmd_name($path, $fcmd);
451                         return ($@) if $@;
452                                 
453                         if ($package) {
454                                 no strict 'refs';
455                                 dbg("package: $package") if isdbg('command');
456                                 eval { @ans = &$package($self, $args) };
457                                 return (DXDebug::shortmess($@)) if $@;
458                         }
459                 } else {
460                         dbg("cmd: $cmd not found") if isdbg('command');
461                         if (++$self->{errors} > $maxerrors) {
462                                 $self->send($self->msg('e26'));
463                                 $self->disconnect;
464                                 return ();
465                         } else {
466                                 return ($self->msg('e1'));
467                         }
468                 }
469         }
470         
471         my $ok = shift @ans;
472         if ($ok) {
473                 delete $self->{errors};
474         } else {
475                 if (++$self->{errors} > $maxerrors) {
476                         $self->send($self->msg('e26'));
477                         $self->disconnect;
478                         return ();
479                 }
480         }
481         return map {s/([^\s])\s+$/$1/; $_} @ans;
482 }
483
484 #
485 # This is called from inside the main cluster processing loop and is used
486 # for despatching commands that are doing some long processing job
487 #
488 sub process
489 {
490         my $t = time;
491         my @dxchan = DXChannel->get_all();
492         my $dxchan;
493         
494         foreach $dxchan (@dxchan) {
495                 next if $dxchan->sort ne 'U';  
496                 
497                 # send a prompt if no activity out on this channel
498                 if ($t >= $dxchan->t + $main::user_interval) {
499                         $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
500                         $dxchan->t($t);
501                 }
502         }
503
504         while (my ($k, $v) = each %nothereslug) {
505                 if ($main::systime >= $v + 300) {
506                         delete $nothereslug{$k};
507                 }
508         }
509 }
510
511 #
512 # finish up a user context
513 #
514 sub disconnect
515 {
516         my $self = shift;
517         my $call = $self->call;
518
519         return if $self->{disconnecting}++;
520
521         delete $self->{senddbg};
522
523         my $uref = Route::User::get($call);
524         my @rout;
525         if ($uref) {
526                 @rout = $main::routeroot->del_user($uref);
527                 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
528
529                 # issue a pc17 to everybody interested
530                 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
531         } else {
532                 confess "trying to disconnect a non existant user $call";
533         }
534
535         # I was the last node visited
536     $self->user->node($main::mycall);
537                 
538         # send info to all logged in thingies
539         $self->tell_login('logoutu');
540
541         Log('DXCommand', "$call disconnected");
542
543         $self->SUPER::disconnect;
544 }
545
546 #
547 # short cut to output a prompt
548 #
549
550 sub prompt
551 {
552         my $self = shift;
553         if ($self->{prompt}) {
554                 $self->send($self->{prompt});
555         } else {
556                 $self->send($self->msg($self->here ? 'pr' : 'pr2', $self->call, cldate($main::systime), ztime($main::systime)));
557         }
558 }
559
560 # broadcast a message to all users [except those mentioned after buffer]
561 sub broadcast
562 {
563         my $pkg = shift;                        # ignored
564         my $s = shift;                          # the line to be rebroadcast
565         
566     foreach my $dxchan (DXChannel->get_all()) {
567                 next unless $dxchan->{sort} eq 'U'; # only interested in user channels  
568                 next if grep $dxchan == $_, @_;
569                 $dxchan->send($s);                      # send it
570         }
571 }
572
573 # gimme all the users
574 sub get_all
575 {
576         return grep {$_->{sort} eq 'U'} DXChannel->get_all();
577 }
578
579 # run a script for this user
580 sub run_script
581 {
582         my $self = shift;
583         my $silent = shift || 0;
584         
585 }
586
587 #
588 # search for the command in the cache of short->long form commands
589 #
590
591 sub search
592 {
593         my ($path, $short_cmd, $suffix) = @_;
594         my ($apath, $acmd);
595         
596         # commands are lower case
597         $short_cmd = lc $short_cmd;
598         dbg("command: $path $short_cmd\n") if isdbg('command');
599
600         # do some checking for funny characters
601         return () if $short_cmd =~ /\/$/;
602
603         # return immediately if we have it
604         ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
605         if ($apath && $acmd) {
606                 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
607                 return ($apath, $acmd);
608         }
609         
610         # if not guess
611         my @parts = split '/', $short_cmd;
612         my $dirfn;
613         my $curdir = $path;
614         my $p;
615         my $i;
616         my @lparts;
617         
618         for ($i = 0; $i < @parts; $i++) {
619                 my  $p = $parts[$i];
620                 opendir(D, $curdir) or confess "can't open $curdir $!";
621                 my @ls = readdir D;
622                 closedir D;
623                 my $l;
624                 foreach $l (sort @ls) {
625                         next if $l =~ /^\./;
626                         if ($i < $#parts) {             # we are dealing with directories
627                                 if ((-d "$curdir/$l") && $p eq substr($l, 0, length $p)) {
628                                         dbg("got dir: $curdir/$l\n") if isdbg('command');
629                                         $dirfn .= "$l/";
630                                         $curdir .= "/$l";
631                                         last;
632                                 }
633                         } else {                        # we are dealing with commands
634                                 @lparts = split /\./, $l;                  
635                                 next if $lparts[$#lparts] ne $suffix;        # only look for .$suffix files
636                                 if ($p eq substr($l, 0, length $p)) {
637                                         pop @lparts; #  remove the suffix
638                                         $l = join '.', @lparts;
639                                         #                 chop $dirfn;               # remove trailing /
640                                         $dirfn = "" unless $dirfn;
641                                         $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
642                                         dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
643                                         return ($path, "$dirfn$l"); 
644                                 }
645                         }
646                 }
647         }
648         return ();  
649 }  
650
651 # clear the command name cache
652 sub clear_cmd_cache
653 {
654         no strict 'refs';
655         
656         for (keys %Cache) {
657                 undef *{$_} unless /cmd_cache/;
658                 dbg("Undefining cmd $_") if isdbg('command');
659         }
660         %cmd_cache = ();
661         %Cache = ();
662 }
663
664 #
665 # the persistant execution of things from the command directories
666 #
667 #
668 # This allows perl programs to call functions dynamically
669
670 # This has been nicked directly from the perlembed pages
671 #
672
673 #require Devel::Symdump;  
674
675 sub valid_package_name {
676         my($string) = @_;
677         $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
678         
679         $string =~ s|/|_|g;
680         return "cmd_$string";
681 }
682
683
684 # this bit of magic finds a command in the offered directory
685 sub find_cmd_name {
686         my $path = shift;
687         my $cmdname = shift;
688         my $package = valid_package_name($cmdname);
689         my $filename = "$path/$cmdname.pl";
690         my $mtime = -M $filename;
691         
692         # return if we can't find it
693         $errstr = undef;
694         unless (defined $mtime) {
695                 $errstr = DXM::msg('e1');
696                 return undef;
697         }
698         
699         if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
700                 #we have compiled this subroutine already,
701                 #it has not been updated on disk, nothing left to do
702                 #print STDERR "already compiled $package->handler\n";
703                 ;
704         } else {
705
706                 my $sub = readfilestr($filename);
707                 unless ($sub) {
708                         $errstr = "Syserr: can't open '$filename' $!";
709                         return undef;
710                 };
711                 
712                 #wrap the code into a subroutine inside our unique package
713                 my $eval = qq( sub $package { $sub } );
714                 
715                 if (isdbg('eval')) {
716                         my @list = split /\n/, $eval;
717                         my $line;
718                         for (@list) {
719                                 dbg($_ . "\n") if isdbg('eval');
720                         }
721                 }
722                 
723                 # get rid of any existing sub and try to compile the new one
724                 no strict 'refs';
725
726                 if (exists $Cache{$package}) {
727                         dbg("Redefining $package") if isdbg('command');
728                         undef *$package;
729                 } else {
730                         dbg("Defining $package") if isdbg('command');
731                 }
732                 eval $eval;
733                 
734                 $Cache{$package} = {mtime => $mtime };
735             
736         }
737
738         return $package;
739 }
740
741 sub local_send
742 {
743         my ($self, $let, $buf) = @_;
744         if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
745                 if ($self->{enhanced}) {
746                         $self->send_later($let, $buf);
747                 } else {
748                         $self->send($buf);
749                 }
750         } else {
751                 $self->delay($buf);
752         }
753 }
754
755 # send a talk message here
756 sub talk
757 {
758         my ($self, $from, $to, $via, $line) = @_;
759         $line =~ s/\\5E/\^/g;
760         $self->local_send('T', "$to de $from: $line") if $self->{talk};
761         Log('talk', $to, $from, $via?$via:$main::mycall, $line);
762         # send a 'not here' message if required
763         unless ($self->{here} && $from ne $to) {
764                 my $key = "$to$from";
765                 unless (exists $nothereslug{$key}) {
766                         my ($ref, $dxchan);
767                         if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
768                                 my $name = $self->user->name || $to;
769                                 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
770                                 $nothereslug{$key} = $main::systime;
771                                 $dxchan->talk($to, $from, undef, $s);
772                         }
773                 }
774         }
775 }
776
777 # send an announce
778 sub announce
779 {
780         my $self = shift;
781         my $line = shift;
782         my $isolate = shift;
783         my $to = shift;
784         my $target = shift;
785         my $text = shift;
786         my ($filter, $hops);
787
788         if (!$self->{ann_talk} && $to ne $self->{call}) {
789                 my $call = AnnTalk::is_talk_candidate($_[0], $text);
790                 return if $call;
791         }
792
793         if ($self->{annfilter}) {
794                 ($filter, $hops) = $self->{annfilter}->it(@_ );
795                 return unless $filter;
796         }
797
798         unless ($self->{ann}) {
799                 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
800         }
801         return if $target eq 'SYSOP' && $self->{priv} < 5;
802         my $buf = "$to$target de $_[0]: $text";
803         $buf =~ s/\%5E/^/g;
804         $buf .= "\a\a" if $self->{beep};
805         $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
806 }
807
808 # send a chat
809 sub chat
810 {
811         my $self = shift;
812         my $line = shift;
813         my $isolate = shift;
814         my $target = shift;
815         my $to = shift;
816         my $text = shift;
817         my ($filter, $hops);
818
819         return unless grep uc $_ eq $target, @{$self->{user}->{group}};
820         
821         $text =~ s/^\#\d+ //;
822         my $buf = "$target de $_[0]: $text";
823         $buf =~ s/\%5E/^/g;
824         $buf .= "\a\a" if $self->{beep};
825         $self->local_send('C', $buf);
826 }
827
828 # send a dx spot
829 sub dx_spot
830 {
831         my $self = shift;
832         my $line = shift;
833         my $isolate = shift;
834         my ($filter, $hops);
835
836         return unless $self->{dx};
837         
838         if ($self->{spotsfilter}) {
839                 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
840                 return unless $filter;
841         }
842
843
844         dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
845         
846         my $t = ztime($_[2]);
847         my $loc;
848         my $clth = $self->{consort} eq 'local' ? 29 : 30;
849         my $comment = substr $_[3], 0, $clth; 
850         $comment .= ' ' x ($clth - length($comment));
851         my $ref = DXUser->get_current($_[4]);
852         if ($ref) {
853                 $loc = $ref->qra || '' if $self->{user}->wantgrid; 
854                 $loc = ' ' . substr($loc, 0, 4) if $loc;
855         } 
856         $loc = "" unless $loc;
857
858         if ($self->{user}->wantdxitu) {
859                 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
860                 $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8]; 
861         } elsif ($self->{user}->wantdxcq) {
862                 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
863                 $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9]; 
864         } elsif ($self->{user}->wantusstate) {
865                 $loc = ' ' . $_[13] if $_[13];
866                 $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12]; 
867         }
868
869         my $buf = sprintf "DX de %-7.7s%11.1f  %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
870         
871         $buf .= "\a\a" if $self->{beep};
872         $buf =~ s/\%5E/^/g;
873         $self->local_send('X', $buf);
874 }
875
876 sub wwv
877 {
878         my $self = shift;
879         my $line = shift;
880         my $isolate = shift;
881         my ($filter, $hops);
882
883         return unless $self->{wwv};
884         
885         if ($self->{wwvfilter}) {
886                 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
887                 return unless $filter;
888         }
889
890         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
891         $buf .= "\a\a" if $self->{beep};
892         $self->local_send('V', $buf);
893 }
894
895 sub wcy
896 {
897         my $self = shift;
898         my $line = shift;
899         my $isolate = shift;
900         my ($filter, $hops);
901
902         return unless $self->{wcy};
903         
904         if ($self->{wcyfilter}) {
905                 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
906                 return unless $filter;
907         }
908
909         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
910         $buf .= "\a\a" if $self->{beep};
911         $self->local_send('Y', $buf);
912 }
913
914 # broadcast debug stuff to all interested parties
915 sub broadcast_debug
916 {
917         my $s = shift;                          # the line to be rebroadcast
918         
919         foreach my $dxchan (DXChannel->get_all) {
920                 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
921                 $dxchan->send_later('L', $s);
922         }
923 }
924
925
926 1;
927 __END__