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