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