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