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