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