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