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