5189746aa5ba35f28e62f649c686618f6720d68b
[spider.git] / perl / DXProtHandle.pm
1 #
2 #
3 # This module impliments the handlers for the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998-2007 Dirk Koopman G1TLH
6 #
7 #
8 #
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXProtVars;
19 use DXCommandmode;
20 use DXLog;
21 use Spot;
22 use DXProtout;
23 use DXDebug;
24 use Filter;
25 use Local;
26 use DXDb;
27 use AnnTalk;
28 use Geomag;
29 use WCY;
30 use BadWords;
31 use DXHash;
32 use Route;
33 use Route::Node;
34 use Script;
35 use DXCIDR;
36
37 use strict;
38 use warnings qw(all);
39 no warnings qw(uninitialized);
40
41
42
43 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
44                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
45                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
46                         $investigation_int $pc19_version $myprot_version
47                         %nodehops $baddx $badspotter $badnode $censorpc
48                         $allowzero $decode_dk0wcy $send_opernam @checklist
49                         $eph_pc15_restime $pc9x_past_age $pc9x_dupe_age
50                         $pc10_dupe_age $pc92_slug_changes $last_pc92_slug
51                         $pc92Ain $pc92Cin $pc92Din $pc92Kin $pc9x_time_tolerance
52                         $pc92filterdef $senderverify $pc11_dwell_time
53                    );
54
55 $pc9x_dupe_age = 60;                    # catch loops of circular (usually) D records
56 $pc10_dupe_age = 45;                    # just something to catch duplicate PC10->PC93 conversions
57 $pc92_slug_changes = 60*1;              # slug any changes going outward for this long
58 $last_pc92_slug = 0;                    # the last time we sent out any delayed add or del PC92s
59 $pc9x_time_tolerance = 15*60;   # the time on a pc9x is allowed to be out by this amount
60 $pc9x_past_age = (122*60)+              # maximum age in the past of a px9x (a config record might be the only
61 $pc9x_time_tolerance;           # thing a node might send - once an hour and we allow an extra hour for luck)
62                                 # this is actually the partition between "yesterday" and "today" but old.
63 $senderverify = 0;                              # 1 - check for forged PC11 or PC61.
64                                 # 2 - if forged, dump them.
65 $pc11_dwell_time = 2;                   # number of seconds to wait for a PC61 to come to substitute the PC11
66
67
68 $pc92filterdef = bless ([
69                           # tag, sort, field, priv, special parser
70                           ['call', 'c', 0],
71                           ['by', 'c', 0],
72                           ['dxcc', 'nc', 1],
73                           ['itu', 'ni', 2],
74                           ['zone', 'nz', 3],
75                          ], 'Filter::Cmd');
76
77 our %pc11q;
78 # this is a place to park an incoming PC11 in the sure and certain hope that
79 # a PC61 will be along soon. This has the side benefit that it will delay a
80 # a PC11 for one second - assuming that it is not removed by a PC61 version
81
82 # incoming talk commands
83 sub handle_10
84 {
85         my $self = shift;
86         my $pcno = shift;
87         my $line = shift;
88         my $origin = shift;
89         my $pc = shift;
90
91         # this is to catch loops caused by bad software ...
92         if (eph_dup($line, $pc10_dupe_age)) {
93                 return;
94         }
95
96
97         # is it for me or one of mine?
98         my ($from, $to, $via, $call, $dxchan);
99         $from = $pc->[1];
100         if ($pc->[5] gt ' ') {
101                 $via = $pc->[2];
102                 $to = $pc->[5];
103         } else {
104                 $to = $pc->[2];
105         }
106
107         # if this is a 'nodx' node then ignore it
108         if ($badnode->in($pc->[6]) || ($via && $badnode->in($via))) {
109                 dbg($line) if isdbg('nologchan');
110                 dbg("PCPROT: Bad Node, dropped");
111                 return;
112         }
113
114
115         my $nossid = $from;
116         $nossid =~ s/-\d+$//;
117         if ($badspotter->in($nossid)) {
118                 dbg($line) if isdbg('nologchan');
119                 dbg("PCPROT: Bad Spotter, dropped");
120                 return;
121         }
122
123         # if we are converting announces to talk is it a dup?
124         if ($ann_to_talk) {
125                 if (AnnTalk::is_talk_candidate($from, $pc->[3]) && AnnTalk::dup($from, $to, $pc->[3])) {
126                         dbg("PCPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
127                         return;
128                 }
129         }
130         
131         # will we allow it at all?
132         if ($censorpc) {
133                 my @bad;
134                 if (@bad = BadWords::check($pc->[3])) {
135                         my $bw = join ', ', @bad; 
136                         dbg($line) if isdbg('nologchan');
137                         dbg("PCPROT: Badwords: '$bw', dropped");
138                         return;
139                 }
140         }
141
142         # convert this to a PC93, coming from mycall with origin set and process it as such
143         $main::me->normal(pc93($to, $from, $via, $pc->[3], $pc->[6]));
144 }
145
146 my %pc11_saved;                                 # delayed PC11s
147 my $pc11_rx;                                    # no of pc11 rxed
148 my $pc61_rx;                                    # no of pc61 rxed
149 my $pc11_to_61;                                 # no of 'better' pc61, that replaced stored waiting pc11
150 my $rpc11_to_61;                                # no of pc11s 'promoted' to pc61 by IP captured in routing table
151
152 # DX Spot handling
153 sub handle_11
154 {
155         my $self = shift;
156         my $pcno = shift;
157         my $line = shift;
158         my $origin = shift;
159         my $pc = shift;
160         my $recurse = shift || 0;
161
162         # route 'foreign' pc26s
163         if ($pcno == 26) {
164                 if ($pc->[7] ne $main::mycall) {
165                         $self->route($pc->[7], $line);
166                         return;
167                 }
168         }
169
170         my $type = 
171
172         dbg("INPUT $self->{call} -> $line origin $origin recurse: $recurse") if isdbg("pc11") || isdbg("pc61"); 
173
174 #       my ($hops) = $pc->[8] =~ /^H(\d+)/;
175
176         # is the spotted callsign blank? This should really be trapped earlier but it
177         # could break other protocol sentences. Also check for lower case characters.
178         if ($pc->[2] =~ /^\s*$/) {
179                 dbg($line) if isdbg('nologchan');
180                 dbg("PCPROT: blank callsign, dropped");
181                 return;
182         }
183         if ($pc->[2] =~ /[a-z]/) {
184                 dbg($line) if isdbg('nologchan');
185                 dbg("PCPROT: lowercase characters, dropped");
186                 return;
187         }
188
189
190         # if this is a 'nodx' node then ignore it
191         if ($badnode->in($pc->[7])) {
192                 dbg($line) if isdbg('nologchan');
193                 dbg("PCPROT: Bad Node $pc->[7], dropped");
194                 return;
195         }
196
197         # convert the date to a unix date
198         my $d = cltounix($pc->[3], $pc->[4]);
199         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
200         if (!$d || (($pcno == 11 || $pcno == 61) && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
201                 dbg($line) if isdbg('nologchan');
202                 dbg("PCPROT: Spot ignored, invalid date or out of range ($pc->[3] $pc->[4])\n");
203                 return;
204         }
205
206         # is it 'baddx'
207         if ($baddx->in($pc->[2])) {
208                 dbg("PCPROT: Bad DX spot '$pc->[2]', ignored");
209                 dbg($line) if isdbg('nologchan');
210                 return;
211         }
212
213         # is it claiming to be BUST(ed)
214         $pc->[5] =~ s/^\s+//;                   # take any leading blanks off
215         $pc->[2] = unpad($pc->[2]);             # take off leading and trailing blanks from spotted callsign
216         if ($pc->[2] =~ /BUST\w*$/) {
217                 dbg($line) if isdbg('nologchan');
218                 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
219                 return;
220         }
221         
222         my $nossid = $pc->[6];
223     $nossid =~ s/-\d+$//;
224
225         my @spot = Spot::prepare($pc->[1], $pc->[2], $d, $pc->[5], $nossid, $pc->[7], $pc->[8]);
226
227         #   $f0 = frequency
228         #   $f1 = call
229         #   $f2 = date in unix format
230         #   $f3 = comment
231         #   $f4 = spotter
232         #   $f5 = spotted dxcc country
233         #   $f6 = spotter dxcc country
234         #   $f7 = origin
235         #   $f8 = spotted itu
236         #   $f9 = spotted cq zone
237         #   $f10 = spotter itu
238         #   $f11 = spotter cq zone
239         #   $f12 = spotted us state
240         #   $f13 = spotter us state
241         #   $f14 = ip address
242
243
244         # is this is a 'bad spotter' or an unknown user then ignore it. 
245         if ($badspotter->in($nossid)) {
246                 dbg($line) if isdbg('nologchan');
247                 dbg("PCPROT: Bad Spotter $pc->[6], dropped");
248                 return;
249         }
250
251         # global spot filtering on INPUT
252         if ($self->{inspotsfilter}) {
253                 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
254                 unless ($filter) {
255                         dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
256                         return;
257                 }
258         }
259
260
261         # this is where we decide to delay PC11s in the hope that a PC61 will be along soon.
262         
263         my $key = join '|', @spot[0..2,4,7]; # not including text
264
265         unless ($recurse) {
266                 if ($pcno == 61) {
267                         if (Spot::dup_find(@spot[0..4,7])) {
268                                 dbg("DUPE $pc->[0] $self->{call} -> key: $key dumped") if isdbg('pc11');
269                                 return;
270                         } else {
271                                 ++$pc61_rx;
272                         }
273
274                         if ($pc11_saved{$key}) {
275                                 ++$pc11_to_61;
276                                 my $percent = $pc11_rx ? $pc11_to_61 * 100 / $pc11_rx : 0;
277                                 dbg(sprintf("PROMOTED BETTER $self->{call} -> $pc->[0] $key, using PC61, saved PC11 DUMPED: $pc61_rx pc11: $pc11_rx better pc61: $pc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
278                                 delete $pc11_saved{$key};
279                         }
280                 }
281                 if ($pcno == 11) {
282
283                         if (Spot::dup_find(@spot[0..4,7])) {
284                                 dbg("DUPE $pc->[0] $self->{call} -> key: $key dumped") if isdbg('pc11');
285                                 return;
286                         } else {
287                                 ++$pc11_rx;
288                         }
289
290                         if ($pc11_saved{$key}) {
291                                 dbg("DUPE $pc->[0] $self->{call } key: is saved, ignored") if isdbg("pc11");
292                                 return;         # because it's a dup
293                         }
294
295                         # can we promote this to a PC61?
296                         my $r = Route::User::get($spot[4]); # find spotter
297                         if ($r && $r->ip) {                     # do we have an ip addres
298                                 $pcno = 61;
299                                 $pc->[0] = 'PC61';
300                                 $pc->[7] = $spot[14] = $r->ip;
301                                 ++$rpc11_to_61;
302                                 my $percent = $pc11_rx ? $rpc11_to_61 * 100 / $pc11_rx : 0;
303                                 dbg(sprintf("PROMOTED ROUTE $self->{call} -> PC11 $key PROMOTED to PC61 with IP $spot[14] pc61: $pc61_rx pc11: $pc11_rx route->pc61 $rpc11_to_61 (%0.1f%%)", $percent)) if isdbg("pc11");
304                                 delete $pc11_saved{$key};
305                         }
306
307                         # if it is STILL (despite all efforts to change it) a PC11
308                         # save it and wait - it will be called from pc11_process
309                         if ($pcno == 11) {
310                                 $pc11_saved{$key} = [$main::systime, $self, $pcno, $line, $origin, $pc];
311                                 dbg("SAVED $self->{call} -> NEW $pc->[0] spot $key waiting for a better offer") if isdbg("pc11");
312                                 return;
313                         }
314                 }
315         }
316
317         my $count =  $pc11_to_61+$rpc11_to_61;
318         my $percent = $pc11_rx ? $count*100 / $pc11_rx : 0;
319         my $pc11_61 = $pc61_rx ? $pc11_rx*100 / $pc61_rx : 0; 
320         dbg(sprintf("PASSED $self->{call} -> $pc->[0] spot $key, pc11: $pc11_rx pc61: $pc61_rx PC11/PC61 ratio: %0.1f%%  total pc11->pc61: $count (%0.1f%%) ", $pc11_61, $percent)) if isdbg("pc11");
321
322         
323         # this goes after the input filtering, but before the add
324         # so that if it is input filtered, it isn't added to the dup
325         # list. This allows it to come in from a "legitimate" source
326         if (Spot::dup(@spot[0..4,7])) {
327                 dbg("PCPROT: Duplicate Spot  $self->{call} -> $pc->[0] $key ignored\n") if isdbg('chanerr') || isdbg('dupespot') || isdbg('pc11');
328                 return;
329         }
330
331         dbg("PROCESSING $self->{call} -> $pc->[0] key: $key") if isdbg('pc11');
332         
333         # we check IP addresses for PC61 - this will also dedupe PC11 copies
334         if (@$pc > 8 && is_ipaddr($pc->[8])) {
335                 my $ip = $pc->[8];
336                 $ip =~ s/,/:/g;
337                 $ip =~ s/^::ffff://;
338                 if (DXCIDR::find($ip)) {
339                         dbg($line) if isdbg('nologchan');
340                         dbg("PCPROT: $ip in badip list, dropped");
341                         return;
342                 }
343         }
344
345         
346         # here we verify the spotter is currently connected to the node it says it is one. AKA email sender verify
347         # but without the explicit probe to the node. We are relying on "historical" information, but it very likely
348         # to be current once we have seen the first PC92C from that node.
349         #
350         # As for spots generated from non-PC92 nodes, we'll see after about  do_pc9x3h20m...
351         #
352         if ($senderverify) {
353                 my $nroute = Route::Node::get($pc->[7]);
354                 my $uroute = Route::Node::get($pc->[6]);
355                 my $local = DXChannel::get($pc->[7]);
356                 
357                 if ($nroute && ($nroute->last_PC92C || ($local && !$local->do_pc9x))) {
358                         my $s = '';
359                         my $ip = $pcno == 61 ?  $pc->[8] : '';
360 #                       $s .= "User $pc->[6] not logged in, " unless $uroute;
361                         $s .= "User $pc->[6] not on node $pc->[7], " unless $nroute->is_user($pc->[6]);
362 #                       $s .= "Node $pc->[7] at '$ip' not on Node's IP " . $nroute->ip if $ip && $nroute && $nroute->ip && $nroute->ip ne $ip;
363                         if ($s) {
364                                 my $action = $senderverify > 1 ? ", DUMPED" : '';
365                                 $s =~ s/, $//;
366                                 dbg("PCPROT: Suspicious Spot $pc->[2] on $pc->[1] by $pc->[6]($ip)\@$pc->[7] $s$action");
367                                 return unless $senderverify < 2;
368                         }
369                 }
370         }
371
372         # we until here to do any censorship to try and reduce the amount of noise that repeated copies
373         # from other connected nodes cause
374         if ($censorpc) {
375                 my @bad;
376                 if (@bad = BadWords::check($pc->[5])) {
377                         my $bw = join ', ', @bad;
378                         dbg($line) if isdbg('nologchan');
379                         dbg("PCPROT: Badwords: '$bw', dropped");
380                         return;
381                 }
382         }
383
384         
385         # add it
386         Spot::add(@spot);
387
388         my $ip = '';
389         $ip ||= $spot[14] if exists $spot[14];
390         if (isdbg('progress')) {
391                 my $sip = $ip ? sprintf "($ip)" : '' unless $ip =~ m|[\(\)\*]|;
392                 $sip ||= '';
393                 my $d = ztime($spot[2]);
394                 my $s = "SPOT: $spot[1] on $spot[0] \@ $d by $spot[4]$sip\@$spot[7]";
395                 $s .= $spot[3] ? " '$spot[3]'" : q{ ''};
396                 $s .=  " route: $origin";
397                 dbg($s);
398         }
399         
400         #
401         # @spot at this point contains:-
402         # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
403         # then  spotted itu, spotted cq, spotters itu, spotters cq
404         # you should be able to route on any of these
405         #
406
407         # fix up qra locators of known users
408         my $user = DXUser::get_current($spot[4]);
409         if ($user) {
410                 my $qra = $user->qra;
411                 unless ($qra && is_qra($qra)) {
412                         my $lat = $user->lat;
413                         my $long = $user->long;
414                         if (defined $lat && defined $long) {
415                                 $user->qra(DXBearing::lltoqra($lat, $long));
416                                 $user->put;
417                         }
418                 }
419
420                 # send a remote command to a distant cluster if it is visible and there is no
421                 # qra locator and we havn't done it for a month.
422
423                 unless ($user->qra) {
424                         my $node;
425                         my $to = $user->homenode;
426                         my $last = $user->lastoper || 0;
427                         if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
428                                 my $cmd = "forward/opernam $spot[4]";
429                                 # send the rcmd but we aren't interested in the replies...
430                                 my $dxchan = $node->dxchan;
431                                 if ($dxchan && $dxchan->is_clx) {
432                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
433                                 } else {
434                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
435                                 }
436                                 if ($to ne $origin) {
437                                         $to = $origin;
438                                         $node = Route::Node::get($to);
439                                         if ($node) {
440                                                 $dxchan = $node->dxchan;
441                                                 if ($dxchan && $dxchan->is_clx) {
442                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
443                                                 } else {
444                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
445                                                 }
446                                         }
447                                 }
448                                 $user->lastoper($main::systime);
449                                 $user->put;
450                         }
451                 }
452         }
453
454         # local processing
455         if (defined &Local::spot) {
456                 my $r;
457                 eval {
458                         $r = Local::spot($self, @spot);
459                 };
460                 return if $r;
461         }
462
463         # DON'T be silly and send on PC26s!
464         return if $pcno == 26;
465
466         # send out the filtered spots
467         send_dx_spot($self, $line, @spot) if @spot;
468
469         # cancel any recursion as we have now processed it
470         if ($recurse) {
471                 if ($pc11_saved{$key}) {
472                         dbg("END RECURSED $self->{call} $pc->[0] key: $key removed and finished") if isdbg('pc11');
473                         delete $pc11_saved{$key};
474                 } else {
475                         dbg("END RECURSED $self->{call} $pc->[0] key: $key finished") if isdbg('pc11');
476                 }
477                 $recurse = 0;
478         } else {
479                 dbg("END NORMAL $self->{call} $pc->[0] key: $key finished") if isdbg('pc11');
480         }
481 }
482
483 # used to kick outstanding PC11 if required
484 sub pc11_process
485 {
486         foreach my $key (keys %pc11_saved) {
487                 my $r = $pc11_saved{$key};
488                 if ($main::systime > $r->[0] + $pc11_dwell_time) {
489                         dbg("SAVED PC11 spot $key timed out waiting, recursing") if isdbg("pc11");
490                         my $self = $r->[1];
491                         delete $pc11_saved{$key};
492                         $self->handle_11(@$r[2..5], 1);
493                 }
494         }
495 }
496
497 # announces
498 sub handle_12
499 {
500         my $self = shift;
501         my $pcno = shift;
502         my $line = shift;
503         my $origin = shift;
504         my $pc = shift;
505
506         # announce duplicate checking
507         $pc->[3] =~ s/^\s+//;                   # remove leading blanks
508
509         # if this is a 'nodx' node then ignore it
510         if ($badnode->in($pc->[5])) {
511                 dbg($line) if isdbg('nologchan');
512                 dbg("PCPROT: Bad Node, dropped");
513                 return;
514         }
515
516         # if this is a 'bad spotter' user then ignore it
517         my $nossid = $pc->[1];
518         $nossid =~ s/-\d+$//;
519         if ($badspotter->in($nossid)) {
520                 dbg($line) if isdbg('nologchan');
521                 dbg("PCPROT: Bad Spotter, dropped");
522                 return;
523         }
524
525         # ignore PC12s from origins that use PCxx protocol
526         my $oref = Route::get($origin);
527         if ($oref->do_pc9x) {
528                 dbg("PCPROT: PC12 rxed from PC9x node, ignored") if isdbg('chanerr');
529                 return;
530         }
531
532         if ($censorpc) {
533                 my @bad;
534                 if (@bad = BadWords::check($pc->[3])) {
535                         my $bw = join ', ', @bad;
536                         dbg($line) if isdbg('nologchan');
537                         dbg("PCPROT: Badwords: '$bw', dropped");
538                         return;
539                 }
540         }
541
542
543         my $dxchan;
544
545         if ((($dxchan = DXChannel::get($pc->[2])) && $dxchan->is_user) || $pc->[4] =~ /^[\#\w.]+$/){
546                 $self->send_chat(0, $line, @$pc[1..6]);
547         } elsif ($pc->[2] eq '*' || $pc->[2] eq $main::mycall) {
548
549                 # ignore something that looks like a chat line coming in with sysop
550                 # flag - this is a kludge...
551                 if ($pc->[3] =~ /^\#\d+ / && $pc->[4] eq '*') {
552                         dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
553                         return;
554                 }
555
556                 # here's a bit of fun, convert incoming ann with a callsign in the first word
557                 # or one saying 'to <call>' to a talk if we can route to the recipient
558                 if ($ann_to_talk) {
559                         my $call = AnnTalk::is_talk_candidate($pc->[1], $pc->[3]);
560                         if ($call) {
561                                 my $ref = Route::get($call);
562                                 if ($ref) {
563                                         $dxchan = $ref->dxchan;
564                                         $dxchan->talk($pc->[1], $call, undef, $pc->[3], $pc->[5]) if $dxchan != $self;
565                                         return;
566                                 }
567                         }
568                 }
569
570                 # send it
571                 $self->send_announce(0, $line, @$pc[1..6]);
572         } else {
573                 $self->route($pc->[2], $line);
574         }
575
576         # local processing
577         if (defined &Local::ann) {
578                 my $r;
579                 eval {
580                         $r = Local::ann($self, $line, @$pc[1..6]);
581                 };
582                 return if $r;
583         }
584 }
585
586 sub handle_15
587 {
588         my $self = shift;
589         my $pcno = shift;
590         my $line = shift;
591         my $origin = shift;
592         my $pc = shift;
593
594         if (eph_dup($line, $eph_pc15_restime)) {
595                 return;
596         } else {
597                 unless ($self->{isolate}) {
598                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
599                 }
600         }
601 }
602
603 # incoming user
604 sub handle_16
605 {
606         my $self = shift;
607         my $pcno = shift;
608         my $line = shift;
609         my $origin = shift;
610         my $pc = shift;
611
612         # general checks
613         my $dxchan;
614         my $ncall = $pc->[1];
615         my $newline = "PC16^";
616
617         # dos I want users from this channel?
618         unless ($self->user->wantpc16) {
619                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
620                 return;
621         }
622
623         # is it me?
624         if ($ncall eq $main::mycall) {
625                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
626                 return;
627         }
628
629         my $h;
630         $h = 1 if DXChannel::get($ncall);
631         if ($h && $self->{call} ne $ncall) {
632                 dbg("PCPROT: trying to update a local node, ignored") if isdbg('chanerr');
633                 return;
634         }
635
636         if (eph_dup($line)) {
637                 return;
638         }
639
640         # isolate now means only accept stuff from this call only
641         if ($self->{isolate} && $ncall ne $self->{call}) {
642                 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
643                 return;
644         }
645
646         my $parent = Route::Node::get($ncall);
647
648         if ($parent) {
649                 $dxchan = $parent->dxchan;
650                 if ($dxchan && $dxchan ne $self) {
651                         dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
652                         return;
653                 }
654
655                 # input filter if required
656                 return unless $self->in_filter_route($parent);
657         } else {
658                 $parent = Route::Node->new($ncall);
659         }
660
661         unless ($h) {
662                 if ($parent->via_pc92) {
663                         dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
664                         return;
665                 }
666         }
667
668         my $i;
669         my @rout;
670         for ($i = 2; $i < $#$pc; $i++) {
671                 my ($call, $conf, $here) = $pc->[$i] =~ /^(\S+) (\S) (\d)/o;
672                 next unless $call && $conf && defined $here && is_callsign($call);
673                 next if $call eq $main::mycall;
674
675                 eph_del_regex("^PC17\\^$call\\^$ncall");
676
677                 $conf = $conf eq '*';
678
679                 # reject this if we think it is a node already
680                 my $r = Route::Node::get($call);
681                 my $u = DXUser::get_current($call) unless $r;
682                 if ($r || ($u && $u->is_node)) {
683                         dbg("PCPROT: $call is a node") if isdbg('chanerr');
684                         next;
685                 }
686
687                 $r = Route::User::get($call);
688                 my $flags = Route::here($here)|Route::conf($conf);
689
690                 if ($r) {
691                         my $au = $r->addparent($parent);
692                         if ($r->flags != $flags) {
693                                 $r->flags($flags);
694                                 $au = $r;
695                         }
696                         push @rout, $r if $h && $au;
697                 } else {
698                         my @ans = $parent->add_user($call, $flags);
699                         push @rout, @ans if $h && @ans;
700                 }
701
702                 # add this station to the user database, if required
703                 my $user = DXUser::get_current($ncall);
704                 $user = DXUser->new($call) unless $user;
705                 $user->homenode($parent->call) if !$user->homenode;
706                 $user->node($parent->call);
707                 $user->lastin($main::systime) unless DXChannel::get($call);
708                 $user->put;
709
710                 # send info to all logged in thingies
711                 $self->tell_login('loginu', "$ncall: $call") if $user->is_local_node;
712                 $self->tell_buddies('loginb', $call, $ncall);
713         }
714         if (@rout) {
715                 $self->route_pc16($origin, $line, $parent, @rout) if @rout;
716 #               $self->route_pc92a($main::mycall, undef, $parent, @rout) if $h && $self->{state} eq 'normal';
717         }
718 }
719
720 # remove a user
721 sub handle_17
722 {
723         my $self = shift;
724         my $pcno = shift;
725         my $line = shift;
726         my $origin = shift;
727         my $pc = shift;
728
729         my $dxchan;
730         my $ncall = $pc->[2];
731         my $ucall = $pc->[1];
732
733         eph_del_regex("^PC16\\^$ncall.*$ucall");
734
735         # do I want users from this channel?
736         unless ($self->user->wantpc16) {
737                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
738                 return;
739         }
740
741         if ($ncall eq $main::mycall) {
742                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
743                 return;
744         }
745
746         # isolate now means only accept stuff from this call only
747         if ($self->{isolate} && $ncall ne $self->{call}) {
748                 dbg("PCPROT: $self->{call} isolated, $ncall ignored") if isdbg('chanerr');
749                 return;
750         }
751
752         my $uref = Route::User::get($ucall);
753         unless ($uref) {
754                 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
755                 return;
756         }
757         my $parent = Route::Node::get($ncall);
758         unless ($parent) {
759                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
760                 return;
761         }
762
763         $dxchan = DXChannel::get($ncall);
764         if ($dxchan && $dxchan ne $self) {
765                 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
766                 return;
767         }
768
769         unless ($dxchan) {
770                 if ($parent->via_pc92) {
771                         dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
772                         return;
773                 }
774         }
775
776         if (DXChannel::get($ucall)) {
777                 dbg("PCPROT: trying do disconnect local user, ignored") if isdbg('chanerr');
778                 return;
779         }
780
781         # input filter if required and then remove user if present
782 #               return unless $self->in_filter_route($parent);
783         $parent->del_user($uref);
784
785         # send info to all logged in thingies
786         my $user = DXUser::get_current($ncall);
787         $self->tell_login('logoutu', "$ncall: $ucall") if $user && $user->is_local_node;
788         $self->tell_buddies('logoutb', $ucall, $ncall);
789
790         if (eph_dup($line)) {
791                 return;
792         }
793
794         $self->route_pc17($origin, $line, $parent, $uref);
795 #       $self->route_pc92d($main::mycall, undef, $parent, $uref) if $dxchan;
796 }
797
798 # link request
799 sub handle_18
800 {
801         my $self = shift;
802         my $pcno = shift;
803         my $line = shift;
804         my $origin = shift;
805         my $pc = shift;
806
807         $self->state('init');
808
809         my $parent = Route::Node::get($self->{call});
810
811         # record the type and version offered
812         if (my ($software, $version) = $pc->[1] =~ /(DXSpider|CC\s*Cluster)\s+Version: (\d+(?:\.\d+)?)/i) {
813                 $version += 0;
814                 $version += 53 if $version < 6;
815                 $self->{version} = $version;
816                 $self->user->version($version);
817                 $parent->version($version);
818                 my ($build) = $pc->[1] =~ /Build: (\d+(?:\.\d+)?)/;
819                 $build += 0;
820                 $self->{build} = $build;
821                 $self->user->build($build);
822                 $parent->build($build);
823                 dbg("$self->{call} = $software version $version build $build");
824                 unless ($self->is_spider) {
825                         dbg("Change U " . $self->user->sort . " C $self->{sort} -> S");
826                         $self->user->sort('S');
827                         $self->user->put;
828                         $self->sort('S');
829                 }
830 #               $self->{handle_xml}++ if DXXml::available() && $pc->[1] =~ /\bxml/;
831         } else {
832                 dbg("$self->{call} = Unknown software ($pc->[1] $pc->[2])");
833                 $self->version(50.0);
834                 $self->version($pc->[2] / 100) if $pc->[2] && $pc->[2] =~ /^\d+$/;
835                 $self->user->version($self->version);
836         }
837
838         if ($pc->[1] =~ /CC\*Cluster/i || $pc->[1] =~ /\bpc9x/i) {
839                 if ($self->{isolate}) {
840                         dbg("$self->{call} pc9x recognised, but node is isolated, using old protocol");
841                 } elsif (!$self->user->wantpc9x) {
842                         dbg("$self->{call} pc9x explicitly switched off, using old protocol");
843                 } else {
844                         $self->{do_pc9x} = 1;
845                         dbg("$self->{call} Set do PC9x");
846                 }
847         }
848
849         # first clear out any nodes on this dxchannel
850         my @rout = $parent->del_nodes;
851         $self->route_pc21($origin, $line, @rout, $parent) if @rout;
852         $self->send_local_config();
853         $self->send(pc20());
854 }
855
856 sub check_add_user
857 {
858         my $call = shift;
859         my $type = shift;
860         my $homenode = shift;
861
862         # add this station to the user database, if required (don't remove SSID from nodes)
863         my $user = DXUser::get_current($call);
864         unless ($user) {
865                 $user = DXUser->new($call);
866                 $user->sort($type || 'U');
867                 if ($user->is_node) {
868                         $user->priv(1);         # I have relented and defaulted nodes
869                         $user->lockout(1) if $user->is_node;
870                 } else {
871                         $user->homenode($homenode) if $homenode;
872                         $user->node($homenode);
873                         $user->priv(0);
874                 }
875                 $user->lastin($main::systime); # this make it last longer than just this invocation
876                 $user->put;                             # just to make sure it gets written away!!!
877                 dbg("DXProt: PC92 new user record for $call created");
878         }
879
880         # this is to fix a problem I introduced some build ago by using this function for users
881         # whereas it was only being used for nodes.
882         if ($user->is_user && $user->lockout && ($user->priv // 0) == 1) {
883                 $user->priv(0);
884                 $user->lockout(0);
885                 dbg("DXProt: PC92 user record for $call depriv'd and unlocked");
886                 $user->put;
887         }
888         return $user;
889 }
890
891 # incoming cluster list
892 sub handle_19
893 {
894         my $self = shift;
895         my $pcno = shift;
896         my $line = shift;
897         my $origin = shift;
898         my $pc = shift;
899
900         my $i;
901         my $newline = "PC19^";
902
903         # new routing list
904         my (@rout, @pc92out);
905
906         # first get the INTERFACE node
907         my $parent = Route::Node::get($self->{call});
908         unless ($parent) {
909                 dbg("PCPROT: my parent $self->{call} has disappeared");
910                 $self->disconnect;
911                 return;
912         }
913
914         my $h;
915
916         # parse the PC19
917         #
918         # We are making a major change from now on. We are only going to accept
919         # PC19s from directly connected nodes.  This means that we are probably
920         # going to throw away most of the data that we are being sent.
921         #
922         # The justification for this is that most of it is wrong or out of date
923         # anyway.
924         #
925         # From now on we are only going to believe PC92 data and locally connected
926         # non-pc92 nodes.
927         #
928         for ($i = 1; $i < $#$pc-1; $i += 4) {
929                 my $here = $pc->[$i];
930                 my $call = uc $pc->[$i+1];
931                 my $conf = $pc->[$i+2];
932                 my $ver = $pc->[$i+3];
933                 next unless defined $here && defined $conf && is_callsign($call);
934
935                 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
936
937                 # check for sane parameters
938                 #                               $ver = 5000 if $ver eq '0000';
939                 next unless $ver && $ver =~ /^\d+$/;
940                 next if $ver < 5000;    # only works with version 5 software
941                 next if length $call < 3; # min 3 letter callsigns
942                 next if $call eq $main::mycall || $call eq $main::myalias;
943
944                 # check that this PC19 isn't trying to alter the wrong dxchan
945                 $h = 0;
946                 my $dxchan = DXChannel::get($call);
947                 if ($dxchan) {
948                         if ($dxchan == $self) {
949                                 $h = 1;
950                         } else {
951                                 dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
952                                 next;
953                         }
954                 }
955
956                 # isolate now means only accept stuff from this call only
957                 if ($self->{isolate} && $call ne $self->{call}) {
958                         dbg("PCPROT: $self->{call} isolated, $call ignored") if isdbg('chanerr');
959                         next;
960                 }
961
962                 my $user = check_add_user($call, 'A');
963
964 #               if (eph_dup($genline)) {
965 #                       dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
966 #                       next;
967 #               }
968
969
970                 unless ($h) {
971                         if ($parent->via_pc92) {
972                                 dbg("PCPROT: non-local node controlled by PC92, ignored") if isdbg('chanerr');
973                                 next;
974                         }
975                 }
976
977                 my $r = Route::Node::get($call);
978                 my $flags = Route::here($here)|Route::conf($conf);
979
980                 # modify the routing table if it is in it, otherwise store it in the pc19list for now
981                 if ($r) {
982                         my $ar;
983                         if ($call ne $parent->call) {
984                                 if ($self->in_filter_route($r)) {
985                                         $ar = $parent->add($call, $ver, $flags);
986 #                                       push @rout, $ar if $ar;
987                                 } else {
988                                         next;
989                                 }
990                         }
991                         if ($r->version ne $ver || $r->flags != $flags) {
992                                 $r->version($ver);
993                                 $r->flags($flags);
994                         }
995                         push @rout, $r;
996                 } else {
997                         if ($call eq $self->{call} || $user->wantroutepc19) {
998                                 my $new = Route->new($call); # throw away
999                                 if ($self->in_filter_route($new)) {
1000                                         my $ar = $parent->add($call, $ver, $flags);
1001                                         $user->wantroutepc19(1) unless defined $user->wantroutepc19;
1002                                         push @rout, $ar if $ar;
1003                                         push @pc92out, $r if $h;
1004                                 } else {
1005                                         next;
1006                                 }
1007                         }
1008                 }
1009
1010                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
1011                 my $mref = DXMsg::get_busy($call);
1012                 $mref->stop_msg($call) if $mref;
1013
1014                 $user->lastin($main::systime) unless DXChannel::get($call);
1015                 $user->put;
1016         }
1017
1018         # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
1019         # but remember there will only be one (pair) these because any extras will be
1020         # thrown away.
1021         if (@rout) {
1022 #               $self->route_pc21($self->{call}, $line, @rout);
1023                 $self->route_pc19($self->{call}, $line, @rout);
1024         }
1025         if (@pc92out && !$pc92_slug_changes) {
1026                 $self->route_pc92a($main::mycall, $line, $main::routeroot, @pc92out) if $self->{state} eq 'normal';
1027         }
1028 }
1029
1030 # send local configuration
1031 sub handle_20
1032 {
1033         my $self = shift;
1034         my $pcno = shift;
1035         my $line = shift;
1036         my $origin = shift;
1037         my $pc = shift;
1038
1039         if ($self->{do_pc9x} && $self->{state} ne 'init92' && $self->{state} ne 'normal') {
1040                 $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
1041                 $self->{do_pc9x} = 0;
1042         }
1043         $self->send_local_config;
1044         $self->send(pc22());
1045         $self->state('normal');
1046         $self->{lastping} = 0;
1047         $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
1048 }
1049
1050 # delete a cluster from the list
1051 #
1052 # This should never occur for directly connected nodes.
1053 #
1054 sub handle_21
1055 {
1056         my $self = shift;
1057         my $pcno = shift;
1058         my $line = shift;
1059         my $origin = shift;
1060         my $pc = shift;
1061
1062         my $call = uc $pc->[1];
1063
1064         eph_del_regex("^PC1[679].*$call");
1065
1066         # if I get a PC21 from the same callsign as self then ignore it
1067         if ($call eq $self->{call}) {
1068                 dbg("PCPROT: self referencing PC21 from $self->{call}");
1069                 return;
1070         }
1071
1072         # for the above reason and also because of the check for PC21s coming
1073         # in for self->call from outside being ignored further down
1074         # we don't need any isolation code here, because we will never
1075         # act on a PC21 with self->call in it.
1076
1077         my $parent = Route::Node::get($self->{call});
1078         unless ($parent) {
1079                 dbg("PCPROT: my parent $self->{call} has disappeared");
1080                 $self->disconnect;
1081                 return;
1082         }
1083
1084         my @rout;
1085
1086         if ($call ne $main::mycall && $call ne $main::myalias) { # don't allow malicious buggers to disconnect me!
1087                 my $node = Route::Node::get($call);
1088                 if ($node) {
1089
1090                         if ($node->via_pc92) {
1091                                 dbg("PCPROT: controlled by PC92, ignored") if isdbg('chanerr');
1092                                 return;
1093                         }
1094
1095                         my $dxchan = DXChannel::get($call);
1096                         if ($dxchan && $dxchan != $self) {
1097                                 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chan');
1098                                 return;
1099                         }
1100
1101                         # input filter it
1102                         return unless $self->in_filter_route($node);
1103
1104                         # routing objects, force a PC21 if it is local
1105                         push @rout, $node->del($parent);
1106                         push @rout, $call if $dxchan && @rout == 0;
1107                 }
1108         } else {
1109                 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chan');
1110                 return;
1111         }
1112
1113         if (eph_dup($line)) {
1114                 return;
1115         }
1116
1117         if (@rout) {
1118                 $self->route_pc21($origin, $line, @rout);
1119 #               $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
1120         }
1121 }
1122
1123
1124 sub handle_22
1125 {
1126         my $self = shift;
1127         my $pcno = shift;
1128         my $line = shift;
1129         my $origin = shift;
1130         my $pc = shift;
1131
1132         if ($self->{do_pc9x}) {
1133                 if ($self->{state} ne 'init92' && $self->{state} ne 'normal') {
1134                         $self->send("Reseting to oldstyle routing because login call not sent in any pc92");
1135                         $self->{do_pc9x} = 0;
1136                 }
1137         }
1138         $self->{lastping} = 0;
1139         $self->state('normal');
1140         $self->route_pc92a($main::mycall, undef, $main::routeroot, Route::Node::get($self->{call}));
1141 }
1142
1143 # WWV info
1144 sub handle_23
1145 {
1146         my $self = shift;
1147         my $pcno = shift;
1148         my $line = shift;
1149         my $origin = shift;
1150         my $pc = shift;
1151
1152         # route foreign' pc27s
1153         if ($pcno == 27) {
1154                 if ($pc->[8] ne $main::mycall) {
1155                         $self->route($pc->[8], $line);
1156                         return;
1157                 }
1158         }
1159
1160
1161         # do some de-duping
1162         my $d = cltounix($pc->[1], sprintf("%02d18Z", $pc->[2]));
1163         my $sfi = unpad($pc->[3]);
1164         my $k = unpad($pc->[4]);
1165         my $i = unpad($pc->[5]);
1166         my ($r) = $pc->[6] =~ /R=(\d+)/;
1167         $r = 0 unless $r;
1168         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $pc->[2] < 0 || $pc->[2] > 23) {
1169                 dbg("PCPROT: WWV Date ($pc->[1] $pc->[2]) out of range") if isdbg('chanerr');
1170                 return;
1171         }
1172
1173         # global wwv filtering on INPUT
1174         my @dxcc = ((Prefix::cty_data($pc->[7]))[0..2], (Prefix::cty_data($pc->[8]))[0..2]);
1175         if ($self->{inwwvfilter}) {
1176                 my ($filter, $hops) = $self->{inwwvfilter}->it(@$pc[7,8], $origin, @dxcc);
1177                 unless ($filter) {
1178                         dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
1179                         return;
1180                 }
1181         }
1182         $pc->[7] =~ s/-\d+$//o;         # remove spotter's ssid
1183         if (Geomag::dup($d,$sfi,$k,$i,$pc->[6],$pc->[7])) {
1184                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1185                 return;
1186         }
1187
1188         # note this only takes the first one it gets
1189         Geomag::update($d, $pc->[2], $sfi, $k, $i, @$pc[6..8], $r);
1190         dbg("WWV: <$pc->[2]>, sfi=$sfi k=$k info=$i '$pc->[6]' $pc->[7]\@$pc->[8] $r route: $origin") if isdbg('progress');
1191
1192         if (defined &Local::wwv) {
1193                 my $rep;
1194                 eval {
1195                         $rep = Local::wwv($self, $pc->[1], $pc->[2], $sfi, $k, $i, @$pc[6..8], $r);
1196                 };
1197                 return if $rep;
1198         }
1199
1200         # DON'T be silly and send on PC27s!
1201         return if $pcno == 27;
1202
1203         # broadcast to the eager world
1204         send_wwv_spot($self, $line, $d, $pc->[2], $sfi, $k, $i, @$pc[6..8]);
1205 }
1206
1207 # set here status
1208 sub handle_24
1209 {
1210         my $self = shift;
1211         my $pcno = shift;
1212         my $line = shift;
1213         my $origin = shift;
1214         my $pc = shift;
1215
1216         my $call = uc $pc->[1];
1217         my ($nref, $uref);
1218         $nref = Route::Node::get($call);
1219         $uref = Route::User::get($call);
1220         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1221
1222         if (eph_dup($line)) {
1223                 return;
1224         }
1225
1226         $nref->here($pc->[2]) if $nref;
1227         $uref->here($pc->[2]) if $uref;
1228         my $ref = $nref || $uref;
1229         return unless $self->in_filter_route($ref);
1230
1231         $self->route_pc24($origin, $line, $ref, $pc->[3]);
1232 }
1233
1234 # merge request
1235 sub handle_25
1236 {
1237         my $self = shift;
1238         my $pcno = shift;
1239         my $line = shift;
1240         my $origin = shift;
1241         my $pc = shift;
1242
1243         if ($pc->[1] ne $main::mycall) {
1244                 $self->route($pc->[1], $line);
1245                 return;
1246         }
1247         if ($pc->[2] eq $main::mycall) {
1248                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chan');
1249                 return;
1250         }
1251
1252         Log('DXProt', "Merge request for $pc->[3] spots and $pc->[4] WWV from $pc->[2]");
1253
1254         # spots
1255         if ($pc->[3] > 0) {
1256                 my @in = reverse Spot::search(1, undef, undef, 0, $pc->[3]);
1257                 my $in;
1258                 foreach $in (@in) {
1259                         $self->send(pc26(@{$in}[0..4], $pc->[2]));
1260                 }
1261         }
1262
1263         # wwv
1264         if ($pc->[4] > 0) {
1265                 my @in = reverse Geomag::search(0, $pc->[4], time, 1);
1266                 my $in;
1267                 foreach $in (@in) {
1268                         $self->send(pc27(@{$in}[0..5], $pc->[2]));
1269                 }
1270         }
1271 }
1272
1273 sub handle_26 {goto &handle_11}
1274 sub handle_27 {goto &handle_23}
1275
1276 # mail/file handling
1277 sub handle_28
1278 {
1279         my $self = shift;
1280         my $pcno = shift;
1281         my $line = shift;
1282         my $origin = shift;
1283         my $pc = shift;
1284
1285         if ($pc->[1] eq $main::mycall) {
1286                 no strict 'refs';
1287                 my $sub = "DXMsg::handle_$pcno";
1288                 &$sub($self, @$pc);
1289         } else {
1290                 $self->route($pc->[1], $line) unless $self->is_clx;
1291         }
1292 }
1293
1294 sub handle_29 {goto &handle_28}
1295 sub handle_30 {goto &handle_28}
1296 sub handle_31 {goto &handle_28}
1297 sub handle_32 {goto &handle_28}
1298 sub handle_33 {goto &handle_28}
1299
1300 sub handle_34
1301 {
1302         my $self = shift;
1303         my $pcno = shift;
1304         my $line = shift;
1305         my $origin = shift;
1306         my $pc = shift;
1307
1308         if (eph_dup($line, $eph_pc34_restime)) {
1309                 return;
1310         } else {
1311                 $self->process_rcmd($pc->[1], $pc->[2], $pc->[2], $pc->[3]);
1312         }
1313 }
1314
1315 # remote command replies
1316 sub handle_35
1317 {
1318         my $self = shift;
1319         my $pcno = shift;
1320         my $line = shift;
1321         my $origin = shift;
1322         my $pc = shift;
1323
1324         eph_del_regex("^PC35\\^$pc->[2]\\^$pc->[1]\\^");
1325         $self->process_rcmd_reply($pc->[1], $pc->[2], $pc->[1], $pc->[3]);
1326 }
1327
1328 sub handle_36 {goto &handle_34}
1329
1330 # database stuff
1331 sub handle_37
1332 {
1333         my $self = shift;
1334         my $pcno = shift;
1335         my $line = shift;
1336         my $origin = shift;
1337         my $pc = shift;
1338
1339         if ($pc->[1] eq $main::mycall) {
1340                 no strict 'refs';
1341                 my $sub = "DXDb::handle_$pcno";
1342                 &$sub($self, @$pc);
1343         } else {
1344                 $self->route($pc->[1], $line) unless $self->is_clx;
1345         }
1346 }
1347
1348 # node connected list from neighbour
1349 sub handle_38
1350 {
1351         my $self = shift;
1352         my $pcno = shift;
1353         my $line = shift;
1354         my $origin = shift;
1355         my $pc = shift;
1356 }
1357
1358 # incoming disconnect
1359 sub handle_39
1360 {
1361         my $self = shift;
1362         my $pcno = shift;
1363         my $line = shift;
1364         my $origin = shift;
1365         my $pc = shift;
1366
1367         if ($pc->[1] eq $self->{call}) {
1368                 $self->disconnect(1);
1369         } else {
1370                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1371         }
1372 }
1373
1374 sub handle_40 {goto &handle_28}
1375
1376 # user info
1377 sub handle_41
1378 {
1379         my $self = shift;
1380         my $pcno = shift;
1381         my $line = shift;
1382         my $origin = shift;
1383         my $pc = shift;
1384
1385         my $call = $pc->[1];
1386         my $sort = $pc->[2];
1387         my $val = $pc->[3];
1388
1389         my $l = "PC41^$call^$sort";
1390         if (eph_dup($l, $eph_info_restime)) {
1391                 return;
1392         }
1393
1394         # input filter if required
1395         #                       my $ref = Route::get($call) || Route->new($call);
1396         #                       return unless $self->in_filter_route($ref);
1397
1398         if ($val eq $sort || $val =~ /^\s*$/) {
1399                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1400                 return;
1401         }
1402
1403         if ($call eq $main::mycall || $call eq $main::myalias) {
1404                 dbg "DXPROT: PC41 trying to update $call from outside via $origin, ignored";
1405                 return;
1406         }
1407         my $chan = DXChannel::get($call);
1408         if ($chan) {
1409                 dbg "DXPROT: PC41 trying to update online $call from outside via $origin, ignored";
1410                 return;
1411         }
1412
1413         # add this station to the user database, if required
1414         my $user = DXUser::get_current($call);
1415         $user = DXUser->new($call) unless $user;
1416
1417         if ($sort == 1) {
1418                 if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
1419                         dbg("PCPROT: invalid name") if isdbg('chanerr');
1420                         return;
1421                 }
1422                 $user->name($val);
1423         } elsif ($sort == 2) {
1424                 $user->qth($val);
1425         } elsif ($sort == 3) {
1426                 if (is_latlong($val)) {
1427                         my ($lat, $long) = DXBearing::stoll($val);
1428                         $user->lat($lat) if $lat;
1429                         $user->long($long) if $long;
1430                         $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra;
1431                 } else {
1432                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1433                         return;
1434                 }
1435         } elsif ($sort == 4) {
1436                 $user->homenode($val);
1437         } elsif ($sort == 5) {
1438                 if (is_qra(uc $val)) {
1439                         my ($lat, $long) = DXBearing::qratoll(uc $val);
1440                         $user->lat($lat) if $lat && !$user->lat;
1441                         $user->long($long) if $long && !$user->long;
1442                         $user->qra(uc $val);
1443                 } else {
1444                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1445                         return;
1446                 }
1447         }
1448         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1449         $user->put;
1450
1451         unless ($self->{isolate}) {
1452                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1453         }
1454
1455         #  perhaps this IS what we want after all
1456         #                       $self->route_pc41($ref, $call, $sort, $val, $pc->[4]);
1457 }
1458
1459 sub handle_42 {goto &handle_28}
1460
1461
1462 # database
1463 sub handle_44 {goto &handle_37}
1464 sub handle_45 {goto &handle_37}
1465 sub handle_46 {goto &handle_37}
1466 sub handle_47 {goto &handle_37}
1467 sub handle_48 {goto &handle_37}
1468
1469 # message and database
1470 sub handle_49
1471 {
1472         my $self = shift;
1473         my $pcno = shift;
1474         my $line = shift;
1475         my $origin = shift;
1476         my $pc = shift;
1477
1478         if (eph_dup($line)) {
1479                 return;
1480         }
1481
1482         if ($pc->[1] eq $main::mycall) {
1483                 DXMsg::handle_49($self, @$pc);
1484         } else {
1485                 $self->route($pc->[1], $line) unless $self->is_clx;
1486         }
1487 }
1488
1489 # keep alive/user list
1490 sub handle_50
1491 {
1492         my $self = shift;
1493         my $pcno = shift;
1494         my $line = shift;
1495         my $origin = shift;
1496         my $pc = shift;
1497
1498         return if (eph_dup($line));
1499
1500         my $call = $pc->[1];
1501
1502         my $node = Route::Node::get($call);
1503         if ($node) {
1504                 return unless $node->call eq $self->{call};
1505                 $node->usercount($pc->[2]) unless $node->users;
1506                 $node->reset_obs;
1507                 $node->PC92C_dxchan($self->call, $pc->[-1]);
1508
1509                 # input filter if required
1510 #               return unless $self->in_filter_route($node);
1511
1512                 unless ($self->{isolate}) {
1513                         DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1514                 }
1515 #               $self->route_pc50($origin, $line, $node, $pc->[2], $pc->[3]) unless eph_dup($line);
1516         }
1517 }
1518
1519 # incoming ping requests/answers
1520 sub handle_51
1521 {
1522         my $self = shift;
1523         my $pcno = shift;
1524         my $line = shift;
1525         my $origin = shift;
1526         my $pc = shift;
1527
1528         my $to = $pc->[1];
1529         my $from = $pc->[2];
1530         my $flag = $pc->[3];
1531
1532         if ($to eq $main::myalias) {
1533                 dbg("DXPROT: Ping addressed to \$myalias ($main::myalias), ignored") if isdbg('chan');
1534                 return;
1535         }
1536
1537         # is it for us?
1538         if ($to eq $main::mycall) {
1539                 if ($flag == 1) {
1540                         $self->send(pc51($from, $to, '0'));
1541                 } else {
1542                         DXXml::Ping::handle_ping_reply($self, $from);
1543                 }
1544         } else {
1545                 if (eph_dup($line)) {
1546                         return;
1547                 }
1548                 # route down an appropriate thingy
1549                 $self->route($to, $line);
1550         }
1551 }
1552
1553 sub handle_61 { goto &handle_11; }
1554
1555 # dunno but route it
1556 sub handle_75
1557 {
1558         my $self = shift;
1559         my $pcno = shift;
1560         my $line = shift;
1561         my $origin = shift;
1562         my $pc = shift;
1563
1564         my $call = $pc->[1];
1565         if ($call ne $main::mycall) {
1566                 $self->route($call, $line);
1567         }
1568 }
1569
1570 # WCY broadcasts
1571 sub handle_73
1572 {
1573         my $self = shift;
1574         my $pcno = shift;
1575         my $line = shift;
1576         my $origin = shift;
1577         my $pc = shift;
1578
1579         my $call = $pc->[1];
1580
1581         # do some de-duping
1582         my $d = cltounix($call, sprintf("%02d18Z", $pc->[2]));
1583         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $pc->[2] < 0 || $pc->[2] > 23) {
1584                 dbg("PCPROT: WCY Date ($call $pc->[2]) out of range") if isdbg('chanerr');
1585                 return;
1586         }
1587         $pc = [ map { unpad($_) } @$pc ];
1588         if (WCY::dup($d)) {
1589                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1590                 return;
1591         }
1592
1593         my $wcy = WCY::update($d, @$pc[2..12]);
1594         dbg("WCY: <$pc->[2]> K=$pc->[5] expK=$pc->[6] A=$pc->[4] R=$pc->[7] SFI=$pc->[3] SA=$pc->[8] GMF=$pc->[9] Au=$pc->[10] $pc->[11]\@$pc->[12] route: $origin") if isdbg('progress');
1595
1596         if (defined &Local::wcy) {
1597                 my $rep;
1598                 eval {
1599                         $rep = Local::wcy($self, @$pc[1..12]);
1600                 };
1601                 return if $rep;
1602         }
1603
1604         # broadcast to the eager world
1605         send_wcy_spot($self, $line, $d, @$pc[2..12]);
1606 }
1607
1608 # remote commands (incoming)
1609 sub handle_84
1610 {
1611         my $self = shift;
1612         my $pcno = shift;
1613         my $line = shift;
1614         my $origin = shift;
1615         my $pc = shift;
1616
1617         $self->process_rcmd($pc->[1], $pc->[2], $pc->[3], $pc->[4]);
1618 }
1619
1620 # remote command replies
1621 sub handle_85
1622 {
1623         my $self = shift;
1624         my $pcno = shift;
1625         my $line = shift;
1626         my $origin = shift;
1627         my $pc = shift;
1628
1629         $self->process_rcmd_reply($pc->[1], $pc->[2], $pc->[3], $pc->[4]);
1630 }
1631
1632 # decode a pc92 call: flag call : version : build
1633 sub _decode_pc92_call
1634 {
1635         my $icall = shift;
1636         my @part = split /:/, $icall;
1637         my ($flag, $call) = unpack "A A*", $part[0];
1638         unless (defined $flag && $flag ge '0' && $flag le '7') {
1639                 dbg("PCPROT: $icall no flag byte (0-7) at front of call, ignored") if isdbg('chanerr');
1640                 return ();
1641         }
1642         unless ($call && is_callsign($call)) {
1643                 dbg("PCPROT: $icall no recognisable callsign, ignored") if isdbg('chanerr');
1644                 return ();
1645         }
1646         my $is_node = $flag & 4;
1647         my $is_extnode = $flag & 2;
1648         my $here = $flag & 1;
1649         my $version = $part[1] || 0;
1650         my $build = $part[2] || 0;
1651         my $ip = $part[3] || '';
1652
1653         # remember that, at this stage, IPV6 addresses have : replaced by ,
1654         if (length $version > 4 && $version =~ /[,\.][\da-f]{1,4}/i) {
1655                 $ip = $version;
1656                 $version = 0;
1657         }
1658         $version =~ s/\D//g;
1659         $version = 0 unless $version && $version =~ /^[0-9]+$/;
1660         $build =~ s/^0\.//;
1661         $build =~ s/\D//g;
1662         $build = 0 unless $build &&  $build =~ /^[0-9]+$/;
1663         if ($ip) {
1664         $ip =~ s/,/:/g;
1665         $ip =~ s/^::ffff://i;
1666                 $ip = '' unless is_ipaddr($ip);
1667     }
1668         dbg("'$icall' = '" . join("', '", $call, $is_node, $is_extnode, $here, $version, $build, $ip) . "'") if isdbg('pc92');
1669         return ($call, $is_node, $is_extnode, $here, $version+0, $build+0, $ip);
1670 }
1671
1672 # decode a pc92 call: flag call : version : build
1673 sub _encode_pc92_call
1674 {
1675         my $ref = shift;
1676
1677         # plain call or value
1678         return $ref unless ref $ref;
1679
1680         my $ext = shift || 0;
1681         my $flag = 0;
1682         my $call = $ref->call;
1683         my $extra = '';
1684         $flag |= $ref->here ? 1 : 0;
1685         if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
1686                 $flag |= 4;
1687                 my $dxchan = DXChannel::get($call);
1688                 $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
1689                 if (($ext & 1) && $ref->version) {
1690                         my $version = $ref->version || 1.0;
1691                         $version =  $version * 100 + 5300 if $version < 50;
1692                         $extra .= ":" . $version;
1693                 }
1694         }
1695         if (($ext & 2) && $ref->ip) {
1696                 my $ip = $ref->ip;
1697                 $ip =~ s/:/,/g;
1698                 $extra .= ':' . $ip;
1699         }
1700
1701         return "$flag$call$extra";
1702 }
1703
1704 my %things_add;
1705 my %things_del;
1706
1707 sub _add_thingy
1708 {
1709         my $parent = shift;
1710         my $s = shift;
1711         my $dxchan = shift;
1712         my $hops = shift;
1713
1714         my ($call, $is_node, $is_extnode, $here, $version, $build, $ip) = @$s;
1715         my @rout;
1716
1717         # remove spurious IPV6 prefix on IPV4 addresses
1718         $build ||= 0;
1719         $version ||= 0;
1720
1721         if ($call) {
1722                 my $ncall = $parent->call;
1723                 if ($ncall ne $call) {
1724                         my $user;
1725                         my $r;
1726
1727                         # normalise call, delete any unnormalised calls in the users file.
1728                         # then ignore this thingy
1729                         my $normcall = normalise_call($call);
1730                         if ($normcall ne $call) {
1731                                 next if DXChannel::get($call);
1732                                 $user = DXUser::get($call);
1733                                 dbg("DXProt::_add_thingy call $call normalised to $normcall, deleting spurious user $call");
1734                                 $user->del if $user;
1735                             $call = $normcall; # this is safe because a route add will ignore duplicates
1736                         }
1737                         
1738                         if ($is_node) {
1739                                 dbg("ROUTE: added node $call to $ncall") if isdbg('routelow');
1740                                 $user = check_add_user($call, 'A');
1741                                 @rout = $parent->add($call, $version, Route::here($here), $ip);
1742                                 $r = Route::Node::get($call);
1743                                 $r->PC92C_dxchan($dxchan->call, $hops) if $r;
1744                                 if ($version && is_numeric($version) && !$r->K && !$user->K) {
1745                                         my $old = $user->sort;
1746                                         if ($user->is_ak1a && (($version >= 5455 &&  $build > 0) || ($version >= 3000 && $version <= 3500)) ) {
1747                                                 $user->sort('S');
1748                                                 dbg("PCPROT::_add_thingy node $call v: $version b: $build sort ($old) updated to " . $user->sort);
1749                                         } elsif ($user->is_spider && ($version < 3000 || ($version > 4000 && $version < 5455))) {
1750                                                 unless ($version > 0  && $build == 0) {
1751                                                         $user->sort('A');
1752                                                         $build ||= 0;
1753                                                         dbg("PCPROT::_add_thingy node $call v: $version b: $build sort ($old) downgraded to " . $user->sort);
1754                                                 }
1755                                         }
1756                                 }
1757                                 $r->version($user->version) if $user->version;
1758                                 $r->build($user->build) if $user->build;
1759                                 $r->K(1) if $user->K;
1760                         } else {
1761                                 dbg("ROUTE: added user $call to $ncall") if isdbg('routelow');
1762                                 $user = check_add_user($call, 'U', $parent->call);
1763                                 @rout = $parent->add_user($call, Route::here($here), $ip);
1764                                 $dxchan->tell_buddies('loginb', $call, $ncall) if $dxchan;
1765                                 $r = Route::User::get($call);
1766                         }
1767                         if ($ip) {
1768                                 $r->ip($ip);
1769                                 Log('DXProt', "PC92A $call -> $ip on $ncall");
1770                         }
1771                         if ($pc92_slug_changes && $parent == $main::routeroot) {
1772                                 $things_add{$call} = Route::get($call);
1773                                 delete $things_del{$call};
1774                         }
1775                         $user->close($main::systime, $ip) if $user;             # this just updates lastseen and the connlist list containing the IP address
1776                 } else {                                
1777                         dbgprintring(10) if isdbg('nologchan');
1778                         dbg("DXProt::add_thingy: Trying to add parent $call to itself $ncall, ignored");
1779                 }
1780         }
1781         
1782         return @rout;
1783 }
1784
1785 sub _del_thingy
1786 {
1787         my $parent = shift;
1788         my $s = shift;
1789         my $dxchan = shift;
1790         my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1791         my @rout;
1792         if ($call) {
1793                 my $ref;
1794                 if ($is_node) {
1795                         $ref = Route::Node::get($call);
1796                         dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
1797                         @rout = $ref->del($parent) if $ref;
1798                 } else {
1799                         dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
1800                         $ref = Route::User::get($call);
1801                         if ($ref) {
1802                                 $dxchan->tell_buddies('logoutb', $call, $parent->call) if $dxchan;
1803                                 @rout = $parent->del_user($ref);
1804                         }
1805                 }
1806                 if ($pc92_slug_changes && $parent == $main::routeroot) {
1807                         $things_del{$call} = $ref unless exists $things_add{$call};
1808                         delete $things_add{$call};
1809                 }
1810         }
1811         return @rout;
1812 }
1813
1814 # this will only happen if we are slugging changes and
1815 # there are some changes to be sent, it will create an add or a delete
1816 # or both
1817 sub gen_pc92_changes
1818 {
1819         my @add = values %things_add;
1820         my @del = values %things_del;
1821         return (\@add, \@del);
1822 }
1823
1824 sub clear_pc92_changes
1825 {
1826         %things_add = %things_del = ();
1827         $last_pc92_slug = $main::systime;
1828 }
1829
1830 my $_last_time;
1831 my $_last_occurs;
1832 my $_last_pc9x_id;
1833
1834 sub last_pc9x_id
1835 {
1836         return $_last_pc9x_id;
1837 }
1838
1839 sub gen_pc9x_t
1840 {
1841         if (!$_last_time || $_last_time != $main::systime) {
1842                 $_last_time = $main::systime;
1843                 $_last_occurs = 0;
1844                 return $_last_pc9x_id = $_last_time - $main::systime_daystart;
1845         } else {
1846                 $_last_occurs++;
1847                 return $_last_pc9x_id = sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
1848         }
1849 }
1850
1851 sub check_pc9x_t
1852 {
1853         my $call = shift;
1854         my $t = shift;
1855         my $pc = shift;
1856         my $create = shift;
1857
1858         # check that the time is between 0 >= $t < 86400
1859         unless ($t >= 0 && $t < 86400) {
1860                 dbg("PCPROT: time invalid t: $t, ignored") if isdbg('chanerr');
1861                 return undef;
1862         }
1863
1864         # check that the time of this pc9x is within tolerance (default 15 mins either way)
1865         my $now = $main::systime - $main::systime_daystart ;
1866         my $diff = abs($now - $t);
1867         unless ($diff < $pc9x_time_tolerance || 86400 - $diff < $pc9x_time_tolerance) {
1868                 my $c = ref $call ? $call->call : $call;
1869                 dbg("PC9XERR: $c time out of range t: $t now: $now diff: $diff > $pc9x_time_tolerance, ignored") if isdbg('chan');
1870                 return undef;
1871         }
1872
1873         my $parent = ref $call ? $call : Route::Node::get($call);
1874         if ($parent) {
1875                 # we only do this for external calls whose routing table
1876                 # record come and go. The reference for mycall is permanent
1877                 # and not that frequently used, it also never times out, so
1878                 # the id on it is completely unreliable. Besides, only commands
1879                 # originating on this box will go through this code...
1880                 if ($parent->call ne $main::mycall) {
1881                         my $lastid = $parent->lastid;
1882                         if (defined $lastid) {
1883                                 if ($t < $lastid) {
1884                                         # note that this is where we determine whether this pc9x has come in yesterday
1885                                         # but is still greater (modulo 86400) than the lastid or is simply an old
1886                                         # duplicate sentence. To determine this we need to do some module 86400
1887                                         # arithmetic. High numbers mean that this is an old duplicate sentence,
1888                                         # low numbers that it is a new sentence.
1889                                         #
1890                                         # Typically you will see yesterday being taken on $t = 84, $lastid = 86235
1891                                         # and old dupes with $t = 234, $lastid = 256 (which give answers 249 and
1892                                         # 86378 respectively in the calculation below).
1893                                         #
1894                                         if ($t+86400-$lastid > $pc9x_past_age) {
1895                                                 dbg("PCPROT: dup id on $t <= lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1896                                                 return undef;
1897                                         }
1898                                 } elsif ($t == $lastid) {
1899                                         dbg("PCPROT: dup id on $t == lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1900                                         return undef;
1901                                 } else {
1902                                         # check that if we have a low number in lastid that yesterday's numbers
1903                                         # (likely in the 85000+ area) don't override them, thus causing flip flopping
1904                                         if ($lastid+86400-$t < $pc9x_past_age) {
1905                                                 dbg("PCPROT: dup id on $t in yesterday, lastid $lastid, ignored") if isdbg('chanerr') || isdbg('pc92dedupe');
1906                                                 return undef;
1907                                         }
1908                                 }
1909                         }
1910                 }
1911         } elsif ($create) {
1912                 $parent = Route::Node->new($call);
1913         } else {
1914                 dbg("PCPROT: $call does not exist, ignored") if isdbg('pc92dedupe');
1915                 return undef;
1916         }
1917         if (isdbg('pc92dedupe')) {
1918                 my $exists = exists $parent->{lastid}; # naughty, naughty :-)
1919                 my $val = $parent->{lastid};
1920                 my $s = $exists ? (defined $val ? $val : 'exists/undef') : 'undef';
1921                 dbg("PCPROT: $call pc92 id lastid $s -> $t");
1922         }
1923         $parent->lastid($t);
1924
1925         return $parent;
1926 }
1927
1928 sub pc92_handle_first_slot
1929 {
1930         my $self = shift;
1931         my $slot = shift;
1932         my $parent = shift;
1933         my $t = shift;
1934         my $hops = shift;
1935         my $oparent = $parent;
1936
1937         my @radd;
1938
1939         my ($call, $is_node, $is_extnode, $here, $version, $build) = @$slot;
1940         if ($call && $is_node) {
1941                 if ($call eq $main::mycall) {
1942                         LogDbg('err', "PCPROT: $self->{call} : $call looped back onto \$main::mycall ($main::mycall), ignored");
1943                         return;
1944                 }
1945                 if ($call eq $main::myalias) {
1946                         LogDbg('err', "PCPROT: $self->{call} : $call looped back onto \$main::myalias ($main::myalias), ignored");
1947                         return;
1948                 }
1949                 # this is only accepted from my "self".
1950                 # this also kills configs from PC92 nodes with external PC19 nodes that are also
1951                 # locally connected. Local nodes always take precedence. But we remember the lastid
1952                 # to try to reduce the number of dupe PC92s for this external node.
1953                 if (DXChannel::get($call) && $call ne $self->{call}) {
1954                         $parent = check_pc9x_t($call, $t, 92); # this will update the lastid time
1955                         dbg("PCPROT: locally connected node $call from other another node $self->{call}, ignored") if isdbg('chanerr');
1956                         return;
1957                 }
1958                 if ($is_extnode) {
1959                         # reparent to external node (note that we must have received a 'C' or 'A' record
1960                         # from the true parent node for this external before we get one for the this node
1961                         unless ($parent = Route::Node::get($call)) {
1962                                 if ($is_extnode && $oparent) {
1963                                         @radd = _add_thingy($oparent, $slot, $self, $hops);
1964                                         $parent = $radd[0];
1965                                 } else {
1966                                         dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
1967                                         return;
1968                                 }
1969                         }
1970                         $parent = check_pc9x_t($call, $t, 92) || return;
1971                         $parent->via_pc92(1);
1972                         $parent->PC92C_dxchan($self->{call}, $hops);
1973                 }
1974         } else {
1975                 dbg("PCPROT: must be \$mycall or external node as first entry, ignored") if isdbg('chanerr');
1976                 return;
1977         }
1978         $parent->here(Route::here($here));
1979         $parent->version($version || $pc19_version) if $version;
1980     $parent->build($build) if $build;
1981         $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
1982         return ($parent, @radd);
1983 }
1984
1985 # DXSpider routing entries
1986 sub handle_92
1987 {
1988         my $self = shift;
1989         my $pcno = shift;
1990         my $line = shift;
1991         my $origin = shift;
1992         my $pc = shift;
1993
1994         my (@radd, @rdel);
1995
1996         my $pcall = $pc->[1];
1997         my $t = $pc->[2];
1998         my $sort = $pc->[3];
1999         my $hops = $pc->[-1];
2000
2001         # this catches loops of A/Ds
2002 #       if (eph_dup($line, $pc9x_dupe_age)) {
2003 #               return;
2004 #       }
2005
2006         if ($pcall eq $main::mycall) {
2007                 LogDbg('err', "PCPROT: looped back, ignored");
2008                 return;
2009         }
2010
2011         if ($pcall eq $main::myalias) {
2012                 LogDbg('err', "PCPROT: looped back to \$myalias ($main::myalias), misconfiguration ignored");
2013                 return;
2014         }
2015
2016         if ($pcall eq $self->{call} && $self->{state} eq 'init') {
2017                 if ($self->{isolate}) {
2018                         dbg("DXPROT: PC9x received, but $pcall is isolated, ignored");
2019                         return;
2020                 } elsif (!$self->user->wantpc9x) {
2021                         dbg("DXPROT: PC9x explicitly switched off on $pcall, ignored");
2022                         return;
2023                 } else {
2024                         $self->state('init92');
2025                         $self->{do_pc9x} = 1;
2026                         dbg("DXPROT: Do pc9x set on $pcall");
2027                 }
2028         }
2029         unless ($self->{do_pc9x}) {
2030                 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
2031                 return;
2032         }
2033
2034         # don't create routing entries for D records that don't already exist
2035         # this is what causes all those PC92 loops!
2036         my $parent = check_pc9x_t($pcall, $t, 92, $sort ne 'D') || return;
2037         my $oparent = $parent;
2038
2039         $parent->do_pc9x(1);
2040         $parent->via_pc92(1);
2041
2042         if ($sort eq 'F' || $sort eq 'R') {
2043
2044                 # this is the route finding section
2045                 # here is where the consequences of the 'find' command
2046                 # are dealt with
2047
2048                 my $from = $pc->[4];
2049                 my $target = $pc->[5];
2050
2051                 if ($sort eq 'F') {
2052                         my $flag;
2053                         my $ref;
2054                         my $dxchan;
2055                         if ($ref = DXChannel::get($target)) {
2056                                 $flag = 1;              # we are directly connected
2057                         } else {
2058                                 $ref = Route::get($target);
2059                                 $dxchan = $ref->dxchan;
2060                                 $flag = 2;
2061                         }
2062                         if ($ref && $flag && $dxchan) {
2063                                 $self->send(pc92r($from, $target, $flag, int($dxchan->{pingave}*1000)));
2064                                 return;
2065                         }
2066                 } elsif ($sort eq 'R') {
2067                         if (my $dxchan = DXChannel::get($from)) {
2068                                 handle_pc92_find_reply($dxchan, $pcall, $from, $target, @$pc[6,7]);
2069                         } else {
2070                                 my $ref = Route::get($from);
2071                                 if ($ref) {
2072                                         my @dxchan = grep {$_->do_pc9x} $ref->alldxchan;
2073                                         if (@dxchan) {
2074                                                 $_->send($line) for @dxchan;
2075                                         } else {
2076                                                 dbg("PCPROT: $self->{call} : type R no return route, ignored") if isdbg('chanerr') || isdbg('route');
2077                                         }
2078                                 } else {
2079                                         dbg("PCPROT: $self->{call} : type R no return route, ignored") if isdbg('chanerr') || isdbg('route');
2080                                 }
2081                         }
2082                         return;
2083                 }
2084
2085         } elsif ($sort eq 'K') {
2086                 $pc92Kin += length $line;
2087
2088                 # remember the last channel we arrived on
2089                 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
2090
2091                 my @ent = _decode_pc92_call($pc->[4]);
2092
2093                 if (@ent) {
2094                         my $add;
2095
2096                         ($parent, $add) = $self->pc92_handle_first_slot(\@ent, $parent, $t, $hops);
2097                         return unless $parent; # dupe
2098                         
2099                         push @radd, $add if $add;
2100                         $parent->reset_obs;
2101                         my $call = $parent->call;
2102                         my $version = $ent[4] || 0;
2103                         my $build = $ent[5] ||  0;
2104                         $build =~ s/^0\.//;
2105                         my $oldbuild = $parent->build || 0;
2106                         my $oldversion = $parent->version || 0;
2107                         my $user = check_add_user($parent->call, 'S');
2108                         my $oldsort = $user->sort // '';
2109
2110                         dbg("PCPROT: PC92 K v: $version ov: $oldversion b: $build ob: $oldbuild pk: " . ($parent->K || '0') . " uk: " . ($user->K || 0)) if isdbg('pc92k');
2111                                 
2112                         if (is_numeric($version) || is_numeric($build)) {
2113                                 my $changed = 0;
2114                                 if (($oldversion ne $version || $build ne $oldbuild)) {
2115                                         dbg("PCPROT: PC92 K node $call updated version: $version (was $oldversion) build: $build (was $oldbuild)");
2116                                         $user->version($parent->version($version));
2117                                         $user->build($parent->build($build));
2118                                         ++$changed;
2119                                 }
2120                                 if ($oldsort ne 'S') {
2121                                         dbg("PCPROT: PC92 K node $call updated sort: $sort (was $oldsort)");
2122                                         $user->sort('S');
2123                                         ++$changed;
2124                                 }
2125                                 unless ($user->K) {
2126                                         dbg("PCPROT: PC92 K node $call updated - marked as PC92 K user");
2127                                         $user->K(1);
2128                                         ++$changed;
2129                                 }
2130                                 $user->put if $changed;
2131                                 $parent->K(1); # mark this as come in on a K
2132                         } else {
2133                                 dbg("DXProt PC92 K version call $call: invalid version: '$version' or build: '$version', ignored");
2134                         }
2135                         dbg("ROUTE: reset obscount on $call now " . $parent->obscount) if isdbg('obscount');
2136                 }
2137         } elsif ($sort eq 'A' || $sort eq 'D' || $sort eq 'C') {
2138
2139                 $pc92Ain += length $line if $sort eq 'A';
2140                 $pc92Cin += length $line if $sort eq 'C';
2141                 $pc92Din += length $line if $sort eq 'D';
2142
2143                 # remember the last channel we arrived on
2144                 $parent->PC92C_dxchan($self->{call}, $hops) unless $self->{call} eq $parent->call;
2145
2146                 # this is the main route section
2147                 # here is where all the routes are created and destroyed
2148
2149                 # cope with missing duplicate node calls in the first slot
2150                 my $me = $pc->[4] || '';
2151                 $me ||= _encode_pc92_call($parent) unless $me ;
2152
2153                 my @ent = map {my @a = _decode_pc92_call($_); @a ? \@a : ()} grep {$_ && /^[0-7]/} $me, @$pc[5 .. $#$pc];
2154
2155                 if (@ent) {
2156
2157                         # look at the first one which will always be a node of some sort
2158                         # except in the case of 'A' or 'D' in which the $pcall is used
2159                         # otherwise use the node call and update any information
2160                         # that needs to be done.
2161                         my $add;
2162
2163                         ($parent, $add) = $self->pc92_handle_first_slot($ent[0], $parent, $t, $hops);
2164                         return unless $parent; # dupe
2165
2166                         shift @ent;
2167                         push @radd, $add if $add;
2168                 }
2169
2170                 # do a pass through removing any references to either mycall
2171                 my @nent;
2172                 for (@ent) {
2173                         my $dxc;
2174                         next unless $_ && @$_;
2175                         if ($_->[0] eq $main::mycall) {
2176                                 dbg("PCPROT: $self->{call} : type $sort $_->[0] refers to me, ignored") if isdbg('route');
2177                                 next;
2178                         }
2179                         if ($_->[0] eq $main::myalias && $_->[1] || $_->[0] eq $main::mycall && $_->[1] == 0) {
2180                                 LogDbg('err',"PCPROT: $self->{call} : type $sort $_->[0] trying to change type to " . $_->[1]?"Node":"User" . ", ignored");
2181                                 next;
2182                         }
2183                         
2184                         push @nent, $_;
2185                 }
2186
2187                 if ($sort eq 'A') {
2188                         for (@nent) {
2189                                 push @radd, _add_thingy($parent, $_, $self, $hops);
2190                         }
2191                 } elsif ($sort eq 'D') {
2192                         for (@nent) {
2193                                 push @rdel, _del_thingy($parent, $_, $self);
2194                         }
2195                 } elsif ($sort eq 'C') {
2196                         my (@nodes, @users);
2197
2198                         # we reset obscounts on config records as well as K records
2199                         $parent->reset_obs;
2200                         dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('obscount');
2201
2202                         #
2203                         foreach my $r (@nent) {
2204                                 #                       my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);
2205                                 if ($r->[0]) {
2206                                         if ($r->[1]) {
2207                                                 push @nodes, $r->[0];
2208                                         } else {
2209                                                 push @users, $r->[0];
2210                                         }
2211                                 } else {
2212                                         dbg("PCPROT: $self->{call} :  pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr') || isdbg('route');
2213                                 }
2214                         }
2215
2216                         my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
2217
2218                         # add users here
2219                         foreach my $r (@nent) {
2220                                 my $call = $r->[0];
2221                                 if ($call) {
2222                                         push @radd,_add_thingy($parent, $r, $self, $hops) if grep $call eq $_, (@$nnodes, @$nusers);
2223                                 }
2224                         }
2225                         # del users here
2226                         foreach my $r (@$dnodes) {
2227                                 push @rdel,_del_thingy($parent, [$r, 1], $self);
2228                         }
2229                         foreach my $r (@$dusers) {
2230                                 push @rdel,_del_thingy($parent, [$r, 0], $self);
2231                         }
2232
2233                         # remember this last PC92C for rebroadcast on demand
2234                         $parent->last_PC92C($line);
2235                 } else {
2236                         dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
2237                         return;
2238                 }
2239
2240                 foreach my $r (@rdel) {
2241                         next unless $r;
2242
2243                         $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
2244                         $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
2245                 }
2246                 my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
2247                 my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
2248                 unshift @pc19, $parent if $self->{state} eq 'init92' && $oparent == $parent;
2249                 $self->route_pc19($pcall, undef, @pc19) if @pc19;
2250                 $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
2251         }
2252
2253         # broadcast it if we get here
2254         $self->broadcast_route_pc9x($pcall, undef, $line, 0);
2255 }
2256
2257 # get all the routes for a thing, bearing in mind that the thing (e.g. a user)
2258 # might be on two or more nodes at the same time or that there may be more than
2259 # one equal distance neighbour to a node.
2260 #
2261 # What this means that if sh/route g1tlh shows that he is on (say) two nodes, then
2262 # a Route::findroutes is done on each of those two nodes, the best route(s) taken from
2263 # each and then combined to give a set of dxchans to send the PC9x record down
2264 #
2265 sub find_pc9x_routes
2266 {
2267         my $to = shift;
2268         my $ref = Route::get($to);
2269         my @parent;
2270         my %cand;
2271
2272         if ($ref->isa('Route::User')) {
2273                 my $dxchan = DXChannel::get($to);
2274                 push @parent, $to if $dxchan;
2275                 push @parent, $ref->parents;
2276         } else {
2277                 @parent = $to;
2278         }
2279         foreach my $p (@parent) {
2280                 my $lasthops;
2281                 my @routes = Route::findroutes($p);
2282                 foreach my $r (@routes) {
2283                         $lasthops = $r->[0] unless defined $lasthops;
2284                         if ($r->[0] == $lasthops) {
2285                                 $cand{$r->[1]->call} = $r->[1];
2286                         } else {
2287                                 last;
2288                         }
2289                 }
2290         }
2291         return values %cand;
2292 }
2293
2294 sub handle_93
2295 {
2296         my $self = shift;
2297         my $pcno = shift;
2298         my $line = shift;
2299         my $origin = shift;
2300         my $pc = shift;
2301
2302 #       $self->{do_pc9x} ||= 1;
2303
2304         my $pcall = $pc->[1];                   # this is now checked earlier
2305
2306         # remember that we are converting PC10->PC93 and self will be $main::me if it
2307         # comes from us
2308         unless ($self->{do_pc9x}) {
2309                 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
2310                 return;
2311         }
2312
2313         my $t = $pc->[2];
2314         my $parent = check_pc9x_t($pcall, $t, 93, 1) || return;
2315
2316         my $to = uc $pc->[3];
2317         my $from = uc $pc->[4];
2318         my $via = uc $pc->[5];
2319         my $text = $pc->[6];
2320         my $onode = uc $pc->[7];
2321         $onode = $pcall if @$pc <= 8;
2322
2323         # this is catch loops caused by bad software ...
2324         if (eph_dup("PC93|$from|$text|$onode", $pc10_dupe_age)) {
2325                 return;
2326         }
2327
2328         if (isdbg('progress')) {
2329                 my $vs = $via ne '*' ? " via $via" : ''; 
2330                 my $s = "ANNTALK: $from\@$onode$vs -> $to '$text' route: $origin";
2331                 dbg($s);
2332         }
2333
2334         # if this is a 'bad spotter' user then ignore it
2335         my $nossid = $from;
2336         $nossid =~ s/-\d+$//;
2337         if ($badspotter->in($nossid)) {
2338                 dbg($line) if isdbg('nologchan');
2339                 dbg("PCPROT: Bad Spotter, dropped");
2340                 return;
2341         }
2342
2343         # ignore PC93 coming in from outside this node with a target of local
2344         if ($to eq 'LOCAL' && $self != $main::me) {
2345                 dbg("PCPROT: incoming LOCAL chat not from local node, ignored") if isdbg('chanerr');
2346                 return;
2347         }
2348
2349         # will we allow it at all?
2350         if ($censorpc) {
2351                 my @bad;
2352                 if (@bad = BadWords::check($text)) {
2353                         my $bw = join ', ', @bad;
2354                         dbg($line) if isdbg('nologchan');
2355                         dbg("PCPROT: Badwords: '$bw', dropped");
2356                         return;
2357                 }
2358         }
2359
2360         # if it is routeable then then treat it like a talk
2361         my $ref = Route::get($to);
2362         if ($ref) {
2363                 my $dxchan;
2364
2365                 # convert to PC10 or local talks where appropriate
2366                 # PC93 capable nodes of the same hop count all get a copy
2367                 # if there is a PC10 node then it will get a copy and that
2368                 # will be it. Hopefully such a node will not figure highly
2369                 # in the route list, unless it is local, 'cos it don't issue PC92s!
2370                 # note that both local and PC93s at the same time are possible if the
2371                 # user on more than one node.
2372                 my @routes = find_pc9x_routes($to);
2373                 my $lasthops;
2374                 foreach $dxchan (@routes) {
2375                         if (ref $dxchan && $dxchan->isa('DXChannel')) {
2376                                 if ($dxchan->{do_pc9x}) {
2377                                         $dxchan->send($line);
2378                                 } else {
2379                                         $dxchan->talk($from, $to, $via, $text, $onode);
2380                                 }
2381                         } else {
2382                                 dbg("ERROR: $to -> $dxchan is not a DXChannel! (convert to pc10)");
2383                         }
2384                 }
2385                 return;
2386
2387         } elsif ($to eq '*' || $to eq 'SYSOP' || $to eq 'WX') {
2388                 # announces
2389                 my $sysop = $to eq 'SYSOP' ? '*' : ' ';
2390                 my $wx = $to eq 'WX' ? '1' : '0';
2391                 my $local = $via eq 'LOCAL' ? '*' : $via;
2392
2393                 $self->send_announce(1, pc12($from, $text, $local, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
2394                 return if $via eq 'LOCAL';
2395         } elsif (!is_callsign($to) && $text =~ /^#\d+ /) {
2396                 # chat messages really only locally connected users
2397                 $self->send_chat(1, $line, $from, '*', $text, $to, $pcall, '0');
2398         }
2399
2400         # broadcast this chat sentence everywhere unless it is targetted to 'LOCAL'
2401         $self->broadcast_route_pc9x($pcall, undef, $line, 0) unless $to eq 'LOCAL' || $via eq 'LOCAL';
2402 }
2403
2404 # if get here then rebroadcast the thing with its Hop count decremented (if
2405 # there is one). If it has a hop count and it decrements to zero then don't
2406 # rebroadcast it.
2407 #
2408 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
2409 #        REBROADCAST!!!!
2410 #
2411
2412 sub handle_default
2413 {
2414         my $self = shift;
2415         my $pcno = shift;
2416         my $line = shift;
2417         my $origin = shift;
2418         my $pc = shift;
2419
2420         unless (eph_dup($line)) {
2421                 if ($pcno >= 90) {
2422                         my $pcall = $pc->[1];
2423                         unless (is_callsign($pcall)) {
2424                                 dbg("PCPROT: invalid callsign string '$pc->[1]', ignored") if isdbg('chanerr');
2425                                 return;
2426                         }
2427                         my $t = $pc->[2];
2428                         my $parent = check_pc9x_t($pcall, $t, $pcno, 1) || return;
2429                         $self->broadcast_route_pc9x($pcall, undef, $line, 0);
2430                 } else {
2431                         unless ($self->{isolate}) {
2432                                 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
2433                         }
2434                 }
2435         }
2436 }
2437
2438 1;