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