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