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