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