add more debugging and (possibly) fix the problem.
[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                         send_pc92_update();
436                         time_out_pc92_routes();
437                         $last_pc92_update = $main::systime + int rand(180);
438                 }
439                 
440                 $last10 = $t;
441         }
442         
443         if ($main::systime - 3600 > $last_hour) {
444                 $last_hour = $main::systime;
445         }
446 }
447
448 #
449 # finish up a pc context
450 #
451
452 #
453 # some active measures
454 #
455
456
457 sub send_dx_spot
458 {
459         my $self = shift;
460         my $line = shift;
461         my @dxchan = DXChannel::get_all();
462         my $dxchan;
463         
464         # send it if it isn't the except list and isn't isolated and still has a hop count
465         # taking into account filtering and so on
466         foreach $dxchan (@dxchan) {
467                 next if $dxchan == $main::me;
468                 next if $dxchan == $self && $self->is_node;
469                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
470         }
471 }
472
473 sub dx_spot
474 {
475         my $self = shift;
476         my $line = shift;
477         my $isolate = shift;
478         my ($filter, $hops);
479
480         if ($self->{spotsfilter}) {
481                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
482                 return unless $filter;
483         }
484         send_prot_line($self, $filter, $hops, $isolate, $line);
485 }
486
487 sub send_prot_line
488 {
489         my ($self, $filter, $hops, $isolate, $line) = @_;
490         my $routeit;
491
492
493         if ($hops) {
494                 $routeit = $line;
495                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
496         } else {
497                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
498                 return unless $routeit;
499         }
500         if ($filter) {
501                 $self->send($routeit);
502         } else {
503                 $self->send($routeit) unless $self->{isolate} || $isolate;
504         }
505 }
506
507
508 sub send_wwv_spot
509 {
510         my $self = shift;
511         my $line = shift;
512         my @dxchan = DXChannel::get_all();
513         my $dxchan;
514         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
515
516         # send it if it isn't the except list and isn't isolated and still has a hop count
517         # taking into account filtering and so on
518         foreach $dxchan (@dxchan) {
519                 next if $dxchan == $main::me;
520                 next if $dxchan == $self && $self->is_node;
521                 my $routeit;
522                 my ($filter, $hops);
523
524                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
525         }
526 }
527
528 sub wwv
529 {
530         my $self = shift;
531         my $line = shift;
532         my $isolate = shift;
533         my ($filter, $hops);
534         
535         if ($self->{wwvfilter}) {
536                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
537                 return unless $filter;
538         }
539         send_prot_line($self, $filter, $hops, $isolate, $line)
540 }
541
542 sub send_wcy_spot
543 {
544         my $self = shift;
545         my $line = shift;
546         my @dxchan = DXChannel::get_all();
547         my $dxchan;
548         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
549         
550         # send it if it isn't the except list and isn't isolated and still has a hop count
551         # taking into account filtering and so on
552         foreach $dxchan (@dxchan) {
553                 next if $dxchan == $main::me;
554                 next if $dxchan == $self;
555
556                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
557         }
558 }
559
560 sub wcy
561 {
562         my $self = shift;
563         my $line = shift;
564         my $isolate = shift;
565         my ($filter, $hops);
566
567         if ($self->{wcyfilter}) {
568                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
569                 return unless $filter;
570         }
571         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
572 }
573
574 # send an announce
575 sub send_announce
576 {
577         my $self = shift;
578         my $line = shift;
579         my @dxchan = DXChannel::get_all();
580         my $dxchan;
581         my $target;
582         my $to = 'To ';
583         my $text = unpad($_[2]);
584                                 
585         if ($_[3] eq '*') {     # sysops
586                 $target = "SYSOP";
587         } elsif ($_[3] gt ' ') { # speciality list handling
588                 my ($name) = split /\./, $_[3]; 
589                 $target = "$name"; # put the rest in later (if bothered) 
590         } 
591         
592         if ($_[5] eq '1') {
593                 $target = "WX"; 
594                 $to = '';
595         }
596         $target = "ALL" if !$target;
597
598
599         # obtain country codes etc 
600         my @a = Prefix::cty_data($_[0]);
601         my @b = Prefix::cty_data($_[4]);
602         if ($self->{inannfilter}) {
603                 my ($filter, $hops) = 
604                         $self->{inannfilter}->it(@_, $self->{call}, 
605                                                                          @a[0..2],
606                                                                          @b[0..2], $a[3], $b[3]);
607                 unless ($filter) {
608                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
609                         return;
610                 }
611         }
612
613         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
614                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
615                 return;
616         }
617
618         Log('ann', $target, $_[0], $text);
619
620         # send it if it isn't the except list and isn't isolated and still has a hop count
621         # taking into account filtering and so on
622         foreach $dxchan (@dxchan) {
623                 next if $dxchan == $main::me;
624                 next if $dxchan == $self && $self->is_node;
625                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
626                                                   @a[0..2], @b[0..2]);
627         }
628 }
629
630 my $msgid = 0;
631
632 sub nextchatmsgid
633 {
634         $msgid++;
635         $msgid = 1 if $msgid > 999;
636         return $msgid;
637 }
638
639 # send a chat line
640 sub send_chat
641 {
642         my $self = shift;
643         my $line = shift;
644         my @dxchan = DXChannel::get_all();
645         my $dxchan;
646         my $target = $_[3];
647         my $text = unpad($_[2]);
648         my $ak1a_line;
649                                 
650         # munge the group and recast the line if required
651         if ($target =~ s/\.LST$//) {
652                 $ak1a_line = $line;
653         }
654         
655         # obtain country codes etc 
656         my @a = Prefix::cty_data($_[0]);
657         my @b = Prefix::cty_data($_[4]);
658         if ($self->{inannfilter}) {
659                 my ($filter, $hops) = 
660                         $self->{inannfilter}->it(@_, $self->{call}, 
661                                                                          @a[0..2],
662                                                                          @b[0..2], $a[3], $b[3]);
663                 unless ($filter) {
664                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
665                         return;
666                 }
667         }
668
669         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
670                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
671                 return;
672         }
673
674
675         Log('chat', $target, $_[0], $text);
676
677         # send it if it isn't the except list and isn't isolated and still has a hop count
678         # taking into account filtering and so on
679         foreach $dxchan (@dxchan) {
680                 my $is_ak1a = $dxchan->is_ak1a;
681                 
682                 if ($dxchan->is_node) {
683                         next if $dxchan == $main::me;
684                         next if $dxchan == $self;
685                         next unless $dxchan->is_spider || $is_ak1a;
686                         next if $target eq 'LOCAL';
687                         if (!$ak1a_line && $is_ak1a) {
688                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
689                         }
690                 }
691                 
692                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
693                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
694         }
695 }
696
697 sub announce
698 {
699         my $self = shift;
700         my $line = shift;
701         my $isolate = shift;
702         my $to = shift;
703         my $target = shift;
704         my $text = shift;
705         my ($filter, $hops);
706
707         if ($self->{annfilter}) {
708                 ($filter, $hops) = $self->{annfilter}->it(@_);
709                 return unless $filter;
710         }
711         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
712 }
713
714 sub chat
715 {
716         goto &announce;
717 }
718
719
720 sub send_local_config
721 {
722         my $self = shift;
723
724         dbg('DXProt::send_local_config') if isdbg('trace');
725
726         # send our nodes
727         if ($self->{do_pc92}) { 
728                 $self->send_pc92_config;
729         } else {
730                 my $node;
731                 my @nodes;
732                 my @localnodes;
733                 my @remotenodes;
734
735                 if ($self->{isolate}) {
736                         @localnodes = ( $main::routeroot );
737                         $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
738                 } else {
739                         # create a list of all the nodes that are not connected to this connection
740                         # and are not themselves isolated, this to make sure that isolated nodes
741                         # don't appear outside of this node
742
743                         # send locally connected nodes
744                         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
745                         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
746                         $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
747
748                         my $node;
749                         my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
750                         my @intcalls;
751                         for $node (@rawintcalls) {
752                                 push @intcalls, $node unless grep $node eq $_, @intcalls; 
753                         }
754                         my $ref = Route::Node::get($self->{call});
755                         my @rnodes = $ref->nodes;
756                         for $node (@intcalls) {
757                                 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
758                         }
759                         $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
760                 }
761         
762                 # get all the users connected on the above nodes and send them out
763                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
764                         if ($node) {
765                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
766                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
767                         } else {
768                                 dbg("sent a null value") if isdbg('chanerr');
769                         }
770                 }
771         }
772 }
773
774 sub gen_pc92_update
775 {
776         my $self = shift;
777         my $with_pc92_nodes = shift;
778         my $node;
779         my @lines;
780         
781         dbg('DXProt::send_pc92_update') if isdbg('trace');
782
783         # send 'my' configuration for all users and pc92 capable nodes
784         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all();
785         my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
786         push @lines, pc92c($main::routeroot, @localnodes);
787
788
789         if ($with_pc92_nodes) {
790                 # send out the configuration of all the directly connected PC92 nodes with current configuration
791                 # but with the dates that the last config came in with.
792                 @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && $_->{do_pc92} } DXChannel::get_all_nodes();
793                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
794                 foreach $node (@localnodes) {
795                         if ($node && $node->lastid->{92}) {
796                                 my @rout = map {my $r = Route::get($_); $r ? ($r) : ()} $node->nodes, $node->users;
797                                 push @lines, gen_pc92_with_time($node->call, 'C', $node->lastid->{92}, @rout);
798                         }
799                 }
800         }
801         
802         # send the configuration of all the directly connected 'external' nodes that don't handle PC92
803         # out with the 'external' marker on the first node.
804         @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && !$_->{do_pc92} } DXChannel::get_all_nodes();
805         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
806         foreach $node (@localnodes) {
807                 if ($node) {
808                         my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
809                         push @lines, pc92c($node, @rout);
810                 } 
811         }
812         return @lines;
813 }
814
815
816 sub send_pc92_config
817 {
818         my $self = shift;
819
820         dbg('DXProt::send_pc92_config') if isdbg('trace');
821
822         my @out = $self->gen_pc92_update(1);
823         
824         # send the complete config out on this interface
825         for (@out) {
826                 $self->send($_);
827         }
828
829
830 sub send_pc92_update
831 {
832         my @out = $main::me->gen_pc92_update(0);
833         
834         # broadcast the lines to all PC92 nodes
835         for (@out) {
836                 $main::me->broadcast_route_pc9x('', undef, $_, 0);
837         }
838
839
840 sub time_out_pc92_routes
841 {
842         my @nodes = grep {$_->do_pc92 || $_->via_pc92} Route::Node::get_all();
843         my @rdel;
844         foreach my $n (@nodes) {
845                 my $o = $n->dec_obs;
846                 if ($o <= 0) {
847                         if (my $dxchan = DXChannel::get($n->call)) {
848                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('route');
849                                 $dxchan->disconnect;
850                                 next;
851                         }
852                         my @parents = map {Route::Node::get($_)} $n->parents;
853                         for (@parents) {
854                                 if ($_) {
855                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('route');
856                                         push @rdel, $n->del($_);
857                                 }
858                         }
859                 } else {
860                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('route');
861                 }
862         }
863         for (@rdel) {
864                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
865         }
866 }
867
868 #
869 # route a message down an appropriate interface for a callsign
870 #
871 # is called route(to, pcline);
872 #
873
874 sub route
875 {
876         my ($self, $call, $line) = @_;
877
878         if (ref $self && $call eq $self->{call}) {
879                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
880                 return;
881         }
882
883         # always send it down the local interface if available
884         my $dxchan = DXChannel::get($call);
885         if ($dxchan) {
886                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
887         } else {
888                 my $cl = Route::get($call);
889                 $dxchan = $cl->dxchan if $cl;
890                 if (ref $dxchan) {
891                         if (ref $self && $dxchan eq $self) {
892                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
893                                 return;
894                         }
895                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
896                 }
897         }
898
899         # try the backstop method
900         unless ($dxchan) {
901                 my $rcall = RouteDB::get($call);
902                 if ($rcall) {
903                         if ($self && $rcall eq $self->{call}) {
904                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
905                                 return;
906                         }
907                         $dxchan = DXChannel::get($rcall);
908                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
909                 }
910         }
911
912         if ($dxchan) {
913                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
914                 if ($routeit) {
915                         $dxchan->send($routeit) unless $dxchan == $main::me;
916                 }
917         } else {
918                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
919         }
920 }
921
922 #
923 # obtain the hops from the list for this callsign and pc no 
924 #
925
926 sub get_hops
927 {
928         my $pcno = shift;
929         my $hops = $DXProt::hopcount{$pcno};
930         $hops = $DXProt::def_hopcount if !$hops;
931         return "H$hops";       
932 }
933
934
935 # adjust the hop count on a per node basis using the user loadable 
936 # hop table if available or else decrement an existing one
937 #
938
939 sub adjust_hops
940 {
941         my $self = shift;
942         my $s = shift;
943         my $call = $self->{call};
944         my $hops;
945         
946         if (($hops) = $s =~ /\^H(\d+)\^?~?$/o) {
947                 my ($pcno) = $s =~ /^PC(\d\d)/o;
948                 confess "$call called adjust_hops with '$s'" unless $pcno;
949                 my $ref = $nodehops{$call} if %nodehops;
950                 if ($ref) {
951                         my $newhops = $ref->{$pcno};
952                         return "" if defined $newhops && $newhops == 0;
953                         $newhops = $ref->{default} unless $newhops;
954                         return "" if defined $newhops && $newhops == 0;
955                         $newhops = $hops if !$newhops;
956                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
957                 }
958         }
959         return $s;
960 }
961
962
963 # load hop tables
964 #
965 sub load_hops
966 {
967         my $self = shift;
968         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
969         do "$main::data/hop_table.pl";
970         return $@ if $@;
971         return ();
972 }
973
974 sub process_rcmd
975 {
976         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
977         if ($tonode eq $main::mycall) {
978                 my $ref = DXUser->get_current($fromnode);
979                 my $cref = Route::Node::get($fromnode);
980                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
981                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
982                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
983                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
984                                 my $oldpriv = $self->{priv};
985                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
986                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
987                                 $self->{priv} = $oldpriv;
988                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
989                                 delete $self->{remotecmd};
990                         } else {
991                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
992                         }
993                 } else {
994                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
995                 }
996         } else {
997                 my $ref = DXUser->get_current($tonode);
998                 if ($ref && $ref->is_clx) {
999                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1000                 } else {
1001                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1002                 }
1003         }
1004 }
1005
1006 sub process_rcmd_reply
1007 {
1008         my ($self, $tonode, $fromnode, $user, $line) = @_;
1009         if ($tonode eq $main::mycall) {
1010                 my $s = $rcmds{$fromnode};
1011                 if ($s) {
1012                         my $dxchan = DXChannel::get($s->{call});
1013                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1014                         $ref->send($line) if $ref;
1015                         delete $rcmds{$fromnode} if !$dxchan;
1016                 } else {
1017                         # send unsolicited ones to the sysop
1018                         my $dxchan = DXChannel::get($main::myalias);
1019                         $dxchan->send($line) if $dxchan;
1020                 }
1021         } else {
1022                 my $ref = DXUser->get_current($tonode);
1023                 if ($ref && $ref->is_clx) {
1024                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1025                 } else {
1026                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1027                 }
1028         }
1029 }
1030
1031 sub send_rcmd_reply
1032 {
1033         my $self = shift;
1034         my $tonode = shift;
1035         my $fromnode = shift;
1036         my $user = shift;
1037         while (@_) {
1038                 my $line = shift;
1039                 $line =~ s/\s*$//;
1040                 Log('rcmd', 'out', $fromnode, $line);
1041                 if ($self->is_clx) {
1042                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1043                 } else {
1044                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1045                 }
1046         }
1047 }
1048
1049 # add a rcmd request to the rcmd queues
1050 sub addrcmd
1051 {
1052         my ($self, $to, $cmd) = @_;
1053
1054         my $r = {};
1055         $r->{call} = $self->{call};
1056         $r->{t} = $main::systime;
1057         $r->{cmd} = $cmd;
1058         $rcmds{$to} = $r;
1059         
1060         my $ref = Route::Node::get($to);
1061         my $dxchan = $ref->dxchan;
1062         if ($dxchan && $dxchan->is_clx) {
1063                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1064         } else {
1065                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1066         }
1067 }
1068
1069 sub disconnect
1070 {
1071         my $self = shift;
1072         my $pc39flag = shift;
1073         my $call = $self->call;
1074
1075         return if $self->{disconnecting}++;
1076         
1077         unless ($pc39flag && $pc39flag == 1) {
1078                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1079         }
1080
1081         # get rid of any PC16/17/19
1082         eph_del_regex("^PC1[679]*$call");
1083
1084         # do routing stuff, remove me from routing table
1085         my $node = Route::Node::get($call);
1086         my @rout;
1087         if ($node) {
1088                 @rout = $node->del($main::routeroot);
1089                 
1090                 # and all my ephemera as well
1091                 for (@rout) {
1092                         my $c = $_->call;
1093                         eph_del_regex("^PC1[679].*$c");
1094                 }
1095         }
1096
1097         RouteDB::delete_interface($call);
1098         
1099         # unbusy and stop and outgoing mail
1100         my $mref = DXMsg::get_busy($call);
1101         $mref->stop_msg($call) if $mref;
1102         
1103         # broadcast to all other nodes that all the nodes connected to via me are gone
1104         unless ($pc39flag && $pc39flag == 2)  {
1105                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1106                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1107         }
1108
1109         # remove outstanding pings
1110         delete $pings{$call};
1111         
1112         # I was the last node visited
1113     $self->user->node($main::mycall);
1114
1115         # send info to all logged in thingies
1116         $self->tell_login('logoutn');
1117
1118         Log('DXProt', $call . " Disconnected");
1119
1120         $self->SUPER::disconnect;
1121 }
1122
1123
1124
1125 # send a talk message to this thingy
1126 #
1127 sub talk
1128 {
1129         my ($self, $from, $to, $via, $line, $origin) = @_;
1130         
1131         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1132         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
1133         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
1134 }
1135
1136 # send it if it isn't the except list and isn't isolated and still has a hop count
1137 # taking into account filtering and so on
1138
1139 sub send_route
1140 {
1141         my $self = shift;
1142         my $origin = shift;
1143         my $generate = shift;
1144         my $no = shift;     # the no of things to filter on 
1145         my $routeit;
1146         my ($filter, $hops);
1147         my @rin;
1148         
1149         for (; @_ && $no; $no--) {
1150                 my $r = shift;
1151                 
1152                 if (!$self->{isolate} && $self->{routefilter}) {
1153                         $filter = undef;
1154                         if ($r) {
1155                                 ($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});
1156                                 if ($filter) {
1157                                         push @rin, $r;
1158                                 } else {
1159                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1160                                 }
1161                         } else {
1162                                 dbg("was sent a null value") if isdbg('chanerr');
1163                         }
1164                 } else {
1165                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1166                 }
1167         }
1168         if (@rin) {
1169                 foreach my $line (&$generate(@rin, @_)) {
1170                         if ($hops) {
1171                                 $routeit = $line;
1172                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1173                         } else {
1174                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1175                                 next unless $routeit;
1176                         }
1177                         
1178                         $self->send($routeit);
1179                 }
1180         }
1181 }
1182
1183 sub broadcast_route
1184 {
1185         my $self = shift;
1186         my $origin = shift;
1187         my $generate = shift;
1188         my $line = shift;
1189         my @dxchan = DXChannel::get_all_nodes();
1190         my $dxchan;
1191
1192         if ($line) {
1193                 $line =~ /\^H(\d+)\^?\~?$/;
1194                 return unless $1 > 0;
1195         }
1196         unless ($self->{isolate}) {
1197                 foreach $dxchan (@dxchan) {
1198                         next if $dxchan == $self;
1199                         next if $dxchan == $main::me;
1200                         next unless $dxchan->isa('DXProt');
1201                         next if $dxchan->{do_pc92};
1202                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
1203  
1204                         $dxchan->send_route($origin, $generate, @_);
1205                 }
1206         }
1207 }
1208
1209 # this is only used for next door nodes on init
1210 sub send_route_pc92
1211 {
1212         my $self = shift;
1213
1214         return unless $self->{do_pc92};
1215         
1216         my $origin = shift;
1217         my $generate = shift;
1218         my $no = shift;     # the no of things to filter on 
1219         my $line;
1220
1221         $line = &$generate(@_);
1222         $self->send($line);
1223 }
1224
1225 sub broadcast_route_pc9x
1226 {
1227         my $self = shift;
1228         my $origin = shift;
1229         my $generate = shift;
1230         my $line = shift;
1231         my $no = shift;
1232         my @dxchan = DXChannel::get_all_nodes();
1233         my $dxchan;
1234
1235         if ($origin eq $main::mycall) {
1236                 $line = &$generate(@_);
1237         } 
1238
1239         $line =~ /\^H(\d+)\^\~?$/;
1240         unless ($1 > 0 && $self->{isolate}) {
1241                 foreach $dxchan (@dxchan) {
1242                         next if $dxchan == $self || $main::me;
1243                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1244                         next unless $dxchan->{do_pc92};
1245                         next unless $dxchan->isa('DXProt');
1246
1247                         $dxchan->send($line);
1248                 }
1249         }
1250 }
1251
1252 sub route_pc16
1253 {
1254         my $self = shift;
1255         return unless $self->user->wantpc16;
1256         my $origin = shift;
1257         my $line = shift;
1258         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
1259 }
1260
1261 sub route_pc17
1262 {
1263         my $self = shift;
1264         return unless $self->user->wantpc16;
1265         my $origin = shift;
1266         my $line = shift;
1267         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
1268 }
1269
1270 sub route_pc19
1271 {
1272         my $self = shift;
1273         my $origin = shift;
1274         my $line = shift;
1275         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
1276 }
1277
1278 sub route_pc21
1279 {
1280         my $self = shift;
1281         my $origin = shift;
1282         my $line = shift;
1283         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
1284 }
1285
1286 sub route_pc24
1287 {
1288         my $self = shift;
1289         my $origin = shift;
1290         my $line = shift;
1291         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1292 }
1293
1294 sub route_pc41
1295 {
1296         my $self = shift;
1297         my $origin = shift;
1298         my $line = shift;
1299         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1300 }
1301
1302 sub route_pc50
1303 {
1304         my $self = shift;
1305         my $origin = shift;
1306         my $line = shift;
1307         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1308 }
1309
1310 sub route_pc92c
1311 {
1312         my $self = shift;
1313         my $origin = shift;
1314         my $line = shift;
1315         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1316 }
1317
1318 sub route_pc92a
1319 {
1320         my $self = shift;
1321         my $origin = shift;
1322         my $line = shift;
1323         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1324 }
1325
1326 sub route_pc92d
1327 {
1328         my $self = shift;
1329         my $origin = shift;
1330         my $line = shift;
1331         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1332 }
1333
1334 sub in_filter_route
1335 {
1336         my $self = shift;
1337         my $r = shift;
1338         my ($filter, $hops) = (1, 1);
1339         
1340         if ($self->{inroutefilter}) {
1341                 ($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);
1342                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1343         }
1344         return $filter;
1345 }
1346
1347 sub eph_dup
1348 {
1349         my $s = shift;
1350         my $t = shift || $eph_restime;
1351         my $r;
1352
1353         # chop the end off
1354         $s =~ s/\^H\d\d?\^?\~?$//;
1355         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1356         $eph{$s} = $main::systime + $t;
1357         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
1358         return $r;
1359 }
1360
1361 sub eph_del_regex
1362 {
1363         my $regex = shift;
1364         my ($key, $val);
1365         while (($key, $val) = each %eph) {
1366                 if ($key =~ m{$regex}) {
1367                         delete $eph{$key};
1368                 }
1369         }
1370 }
1371
1372 sub eph_clean
1373 {
1374         my ($key, $val);
1375         
1376         while (($key, $val) = each %eph) {
1377                 if ($main::systime >= $val) {
1378                         delete $eph{$key};
1379                 }
1380         }
1381 }
1382
1383 sub eph_list
1384 {
1385         my ($key, $val);
1386         my @out;
1387
1388         while (($key, $val) = each %eph) {
1389                 push @out, $key, $val;
1390         }
1391         return @out;
1392 }
1393
1394 sub run_cmd
1395 {
1396         goto &DXCommandmode::run_cmd;
1397 }
1398
1399
1400 # import any msgs in the chat directory
1401 # the messages are sent to the chat group which forms the
1402 # the first part of the name (eg: solar.1243.txt would be
1403 # sent to chat group SOLAR)
1404
1405 # Each message found is sent: one non-blank line to one chat
1406 # message. So 4 lines = 4 chat messages.
1407
1408 # The special name LOCAL is for local users ANN
1409 # The special name ALL is for ANN/FULL
1410 # The special name SYSOP is for ANN/SYSOP
1411 #
1412 sub import_chat
1413 {
1414         # are there any to do in this directory?
1415         return unless -d $chatimportfn;
1416         unless (opendir(DIR, $chatimportfn)) {
1417                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1418                 Log('msg', "can\'t open $chatimportfn $!");
1419                 return;
1420         } 
1421
1422         my @names = readdir(DIR);
1423         closedir(DIR);
1424         my $name;
1425         foreach $name (@names) {
1426                 next if $name =~ /^\./;
1427                 my $splitit = $name =~ /^split/;
1428                 my $fn = "$chatimportfn/$name";
1429                 next unless -f $fn;
1430                 unless (open(MSG, $fn)) {
1431                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1432                         Log('msg', "can\'t open import file $fn $!");
1433                         unlink($fn);
1434                         next;
1435                 }
1436                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1437                 close(MSG);
1438                 unlink($fn);
1439
1440                 my @cat = split /\./, $name;
1441                 my $target = uc $cat[0];
1442
1443                 foreach my $text (@msg) {
1444                         next unless $text && $text !~ /^\s*#/;
1445                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
1446                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
1447                                 if ($target ne 'LOCAL') {
1448                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
1449                                 } else {
1450                                         Log('ann', 'LOCAL', $main::mycall, $text);
1451                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
1452                                 }
1453                         } else {
1454                                 my $msgid = nextchatmsgid();
1455                                 $text = "#$msgid $text";
1456                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
1457                         }
1458                 }
1459         }
1460 }
1461
1462 1;
1463 __END__