3407e9bc03c6a297eacc868c5a9d1adc1bf318c7
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXProtVars;
19 use DXCommandmode;
20 use DXLog;
21 use Spot;
22 use DXProtout;
23 use DXDebug;
24 use Filter;
25 use Local;
26 use DXDb;
27 use AnnTalk;
28 use Geomag;
29 use WCY;
30 use BadWords;
31 use DXHash;
32 use Route;
33 use Route::Node;
34 use Script;
35 use RouteDB;
36 use DXProtHandle;
37
38 use strict;
39
40 use vars qw($VERSION $BRANCH);
41 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
42                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
43                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
44                         $investigation_int $pc19_version $myprot_version
45                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
46                         $allowzero $decode_dk0wcy $send_opernam @checklist
47                         $eph_pc15_restime $pc92_update_period $last_pc92_update
48                    );
49
50 ($VERSION, $BRANCH) = dxver(q$Revision$);
51
52 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
53 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
54
55 $last_hour = time;                              # last time I did an hourly periodic update
56 %rcmds = ();                    # outstanding rcmd requests outbound
57 %nodehops = ();                 # node specific hop control
58 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
59
60 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
61                                                                 # loads of 'bad things'
62 $baddx = new DXHash "baddx";
63 $badspotter = new DXHash "badspotter";
64 $badnode = new DXHash "badnode";
65 $last10 = $last_pc50 = time;
66 $ann_to_talk = 1;
67 $rspfcheck = 1;
68 $eph_restime = 180;
69 $eph_info_restime = 60*60;
70 $eph_pc15_restime = 6*60;
71 $eph_pc34_restime = 30;
72 $pingint = 5*60;
73 $obscount = 2;
74 $chatdupeage = 20 * 60 * 60;
75 $chatimportfn = "$main::root/chat_import";
76 $investigation_int = 12*60*60;  # time between checks to see if we can see this node
77 $pc19_version = 5466;                   # the visible version no for outgoing PC19s generated from pc59
78 $pc92_update_period = 30*60;    # the period between PC92 C updates
79 $last_pc92_update = time + int rand(180);               # the last time a PC92 config update
80
81
82 @checklist = 
83 (
84  [ qw(i c c m bp bc c) ],                       # pc10
85  [ qw(i f m d t m c c h) ],             # pc11
86  [ qw(i c bm m bm bm p h) ],            # pc12
87  [ qw(i c h) ],                                 # 
88  [ qw(i c h) ],                                 # 
89  [ qw(i c m h) ],                                       # 
90  undef ,                                                # pc16 has to be validated manually
91  [ qw(i c c h) ],                                       # pc17
92  [ qw(i m n) ],                                 # pc18
93  undef ,                                                # pc19 has to be validated manually
94  undef ,                                                # pc20 no validation
95  [ qw(i c m h) ],                                       # pc21
96  undef ,                                                # pc22 no validation
97  [ qw(i d n n n n m c c h) ],           # pc23
98  [ qw(i c p h) ],                                       # pc24
99  [ qw(i c c n n) ],                             # pc25
100  [ qw(i f m d t m c c bc) ],            # pc26
101  [ qw(i d n n n n m c c bc) ],  # pc27
102  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
103  [ qw(i c c n m) ],                             # pc29
104  [ qw(i c c n) ],                                       # pc30
105  [ qw(i c c n) ],                                       # pc31
106  [ qw(i c c n) ],                                       # pc32
107  [ qw(i c c n) ],                                       # pc33
108  [ qw(i c c m) ],                                       # pc34
109  [ qw(i c c m) ],                                       # pc35
110  [ qw(i c c m) ],                                       # pc36
111  [ qw(i c c n m) ],                             # pc37
112  undef,                                                 # pc38 not interested
113  [ qw(i c m) ],                                 # pc39
114  [ qw(i c c m p n) ],                           # pc40
115  [ qw(i c n m h) ],                             # pc41
116  [ qw(i c c n) ],                                       # pc42
117  undef,                                                 # pc43 don't handle it
118  [ qw(i c c n m m c) ],                 # pc44
119  [ qw(i c c n m) ],                             # pc45
120  [ qw(i c c n) ],                                       # pc46
121  undef,                                                 # pc47
122  undef,                                                 # pc48
123  [ qw(i c m h) ],                                       # pc49
124  [ qw(i c n h) ],                                       # pc50
125  [ qw(i c c n) ],                                       # pc51
126  undef,
127  undef,
128  undef,
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,
134  undef,                                                 # pc60
135  undef,
136  undef,
137  undef,
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,
144  undef,                                                 # pc70
145  undef,
146  undef,
147  [ qw(i d n n n n n n m m m c c h) ],   # pc73
148  undef,
149  undef,
150  undef,
151  undef,
152  undef,
153  undef,
154  undef,                                                 # pc80
155  undef,
156  undef,
157  undef,
158  [ qw(i c c c m) ],                             # pc84
159  [ qw(i c c c m) ],                             # pc85
160  undef,
161  undef,
162  undef,
163  undef,
164  [ qw(i c n) ],                                 # pc90
165 );
166
167 # use the entry in the check list to check the field list presented
168 # return OK if line NOT in check list (for now)
169 sub check
170 {
171         my $n = shift;
172         $n -= 10;
173         return 0 if $n < 0 || $n > @checklist; 
174         my $ref = $checklist[$n];
175         return 0 unless ref $ref;
176         
177         my $i;
178         for ($i = 1; $i < @$ref; $i++) {
179                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
180                 return 0 unless $act;
181                 next if $blank && $_[$i] =~ /^[ \*]$/;
182                 if ($act eq 'c') {
183                         return $i unless is_callsign($_[$i]);
184                 } elsif ($act eq 'i') {                 
185                         ;                                       # do nothing
186                 } elsif ($act eq 'm') {
187                         return $i unless is_pctext($_[$i]);
188                 } elsif ($act eq 'p') {
189                         return $i unless is_pcflag($_[$i]);
190                 } elsif ($act eq 'f') {
191                         return $i unless is_freq($_[$i]);
192                 } elsif ($act eq 'n') {
193                         return $i unless $_[$i] =~ /^[\d ]+$/;
194                 } elsif ($act eq 'h') {
195                         return $i unless $_[$i] =~ /^H\d\d?$/;
196                 } elsif ($act eq 'd') {
197                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
198                 } elsif ($act eq 't') {
199                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
200                 } 
201         }
202         return 0;
203 }
204
205 sub init
206 {
207         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
208         confess $@ if $@;
209
210         my $user = DXUser->get($main::mycall);
211         die "User $main::mycall not setup or disappeared RTFM" unless $user;
212         
213         $myprot_version += $main::version*100;
214         $main::me = DXProt->new($main::mycall, 0, $user); 
215         $main::me->{here} = 1;
216         $main::me->{state} = "indifferent";
217         $main::me->{sort} = 'S';    # S for spider
218         $main::me->{priv} = 9;
219         $main::me->{metric} = 0;
220         $main::me->{pingave} = 0;
221         $main::me->{registered} = 1;
222         $main::me->{version} = $main::version;
223         $main::me->{build} = $main::build;
224         $main::me->{do_pc92} = 1;
225         $main::me->{via_pc92} = 1;
226 }
227
228 #
229 # obtain a new connection this is derived from dxchannel
230 #
231
232 sub new 
233 {
234         my $self = DXChannel::alloc(@_);
235
236         # add this node to the table, the values get filled in later
237         my $pkg = shift;
238         my $call = shift;
239         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
240
241         return $self;
242 }
243
244 # this is how a pc connection starts (for an incoming connection)
245 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
246 # all the crap that comes between).
247 sub start
248 {
249         my ($self, $line, $sort) = @_;
250         my $call = $self->{call};
251         my $user = $self->{user};
252
253         # log it
254         my $host = $self->{conn}->{peerhost};
255         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
256         $host ||= "unknown";
257
258         Log('DXProt', "$call connected from $host");
259         
260         # remember type of connection
261         $self->{consort} = $line;
262         $self->{outbound} = $sort eq 'O';
263         my $priv = $user->priv;
264         $priv = $user->priv(1) unless $priv;
265         $self->{priv} = $priv;     # other clusters can always be 'normal' users
266         $self->{lang} = $user->lang || 'en';
267         $self->{isolate} = $user->{isolate};
268         $self->{consort} = $line;       # save the connection type
269         $self->{here} = 1;
270         $self->{width} = 80;
271
272         # sort out registration
273         $self->{registered} = 1;
274
275         # get the output filters
276         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
277         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
278         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
279         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
280         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
281
282
283         # get the INPUT filters (these only pertain to Clusters)
284         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
285         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
286         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
287         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
288         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
289         
290         # set unbuffered and no echo
291         $self->send_now('B',"0");
292         $self->send_now('E',"0");
293         $self->conn->echo(0) if $self->conn->can('echo');
294         
295         # ping neighbour node stuff
296         my $ping = $user->pingint;
297         $ping = $pingint unless defined $ping;
298         $self->{pingint} = $ping;
299         $self->{nopings} = $user->nopings || $obscount;
300         $self->{pingtime} = [ ];
301         $self->{pingave} = 999;
302         $self->{metric} ||= 100;
303         $self->{lastping} = $main::systime;
304         
305         # send initialisation string
306         unless ($self->{outbound}) {
307                 $self->sendinit;
308         }
309         
310         $self->state('init');
311         $self->{pc50_t} = $main::systime;
312
313         # send info to all logged in thingies
314         $self->tell_login('loginn');
315
316         # run a script send the output to the debug file
317         my $script = new Script(lc $call) || new Script('node_default');
318         $script->run($self) if $script;
319 }
320
321 #
322 # send outgoing 'challenge'
323 #
324
325 sub sendinit
326 {
327         my $self = shift;
328         $self->send(pc18());
329 }
330
331 #
332 # This is the normal pcxx despatcher
333 #
334 sub normal
335 {
336         my ($self, $line) = @_;
337
338         if ($line =~ '^<\w+\s' && $main::do_xml) {
339                 DXXml::normal($self, $line);
340                 return;
341         }
342
343         my @field = split /\^/, $line;
344         return unless @field;
345         
346         pop @field if $field[-1] eq '~';
347         
348 #       print join(',', @field), "\n";
349                                                 
350         
351         # process PC frames, this will fail unless the frame starts PCnn
352         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
353         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
354                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
355                 return;
356         }
357
358         # check for and dump bad protocol messages
359         my $n = check($pcno, @field);
360         if ($n) {
361                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
362                 return;
363         }
364
365         # modify the hop count here
366         if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
367                 $trail ||= '';
368                 $hops--;
369                 return if $hops < 0;
370                 $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
371                 $field[-1] = "H$hops";
372         }
373
374         # send it out for processing
375         my $origin = $self->{call};
376         no strict 'subs';
377         my $sub = "handle_$pcno";
378
379         if ($self->can($sub)) {
380                 $self->$sub($pcno, $line, $origin, @field);
381         } else {
382                 $self->handle_default($pcno, $line, $origin, @field);
383         }
384 }
385
386 #
387 # This is called from inside the main cluster processing loop and is used
388 # for despatching commands that are doing some long processing job
389 #
390 sub process
391 {
392         my $t = time;
393         my @dxchan = DXChannel::get_all();
394         my $dxchan;
395         my $pc50s;
396         
397         # send out a pc50 on EVERY channel all at once
398         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
399                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
400                 eph_dup($pc50s);
401                 $last_pc50 = $t;
402         }
403
404         foreach $dxchan (@dxchan) {
405                 next unless $dxchan->is_node;
406                 next if $dxchan->handle_xml;
407                 next if $dxchan == $main::me;
408
409                 # send the pc50
410                 $dxchan->send($pc50s) if $pc50s;
411                 
412                 # send a ping out on this channel
413                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
414                         if ($dxchan->{nopings} <= 0) {
415                                 $dxchan->disconnect;
416                         } else {
417                                 DXXml::Ping::add($main::me, $dxchan->call);
418                                 $dxchan->{nopings} -= 1;
419                                 $dxchan->{lastping} = $t;
420                                 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
421                         }
422                 }
423         }
424
425         Investigate::process();
426
427         # every ten seconds
428         if ($t - $last10 >= 10) {       
429                 # clean out ephemera 
430
431                 eph_clean();
432                 import_chat();
433
434                 if ($main::systime >= $last_pc92_update + $pc92_update_period) {
435                         dbg("ROUTE: sending pc92 update") if isdbg('route');
436                         send_pc92_update();
437                         time_out_pc92_routes();
438                         $last_pc92_update = $main::systime + int rand(180);
439                 }
440                 
441                 $last10 = $t;
442         }
443         
444         if ($main::systime - 3600 > $last_hour) {
445                 $last_hour = $main::systime;
446         }
447 }
448
449 #
450 # finish up a pc context
451 #
452
453 #
454 # some active measures
455 #
456
457
458 sub send_dx_spot
459 {
460         my $self = shift;
461         my $line = shift;
462         my @dxchan = DXChannel::get_all();
463         my $dxchan;
464         
465         # send it if it isn't the except list and isn't isolated and still has a hop count
466         # taking into account filtering and so on
467         foreach $dxchan (@dxchan) {
468                 next if $dxchan == $main::me;
469                 next if $dxchan == $self && $self->is_node;
470                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
471         }
472 }
473
474 sub dx_spot
475 {
476         my $self = shift;
477         my $line = shift;
478         my $isolate = shift;
479         my ($filter, $hops);
480
481         if ($self->{spotsfilter}) {
482                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
483                 return unless $filter;
484         }
485         send_prot_line($self, $filter, $hops, $isolate, $line);
486 }
487
488 sub send_prot_line
489 {
490         my ($self, $filter, $hops, $isolate, $line) = @_;
491         my $routeit;
492
493
494         if ($hops) {
495                 $routeit = $line;
496                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
497         } else {
498                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
499                 return unless $routeit;
500         }
501         if ($filter) {
502                 $self->send($routeit);
503         } else {
504                 $self->send($routeit) unless $self->{isolate} || $isolate;
505         }
506 }
507
508
509 sub send_wwv_spot
510 {
511         my $self = shift;
512         my $line = shift;
513         my @dxchan = DXChannel::get_all();
514         my $dxchan;
515         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
516
517         # send it if it isn't the except list and isn't isolated and still has a hop count
518         # taking into account filtering and so on
519         foreach $dxchan (@dxchan) {
520                 next if $dxchan == $main::me;
521                 next if $dxchan == $self && $self->is_node;
522                 my $routeit;
523                 my ($filter, $hops);
524
525                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
526         }
527 }
528
529 sub wwv
530 {
531         my $self = shift;
532         my $line = shift;
533         my $isolate = shift;
534         my ($filter, $hops);
535         
536         if ($self->{wwvfilter}) {
537                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
538                 return unless $filter;
539         }
540         send_prot_line($self, $filter, $hops, $isolate, $line)
541 }
542
543 sub send_wcy_spot
544 {
545         my $self = shift;
546         my $line = shift;
547         my @dxchan = DXChannel::get_all();
548         my $dxchan;
549         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
550         
551         # send it if it isn't the except list and isn't isolated and still has a hop count
552         # taking into account filtering and so on
553         foreach $dxchan (@dxchan) {
554                 next if $dxchan == $main::me;
555                 next if $dxchan == $self;
556
557                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
558         }
559 }
560
561 sub wcy
562 {
563         my $self = shift;
564         my $line = shift;
565         my $isolate = shift;
566         my ($filter, $hops);
567
568         if ($self->{wcyfilter}) {
569                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
570                 return unless $filter;
571         }
572         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
573 }
574
575 # send an announce
576 sub send_announce
577 {
578         my $self = shift;
579         my $line = shift;
580         my @dxchan = DXChannel::get_all();
581         my $dxchan;
582         my $target;
583         my $to = 'To ';
584         my $text = unpad($_[2]);
585                                 
586         if ($_[3] eq '*') {     # sysops
587                 $target = "SYSOP";
588         } elsif ($_[3] gt ' ') { # speciality list handling
589                 my ($name) = split /\./, $_[3]; 
590                 $target = "$name"; # put the rest in later (if bothered) 
591         } 
592         
593         if ($_[5] eq '1') {
594                 $target = "WX"; 
595                 $to = '';
596         }
597         $target = "ALL" if !$target;
598
599
600         # obtain country codes etc 
601         my @a = Prefix::cty_data($_[0]);
602         my @b = Prefix::cty_data($_[4]);
603         if ($self->{inannfilter}) {
604                 my ($filter, $hops) = 
605                         $self->{inannfilter}->it(@_, $self->{call}, 
606                                                                          @a[0..2],
607                                                                          @b[0..2], $a[3], $b[3]);
608                 unless ($filter) {
609                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
610                         return;
611                 }
612         }
613
614         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
615                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
616                 return;
617         }
618
619         Log('ann', $target, $_[0], $text);
620
621         # send it if it isn't the except list and isn't isolated and still has a hop count
622         # taking into account filtering and so on
623         foreach $dxchan (@dxchan) {
624                 next if $dxchan == $main::me;
625                 next if $dxchan == $self && $self->is_node;
626                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
627                                                   @a[0..2], @b[0..2]);
628         }
629 }
630
631 my $msgid = 0;
632
633 sub nextchatmsgid
634 {
635         $msgid++;
636         $msgid = 1 if $msgid > 999;
637         return $msgid;
638 }
639
640 # send a chat line
641 sub send_chat
642 {
643         my $self = shift;
644         my $line = shift;
645         my @dxchan = DXChannel::get_all();
646         my $dxchan;
647         my $target = $_[3];
648         my $text = unpad($_[2]);
649         my $ak1a_line;
650                                 
651         # munge the group and recast the line if required
652         if ($target =~ s/\.LST$//) {
653                 $ak1a_line = $line;
654         }
655         
656         # obtain country codes etc 
657         my @a = Prefix::cty_data($_[0]);
658         my @b = Prefix::cty_data($_[4]);
659         if ($self->{inannfilter}) {
660                 my ($filter, $hops) = 
661                         $self->{inannfilter}->it(@_, $self->{call}, 
662                                                                          @a[0..2],
663                                                                          @b[0..2], $a[3], $b[3]);
664                 unless ($filter) {
665                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
666                         return;
667                 }
668         }
669
670         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
671                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
672                 return;
673         }
674
675
676         Log('chat', $target, $_[0], $text);
677
678         # send it if it isn't the except list and isn't isolated and still has a hop count
679         # taking into account filtering and so on
680         foreach $dxchan (@dxchan) {
681                 my $is_ak1a = $dxchan->is_ak1a;
682                 
683                 if ($dxchan->is_node) {
684                         next if $dxchan == $main::me;
685                         next if $dxchan == $self;
686                         next unless $dxchan->is_spider || $is_ak1a;
687                         next if $target eq 'LOCAL';
688                         if (!$ak1a_line && $is_ak1a) {
689                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
690                         }
691                 }
692                 
693                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
694                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
695         }
696 }
697
698 sub announce
699 {
700         my $self = shift;
701         my $line = shift;
702         my $isolate = shift;
703         my $to = shift;
704         my $target = shift;
705         my $text = shift;
706         my ($filter, $hops);
707
708         if ($self->{annfilter}) {
709                 ($filter, $hops) = $self->{annfilter}->it(@_);
710                 return unless $filter;
711         }
712         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
713 }
714
715 sub chat
716 {
717         goto &announce;
718 }
719
720
721 sub send_local_config
722 {
723         my $self = shift;
724
725         dbg('DXProt::send_local_config') if isdbg('trace');
726
727         # send our nodes
728         if ($self->{do_pc92}) { 
729                 $self->send_pc92_config;
730         } else {
731                 my $node;
732                 my @nodes;
733                 my @localnodes;
734                 my @remotenodes;
735
736                 if ($self->{isolate}) {
737                         @localnodes = ( $main::routeroot );
738                         $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
739                 } else {
740                         # create a list of all the nodes that are not connected to this connection
741                         # and are not themselves isolated, this to make sure that isolated nodes
742                         # don't appear outside of this node
743
744                         # send locally connected nodes
745                         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
746                         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
747                         $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
748
749                         my $node;
750                         my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
751                         my @intcalls;
752                         for $node (@rawintcalls) {
753                                 push @intcalls, $node unless grep $node eq $_, @intcalls; 
754                         }
755                         my $ref = Route::Node::get($self->{call});
756                         my @rnodes = $ref->nodes;
757                         for $node (@intcalls) {
758                                 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
759                         }
760                         $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
761                 }
762         
763                 # get all the users connected on the above nodes and send them out
764                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
765                         if ($node) {
766                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
767                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
768                         } else {
769                                 dbg("sent a null value") if isdbg('chanerr');
770                         }
771                 }
772         }
773 }
774
775 sub gen_pc92_update
776 {
777         my $self = shift;
778         my $with_pc92_nodes = shift;
779         my $node;
780         my @lines;
781         
782         dbg('ROUTE: DXProt::gen_pc92_update start') if isdbg('route');
783
784         # send 'my' configuration for all users and pc92 capable nodes
785         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all();
786         dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('route');
787         my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
788         dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('route');
789         push @lines, pc92c($main::routeroot, @localnodes);
790
791
792         if ($with_pc92_nodes) {
793                 # send out the configuration of all the directly connected PC92 nodes with current configuration
794                 # but with the dates that the last config came in with.
795                 @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && $_->{do_pc92} } DXChannel::get_all_nodes();
796                 dbg("ROUTE: pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('route');
797                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
798                 dbg("ROUTE: pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('route');
799                 foreach $node (@localnodes) {
800                         if ($node && $node->lastid->{92}) {
801                                 my @rout = map {my $r = Route::get($_); $r ? ($r) : ()} $node->nodes, $node->users;
802                                 push @lines, gen_pc92_with_time($node->call, 'C', $node->lastid->{92}, @rout);
803                         }
804                 }
805         }
806         
807         # send the configuration of all the directly connected 'external' nodes that don't handle PC92
808         # out with the 'external' marker on the first node.
809         @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && !$_->{do_pc92} } DXChannel::get_all_nodes();
810         dbg("ROUTE: non pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('route');
811         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
812         dbg("ROUTE: non pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('route');
813         foreach $node (@localnodes) {
814                 if ($node) {
815                         my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
816                         push @lines, pc92c($node, @rout);
817                 } 
818         }
819
820         dbg('ROUTE: DXProt::gen_pc92_update end') if isdbg('route');
821         return @lines;
822 }
823
824
825 sub send_pc92_config
826 {
827         my $self = shift;
828
829         dbg('DXProt::send_pc92_config') if isdbg('trace');
830
831         my @out = $self->gen_pc92_update(1);
832         
833         # send the complete config out on this interface
834         for (@out) {
835                 $self->send($_);
836         }
837
838
839 sub send_pc92_update
840 {
841         my @out = $main::me->gen_pc92_update(0);
842         
843         # broadcast the lines to all PC92 nodes
844         for (@out) {
845                 $main::me->broadcast_route_pc9x($main::mycall, undef, $_, 0);
846         }
847
848
849 sub time_out_pc92_routes
850 {
851         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc92 || $_->via_pc92)} Route::Node::get_all();
852         my @rdel;
853         foreach my $n (@nodes) {
854                 my $o = $n->dec_obs;
855                 if ($o <= 0) {
856                         if (my $dxchan = DXChannel::get($n->call)) {
857                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('route');
858                                 $dxchan->disconnect;
859                                 next;
860                         }
861                         my @parents = map {Route::Node::get($_)} $n->parents;
862                         for (@parents) {
863                                 if ($_) {
864                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('route');
865                                         push @rdel, $n->del($_);
866                                 }
867                         }
868                 } else {
869                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('route');
870                 }
871         }
872         for (@rdel) {
873                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
874         }
875 }
876
877 #
878 # route a message down an appropriate interface for a callsign
879 #
880 # is called route(to, pcline);
881 #
882
883 sub route
884 {
885         my ($self, $call, $line) = @_;
886
887         if (ref $self && $call eq $self->{call}) {
888                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
889                 return;
890         }
891
892         # always send it down the local interface if available
893         my $dxchan = DXChannel::get($call);
894         if ($dxchan) {
895                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
896         } else {
897                 my $cl = Route::get($call);
898                 $dxchan = $cl->dxchan if $cl;
899                 if (ref $dxchan) {
900                         if (ref $self && $dxchan eq $self) {
901                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
902                                 return;
903                         }
904                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
905                 }
906         }
907
908         # try the backstop method
909         unless ($dxchan) {
910                 my $rcall = RouteDB::get($call);
911                 if ($rcall) {
912                         if ($self && $rcall eq $self->{call}) {
913                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
914                                 return;
915                         }
916                         $dxchan = DXChannel::get($rcall);
917                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
918                 }
919         }
920
921         if ($dxchan) {
922                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
923                 if ($routeit) {
924                         $dxchan->send($routeit) unless $dxchan == $main::me;
925                 }
926         } else {
927                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
928         }
929 }
930
931 #
932 # obtain the hops from the list for this callsign and pc no 
933 #
934
935 sub get_hops
936 {
937         my $pcno = shift;
938         my $hops = $DXProt::hopcount{$pcno};
939         $hops = $DXProt::def_hopcount if !$hops;
940         return "H$hops";       
941 }
942
943
944 # adjust the hop count on a per node basis using the user loadable 
945 # hop table if available or else decrement an existing one
946 #
947
948 sub adjust_hops
949 {
950         my $self = shift;
951         my $s = shift;
952         my $call = $self->{call};
953         my $hops;
954         
955         if (($hops) = $s =~ /\^H(\d+)\^?~?$/o) {
956                 my ($pcno) = $s =~ /^PC(\d\d)/o;
957                 confess "$call called adjust_hops with '$s'" unless $pcno;
958                 my $ref = $nodehops{$call} if %nodehops;
959                 if ($ref) {
960                         my $newhops = $ref->{$pcno};
961                         return "" if defined $newhops && $newhops == 0;
962                         $newhops = $ref->{default} unless $newhops;
963                         return "" if defined $newhops && $newhops == 0;
964                         $newhops = $hops if !$newhops;
965                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
966                 }
967         }
968         return $s;
969 }
970
971
972 # load hop tables
973 #
974 sub load_hops
975 {
976         my $self = shift;
977         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
978         do "$main::data/hop_table.pl";
979         return $@ if $@;
980         return ();
981 }
982
983 sub process_rcmd
984 {
985         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
986         if ($tonode eq $main::mycall) {
987                 my $ref = DXUser->get_current($fromnode);
988                 my $cref = Route::Node::get($fromnode);
989                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
990                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
991                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
992                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
993                                 my $oldpriv = $self->{priv};
994                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
995                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
996                                 $self->{priv} = $oldpriv;
997                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
998                                 delete $self->{remotecmd};
999                         } else {
1000                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1001                         }
1002                 } else {
1003                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1004                 }
1005         } else {
1006                 my $ref = DXUser->get_current($tonode);
1007                 if ($ref && $ref->is_clx) {
1008                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1009                 } else {
1010                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1011                 }
1012         }
1013 }
1014
1015 sub process_rcmd_reply
1016 {
1017         my ($self, $tonode, $fromnode, $user, $line) = @_;
1018         if ($tonode eq $main::mycall) {
1019                 my $s = $rcmds{$fromnode};
1020                 if ($s) {
1021                         my $dxchan = DXChannel::get($s->{call});
1022                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1023                         $ref->send($line) if $ref;
1024                         delete $rcmds{$fromnode} if !$dxchan;
1025                 } else {
1026                         # send unsolicited ones to the sysop
1027                         my $dxchan = DXChannel::get($main::myalias);
1028                         $dxchan->send($line) if $dxchan;
1029                 }
1030         } else {
1031                 my $ref = DXUser->get_current($tonode);
1032                 if ($ref && $ref->is_clx) {
1033                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1034                 } else {
1035                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1036                 }
1037         }
1038 }
1039
1040 sub send_rcmd_reply
1041 {
1042         my $self = shift;
1043         my $tonode = shift;
1044         my $fromnode = shift;
1045         my $user = shift;
1046         while (@_) {
1047                 my $line = shift;
1048                 $line =~ s/\s*$//;
1049                 Log('rcmd', 'out', $fromnode, $line);
1050                 if ($self->is_clx) {
1051                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1052                 } else {
1053                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1054                 }
1055         }
1056 }
1057
1058 # add a rcmd request to the rcmd queues
1059 sub addrcmd
1060 {
1061         my ($self, $to, $cmd) = @_;
1062
1063         my $r = {};
1064         $r->{call} = $self->{call};
1065         $r->{t} = $main::systime;
1066         $r->{cmd} = $cmd;
1067         $rcmds{$to} = $r;
1068         
1069         my $ref = Route::Node::get($to);
1070         my $dxchan = $ref->dxchan;
1071         if ($dxchan && $dxchan->is_clx) {
1072                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1073         } else {
1074                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1075         }
1076 }
1077
1078 sub disconnect
1079 {
1080         my $self = shift;
1081         my $pc39flag = shift;
1082         my $call = $self->call;
1083
1084         return if $self->{disconnecting}++;
1085         
1086         unless ($pc39flag && $pc39flag == 1) {
1087                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1088         }
1089
1090         # get rid of any PC16/17/19
1091         eph_del_regex("^PC1[679]*$call");
1092
1093         # do routing stuff, remove me from routing table
1094         my $node = Route::Node::get($call);
1095         my @rout;
1096         if ($node) {
1097                 @rout = $node->del($main::routeroot);
1098                 
1099                 # and all my ephemera as well
1100                 for (@rout) {
1101                         my $c = $_->call;
1102                         eph_del_regex("^PC1[679].*$c");
1103                 }
1104         }
1105
1106         RouteDB::delete_interface($call);
1107         
1108         # unbusy and stop and outgoing mail
1109         my $mref = DXMsg::get_busy($call);
1110         $mref->stop_msg($call) if $mref;
1111         
1112         # broadcast to all other nodes that all the nodes connected to via me are gone
1113         unless ($pc39flag && $pc39flag == 2)  {
1114                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1115                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1116         }
1117
1118         # remove outstanding pings
1119         delete $pings{$call};
1120         
1121         # I was the last node visited
1122     $self->user->node($main::mycall);
1123
1124         # send info to all logged in thingies
1125         $self->tell_login('logoutn');
1126
1127         Log('DXProt', $call . " Disconnected");
1128
1129         $self->SUPER::disconnect;
1130 }
1131
1132
1133
1134 # send a talk message to this thingy
1135 #
1136 sub talk
1137 {
1138         my ($self, $from, $to, $via, $line, $origin) = @_;
1139         
1140         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1141         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
1142         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
1143 }
1144
1145 # send it if it isn't the except list and isn't isolated and still has a hop count
1146 # taking into account filtering and so on
1147
1148 sub send_route
1149 {
1150         my $self = shift;
1151         my $origin = shift;
1152         my $generate = shift;
1153         my $no = shift;     # the no of things to filter on 
1154         my $routeit;
1155         my ($filter, $hops);
1156         my @rin;
1157         
1158         for (; @_ && $no; $no--) {
1159                 my $r = shift;
1160                 
1161                 if (!$self->{isolate} && $self->{routefilter}) {
1162                         $filter = undef;
1163                         if ($r) {
1164                                 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->{state}, $r->{state});
1165                                 if ($filter) {
1166                                         push @rin, $r;
1167                                 } else {
1168                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1169                                 }
1170                         } else {
1171                                 dbg("was sent a null value") if isdbg('chanerr');
1172                         }
1173                 } else {
1174                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1175                 }
1176         }
1177         if (@rin) {
1178                 foreach my $line (&$generate(@rin, @_)) {
1179                         if ($hops) {
1180                                 $routeit = $line;
1181                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1182                         } else {
1183                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1184                                 next unless $routeit;
1185                         }
1186                         
1187                         $self->send($routeit);
1188                 }
1189         }
1190 }
1191
1192 sub broadcast_route
1193 {
1194         my $self = shift;
1195         my $origin = shift;
1196         my $generate = shift;
1197         my $line = shift;
1198         my @dxchan = DXChannel::get_all_nodes();
1199         my $dxchan;
1200
1201         if ($line) {
1202                 $line =~ /\^H(\d+)\^?\~?$/;
1203                 return unless $1 > 0;
1204         }
1205         unless ($self->{isolate}) {
1206                 foreach $dxchan (@dxchan) {
1207                         next if $dxchan == $self;
1208                         next if $dxchan == $main::me;
1209                         next unless $dxchan->isa('DXProt');
1210                         next if $dxchan->{do_pc92};
1211                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
1212  
1213                         $dxchan->send_route($origin, $generate, @_);
1214                 }
1215         }
1216 }
1217
1218 # this is only used for next door nodes on init
1219 sub send_route_pc92
1220 {
1221         my $self = shift;
1222
1223         return unless $self->{do_pc92};
1224         
1225         my $origin = shift;
1226         my $generate = shift;
1227         my $no = shift;     # the no of things to filter on 
1228         my $line;
1229
1230         $line = &$generate(@_);
1231         $self->send($line);
1232 }
1233
1234 sub broadcast_route_pc9x
1235 {
1236         my $self = shift;
1237         my $origin = shift;
1238         my $generate = shift;
1239         my $line = shift;
1240         my $no = shift;
1241         my @dxchan = DXChannel::get_all_nodes();
1242         my $dxchan;
1243
1244         if ($origin eq $main::mycall && $generate && !$line) {
1245                 $line = &$generate(@_);
1246         } 
1247
1248         $line =~ /\^H(\d+)\^\~?$/;
1249         unless ($1 > 0 && $self->{isolate}) {
1250                 foreach $dxchan (@dxchan) {
1251                         next if $dxchan == $self || $dxchan == $main::me;
1252                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1253                         next unless $dxchan->{do_pc92};
1254                         next unless $dxchan->isa('DXProt');
1255
1256                         $dxchan->send($line);
1257                 }
1258         }
1259 }
1260
1261 sub route_pc16
1262 {
1263         my $self = shift;
1264         return unless $self->user->wantpc16;
1265         my $origin = shift;
1266         my $line = shift;
1267         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
1268 }
1269
1270 sub route_pc17
1271 {
1272         my $self = shift;
1273         return unless $self->user->wantpc16;
1274         my $origin = shift;
1275         my $line = shift;
1276         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
1277 }
1278
1279 sub route_pc19
1280 {
1281         my $self = shift;
1282         my $origin = shift;
1283         my $line = shift;
1284         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
1285 }
1286
1287 sub route_pc21
1288 {
1289         my $self = shift;
1290         my $origin = shift;
1291         my $line = shift;
1292         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
1293 }
1294
1295 sub route_pc24
1296 {
1297         my $self = shift;
1298         my $origin = shift;
1299         my $line = shift;
1300         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1301 }
1302
1303 sub route_pc41
1304 {
1305         my $self = shift;
1306         my $origin = shift;
1307         my $line = shift;
1308         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1309 }
1310
1311 sub route_pc50
1312 {
1313         my $self = shift;
1314         my $origin = shift;
1315         my $line = shift;
1316         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1317 }
1318
1319 sub route_pc92c
1320 {
1321         my $self = shift;
1322         my $origin = shift;
1323         my $line = shift;
1324         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1325 }
1326
1327 sub route_pc92a
1328 {
1329         my $self = shift;
1330         my $origin = shift;
1331         my $line = shift;
1332         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1333 }
1334
1335 sub route_pc92d
1336 {
1337         my $self = shift;
1338         my $origin = shift;
1339         my $line = shift;
1340         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1341 }
1342
1343 sub in_filter_route
1344 {
1345         my $self = shift;
1346         my $r = shift;
1347         my ($filter, $hops) = (1, 1);
1348         
1349         if ($self->{inroutefilter}) {
1350                 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->state, $r->state);
1351                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1352         }
1353         return $filter;
1354 }
1355
1356 sub eph_dup
1357 {
1358         my $s = shift;
1359         my $t = shift || $eph_restime;
1360         my $r;
1361
1362         # chop the end off
1363         $s =~ s/\^H\d\d?\^?\~?$//;
1364         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1365         $eph{$s} = $main::systime + $t;
1366         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
1367         return $r;
1368 }
1369
1370 sub eph_del_regex
1371 {
1372         my $regex = shift;
1373         my ($key, $val);
1374         while (($key, $val) = each %eph) {
1375                 if ($key =~ m{$regex}) {
1376                         delete $eph{$key};
1377                 }
1378         }
1379 }
1380
1381 sub eph_clean
1382 {
1383         my ($key, $val);
1384         
1385         while (($key, $val) = each %eph) {
1386                 if ($main::systime >= $val) {
1387                         delete $eph{$key};
1388                 }
1389         }
1390 }
1391
1392 sub eph_list
1393 {
1394         my ($key, $val);
1395         my @out;
1396
1397         while (($key, $val) = each %eph) {
1398                 push @out, $key, $val;
1399         }
1400         return @out;
1401 }
1402
1403 sub run_cmd
1404 {
1405         goto &DXCommandmode::run_cmd;
1406 }
1407
1408
1409 # import any msgs in the chat directory
1410 # the messages are sent to the chat group which forms the
1411 # the first part of the name (eg: solar.1243.txt would be
1412 # sent to chat group SOLAR)
1413
1414 # Each message found is sent: one non-blank line to one chat
1415 # message. So 4 lines = 4 chat messages.
1416
1417 # The special name LOCAL is for local users ANN
1418 # The special name ALL is for ANN/FULL
1419 # The special name SYSOP is for ANN/SYSOP
1420 #
1421 sub import_chat
1422 {
1423         # are there any to do in this directory?
1424         return unless -d $chatimportfn;
1425         unless (opendir(DIR, $chatimportfn)) {
1426                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1427                 Log('msg', "can\'t open $chatimportfn $!");
1428                 return;
1429         } 
1430
1431         my @names = readdir(DIR);
1432         closedir(DIR);
1433         my $name;
1434         foreach $name (@names) {
1435                 next if $name =~ /^\./;
1436                 my $splitit = $name =~ /^split/;
1437                 my $fn = "$chatimportfn/$name";
1438                 next unless -f $fn;
1439                 unless (open(MSG, $fn)) {
1440                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1441                         Log('msg', "can\'t open import file $fn $!");
1442                         unlink($fn);
1443                         next;
1444                 }
1445                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1446                 close(MSG);
1447                 unlink($fn);
1448
1449                 my @cat = split /\./, $name;
1450                 my $target = uc $cat[0];
1451
1452                 foreach my $text (@msg) {
1453                         next unless $text && $text !~ /^\s*#/;
1454                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
1455                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
1456                                 if ($target ne 'LOCAL') {
1457                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
1458                                 } else {
1459                                         Log('ann', 'LOCAL', $main::mycall, $text);
1460                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
1461                                 }
1462                         } else {
1463                                 my $msgid = nextchatmsgid();
1464                                 $text = "#$msgid $text";
1465                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
1466                         }
1467                 }
1468         }
1469 }
1470
1471 1;
1472 __END__