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