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