5eb35f6b768dcee4aa5f378e9de29888a04b69e2
[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 sub check_add_node
583 {
584         my $call = shift;
585         
586         # add this station to the user database, if required (don't remove SSID from nodes)
587         my $user = DXUser->get_current($call);
588         if (!$user) {
589                 $user = DXUser->new($call);
590                 $user->priv(1);         # I have relented and defaulted nodes
591                 $user->lockout(1);
592                 $user->homenode($call);
593                 $user->node($call);
594         }
595         $user->sort('A') unless $user->is_node;
596         return $user;
597 }
598
599 # incoming cluster list
600 sub handle_19
601 {
602         my $self = shift;
603         my $pcno = shift;
604         my $line = shift;
605         my $origin = shift;
606
607         my $i;
608         my $newline = "PC19^";
609
610         # new routing list
611         my @rout;
612
613         # first get the INTERFACE node
614         my $parent = Route::Node::get($self->{call});
615         unless ($parent) {
616                 dbg("DXPROT: my parent $self->{call} has disappeared");
617                 $self->disconnect;
618                 return;
619         }
620
621         # parse the PC19
622         # 
623         # We are making a major change from now on. We are only going to accept
624         # PC19s from directly connected nodes.  This means that we are probably
625         # going to throw away most of the data that we are being sent. 
626         #
627         # The justification for this is that most of it is wrong or out of date
628         # anyway. 
629         # 
630         # From now on we are only going to believe PC92 data.
631         #
632         for ($i = 1; $i < $#_-1; $i += 4) {
633                 my $here = $_[$i];
634                 my $call = uc $_[$i+1];
635                 my $conf = $_[$i+2];
636                 my $ver = $_[$i+3];
637                 next unless defined $here && defined $conf && is_callsign($call);
638
639                 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
640                                 
641                 # check for sane parameters
642                 #                               $ver = 5000 if $ver eq '0000';
643                 next unless $ver && $ver =~ /^\d+$/;
644                 next if $ver < 5000;    # only works with version 5 software
645                 next if length $call < 3; # min 3 letter callsigns
646                 next if $call eq $main::mycall;
647
648                 # check that this PC19 isn't trying to alter the wrong dxchan
649                 my $dxchan = DXChannel::get($call);
650                 if ($dxchan && $dxchan != $self) {
651                         dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
652                         next;
653                 }
654
655                 my $user = check_add_node($call);
656                 
657 #               if (eph_dup($genline)) {
658 #                       dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
659 #                       next;
660 #               }
661
662                 RouteDB::update($call, $self->{call}, $dxchan ? 1 : undef);
663
664                 unless ($dxchan) {
665                         dbg("PCPROT: PC19 not directly connected, ignored") if isdbg('chanerr');
666                         next;
667                 }
668
669                 my $r = Route::Node::get($call);
670                 my $flags = Route::here($here)|Route::conf($conf);
671
672                 # modify the routing table if it is in it, otherwise store it in the pc19list for now
673                 if ($r) {
674                         my $ar;
675                         if ($call ne $parent->call) {
676                                 if ($self->in_filter_route($r)) {
677                                         $ar = $parent->add($call, $ver, $flags);
678 #                                       push @rout, $ar if $ar;
679                                 } else {
680                                         next;
681                                 }
682                         }
683                         if ($r->version ne $ver || $r->flags != $flags) {
684                                 $r->version($ver);
685                                 $r->flags($flags);
686                         }
687                         push @rout, $r;
688                 } else {
689                         if ($call eq $self->{call} || $user->wantroutepc19) {
690                                 my $new = Route->new($call); # throw away
691                                 if ($self->in_filter_route($new)) {
692                                         my $ar = $parent->add($call, $ver, $flags);
693                                         $user->wantroutepc19(1) unless defined $user->wantroutepc19;
694                                         push @rout, $ar if $ar;
695                                 } else {
696                                         next;
697                                 }
698                         }
699                 }
700
701                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
702                 my $mref = DXMsg::get_busy($call);
703                 $mref->stop_msg($call) if $mref;
704                                 
705                 $user->lastin($main::systime) unless DXChannel::get($call);
706                 $user->put;
707         }
708
709         # we are not automatically sending out PC19s, we send out a composite PC21,PC19 instead
710         # but remember there will only be one (pair) these because any extras will be
711         # thrown away.
712         if (@rout) {
713                 $self->route_pc21($self->{call}, $line, @rout);
714                 $self->route_pc19($self->{call}, $line, @rout);
715                 $self->route_pc92a($main::mycall, $line, @rout);
716         }
717 }
718                 
719 # send local configuration
720 sub handle_20
721 {
722         my $self = shift;
723         my $pcno = shift;
724         my $line = shift;
725         my $origin = shift;
726         $self->send_local_config();
727         $self->send(pc22());
728         $self->state('normal');
729         $self->{lastping} = 0;
730 }
731                 
732 # delete a cluster from the list
733 #
734 # This should never occur for directly connected nodes.
735 #
736 sub handle_21
737 {
738         my $self = shift;
739         my $pcno = shift;
740         my $line = shift;
741         my $origin = shift;
742         my $call = uc $_[1];
743
744         eph_del_regex("^PC1[679].*$call");
745                         
746         # if I get a PC21 from the same callsign as self then ignore it
747         if ($call eq $self->call) {
748                 dbg("PCPROT: self referencing PC21 from $self->{call}");
749                 return;
750         }
751
752         RouteDB::delete($call, $self->{call});
753
754         my $parent = Route::Node::get($self->{call});
755         unless ($parent) {
756                 dbg("PCPROT: my parent $self->{call} has disappeared");
757                 $self->disconnect;
758                 return;
759         }
760
761         my @rout;
762         
763         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
764                 my $node = Route::Node::get($call);
765                 if ($node) {
766                         
767                         my $dxchan = DXChannel::get($call);
768                         if ($dxchan && $dxchan != $self) {
769                                 dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
770                                 return;
771                         }
772                         
773                         # input filter it
774                         return unless $self->in_filter_route($node);
775                         
776                         # routing objects, force a PC21 if it is local
777                         push @rout, $node->del($parent);
778                         push @rout, $call if $dxchan && @rout == 0;
779                 }
780         } else {
781                 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
782                 return;
783         }
784
785         if (@rout) {
786                 $self->route_pc21($origin, $line, @rout);
787                 $self->route_pc92d($main::mycall, $line, @rout);
788         }
789 }
790                 
791
792 sub handle_22
793 {
794         my $self = shift;
795         my $pcno = shift;
796         my $line = shift;
797         my $origin = shift;
798         $self->state('normal');
799         $self->{lastping} = 0;
800 }
801                                 
802 # WWV info
803 sub handle_23
804 {
805         my $self = shift;
806         my $pcno = shift;
807         my $line = shift;
808         my $origin = shift;
809                         
810         # route foreign' pc27s 
811         if ($pcno == 27) {
812                 if ($_[8] ne $main::mycall) {
813                         $self->route($_[8], $line);
814                         return;
815                 }
816         }
817
818         # only do a rspf check on PC23 (not 27)
819         if ($pcno == 23) {
820                 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
821         }
822
823         # do some de-duping
824         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
825         my $sfi = unpad($_[3]);
826         my $k = unpad($_[4]);
827         my $i = unpad($_[5]);
828         my ($r) = $_[6] =~ /R=(\d+)/;
829         $r = 0 unless $r;
830         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
831                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
832                 return;
833         }
834
835         # global wwv filtering on INPUT
836         my @dxcc = ((Prefix::cty_data($_[7]))[0..2], (Prefix::cty_data($_[8]))[0..2]);
837         if ($self->{inwwvfilter}) {
838                 my ($filter, $hops) = $self->{inwwvfilter}->it(@_[7,8], $origin, @dxcc);
839                 unless ($filter) {
840                         dbg("PCPROT: Rejected by input wwv filter") if isdbg('chanerr');
841                         return;
842                 }
843         }
844         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
845         if (Geomag::dup($d,$sfi,$k,$i,$_[6],$_[7])) {
846                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
847                 return;
848         }
849                 
850         # note this only takes the first one it gets
851         Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
852
853         my $rep;
854         eval {
855                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
856         };
857         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
858         return if $rep;
859
860         # DON'T be silly and send on PC27s!
861         return if $pcno == 27;
862
863         # broadcast to the eager world
864         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
865 }
866                 
867 # set here status
868 sub handle_24
869 {
870         my $self = shift;
871         my $pcno = shift;
872         my $line = shift;
873         my $origin = shift;
874         my $call = uc $_[1];
875         my ($nref, $uref);
876         $nref = Route::Node::get($call);
877         $uref = Route::User::get($call);
878         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
879                         
880         if (eph_dup($line)) {
881                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
882                 return;
883         }
884         
885         $nref->here($_[2]) if $nref;
886         $uref->here($_[2]) if $uref;
887         my $ref = $nref || $uref;
888         return unless $self->in_filter_route($ref);
889
890         $self->route_pc24($origin, $line, $ref, $_[3]);
891 }
892                 
893 # merge request
894 sub handle_25
895 {
896         my $self = shift;
897         my $pcno = shift;
898         my $line = shift;
899         my $origin = shift;
900         if ($_[1] ne $main::mycall) {
901                 $self->route($_[1], $line);
902                 return;
903         }
904         if ($_[2] eq $main::mycall) {
905                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
906                 return;
907         }
908
909         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
910                         
911         # spots
912         if ($_[3] > 0) {
913                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
914                 my $in;
915                 foreach $in (@in) {
916                         $self->send(pc26(@{$in}[0..4], $_[2]));
917                 }
918         }
919
920         # wwv
921         if ($_[4] > 0) {
922                 my @in = reverse Geomag::search(0, $_[4], time, 1);
923                 my $in;
924                 foreach $in (@in) {
925                         $self->send(pc27(@{$in}[0..5], $_[2]));
926                 }
927         }
928 }
929
930 sub handle_26 {goto &handle_11}
931 sub handle_27 {goto &handle_23}
932
933 # mail/file handling
934 sub handle_28
935 {
936         my $self = shift;
937         my $pcno = shift;
938         my $line = shift;
939         my $origin = shift;
940         if ($_[1] eq $main::mycall) {
941                 no strict 'refs';
942                 my $sub = "DXMsg::handle_$pcno";
943                 &$sub($self, @_);
944         } else {
945                 $self->route($_[1], $line) unless $self->is_clx;
946         }
947 }
948
949 sub handle_29 {goto &handle_28}
950 sub handle_30 {goto &handle_28}
951 sub handle_31 {goto &handle_28}
952 sub handle_32 {goto &handle_28}
953 sub handle_33 {goto &handle_28}
954                 
955 sub handle_34
956 {
957         my $self = shift;
958         my $pcno = shift;
959         my $line = shift;
960         my $origin = shift;
961         if (eph_dup($line, $eph_pc34_restime)) {
962                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
963         } else {
964                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
965         }
966 }
967                 
968 # remote command replies
969 sub handle_35
970 {
971         my $self = shift;
972         my $pcno = shift;
973         my $line = shift;
974         my $origin = shift;
975         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
976         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
977 }
978                 
979 sub handle_36 {goto &handle_34}
980
981 # database stuff
982 sub handle_37
983 {
984         my $self = shift;
985         my $pcno = shift;
986         my $line = shift;
987         my $origin = shift;
988         if ($_[1] eq $main::mycall) {
989                 no strict 'refs';
990                 my $sub = "DXDb::handle_$pcno";
991                 &$sub($self, @_);
992         } else {
993                 $self->route($_[1], $line) unless $self->is_clx;
994         }
995 }
996
997 # node connected list from neighbour
998 sub handle_38
999 {
1000         my $self = shift;
1001         my $pcno = shift;
1002         my $line = shift;
1003         my $origin = shift;
1004 }
1005                 
1006 # incoming disconnect
1007 sub handle_39
1008 {
1009         my $self = shift;
1010         my $pcno = shift;
1011         my $line = shift;
1012         my $origin = shift;
1013         if ($_[1] eq $self->{call}) {
1014                 $self->disconnect(1);
1015         } else {
1016                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1017         }
1018 }
1019
1020 sub handle_40 {goto &handle_28}
1021                 
1022 # user info
1023 sub handle_41
1024 {
1025         my $self = shift;
1026         my $pcno = shift;
1027         my $line = shift;
1028         my $origin = shift;
1029         my $call = $_[1];
1030
1031         my $l = $line;
1032         $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1033         if (eph_dup($l, $eph_info_restime)) {
1034                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1035                 return;
1036         }
1037                         
1038         # input filter if required
1039         #                       my $ref = Route::get($call) || Route->new($call);
1040         #                       return unless $self->in_filter_route($ref);
1041
1042         if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1043                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1044                 return;
1045         }
1046
1047         # add this station to the user database, if required
1048         my $user = DXUser->get_current($call);
1049         $user = DXUser->new($call) unless $user;
1050                         
1051         if ($_[2] == 1) {
1052                 $user->name($_[3]);
1053         } elsif ($_[2] == 2) {
1054                 $user->qth($_[3]);
1055         } elsif ($_[2] == 3) {
1056                 if (is_latlong($_[3])) {
1057                         my ($lat, $long) = DXBearing::stoll($_[3]);
1058                         $user->lat($lat);
1059                         $user->long($long);
1060                         $user->qra(DXBearing::lltoqra($lat, $long));
1061                 } else {
1062                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1063                         return;
1064                 }
1065         } elsif ($_[2] == 4) {
1066                 $user->homenode($_[3]);
1067         } elsif ($_[2] == 5) {
1068                 if (is_qra(uc $_[3])) {
1069                         my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1070                         $user->lat($lat);
1071                         $user->long($long);
1072                         $user->qra(uc $_[3]);
1073                 } else {
1074                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1075                         return;
1076                 }
1077         }
1078         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1079         $user->put;
1080
1081         unless ($self->{isolate}) {
1082                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1083         }
1084
1085         #  perhaps this IS what we want after all
1086         #                       $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1087 }
1088
1089 sub handle_42 {goto &handle_28}
1090
1091
1092 # database
1093 sub handle_44 {goto &handle_37}
1094 sub handle_45 {goto &handle_37}
1095 sub handle_46 {goto &handle_37}
1096 sub handle_47 {goto &handle_37}
1097 sub handle_48 {goto &handle_37}
1098                 
1099 # message and database
1100 sub handle_49
1101 {
1102         my $self = shift;
1103         my $pcno = shift;
1104         my $line = shift;
1105         my $origin = shift;
1106
1107         if (eph_dup($line)) {
1108                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1109                 return;
1110         }
1111         
1112         if ($_[1] eq $main::mycall) {
1113                 DXMsg::handle_49($self, @_);
1114         } else {
1115                 $self->route($_[1], $line) unless $self->is_clx;
1116         }
1117 }
1118
1119 # keep alive/user list
1120 sub handle_50
1121 {
1122         my $self = shift;
1123         my $pcno = shift;
1124         my $line = shift;
1125         my $origin = shift;
1126
1127         my $call = $_[1];
1128
1129         RouteDB::update($call, $self->{call});
1130
1131         my $node = Route::Node::get($call);
1132         if ($node) {
1133                 return unless $node->call eq $self->{call};
1134                 $node->usercount($_[2]);
1135
1136                 # input filter if required
1137                 return unless $self->in_filter_route($node);
1138
1139                 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1140         }
1141 }
1142                 
1143 # incoming ping requests/answers
1144 sub handle_51
1145 {
1146         my $self = shift;
1147         my $pcno = shift;
1148         my $line = shift;
1149         my $origin = shift;
1150         my $to = $_[1];
1151         my $from = $_[2];
1152         my $flag = $_[3];
1153
1154                         
1155         # is it for us?
1156         if ($to eq $main::mycall) {
1157                 if ($flag == 1) {
1158                         $self->send(pc51($from, $to, '0'));
1159                 } else {
1160                         DXXml::Ping::handle_ping_reply($self, $from);
1161                 }
1162         } else {
1163
1164                 RouteDB::update($from, $self->{call});
1165
1166                 if (eph_dup($line)) {
1167                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1168                         return;
1169                 }
1170                 # route down an appropriate thingy
1171                 $self->route($to, $line);
1172         }
1173 }
1174
1175 # dunno but route it
1176 sub handle_75
1177 {
1178         my $self = shift;
1179         my $pcno = shift;
1180         my $line = shift;
1181         my $origin = shift;
1182         my $call = $_[1];
1183         if ($call ne $main::mycall) {
1184                 $self->route($call, $line);
1185         }
1186 }
1187
1188 # WCY broadcasts
1189 sub handle_73
1190 {
1191         my $self = shift;
1192         my $pcno = shift;
1193         my $line = shift;
1194         my $origin = shift;
1195         my $call = $_[1];
1196                         
1197         # do some de-duping
1198         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1199         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1200                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1201                 return;
1202         }
1203         @_ = map { unpad($_) } @_;
1204         if (WCY::dup($d)) {
1205                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1206                 return;
1207         }
1208                 
1209         my $wcy = WCY::update($d, @_[2..12]);
1210
1211         my $rep;
1212         eval {
1213                 $rep = Local::wcy($self, @_[1..12]);
1214         };
1215         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1216         return if $rep;
1217
1218         # broadcast to the eager world
1219         send_wcy_spot($self, $line, $d, @_[2..12]);
1220 }
1221
1222 # remote commands (incoming)
1223 sub handle_84
1224 {
1225         my $self = shift;
1226         my $pcno = shift;
1227         my $line = shift;
1228         my $origin = shift;
1229         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1230 }
1231
1232 # remote command replies
1233 sub handle_85
1234 {
1235         my $self = shift;
1236         my $pcno = shift;
1237         my $line = shift;
1238         my $origin = shift;
1239         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1240 }
1241
1242 # decode a pc92 call: flag call : version : build
1243 sub _decode_pc92_call
1244 {
1245         my $icall = shift;
1246         my @part = split /:/, $icall;
1247         my ($flag, $call) = unpack "A A*", $part[0];
1248         return () unless $flag && $flag ge '0' && $flag le '7';
1249         return () unless $call && is_callsign($call);
1250         my $is_node = $flag & 4;
1251         my $is_extnode = $flag & 2;
1252         my $here = $flag & 1;
1253         return ($call, $is_node, $is_extnode, $here, $part[1], $part[2]);
1254 }
1255
1256 # decode a pc92 call: flag call : version : build
1257 sub _encode_pc92_call
1258 {
1259         my $ref = shift;
1260         my $ext = shift;
1261         my $flag = 0;
1262         my $call = $ref->call; 
1263         my $extra = '';
1264         $flag |= $ref->here ? 1 : 0;
1265         if ($ext && ($ref->isa('Route::Node') || $ref->isa('DXProt'))) {
1266                 $flag |= 4;
1267 #               if ($ref->version || $ref->build) {
1268                 if ($ref->version) {
1269                         my $version = $ref->version || 1.0;
1270                         $version =  $version * 100 + 5300 if $version < 50;
1271                         $extra .= ":" . $version;
1272 #                       $extra .= (":" . ($ref->build ? $ref->build : '')) if $ref->build;
1273                 }
1274         }
1275         return "$flag$call$extra";
1276 }
1277
1278 sub _add_thingy
1279 {
1280         my $parent = shift;
1281         my $s = shift;
1282         my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($s);
1283         my @rout;
1284
1285         if ($call) {
1286                 if ($is_node) {
1287                         @rout = $parent->add($call, $version, Route::here($here));
1288                 } else {
1289                         @rout = $parent->add_user($call, Route::here($here));
1290                 }
1291         }
1292         return @rout;
1293 }
1294
1295 sub _del_thingy
1296 {
1297         my $parent = shift;
1298         my $s = shift;
1299         my ($call, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($s);
1300         my @rout;
1301         if ($call) {
1302                 if ($is_node) {
1303                         my $nref = Route::Node::get($call);
1304                         @rout = $nref->del($parent) if $nref;
1305                 } else {
1306                         my $uref = Route::User::get($call);
1307                         @rout = $parent->del_user($uref) if $uref;
1308                 }
1309         }
1310         return @rout;
1311 }
1312
1313 # DXSpider routing entries
1314 sub handle_92
1315 {
1316         my $self = shift;
1317         my $pcno = shift;
1318         my $line = shift;
1319         my $origin = shift;
1320
1321         $self->{do_pc92} ||= 1;
1322
1323         my ($pcall, $is_node, $is_extnode, $here, $version, $build) = _decode_pc92_call($_[1]);
1324         unless ($pcall) {
1325                 dbg("PCPROT: invalid callsign string '$_[1]', ignored") if isdbg('chanerr');
1326                 return;
1327         }
1328         my $t = $_[2];
1329         my $sort = $_[3];
1330         
1331         if ($pcall eq $main::mycall) {
1332                 dbg("PCPROT: looped back, ignored") if isdbg('chanerr');
1333                 return;
1334         }
1335
1336         my $parent = Route::Node::get($pcall);
1337         if ($parent) {
1338                 my $lastid = $parent->lastid->{92} || 0;
1339                 if ($lastid > $t) {
1340                         dbg("PCPROT: dup / old id <= $lastid, ignored") if isdbg('chanerr');
1341                         return;
1342                 }
1343                 $parent->flags(Route::here($here));
1344                 $parent->version($version) if $version;
1345         } else {
1346                 $parent = Route::Node->new($pcall, $version, Route::here($here));
1347         }
1348         $parent->lastid->{92} = $t;
1349         $parent->build($build) if $build;
1350
1351         if ($sort eq 'A') {
1352                 if ($_[4]) {
1353                         _add_thingy($parent, $_[4]);
1354                 }
1355         } elsif ($sort eq 'D') {
1356                 if ($_[4]) {
1357                         _del_thingy($parent, $_[4]);
1358                 }
1359         } elsif ($sort eq 'C') {
1360                 my $i;
1361                 $parent->del_nodes;
1362                 $parent->_del_users;
1363                 for ($i = 4; $_[$i]; $i++) {
1364                         _add_thingy($parent, $_[$i]);
1365                 }
1366         } else {
1367                 dbg("PCPROT: unknown action '$sort', ignored") if isdbg('chanerr');
1368         }
1369 }
1370
1371 # if get here then rebroadcast the thing with its Hop count decremented (if
1372 # there is one). If it has a hop count and it decrements to zero then don't
1373 # rebroadcast it.
1374 #
1375 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1376 #        REBROADCAST!!!!
1377 #
1378
1379 sub handle_default
1380 {
1381         my $self = shift;
1382         my $pcno = shift;
1383         my $line = shift;
1384         my $origin = shift;
1385
1386         if (eph_dup($line)) {
1387                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1388         } else {
1389                 unless ($self->{isolate}) {
1390                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1391                 }
1392         }
1393 }