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