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