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