baaea72dde04b32fe8edd7565d935c5dd288264a
[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_my_pc92_config
776 {
777         my $node = shift;
778         
779         if ($node->{call} eq $main::mycall) {
780                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
781                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
782                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
783                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
784                 return pc92c($main::routeroot, @localnodes);
785         } else {
786                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
787                 return pc92c($node, @rout);
788         }
789 }
790
791 sub gen_pc92_update
792 {
793         my $self = shift;
794         my $with_pc92_nodes = shift;
795         my $node;
796         my @lines;
797         my @dxchan;
798         my @localnodes;
799
800         dbg('ROUTE: DXProt::gen_pc92_update start') if isdbg('routelow');
801
802         # send 'my' configuration for all channels
803         push @lines, gen_my_pc92_config($main::routeroot);
804         
805         if ($with_pc92_nodes) {
806                 # send out the configuration of all the directly connected PC92 nodes with current configuration
807                 # but with the dates that the last config came in with.
808                 @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && $_->{do_pc92} } DXChannel::get_all_nodes();
809                 dbg("ROUTE: pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
810                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
811                 dbg("ROUTE: pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
812                 foreach $node (@localnodes) {
813                         if ($node && $node->lastid->{92}) {
814                                 my @rout = map {my $r = Route::get($_); $r ? ($r) : ()} $node->nodes, $node->users;
815                                 push @lines, gen_pc92_with_time($node->call, 'C', $node->lastid->{92}, @rout);
816                         }
817                 }
818         }
819         
820         # send the configuration of all the directly connected 'external' nodes that don't handle PC92
821         # out with the 'external' marker on the first node.
822         @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && !$_->{do_pc92} } DXChannel::get_all_nodes();
823         dbg("ROUTE: non pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
824         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
825         dbg("ROUTE: non pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
826         foreach $node (@localnodes) {
827                 if ($node) {
828                         push @lines, gen_my_pc92_config($node);
829                 } 
830         }
831
832         dbg('ROUTE: DXProt::gen_pc92_update end with ' . scalar @lines . ' lines') if isdbg('routelow');
833         return @lines;
834 }
835
836
837 sub send_pc92_config
838 {
839         my $self = shift;
840
841         dbg('DXProt::send_pc92_config') if isdbg('trace');
842
843         my @out = $self->gen_pc92_update(1);
844         
845         # send the complete config out on this interface
846         for (@out) {
847                 $self->send($_);
848         }
849
850
851 sub send_pc92_update
852 {
853         my @out = $main::me->gen_pc92_update(0);
854         
855         # broadcast the lines to all PC92 nodes
856         for (@out) {
857                 $main::me->broadcast_route_pc9x($main::mycall, undef, $_, 0);
858         }
859
860
861 sub time_out_pc92_routes
862 {
863         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc92 || $_->via_pc92)} Route::Node::get_all();
864         my @rdel;
865         foreach my $n (@nodes) {
866                 my $o = $n->dec_obs;
867                 if ($o <= 0) {
868                         if (my $dxchan = DXChannel::get($n->call)) {
869                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('route');
870                                 $dxchan->disconnect;
871                                 next;
872                         }
873                         my @parents = map {Route::Node::get($_)} $n->parents;
874                         for (@parents) {
875                                 if ($_) {
876                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('route');
877                                         push @rdel, $n->del($_);
878                                 }
879                         }
880                 } else {
881                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('route');
882                 }
883         }
884         for (@rdel) {
885                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
886         }
887 }
888
889 #
890 # route a message down an appropriate interface for a callsign
891 #
892 # is called route(to, pcline);
893 #
894
895 sub route
896 {
897         my ($self, $call, $line) = @_;
898
899         if (ref $self && $call eq $self->{call}) {
900                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
901                 return;
902         }
903
904         # always send it down the local interface if available
905         my $dxchan = DXChannel::get($call);
906         if ($dxchan) {
907                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
908         } else {
909                 my $cl = Route::get($call);
910                 $dxchan = $cl->dxchan if $cl;
911                 if (ref $dxchan) {
912                         if (ref $self && $dxchan eq $self) {
913                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
914                                 return;
915                         }
916                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
917                 }
918         }
919
920         # try the backstop method
921         unless ($dxchan) {
922                 my $rcall = RouteDB::get($call);
923                 if ($rcall) {
924                         if ($self && $rcall eq $self->{call}) {
925                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
926                                 return;
927                         }
928                         $dxchan = DXChannel::get($rcall);
929                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
930                 }
931         }
932
933         if ($dxchan) {
934                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
935                 if ($routeit) {
936                         $dxchan->send($routeit) unless $dxchan == $main::me;
937                 }
938         } else {
939                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
940         }
941 }
942
943 #
944 # obtain the hops from the list for this callsign and pc no 
945 #
946
947 sub get_hops
948 {
949         my $pcno = shift;
950         my $hops = $DXProt::hopcount{$pcno};
951         $hops = $DXProt::def_hopcount if !$hops;
952         return "H$hops";       
953 }
954
955
956 # adjust the hop count on a per node basis using the user loadable 
957 # hop table if available or else decrement an existing one
958 #
959
960 sub adjust_hops
961 {
962         my $self = shift;
963         my $s = shift;
964         my $call = $self->{call};
965         my $hops;
966         
967         if (($hops) = $s =~ /\^H(\d+)\^?~?$/o) {
968                 my ($pcno) = $s =~ /^PC(\d\d)/o;
969                 confess "$call called adjust_hops with '$s'" unless $pcno;
970                 my $ref = $nodehops{$call} if %nodehops;
971                 if ($ref) {
972                         my $newhops = $ref->{$pcno};
973                         return "" if defined $newhops && $newhops == 0;
974                         $newhops = $ref->{default} unless $newhops;
975                         return "" if defined $newhops && $newhops == 0;
976                         $newhops = $hops if !$newhops;
977                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
978                 }
979         }
980         return $s;
981 }
982
983
984 # load hop tables
985 #
986 sub load_hops
987 {
988         my $self = shift;
989         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
990         do "$main::data/hop_table.pl";
991         return $@ if $@;
992         return ();
993 }
994
995 sub process_rcmd
996 {
997         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
998         if ($tonode eq $main::mycall) {
999                 my $ref = DXUser->get_current($fromnode);
1000                 my $cref = Route::Node::get($fromnode);
1001                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1002                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1003                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1004                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1005                                 my $oldpriv = $self->{priv};
1006                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1007                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1008                                 $self->{priv} = $oldpriv;
1009                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1010                                 delete $self->{remotecmd};
1011                         } else {
1012                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1013                         }
1014                 } else {
1015                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1016                 }
1017         } else {
1018                 my $ref = DXUser->get_current($tonode);
1019                 if ($ref && $ref->is_clx) {
1020                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1021                 } else {
1022                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1023                 }
1024         }
1025 }
1026
1027 sub process_rcmd_reply
1028 {
1029         my ($self, $tonode, $fromnode, $user, $line) = @_;
1030         if ($tonode eq $main::mycall) {
1031                 my $s = $rcmds{$fromnode};
1032                 if ($s) {
1033                         my $dxchan = DXChannel::get($s->{call});
1034                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1035                         $ref->send($line) if $ref;
1036                         delete $rcmds{$fromnode} if !$dxchan;
1037                 } else {
1038                         # send unsolicited ones to the sysop
1039                         my $dxchan = DXChannel::get($main::myalias);
1040                         $dxchan->send($line) if $dxchan;
1041                 }
1042         } else {
1043                 my $ref = DXUser->get_current($tonode);
1044                 if ($ref && $ref->is_clx) {
1045                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1046                 } else {
1047                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1048                 }
1049         }
1050 }
1051
1052 sub send_rcmd_reply
1053 {
1054         my $self = shift;
1055         my $tonode = shift;
1056         my $fromnode = shift;
1057         my $user = shift;
1058         while (@_) {
1059                 my $line = shift;
1060                 $line =~ s/\s*$//;
1061                 Log('rcmd', 'out', $fromnode, $line);
1062                 if ($self->is_clx) {
1063                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1064                 } else {
1065                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1066                 }
1067         }
1068 }
1069
1070 # add a rcmd request to the rcmd queues
1071 sub addrcmd
1072 {
1073         my ($self, $to, $cmd) = @_;
1074
1075         my $r = {};
1076         $r->{call} = $self->{call};
1077         $r->{t} = $main::systime;
1078         $r->{cmd} = $cmd;
1079         $rcmds{$to} = $r;
1080         
1081         my $ref = Route::Node::get($to);
1082         my $dxchan = $ref->dxchan;
1083         if ($dxchan && $dxchan->is_clx) {
1084                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1085         } else {
1086                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1087         }
1088 }
1089
1090 sub disconnect
1091 {
1092         my $self = shift;
1093         my $pc39flag = shift;
1094         my $call = $self->call;
1095
1096         return if $self->{disconnecting}++;
1097         
1098         unless ($pc39flag && $pc39flag == 1) {
1099                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1100         }
1101
1102         # get rid of any PC16/17/19
1103         eph_del_regex("^PC1[679]*$call");
1104
1105         # do routing stuff, remove me from routing table
1106         my $node = Route::Node::get($call);
1107         my @rout;
1108         if ($node) {
1109                 @rout = $node->del($main::routeroot);
1110                 
1111                 # and all my ephemera as well
1112                 for (@rout) {
1113                         my $c = $_->call;
1114                         eph_del_regex("^PC1[679].*$c");
1115                 }
1116         }
1117
1118         RouteDB::delete_interface($call);
1119         
1120         # unbusy and stop and outgoing mail
1121         my $mref = DXMsg::get_busy($call);
1122         $mref->stop_msg($call) if $mref;
1123         
1124         # broadcast to all other nodes that all the nodes connected to via me are gone
1125         unless ($pc39flag && $pc39flag == 2)  {
1126                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1127                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1128         }
1129
1130         # remove outstanding pings
1131         delete $pings{$call};
1132         
1133         # I was the last node visited
1134     $self->user->node($main::mycall);
1135
1136         # send info to all logged in thingies
1137         $self->tell_login('logoutn');
1138
1139         Log('DXProt', $call . " Disconnected");
1140
1141         $self->SUPER::disconnect;
1142 }
1143
1144
1145
1146 # send a talk message to this thingy
1147 #
1148 sub talk
1149 {
1150         my ($self, $from, $to, $via, $line, $origin) = @_;
1151         
1152         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1153         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
1154         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
1155 }
1156
1157 # send it if it isn't the except list and isn't isolated and still has a hop count
1158 # taking into account filtering and so on
1159
1160 sub send_route
1161 {
1162         my $self = shift;
1163         my $origin = shift;
1164         my $generate = shift;
1165         my $no = shift;     # the no of things to filter on 
1166         my $routeit;
1167         my ($filter, $hops);
1168         my @rin;
1169         
1170         for (; @_ && $no; $no--) {
1171                 my $r = shift;
1172                 
1173                 if (!$self->{isolate} && $self->{routefilter}) {
1174                         $filter = undef;
1175                         if ($r) {
1176                                 ($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});
1177                                 if ($filter) {
1178                                         push @rin, $r;
1179                                 } else {
1180                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1181                                 }
1182                         } else {
1183                                 dbg("was sent a null value") if isdbg('chanerr');
1184                         }
1185                 } else {
1186                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1187                 }
1188         }
1189         if (@rin) {
1190                 foreach my $line (&$generate(@rin, @_)) {
1191                         if ($hops) {
1192                                 $routeit = $line;
1193                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1194                         } else {
1195                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1196                                 next unless $routeit;
1197                         }
1198                         
1199                         $self->send($routeit);
1200                 }
1201         }
1202 }
1203
1204 sub broadcast_route
1205 {
1206         my $self = shift;
1207         my $origin = shift;
1208         my $generate = shift;
1209         my $line = shift;
1210         my @dxchan = DXChannel::get_all_nodes();
1211         my $dxchan;
1212
1213         if ($line) {
1214                 $line =~ /\^H(\d+)\^?\~?$/;
1215                 return unless $1 > 0;
1216         }
1217         unless ($self->{isolate}) {
1218                 foreach $dxchan (@dxchan) {
1219                         next if $dxchan == $self;
1220                         next if $dxchan == $main::me;
1221                         next unless $dxchan->isa('DXProt');
1222                         next if $dxchan->{do_pc92};
1223                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
1224  
1225                         $dxchan->send_route($origin, $generate, @_);
1226                 }
1227         }
1228 }
1229
1230 # this is only used for next door nodes on init
1231 sub send_route_pc92
1232 {
1233         my $self = shift;
1234
1235         return unless $self->{do_pc92};
1236         
1237         my $origin = shift;
1238         my $generate = shift;
1239         my $no = shift;     # the no of things to filter on 
1240         my $line;
1241
1242         $line = &$generate(@_);
1243         $self->send($line);
1244 }
1245
1246 sub broadcast_route_pc9x
1247 {
1248         my $self = shift;
1249         my $origin = shift;
1250         my $generate = shift;
1251         my $line = shift;
1252         my $no = shift;
1253         my @dxchan = DXChannel::get_all_nodes();
1254         my $dxchan;
1255
1256         if ($origin eq $main::mycall && $generate && !$line) {
1257                 $line = &$generate(@_);
1258         } 
1259
1260         $line =~ /\^H(\d+)\^\~?$/;
1261         unless ($1 > 0 && $self->{isolate}) {
1262                 foreach $dxchan (@dxchan) {
1263                         next if $dxchan == $self || $dxchan == $main::me;
1264                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1265                         next unless $dxchan->{do_pc92};
1266                         next unless $dxchan->isa('DXProt');
1267
1268                         $dxchan->send($line);
1269                 }
1270         }
1271 }
1272
1273 sub route_pc16
1274 {
1275         my $self = shift;
1276         return unless $self->user->wantpc16;
1277         my $origin = shift;
1278         my $line = shift;
1279         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
1280 }
1281
1282 sub route_pc17
1283 {
1284         my $self = shift;
1285         return unless $self->user->wantpc16;
1286         my $origin = shift;
1287         my $line = shift;
1288         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
1289 }
1290
1291 sub route_pc19
1292 {
1293         my $self = shift;
1294         my $origin = shift;
1295         my $line = shift;
1296         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
1297 }
1298
1299 sub route_pc21
1300 {
1301         my $self = shift;
1302         my $origin = shift;
1303         my $line = shift;
1304         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
1305 }
1306
1307 sub route_pc24
1308 {
1309         my $self = shift;
1310         my $origin = shift;
1311         my $line = shift;
1312         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1313 }
1314
1315 sub route_pc41
1316 {
1317         my $self = shift;
1318         my $origin = shift;
1319         my $line = shift;
1320         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1321 }
1322
1323 sub route_pc50
1324 {
1325         my $self = shift;
1326         my $origin = shift;
1327         my $line = shift;
1328         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1329 }
1330
1331 sub route_pc92c
1332 {
1333         my $self = shift;
1334         my $origin = shift;
1335         my $line = shift;
1336         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1337 }
1338
1339 sub route_pc92a
1340 {
1341         my $self = shift;
1342         my $origin = shift;
1343         my $line = shift;
1344         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1345 }
1346
1347 sub route_pc92d
1348 {
1349         my $self = shift;
1350         my $origin = shift;
1351         my $line = shift;
1352         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1353 }
1354
1355 sub in_filter_route
1356 {
1357         my $self = shift;
1358         my $r = shift;
1359         my ($filter, $hops) = (1, 1);
1360         
1361         if ($self->{inroutefilter}) {
1362                 ($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);
1363                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1364         }
1365         return $filter;
1366 }
1367
1368 sub eph_dup
1369 {
1370         my $s = shift;
1371         my $t = shift || $eph_restime;
1372         my $r;
1373
1374         # chop the end off
1375         $s =~ s/\^H\d\d?\^?\~?$//;
1376         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1377         $eph{$s} = $main::systime + $t;
1378         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
1379         return $r;
1380 }
1381
1382 sub eph_del_regex
1383 {
1384         my $regex = shift;
1385         my ($key, $val);
1386         while (($key, $val) = each %eph) {
1387                 if ($key =~ m{$regex}) {
1388                         delete $eph{$key};
1389                 }
1390         }
1391 }
1392
1393 sub eph_clean
1394 {
1395         my ($key, $val);
1396         
1397         while (($key, $val) = each %eph) {
1398                 if ($main::systime >= $val) {
1399                         delete $eph{$key};
1400                 }
1401         }
1402 }
1403
1404 sub eph_list
1405 {
1406         my ($key, $val);
1407         my @out;
1408
1409         while (($key, $val) = each %eph) {
1410                 push @out, $key, $val;
1411         }
1412         return @out;
1413 }
1414
1415 sub run_cmd
1416 {
1417         goto &DXCommandmode::run_cmd;
1418 }
1419
1420
1421 # import any msgs in the chat directory
1422 # the messages are sent to the chat group which forms the
1423 # the first part of the name (eg: solar.1243.txt would be
1424 # sent to chat group SOLAR)
1425
1426 # Each message found is sent: one non-blank line to one chat
1427 # message. So 4 lines = 4 chat messages.
1428
1429 # The special name LOCAL is for local users ANN
1430 # The special name ALL is for ANN/FULL
1431 # The special name SYSOP is for ANN/SYSOP
1432 #
1433 sub import_chat
1434 {
1435         # are there any to do in this directory?
1436         return unless -d $chatimportfn;
1437         unless (opendir(DIR, $chatimportfn)) {
1438                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1439                 Log('msg', "can\'t open $chatimportfn $!");
1440                 return;
1441         } 
1442
1443         my @names = readdir(DIR);
1444         closedir(DIR);
1445         my $name;
1446         foreach $name (@names) {
1447                 next if $name =~ /^\./;
1448                 my $splitit = $name =~ /^split/;
1449                 my $fn = "$chatimportfn/$name";
1450                 next unless -f $fn;
1451                 unless (open(MSG, $fn)) {
1452                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1453                         Log('msg', "can\'t open import file $fn $!");
1454                         unlink($fn);
1455                         next;
1456                 }
1457                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1458                 close(MSG);
1459                 unlink($fn);
1460
1461                 my @cat = split /\./, $name;
1462                 my $target = uc $cat[0];
1463
1464                 foreach my $text (@msg) {
1465                         next unless $text && $text !~ /^\s*#/;
1466                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
1467                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
1468                                 if ($target ne 'LOCAL') {
1469                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
1470                                 } else {
1471                                         Log('ann', 'LOCAL', $main::mycall, $text);
1472                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
1473                                 }
1474                         } else {
1475                                 my $msgid = nextchatmsgid();
1476                                 $text = "#$msgid $text";
1477                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
1478                         }
1479                 }
1480         }
1481 }
1482
1483 1;
1484 __END__