replace eph_dup for PC21s
[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 (eph_dup($line)) {
876                 dbg("PCPROT: dup PC21 detected") if isdbg('chanerr');
877                 return;
878         }
879
880         if (@rout) {
881                 $self->route_pc21($origin, $line, @rout);
882 #               $self->route_pc92d($main::mycall, $line, $main::routeroot, @rout);
883         }
884 }
885                 
886
887 sub handle_22
888 {
889         my $self = shift;
890         my $pcno = shift;
891         my $line = shift;
892         my $origin = shift;
893
894         if ($self->{do_pc9x}) {
895                 if ($self->{state} ne 'init92') {
896                         dbg("PCPROT: disconnecting because login call not sent in any pc92") if isdbg('chanerr');
897                         $self->send("**** You logged in with $self->{call} but that is NOT your \$mycall");
898                         $self->disconnect;
899                         return;
900                 }
901         }
902         $self->{lastping} = 0;
903         $self->state('normal');
904         $self->send_delayed_pc92;
905 }
906                                 
907 # WWV info
908 sub handle_23
909 {
910         my $self = shift;
911         my $pcno = shift;
912         my $line = shift;
913         my $origin = shift;
914                         
915         # route foreign' pc27s 
916         if ($pcno == 27) {
917                 if ($_[8] ne $main::mycall) {
918                         $self->route($_[8], $line);
919                         return;
920                 }
921         }
922
923         # only do a rspf check on PC23 (not 27)
924         if ($pcno == 23) {
925                 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
926         }
927
928         # do some de-duping
929         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
930         my $sfi = unpad($_[3]);
931         my $k = unpad($_[4]);
932         my $i = unpad($_[5]);
933         my ($r) = $_[6] =~ /R=(\d+)/;
934         $r = 0 unless $r;
935         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
936                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
937                 return;
938         }
939
940         # global wwv filtering on INPUT
941         my @dxcc = ((Prefix::cty_data($_[7]))[0..2], (Prefix::cty_data($_[8]))[0..2]);
942         if ($self->{inwwvfilter}) {
943                 my ($filter, $hops) = $self->{inwwvfilter}->it(@_[7,8], $origin, @dxcc);
944                 unless ($filter) {
945                         dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
946                         return;
947                 }
948         }
949         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
950         if (Geomag::dup($d,$sfi,$k,$i,$_[6],$_[7])) {
951                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
952                 return;
953         }
954                 
955         # note this only takes the first one it gets
956         Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
957
958         my $rep;
959         eval {
960                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
961         };
962         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
963         return if $rep;
964
965         # DON'T be silly and send on PC27s!
966         return if $pcno == 27;
967
968         # broadcast to the eager world
969         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
970 }
971                 
972 # set here status
973 sub handle_24
974 {
975         my $self = shift;
976         my $pcno = shift;
977         my $line = shift;
978         my $origin = shift;
979         my $call = uc $_[1];
980         my ($nref, $uref);
981         $nref = Route::Node::get($call);
982         $uref = Route::User::get($call);
983         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
984                         
985         if (eph_dup($line)) {
986                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
987                 return;
988         }
989         
990         $nref->here($_[2]) if $nref;
991         $uref->here($_[2]) if $uref;
992         my $ref = $nref || $uref;
993         return unless $self->in_filter_route($ref);
994
995         $self->route_pc24($origin, $line, $ref, $_[3]);
996 }
997                 
998 # merge request
999 sub handle_25
1000 {
1001         my $self = shift;
1002         my $pcno = shift;
1003         my $line = shift;
1004         my $origin = shift;
1005         if ($_[1] ne $main::mycall) {
1006                 $self->route($_[1], $line);
1007                 return;
1008         }
1009         if ($_[2] eq $main::mycall) {
1010                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1011                 return;
1012         }
1013
1014         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1015                         
1016         # spots
1017         if ($_[3] > 0) {
1018                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1019                 my $in;
1020                 foreach $in (@in) {
1021                         $self->send(pc26(@{$in}[0..4], $_[2]));
1022                 }
1023         }
1024
1025         # wwv
1026         if ($_[4] > 0) {
1027                 my @in = reverse Geomag::search(0, $_[4], time, 1);
1028                 my $in;
1029                 foreach $in (@in) {
1030                         $self->send(pc27(@{$in}[0..5], $_[2]));
1031                 }
1032         }
1033 }
1034
1035 sub handle_26 {goto &handle_11}
1036 sub handle_27 {goto &handle_23}
1037
1038 # mail/file handling
1039 sub handle_28
1040 {
1041         my $self = shift;
1042         my $pcno = shift;
1043         my $line = shift;
1044         my $origin = shift;
1045         if ($_[1] eq $main::mycall) {
1046                 no strict 'refs';
1047                 my $sub = "DXMsg::handle_$pcno";
1048                 &$sub($self, @_);
1049         } else {
1050                 $self->route($_[1], $line) unless $self->is_clx;
1051         }
1052 }
1053
1054 sub handle_29 {goto &handle_28}
1055 sub handle_30 {goto &handle_28}
1056 sub handle_31 {goto &handle_28}
1057 sub handle_32 {goto &handle_28}
1058 sub handle_33 {goto &handle_28}
1059                 
1060 sub handle_34
1061 {
1062         my $self = shift;
1063         my $pcno = shift;
1064         my $line = shift;
1065         my $origin = shift;
1066         if (eph_dup($line, $eph_pc34_restime)) {
1067                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1068         } else {
1069                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1070         }
1071 }
1072                 
1073 # remote command replies
1074 sub handle_35
1075 {
1076         my $self = shift;
1077         my $pcno = shift;
1078         my $line = shift;
1079         my $origin = shift;
1080         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1081         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1082 }
1083                 
1084 sub handle_36 {goto &handle_34}
1085
1086 # database stuff
1087 sub handle_37
1088 {
1089         my $self = shift;
1090         my $pcno = shift;
1091         my $line = shift;
1092         my $origin = shift;
1093         if ($_[1] eq $main::mycall) {
1094                 no strict 'refs';
1095                 my $sub = "DXDb::handle_$pcno";
1096                 &$sub($self, @_);
1097         } else {
1098                 $self->route($_[1], $line) unless $self->is_clx;
1099         }
1100 }
1101
1102 # node connected list from neighbour
1103 sub handle_38
1104 {
1105         my $self = shift;
1106         my $pcno = shift;
1107         my $line = shift;
1108         my $origin = shift;
1109 }
1110                 
1111 # incoming disconnect
1112 sub handle_39
1113 {
1114         my $self = shift;
1115         my $pcno = shift;
1116         my $line = shift;
1117         my $origin = shift;
1118         if ($_[1] eq $self->{call}) {
1119                 $self->disconnect(1);
1120         } else {
1121                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1122         }
1123 }
1124
1125 sub handle_40 {goto &handle_28}
1126                 
1127 # user info
1128 sub handle_41
1129 {
1130         my $self = shift;
1131         my $pcno = shift;
1132         my $line = shift;
1133         my $origin = shift;
1134         my $call = $_[1];
1135         my $sort = $_[2];
1136         my $val = $_[3];
1137         
1138         my $l = "PC41^$call^$sort";
1139         if (eph_dup($l, $eph_info_restime)) {
1140                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1141                 return;
1142         }
1143                         
1144         # input filter if required
1145         #                       my $ref = Route::get($call) || Route->new($call);
1146         #                       return unless $self->in_filter_route($ref);
1147
1148         if ($val eq $sort || $val =~ /^\s*$/) {
1149                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1150                 return;
1151         }
1152
1153         # add this station to the user database, if required
1154         my $user = DXUser->get_current($call);
1155         $user = DXUser->new($call) unless $user;
1156                         
1157         if ($sort == 1) {
1158                 if (($val =~ /spotter/i || $val =~ /self/i) && $user->name && $user->name ne $val) {
1159                         dbg("PCPROT: invalid name") if isdbg('chanerr');
1160                         if ($main::mycall eq 'GB7DJK' || $main::mycall eq 'GB7BAA' || $main::mycall eq 'WR3D') {
1161                                 DXChannel::broadcast_nodes(pc41($_[1], 1, $user->name)); # send it to everyone including me
1162                         }
1163                         return;
1164                 }
1165                 $user->name($val);
1166         } elsif ($sort == 2) {
1167                 $user->qth($val);
1168         } elsif ($sort == 3) {
1169                 if (is_latlong($val)) {
1170                         my ($lat, $long) = DXBearing::stoll($val);
1171                         $user->lat($lat) if $lat;
1172                         $user->long($long) if $long;
1173                         $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra;
1174                 } else {
1175                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1176                         return;
1177                 }
1178         } elsif ($sort == 4) {
1179                 $user->homenode($val);
1180         } elsif ($sort == 5) {
1181                 if (is_qra(uc $val)) {
1182                         my ($lat, $long) = DXBearing::qratoll(uc $val);
1183                         $user->lat($lat) if $lat && !$user->lat;
1184                         $user->long($long) if $long && !$user->long;
1185                         $user->qra(uc $val);
1186                 } else {
1187                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1188                         return;
1189                 }
1190         }
1191         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1192         $user->put;
1193
1194         unless ($self->{isolate}) {
1195                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1196         }
1197
1198         #  perhaps this IS what we want after all
1199         #                       $self->route_pc41($ref, $call, $sort, $val, $_[4]);
1200 }
1201
1202 sub handle_42 {goto &handle_28}
1203
1204
1205 # database
1206 sub handle_44 {goto &handle_37}
1207 sub handle_45 {goto &handle_37}
1208 sub handle_46 {goto &handle_37}
1209 sub handle_47 {goto &handle_37}
1210 sub handle_48 {goto &handle_37}
1211                 
1212 # message and database
1213 sub handle_49
1214 {
1215         my $self = shift;
1216         my $pcno = shift;
1217         my $line = shift;
1218         my $origin = shift;
1219
1220         if (eph_dup($line)) {
1221                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1222                 return;
1223         }
1224         
1225         if ($_[1] eq $main::mycall) {
1226                 DXMsg::handle_49($self, @_);
1227         } else {
1228                 $self->route($_[1], $line) unless $self->is_clx;
1229         }
1230 }
1231
1232 # keep alive/user list
1233 sub handle_50
1234 {
1235         my $self = shift;
1236         my $pcno = shift;
1237         my $line = shift;
1238         my $origin = shift;
1239
1240         my $call = $_[1];
1241
1242         RouteDB::update($call, $self->{call});
1243
1244         my $node = Route::Node::get($call);
1245         if ($node) {
1246                 return unless $node->call eq $self->{call};
1247                 $node->usercount($_[2]);
1248
1249                 # input filter if required
1250                 return unless $self->in_filter_route($node);
1251
1252                 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1253         }
1254 }
1255                 
1256 # incoming ping requests/answers
1257 sub handle_51
1258 {
1259         my $self = shift;
1260         my $pcno = shift;
1261         my $line = shift;
1262         my $origin = shift;
1263         my $to = $_[1];
1264         my $from = $_[2];
1265         my $flag = $_[3];
1266
1267                         
1268         # is it for us?
1269         if ($to eq $main::mycall) {
1270                 if ($flag == 1) {
1271                         $self->send(pc51($from, $to, '0'));
1272                 } else {
1273                         DXXml::Ping::handle_ping_reply($self, $from);
1274                 }
1275         } else {
1276
1277                 RouteDB::update($from, $self->{call});
1278
1279                 if (eph_dup($line)) {
1280                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1281                         return;
1282                 }
1283                 # route down an appropriate thingy
1284                 $self->route($to, $line);
1285         }
1286 }
1287
1288 # dunno but route it
1289 sub handle_75
1290 {
1291         my $self = shift;
1292         my $pcno = shift;
1293         my $line = shift;
1294         my $origin = shift;
1295         my $call = $_[1];
1296         if ($call ne $main::mycall) {
1297                 $self->route($call, $line);
1298         }
1299 }
1300
1301 # WCY broadcasts
1302 sub handle_73
1303 {
1304         my $self = shift;
1305         my $pcno = shift;
1306         my $line = shift;
1307         my $origin = shift;
1308         my $call = $_[1];
1309                         
1310         # do some de-duping
1311         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1312         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1313                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1314                 return;
1315         }
1316         @_ = map { unpad($_) } @_;
1317         if (WCY::dup($d)) {
1318                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1319                 return;
1320         }
1321                 
1322         my $wcy = WCY::update($d, @_[2..12]);
1323
1324         my $rep;
1325         eval {
1326                 $rep = Local::wcy($self, @_[1..12]);
1327         };
1328         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1329         return if $rep;
1330
1331         # broadcast to the eager world
1332         send_wcy_spot($self, $line, $d, @_[2..12]);
1333 }
1334
1335 # remote commands (incoming)
1336 sub handle_84
1337 {
1338         my $self = shift;
1339         my $pcno = shift;
1340         my $line = shift;
1341         my $origin = shift;
1342         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1343 }
1344
1345 # remote command replies
1346 sub handle_85
1347 {
1348         my $self = shift;
1349         my $pcno = shift;
1350         my $line = shift;
1351         my $origin = shift;
1352         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1353 }
1354
1355 # decode a pc92 call: flag call : version : build
1356 sub _decode_pc92_call
1357 {
1358         my $icall = shift;
1359         my @part = split /:/, $icall;
1360         my ($flag, $call) = unpack "A A*", $part[0];
1361         return () unless defined $flag && $flag ge '0' && $flag le '7';
1362         return () unless $call && is_callsign($call);
1363         my $is_node = $flag & 4;
1364         my $is_extnode = $flag & 2;
1365         my $here = $flag & 1;
1366         return ($call, $is_node, $is_extnode, $here, $part[1], $part[2]);
1367 }
1368
1369 # decode a pc92 call: flag call : version : build
1370 sub _encode_pc92_call
1371 {
1372         my $ref = shift;
1373
1374         # plain call or value
1375         return $ref unless ref $ref;
1376
1377         my $ext = shift;
1378         my $flag = 0;
1379         my $call = $ref->call; 
1380         my $extra = '';
1381         $flag |= $ref->here ? 1 : 0;
1382         if ($ref->isa('Route::Node') || $ref->isa('DXProt')) {
1383                 $flag |= 4;
1384                 my $dxchan = DXChannel::get($call);
1385                 $flag |= 2 if $call ne $main::mycall && $dxchan && !$dxchan->{do_pc9x};
1386                 if ($ext) {
1387                         if ($ref->version) {
1388                                 my $version = $ref->version || 1.0;
1389                                 $version =  $version * 100 + 5300 if $version < 50;
1390                                 $extra .= ":" . $version;
1391                         }
1392                 }
1393         }
1394         return "$flag$call$extra";
1395 }
1396
1397 sub _add_thingy
1398 {
1399         my $parent = shift;
1400         my $s = shift;
1401         my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1402         my @rout;
1403
1404         if ($call) {
1405                 if ($is_node) {
1406                         dbg("ROUTE: added node $call to " . $parent->call) if isdbg('routelow');
1407                         @rout = $parent->add($call, $version, Route::here($here));
1408                 } else {
1409                         dbg("ROUTE: added user $call to " . $parent->call) if isdbg('routelow');
1410                         @rout = $parent->add_user($call, Route::here($here));
1411                 }
1412         }
1413         return @rout;
1414 }
1415
1416 sub _del_thingy
1417 {
1418         my $parent = shift;
1419         my $s = shift;
1420         my ($call, $is_node, $is_extnode, $here, $version, $build) = @$s;
1421         my @rout;
1422         if ($call) {
1423                 if ($is_node) {
1424                         my $nref = Route::Node::get($call);
1425                         dbg("ROUTE: deleting node $call from " . $parent->call) if isdbg('routelow');
1426                         @rout = $nref->del($parent) if $nref;
1427                 } else {
1428                         my $uref = Route::User::get($call);
1429                         dbg("ROUTE: deleting user $call from " . $parent->call) if isdbg('routelow');
1430                         @rout = $parent->del_user($uref) if $uref;
1431                 }
1432         }
1433         return @rout;
1434 }
1435
1436 my $_last_time;
1437 my $_last_occurs;
1438
1439 sub gen_pc9x_t
1440 {
1441         if (!$_last_time || $_last_time != $main::systime) {
1442                 $_last_time = $main::systime;
1443                 $_last_occurs = 0;
1444                 return $_last_time - $main::systime_daystart;
1445         } else {
1446                 $_last_occurs++;
1447                 return sprintf "%d.%02d", $_last_time - $main::systime_daystart, $_last_occurs;
1448         }
1449 }
1450
1451 sub check_pc9x_t
1452 {
1453         my $call = shift;
1454         my $t = shift;
1455         my $pc = shift;
1456         my $create = shift;
1457
1458         my $parent = ref $call ? $call : Route::Node::get($call);
1459         if ($parent) {
1460                 # we only do this for external calls whose routing table
1461                 # record come and go. The reference for mycall is permanent
1462                 # and not that frequently used, it also never times out, so
1463                 # the id on it is completely unreliable. Besides, only commands
1464                 # originating on this box will go through this code...
1465                 if ($parent->call ne $main::mycall) {
1466                         my $lastid = $parent->lastid->{$pc} || 0;
1467                         if ($t < $lastid) {
1468                                 if (my $d = $lastid-86400+$t > $pc9x_past_age) {
1469                                         dbg("PCPROT: $call id $t <= $lastid, ignored") if isdbg('chanerr');
1470                                         return;
1471                                 } 
1472                         }
1473                         if ($lastid == $t) {
1474                                 dbg("PCPROT: dup id on $call = $lastid, ignored") if isdbg('chanerr');
1475                                 return;
1476                         }
1477                 }
1478         } elsif ($create) {
1479                 $parent = Route::Node->new($call);
1480         }
1481         $parent->lastid->{$pc} = $t;
1482
1483         return $parent;
1484 }
1485
1486 # DXSpider routing entries
1487 sub handle_92
1488 {
1489         my $self = shift;
1490         my $pcno = shift;
1491         my $line = shift;
1492         my $origin = shift;
1493
1494         my (@radd, @rdel);
1495         
1496         my $pcall = $_[1];
1497         unless ($pcall) {
1498                 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1499                 return;
1500         }
1501         my $t = $_[2];
1502         my $sort = $_[3];
1503         
1504         if ($pcall eq $main::mycall) {
1505                 dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
1506                 return;
1507         }
1508
1509         if ($pcall eq $self->{call} && $self->{state} eq 'init') {
1510                 $self->state('init92');
1511                 $self->{do_pc9x} = 1;
1512                 dbg("Do pc9x set on $pcall");
1513         } 
1514         unless ($self->{do_pc9x}) {
1515                 dbg("PCPROT: PC9x come in from non-PC9x node, ignored") if isdbg('chanerr');
1516                 return;
1517         }
1518
1519         my $parent = check_pc9x_t($pcall, $t, 92, 1) || return;
1520         my $oparent = $parent;
1521         
1522         $parent->do_pc9x(1);
1523         $parent->via_pc92(1);
1524
1525         if ($sort eq 'F' || $sort eq 'R') {
1526
1527                 # this is the route finding section
1528                 # here is where the consequences of the 'find' command
1529                 # are dealt with
1530
1531                 my $from = $_[4];
1532                 my $target = $_[5];
1533                 
1534                 if ($sort eq 'F') {
1535                         my $flag;
1536                         my $ref;
1537                         my $dxchan;
1538                         if ($ref = DXChannel::get($target)) {
1539                                 $flag = 1;              # we are directly connected
1540                         } else {
1541                                 $ref = Route::get($target);
1542                                 $dxchan = $ref->dxchan;
1543                                 $flag = 2;
1544                         }
1545                         if ($ref && $flag && $dxchan) {
1546                                 $self->send(pc92r($from, $target, $flag, int($dxchan->{pingave}*1000)));
1547                                 return;
1548                         }
1549                 } elsif ($sort eq 'R') {
1550                         if (my $dxchan = DXChannel::get($from)) {
1551                                 handle_pc92_find_reply($dxchan, $pcall, $from, $target, @_[6,7]);
1552                         } else {
1553                                 my $ref = Route::get($from);
1554                                 if ($ref) {
1555                                         my @dxchan = grep {$_->do_pc9x} $ref->alldxchan;
1556                                         if (@dxchan) {
1557                                                 $_->send($line) for @dxchan;
1558                                         } else {
1559                                                 dbg("PCPROT: no return route, ignored") if isdbg('chanerr')
1560                                         }
1561                                 } else {
1562                                         dbg("PCPROT: no return route, ignored") if isdbg('chanerr')
1563                                 }
1564                         }
1565                         return;
1566                 }
1567         } elsif ($sort eq 'A' || $sort eq 'D' || $sort eq 'C') {
1568
1569                 # this is the main route section
1570                 # here is where all the routes are created and destroyed
1571
1572                 my @ent = map {[ _decode_pc92_call($_) ]} grep {$_ && /^[0-7]/} @_[4 .. $#_];
1573                 if (@ent) {
1574
1575                         # look at the first one which will always be a node of some sort
1576                         # and update any information that needs to be done. 
1577                         my ($call, $is_node, $is_extnode, $here, $version, $build) = @{$ent[0]}; 
1578                         if ($call && $is_node) {
1579                                 if ($call eq $main::mycall) {
1580                                         dbg("PCPROT: looped back on node entry, ignored") if isdbg('chanerr');
1581                                         return;
1582                                 }
1583                                 if ($is_extnode) {
1584                                         # this is only accepted from my "self"
1585                                         if (DXChannel::get($call) && $call ne $self->{call}) {
1586                                                 dbg("PCPROT: locally connected node config for $call from other another node $self->{call}, ignored") if isdbg('chanerr');
1587                                                 return;
1588                                         }
1589                                         # reparent to external node (note that we must have received a 'C' or 'A' record
1590                                         # from the true parent node for this external before we get one for the this node
1591                                         unless ($parent = Route::Node::get($call)) {
1592                                                 dbg("PCPROT: no previous C or A for this external node received, ignored") if isdbg('chanerr');
1593                                                 return;
1594                                         }
1595                                         $parent = check_pc9x_t($call, $t, 92) || return;
1596                                         $parent->via_pc92(1);
1597                                 }
1598                         } else {
1599                                 dbg("PCPROT: must be mycall or external node as first entry, ignored") if isdbg('chanerr');
1600                                 return;
1601                         }
1602                         $parent->here(Route::here($here));
1603                         $parent->version($version) if $version && $version > $parent->version;
1604                         $parent->build($build) if $build && $build > $parent->build;
1605                         shift @ent;
1606                 }
1607
1608                 # do a pass through removing any references to either locally connected nodes or mycall
1609                 my @nent;
1610                 for (@ent) {
1611                         next unless $_;
1612                         if ($_->[0] eq $main::mycall || DXChannel::get($_->[0])) {
1613                                 dbg("PCPROT: $_->[0] refers to locally connected node, ignored") if isdbg('chanerr');
1614                                 next;
1615                         } 
1616                         push @nent, $_;
1617                 }
1618
1619                 if ($sort eq 'A') {
1620                         for (@nent) {
1621                                 push @radd, _add_thingy($parent, $_);
1622                         }
1623                 } elsif ($sort eq 'D') {
1624                         for (@nent) {
1625                                 push @rdel, _del_thingy($parent, $_);
1626                         }
1627                 } elsif ($sort eq 'C') {
1628                         my (@nodes, @users);
1629
1630                         # we only reset obscounts on config records
1631                         $oparent->reset_obs;
1632                         dbg("ROUTE: reset obscount on $pcall now " . $oparent->obscount) if isdbg('route');
1633                         if ($oparent != $parent) {
1634                                 $parent->reset_obs;
1635                                 dbg("ROUTE: reset obscount on $parent->{call} now " . $parent->obscount) if isdbg('route');
1636                         }
1637
1638                         # 
1639                         foreach my $r (@nent) {
1640                                 #                       my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_);                     
1641                                 if ($r->[0]) {
1642                                         if ($r->[1]) {
1643                                                 push @nodes, $r->[0];
1644                                         } else {
1645                                                 push @users, $r->[0];
1646                                         }
1647                                 } else {
1648                                         dbg("DXPROT: pc92 call entry '$_' not decoded, ignored") if isdbg('chanerr'); 
1649                                 }
1650                         }
1651
1652                         my ($dnodes, $dusers, $nnodes, $nusers) = $parent->calc_config_changes(\@nodes, \@users);
1653
1654                         # add users here
1655                         foreach my $r (@nent) {
1656                                 my $call = $r->[0];
1657                                 if ($call) {
1658                                         push @radd,_add_thingy($parent, $r) if grep $call eq $_, (@$nnodes, @$nusers);
1659                                 }
1660                         }
1661                         # del users here
1662                         foreach my $r (@$dnodes) {
1663                                 push @rdel,_del_thingy($parent, [$r, 1]);
1664                         }
1665                         foreach my $r (@$dusers) {
1666                                 push @rdel,_del_thingy($parent, [$r, 0]);
1667                         }
1668                 } else {
1669                         dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
1670                         return;
1671                 }
1672
1673                 foreach my $r (@rdel) {
1674                         next unless $r;
1675                 
1676                         $self->route_pc21($pcall, undef, $r) if $r->isa('Route::Node');
1677                         $self->route_pc17($pcall, undef, $parent, $r) if $r->isa('Route::User');
1678                 }
1679                 my @pc19 = grep { $_ && $_->isa('Route::Node') } @radd;
1680                 my @pc16 = grep { $_ && $_->isa('Route::User') } @radd;
1681                 unshift @pc19, $parent if $self->{state} eq 'init92' && $oparent == $parent;
1682                 $self->route_pc19($pcall, undef, @pc19) if @pc19;
1683                 $self->route_pc16($pcall, undef, $parent, @pc16) if @pc16;
1684         }
1685
1686         # broadcast it if we get here
1687         $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1688 }
1689
1690
1691 sub handle_93
1692 {
1693         my $self = shift;
1694         my $pcno = shift;
1695         my $line = shift;
1696         my $origin = shift;
1697
1698 #       $self->{do_pc9x} ||= 1;
1699
1700         my $pcall = $_[1];
1701         unless (is_callsign($pcall)) {
1702                 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1703                 return;
1704         }
1705         my $t = $_[2];
1706         my $parent = check_pc9x_t($pcall, $t, 93, 1) || return;
1707
1708         my $to = $_[3];
1709         my $from = $_[4];
1710         my $via = $_[5];
1711         my $text = $_[6];
1712         my $onode = $_[7];
1713         $onode = $pcall if @_ <= 8;
1714
1715         # will we allow it at all?
1716         if ($censorpc) {
1717                 my @bad;
1718                 if (@bad = BadWords::check($text)) {
1719                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
1720                         return;
1721                 }
1722         }
1723         
1724         # if this is a 'bad spotter' user then ignore it
1725         my $nossid = $from;
1726         $nossid =~ s/-\d+$//;
1727         if ($badspotter->in($nossid)) {
1728                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
1729                 return;
1730         }
1731
1732         if (is_callsign($to)) {
1733                 # local talks 
1734                 my $dxchan;
1735                 $dxchan = DXChannel::get($main::myalias) if $to eq $main::mycall;
1736                 $dxchan = DXChannel::get($to) unless $dxchan;
1737                 if ($dxchan && $dxchan->is_user) {
1738                         $dxchan->talk($from, $to, $via, $text, $onode);
1739                         return;
1740                 }
1741
1742                 # convert to PC10 talks where appropriate
1743                 my $ref = Route::get($to);
1744                 if ($ref) {
1745                         my @dxchan = $ref->alldxchan;
1746                         for $dxchan (@dxchan) {
1747                                 if ($dxchan->{do_pc9x}) {
1748                                         $dxchan->send($line);
1749                                 } else {
1750                                         $dxchan->talk($from, $to, $via, $text, $onode);
1751                                 }
1752                         }
1753                         return;
1754                 }
1755
1756                 # otherwise, drop through and allow it to be broadcast
1757         } elsif ($to eq '*' || uc $to eq 'SYSOP' || uc $to eq 'WX') {
1758                 # announces
1759                 my $sysop = uc $to eq 'SYSOP' ? '*' : ' ';
1760                 my $wx = uc $to eq 'WX' ? '1' : '0';
1761                 my $local = $via eq 'LOCAL' ? '*' : $via;
1762                 
1763                 $self->send_announce(1, pc12($from, $text, $local, $via, $sysop, $wx, $pcall), $from, $local, $text, $sysop, $pcall, $wx, $via eq 'LOCAL' ? $via : undef);
1764                 return if $via eq 'LOCAL';
1765         } else {
1766                 # chat messages to non-pc9x nodes
1767                 $self->send_chat(1, pc12($from, $text, undef, $to, undef, $pcall), $from, '*', $text, $to, $pcall, '0');
1768         }
1769         $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1770 }
1771
1772 # if get here then rebroadcast the thing with its Hop count decremented (if
1773 # there is one). If it has a hop count and it decrements to zero then don't
1774 # rebroadcast it.
1775 #
1776 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1777 #        REBROADCAST!!!!
1778 #
1779
1780 sub handle_default
1781 {
1782         my $self = shift;
1783         my $pcno = shift;
1784         my $line = shift;
1785         my $origin = shift;
1786
1787         if (eph_dup($line)) {
1788                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1789         } else {
1790                 if ($pcno >= 90) {
1791                         my $pcall = $_[1];
1792                         unless (is_callsign($pcall)) {
1793                                 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1794                                 return;
1795                         }
1796                         my $t = $_[2];
1797                         my $parent = check_pc9x_t($pcall, $t, $pcno, 1) || return;
1798                         $self->broadcast_route_pc9x($pcall, undef, $line, 0);
1799                 } else {
1800                         unless ($self->{isolate}) {
1801                                 DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1802                         }
1803                 }
1804         }
1805 }
1806
1807 1;