b8effe395388bde7cb1d84b942ab01a3b2c2f250
[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 Investigate;
36 use RouteDB;
37
38
39 use strict;
40
41 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
42                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
43                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
44                         $investigation_int $pc19_version $myprot_version
45                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
46                         $allowzero $decode_dk0wcy $send_opernam @checklist
47                    );
48         
49 # incoming talk commands
50 sub handle_10
51 {
52         my $self = shift;
53         my $pcno = shift;
54         my $line = shift;
55         my $origin = shift;
56
57         # rsfp check
58         return if $rspfcheck and !$self->rspfcheck(0, $_[6], $_[1]);
59                         
60         # will we allow it at all?
61         if ($censorpc) {
62                 my @bad;
63                 if (@bad = BadWords::check($_[3])) {
64                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
65                         return;
66                 }
67         }
68
69         # is it for me or one of mine?
70         my ($from, $to, $via, $call, $dxchan);
71         $from = $_[1];
72         if ($_[5] gt ' ') {
73                 $via = $_[2];
74                 $to = $_[5];
75         } else {
76                 $to = $_[2];
77         }
78
79         # if this is a 'nodx' node then ignore it
80         if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
81                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
82                 return;
83         }
84
85         # if this is a 'bad spotter' user then ignore it
86         my $nossid = $from;
87         $nossid =~ s/-\d+$//;
88         if ($badspotter->in($nossid)) {
89                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
90                 return;
91         }
92
93         # if we are converting announces to talk is it a dup?
94         if ($ann_to_talk) {
95                 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
96                         dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
97                         return;
98                 }
99         }
100
101         # remember a route to this node and also the node on which this user is
102         RouteDB::update($_[6], $self->{call});
103 #       RouteDB::update($to, $_[6]);
104
105         # it is here and logged on
106         $dxchan = DXChannel::get($main::myalias) if $to eq $main::mycall;
107         $dxchan = DXChannel::get($to) unless $dxchan;
108         if ($dxchan && $dxchan->is_user) {
109                 $_[3] =~ s/\%5E/^/g;
110                 $dxchan->talk($from, $to, $via, $_[3]);
111                 return;
112         }
113
114         # is it elsewhere, visible on the cluster via the to address?
115         # note: this discards the via unless the to address is on
116         # the via address
117         my ($ref, $vref);
118         if ($ref = Route::get($to)) {
119                 $vref = Route::Node::get($via) if $via;
120                 $vref = undef unless $vref && grep $to eq $_, $vref->users;
121                 $ref->dxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
122                 return;
123         }
124
125         # can we see an interface to send it down?
126         
127         # not visible here, send a message of condolence
128         $vref = undef;
129         $ref = Route::get($from);
130         $vref = $ref = Route::Node::get($_[6]) unless $ref; 
131         if ($ref) {
132                 $dxchan = $ref->dxchan;
133                 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
134         }
135 }
136
137 # DX Spot handling
138 sub handle_11
139 {
140         my $self = shift;
141         my $pcno = shift;
142         my $line = shift;
143         my $origin = shift;
144
145         # route 'foreign' pc26s 
146         if ($pcno == 26) {
147                 if ($_[7] ne $main::mycall) {
148                         $self->route($_[7], $line);
149                         return;
150                 }
151         }
152                         
153         # rsfp check
154         #                       return if $rspfcheck and !$self->rspfcheck(1, $_[7], $_[6]);
155         
156         # is the spotted callsign blank? This should really be trapped earlier but it
157         # could break other protocol sentences. Also check for lower case characters.
158         if ($_[2] =~ /^\s*$/) {
159                 dbg("PCPROT: blank callsign, dropped") if isdbg('chanerr');
160                 return;
161         }
162         if ($_[2] =~ /[a-z]/) {
163                 dbg("PCPROT: lowercase characters, dropped") if isdbg('chanerr');
164                 return;
165         }
166
167
168         # if this is a 'nodx' node then ignore it
169         if ($badnode->in($_[7])) {
170                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
171                 return;
172         }
173                         
174         # if this is a 'bad spotter' user then ignore it
175         my $nossid = $_[6];
176         $nossid =~ s/-\d+$//;
177         if ($badspotter->in($nossid)) {
178                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
179                 return;
180         }
181                         
182         # convert the date to a unix date
183         my $d = cltounix($_[3], $_[4]);
184         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
185         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
186                 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
187                 return;
188         }
189
190         # is it 'baddx'
191         if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
192                 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
193                 return;
194         }
195                         
196         # do some de-duping
197         $_[5] =~ s/^\s+//;                      # take any leading blanks off
198         $_[2] = unpad($_[2]);           # take off leading and trailing blanks from spotted callsign
199         if ($_[2] =~ /BUST\w*$/) {
200                 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
201                 return;
202         }
203         if ($censorpc) {
204                 my @bad;
205                 if (@bad = BadWords::check($_[5])) {
206                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
207                         return;
208                 }
209         }
210
211         # remember a route
212 #       RouteDB::update($_[7], $self->{call});
213 #       RouteDB::update($_[6], $_[7]);
214         
215         my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $nossid, $_[7]);
216         # global spot filtering on INPUT
217         if ($self->{inspotsfilter}) {
218                 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
219                 unless ($filter) {
220                         dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
221                         return;
222                 }
223         }
224
225         # this goes after the input filtering, but before the add
226         # so that if it is input filtered, it isn't added to the dup
227         # list. This allows it to come in from a "legitimate" source
228         if (Spot::dup(@spot[0..4,5])) {
229                 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
230                 return;
231         }
232
233         # add it 
234         Spot::add(@spot);
235
236         #
237         # @spot at this point contains:-
238         # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
239         # then  spotted itu, spotted cq, spotters itu, spotters cq
240         # you should be able to route on any of these
241         #
242                         
243         # fix up qra locators of known users 
244         my $user = DXUser->get_current($spot[4]);
245         if ($user) {
246                 my $qra = $user->qra;
247                 unless ($qra && is_qra($qra)) {
248                         my $lat = $user->lat;
249                         my $long = $user->long;
250                         if (defined $lat && defined $long) {
251                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
252                                 $user->put;
253                         }
254                 }
255
256                 # send a remote command to a distant cluster if it is visible and there is no
257                 # qra locator and we havn't done it for a month.
258
259                 unless ($user->qra) {
260                         my $node;
261                         my $to = $user->homenode;
262                         my $last = $user->lastoper || 0;
263                         if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
264                                 my $cmd = "forward/opernam $spot[4]";
265                                 # send the rcmd but we aren't interested in the replies...
266                                 my $dxchan = $node->dxchan;
267                                 if ($dxchan && $dxchan->is_clx) {
268                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
269                                 } else {
270                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
271                                 }
272                                 if ($to ne $_[7]) {
273                                         $to = $_[7];
274                                         $node = Route::Node::get($to);
275                                         if ($node) {
276                                                 $dxchan = $node->dxchan;
277                                                 if ($dxchan && $dxchan->is_clx) {
278                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
279                                                 } else {
280                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
281                                                 }
282                                         }
283                                 }
284                                 $user->lastoper($main::systime);
285                                 $user->put;
286                         }
287                 }
288         }
289                                 
290         # local processing 
291         my $r;
292         eval {
293                 $r = Local::spot($self, @spot);
294         };
295         #                       dbg("Local::spot1 error $@") if isdbg('local') if $@;
296         return if $r;
297
298         # DON'T be silly and send on PC26s!
299         return if $pcno == 26;
300
301         # send out the filtered spots
302         send_dx_spot($self, $line, @spot) if @spot;
303 }
304                 
305 # announces
306 sub handle_12
307 {
308         my $self = shift;
309         my $pcno = shift;
310         my $line = shift;
311         my $origin = shift;
312
313         #                       return if $rspfcheck and !$self->rspfcheck(1, $_[5], $_[1]);
314
315         # announce duplicate checking
316         $_[3] =~ s/^\s+//;                      # remove leading blanks
317
318         if ($censorpc) {
319                 my @bad;
320                 if (@bad = BadWords::check($_[3])) {
321                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
322                         return;
323                 }
324         }
325
326         # if this is a 'nodx' node then ignore it
327         if ($badnode->in($_[5])) {
328                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
329                 return;
330         }
331
332         # if this is a 'bad spotter' user then ignore it
333         my $nossid = $_[1];
334         $nossid =~ s/-\d+$//;
335         if ($badspotter->in($nossid)) {
336                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
337                 return;
338         }
339
340
341         my $dxchan;
342         
343         if ((($dxchan = DXChannel::get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
344                 $self->send_chat($line, @_[1..6]);
345         } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
346
347                 # remember a route
348 #               RouteDB::update($_[5], $self->{call});
349 #               RouteDB::update($_[1], $_[5]);
350
351                 # ignore something that looks like a chat line coming in with sysop
352                 # flag - this is a kludge...
353                 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
354                         dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
355                         return;
356                 }
357
358                 # here's a bit of fun, convert incoming ann with a callsign in the first word
359                 # or one saying 'to <call>' to a talk if we can route to the recipient
360                 if ($ann_to_talk) {
361                         my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
362                         if ($call) {
363                                 my $ref = Route::get($call);
364                                 if ($ref) {
365                                         $dxchan = $ref->dxchan;
366                                         $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
367                                         return;
368                                 }
369                         }
370                 }
371         
372                 # send it
373                 $self->send_announce($line, @_[1..6]);
374         } else {
375                 $self->route($_[2], $line);
376         }
377 }
378                 
379 # incoming user         
380 sub handle_16
381 {
382         my $self = shift;
383         my $pcno = shift;
384         my $line = shift;
385         my $origin = shift;
386
387         # general checks
388         my $dxchan;
389         my $ncall = $_[1];
390         my $newline = "PC16^";
391                         
392         # dos I want users from this channel?
393         unless ($self->user->wantpc16) {
394                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
395                 return;
396         }
397         # is it me?
398         if ($ncall eq $main::mycall) {
399                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
400                 return;
401         }
402
403         my $h;
404         $h = 1 if DXChannel::get($ncall);
405         RouteDB::update($ncall, $self->{call}, $h);
406
407         if (eph_dup($line)) {
408                 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
409                 return;
410         }
411
412         unless ($h) {
413                 dbg("PCPROT: non-local PC16, ignored") if isdbg('chanerr');
414                 return;
415         }
416
417         my $parent = Route::Node::get($ncall); 
418
419         if ($parent) {
420                 $dxchan = $parent->dxchan;
421                 if ($dxchan && $dxchan ne $self) {
422                         dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
423                         return;
424                 }
425
426                 # input filter if required
427                 return unless $self->in_filter_route($parent);
428         }
429
430         my $i;
431         my @rout;
432         for ($i = 2; $i < $#_; $i++) {
433                 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
434                 next unless $call && $conf && defined $here && is_callsign($call);
435                 next if $call eq $main::mycall;
436
437                 eph_del_regex("^PC17\\^$call\\^$ncall");
438                                 
439                 $conf = $conf eq '*';
440
441                 # reject this if we think it is a node already
442                 my $r = Route::Node::get($call);
443                 my $u = DXUser->get_current($call) unless $r;
444                 if ($r || ($u && $u->is_node)) {
445                         dbg("PCPROT: $call is a node") if isdbg('chanerr');
446                         next;
447                 }
448                                 
449                 $r = Route::User::get($call);
450                 my $flags = Route::here($here)|Route::conf($conf);
451                                 
452                 if ($r) {
453                         my $au = $r->addparent($parent);                                        
454                         if ($r->flags != $flags) {
455                                 $r->flags($flags);
456                                 $au = $r;
457                         }
458                         push @rout, $r if $au;
459                 } else {
460                         push @rout, $parent->add_user($call, $flags);
461                 }
462                 
463                 # send info to all logged in thingies
464                 $self->tell_login('loginu', "$ncall: $call") if DXUser->get_current($ncall)->is_local_node;
465                 $self->tell_buddies('loginb', $call, $ncall);
466                                 
467                 # add this station to the user database, if required
468 #               $call =~ s/-\d+$//o;    # remove ssid for users
469                 my $user = DXUser->get_current($call);
470                 $user = DXUser->new($call) if !$user;
471                 $user->homenode($parent->call) if !$user->homenode;
472                 $user->node($parent->call);
473                 $user->lastin($main::systime) unless DXChannel::get($call);
474                 $user->put;
475         }
476         $self->route_pc16($origin, $line, $parent, @rout) if @rout;
477         
478 }
479                 
480 # remove a user
481 sub handle_17
482 {
483         my $self = shift;
484         my $pcno = shift;
485         my $line = shift;
486         my $origin = shift;
487         my $dxchan;
488         my $ncall = $_[2];
489         my $ucall = $_[1];
490
491         eph_del_regex("^PC16\\^$ncall.*$ucall");
492                         
493         # do I want users from this channel?
494         unless ($self->user->wantpc16) {
495                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
496                 return;
497         }
498         if ($ncall eq $main::mycall) {
499                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
500                 return;
501         }
502
503         RouteDB::delete($ncall, $self->{call});
504
505         unless ($ncall eq $self->{call}) {
506                 dbg("PCPROT: PC17 from non-local $ncall, ignored") if isdbg('chanerr');
507                 return;
508         }
509
510         my $uref = Route::User::get($ucall);
511         unless ($uref) {
512                 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
513         }
514         my $parent = Route::Node::get($ncall);
515         unless ($parent) {
516                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
517         }                       
518
519         $dxchan = $parent->dxchan if $parent;
520         if ($dxchan && $dxchan ne $self) {
521                 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
522                 return;
523         }
524
525         # input filter if required and then remove user if present
526         if ($parent) {
527 #               return unless $self->in_filter_route($parent);  
528                 $parent->del_user($uref) if $uref;
529         } else {
530                 $parent = Route->new($ncall);  # throw away
531         }
532
533         # send info to all logged in thingies
534         $self->tell_login('logoutu', "$ncall: $ucall") if DXUser->get_current($ncall)->is_local_node;
535         $self->tell_buddies('logoutb', $ucall, $ncall);
536
537         if (eph_dup($line)) {
538                 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
539                 return;
540         }
541
542         $uref = Route->new($ucall) unless $uref; # throw away
543         $self->route_pc17($origin, $line, $parent, $uref);
544 }
545                 
546 # link request
547 sub handle_18
548 {
549         my $self = shift;
550         my $pcno = shift;
551         my $line = shift;
552         my $origin = shift;
553         $self->state('init');   
554
555         # record the type and version offered
556         if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
557                 $self->version(53 + $1);
558                 $self->user->version(53 + $1);
559                 $self->build(0 + $2);
560                 $self->user->build(0 + $2);
561                 unless ($self->is_spider) {
562                         $self->user->sort('S');
563                         $self->user->put;
564                         $self->sort('S');
565                 }
566                 $self->{handle_xml}++ if DXXml::available() && $_[1] =~ /\bxml\b/;
567                 $self->{do_pc92}++ if $_[1] =~ /\bpc92\b/;
568         } else {
569                 $self->version(50.0);
570                 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
571                 $self->user->version($self->version);
572         }
573
574         # first clear out any nodes on this dxchannel
575         my $parent = Route::Node::get($self->{call});
576         my @rout = $parent->del_nodes;
577         $self->route_pc21($origin, $line, @rout, $parent) if @rout;
578         $self->send_local_config();
579         $self->send(pc20());
580 }
581                 
582 # incoming cluster list
583 sub handle_19
584 {
585         my $self = shift;
586         my $pcno = shift;
587         my $line = shift;
588         my $origin = shift;
589
590         my $i;
591         my $newline = "PC19^";
592
593         # new routing list
594         my @rout;
595
596         # first get the INTERFACE node
597         my $parent = Route::Node::get($self->{call});
598         unless ($parent) {
599                 dbg("DXPROT: my parent $self->{call} has disappeared");
600                 $self->disconnect;
601                 return;
602         }
603
604         # parse the PC19
605         # 
606         # We are making a major change from now on. We are only going to accept
607         # PC19s from directly connected nodes.  This means that we are probably
608         # going to throw away most of the data that we are being sent. 
609         #
610         # The justification for this is that most of it is wrong or out of date
611         # anyway. 
612         # 
613         # From now on we are only going to believe PC92 data.
614         #
615         for ($i = 1; $i < $#_-1; $i += 4) {
616                 my $here = $_[$i];
617                 my $call = uc $_[$i+1];
618                 my $conf = $_[$i+2];
619                 my $ver = $_[$i+3];
620                 next unless defined $here && defined $conf && is_callsign($call);
621
622                 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
623                                 
624                 # check for sane parameters
625                 #                               $ver = 5000 if $ver eq '0000';
626                 next unless $ver && $ver =~ /^\d+$/;
627                 next if $ver < 5000;    # only works with version 5 software
628                 next if length $call < 3; # min 3 letter callsigns
629                 next if $call eq $main::mycall;
630
631                 # check that this PC19 isn't trying to alter the wrong dxchan
632                 my $dxchan = DXChannel::get($call);
633                 if ($dxchan && $dxchan != $self) {
634                         dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
635                         next;
636                 }
637
638                 # add this station to the user database, if required (don't remove SSID from nodes)
639                 my $user = DXUser->get_current($call);
640                 if (!$user) {
641                         $user = DXUser->new($call);
642                         $user->priv(1);         # I have relented and defaulted nodes
643                         $user->lockout(1);
644                         $user->homenode($call);
645                         $user->node($call);
646                 }
647                 $user->sort('A') unless $user->is_node;
648
649 #               if (eph_dup($genline)) {
650 #                       dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
651 #                       next;
652 #               }
653
654                 RouteDB::update($call, $self->{call}, $dxchan ? 1 : undef);
655
656                 unless ($dxchan) {
657                         dbg("PCPROT: PC19 not directly connected, ignored") if isdbg('chanerr');
658                         next;
659                 }
660
661                 my $r = Route::Node::get($call);
662                 my $flags = Route::here($here)|Route::conf($conf);
663
664                 # modify the routing table if it is in it, otherwise store it in the pc19list for now
665                 if ($r) {
666                         my $ar;
667                         if ($call ne $parent->call) {
668                                 if ($self->in_filter_route($r)) {
669                                         $ar = $parent->add($call, $ver, $flags);
670 #                                       push @rout, $ar if $ar;
671                                 } else {
672                                         next;
673                                 }
674                         }
675                         if ($r->version ne $ver || $r->flags != $flags) {
676                                 $r->version($ver);
677                                 $r->flags($flags);
678                         }
679                         push @rout, $r;
680                 } else {
681                         if ($call eq $self->{call} || $user->wantroutepc19) {
682                                 my $new = Route->new($call); # throw away
683                                 if ($self->in_filter_route($new)) {
684                                         my $ar = $parent->add($call, $ver, $flags);
685                                         $user->wantroutepc19(1) unless defined $user->wantroutepc19;
686                                         push @rout, $ar if $ar;
687                                 } else {
688                                         next;
689                                 }
690                         }
691                 }
692
693                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
694                 my $mref = DXMsg::get_busy($call);
695                 $mref->stop_msg($call) if $mref;
696                                 
697                 $user->lastin($main::systime) unless DXChannel::get($call);
698                 $user->put;
699         }
700
701         # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
702         # but remember there will only be one (pair) these because any extras will be
703         # thrown away.
704         if (@rout) {
705                 $self->route_pc21($self->{call}, $line, @rout);
706                 $self->route_pc19($self->{call}, $line, @rout);
707                 $self->route_pc92a($main::mycall, $line, @rout);
708         }
709 }
710                 
711 # send local configuration
712 sub handle_20
713 {
714         my $self = shift;
715         my $pcno = shift;
716         my $line = shift;
717         my $origin = shift;
718         $self->send_local_config();
719         $self->send(pc22());
720         $self->state('normal');
721         $self->{lastping} = 0;
722 }
723                 
724 # delete a cluster from the list
725 #
726 # This should never occur for directly connected nodes.
727 #
728 sub handle_21
729 {
730         my $self = shift;
731         my $pcno = shift;
732         my $line = shift;
733         my $origin = shift;
734         my $call = uc $_[1];
735
736         eph_del_regex("^PC1[679].*$call");
737                         
738         # if I get a PC21 from the same callsign as self then ignore it
739         if ($call eq $self->call) {
740                 dbg("PCPROT: self referencing PC21 from $self->{call}");
741                 return;
742         }
743
744         RouteDB::delete($call, $self->{call});
745
746         my $parent = Route::Node::get($self->{call});
747         unless ($parent) {
748                 dbg("PCPROT: my parent $self->{call} has disappeared");
749                 $self->disconnect;
750                 return;
751         }
752
753         my @rout;
754         
755         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
756                 my $node = Route::Node::get($call);
757                 if ($node) {
758                         
759                         my $dxchan = DXChannel::get($call);
760                         if ($dxchan && $dxchan != $self) {
761                                 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
762                                 return;
763                         }
764                         
765                         # input filter it
766                         return unless $self->in_filter_route($node);
767                         
768                         # routing objects, force a PC21 if it is local
769                         push @rout, $node->del($parent);
770                         push @rout, $call if $dxchan && @rout == 0;
771                 }
772         } else {
773                 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
774                 return;
775         }
776
777         if (@rout) {
778                 $self->route_pc21($origin, $line, @rout);
779                 $self->route_pc92d($main::mycall, $line, @rout);
780         }
781 }
782                 
783
784 sub handle_22
785 {
786         my $self = shift;
787         my $pcno = shift;
788         my $line = shift;
789         my $origin = shift;
790         $self->state('normal');
791         $self->{lastping} = 0;
792 }
793                                 
794 # WWV info
795 sub handle_23
796 {
797         my $self = shift;
798         my $pcno = shift;
799         my $line = shift;
800         my $origin = shift;
801                         
802         # route foreign' pc27s 
803         if ($pcno == 27) {
804                 if ($_[8] ne $main::mycall) {
805                         $self->route($_[8], $line);
806                         return;
807                 }
808         }
809
810         # only do a rspf check on PC23 (not 27)
811         if ($pcno == 23) {
812                 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
813         }
814
815         # do some de-duping
816         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
817         my $sfi = unpad($_[3]);
818         my $k = unpad($_[4]);
819         my $i = unpad($_[5]);
820         my ($r) = $_[6] =~ /R=(\d+)/;
821         $r = 0 unless $r;
822         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
823                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
824                 return;
825         }
826
827         # global wwv filtering on INPUT
828         my @dxcc = ((Prefix::cty_data($_[7]))[0..2], (Prefix::cty_data($_[8]))[0..2]);
829         if ($self->{inwwvfilter}) {
830                 my ($filter, $hops) = $self->{inwwvfilter}->it(@_[7,8], $origin, @dxcc);
831                 unless ($filter) {
832                         dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
833                         return;
834                 }
835         }
836         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
837         if (Geomag::dup($d,$sfi,$k,$i,$_[6],$_[7])) {
838                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
839                 return;
840         }
841                 
842         # note this only takes the first one it gets
843         Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
844
845         my $rep;
846         eval {
847                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
848         };
849         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
850         return if $rep;
851
852         # DON'T be silly and send on PC27s!
853         return if $pcno == 27;
854
855         # broadcast to the eager world
856         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
857 }
858                 
859 # set here status
860 sub handle_24
861 {
862         my $self = shift;
863         my $pcno = shift;
864         my $line = shift;
865         my $origin = shift;
866         my $call = uc $_[1];
867         my ($nref, $uref);
868         $nref = Route::Node::get($call);
869         $uref = Route::User::get($call);
870         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
871                         
872         if (eph_dup($line)) {
873                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
874                 return;
875         }
876         
877         $nref->here($_[2]) if $nref;
878         $uref->here($_[2]) if $uref;
879         my $ref = $nref || $uref;
880         return unless $self->in_filter_route($ref);
881
882         $self->route_pc24($origin, $line, $ref, $_[3]);
883 }
884                 
885 # merge request
886 sub handle_25
887 {
888         my $self = shift;
889         my $pcno = shift;
890         my $line = shift;
891         my $origin = shift;
892         if ($_[1] ne $main::mycall) {
893                 $self->route($_[1], $line);
894                 return;
895         }
896         if ($_[2] eq $main::mycall) {
897                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
898                 return;
899         }
900
901         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
902                         
903         # spots
904         if ($_[3] > 0) {
905                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
906                 my $in;
907                 foreach $in (@in) {
908                         $self->send(pc26(@{$in}[0..4], $_[2]));
909                 }
910         }
911
912         # wwv
913         if ($_[4] > 0) {
914                 my @in = reverse Geomag::search(0, $_[4], time, 1);
915                 my $in;
916                 foreach $in (@in) {
917                         $self->send(pc27(@{$in}[0..5], $_[2]));
918                 }
919         }
920 }
921
922 sub handle_26 {goto &handle_11}
923 sub handle_27 {goto &handle_23}
924
925 # mail/file handling
926 sub handle_28
927 {
928         my $self = shift;
929         my $pcno = shift;
930         my $line = shift;
931         my $origin = shift;
932         if ($_[1] eq $main::mycall) {
933                 no strict 'refs';
934                 my $sub = "DXMsg::handle_$pcno";
935                 &$sub($self, @_);
936         } else {
937                 $self->route($_[1], $line) unless $self->is_clx;
938         }
939 }
940
941 sub handle_29 {goto &handle_28}
942 sub handle_30 {goto &handle_28}
943 sub handle_31 {goto &handle_28}
944 sub handle_32 {goto &handle_28}
945 sub handle_33 {goto &handle_28}
946                 
947 sub handle_34
948 {
949         my $self = shift;
950         my $pcno = shift;
951         my $line = shift;
952         my $origin = shift;
953         if (eph_dup($line, $eph_pc34_restime)) {
954                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
955         } else {
956                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
957         }
958 }
959                 
960 # remote command replies
961 sub handle_35
962 {
963         my $self = shift;
964         my $pcno = shift;
965         my $line = shift;
966         my $origin = shift;
967         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
968         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
969 }
970                 
971 sub handle_36 {goto &handle_34}
972
973 # database stuff
974 sub handle_37
975 {
976         my $self = shift;
977         my $pcno = shift;
978         my $line = shift;
979         my $origin = shift;
980         if ($_[1] eq $main::mycall) {
981                 no strict 'refs';
982                 my $sub = "DXDb::handle_$pcno";
983                 &$sub($self, @_);
984         } else {
985                 $self->route($_[1], $line) unless $self->is_clx;
986         }
987 }
988
989 # node connected list from neighbour
990 sub handle_38
991 {
992         my $self = shift;
993         my $pcno = shift;
994         my $line = shift;
995         my $origin = shift;
996 }
997                 
998 # incoming disconnect
999 sub handle_39
1000 {
1001         my $self = shift;
1002         my $pcno = shift;
1003         my $line = shift;
1004         my $origin = shift;
1005         if ($_[1] eq $self->{call}) {
1006                 $self->disconnect(1);
1007         } else {
1008                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1009         }
1010 }
1011
1012 sub handle_40 {goto &handle_28}
1013                 
1014 # user info
1015 sub handle_41
1016 {
1017         my $self = shift;
1018         my $pcno = shift;
1019         my $line = shift;
1020         my $origin = shift;
1021         my $call = $_[1];
1022
1023         my $l = $line;
1024         $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1025         if (eph_dup($l, $eph_info_restime)) {
1026                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1027                 return;
1028         }
1029                         
1030         # input filter if required
1031         #                       my $ref = Route::get($call) || Route->new($call);
1032         #                       return unless $self->in_filter_route($ref);
1033
1034         if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1035                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1036                 return;
1037         }
1038
1039         # add this station to the user database, if required
1040         my $user = DXUser->get_current($call);
1041         $user = DXUser->new($call) unless $user;
1042                         
1043         if ($_[2] == 1) {
1044                 $user->name($_[3]);
1045         } elsif ($_[2] == 2) {
1046                 $user->qth($_[3]);
1047         } elsif ($_[2] == 3) {
1048                 if (is_latlong($_[3])) {
1049                         my ($lat, $long) = DXBearing::stoll($_[3]);
1050                         $user->lat($lat);
1051                         $user->long($long);
1052                         $user->qra(DXBearing::lltoqra($lat, $long));
1053                 } else {
1054                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1055                         return;
1056                 }
1057         } elsif ($_[2] == 4) {
1058                 $user->homenode($_[3]);
1059         } elsif ($_[2] == 5) {
1060                 if (is_qra(uc $_[3])) {
1061                         my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1062                         $user->lat($lat);
1063                         $user->long($long);
1064                         $user->qra(uc $_[3]);
1065                 } else {
1066                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1067                         return;
1068                 }
1069         }
1070         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1071         $user->put;
1072
1073         unless ($self->{isolate}) {
1074                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1075         }
1076
1077         #  perhaps this IS what we want after all
1078         #                       $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1079 }
1080
1081 sub handle_42 {goto &handle_28}
1082
1083
1084 # database
1085 sub handle_44 {goto &handle_37}
1086 sub handle_45 {goto &handle_37}
1087 sub handle_46 {goto &handle_37}
1088 sub handle_47 {goto &handle_37}
1089 sub handle_48 {goto &handle_37}
1090                 
1091 # message and database
1092 sub handle_49
1093 {
1094         my $self = shift;
1095         my $pcno = shift;
1096         my $line = shift;
1097         my $origin = shift;
1098
1099         if (eph_dup($line)) {
1100                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1101                 return;
1102         }
1103         
1104         if ($_[1] eq $main::mycall) {
1105                 DXMsg::handle_49($self, @_);
1106         } else {
1107                 $self->route($_[1], $line) unless $self->is_clx;
1108         }
1109 }
1110
1111 # keep alive/user list
1112 sub handle_50
1113 {
1114         my $self = shift;
1115         my $pcno = shift;
1116         my $line = shift;
1117         my $origin = shift;
1118
1119         my $call = $_[1];
1120
1121         RouteDB::update($call, $self->{call});
1122
1123         my $node = Route::Node::get($call);
1124         if ($node) {
1125                 return unless $node->call eq $self->{call};
1126                 $node->usercount($_[2]);
1127
1128                 # input filter if required
1129                 return unless $self->in_filter_route($node);
1130
1131                 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1132         }
1133 }
1134                 
1135 # incoming ping requests/answers
1136 sub handle_51
1137 {
1138         my $self = shift;
1139         my $pcno = shift;
1140         my $line = shift;
1141         my $origin = shift;
1142         my $to = $_[1];
1143         my $from = $_[2];
1144         my $flag = $_[3];
1145
1146                         
1147         # is it for us?
1148         if ($to eq $main::mycall) {
1149                 if ($flag == 1) {
1150                         $self->send(pc51($from, $to, '0'));
1151                 } else {
1152                         DXXml::Ping::handle_ping_reply($self, $from);
1153                 }
1154         } else {
1155
1156                 RouteDB::update($from, $self->{call});
1157
1158                 if (eph_dup($line)) {
1159                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1160                         return;
1161                 }
1162                 # route down an appropriate thingy
1163                 $self->route($to, $line);
1164         }
1165 }
1166
1167 # dunno but route it
1168 sub handle_75
1169 {
1170         my $self = shift;
1171         my $pcno = shift;
1172         my $line = shift;
1173         my $origin = shift;
1174         my $call = $_[1];
1175         if ($call ne $main::mycall) {
1176                 $self->route($call, $line);
1177         }
1178 }
1179
1180 # WCY broadcasts
1181 sub handle_73
1182 {
1183         my $self = shift;
1184         my $pcno = shift;
1185         my $line = shift;
1186         my $origin = shift;
1187         my $call = $_[1];
1188                         
1189         # do some de-duping
1190         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1191         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1192                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1193                 return;
1194         }
1195         @_ = map { unpad($_) } @_;
1196         if (WCY::dup($d)) {
1197                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1198                 return;
1199         }
1200                 
1201         my $wcy = WCY::update($d, @_[2..12]);
1202
1203         my $rep;
1204         eval {
1205                 $rep = Local::wcy($self, @_[1..12]);
1206         };
1207         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1208         return if $rep;
1209
1210         # broadcast to the eager world
1211         send_wcy_spot($self, $line, $d, @_[2..12]);
1212 }
1213
1214 # remote commands (incoming)
1215 sub handle_84
1216 {
1217         my $self = shift;
1218         my $pcno = shift;
1219         my $line = shift;
1220         my $origin = shift;
1221         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1222 }
1223
1224 # remote command replies
1225 sub handle_85
1226 {
1227         my $self = shift;
1228         my $pcno = shift;
1229         my $line = shift;
1230         my $origin = shift;
1231         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1232 }
1233
1234 # decode a pc92 call: flag call : version : build
1235 sub _decode_pc92_call
1236 {
1237         my $icall = shift;
1238         my @part = split /:/, $icall;
1239         my ($flag, $call) = unpack "A A*", $part[0];
1240         return () unless $flag && $flag ge '0' && $flag le '7';
1241         return () unless $call && is_callsign($call);
1242         my $is_node = $flag & 4;
1243         my $is_extnode = $flag & 2;
1244         my $here = $flag & 1;
1245         return ($call, $is_node, $is_extnode, $here, $part[1], $part[2]);
1246 }
1247
1248 # decode a pc92 call: flag call : version : build
1249 sub _encode_pc92_call
1250 {
1251         my $ref = shift;
1252         my $ext = shift;
1253         my $flag = 0;
1254         my $call = $ref->call; 
1255         my $extra = '';
1256         $flag |= $ref->here ? 1 : 0;
1257         if ($ext && ($ref->isa('Route::Node') || $ref->isa('DXProt'))) {
1258                 $flag |= 4;
1259 #               if ($ref->version || $ref->build) {
1260                 if ($ref->version) {
1261                         my $version = $ref->version || 1.0;
1262                         $version = $version * 100 + 5300;
1263                         $extra .= ":" . $version;
1264 #                       $extra .= (":" . ($ref->build ? $ref->build : '')) if $ref->build;
1265                 }
1266         }
1267         return "$flag$call$extra";
1268 }
1269
1270 # DXSpider routing entries
1271 sub handle_92
1272 {
1273         my $self = shift;
1274         my $pcno = shift;
1275         my $line = shift;
1276         my $origin = shift;
1277
1278         $self->{do_pc92} ||= 1;
1279
1280         my ($ncall, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_[1]);
1281         unless ($ncall) {
1282                 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1283                 return;
1284         }
1285         my $t = $_[2];
1286         my $sort = $_[3];
1287         
1288         if ($ncall eq $main::mycall) {
1289                 dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
1290                 return;
1291         }
1292
1293         my $nref = Route::Node::get($ncall);
1294         if ($nref) {
1295                 my $lastid = $nref->lastid->{92} || 0;
1296                 if ($lastid > $t) {
1297                         dbg("PCPROT: dup / old id <= $lastid, ignored") if isdbg('chanerr');
1298                         return;
1299                 }
1300                 $nref->flags(Route::flags($here));
1301         } else {
1302                 $nref = Route::Node->new($ncall, $version, Route::here($here));
1303         }
1304         $nref->lastid->{92} = $t;
1305
1306         if ($sort eq 'A') {
1307                 
1308         } elsif ($sort eq 'D') {
1309
1310         } elsif ($sort eq 'C') {
1311
1312         } else {
1313                 dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
1314         }
1315 }
1316
1317 # if get here then rebroadcast the thing with its Hop count decremented (if
1318 # there is one). If it has a hop count and it decrements to zero then don't
1319 # rebroadcast it.
1320 #
1321 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1322 #        REBROADCAST!!!!
1323 #
1324
1325 sub handle_default
1326 {
1327         my $self = shift;
1328         my $pcno = shift;
1329         my $line = shift;
1330         my $origin = shift;
1331
1332         if (eph_dup($line)) {
1333                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1334         } else {
1335                 unless ($self->{isolate}) {
1336                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1337                 }
1338         }
1339 }