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