added even more checking tp PC16,17 and 39
[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 DXCluster;
19 use DXProtVars;
20 use DXCommandmode;
21 use DXLog;
22 use Spot;
23 use DXProtout;
24 use DXDebug;
25 use Filter;
26 use Local;
27 use DXDb;
28 use AnnTalk;
29 use Geomag;
30 use WCY;
31 use Time::HiRes qw(gettimeofday tv_interval);
32 use BadWords;
33
34 use strict;
35 use vars qw($me $pc11_max_age $pc23_max_age
36                         $last_hour %pings %rcmds
37                         %nodehops @baddx $baddxfn $censorpc
38                         $allowzero $decode_dk0wcy $send_opernam @checklist);
39
40 $me = undef;                                    # the channel id for this cluster
41 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
42 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
43
44 $last_hour = time;                              # last time I did an hourly periodic update
45 %pings = ();                    # outstanding ping requests outbound
46 %rcmds = ();                    # outstanding rcmd requests outbound
47 %nodehops = ();                 # node specific hop control
48 @baddx = ();                    # list of illegal spotted callsigns
49 $censorpc = 0;                                  # Do a BadWords::check on text fields and reject things
50
51 $baddxfn = "$main::data/baddx.pl";
52
53 @checklist = 
54 (
55  [ qw(c c m bp bc c) ],                 # pc10
56  [ qw(f m d t m c c h) ],               # pc11
57  [ qw(c bc m bp bm p h) ],              # pc12
58  [ qw(c h) ],                                   # 
59  [ qw(c h) ],                                   # 
60  [ qw(c m h) ],                                 # 
61  undef ,                                                # pc16 has to be validated manually
62  [ qw(c c h) ],                                 # pc17
63  [ qw(m n) ],                                   # pc18
64  undef ,                                                # pc19 has to be validated manually
65  undef ,                                                # pc20 no validation
66  [ qw(c m h) ],                                 # pc21
67  undef ,                                                # pc22 no validation
68  [ qw(d n n n n m c c h) ],             # pc23
69  [ qw(c p h) ],                                 # pc24
70  [ qw(c c n n) ],                               # pc25
71  [ qw(f m d t m c c bc) ],              # pc26
72  [ qw(d n n n n m c c bc) ],    # pc27
73  [ qw(c c m c d t p m bp n p bp bc) ], # pc28
74  [ qw(c c n m) ],                               # pc29
75  [ qw(c c n) ],                                 # pc30
76  [ qw(c c n) ],                                 # pc31
77  [ qw(c c n) ],                                 # pc32
78  [ qw(c c n) ],                                 # pc33
79  [ qw(c c m) ],                                 # pc34
80  [ qw(c c m) ],                                 # pc35
81  [ qw(c c m) ],                                 # pc36
82  [ qw(c c n m) ],                               # pc37
83  undef,                                                 # pc38 not interested
84  [ qw(c m) ],                                   # pc39
85  [ qw(c c m p n) ],                             # pc40
86  [ qw(c n m h) ],                               # pc41
87  [ qw(c c n) ],                                 # pc42
88  undef,                                                 # pc43 don't handle it
89  [ qw(c c n m m c) ],                   # pc44
90  [ qw(c c n m) ],                               # pc45
91  [ qw(c c n) ],                                 # pc46
92  undef,                                                 # pc47
93  undef,                                                 # pc48
94  [ qw(c m h) ],                                 # pc49
95  [ qw(c n h) ],                                 # pc50
96  [ qw(c c n) ],                                 # pc51
97  undef,
98  undef,
99  undef,
100  undef,
101  undef,
102  undef,
103  undef,
104  undef,
105  undef,                                                 # pc60
106  undef,
107  undef,
108  undef,
109  undef,
110  undef,
111  undef,
112  undef,
113  undef,
114  undef,
115  undef,                                                 # pc70
116  undef,
117  undef,
118  [ qw(d n n n n n n m m m c c h) ],     # pc73
119  undef,
120  undef,
121  undef,
122  undef,
123  undef,
124  undef,
125  undef,                                                 # pc80
126  undef,
127  undef,
128  undef,
129  [ qw(c c c m) ],                               # pc84
130  [ qw(c c c m) ],                               # pc85
131 );
132
133 # use the entry in the check list to check the field list presented
134 # return OK if line NOT in check list (for now)
135 sub check
136 {
137         my $n = shift;
138         $n -= 10;
139         return 0 if $n < 0 || $n > @checklist; 
140         my $ref = $checklist[$n];
141         return 0 unless ref $ref;
142         
143         my $i;
144         shift;    # not interested in the first field
145         for ($i = 0; $i < @$ref; $i++) {
146                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
147                 return 0 unless $act;
148                 next if $blank && $_[$i] =~ /^[ \*]$/;
149                 if ($act eq 'c') {
150                         return $i+1 unless is_callsign($_[$i]);
151                 } elsif ($act eq 'm') {
152                         return $i+1 unless is_pctext($_[$i]);
153                 } elsif ($act eq 'p') {
154                         return $i+1 unless is_pcflag($_[$i]);
155                 } elsif ($act eq 'f') {
156                         return $i+1 unless is_freq($_[$i]);
157                 } elsif ($act eq 'n') {
158                         return $i+1 unless $_[$i] =~ /^[\d ]+$/;
159                 } elsif ($act eq 'h') {
160                         return $i+1 unless $_[$i] =~ /^H\d\d?$/;
161                 } elsif ($act eq 'd') {
162                         return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
163                 } elsif ($act eq 't') {
164                         return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
165                 }
166         }
167         return 0;
168 }
169
170 sub init
171 {
172         my $user = DXUser->get($main::mycall);
173         $DXProt::myprot_version += $main::version*100;
174         $me = DXProt->new($main::mycall, 0, $user); 
175         $me->{here} = 1;
176         $me->{state} = "indifferent";
177         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
178         confess $@ if $@;
179         $me->{sort} = 'S';    # S for spider
180
181         # load the baddx file
182         do "$baddxfn" if -e "$baddxfn";
183         print "$@\n" if $@;
184 }
185
186 #
187 # obtain a new connection this is derived from dxchannel
188 #
189
190 sub new 
191 {
192         my $self = DXChannel::alloc(@_);
193         return $self;
194 }
195
196 # this is how a pc connection starts (for an incoming connection)
197 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
198 # all the crap that comes between).
199 sub start
200 {
201         my ($self, $line, $sort) = @_;
202         my $call = $self->{call};
203         my $user = $self->{user};
204         
205         # remember type of connection
206         $self->{consort} = $line;
207         $self->{outbound} = $sort eq 'O';
208         $self->{priv} = $user->priv || 1;     # other clusters can always be 'normal' users
209         $self->{lang} = $user->lang || 'en';
210         $self->{isolate} = $user->{isolate};
211         $self->{consort} = $line;       # save the connection type
212         $self->{here} = 1;
213
214         # get the output filters
215         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
216         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
217         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
218         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
219
220
221         # get the INPUT filters (these only pertain to Clusters)
222         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
223         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
224         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
225         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
226         
227         # set unbuffered and no echo
228         $self->send_now('B',"0");
229         $self->send_now('E',"0");
230         
231         # ping neighbour node stuff
232         my $ping = $user->pingint;
233         $ping = 5*60 unless defined $ping;
234         $self->{pingint} = $ping;
235         $self->{nopings} = $user->nopings || 2;
236         $self->{pingtime} = [ ];
237         $self->{pingave} = 0;
238
239         # send initialisation string
240         unless ($self->{outbound}) {
241 #               $self->send(pc38()) if DXNode->get_all();
242                 $self->send(pc18());
243                 $self->{lastping} = $main::systime;
244         } else {
245                 # remove from outstanding connects queue
246                 @main::outstanding_connects = grep {$_->{call} ne $call} @main::outstanding_connects;
247                 $self->{lastping} = $main::systime + $self->pingint / 2;
248         }
249         $self->state('init');
250         $self->pc50_t(time);
251
252         # send info to all logged in thingies
253         $self->tell_login('loginn');
254
255         Log('DXProt', "$call connected");
256 }
257
258 #
259 # This is the normal pcxx despatcher
260 #
261 sub normal
262 {
263         my ($self, $line) = @_;
264         my @field = split /\^/, $line;
265         return unless @field;
266         
267         pop @field if $field[-1] eq '~';
268         
269 #       print join(',', @field), "\n";
270                                                 
271         # ignore any lines that don't start with PC
272         return if !$field[0] =~ /^PC/;
273         
274         # process PC frames
275         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
276         return unless $pcno;
277         return if $pcno < 10 || $pcno > 99;
278
279         # check for and dump bad protocol messages
280         my $n = check($pcno, @field);
281         if ($n) {
282                 dbg('chan', "bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")");
283                 return;
284         }
285
286         # local processing 1
287         my $pcr;
288         eval {
289                 $pcr = Local::pcprot($self, $pcno, @field);
290         };
291 #       dbg('local', "Local::pcprot error $@") if $@;
292         return if $pcr;
293         
294  SWITCH: {
295                 if ($pcno == 10) {              # incoming talk
296
297                         # will we allow it at all?
298                         if ($censorpc) {
299                                 my @bad;
300                                 if (@bad = BadWords::check($field[3])) {
301                                         dbg('chan', "Bad words: @bad, dropped" );
302                                         return;
303                                 }
304                         }
305
306                         # is it for me or one of mine?
307                         my ($to, $via, $call, $dxchan);
308                         if ($field[5] gt ' ') {
309                                 $call = $via = $field[2];
310                                 $to = $field[5];
311                         } else {
312                                 $call = $to = $field[2];
313                         }
314                         $dxchan = DXChannel->get($call);
315                         if ($dxchan && $dxchan->is_user) {
316                                 $field[3] =~ s/\%5E/^/g;
317                                 $dxchan->talk($field[1], $to, $via, $field[3]);
318                         } else {
319                                 $self->route($field[2], $line); # relay it on its way
320                         }
321                         return;
322                 }
323                 
324                 if ($pcno == 11 || $pcno == 26) { # dx spot
325
326                         # route 'foreign' pc26s 
327                         if ($pcno == 26) {
328                                 if ($field[7] ne $main::mycall) {
329                                         $self->route($field[7], $line);
330                                         return;
331                                 }
332                         }
333                         
334                         # if this is a 'nodx' node then ignore it
335                         if (grep $field[7] =~ /^$_/,  @DXProt::nodx_node) {
336                                 dbg('chan', "Bad DXNode, dropped");
337                                 return;
338                         }
339                         
340                         # convert the date to a unix date
341                         my $d = cltounix($field[3], $field[4]);
342                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
343                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
344                                 dbg('chan', "Spot ignored, invalid date or out of range ($field[3] $field[4])\n");
345                                 return;
346                         }
347
348                         # is it 'baddx'
349                         if (grep $field[2] eq $_, @baddx) {
350                                 dbg('chan', "Bad DX spot, ignored");
351                                 return;
352                         }
353                         
354                         # do some de-duping
355                         $field[5] =~ s/^\s+//;      # take any leading blanks off
356                         if (Spot::dup($field[1], $field[2], $d, $field[5])) {
357                                 dbg('chan', "Duplicate Spot ignored\n");
358                                 return;
359                         }
360                         if ($censorpc) {
361                                 my @bad;
362                                 if (@bad = BadWords::check($field[5])) {
363                                         dbg('chan', "Bad words: @bad, dropped" );
364                                         return;
365                                 }
366                         }
367                         
368                         my @spot = Spot::add($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
369
370             #
371                         # @spot at this point contains:-
372             # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
373                         # then  spotted itu, spotted cq, spotters itu, spotters cq
374                         # you should be able to route on any of these
375             #
376                         
377                         # fix up qra locators of known users 
378                         my $user = DXUser->get_current($spot[4]);
379                         if ($user) {
380                                 my $qra = $user->qra;
381                                 unless ($qra && DXBearing::is_qra($qra)) {
382                                         my $lat = $user->lat;
383                                         my $long = $user->long;
384                                         if (defined $lat && defined $long) {
385                                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
386                                                 $user->put;
387                                         }
388                                 }
389
390                                 # send a remote command to a distant cluster if it is visible and there is no
391                                 # qra locator and we havn't done it for a month.
392
393                                 unless ($user->qra) {
394                                         my $node;
395                                         my $to = $user->homenode;
396                                         my $last = $user->lastoper || 0;
397                                         if ($send_opernam && $main::systime > $last + $DXUser::lastoperinterval && $to && ($node = DXCluster->get_exact($to)) ) {
398                                                 my $cmd = "forward/opernam $spot[4]";
399                                                 # send the rcmd but we aren't interested in the replies...
400                                                 if ($node && $node->dxchan && $node->dxchan->is_clx) {
401                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
402                                                 } else {
403                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
404                                                 }
405                                                 if ($to ne $field[7]) {
406                                                         $to = $field[7];
407                                                         $node = DXCluster->get_exact($to);
408                                                         if ($node && $node->dxchan && $node->dxchan->is_clx) {
409                                                                 route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
410                                                         } else {
411                                                                 route(undef, $to, pc34($main::mycall, $to, $cmd));
412                                                         }
413                                                 }
414                                                 $user->lastoper($main::systime);
415                                                 $user->put;
416                                         }
417                                 }
418                         }
419                                 
420                         # local processing 
421                         my $r;
422                         eval {
423                                 $r = Local::spot($self, @spot);
424                         };
425 #                       dbg('local', "Local::spot1 error $@") if $@;
426                         return if $r;
427
428                         # DON'T be silly and send on PC26s!
429                         return if $pcno == 26;
430
431                         # send out the filtered spots
432                         send_dx_spot($self, $line, @spot) if @spot;
433                         return;
434                 }
435                 
436                 if ($pcno == 12) {              # announces
437                         # announce duplicate checking
438                         $field[3] =~ s/^\s+//;  # remove leading blanks
439                         if (AnnTalk::dup($field[1], $field[2], $field[3])) {
440                                 dbg('chan', "Duplicate Announce ignored");
441                                 return;
442                         }
443
444                         if ($censorpc) {
445                                 my @bad;
446                                 if (@bad = BadWords::check($field[3])) {
447                                         dbg('chan', "Bad words: @bad, dropped" );
448                                         return;
449                                 }
450                         }
451                         
452                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
453                                 
454                                 # global ann filtering on INPUT
455                                 if ($self->{inannfilter}) {
456                                         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
457                                         my @dxcc = Prefix::extract($field[1]);
458                                         if (@dxcc > 0) {
459                                                 $ann_dxcc = $dxcc[1]->dxcc;
460                                                 $ann_itu = $dxcc[1]->itu;
461                                                 $ann_cq = $dxcc[1]->cq();                                               
462                                         }
463                                         @dxcc = Prefix::extract($field[5]);
464                                         if (@dxcc > 0) {
465                                                 $org_dxcc = $dxcc[1]->dxcc;
466                                                 $org_itu = $dxcc[1]->itu;
467                                                 $org_cq = $dxcc[1]->cq();                                               
468                                         }
469                                         my ($filter, $hops) = $self->{inannfilter}->it(@field[1..6], $self->{call}, 
470                                                                                                         $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
471                                         unless ($filter) {
472                                                 dbg('chan', "Rejected by filter");
473                                                 return;
474                                         }
475                                 }
476
477                                 # send it
478                                 $self->send_announce($line, @field[1..6]);
479                         } else {
480                                 $self->route($field[2], $line);
481                         }
482                         
483                         return;
484                 }
485                 
486                 if ($pcno == 13) {
487                         last SWITCH;
488                 }
489                 if ($pcno == 14) {
490                         last SWITCH;
491                 }
492                 if ($pcno == 15) {
493                         last SWITCH;
494                 }
495                 
496                 if ($pcno == 16) {              # add a user
497                         my $node = DXCluster->get_exact($field[1]); 
498                         my $dxchan;
499                         if (!$node && ($dxchan = DXChannel->get($field[1]))) {
500                                 # add it to the node table if it isn't present and it's
501                                 # connected locally
502                                 $node = DXNode->new($dxchan, $field[1], 0, 1, 5400);
503                                 dbg('chan', "$field[1] no PC19 yet, autovivified as node");
504 #                               broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
505                                 
506                         }
507                         if ($field[2] eq $main::mycall || $field[2] eq $main::myalias || $field[1] eq $main::myalias || $field[1] eq $main::mycall) {
508                                 dbg('chan', "LOOP: trying to connect myself!");
509                                 return;
510                         }
511                         unless ($node) {
512                                 dbg('chan', "Node $field[1] not in config");
513                                 return;
514                         }
515                         unless ($node->isa('DXNode')) {
516                                 dbg('chan', "$field[1] is not a node");
517                                 return;
518                         }
519                         if ($node->dxchan != $self) {
520                                 dbg('chan', "LOOP: $field[1] came in on wrong channel");
521                                 return;
522                         }
523                         if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
524                                 dbg('chan', "LOOP: $field[1] connected locally");
525                                 return;
526                         }
527                         my $i;
528                                                 
529                         for ($i = 2; $i < $#field; $i++) {
530                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
531                                 next unless $call && $confmode && defined $here && is_callsign($call);
532                                 my $ref = DXCluster->get_exact($call); 
533                                 if ($ref) {
534                                         if ($ref->isa('DXNode')) {
535                                                 dbg('chan', "LOOP: $call is a node");
536                                                 next;
537                                         }
538                                         my $rcall = $ref->call;
539                                         dbg('chan', "LOOP: already have $call on $rcall");
540                                         next;
541                                 }
542                                 
543                                 $confmode = $confmode eq '*';
544                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
545                                 
546                                 # add this station to the user database, if required
547                                 $call =~ s/-\d+$//o;        # remove ssid for users
548                                 my $user = DXUser->get_current($call);
549                                 $user = DXUser->new($call) if !$user;
550                                 $user->homenode($node->call) if !$user->homenode;
551                                 $user->node($node->call);
552                                 $user->lastin($main::systime) unless DXChannel->get($call);
553                                 $user->put;
554                         }
555                         
556                         # queue up any messages (look for privates only)
557                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
558                         last SWITCH;
559                 }
560                 
561                 if ($pcno == 17) {              # remove a user
562                         my $node = DXCluster->get_exact($field[2]);
563                         my $dxchan;
564                         if (!$node && ($dxchan = DXChannel->get($field[2]))) {
565                                 # add it to the node table if it isn't present and it's
566                                 # connected locally
567                                 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
568                                 dbg('chan', "$field[2] no PC19 yet, autovivified as node");
569 #                               broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
570                         }
571                         if ($field[2] eq $main::mycall || $field[2] eq $main::myalias || $field[1] eq $main::myalias || $field[1] eq $main::mycall) {
572                                 dbg('chan', "LOOP: trying to disconnect me!");
573                                 return;
574                         }
575                         unless ($node) {
576                                 dbg('chan', "Node $field[2] not in config");
577                                 return;
578                         }
579                         unless ($node->isa('DXNode')) {
580                                 dbg('chan', "LOOP: $field[2] is not a node");
581                                 return;
582                         }
583                         if ($node->dxchan != $self) {
584                                 dbg('chan', "LOOP: $field[2] came in on wrong channel");
585                                 return;
586                         }
587                         if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
588                                 dbg('chan', "LOOP: $field[2] connected locally");
589                                 return;
590                         }
591                         my $ref = DXCluster->get_exact($field[1]);
592                         if ($ref) {
593                                 $ref->del;
594                         } else {
595                                 dbg('chan', "$field[1] not known" );
596                                 return;
597                         }
598                         last SWITCH;
599                 }
600                 
601                 if ($pcno == 18) {              # link request
602                         $self->state('init');   
603
604                         # first clear out any nodes on this dxchannel
605                         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
606                         foreach my $node (@gonenodes) {
607                                 next if $node->dxchan == $DXProt::me;
608                                 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate}; 
609                                 $node->del();
610                         }
611                         $self->send_local_config();
612                         $self->send(pc20());
613                         return;             # we don't pass these on
614                 }
615                 
616                 if ($pcno == 19) {              # incoming cluster list
617                         my $i;
618                         my $newline = "PC19^";
619                         for ($i = 1; $i < $#field-1; $i += 4) {
620                                 my $here = $field[$i];
621                                 my $call = uc $field[$i+1];
622                                 my $confmode = $field[$i+2];
623                                 my $ver = $field[$i+3];
624                                 next unless defined $here && defined $confmode && is_callsign($call);
625
626                                 $ver = 5400 if !$ver && $allowzero;
627                                 
628                                 # now check the call over
629                                 my $node = DXCluster->get_exact($call);
630                                 if ($node) {
631                                         my $dxchan;
632                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
633                                                 dbg('chan', "LOOP: $call connected locally");
634                                         }
635                                     if ($node->dxchan != $self) {
636                                                 dbg('chan', "LOOP: $call come in on wrong channel");
637                                                 next;
638                                         }
639                                         dbg('chan', "already have $call");
640                                         next;
641                                 }
642                                 
643                                 # check for sane parameters
644                                 next if $ver < 5000; # only works with version 5 software
645                                 next if length $call < 3; # min 3 letter callsigns
646
647                                 # add it to the nodes table and outgoing line
648                                 $newline .= "$here^$call^$confmode^$ver^";
649                                 DXNode->new($self, $call, $confmode, $here, $ver);
650                                 
651                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
652                                 my $mref = DXMsg::get_busy($call);
653                                 $mref->stop_msg($call) if $mref;
654                                 
655                                 # add this station to the user database, if required (don't remove SSID from nodes)
656                                 my $user = DXUser->get_current($call);
657                                 if (!$user) {
658                                         $user = DXUser->new($call);
659                                         $user->sort('A');
660                                         $user->priv(1);                   # I have relented and defaulted nodes
661                                         $self->{priv} = 1;                # to user RCMDs allowed
662                                         $user->homenode($call);
663                                         $user->node($call);
664                                 }
665                                 $user->lastin($main::systime) unless DXChannel->get($call);
666                                 $user->put;
667                         }
668                         
669                         return if $newline eq "PC19^";
670
671                         # add hop count 
672                         $newline .=  get_hops(19) . "^";
673                         $line = $newline;
674                         last SWITCH;
675                 }
676                 
677                 if ($pcno == 20) {              # send local configuration
678                         $self->send_local_config();
679                         $self->send(pc22());
680                         $self->state('normal');
681                         return;
682                 }
683                 
684                 if ($pcno == 21) {              # delete a cluster from the list
685                         my $call = uc $field[1];
686                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
687                                 my $node = DXCluster->get_exact($call);
688                                 if ($node) {
689                                         unless ($node->isa('DXNode')) {
690                                                 dbg('chan', "$call is not a node");
691                                                 return;
692                                         }
693                                         if ($call eq $self->{call}) {
694                                                 dbg('chan', "LOOP: Trying to disconnect myself with PC21");
695                                                 return;
696                                         } 
697                                         if ($node->dxchan != $self) {
698                                                 dbg('chan', "LOOP: $call come in on wrong channel");
699                                                 return;
700                                         }
701                                         my $dxchan;
702                                         if ($dxchan = DXChannel->get($call)) {
703                                                 dbg('chan', "LOOP: $call connected locally");
704                                                 return;
705                                         }
706                                         $node->del();
707                                 } else {
708                                         dbg('chan', "$call not in table, dropped");
709                                         return;
710                                 }
711                         } else {
712                                 dbg('chan', "I WILL NOT be disconnected!");
713                                 return;
714                         }
715                         last SWITCH;
716                 }
717                 
718                 if ($pcno == 22) {
719                         $self->state('normal');
720                         return;
721                 }
722                                 
723                 if ($pcno == 23 || $pcno == 27) { # WWV info
724                         
725                         # route 'foreign' pc27s 
726                         if ($pcno == 27) {
727                                 if ($field[8] ne $main::mycall) {
728                                         $self->route($field[8], $line);
729                                         return;
730                                 }
731                         }
732
733                         # do some de-duping
734                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
735                         my $sfi = unpad($field[3]);
736                         my $k = unpad($field[4]);
737                         my $i = unpad($field[5]);
738                         my ($r) = $field[6] =~ /R=(\d+)/;
739                         $r = 0 unless $r;
740                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
741                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
742                                 return;
743                         }
744                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
745                                 dbg('chan', "Dup WWV Spot ignored\n");
746                                 return;
747                         }
748                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
749                 
750                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
751
752                         my $rep;
753                         eval {
754                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
755                         };
756 #                       dbg('local', "Local::wwv2 error $@") if $@;
757                         return if $rep;
758
759                         # DON'T be silly and send on PC27s!
760                         return if $pcno == 27;
761
762                         # broadcast to the eager world
763                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
764                         return;
765                 }
766                 
767                 if ($pcno == 24) {              # set here status
768                         my $call = uc $field[1];
769                         my $ref = DXCluster->get_exact($call);
770                         $ref->here($field[2]) if $ref;
771                         last SWITCH;
772                 }
773                 
774                 if ($pcno == 25) {      # merge request
775                         if ($field[1] ne $main::mycall) {
776                                 $self->route($field[1], $line);
777                                 return;
778                         }
779                         if ($field[2] eq $main::mycall) {
780                                 dbg('chan', "Trying to merge to myself, ignored");
781                                 return;
782                         }
783
784                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
785                         
786                         # spots
787                         if ($field[3] > 0) {
788                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
789                                 my $in;
790                                 foreach $in (@in) {
791                                         $self->send(pc26(@{$in}[0..4], $field[2]));
792                                 }
793                         }
794
795                         # wwv
796                         if ($field[4] > 0) {
797                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
798                                 my $in;
799                                 foreach $in (@in) {
800                                         $self->send(pc27(@{$in}[0..5], $field[2]));
801                                 }
802                         }
803                         return;
804                 }
805
806                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
807                         if ($pcno == 49 || $field[1] eq $main::mycall) {
808                                 DXMsg::process($self, $line);
809                         } else {
810                                 $self->route($field[1], $line) unless $self->is_clx;
811                         }
812                         return;
813                 }
814                 
815                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
816                         if ($field[1] eq $main::mycall) {
817                                 my $ref = DXUser->get_current($field[2]);
818                                 my $cref = DXCluster->get($field[2]);
819                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
820                                 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
821                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
822                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
823                                                 my $oldpriv = $self->{priv};
824                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
825                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
826                                                 $self->{priv} = $oldpriv;
827                                                 for (@in) {
828                                                         s/\s*$//og;
829                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
830                                                         Log('rcmd', 'out', $field[2], $_);
831                                                 }
832                                                 delete $self->{remotecmd};
833                                         } else {
834                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
835                                         }
836                                 } else {
837                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
838                                 }
839                         } else {
840                                 my $ref = DXUser->get_current($field[1]);
841                                 if ($ref && $ref->is_clx) {
842                                         route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
843                                 } else {
844                                         $self->route($field[1], $line);
845                                 }
846                         }
847                         return;
848                 }
849                 
850                 if ($pcno == 35) {              # remote command replies
851                         if ($field[1] eq $main::mycall) {
852                                 my $s = $rcmds{$field[2]};
853                                 if ($s) {
854                                         my $dxchan = DXChannel->get($s->{call});
855                                         $dxchan->send($field[3]) if $dxchan;
856                                         delete $rcmds{$field[2]} if !$dxchan;
857                                 } else {
858                                         # send unsolicited ones to the sysop
859                                         my $dxchan = DXChannel->get($main::myalias);
860                                         $dxchan->send($field[3]) if $dxchan;
861                                 }
862                         } else {
863                                 my $ref = DXUser->get_current($field[1]);
864                                 if ($ref && $ref->is_clx) {
865                                         route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
866                                 } else {
867                                         $self->route($field[1], $line);
868                                 }
869                         }
870                         return;
871                 }
872                 
873                 # for pc 37 see 44 onwards
874
875                 if ($pcno == 38) {              # node connected list from neighbour
876                         return;
877                 }
878                 
879                 if ($pcno == 39) {              # incoming disconnect
880                         if ($field[1] eq $self->{call}) {
881                                 $self->disconnect(1);
882                         } else {
883                                 dbg('chan', "LOOP: came in on wrong channel");
884                         }
885                         return;
886                 }
887                 
888                 if ($pcno == 41) {              # user info
889                         # add this station to the user database, if required
890                         my $user = DXUser->get_current($field[1]);
891                         if (!$user) {
892                                 # then try without an SSID
893                                 $field[1] =~ s/-\d+$//o;
894                                 $user = DXUser->get_current($field[1]);
895                         }
896                         $user = DXUser->new($field[1]) if !$user;
897                         
898                         if ($field[2] == 1) {
899                                 $user->name($field[3]);
900                         } elsif ($field[2] == 2) {
901                                 $user->qth($field[3]);
902                         } elsif ($field[2] == 3) {
903                                 my ($lat, $long) = DXBearing::stoll($field[3]);
904                                 $user->lat($lat);
905                                 $user->long($long);
906                                 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
907                         } elsif ($field[2] == 4) {
908                                 $user->homenode($field[3]);
909                         }
910                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
911                         $user->put;
912                         last SWITCH;
913                 }
914                 if ($pcno == 43) {
915                         last SWITCH;
916                 }
917                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
918                         DXDb::process($self, $line);
919                         return;
920                 }
921                 
922                 if ($pcno == 50) {              # keep alive/user list
923                         my $node = DXCluster->get_exact($field[1]);
924                         if ($node) {
925                                 return unless $node->isa('DXNode');
926                                 return unless $node->dxchan == $self;
927                                 $node->update_users($field[2]);
928                         }
929                         last SWITCH;
930                 }
931                 
932                 if ($pcno == 51) {              # incoming ping requests/answers
933                         
934                         # is it for us?
935                         if ($field[1] eq $main::mycall) {
936                                 my $flag = $field[3];
937                                 if ($flag == 1) {
938                                         $self->send(pc51($field[2], $field[1], '0'));
939                                 } else {
940                                         # it's a reply, look in the ping list for this one
941                                         my $ref = $pings{$field[2]};
942                                         if ($ref) {
943                                                 my $tochan =  DXChannel->get($field[2]);
944                                                 while (@$ref) {
945                                                         my $r = shift @$ref;
946                                                         my $dxchan = DXChannel->get($r->{call});
947                                                         next unless $dxchan;
948                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
949                                                         if ($dxchan->is_user) {
950                                                                 my $s = sprintf "%.2f", $t; 
951                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
952                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
953                                                         } elsif ($dxchan->is_node) {
954                                                                 if ($tochan) {
955                                                                         $tochan->{nopings} = 2; # pump up the timer
956                                                                         push @{$tochan->{pingtime}}, $t;
957                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
958                                                                         my $st;
959                                                                         for (@{$tochan->{pingtime}}) {
960                                                                                 $st += $_;
961                                                                         }
962                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
963                                                                 }
964                                                         } 
965                                                 }
966                                         }
967                                 }
968                         } else {
969                                 # route down an appropriate thingy
970                                 $self->route($field[1], $line);
971                         }
972                         return;
973                 }
974
975                 if ($pcno == 75) {              # dunno but route it
976                         if ($field[1] ne $main::mycall) {
977                                 $self->route($field[1], $line);
978                         }
979                         return;
980                 }
981
982                 if ($pcno == 73) {  # WCY broadcasts
983                         
984                         # do some de-duping
985                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
986                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
987                                 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
988                                 return;
989                         }
990                         @field = map { unpad($_) } @field;
991                         if (WCY::dup($d,@field[3..7])) {
992                                 dbg('chan', "Dup WCY Spot ignored\n");
993                                 return;
994                         }
995                 
996                         my $wcy = WCY::update($d, @field[2..12]);
997
998                         my $rep;
999                         eval {
1000                                 $rep = Local::wwv($self, @field[1..12]);
1001                         };
1002                         # dbg('local', "Local::wcy error $@") if $@;
1003                         return if $rep;
1004
1005                         # broadcast to the eager world
1006                         send_wcy_spot($self, $line, $d, @field[2..12]);
1007                         return;
1008                 }
1009
1010                 if ($pcno == 84) { # remote commands (incoming)
1011                         if ($field[1] eq $main::mycall) {
1012                                 my $ref = DXUser->get_current($field[2]);
1013                                 my $cref = DXCluster->get($field[2]);
1014                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
1015                                 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
1016                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
1017                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1018                                                 my $oldpriv = $self->{priv};
1019                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
1020                                                 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1021                                                 $self->{priv} = $oldpriv;
1022                                                 for (@in) {
1023                                                         s/\s*$//og;
1024                                                         $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1025                                                         Log('rcmd', 'out', $field[2], $_);
1026                                                 }
1027                                                 delete $self->{remotecmd};
1028                                         } else {
1029                                                 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1030                                         }
1031                                 } else {
1032                                         $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1033                                 }
1034                         } else {
1035                                 my $ref = DXUser->get_current($field[1]);
1036                                 if ($ref && $ref->is_clx) {
1037                                         $self->route($field[1], $line);
1038                                 } else {
1039                                         route($field[1], pc34($field[2], $field[1], $field[4]));
1040                                 }
1041                         }
1042                         return;
1043                 }
1044
1045                 if ($pcno == 85) {              # remote command replies
1046                         if ($field[1] eq $main::mycall) {
1047                                 my $dxchan = DXChannel->get($field[3]);
1048                                 if ($dxchan) {
1049                                         $dxchan->send($field[4]);
1050                                 } else {
1051                                         my $s = $rcmds{$field[2]};
1052                                         if ($s) {
1053                                                 $dxchan = DXChannel->get($s->{call});
1054                                                 $dxchan->send($field[4]) if $dxchan;
1055                                                 delete $rcmds{$field[2]} if !$dxchan;
1056                                         } else {
1057                                                 # send unsolicited ones to the sysop
1058                                                 my $dxchan = DXChannel->get($main::myalias);
1059                                                 $dxchan->send($field[4]) if $dxchan;
1060                                         }
1061                                 }
1062                         } else {
1063                                 my $ref = DXUser->get_current($field[1]);
1064                                 if ($ref && $ref->is_clx) {
1065                                         $self->route($field[1], $line);
1066                                 } else {
1067                                         route($field[1], pc35($field[2], $field[1], $field[4]));
1068                                 }
1069                         }
1070                         return;
1071                 }
1072         }
1073          
1074         # if get here then rebroadcast the thing with its Hop count decremented (if
1075         # there is one). If it has a hop count and it decrements to zero then don't
1076         # rebroadcast it.
1077         #
1078         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1079         #        REBROADCAST!!!!
1080         #
1081          
1082         unless ($self->{isolate}) {
1083                 broadcast_ak1a($line, $self); # send it to everyone but me
1084         }
1085 }
1086
1087 #
1088 # This is called from inside the main cluster processing loop and is used
1089 # for despatching commands that are doing some long processing job
1090 #
1091 sub process
1092 {
1093         my $t = time;
1094         my @dxchan = DXChannel->get_all();
1095         my $dxchan;
1096         
1097         foreach $dxchan (@dxchan) {
1098                 next unless $dxchan->is_node();
1099                 next if $dxchan == $me;
1100                 
1101                 # send a pc50 out on this channel
1102                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1103                         $dxchan->send(pc50(scalar DXChannel::get_all_users));
1104                         $dxchan->pc50_t($t);
1105                 } 
1106
1107                 # send a ping out on this channel
1108                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1109                         if ($dxchan->{nopings} <= 0) {
1110                                 $dxchan->disconnect;
1111                         } else {
1112                                 addping($main::mycall, $dxchan->call);
1113                                 $dxchan->{nopings} -= 1;
1114                                 $dxchan->{lastping} = $t;
1115                         }
1116                 }
1117         }
1118         
1119         my $key;
1120         my $val;
1121         my $cutoff;
1122         if ($main::systime - 3600 > $last_hour) {
1123 #               Spot::process;
1124 #               Geomag::process;
1125 #               AnnTalk::process;
1126                 $last_hour = $main::systime;
1127         }
1128 }
1129
1130 #
1131 # finish up a pc context
1132 #
1133 sub finish
1134 {
1135         my $self = shift;
1136         my $call = $self->call;
1137         my $conn = shift;
1138         my $ref = DXCluster->get_exact($call);
1139         
1140         # unbusy and stop and outgoing mail
1141         my $mref = DXMsg::get_busy($call);
1142         $mref->stop_msg($call) if $mref;
1143         
1144         # broadcast to all other nodes that all the nodes connected to via me are gone
1145         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1146         my $node;
1147         
1148         foreach $node (@gonenodes) {
1149                 next if $node->call eq $call;
1150                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
1151                 $node->del();
1152         }
1153
1154         # remove outstanding pings
1155         delete $pings{$call};
1156         
1157         # now broadcast to all other ak1a nodes that I have gone
1158         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1159
1160         # I was the last node visited
1161     $self->user->node($main::mycall);
1162
1163         # send info to all logged in thingies
1164         $self->tell_login('logoutn');
1165
1166         Log('DXProt', $call . " Disconnected");
1167         $ref->del() if $ref;
1168 }
1169
1170 #
1171 # some active measures
1172 #
1173 sub send_dx_spot
1174 {
1175         my $self = shift;
1176         my $line = shift;
1177         my @dxchan = DXChannel->get_all();
1178         my $dxchan;
1179         
1180         # send it if it isn't the except list and isn't isolated and still has a hop count
1181         # taking into account filtering and so on
1182         foreach $dxchan (@dxchan) {
1183                 my $routeit;
1184                 my ($filter, $hops);
1185
1186                 if ($dxchan->{spotsfilter}) {
1187                     ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1188                         next unless $filter;
1189                 }
1190                 
1191                 if ($dxchan->is_node) {
1192                         next if $dxchan == $self;
1193                         if ($hops) {
1194                                 $routeit = $line;
1195                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1196                         } else {
1197                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1198                                 next unless $routeit;
1199                         }
1200                         if ($filter) {
1201                                 $dxchan->send($routeit) if $routeit;
1202                         } else {
1203                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1204                         }
1205                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1206                         my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1207                         $buf .= "\a\a" if $dxchan->{beep};
1208                         $buf =~ s/\%5E/^/g;
1209                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1210                                 $dxchan->send($buf);
1211                         } else {
1212                                 $dxchan->delay($buf);
1213                         }
1214                 }                                       
1215         }
1216 }
1217
1218 sub send_wwv_spot
1219 {
1220         my $self = shift;
1221         my $line = shift;
1222         my @dxchan = DXChannel->get_all();
1223         my $dxchan;
1224         
1225         # send it if it isn't the except list and isn't isolated and still has a hop count
1226         # taking into account filtering and so on
1227         foreach $dxchan (@dxchan) {
1228                 my $routeit;
1229                 my ($filter, $hops);
1230
1231                 if ($dxchan->{wwvfilter}) {
1232                          ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call} );
1233                          next unless $filter;
1234                 }
1235                 if ($dxchan->is_node) {
1236                         next if $dxchan == $self;
1237                         if ($hops) {
1238                                 $routeit = $line;
1239                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1240                         } else {
1241                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1242                                 next unless $routeit;
1243                         }
1244                         if ($filter) {
1245                                 $dxchan->send($routeit) if $routeit;
1246                         } else {
1247                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1248                                 
1249                         }
1250                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1251                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1252                         $buf .= "\a\a" if $dxchan->{beep};
1253                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1254                                 $dxchan->send($buf);
1255                         } else {
1256                                 $dxchan->delay($buf);
1257                         }
1258                 }                                       
1259         }
1260 }
1261
1262 sub send_wcy_spot
1263 {
1264         my $self = shift;
1265         my $line = shift;
1266         my @dxchan = DXChannel->get_all();
1267         my $dxchan;
1268         
1269         # send it if it isn't the except list and isn't isolated and still has a hop count
1270         # taking into account filtering and so on
1271         foreach $dxchan (@dxchan) {
1272                 my $routeit;
1273                 my ($filter, $hops);
1274
1275                 if ($dxchan->{wcyfilter}) {
1276                          ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call} );
1277                          next unless $filter;
1278                 }
1279                 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1280                         next if $dxchan == $self;
1281                         if ($hops) {
1282                                 $routeit = $line;
1283                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1284                         } else {
1285                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1286                                 next unless $routeit;
1287                         }
1288                         if ($filter) {
1289                                 $dxchan->send($routeit) if $routeit;
1290                         } else {
1291                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1292                         }
1293                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1294                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1295                         $buf .= "\a\a" if $dxchan->{beep};
1296                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1297                                 $dxchan->send($buf);
1298                         } else {
1299                                 $dxchan->delay($buf);
1300                         }
1301                 }                                       
1302         }
1303 }
1304
1305 # send an announce
1306 sub send_announce
1307 {
1308         my $self = shift;
1309         my $line = shift;
1310         my @dxchan = DXChannel->get_all();
1311         my $dxchan;
1312         my $text = unpad($_[2]);
1313         my $target;
1314         my $to = 'To ';
1315                                 
1316         if ($_[3] eq '*') {     # sysops
1317                 $target = "SYSOP";
1318         } elsif ($_[3] gt ' ') { # speciality list handling
1319                 my ($name) = split /\./, $_[3]; 
1320                 $target = "$name"; # put the rest in later (if bothered) 
1321         } 
1322         
1323         if ($_[5] eq '1') {
1324                 $target = "WX"; 
1325                 $to = '';
1326         }
1327         $target = "All" if !$target;
1328         
1329         Log('ann', $target, $_[0], $text);
1330
1331         # send it if it isn't the except list and isn't isolated and still has a hop count
1332         # taking into account filtering and so on
1333         foreach $dxchan (@dxchan) {
1334                 my $routeit;
1335                 my ($filter, $hops);
1336
1337                 if ($dxchan->{annfilter}) {
1338                         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1339                         my @dxcc = Prefix::extract($_[0]);
1340                         if (@dxcc > 0) {
1341                                 $ann_dxcc = $dxcc[1]->dxcc;
1342                                 $ann_itu = $dxcc[1]->itu;
1343                                 $ann_cq = $dxcc[1]->cq;                                         
1344                         }
1345                         @dxcc = Prefix::extract($_[4]);
1346                         if (@dxcc > 0) {
1347                                 $org_dxcc = $dxcc[1]->dxcc;
1348                                 $org_itu = $dxcc[1]->itu;
1349                                 $org_cq = $dxcc[1]->cq;                                         
1350                         }
1351                         ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1352                         next unless $filter;
1353                 } 
1354                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1355                         next if $dxchan == $self;
1356                         if ($hops) {
1357                                 $routeit = $line;
1358                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1359                         } else {
1360                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1361                                 next unless $routeit;
1362                         }
1363                         if ($filter) {
1364                                 $dxchan->send($routeit) if $routeit;
1365                         } else {
1366                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1367                                 
1368                         }
1369                 } elsif ($dxchan->is_user) {
1370                         unless ($dxchan->{ann}) {
1371                                 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1372                         }
1373                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1374                         my $buf = "$to$target de $_[0]: $text";
1375                         $buf =~ s/\%5E/^/g;
1376                         $buf .= "\a\a" if $dxchan->{beep};
1377                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1378                                 $dxchan->send($buf);
1379                         } else {
1380                                 $dxchan->delay($buf);
1381                         }
1382                 }                                       
1383         }
1384 }
1385
1386 sub send_local_config
1387 {
1388         my $self = shift;
1389         my $n;
1390         my @nodes;
1391         my @localnodes;
1392         my @remotenodes;
1393                 
1394         # send our nodes
1395         if ($self->{isolate}) {
1396                 @localnodes = (DXCluster->get_exact($main::mycall));
1397         } else {
1398                 # create a list of all the nodes that are not connected to this connection
1399                 # and are not themselves isolated, this to make sure that isolated nodes
1400         # don't appear outside of this node
1401                 @nodes = DXNode::get_all();
1402                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1403                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1404                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1405                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1406                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1407                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1408         }
1409
1410         my @s = $me->pc19(@localnodes, @remotenodes);
1411         for (@s) {
1412                 my $routeit = adjust_hops($self, $_);
1413                 $self->send($routeit) if $routeit;
1414         }
1415         
1416         # get all the users connected on the above nodes and send them out
1417         foreach $n (@localnodes, @remotenodes) {
1418                 my @users = values %{$n->list};
1419                 my @s = pc16($n, @users);
1420                 for (@s) {
1421                         my $routeit = adjust_hops($self, $_);
1422                         $self->send($routeit) if $routeit;
1423                 }
1424         }
1425 }
1426
1427 #
1428 # route a message down an appropriate interface for a callsign
1429 #
1430 # is called route(to, pcline);
1431 #
1432 sub route
1433 {
1434         my ($self, $call, $line) = @_;
1435
1436         if (ref $self && $call eq $self->{call}) {
1437                 dbg('chan', "Trying to route back to source, dropped");
1438                 return;
1439         }
1440
1441         # always send it down the local interface if available
1442         my $dxchan = DXChannel->get($call);
1443         unless ($dxchan) {
1444                 my $cl = DXCluster->get_exact($call);
1445                 $dxchan = $cl->dxchan if $cl;
1446         }
1447         if ($dxchan) {
1448                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1449                 if ($routeit) {
1450                         $dxchan->send($routeit);
1451                 }
1452         }
1453 }
1454
1455 # broadcast a message to all clusters taking into account isolation
1456 # [except those mentioned after buffer]
1457 sub broadcast_ak1a
1458 {
1459         my $s = shift;                          # the line to be rebroadcast
1460         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1461         my @dxchan = DXChannel::get_all_nodes();
1462         my $dxchan;
1463         
1464         # send it if it isn't the except list and isn't isolated and still has a hop count
1465         foreach $dxchan (@dxchan) {
1466                 next if grep $dxchan == $_, @except;
1467                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1468                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1469         }
1470 }
1471
1472 # broadcast a message to all clusters ignoring isolation
1473 # [except those mentioned after buffer]
1474 sub broadcast_all_ak1a
1475 {
1476         my $s = shift;                          # the line to be rebroadcast
1477         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1478         my @dxchan = DXChannel::get_all_nodes();
1479         my $dxchan;
1480         
1481         # send it if it isn't the except list and isn't isolated and still has a hop count
1482         foreach $dxchan (@dxchan) {
1483                 next if grep $dxchan == $_, @except;
1484                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1485                 $dxchan->send($routeit);
1486         }
1487 }
1488
1489 # broadcast to all users
1490 # storing the spot or whatever until it is in a state to receive it
1491 sub broadcast_users
1492 {
1493         my $s = shift;                          # the line to be rebroadcast
1494         my $sort = shift;           # the type of transmission
1495         my $fref = shift;           # a reference to an object to filter on
1496         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1497         my @dxchan = DXChannel::get_all_users();
1498         my $dxchan;
1499         my @out;
1500         
1501         foreach $dxchan (@dxchan) {
1502                 next if grep $dxchan == $_, @except;
1503                 push @out, $dxchan;
1504         }
1505         broadcast_list($s, $sort, $fref, @out);
1506 }
1507
1508 # broadcast to a list of users
1509 sub broadcast_list
1510 {
1511         my $s = shift;
1512         my $sort = shift;
1513         my $fref = shift;
1514         my $dxchan;
1515         
1516         foreach $dxchan (@_) {
1517                 my $filter = 1;
1518                 
1519                 if ($sort eq 'dx') {
1520                     next unless $dxchan->{dx};
1521                         ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1522                         next unless $filter;
1523                 }
1524                 next if $sort eq 'ann' && !$dxchan->{ann};
1525                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1526                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1527                 next if $sort eq 'wx' && !$dxchan->{wx};
1528
1529                 $s =~ s/\a//og unless $dxchan->{beep};
1530
1531                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1532                         $dxchan->send($s);      
1533                 } else {
1534                         $dxchan->delay($s);
1535                 }
1536         }
1537 }
1538
1539
1540 #
1541 # obtain the hops from the list for this callsign and pc no 
1542 #
1543
1544 sub get_hops
1545 {
1546         my $pcno = shift;
1547         my $hops = $DXProt::hopcount{$pcno};
1548         $hops = $DXProt::def_hopcount if !$hops;
1549         return "H$hops";       
1550 }
1551
1552
1553 # adjust the hop count on a per node basis using the user loadable 
1554 # hop table if available or else decrement an existing one
1555 #
1556
1557 sub adjust_hops
1558 {
1559         my $self = shift;
1560         my $s = shift;
1561         my $call = $self->{call};
1562         my $hops;
1563         
1564         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1565                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1566                 confess "$call called adjust_hops with '$s'" unless $pcno;
1567                 my $ref = $nodehops{$call} if %nodehops;
1568                 if ($ref) {
1569                         my $newhops = $ref->{$pcno};
1570                         return "" if defined $newhops && $newhops == 0;
1571                         $newhops = $ref->{default} unless $newhops;
1572                         return "" if defined $newhops && $newhops == 0;
1573                         $newhops = $hops if !$newhops;
1574                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1575                 } else {
1576                         # simply decrement it
1577                         $hops--;
1578                         return "" if !$hops;
1579                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1580                 }
1581         }
1582         return $s;
1583 }
1584
1585
1586 # load hop tables
1587 #
1588 sub load_hops
1589 {
1590         my $self = shift;
1591         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1592         do "$main::data/hop_table.pl";
1593         return $@ if $@;
1594         return 0;
1595 }
1596
1597
1598 # add a ping request to the ping queues
1599 sub addping
1600 {
1601         my ($from, $to) = @_;
1602         my $ref = $pings{$to} || [];
1603         my $r = {};
1604         $r->{call} = $from;
1605         $r->{t} = [ gettimeofday ];
1606         route(undef, $to, pc51($to, $main::mycall, 1));
1607         push @$ref, $r;
1608         $pings{$to} = $ref;
1609 }
1610
1611 # add a rcmd request to the rcmd queues
1612 sub addrcmd
1613 {
1614         my ($self, $to, $cmd) = @_;
1615
1616         my $r = {};
1617         $r->{call} = $self->{call};
1618         $r->{t} = $main::systime;
1619         $r->{cmd} = $cmd;
1620         $rcmds{$to} = $r;
1621
1622         my $ref = DXCluster->get_exact($to);
1623     if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1624                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1625         } else {
1626                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1627         }
1628 }
1629
1630 sub disconnect
1631 {
1632         my $self = shift;
1633         my $nopc39 = shift;
1634
1635         if ($self->{conn} && !$nopc39) {
1636                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1637         }
1638
1639         $self->SUPER::disconnect;
1640 }
1641
1642
1643
1644 # send a talk message to this thingy
1645 #
1646 sub talk
1647 {
1648         my ($self, $from, $to, $via, $line) = @_;
1649         
1650         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1651         $self->send(DXProt::pc10($from, $to, $via, $line));
1652         Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1653 }
1654 1;
1655 __END__