]> dxcluster.net Git - spider.git/blob - perl/DXCommandmode.pm
regenerate prefix_data from new cty.dat
[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 $cmdimportdir);
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 $cmdimportdir = "$main::root/cmd_import"; # the base directory for importing command scripts 
57                                           # this does not exist as default, you need to create it manually
58
59
60
61 use vars qw($VERSION $BRANCH);
62
63 main::mkver($VERSION = q$Revision$);
64
65 #
66 # obtain a new connection this is derived from dxchannel
67 #
68
69 sub new 
70 {
71         my $self = DXChannel::alloc(@_);
72
73         # routing, this must go out here to prevent race condx
74         my $pkg = shift;
75         my $call = shift;
76         my @rout = $main::routeroot->add_user($call, 1);
77
78         
79         my $ref = Route::User::get($call);
80         $main::me->route_pc16($main::mycall, undef, $main::routeroot, $ref) if $ref;
81         return $self;
82 }
83
84 # this is how a a connection starts, you get a hello message and the motd with
85 # possibly some other messages asking you to set various things up if you are
86 # new (or nearly new and slacking) user.
87
88 sub start
89
90         my ($self, $line, $sort) = @_;
91         my $user = $self->{user};
92         my $call = $self->{call};
93         my $name = $user->{name};
94         
95         # log it
96         my $host = $self->{conn}->{peerhost} || "unknown";
97         Log('DXCommand', "$call connected from $host");
98
99         $self->{name} = $name ? $name : $call;
100         $self->send($self->msg('l2',$self->{name}));
101         $self->state('prompt');         # a bit of room for further expansion, passwords etc
102         $self->{priv} = $user->priv || 0;
103         $self->{lang} = $user->lang || $main::lang || 'en';
104         my $pagelth = $user->pagelth;
105         $pagelth = $default_pagelth unless defined $pagelth;
106         $self->{pagelth} = $pagelth;
107         ($self->{width}) = $line =~ /width=(\d+)/; $line =~ s/\s*width=\d+\s*//;
108         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
109         $self->{consort} = $line;       # save the connection type
110         
111         # set some necessary flags on the user if they are connecting
112         $self->{beep} = $user->wantbeep;
113         $self->{ann} = $user->wantann;
114         $self->{wwv} = $user->wantwwv;
115         $self->{wcy} = $user->wantwcy;
116         $self->{talk} = $user->wanttalk;
117         $self->{wx} = $user->wantwx;
118         $self->{dx} = $user->wantdx;
119         $self->{logininfo} = $user->wantlogininfo;
120         $self->{ann_talk} = $user->wantann_talk;
121         $self->{here} = 1;
122         $self->{prompt} = $user->prompt if $user->prompt;
123
124         # sort out new dx spot stuff
125         $user->wantdxcq(0) unless defined $user->{wantdxcq};
126         $user->wantdxitu(0) unless defined $user->{wantdxitu};  
127         $user->wantusstate(0) unless defined $user->{wantusstate};
128
129         # sort out registration
130         if ($main::reqreg == 1) {
131                 $self->{registered} = $user->registered;
132         } elsif ($main::reqreg == 2) {
133                 $self->{registered} = !$user->registered;
134         } else {
135                 $self->{registered} = 1;
136         }
137
138
139         # decide which motd to send
140         my $motd;
141         unless ($self->{registered}) {
142                 $motd = "${main::motd}_nor_$self->{lang}";
143                 $motd = "${main::motd}_nor" unless -e $motd;
144         }
145         $motd = "${main::motd}_$self->{lang}" unless $motd && -e $motd;
146         $motd = $main::motd unless $motd && -e $motd;
147         $self->send_file($motd) if -e $motd;
148
149         # sort out privilege reduction
150         $self->{priv} = 0 if $line =~ /^(ax|te)/ && !$self->conn->{usedpasswd};
151
152         # get the filters
153         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'user_default', 0);
154         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'user_default', 0);
155         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'user_default', 0);
156         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'user_default', 0) ;
157
158         # clean up qra locators
159         my $qra = $user->qra;
160         $qra = undef if ($qra && !DXBearing::is_qra($qra));
161         unless ($qra) {
162                 my $lat = $user->lat;
163                 my $long = $user->long;
164                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
165         }
166
167         # decide on echo
168         my $echo = $user->wantecho;
169         unless ($echo) {
170                 $self->send_now('E', "0");
171                 $self->send($self->msg('echow'));
172                 $self->conn->echo($echo) if $self->conn->can('echo');
173         }
174         
175         $self->tell_login('loginu');
176         
177         # do we need to send a forward/opernam?
178         my $lastoper = $user->lastoper || 0;
179         my $homenode = $user->homenode || ""; 
180         if ($homenode eq $main::mycall && $main::systime >= $lastoper + $DXUser::lastoperinterval) {
181                 run_cmd($main::me, "forward/opernam $call");
182                 $user->lastoper($main::systime + ((int rand(10)) * 86400));
183         }
184
185         # ALWAYS output the user
186         my $thing = Thingy::Hello->new(user => $call, h => $self->{here});
187         $thing->broadcast($self);
188         $self->lasthello($main::systime);
189
190         # run a script send the output to the punter
191         my $script = new Script(lc $call) || new Script('user_default');
192         $script->run($self) if $script;
193
194         # send cluster info
195         my $info = Route::cluster();
196         $self->send("Cluster:$info");
197
198         # send prompts and things
199         $self->send($self->msg('namee1')) if !$user->name;
200         $self->send($self->msg('qthe1')) if !$user->qth;
201         $self->send($self->msg('qll')) if !$user->qra || (!$user->lat && !$user->long);
202         $self->send($self->msg('hnodee1')) if !$user->qth;
203         $self->send($self->msg('m9')) if DXMsg::for_me($call);
204         $self->lastmsgpoll($main::systime);
205         $self->prompt;
206 }
207
208 #
209 # This is the normal command prompt driver
210 #
211
212 sub normal
213 {
214         my $self = shift;
215         my $cmdline = shift;
216         my @ans;
217
218         # save this for them's that need it
219         my $rawline = $cmdline;
220         
221         # remove leading and trailing spaces
222         $cmdline =~ s/^\s*(.*)\s*$/$1/;
223         
224         if ($self->{state} eq 'page') {
225                 my $i = $self->{pagelth};
226                 my $ref = $self->{pagedata};
227                 my $tot = @$ref;
228                 
229                 # abort if we get a line starting in with a
230                 if ($cmdline =~ /^a/io) {
231                         undef $ref;
232                         $i = 0;
233                 }
234         
235                 # send a tranche of data
236                 while ($i-- > 0 && @$ref) {
237                         my $line = shift @$ref;
238                         $line =~ s/\s+$//o;     # why am having to do this? 
239                         $self->send($line);
240                 }
241                 
242                 # reset state if none or else chuck out an intermediate prompt
243                 if ($ref && @$ref) {
244                         $tot -= $self->{pagelth};
245                         $self->send($self->msg('page', $tot));
246                 } else {
247                         $self->state('prompt');
248                 }
249         } elsif ($self->{state} eq 'sysop') {
250                 my $passwd = $self->{user}->passwd;
251                 if ($passwd) {
252                         my @pw = grep {$_ !~ /\s/} split //, $passwd;
253                         my @l = @{$self->{passwd}};
254                         my $str = "$pw[$l[0]].*$pw[$l[1]].*$pw[$l[2]].*$pw[$l[3]].*$pw[$l[4]]";
255                         if ($cmdline =~ /$str/) {
256                                 $self->{priv} = $self->{user}->priv;
257                         } else {
258                                 $self->send($self->msg('sorry'));
259                         }
260                 } else {
261                         $self->send($self->msg('sorry'));
262                 }
263                 $self->state('prompt');
264         } elsif ($self->{state} eq 'passwd') {
265                 my $passwd = $self->{user}->passwd;
266                 if ($passwd && $cmdline eq $passwd) {
267                         $self->send($self->msg('pw1'));
268                         $self->state('passwd1');
269                 } else {
270                         $self->conn->{echo} = $self->conn->{decho};
271                         delete $self->conn->{decho};
272                         $self->send($self->msg('sorry'));
273                         $self->state('prompt');
274                 }
275         } elsif ($self->{state} eq 'passwd1') {
276                 $self->{passwd} = $cmdline;
277                 $self->send($self->msg('pw2'));
278                 $self->state('passwd2');
279         } elsif ($self->{state} eq 'passwd2') {
280                 if ($cmdline eq $self->{passwd}) {
281                         $self->{user}->passwd($cmdline);
282                         $self->send($self->msg('pw3'));
283                 } else {
284                         $self->send($self->msg('pw4'));
285                 }
286                 $self->conn->{echo} = $self->conn->{decho};
287                 delete $self->conn->{decho};
288                 $self->state('prompt');
289         } elsif ($self->{state} eq 'talk') {
290                 if ($cmdline =~ m{^(?:/EX|/ABORT)}i) {
291                         for (@{$self->{talklist}}) {
292                                 $self->send_talks($_,  $self->msg('talkend'));
293                         }
294                         $self->state('prompt');
295                         delete $self->{talklist};
296                 } elsif ($cmdline =~ m|^/+\w+|) {
297                         $cmdline =~ s|^/||;
298                         my $sendit = $cmdline =~ s|^/+||;
299                         my @in = $self->run_cmd($cmdline);
300                         $self->send_ans(@in);
301                         if ($sendit && $self->{talklist} && @{$self->{talklist}}) {
302                                 foreach my $l (@in) {
303                                         my @bad;
304                                         if (@bad = BadWords::check($l)) {
305                                                 $self->badcount(($self->badcount||0) + @bad);
306                                                 Log('DXCommand', "$self->{call} swore: $l");
307                                         } else {
308                                                 for (@{$self->{talklist}}) {
309                                                         $self->send_talks($_, $l);
310                                                 }
311                                         }
312                                 }
313                         }
314                         $self->send($self->talk_prompt);
315                 } elsif ($self->{talklist} && @{$self->{talklist}}) {
316                         # send what has been said to whoever is in this person's talk list
317                         my @bad;
318                         if (@bad = BadWords::check($cmdline)) {
319                                 $self->badcount(($self->badcount||0) + @bad);
320                                 Log('DXCommand', "$self->{call} swore: $cmdline");
321                         } else {
322                                 for (@{$self->{talklist}}) {
323                                         $self->send_talks($_, $rawline);
324                                 }
325                         }
326                         $self->send($self->talk_prompt) if $self->{state} eq 'talk';
327                 } else {
328                         # for safety
329                         $self->state('prompt');
330                 }
331         } elsif (my $func = $self->{func}) {
332                 no strict 'refs';
333                 my @ans;
334                 if (ref $self->{edit}) {
335                         eval { @ans = $self->{edit}->$func($self, $rawline)};
336                 } else {
337                         eval {  @ans = &{$self->{func}}($self, $rawline) };
338                 }
339                 if ($@) {
340                         $self->send_ans("Syserr: on stored func $self->{func}", $@);
341                         delete $self->{func};
342                         $self->state('prompt');
343                         undef $@;
344                 }
345                 $self->send_ans(@ans);
346         } else {
347                 $self->send_ans(run_cmd($self, $cmdline));
348         } 
349
350         # check for excessive swearing
351         if ($self->{badcount} && $self->{badcount} >= $maxbadcount) {
352                 Log('DXCommand', "$self->{call} logged out for excessive swearing");
353                 $self->disconnect;
354                 return;
355         }
356
357         # send a prompt only if we are in a prompt state
358         $self->prompt() if $self->{state} =~ /^prompt/o;
359 }
360
361 # send out the talk messages taking into account vias and connectivity
362 sub send_talks
363 {
364         my ($self, $ent, $line) = @_;
365         
366         my ($to, $via) = $ent =~ /(\S+)>(\S+)/;
367         $to = $ent unless $to;
368         my $call = $via ? $via : $to;
369         my $clref = Route::get($call);
370         my $dxchan = $clref->dxchan if $clref;
371         if ($dxchan) {
372                 $dxchan->talk($self->{call}, $to, $via, $line);
373         } else {
374                 $self->send($self->msg('disc2', $via ? $via : $to));
375                 my @l = grep { $_ ne $ent } @{$self->{talklist}};
376                 if (@l) {
377                         $self->{talklist} = \@l;
378                 } else {
379                         delete $self->{talklist};
380                         $self->state('prompt');
381                 }
382         }
383 }
384
385 sub talk_prompt
386 {
387         my $self = shift;
388         my @call;
389         for (@{$self->{talklist}}) {
390                 my ($to, $via) = /(\S+)>(\S+)/;
391                 $to = $_ unless $to;
392                 push @call, $to;
393         }
394         return $self->msg('talkprompt', join(',', @call));
395 }
396
397 #
398 # send a load of stuff to a command user with page prompting
399 # and stuff
400 #
401
402 sub send_ans
403 {
404         my $self = shift;
405         
406         if ($self->{pagelth} && @_ > $self->{pagelth}) {
407                 my $i;
408                 for ($i = $self->{pagelth}; $i-- > 0; ) {
409                         my $line = shift @_;
410                         $line =~ s/\s+$//o;     # why am having to do this? 
411                         $self->send($line);
412                 }
413                 $self->{pagedata} =  [ @_ ];
414                 $self->state('page');
415                 $self->send($self->msg('page', scalar @_));
416         } else {
417                 for (@_) {
418                         if (defined $_) {
419                                 $self->send($_);
420                         } else {
421                                 $self->send('');
422                         }
423                 }
424         } 
425 }
426
427 # this is the thing that runs the command, it is done like this for the 
428 # benefit of remote command execution
429 #
430
431 sub run_cmd
432 {
433         my $self = shift;
434         my $user = $self->{user};
435         my $call = $self->{call};
436         my $cmdline = shift;
437         my @ans;
438         
439
440         return () if length $cmdline == 0;
441                 
442         # split the command line up into parts, the first part is the command
443         my ($cmd, $args) = split /\s+/, $cmdline, 2;
444         $args = "" unless defined $args;
445                 
446         if ($cmd) {
447                 # strip out // and .. on command only
448                 $cmd =~ s|//|/|g;
449                 $cmd =~ s|^/||g;                # no leading / either
450                 $cmd =~ s|[^-?\w/]||g;  # and no funny characters
451                                         
452                 my ($path, $fcmd);
453                         
454                 dbg("cmd: $cmd") if isdbg('command');
455                         
456                 # alias it if possible
457                 my $acmd = CmdAlias::get_cmd($cmd);
458                 if ($acmd) {
459                         ($cmd, $args) = split /\s+/, "$acmd $args", 2;
460                         $args = "" unless defined $args;
461                         dbg("aliased cmd: $cmd $args") if isdbg('command');
462                 }
463                         
464                 # first expand out the entry to a command
465                 ($path, $fcmd) = search($main::localcmd, $cmd, "pl");
466                 ($path, $fcmd) = search($main::cmd, $cmd, "pl") unless $path && $fcmd;
467
468                 if ($path && $cmd) {
469                         dbg("path: $cmd cmd: $fcmd") if isdbg('command');
470                         
471                         my $package = find_cmd_name($path, $fcmd);
472                         return ($@) if $@;
473                                 
474                         if ($package) {
475                                 no strict 'refs';
476                                 dbg("package: $package") if isdbg('command');
477                                 eval { @ans = &$package($self, $args) };
478                                 return (DXDebug::shortmess($@)) if $@;
479                         }
480                 } else {
481                         dbg("cmd: $cmd not found") if isdbg('command');
482                         if (++$self->{errors} > $maxerrors) {
483                                 $self->send($self->msg('e26'));
484                                 $self->disconnect;
485                                 return ();
486                         } else {
487                                 return ($self->msg('e1'));
488                         }
489                 }
490         }
491         
492         my $ok = shift @ans;
493         if ($ok) {
494                 delete $self->{errors};
495         } else {
496                 if (++$self->{errors} > $maxerrors) {
497                         $self->send($self->msg('e26'));
498                         $self->disconnect;
499                         return ();
500                 }
501         }
502         return map {s/([^\s])\s+$/$1/; $_} @ans;
503 }
504
505 #
506 # This is called from inside the main cluster processing loop and is used
507 # for despatching commands that are doing some long processing job
508 #
509 sub process
510 {
511         my $t = time;
512         my @dxchan = DXChannel::get_all();
513         my $dxchan;
514         
515         foreach $dxchan (@dxchan) {
516                 next if $dxchan->sort ne 'U';  
517         
518                 # send a outstanding message prompt if required
519                 if ($t >= $dxchan->lastmsgpoll + $msgpolltime) {
520                         $dxchan->send($dxchan->msg('m9')) if DXMsg::for_me($dxchan->call);
521                         $dxchan->lastmsgpoll($t);
522                 }
523                 
524                 # send a prompt if no activity out on this channel
525                 if ($t >= $dxchan->t + $main::user_interval) {
526                         $dxchan->prompt() if $dxchan->{state} =~ /^prompt/o;
527                         $dxchan->t($t);
528                 }
529         }
530
531         while (my ($k, $v) = each %nothereslug) {
532                 if ($main::systime >= $v + 300) {
533                         delete $nothereslug{$k};
534                 }
535         }
536
537         import_cmd();
538 }
539
540 #
541 # finish up a user context
542 #
543 sub disconnect
544 {
545         my $self = shift;
546         my $call = $self->call;
547
548         return if $self->{disconnecting}++;
549
550         delete $self->{senddbg};
551
552         my $uref = Route::User::get($call);
553         my @rout;
554         if ($uref) {
555                 @rout = $main::routeroot->del_user($uref);
556                 dbg("B/C PC17 on $main::mycall for: $call") if isdbg('route');
557
558                 # issue a pc17 to everybody interested
559                 $main::me->route_pc17($main::mycall, undef, $main::routeroot, $uref);
560
561                 my $thing = Thingy::Bye->new(user=>$call);
562                 $thing->broadcast($self);
563         } else {
564                 confess "trying to disconnect a non existant user $call";
565         }
566
567         # I was the last node visited
568     $self->user->node($main::mycall);
569                 
570         # send info to all logged in thingies
571         $self->tell_login('logoutu');
572
573         # remove any outstanding pings I have sent
574         Thingy::Ping::forget($call);
575         
576         Log('DXCommand', "$call disconnected");
577
578         $self->SUPER::disconnect;
579 }
580
581 #
582 # short cut to output a prompt
583 #
584
585 sub prompt
586 {
587         my $self = shift;
588         my $call = $self->call;
589         my $date = cldate($main::systime);
590         my $time = ztime($main::systime);
591         my $prompt = $self->{prompt} || $self->msg('pr');
592
593         $call = "($call)" unless $self->here;
594         $prompt =~ s/\%C/$call/g;
595         $prompt =~ s/\%D/$date/g;
596         $prompt =~ s/\%T/$time/g;
597         $prompt =~ s/\%M/$main::mycall/g;
598         
599         $self->send($prompt);
600 }
601
602 # broadcast a message to all users [except those mentioned after buffer]
603 sub broadcast
604 {
605         my $pkg = shift;                        # ignored
606         my $s = shift;                          # the line to be rebroadcast
607         
608     foreach my $dxchan (DXChannel::get_all()) {
609                 next unless $dxchan->{sort} eq 'U'; # only interested in user channels  
610                 next if grep $dxchan == $_, @_;
611                 $dxchan->send($s);                      # send it
612         }
613 }
614
615 # gimme all the users
616 sub get_all
617 {
618         return grep {$_->{sort} eq 'U'} DXChannel::get_all();
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 # Import any commands contained in any files in import_cmd directory
963 #
964 # If the filename has a recogisable callsign as some delimited part
965 # of it, then this is the user the command will be run as. 
966 #
967 sub import_cmd
968 {
969         # are there any to do in this directory?
970         return unless -d $cmdimportdir;
971         unless (opendir(DIR, $cmdimportdir)) {
972                 dbg("can\'t open $cmdimportdir $!");
973                 Log('err', "can\'t open $cmdimportdir $!");
974                 return;
975         } 
976
977         my @names = readdir(DIR);
978         closedir(DIR);
979         my $name;
980         foreach $name (@names) {
981                 next if $name =~ /^\./;
982
983                 my $s = Script->new($name, $cmdimportdir);
984                 if ($s) {
985
986                         dbg("Run import cmd file $name");
987                         Log('DXCommand', "Run import cmd file $name");
988                         my @cat = split /[^A-Za-z0-9]+/, $name;
989                         my ($call) = grep {is_callsign(uc $_)} @cat;
990                         $call ||= $main::mycall;
991                         $call = uc $call;
992                         my @out;
993                         
994                         
995                         $s->inscript(0);        # switch off script checks
996                         
997                         if ($call eq $main::mycall) {
998                                 @out = $s->run($main::me, 1);
999                         } else {
1000                                 my $dxchan = DXChannel::get($call);
1001                             if ($dxchan) {
1002                                         @out = $s->run($dxchan, 1);
1003                                 } else {
1004                                         my $u = DXUser->get($call);
1005                                         if ($u) {
1006                                                 $dxchan = $main::me;
1007                                                 my $old = $dxchan->{call};
1008                                                 my $priv = $dxchan->{priv};
1009                                                 my $user = $dxchan->{user};
1010                                                 $dxchan->{call} = $call;
1011                                                 $dxchan->{priv} = $u->priv;
1012                                                 $dxchan->{user} = $u;
1013                                                 @out = $s->run($dxchan, 1);
1014                                                 $dxchan->{call} = $call;
1015                                                 $dxchan->{priv} = $priv;
1016                                                 $dxchan->{user} = $user;
1017                                         } else {
1018                                                 Log('err', "Trying to run import cmd for non-existant user $call");
1019                                                 dbg( "Trying to run import cmd for non-existant user $call");
1020                                         }
1021                                 }
1022                         }
1023                         $s->erase;
1024                         for (@out) {
1025                                 Log('DXCommand', "Import cmd $name/$call: $_");
1026                                 dbg("Import cmd $name/$call: $_");
1027                         }
1028                 } else {
1029                         Log("Failed to open $cmdimportdir/$name $!");
1030                         dbg("Failed to open $cmdimportdir/$name $!");
1031                         unlink "$cmdimportdir/$name";
1032                 }
1033         }
1034 }
1035
1036 1;
1037 __END__