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