log agw port on connection
[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                                         
439                 my ($path, $fcmd);
440                         
441                 dbg("cmd: $cmd") if isdbg('command');
442                         
443                 # alias it if possible
444                 my $acmd = CmdAlias::get_cmd($cmd);
445                 if ($acmd) {
446                         ($cmd, $args) = split /\s+/, "$acmd $args", 2;
447                         $args = "" unless defined $args;
448                         dbg("aliased cmd: $cmd $args") if isdbg('command');
449                 }
450                         
451                 # first expand out the entry to a command
452                 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
453                 ($path, $fcmd) = search($main::cmd, $cmd, "pl") if !$path || !$fcmd;
454
455                 if ($path && $cmd) {
456                         dbg("path: $cmd cmd: $fcmd") if isdbg('command');
457                         
458                         my $package = find_cmd_name($path, $fcmd);
459                         return ($@) if $@;
460                                 
461                         if ($package) {
462                                 no strict 'refs';
463                                 dbg("package: $package") if isdbg('command');
464                                 eval { @ans = &$package($self, $args) };
465                                 return (DXDebug::shortmess($@)) if $@;
466                         }
467                 } else {
468                         dbg("cmd: $cmd not found") if isdbg('command');
469                         if (++$self->{errors} > $maxerrors) {
470                                 $self->send($self->msg('e26'));
471                                 $self->disconnect;
472                                 return ();
473                         } else {
474                                 return ($self->msg('e1'));
475                         }
476                 }
477         }
478         
479         my $ok = shift @ans;
480         if ($ok) {
481                 delete $self->{errors};
482         } else {
483                 if (++$self->{errors} > $maxerrors) {
484                         $self->send($self->msg('e26'));
485                         $self->disconnect;
486                         return ();
487                 }
488         }
489         return map {s/([^\s])\s+$/$1/; $_} @ans;
490 }
491
492 #
493 # This is called from inside the main cluster processing loop and is used
494 # for despatching commands that are doing some long processing job
495 #
496 sub process
497 {
498         my $t = time;
499         my @dxchan = DXChannel::get_all();
500         my $dxchan;
501         
502         foreach $dxchan (@dxchan) {
503                 next if $dxchan->sort ne 'U';  
504         
505                 # send a outstanding message prompt if required
506                 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
507                         $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
508                         $dxchan->lastmsgpoll($t);
509                 }
510                 
511                 # send a prompt if no activity out on this channel
512                 if ($t >= $dxchan->t + $main::user_interval) {
513                         $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
514                         $dxchan->t($t);
515                 }
516         }
517
518         while (my ($k, $v) = each %nothereslug) {
519                 if ($main::systime >= $v + 300) {
520                         delete $nothereslug{$k};
521                 }
522         }
523 }
524
525 #
526 # finish up a user context
527 #
528 sub disconnect
529 {
530         my $self = shift;
531         my $call = $self->call;
532
533         return if $self->{disconnecting}++;
534
535         delete $self->{senddbg};
536
537         my $uref = Route::User::get($call);
538         my @rout;
539         if ($uref) {
540                 @rout = $main::routeroot->del_user($uref);
541                 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
542
543                 # issue a pc17 to everybody interested
544                 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
545         } else {
546                 confess "trying to disconnect a non existant user $call";
547         }
548
549         # I was the last node visited
550     $self->user->node($main::mycall);
551                 
552         # send info to all logged in thingies
553         $self->tell_login('logoutu');
554
555         Log('DXCommand', "$call disconnected");
556
557         $self->SUPER::disconnect;
558 }
559
560 #
561 # short cut to output a prompt
562 #
563
564 sub prompt
565 {
566         my $self = shift;
567         my $call = $self->call;
568         my $date = cldate($main::systime);
569         my $time = ztime($main::systime);
570         my $prompt = $self->{prompt} || $self->msg('pr');
571
572         $call = "($call)" unless $self->here;
573         $prompt =~ s/\%C/$call/g;
574         $prompt =~ s/\%D/$date/g;
575         $prompt =~ s/\%T/$time/g;
576         $prompt =~ s/\%M/$main::mycall/g;
577         
578         $self->send($prompt);
579 }
580
581 # broadcast a message to all users [except those mentioned after buffer]
582 sub broadcast
583 {
584         my $pkg = shift;                        # ignored
585         my $s = shift;                          # the line to be rebroadcast
586         
587     foreach my $dxchan (DXChannel::get_all()) {
588                 next unless $dxchan->{sort} eq 'U'; # only interested in user channels  
589                 next if grep $dxchan == $_, @_;
590                 $dxchan->send($s);                      # send it
591         }
592 }
593
594 # gimme all the users
595 sub get_all
596 {
597         return grep {$_->{sort} eq 'U'} DXChannel::get_all();
598 }
599
600 # run a script for this user
601 sub run_script
602 {
603         my $self = shift;
604         my $silent = shift || 0;
605         
606 }
607
608 #
609 # search for the command in the cache of short->long form commands
610 #
611
612 sub search
613 {
614         my ($path, $short_cmd, $suffix) = @_;
615         my ($apath, $acmd);
616         
617         # commands are lower case
618         $short_cmd = lc $short_cmd;
619         dbg("command: $path $short_cmd\n") if isdbg('command');
620
621         # do some checking for funny characters
622         return () if $short_cmd =~ /\/$/;
623
624         # return immediately if we have it
625         ($apath, $acmd) = split ',', $cmd_cache{$short_cmd} if $cmd_cache{$short_cmd};
626         if ($apath && $acmd) {
627                 dbg("cached $short_cmd = ($apath, $acmd)\n") if isdbg('command');
628                 return ($apath, $acmd);
629         }
630         
631         # if not guess
632         my @parts = split '/', $short_cmd;
633         my $dirfn;
634         my $curdir = $path;
635         my $p;
636         my $i;
637         my @lparts;
638         
639         for ($i = 0; $i < @parts; $i++) {
640                 my  $p = $parts[$i];
641                 opendir(D, $curdir) or confess "can't open $curdir $!";
642                 my @ls = readdir D;
643                 closedir D;
644                 my $l;
645                 foreach $l (sort @ls) {
646                         next if $l =~ /^\./;
647                         if ($i < $#parts) {             # we are dealing with directories
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                                         last;
653                                 }
654                         } else {                        # we are dealing with commands
655                                 @lparts = split /\./, $l;                  
656                                 next if $lparts[$#lparts] ne $suffix;        # only look for .$suffix files
657                                 if ($p eq substr($l, 0, length $p)) {
658                                         pop @lparts; #  remove the suffix
659                                         $l = join '.', @lparts;
660                                         #                 chop $dirfn;               # remove trailing /
661                                         $dirfn = "" unless $dirfn;
662                                         $cmd_cache{$short_cmd} = join(',', ($path, "$dirfn$l")); # cache it
663                                         dbg("got path: $path cmd: $dirfn$l\n") if isdbg('command');
664                                         return ($path, "$dirfn$l"); 
665                                 }
666                         }
667                 }
668         }
669         return ();  
670 }  
671
672 # clear the command name cache
673 sub clear_cmd_cache
674 {
675         no strict 'refs';
676         
677         for (keys %Cache) {
678                 undef *{$_} unless /cmd_cache/;
679                 dbg("Undefining cmd $_") if isdbg('command');
680         }
681         %cmd_cache = ();
682         %Cache = ();
683 }
684
685 #
686 # the persistant execution of things from the command directories
687 #
688 #
689 # This allows perl programs to call functions dynamically
690
691 # This has been nicked directly from the perlembed pages
692 #
693
694 #require Devel::Symdump;  
695
696 sub valid_package_name {
697         my($string) = @_;
698         $string =~ s|([^A-Za-z0-9_/])|sprintf("_%2x",unpack("C",$1))|eg;
699         
700         $string =~ s|/|_|g;
701         return "cmd_$string";
702 }
703
704
705 # this bit of magic finds a command in the offered directory
706 sub find_cmd_name {
707         my $path = shift;
708         my $cmdname = shift;
709         my $package = valid_package_name($cmdname);
710         my $filename = "$path/$cmdname.pl";
711         my $mtime = -M $filename;
712         
713         # return if we can't find it
714         $errstr = undef;
715         unless (defined $mtime) {
716                 $errstr = DXM::msg('e1');
717                 return undef;
718         }
719         
720         if(defined $Cache{$package}->{mtime} &&$Cache{$package}->{mtime} <= $mtime) {
721                 #we have compiled this subroutine already,
722                 #it has not been updated on disk, nothing left to do
723                 #print STDERR "already compiled $package->handler\n";
724                 ;
725         } else {
726
727                 my $sub = readfilestr($filename);
728                 unless ($sub) {
729                         $errstr = "Syserr: can't open '$filename' $!";
730                         return undef;
731                 };
732                 
733                 #wrap the code into a subroutine inside our unique package
734                 my $eval = qq( sub $package { $sub } );
735                 
736                 if (isdbg('eval')) {
737                         my @list = split /\n/, $eval;
738                         my $line;
739                         for (@list) {
740                                 dbg($_ . "\n") if isdbg('eval');
741                         }
742                 }
743                 
744                 # get rid of any existing sub and try to compile the new one
745                 no strict 'refs';
746
747                 if (exists $Cache{$package}) {
748                         dbg("Redefining $package") if isdbg('command');
749                         undef *$package;
750                 } else {
751                         dbg("Defining $package") if isdbg('command');
752                 }
753                 eval $eval;
754                 
755                 $Cache{$package} = {mtime => $mtime };
756             
757         }
758
759         return $package;
760 }
761
762 sub local_send
763 {
764         my ($self, $let, $buf) = @_;
765         if ($self->{state} eq 'prompt' || $self->{state} eq 'talk') {
766                 if ($self->{enhanced}) {
767                         $self->send_later($let, $buf);
768                 } else {
769                         $self->send($buf);
770                 }
771         } else {
772                 $self->delay($buf);
773         }
774 }
775
776 # send a talk message here
777 sub talk
778 {
779         my ($self, $from, $to, $via, $line) = @_;
780         $line =~ s/\\5E/\^/g;
781         $self->local_send('T', "$to de $from: $line") if $self->{talk};
782         Log('talk', $to, $from, $via?$via:$main::mycall, $line);
783         # send a 'not here' message if required
784         unless ($self->{here} && $from ne $to) {
785                 my $key = "$to$from";
786                 unless (exists $nothereslug{$key}) {
787                         my ($ref, $dxchan);
788                         if (($ref = Route::get($from)) && ($dxchan = $ref->dxchan)) {
789                                 my $name = $self->user->name || $to;
790                                 my $s = $self->user->nothere || $dxchan->msg('nothere', $name);
791                                 $nothereslug{$key} = $main::systime;
792                                 $dxchan->talk($to, $from, undef, $s);
793                         }
794                 }
795         }
796 }
797
798 # send an announce
799 sub announce
800 {
801         my $self = shift;
802         my $line = shift;
803         my $isolate = shift;
804         my $to = shift;
805         my $target = shift;
806         my $text = shift;
807         my ($filter, $hops);
808
809         if (!$self->{ann_talk} && $to ne $self->{call}) {
810                 my $call = AnnTalk::is_talk_candidate($_[0], $text);
811                 return if $call;
812         }
813
814         if ($self->{annfilter}) {
815                 ($filter, $hops) = $self->{annfilter}->it(@_ );
816                 return unless $filter;
817         }
818
819         unless ($self->{ann}) {
820                 return if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
821         }
822         return if $target eq 'SYSOP' && $self->{priv} < 5;
823         my $buf = "$to$target de $_[0]: $text";
824         $buf =~ s/\%5E/^/g;
825         $buf .= "\a\a" if $self->{beep};
826         $self->local_send($target eq 'WX' ? 'W' : 'N', $buf);
827 }
828
829 # send a chat
830 sub chat
831 {
832         my $self = shift;
833         my $line = shift;
834         my $isolate = shift;
835         my $target = shift;
836         my $to = shift;
837         my $text = shift;
838         my ($filter, $hops);
839
840         return unless grep uc $_ eq $target, @{$self->{user}->{group}};
841         
842         $text =~ s/^\#\d+ //;
843         my $buf = "$target de $_[0]: $text";
844         $buf =~ s/\%5E/^/g;
845         $buf .= "\a\a" if $self->{beep};
846         $self->local_send('C', $buf);
847 }
848
849 sub format_dx_spot
850 {
851         my $self = shift;
852
853         my $t = ztime($_[2]);
854         my $loc = '';
855         my $clth = $self->{consort} eq 'local' ? 29 : 30;
856         my $comment = substr $_[3], 0, $clth; 
857         $comment .= ' ' x ($clth - length($comment));
858         if ($self->{user}->wantgrid) { 
859                 my $ref = DXUser->get_current($_[4]);
860                 if ($ref) {
861                         $loc = $ref->qra || '';
862                         $loc = ' ' . substr($loc, 0, 4) if $loc;
863                 }
864         }
865
866         if ($self->{user}->wantdxitu) {
867                 $loc = ' ' . sprintf("%2d", $_[10]) if defined $_[10];
868                 $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[8]) if defined $_[8]; 
869         } elsif ($self->{user}->wantdxcq) {
870                 $loc = ' ' . sprintf("%2d", $_[11]) if defined $_[11];
871                 $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . sprintf("%2d", $_[9]) if defined $_[9]; 
872         } elsif ($self->{user}->wantusstate) {
873                 $loc = ' ' . $_[13] if $_[13];
874                 $comment = substr($comment, 0,  $self->{consort} eq 'local' ? 26 : 27) . ' ' . $_[12] if $_[12]; 
875         }
876
877         return sprintf "DX de %-7.7s%11.1f  %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
878 }
879
880 # send a dx spot
881 sub dx_spot
882 {
883         my $self = shift;
884         my $line = shift;
885         my $isolate = shift;
886         return unless $self->{dx};
887
888         my ($filter, $hops);
889
890         if ($self->{spotsfilter}) {
891                 ($filter, $hops) = $self->{spotsfilter}->it(@_ );
892                 return unless $filter;
893         }
894
895         dbg('spot: "' . join('","', @_) . '"') if isdbg('dxspot');
896
897         my $buf;
898         if ($self->{ve7cc}) {
899                 $buf = VE7CC::dx_spot($self, @_);
900         } else {
901                 $buf = $self->format_dx_spot(@_);
902                 $buf .= "\a\a" if $self->{beep};
903                 $buf =~ s/\%5E/^/g;
904         }
905
906         $self->local_send('X', $buf);
907 }
908
909 sub wwv
910 {
911         my $self = shift;
912         my $line = shift;
913         my $isolate = shift;
914         my ($filter, $hops);
915
916         return unless $self->{wwv};
917         
918         if ($self->{wwvfilter}) {
919                 ($filter, $hops) = $self->{wwvfilter}->it(@_ );
920                 return unless $filter;
921         }
922
923         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
924         $buf .= "\a\a" if $self->{beep};
925         $self->local_send('V', $buf);
926 }
927
928 sub wcy
929 {
930         my $self = shift;
931         my $line = shift;
932         my $isolate = shift;
933         my ($filter, $hops);
934
935         return unless $self->{wcy};
936         
937         if ($self->{wcyfilter}) {
938                 ($filter, $hops) = $self->{wcyfilter}->it(@_ );
939                 return unless $filter;
940         }
941
942         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
943         $buf .= "\a\a" if $self->{beep};
944         $self->local_send('Y', $buf);
945 }
946
947 # broadcast debug stuff to all interested parties
948 sub broadcast_debug
949 {
950         my $s = shift;                          # the line to be rebroadcast
951         
952         foreach my $dxchan (DXChannel::get_all) {
953                 next unless $dxchan->{enhanced} && $dxchan->{senddbg};
954                 $dxchan->send_later('L', $s);
955         }
956 }
957
958 sub do_entry_stuff
959 {
960         my $self = shift;
961         my $line = shift;
962         my @out;
963         
964         if ($self->state eq 'enterbody') {
965                 my $loc = $self->{loc} || confess "local var gone missing" ;
966                 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
967                         no strict 'refs';
968                         push @out, &{$loc->{endaction}}($self);          # like this for < 5.8.0
969                         $self->func(undef);
970                         $self->state('prompt');
971                 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
972                         push @out, $self->msg('m10');
973                         delete $loc->{lines};
974                         delete $self->{loc};
975                         $self->func(undef);
976                         $self->state('prompt');
977                 } else {
978                         push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
979                         # i.e. it ain't and end or abort, therefore store the line
980                 }
981         } else {
982                 confess "Invalid state $self->{state}";
983         }
984         return @out;
985 }
986
987 sub store_startup_script
988 {
989         my $self = shift;
990         my $loc = $self->{loc} || confess "local var gone missing" ;
991         my @out;
992         my $call = $loc->{call} || confess "callsign gone missing";
993         confess "lines array gone missing" unless ref $loc->{lines};
994         my $r = Script::store($call, $loc->{lines});
995         if (defined $r) {
996                 if ($r) {
997                         push @out, $self->msg('m19', $call, $r);
998                 } else {
999                         push @out, $self->msg('m20', $call);
1000                 }
1001         } else {
1002                 push @out, "error opening startup script $call $!";
1003         } 
1004         return @out;
1005 }
1006
1007 1;
1008 __END__