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