some cosmetic changes to error messages on pc16/19
[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[1] eq $main::mycall || $field[2] eq $main::mycall) {
508                                 dbg('chan', "LOOP: trying to alter config on this node from outside!");
509                                 return;
510                         }
511                         if ($field[2] eq $main::myalias && DXChannel->get($field[1])) {
512                                 dbg('chan', "LOOP: trying to connect sysop from outside!");
513                                 return;
514                         }
515                         unless ($node) {
516                                 dbg('chan', "Node $field[1] not in config");
517                                 return;
518                         }
519                         unless ($node->isa('DXNode')) {
520                                 dbg('chan', "$field[1] is not a node");
521                                 return;
522                         }
523                         if ($node->dxchan != $self) {
524                                 dbg('chan', "LOOP: $field[1] came in on wrong channel");
525                                 return;
526                         }
527                         if (($dxchan = DXChannel->get($field[1])) && $dxchan != $self) {
528                                 dbg('chan', "LOOP: $field[1] connected locally");
529                                 return;
530                         }
531                         my $i;
532                                                 
533                         for ($i = 2; $i < $#field; $i++) {
534                                 my ($call, $confmode, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
535                                 next unless $call && $confmode && defined $here && is_callsign($call);
536                                 my $ref = DXCluster->get_exact($call); 
537                                 if ($ref) {
538                                         if ($ref->isa('DXNode')) {
539                                                 dbg('chan', "LOOP: $call is a node");
540                                                 next;
541                                         }
542                                         my $rcall = $ref->mynode->call;
543                                         dbg('chan', "LOOP: already have $call on $rcall");
544                                         next;
545                                 }
546                                 
547                                 $confmode = $confmode eq '*';
548                                 DXNodeuser->new($self, $node, $call, $confmode, $here);
549                                 
550                                 # add this station to the user database, if required
551                                 $call =~ s/-\d+$//o;        # remove ssid for users
552                                 my $user = DXUser->get_current($call);
553                                 $user = DXUser->new($call) if !$user;
554                                 $user->homenode($node->call) if !$user->homenode;
555                                 $user->node($node->call);
556                                 $user->lastin($main::systime) unless DXChannel->get($call);
557                                 $user->put;
558                         }
559                         
560                         # queue up any messages (look for privates only)
561                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
562                         last SWITCH;
563                 }
564                 
565                 if ($pcno == 17) {              # remove a user
566                         my $node = DXCluster->get_exact($field[2]);
567                         my $dxchan;
568                         if (!$node && ($dxchan = DXChannel->get($field[2]))) {
569                                 # add it to the node table if it isn't present and it's
570                                 # connected locally
571                                 $node = DXNode->new($dxchan, $field[2], 0, 1, 5400);
572                                 dbg('chan', "$field[2] no PC19 yet, autovivified as node");
573 #                               broadcast_ak1a(pc19($dxchan, $node), $dxchan, $self) unless $dxchan->{isolate};
574                         }
575                         if ($field[1] eq $main::mycall || $field[2] eq $main::mycall) {
576                                 dbg('chan', "LOOP: trying to alter config on this node from outside!");
577                                 return;
578                         }
579                         if ($field[1] eq $main::myalias && DXChannel->get($field[1])) {
580                                 dbg('chan', "LOOP: trying to disconnect sysop from outside!");
581                                 return;
582                         }
583                         unless ($node) {
584                                 dbg('chan', "Node $field[2] not in config");
585                                 return;
586                         }
587                         unless ($node->isa('DXNode')) {
588                                 dbg('chan', "LOOP: $field[2] is not a node");
589                                 return;
590                         }
591                         if ($node->dxchan != $self) {
592                                 dbg('chan', "LOOP: $field[2] came in on wrong channel");
593                                 return;
594                         }
595                         if (($dxchan = DXChannel->get($field[2])) && $dxchan != $self) {
596                                 dbg('chan', "LOOP: $field[2] connected locally");
597                                 return;
598                         }
599                         my $ref = DXCluster->get_exact($field[1]);
600                         if ($ref) {
601                                 $ref->del;
602                         } else {
603                                 dbg('chan', "$field[1] not known" );
604                                 return;
605                         }
606                         last SWITCH;
607                 }
608                 
609                 if ($pcno == 18) {              # link request
610                         $self->state('init');   
611
612                         # first clear out any nodes on this dxchannel
613                         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
614                         foreach my $node (@gonenodes) {
615                                 next if $node->dxchan == $DXProt::me;
616                                 broadcast_ak1a(pc21($node->call, 'Gone, re-init') , $self) unless $self->{isolate}; 
617                                 $node->del();
618                         }
619                         $self->send_local_config();
620                         $self->send(pc20());
621                         return;             # we don't pass these on
622                 }
623                 
624                 if ($pcno == 19) {              # incoming cluster list
625                         my $i;
626                         my $newline = "PC19^";
627                         for ($i = 1; $i < $#field-1; $i += 4) {
628                                 my $here = $field[$i];
629                                 my $call = uc $field[$i+1];
630                                 my $confmode = $field[$i+2];
631                                 my $ver = $field[$i+3];
632                                 next unless defined $here && defined $confmode && is_callsign($call);
633
634                                 $ver = 5400 if !$ver && $allowzero;
635                                 
636                                 # now check the call over
637                                 my $node = DXCluster->get_exact($call);
638                                 if ($node) {
639                                         my $dxchan;
640                                         if (($dxchan = DXChannel->get($call)) && $dxchan != $self) {
641                                                 dbg('chan', "LOOP: $call connected locally");
642                                         }
643                                     if ($node->dxchan != $self) {
644                                                 dbg('chan', "LOOP: $call come in on wrong channel");
645                                                 next;
646                                         }
647                                         my $rcall = $node->mynode->call;
648                                         dbg('chan', "already have $call on $rcall");
649                                         next;
650                                 }
651                                 
652                                 # check for sane parameters
653                                 next if $ver < 5000; # only works with version 5 software
654                                 next if length $call < 3; # min 3 letter callsigns
655
656                                 # add it to the nodes table and outgoing line
657                                 $newline .= "$here^$call^$confmode^$ver^";
658                                 DXNode->new($self, $call, $confmode, $here, $ver);
659                                 
660                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
661                                 my $mref = DXMsg::get_busy($call);
662                                 $mref->stop_msg($call) if $mref;
663                                 
664                                 # add this station to the user database, if required (don't remove SSID from nodes)
665                                 my $user = DXUser->get_current($call);
666                                 if (!$user) {
667                                         $user = DXUser->new($call);
668                                         $user->sort('A');
669                                         $user->priv(1);                   # I have relented and defaulted nodes
670                                         $self->{priv} = 1;                # to user RCMDs allowed
671                                         $user->homenode($call);
672                                         $user->node($call);
673                                 }
674                                 $user->lastin($main::systime) unless DXChannel->get($call);
675                                 $user->put;
676                         }
677                         
678                         return if $newline eq "PC19^";
679
680                         # add hop count 
681                         $newline .=  get_hops(19) . "^";
682                         $line = $newline;
683                         last SWITCH;
684                 }
685                 
686                 if ($pcno == 20) {              # send local configuration
687                         $self->send_local_config();
688                         $self->send(pc22());
689                         $self->state('normal');
690                         return;
691                 }
692                 
693                 if ($pcno == 21) {              # delete a cluster from the list
694                         my $call = uc $field[1];
695                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
696                                 my $node = DXCluster->get_exact($call);
697                                 if ($node) {
698                                         unless ($node->isa('DXNode')) {
699                                                 dbg('chan', "$call is not a node");
700                                                 return;
701                                         }
702                                         if ($call eq $self->{call}) {
703                                                 dbg('chan', "LOOP: Trying to disconnect myself with PC21");
704                                                 return;
705                                         } 
706                                         if ($node->dxchan != $self) {
707                                                 dbg('chan', "LOOP: $call come in on wrong channel");
708                                                 return;
709                                         }
710                                         my $dxchan;
711                                         if ($dxchan = DXChannel->get($call)) {
712                                                 dbg('chan', "LOOP: $call connected locally");
713                                                 return;
714                                         }
715                                         $node->del();
716                                 } else {
717                                         dbg('chan', "$call not in table, dropped");
718                                         return;
719                                 }
720                         } else {
721                                 dbg('chan', "I WILL NOT be disconnected!");
722                                 return;
723                         }
724                         last SWITCH;
725                 }
726                 
727                 if ($pcno == 22) {
728                         $self->state('normal');
729                         return;
730                 }
731                                 
732                 if ($pcno == 23 || $pcno == 27) { # WWV info
733                         
734                         # route 'foreign' pc27s 
735                         if ($pcno == 27) {
736                                 if ($field[8] ne $main::mycall) {
737                                         $self->route($field[8], $line);
738                                         return;
739                                 }
740                         }
741
742                         # do some de-duping
743                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
744                         my $sfi = unpad($field[3]);
745                         my $k = unpad($field[4]);
746                         my $i = unpad($field[5]);
747                         my ($r) = $field[6] =~ /R=(\d+)/;
748                         $r = 0 unless $r;
749                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
750                                 dbg('chan', "WWV Date ($field[1] $field[2]) out of range");
751                                 return;
752                         }
753                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
754                                 dbg('chan', "Dup WWV Spot ignored\n");
755                                 return;
756                         }
757                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
758                 
759                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
760
761                         my $rep;
762                         eval {
763                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
764                         };
765 #                       dbg('local', "Local::wwv2 error $@") if $@;
766                         return if $rep;
767
768                         # DON'T be silly and send on PC27s!
769                         return if $pcno == 27;
770
771                         # broadcast to the eager world
772                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
773                         return;
774                 }
775                 
776                 if ($pcno == 24) {              # set here status
777                         my $call = uc $field[1];
778                         my $ref = DXCluster->get_exact($call);
779                         $ref->here($field[2]) if $ref;
780                         last SWITCH;
781                 }
782                 
783                 if ($pcno == 25) {      # merge request
784                         if ($field[1] ne $main::mycall) {
785                                 $self->route($field[1], $line);
786                                 return;
787                         }
788                         if ($field[2] eq $main::mycall) {
789                                 dbg('chan', "Trying to merge to myself, ignored");
790                                 return;
791                         }
792
793                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
794                         
795                         # spots
796                         if ($field[3] > 0) {
797                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
798                                 my $in;
799                                 foreach $in (@in) {
800                                         $self->send(pc26(@{$in}[0..4], $field[2]));
801                                 }
802                         }
803
804                         # wwv
805                         if ($field[4] > 0) {
806                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
807                                 my $in;
808                                 foreach $in (@in) {
809                                         $self->send(pc27(@{$in}[0..5], $field[2]));
810                                 }
811                         }
812                         return;
813                 }
814
815                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
816                         if ($pcno == 49 || $field[1] eq $main::mycall) {
817                                 DXMsg::process($self, $line);
818                         } else {
819                                 $self->route($field[1], $line) unless $self->is_clx;
820                         }
821                         return;
822                 }
823                 
824                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
825                         if ($field[1] eq $main::mycall) {
826                                 my $ref = DXUser->get_current($field[2]);
827                                 my $cref = DXCluster->get($field[2]);
828                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
829                                 unless (!$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
830                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
831                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
832                                                 my $oldpriv = $self->{priv};
833                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
834                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
835                                                 $self->{priv} = $oldpriv;
836                                                 for (@in) {
837                                                         s/\s*$//og;
838                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
839                                                         Log('rcmd', 'out', $field[2], $_);
840                                                 }
841                                                 delete $self->{remotecmd};
842                                         } else {
843                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
844                                         }
845                                 } else {
846                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
847                                 }
848                         } else {
849                                 my $ref = DXUser->get_current($field[1]);
850                                 if ($ref && $ref->is_clx) {
851                                         route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
852                                 } else {
853                                         $self->route($field[1], $line);
854                                 }
855                         }
856                         return;
857                 }
858                 
859                 if ($pcno == 35) {              # remote command replies
860                         if ($field[1] eq $main::mycall) {
861                                 my $s = $rcmds{$field[2]};
862                                 if ($s) {
863                                         my $dxchan = DXChannel->get($s->{call});
864                                         $dxchan->send($field[3]) if $dxchan;
865                                         delete $rcmds{$field[2]} if !$dxchan;
866                                 } else {
867                                         # send unsolicited ones to the sysop
868                                         my $dxchan = DXChannel->get($main::myalias);
869                                         $dxchan->send($field[3]) if $dxchan;
870                                 }
871                         } else {
872                                 my $ref = DXUser->get_current($field[1]);
873                                 if ($ref && $ref->is_clx) {
874                                         route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
875                                 } else {
876                                         $self->route($field[1], $line);
877                                 }
878                         }
879                         return;
880                 }
881                 
882                 # for pc 37 see 44 onwards
883
884                 if ($pcno == 38) {              # node connected list from neighbour
885                         return;
886                 }
887                 
888                 if ($pcno == 39) {              # incoming disconnect
889                         if ($field[1] eq $self->{call}) {
890                                 $self->disconnect(1);
891                         } else {
892                                 dbg('chan', "LOOP: came in on wrong channel");
893                         }
894                         return;
895                 }
896                 
897                 if ($pcno == 41) {              # user info
898                         # add this station to the user database, if required
899                         my $user = DXUser->get_current($field[1]);
900                         if (!$user) {
901                                 # then try without an SSID
902                                 $field[1] =~ s/-\d+$//o;
903                                 $user = DXUser->get_current($field[1]);
904                         }
905                         $user = DXUser->new($field[1]) if !$user;
906                         
907                         if ($field[2] == 1) {
908                                 $user->name($field[3]);
909                         } elsif ($field[2] == 2) {
910                                 $user->qth($field[3]);
911                         } elsif ($field[2] == 3) {
912                                 my ($lat, $long) = DXBearing::stoll($field[3]);
913                                 $user->lat($lat);
914                                 $user->long($long);
915                                 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
916                         } elsif ($field[2] == 4) {
917                                 $user->homenode($field[3]);
918                         }
919                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
920                         $user->put;
921                         last SWITCH;
922                 }
923                 if ($pcno == 43) {
924                         last SWITCH;
925                 }
926                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
927                         DXDb::process($self, $line);
928                         return;
929                 }
930                 
931                 if ($pcno == 50) {              # keep alive/user list
932                         my $node = DXCluster->get_exact($field[1]);
933                         if ($node) {
934                                 return unless $node->isa('DXNode');
935                                 return unless $node->dxchan == $self;
936                                 $node->update_users($field[2]);
937                         }
938                         last SWITCH;
939                 }
940                 
941                 if ($pcno == 51) {              # incoming ping requests/answers
942                         
943                         # is it for us?
944                         if ($field[1] eq $main::mycall) {
945                                 my $flag = $field[3];
946                                 if ($flag == 1) {
947                                         $self->send(pc51($field[2], $field[1], '0'));
948                                 } else {
949                                         # it's a reply, look in the ping list for this one
950                                         my $ref = $pings{$field[2]};
951                                         if ($ref) {
952                                                 my $tochan =  DXChannel->get($field[2]);
953                                                 while (@$ref) {
954                                                         my $r = shift @$ref;
955                                                         my $dxchan = DXChannel->get($r->{call});
956                                                         next unless $dxchan;
957                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
958                                                         if ($dxchan->is_user) {
959                                                                 my $s = sprintf "%.2f", $t; 
960                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
961                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
962                                                         } elsif ($dxchan->is_node) {
963                                                                 if ($tochan) {
964                                                                         $tochan->{nopings} = 2; # pump up the timer
965                                                                         push @{$tochan->{pingtime}}, $t;
966                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
967                                                                         my $st;
968                                                                         for (@{$tochan->{pingtime}}) {
969                                                                                 $st += $_;
970                                                                         }
971                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
972                                                                 }
973                                                         } 
974                                                 }
975                                         }
976                                 }
977                         } else {
978                                 # route down an appropriate thingy
979                                 $self->route($field[1], $line);
980                         }
981                         return;
982                 }
983
984                 if ($pcno == 75) {              # dunno but route it
985                         if ($field[1] ne $main::mycall) {
986                                 $self->route($field[1], $line);
987                         }
988                         return;
989                 }
990
991                 if ($pcno == 73) {  # WCY broadcasts
992                         
993                         # do some de-duping
994                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
995                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
996                                 dbg('chan', "WCY Date ($field[1] $field[2]) out of range");
997                                 return;
998                         }
999                         @field = map { unpad($_) } @field;
1000                         if (WCY::dup($d,@field[3..7])) {
1001                                 dbg('chan', "Dup WCY Spot ignored\n");
1002                                 return;
1003                         }
1004                 
1005                         my $wcy = WCY::update($d, @field[2..12]);
1006
1007                         my $rep;
1008                         eval {
1009                                 $rep = Local::wwv($self, @field[1..12]);
1010                         };
1011                         # dbg('local', "Local::wcy error $@") if $@;
1012                         return if $rep;
1013
1014                         # broadcast to the eager world
1015                         send_wcy_spot($self, $line, $d, @field[2..12]);
1016                         return;
1017                 }
1018
1019                 if ($pcno == 84) { # remote commands (incoming)
1020                         if ($field[1] eq $main::mycall) {
1021                                 my $ref = DXUser->get_current($field[2]);
1022                                 my $cref = DXCluster->get($field[2]);
1023                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
1024                                 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->mynode->call ne $ref->homenode) {    # not allowed to relay RCMDS!
1025                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
1026                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1027                                                 my $oldpriv = $self->{priv};
1028                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
1029                                                 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1030                                                 $self->{priv} = $oldpriv;
1031                                                 for (@in) {
1032                                                         s/\s*$//og;
1033                                                         $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1034                                                         Log('rcmd', 'out', $field[2], $_);
1035                                                 }
1036                                                 delete $self->{remotecmd};
1037                                         } else {
1038                                                 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1039                                         }
1040                                 } else {
1041                                         $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1042                                 }
1043                         } else {
1044                                 my $ref = DXUser->get_current($field[1]);
1045                                 if ($ref && $ref->is_clx) {
1046                                         $self->route($field[1], $line);
1047                                 } else {
1048                                         route($field[1], pc34($field[2], $field[1], $field[4]));
1049                                 }
1050                         }
1051                         return;
1052                 }
1053
1054                 if ($pcno == 85) {              # remote command replies
1055                         if ($field[1] eq $main::mycall) {
1056                                 my $dxchan = DXChannel->get($field[3]);
1057                                 if ($dxchan) {
1058                                         $dxchan->send($field[4]);
1059                                 } else {
1060                                         my $s = $rcmds{$field[2]};
1061                                         if ($s) {
1062                                                 $dxchan = DXChannel->get($s->{call});
1063                                                 $dxchan->send($field[4]) if $dxchan;
1064                                                 delete $rcmds{$field[2]} if !$dxchan;
1065                                         } else {
1066                                                 # send unsolicited ones to the sysop
1067                                                 my $dxchan = DXChannel->get($main::myalias);
1068                                                 $dxchan->send($field[4]) if $dxchan;
1069                                         }
1070                                 }
1071                         } else {
1072                                 my $ref = DXUser->get_current($field[1]);
1073                                 if ($ref && $ref->is_clx) {
1074                                         $self->route($field[1], $line);
1075                                 } else {
1076                                         route($field[1], pc35($field[2], $field[1], $field[4]));
1077                                 }
1078                         }
1079                         return;
1080                 }
1081         }
1082          
1083         # if get here then rebroadcast the thing with its Hop count decremented (if
1084         # there is one). If it has a hop count and it decrements to zero then don't
1085         # rebroadcast it.
1086         #
1087         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1088         #        REBROADCAST!!!!
1089         #
1090          
1091         unless ($self->{isolate}) {
1092                 broadcast_ak1a($line, $self); # send it to everyone but me
1093         }
1094 }
1095
1096 #
1097 # This is called from inside the main cluster processing loop and is used
1098 # for despatching commands that are doing some long processing job
1099 #
1100 sub process
1101 {
1102         my $t = time;
1103         my @dxchan = DXChannel->get_all();
1104         my $dxchan;
1105         
1106         foreach $dxchan (@dxchan) {
1107                 next unless $dxchan->is_node();
1108                 next if $dxchan == $me;
1109                 
1110                 # send a pc50 out on this channel
1111                 if ($t >= $dxchan->pc50_t + $DXProt::pc50_interval) {
1112                         $dxchan->send(pc50(scalar DXChannel::get_all_users));
1113                         $dxchan->pc50_t($t);
1114                 } 
1115
1116                 # send a ping out on this channel
1117                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1118                         if ($dxchan->{nopings} <= 0) {
1119                                 $dxchan->disconnect;
1120                         } else {
1121                                 addping($main::mycall, $dxchan->call);
1122                                 $dxchan->{nopings} -= 1;
1123                                 $dxchan->{lastping} = $t;
1124                         }
1125                 }
1126         }
1127         
1128         my $key;
1129         my $val;
1130         my $cutoff;
1131         if ($main::systime - 3600 > $last_hour) {
1132 #               Spot::process;
1133 #               Geomag::process;
1134 #               AnnTalk::process;
1135                 $last_hour = $main::systime;
1136         }
1137 }
1138
1139 #
1140 # finish up a pc context
1141 #
1142 sub finish
1143 {
1144         my $self = shift;
1145         my $call = $self->call;
1146         my $conn = shift;
1147         my $ref = DXCluster->get_exact($call);
1148         
1149         # unbusy and stop and outgoing mail
1150         my $mref = DXMsg::get_busy($call);
1151         $mref->stop_msg($call) if $mref;
1152         
1153         # broadcast to all other nodes that all the nodes connected to via me are gone
1154         my @gonenodes = map { $_->dxchan == $self ? $_ : () } DXNode::get_all();
1155         my $node;
1156         
1157         foreach $node (@gonenodes) {
1158                 next if $node->call eq $call;
1159                 broadcast_ak1a(pc21($node->call, 'Gone') , $self) unless $self->{isolate}; 
1160                 $node->del();
1161         }
1162
1163         # remove outstanding pings
1164         delete $pings{$call};
1165         
1166         # now broadcast to all other ak1a nodes that I have gone
1167         broadcast_ak1a(pc21($call, 'Gone.'), $self) unless $self->{isolate};
1168
1169         # I was the last node visited
1170     $self->user->node($main::mycall);
1171
1172         # send info to all logged in thingies
1173         $self->tell_login('logoutn');
1174
1175         Log('DXProt', $call . " Disconnected");
1176         $ref->del() if $ref;
1177 }
1178
1179 #
1180 # some active measures
1181 #
1182 sub send_dx_spot
1183 {
1184         my $self = shift;
1185         my $line = shift;
1186         my @dxchan = DXChannel->get_all();
1187         my $dxchan;
1188         
1189         # send it if it isn't the except list and isn't isolated and still has a hop count
1190         # taking into account filtering and so on
1191         foreach $dxchan (@dxchan) {
1192                 my $routeit;
1193                 my ($filter, $hops);
1194
1195                 if ($dxchan->{spotsfilter}) {
1196                     ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1197                         next unless $filter;
1198                 }
1199                 
1200                 if ($dxchan->is_node) {
1201                         next if $dxchan == $self;
1202                         if ($hops) {
1203                                 $routeit = $line;
1204                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1205                         } else {
1206                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1207                                 next unless $routeit;
1208                         }
1209                         if ($filter) {
1210                                 $dxchan->send($routeit) if $routeit;
1211                         } else {
1212                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1213                         }
1214                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1215                         my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1216                         $buf .= "\a\a" if $dxchan->{beep};
1217                         $buf =~ s/\%5E/^/g;
1218                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1219                                 $dxchan->send($buf);
1220                         } else {
1221                                 $dxchan->delay($buf);
1222                         }
1223                 }                                       
1224         }
1225 }
1226
1227 sub send_wwv_spot
1228 {
1229         my $self = shift;
1230         my $line = shift;
1231         my @dxchan = DXChannel->get_all();
1232         my $dxchan;
1233         
1234         # send it if it isn't the except list and isn't isolated and still has a hop count
1235         # taking into account filtering and so on
1236         foreach $dxchan (@dxchan) {
1237                 my $routeit;
1238                 my ($filter, $hops);
1239
1240                 if ($dxchan->{wwvfilter}) {
1241                          ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call} );
1242                          next unless $filter;
1243                 }
1244                 if ($dxchan->is_node) {
1245                         next if $dxchan == $self;
1246                         if ($hops) {
1247                                 $routeit = $line;
1248                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1249                         } else {
1250                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1251                                 next unless $routeit;
1252                         }
1253                         if ($filter) {
1254                                 $dxchan->send($routeit) if $routeit;
1255                         } else {
1256                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1257                                 
1258                         }
1259                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1260                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1261                         $buf .= "\a\a" if $dxchan->{beep};
1262                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1263                                 $dxchan->send($buf);
1264                         } else {
1265                                 $dxchan->delay($buf);
1266                         }
1267                 }                                       
1268         }
1269 }
1270
1271 sub send_wcy_spot
1272 {
1273         my $self = shift;
1274         my $line = shift;
1275         my @dxchan = DXChannel->get_all();
1276         my $dxchan;
1277         
1278         # send it if it isn't the except list and isn't isolated and still has a hop count
1279         # taking into account filtering and so on
1280         foreach $dxchan (@dxchan) {
1281                 my $routeit;
1282                 my ($filter, $hops);
1283
1284                 if ($dxchan->{wcyfilter}) {
1285                          ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call} );
1286                          next unless $filter;
1287                 }
1288                 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1289                         next if $dxchan == $self;
1290                         if ($hops) {
1291                                 $routeit = $line;
1292                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1293                         } else {
1294                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1295                                 next unless $routeit;
1296                         }
1297                         if ($filter) {
1298                                 $dxchan->send($routeit) if $routeit;
1299                         } else {
1300                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1301                         }
1302                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1303                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1304                         $buf .= "\a\a" if $dxchan->{beep};
1305                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1306                                 $dxchan->send($buf);
1307                         } else {
1308                                 $dxchan->delay($buf);
1309                         }
1310                 }                                       
1311         }
1312 }
1313
1314 # send an announce
1315 sub send_announce
1316 {
1317         my $self = shift;
1318         my $line = shift;
1319         my @dxchan = DXChannel->get_all();
1320         my $dxchan;
1321         my $text = unpad($_[2]);
1322         my $target;
1323         my $to = 'To ';
1324                                 
1325         if ($_[3] eq '*') {     # sysops
1326                 $target = "SYSOP";
1327         } elsif ($_[3] gt ' ') { # speciality list handling
1328                 my ($name) = split /\./, $_[3]; 
1329                 $target = "$name"; # put the rest in later (if bothered) 
1330         } 
1331         
1332         if ($_[5] eq '1') {
1333                 $target = "WX"; 
1334                 $to = '';
1335         }
1336         $target = "All" if !$target;
1337         
1338         Log('ann', $target, $_[0], $text);
1339
1340         # send it if it isn't the except list and isn't isolated and still has a hop count
1341         # taking into account filtering and so on
1342         foreach $dxchan (@dxchan) {
1343                 my $routeit;
1344                 my ($filter, $hops);
1345
1346                 if ($dxchan->{annfilter}) {
1347                         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1348                         my @dxcc = Prefix::extract($_[0]);
1349                         if (@dxcc > 0) {
1350                                 $ann_dxcc = $dxcc[1]->dxcc;
1351                                 $ann_itu = $dxcc[1]->itu;
1352                                 $ann_cq = $dxcc[1]->cq;                                         
1353                         }
1354                         @dxcc = Prefix::extract($_[4]);
1355                         if (@dxcc > 0) {
1356                                 $org_dxcc = $dxcc[1]->dxcc;
1357                                 $org_itu = $dxcc[1]->itu;
1358                                 $org_cq = $dxcc[1]->cq;                                         
1359                         }
1360                         ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1361                         next unless $filter;
1362                 } 
1363                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1364                         next if $dxchan == $self;
1365                         if ($hops) {
1366                                 $routeit = $line;
1367                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1368                         } else {
1369                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1370                                 next unless $routeit;
1371                         }
1372                         if ($filter) {
1373                                 $dxchan->send($routeit) if $routeit;
1374                         } else {
1375                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1376                                 
1377                         }
1378                 } elsif ($dxchan->is_user) {
1379                         unless ($dxchan->{ann}) {
1380                                 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1381                         }
1382                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1383                         my $buf = "$to$target de $_[0]: $text";
1384                         $buf =~ s/\%5E/^/g;
1385                         $buf .= "\a\a" if $dxchan->{beep};
1386                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1387                                 $dxchan->send($buf);
1388                         } else {
1389                                 $dxchan->delay($buf);
1390                         }
1391                 }                                       
1392         }
1393 }
1394
1395 sub send_local_config
1396 {
1397         my $self = shift;
1398         my $n;
1399         my @nodes;
1400         my @localnodes;
1401         my @remotenodes;
1402                 
1403         # send our nodes
1404         if ($self->{isolate}) {
1405                 @localnodes = (DXCluster->get_exact($main::mycall));
1406         } else {
1407                 # create a list of all the nodes that are not connected to this connection
1408                 # and are not themselves isolated, this to make sure that isolated nodes
1409         # don't appear outside of this node
1410                 @nodes = DXNode::get_all();
1411                 @nodes = grep { $_->{call} ne $main::mycall } @nodes;
1412                 @nodes = grep { $_->dxchan != $self } @nodes if @nodes;
1413                 @nodes = grep { !$_->dxchan->{isolate} } @nodes if @nodes;
1414                 @localnodes = grep { $_->dxchan->{call} eq $_->{call} } @nodes if @nodes;
1415                 unshift @localnodes, DXCluster->get_exact($main::mycall);
1416                 @remotenodes = grep { $_->dxchan->{call} ne $_->{call} } @nodes if @nodes;
1417         }
1418
1419         my @s = $me->pc19(@localnodes, @remotenodes);
1420         for (@s) {
1421                 my $routeit = adjust_hops($self, $_);
1422                 $self->send($routeit) if $routeit;
1423         }
1424         
1425         # get all the users connected on the above nodes and send them out
1426         foreach $n (@localnodes, @remotenodes) {
1427                 my @users = values %{$n->list};
1428                 my @s = pc16($n, @users);
1429                 for (@s) {
1430                         my $routeit = adjust_hops($self, $_);
1431                         $self->send($routeit) if $routeit;
1432                 }
1433         }
1434 }
1435
1436 #
1437 # route a message down an appropriate interface for a callsign
1438 #
1439 # is called route(to, pcline);
1440 #
1441 sub route
1442 {
1443         my ($self, $call, $line) = @_;
1444
1445         if (ref $self && $call eq $self->{call}) {
1446                 dbg('chan', "Trying to route back to source, dropped");
1447                 return;
1448         }
1449
1450         # always send it down the local interface if available
1451         my $dxchan = DXChannel->get($call);
1452         unless ($dxchan) {
1453                 my $cl = DXCluster->get_exact($call);
1454                 $dxchan = $cl->dxchan if $cl;
1455         }
1456         if ($dxchan) {
1457                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1458                 if ($routeit) {
1459                         $dxchan->send($routeit);
1460                 }
1461         }
1462 }
1463
1464 # broadcast a message to all clusters taking into account isolation
1465 # [except those mentioned after buffer]
1466 sub broadcast_ak1a
1467 {
1468         my $s = shift;                          # the line to be rebroadcast
1469         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1470         my @dxchan = DXChannel::get_all_nodes();
1471         my $dxchan;
1472         
1473         # send it if it isn't the except list and isn't isolated and still has a hop count
1474         foreach $dxchan (@dxchan) {
1475                 next if grep $dxchan == $_, @except;
1476                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1477                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1478         }
1479 }
1480
1481 # broadcast a message to all clusters ignoring isolation
1482 # [except those mentioned after buffer]
1483 sub broadcast_all_ak1a
1484 {
1485         my $s = shift;                          # the line to be rebroadcast
1486         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1487         my @dxchan = DXChannel::get_all_nodes();
1488         my $dxchan;
1489         
1490         # send it if it isn't the except list and isn't isolated and still has a hop count
1491         foreach $dxchan (@dxchan) {
1492                 next if grep $dxchan == $_, @except;
1493                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1494                 $dxchan->send($routeit);
1495         }
1496 }
1497
1498 # broadcast to all users
1499 # storing the spot or whatever until it is in a state to receive it
1500 sub broadcast_users
1501 {
1502         my $s = shift;                          # the line to be rebroadcast
1503         my $sort = shift;           # the type of transmission
1504         my $fref = shift;           # a reference to an object to filter on
1505         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1506         my @dxchan = DXChannel::get_all_users();
1507         my $dxchan;
1508         my @out;
1509         
1510         foreach $dxchan (@dxchan) {
1511                 next if grep $dxchan == $_, @except;
1512                 push @out, $dxchan;
1513         }
1514         broadcast_list($s, $sort, $fref, @out);
1515 }
1516
1517 # broadcast to a list of users
1518 sub broadcast_list
1519 {
1520         my $s = shift;
1521         my $sort = shift;
1522         my $fref = shift;
1523         my $dxchan;
1524         
1525         foreach $dxchan (@_) {
1526                 my $filter = 1;
1527                 
1528                 if ($sort eq 'dx') {
1529                     next unless $dxchan->{dx};
1530                         ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1531                         next unless $filter;
1532                 }
1533                 next if $sort eq 'ann' && !$dxchan->{ann};
1534                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1535                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1536                 next if $sort eq 'wx' && !$dxchan->{wx};
1537
1538                 $s =~ s/\a//og unless $dxchan->{beep};
1539
1540                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1541                         $dxchan->send($s);      
1542                 } else {
1543                         $dxchan->delay($s);
1544                 }
1545         }
1546 }
1547
1548
1549 #
1550 # obtain the hops from the list for this callsign and pc no 
1551 #
1552
1553 sub get_hops
1554 {
1555         my $pcno = shift;
1556         my $hops = $DXProt::hopcount{$pcno};
1557         $hops = $DXProt::def_hopcount if !$hops;
1558         return "H$hops";       
1559 }
1560
1561
1562 # adjust the hop count on a per node basis using the user loadable 
1563 # hop table if available or else decrement an existing one
1564 #
1565
1566 sub adjust_hops
1567 {
1568         my $self = shift;
1569         my $s = shift;
1570         my $call = $self->{call};
1571         my $hops;
1572         
1573         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1574                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1575                 confess "$call called adjust_hops with '$s'" unless $pcno;
1576                 my $ref = $nodehops{$call} if %nodehops;
1577                 if ($ref) {
1578                         my $newhops = $ref->{$pcno};
1579                         return "" if defined $newhops && $newhops == 0;
1580                         $newhops = $ref->{default} unless $newhops;
1581                         return "" if defined $newhops && $newhops == 0;
1582                         $newhops = $hops if !$newhops;
1583                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1584                 } else {
1585                         # simply decrement it
1586                         $hops--;
1587                         return "" if !$hops;
1588                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1589                 }
1590         }
1591         return $s;
1592 }
1593
1594
1595 # load hop tables
1596 #
1597 sub load_hops
1598 {
1599         my $self = shift;
1600         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1601         do "$main::data/hop_table.pl";
1602         return $@ if $@;
1603         return 0;
1604 }
1605
1606
1607 # add a ping request to the ping queues
1608 sub addping
1609 {
1610         my ($from, $to) = @_;
1611         my $ref = $pings{$to} || [];
1612         my $r = {};
1613         $r->{call} = $from;
1614         $r->{t} = [ gettimeofday ];
1615         route(undef, $to, pc51($to, $main::mycall, 1));
1616         push @$ref, $r;
1617         $pings{$to} = $ref;
1618 }
1619
1620 # add a rcmd request to the rcmd queues
1621 sub addrcmd
1622 {
1623         my ($self, $to, $cmd) = @_;
1624
1625         my $r = {};
1626         $r->{call} = $self->{call};
1627         $r->{t} = $main::systime;
1628         $r->{cmd} = $cmd;
1629         $rcmds{$to} = $r;
1630
1631         my $ref = DXCluster->get_exact($to);
1632     if ($ref && $ref->dxchan && $ref->dxchan->is_clx) {
1633                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1634         } else {
1635                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1636         }
1637 }
1638
1639 sub disconnect
1640 {
1641         my $self = shift;
1642         my $nopc39 = shift;
1643
1644         if ($self->{conn} && !$nopc39) {
1645                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1646         }
1647
1648         $self->SUPER::disconnect;
1649 }
1650
1651
1652
1653 # send a talk message to this thingy
1654 #
1655 sub talk
1656 {
1657         my ($self, $from, $to, $via, $line) = @_;
1658         
1659         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1660         $self->send(DXProt::pc10($from, $to, $via, $line));
1661         Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1662 }
1663 1;
1664 __END__