add delete_interface on disconnect
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 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 Time::HiRes qw(gettimeofday tv_interval);
31 use BadWords;
32 use DXHash;
33 use Route;
34 use Route::Node;
35 use Script;
36 use Investigate;
37 use RouteDB;
38
39
40 use strict;
41
42 use vars qw($VERSION $BRANCH);
43 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
44 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
45 $main::build += $VERSION;
46 $main::branch += $BRANCH;
47
48 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
49                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
50                         $pingint $obscount %pc19list $chatdupeage
51                         $investigation_int $pc19_version
52                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
53                         $allowzero $decode_dk0wcy $send_opernam @checklist);
54
55 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
56 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
57
58 $last_hour = time;                              # last time I did an hourly periodic update
59 %pings = ();                    # outstanding ping requests outbound
60 %rcmds = ();                    # outstanding rcmd requests outbound
61 %nodehops = ();                 # node specific hop control
62 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
63
64 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
65                                                                 # loads of 'bad things'
66 $baddx = new DXHash "baddx";
67 $badspotter = new DXHash "badspotter";
68 $badnode = new DXHash "badnode";
69 $last10 = $last_pc50 = time;
70 $ann_to_talk = 1;
71 $rspfcheck = 1;
72 $eph_restime = 180;
73 $eph_info_restime = 60*60;
74 $eph_pc34_restime = 30;
75 $pingint = 5*60;
76 $obscount = 2;
77 $chatdupeage = 20 * 60 * 60;
78 $investigation_int = 12*60*60;  # time between checks to see if we can see this node
79 $pc19_version = 5466;                   # the visible version no for outgoing PC19s generated from pc59
80
81 @checklist = 
82 (
83  [ qw(i c c m bp bc c) ],                       # pc10
84  [ qw(i f m d t m c c h) ],             # pc11
85  [ qw(i c bm m bm bm p h) ],            # pc12
86  [ qw(i c h) ],                                 # 
87  [ qw(i c h) ],                                 # 
88  [ qw(i c m h) ],                                       # 
89  undef ,                                                # pc16 has to be validated manually
90  [ qw(i c c h) ],                                       # pc17
91  [ qw(i m n) ],                                 # pc18
92  undef ,                                                # pc19 has to be validated manually
93  undef ,                                                # pc20 no validation
94  [ qw(i c m h) ],                                       # pc21
95  undef ,                                                # pc22 no validation
96  [ qw(i d n n n n m c c h) ],           # pc23
97  [ qw(i c p h) ],                                       # pc24
98  [ qw(i c c n n) ],                             # pc25
99  [ qw(i f m d t m c c bc) ],            # pc26
100  [ qw(i d n n n n m c c bc) ],  # pc27
101  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
102  [ qw(i c c n m) ],                             # pc29
103  [ qw(i c c n) ],                                       # pc30
104  [ qw(i c c n) ],                                       # pc31
105  [ qw(i c c n) ],                                       # pc32
106  [ qw(i c c n) ],                                       # pc33
107  [ qw(i c c m) ],                                       # pc34
108  [ qw(i c c m) ],                                       # pc35
109  [ qw(i c c m) ],                                       # pc36
110  [ qw(i c c n m) ],                             # pc37
111  undef,                                                 # pc38 not interested
112  [ qw(i c m) ],                                 # pc39
113  [ qw(i c c m p n) ],                           # pc40
114  [ qw(i c n m h) ],                             # pc41
115  [ qw(i c c n) ],                                       # pc42
116  undef,                                                 # pc43 don't handle it
117  [ qw(i c c n m m c) ],                 # pc44
118  [ qw(i c c n m) ],                             # pc45
119  [ qw(i c c n) ],                                       # pc46
120  undef,                                                 # pc47
121  undef,                                                 # pc48
122  [ qw(i c m h) ],                                       # pc49
123  [ qw(i c n h) ],                                       # pc50
124  [ qw(i c c n) ],                                       # pc51
125  undef,
126  undef,
127  undef,
128  undef,
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,                                                 # pc60
134  undef,
135  undef,
136  undef,
137  undef,
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,                                                 # pc70
144  undef,
145  undef,
146  [ qw(i d n n n n n n m m m c c h) ],   # pc73
147  undef,
148  undef,
149  undef,
150  undef,
151  undef,
152  undef,
153  undef,                                                 # pc80
154  undef,
155  undef,
156  undef,
157  [ qw(i c c c m) ],                             # pc84
158  [ qw(i c c c m) ],                             # pc85
159  undef,
160  undef,
161  undef,
162  undef,
163  [ qw(i c n) ],                                 # pc90
164 );
165
166 # use the entry in the check list to check the field list presented
167 # return OK if line NOT in check list (for now)
168 sub check
169 {
170         my $n = shift;
171         $n -= 10;
172         return 0 if $n < 0 || $n > @checklist; 
173         my $ref = $checklist[$n];
174         return 0 unless ref $ref;
175         
176         my $i;
177         for ($i = 1; $i < @$ref; $i++) {
178                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
179                 return 0 unless $act;
180                 next if $blank && $_[$i] =~ /^[ \*]$/;
181                 if ($act eq 'c') {
182                         return $i unless is_callsign($_[$i]);
183                 } elsif ($act eq 'i') {                 
184                         ;                                       # do nothing
185                 } elsif ($act eq 'm') {
186                         return $i unless is_pctext($_[$i]);
187                 } elsif ($act eq 'p') {
188                         return $i unless is_pcflag($_[$i]);
189                 } elsif ($act eq 'f') {
190                         return $i unless is_freq($_[$i]);
191                 } elsif ($act eq 'n') {
192                         return $i unless $_[$i] =~ /^[\d ]+$/;
193                 } elsif ($act eq 'h') {
194                         return $i unless $_[$i] =~ /^H\d\d?$/;
195                 } elsif ($act eq 'd') {
196                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
197                 } elsif ($act eq 't') {
198                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
199                 } 
200         }
201         return 0;
202 }
203
204 sub init
205 {
206         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
207         confess $@ if $@;
208 }
209
210 #
211 # obtain a new connection this is derived from dxchannel
212 #
213
214 sub new 
215 {
216         my $self = DXChannel::alloc(@_);
217
218         # add this node to the table, the values get filled in later
219         my $pkg = shift;
220         my $call = shift;
221         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
222
223         return $self;
224 }
225
226 # this is how a pc connection starts (for an incoming connection)
227 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
228 # all the crap that comes between).
229 sub start
230 {
231         my ($self, $line, $sort) = @_;
232         my $call = $self->{call};
233         my $user = $self->{user};
234
235         # log it
236         my $host = $self->{conn}->{peerhost} || "unknown";
237         Log('DXProt', "$call connected from $host");
238         
239         # remember type of connection
240         $self->{consort} = $line;
241         $self->{outbound} = $sort eq 'O';
242         my $priv = $user->priv;
243         $priv = $user->priv(1) unless $priv;
244         $self->{priv} = $priv;     # other clusters can always be 'normal' users
245         $self->{lang} = $user->lang || 'en';
246         $self->{isolate} = $user->{isolate};
247         $self->{consort} = $line;       # save the connection type
248         $self->{here} = 1;
249         $self->{width} = 80;
250
251         # sort out registration
252         $self->{registered} = 1;
253
254         # get the output filters
255         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
256         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
257         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
258         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
259         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
260
261
262         # get the INPUT filters (these only pertain to Clusters)
263         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
264         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
265         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
266         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
267         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
268         
269         # set unbuffered and no echo
270         $self->send_now('B',"0");
271         $self->send_now('E',"0");
272         $self->conn->echo(0) if $self->conn->can('echo');
273         
274         # ping neighbour node stuff
275         my $ping = $user->pingint;
276         $ping = $pingint unless defined $ping;
277         $self->{pingint} = $ping;
278         $self->{nopings} = $user->nopings || $obscount;
279         $self->{pingtime} = [ ];
280         $self->{pingave} = 999;
281         $self->{metric} ||= 100;
282         $self->{lastping} = $main::systime;
283
284         # send initialisation string
285         unless ($self->{outbound}) {
286                 $self->sendinit;
287         }
288         
289         $self->state('init');
290         $self->{pc50_t} = $main::systime;
291
292         # send info to all logged in thingies
293         $self->tell_login('loginn');
294
295         # run a script send the output to the debug file
296         my $script = new Script(lc $call) || new Script('node_default');
297         $script->run($self) if $script;
298 }
299
300 #
301 # send outgoing 'challenge'
302 #
303
304 sub sendinit
305 {
306         my $self = shift;
307         $self->send(pc18());
308 }
309
310 sub removepc90
311 {
312         $_[0] =~ s/^PC90\^[-A-Z0-9]+\^\d+\^//;
313         $_[0] =~ s/^PC91\^[-A-Z0-9]+\^\d+\^[-A-Z0-9]+\^//;
314 }
315
316 #sub send
317 #{
318 #       my $self = shift;
319 #       while (@_) {
320 #               my $line = shift;
321 #               $self->SUPER::send($line);
322 #       }
323 #}
324
325 #
326 # This is the normal pcxx despatcher
327 #
328 sub normal
329 {
330         my ($self, $line) = @_;
331
332         # remove any incoming PC90 frames
333         removepc90($line);
334
335         my @field = split /\^/, $line;
336         return unless @field;
337         
338         pop @field if $field[-1] eq '~';
339         
340 #       print join(',', @field), "\n";
341                                                 
342         
343         # process PC frames, this will fail unless the frame starts PCnn
344         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
345         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
346                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
347                 return;
348         }
349
350         # check for and dump bad protocol messages
351         my $n = check($pcno, @field);
352         if ($n) {
353                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
354                 return;
355         }
356
357         my $origin = $self->{call};
358         no strict 'subs';
359         my $sub = "handle_$pcno";
360
361         if ($self->can($sub)) {
362                 $self->$sub($pcno, $line, $origin, @field);
363         } else {
364                 $self->handle_default($pcno, $line, $origin, @field);
365         }
366 }
367         
368 # incoming talk commands
369 sub handle_10
370 {
371         my $self = shift;
372         my $pcno = shift;
373         my $line = shift;
374         my $origin = shift;
375
376         # rsfp check
377         return if $rspfcheck and !$self->rspfcheck(0, $_[6], $_[1]);
378                         
379         # will we allow it at all?
380         if ($censorpc) {
381                 my @bad;
382                 if (@bad = BadWords::check($_[3])) {
383                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
384                         return;
385                 }
386         }
387
388         # is it for me or one of mine?
389         my ($from, $to, $via, $call, $dxchan);
390         $from = $_[1];
391         if ($_[5] gt ' ') {
392                 $via = $_[2];
393                 $to = $_[5];
394         } else {
395                 $to = $_[2];
396         }
397
398         # if this is a 'nodx' node then ignore it
399         if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
400                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
401                 return;
402         }
403
404         # if this is a 'bad spotter' user then ignore it
405         my $nossid = $from;
406         $nossid =~ s/-\d+$//;
407         if ($badspotter->in($nossid)) {
408                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
409                 return;
410         }
411
412         # if we are converting announces to talk is it a dup?
413         if ($ann_to_talk) {
414                 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
415                         dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
416                         return;
417                 }
418         }
419
420         # remember a route to this node and also the node on which this user is
421         RouteDB::update($_[6], $self->{call});
422 #       RouteDB::update($to, $_[6]);
423
424         # it is here and logged on
425         $dxchan = DXChannel->get($main::myalias) if $to eq $main::mycall;
426         $dxchan = DXChannel->get($to) unless $dxchan;
427         if ($dxchan && $dxchan->is_user) {
428                 $_[3] =~ s/\%5E/^/g;
429                 $dxchan->talk($from, $to, $via, $_[3]);
430                 return;
431         }
432
433         # is it elsewhere, visible on the cluster via the to address?
434         # note: this discards the via unless the to address is on
435         # the via address
436         my ($ref, $vref);
437         if ($ref = Route::get($to)) {
438                 $vref = Route::Node::get($via) if $via;
439                 $vref = undef unless $vref && grep $to eq $_, $vref->users;
440                 $ref->dxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
441                 return;
442         }
443
444         # can we see an interface to send it down?
445         
446         # not visible here, send a message of condolence
447         $vref = undef;
448         $ref = Route::get($from);
449         $vref = $ref = Route::Node::get($_[6]) unless $ref; 
450         if ($ref) {
451                 $dxchan = $ref->dxchan;
452                 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
453         }
454 }
455
456 # DX Spot handling
457 sub handle_11
458 {
459         my $self = shift;
460         my $pcno = shift;
461         my $line = shift;
462         my $origin = shift;
463
464         # route 'foreign' pc26s 
465         if ($pcno == 26) {
466                 if ($_[7] ne $main::mycall) {
467                         $self->route($_[7], $line);
468                         return;
469                 }
470         }
471                         
472         # rsfp check
473         #                       return if $rspfcheck and !$self->rspfcheck(1, $_[7], $_[6]);
474
475         # if this is a 'nodx' node then ignore it
476         if ($badnode->in($_[7])) {
477                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
478                 return;
479         }
480                         
481         # if this is a 'bad spotter' user then ignore it
482         my $nossid = $_[6];
483         $nossid =~ s/-\d+$//;
484         if ($badspotter->in($nossid)) {
485                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
486                 return;
487         }
488                         
489         # convert the date to a unix date
490         my $d = cltounix($_[3], $_[4]);
491         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
492         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
493                 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
494                 return;
495         }
496
497         # is it 'baddx'
498         if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
499                 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
500                 return;
501         }
502                         
503         # do some de-duping
504         $_[5] =~ s/^\s+//;                      # take any leading blanks off
505         $_[2] = unpad($_[2]);           # take off leading and trailing blanks from spotted callsign
506         if ($_[2] =~ /BUST\w*$/) {
507                 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
508                 return;
509         }
510         if ($censorpc) {
511                 my @bad;
512                 if (@bad = BadWords::check($_[5])) {
513                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
514                         return;
515                 }
516         }
517
518         # remember a route
519 #       RouteDB::update($_[7], $self->{call});
520 #       RouteDB::update($_[6], $_[7]);
521         
522         my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $_[6], $_[7]);
523         # global spot filtering on INPUT
524         if ($self->{inspotsfilter}) {
525                 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
526                 unless ($filter) {
527                         dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
528                         return;
529                 }
530         }
531
532         # this goes after the input filtering, but before the add
533         # so that if it is input filtered, it isn't added to the dup
534         # list. This allows it to come in from a "legitimate" source
535         if (Spot::dup($_[1], $_[2], $d, $_[5], $_[6])) {
536                 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
537                 return;
538         }
539
540         # add it 
541         Spot::add(@spot);
542
543         #
544         # @spot at this point contains:-
545         # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
546         # then  spotted itu, spotted cq, spotters itu, spotters cq
547         # you should be able to route on any of these
548         #
549                         
550         # fix up qra locators of known users 
551         my $user = DXUser->get_current($spot[4]);
552         if ($user) {
553                 my $qra = $user->qra;
554                 unless ($qra && is_qra($qra)) {
555                         my $lat = $user->lat;
556                         my $long = $user->long;
557                         if (defined $lat && defined $long) {
558                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
559                                 $user->put;
560                         }
561                 }
562
563                 # send a remote command to a distant cluster if it is visible and there is no
564                 # qra locator and we havn't done it for a month.
565
566                 unless ($user->qra) {
567                         my $node;
568                         my $to = $user->homenode;
569                         my $last = $user->lastoper || 0;
570                         if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
571                                 my $cmd = "forward/opernam $spot[4]";
572                                 # send the rcmd but we aren't interested in the replies...
573                                 my $dxchan = $node->dxchan;
574                                 if ($dxchan && $dxchan->is_clx) {
575                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
576                                 } else {
577                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
578                                 }
579                                 if ($to ne $_[7]) {
580                                         $to = $_[7];
581                                         $node = Route::Node::get($to);
582                                         if ($node) {
583                                                 $dxchan = $node->dxchan;
584                                                 if ($dxchan && $dxchan->is_clx) {
585                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
586                                                 } else {
587                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
588                                                 }
589                                         }
590                                 }
591                                 $user->lastoper($main::systime);
592                                 $user->put;
593                         }
594                 }
595         }
596                                 
597         # local processing 
598         my $r;
599         eval {
600                 $r = Local::spot($self, @spot);
601         };
602         #                       dbg("Local::spot1 error $@") if isdbg('local') if $@;
603         return if $r;
604
605         # DON'T be silly and send on PC26s!
606         return if $pcno == 26;
607
608         # send out the filtered spots
609         send_dx_spot($self, $line, @spot) if @spot;
610 }
611                 
612 # announces
613 sub handle_12
614 {
615         my $self = shift;
616         my $pcno = shift;
617         my $line = shift;
618         my $origin = shift;
619
620         #                       return if $rspfcheck and !$self->rspfcheck(1, $_[5], $_[1]);
621
622         # announce duplicate checking
623         $_[3] =~ s/^\s+//;                      # remove leading blanks
624
625         if ($censorpc) {
626                 my @bad;
627                 if (@bad = BadWords::check($_[3])) {
628                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
629                         return;
630                 }
631         }
632
633         # if this is a 'nodx' node then ignore it
634         if ($badnode->in($_[5])) {
635                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
636                 return;
637         }
638
639         # if this is a 'bad spotter' user then ignore it
640         my $nossid = $_[1];
641         $nossid =~ s/-\d+$//;
642         if ($badspotter->in($nossid)) {
643                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
644                 return;
645         }
646
647
648         my $dxchan;
649         
650         if ((($dxchan = DXChannel->get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
651                 $self->send_chat($line, @_[1..6]);
652         } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
653
654                 # remember a route
655 #               RouteDB::update($_[5], $self->{call});
656 #               RouteDB::update($_[1], $_[5]);
657
658                 # ignore something that looks like a chat line coming in with sysop
659                 # flag - this is a kludge...
660                 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
661                         dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
662                         return;
663                 }
664
665                 # here's a bit of fun, convert incoming ann with a callsign in the first word
666                 # or one saying 'to <call>' to a talk if we can route to the recipient
667                 if ($ann_to_talk) {
668                         my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
669                         if ($call) {
670                                 my $ref = Route::get($call);
671                                 if ($ref) {
672                                         $dxchan = $ref->dxchan;
673                                         $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
674                                         return;
675                                 }
676                         }
677                 }
678         
679                 # send it
680                 $self->send_announce($line, @_[1..6]);
681         } else {
682                 $self->route($_[2], $line);
683         }
684 }
685                 
686 # incoming user         
687 sub handle_16
688 {
689         my $self = shift;
690         my $pcno = shift;
691         my $line = shift;
692         my $origin = shift;
693
694         # general checks
695         my $dxchan;
696         my $ncall = $_[1];
697         my $newline = "PC16^";
698                         
699         # dos I want users from this channel?
700         unless ($self->user->wantpc16) {
701                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
702                 return;
703         }
704         # is it me?
705         if ($ncall eq $main::mycall) {
706                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
707                 return;
708         }
709
710         RouteDB::update($ncall, $self->{call});
711
712         # do we believe this call? 
713         unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
714                 if (my $ivp = Investigate::get($ncall, $self->{call})) {
715                         $ivp->store_pcxx($pcno,$line,$origin,@_);
716                 } else {
717                         dbg("PCPROT: We don't believe $ncall on $self->{call}") if isdbg('chanerr');
718                 }
719                 return;
720         }
721
722         if (eph_dup($line)) {
723                 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
724                 return;
725         }
726
727         my $parent = Route::Node::get($ncall); 
728
729         # if there is a parent, proceed, otherwise if there is a latent PC19 in the PC19list, 
730         # fix it up in the routing tables and issue it forth before the PC16
731         unless ($parent) {
732                 my $nl = $pc19list{$ncall};
733
734                 if ($nl && @_ > 3) { # 3 because of the hop count!
735
736                         # this is a new (remembered) node, now attach it to me if it isn't in filtered
737                         # and we haven't disallowed it
738                         my $user = DXUser->get_current($ncall);
739                         if (!$user) {
740                                 $user = DXUser->new($ncall);
741                                 $user->sort('A');
742                                 $user->priv(1); # I have relented and defaulted nodes
743                                 $user->lockout(1);
744                                 $user->homenode($ncall);
745                                 $user->node($ncall);
746                         }
747
748                         my $wantpc19 = $user->wantroutepc19;
749                         if ($wantpc19 || !defined $wantpc19) {
750                                 my $new = Route->new($ncall); # throw away
751                                 if ($self->in_filter_route($new)) {
752                                         my @nrout;
753                                         for (@$nl) {
754                                                 $parent = Route::Node::get($_->[0]);
755                                                 $dxchan = $parent->dxchan if $parent;
756                                                 if ($dxchan && $dxchan ne $self) {
757                                                         dbg("PCPROT: PC19 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
758                                                         $parent = undef;
759                                                 }
760                                                 if ($parent) {
761                                                         my $r = $parent->add($ncall, $_->[1], $_->[2]);
762                                                         push @nrout, $r unless @nrout;
763                                                 }
764                                         }
765                                         $user->wantroutepc19(1) unless defined $wantpc19; # for now we work on the basis that pc16 = real route 
766                                         $user->lastin($main::systime) unless DXChannel->get($ncall);
767                                         $user->put;
768                                                 
769                                         # route the pc19 - this will cause 'stuttering PC19s' for a while
770                                         $self->route_pc19($origin, $line, @nrout) if @nrout ;
771                                         $parent = Route::Node::get($ncall);
772                                         unless ($parent) {
773                                                 dbg("PCPROT: lost $ncall after sending PC19 for it?");
774                                                 return;
775                                         }
776                                 } else {
777                                         return;
778                                 }
779                                 delete $pc19list{$ncall};
780                         }
781                 } else {
782                         dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
783                         return;
784                 }
785         } else {
786                                 
787                 $dxchan = $parent->dxchan;
788                 if ($dxchan && $dxchan ne $self) {
789                         dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
790                         return;
791                 }
792
793                 # input filter if required
794                 return unless $self->in_filter_route($parent);
795         }
796
797         my $i;
798         my @rout;
799         for ($i = 2; $i < $#_; $i++) {
800                 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
801                 next unless $call && $conf && defined $here && is_callsign($call);
802                 next if $call eq $main::mycall;
803
804                 eph_del_regex("^PC17\\^$call\\^$ncall");
805                                 
806                 $conf = $conf eq '*';
807
808                 # reject this if we think it is a node already
809                 my $r = Route::Node::get($call);
810                 my $u = DXUser->get_current($call) unless $r;
811                 if ($r || ($u && $u->is_node)) {
812                         dbg("PCPROT: $call is a node") if isdbg('chanerr');
813                         next;
814                 }
815                                 
816                 $r = Route::User::get($call);
817                 my $flags = Route::here($here)|Route::conf($conf);
818                                 
819                 if ($r) {
820                         my $au = $r->addparent($parent);                                        
821                         if ($r->flags != $flags) {
822                                 $r->flags($flags);
823                                 $au = $r;
824                         }
825                         push @rout, $r if $au;
826                 } else {
827                         push @rout, $parent->add_user($call, $flags);
828                 }
829                 
830                                 
831                 # add this station to the user database, if required
832                 $call =~ s/-\d+$//o;    # remove ssid for users
833                 my $user = DXUser->get_current($call);
834                 $user = DXUser->new($call) if !$user;
835                 $user->homenode($parent->call) if !$user->homenode;
836                 $user->node($parent->call);
837                 $user->lastin($main::systime) unless DXChannel->get($call);
838                 $user->put;
839         }
840         $self->route_pc16($origin, $line, $parent, @rout) if @rout;
841 }
842                 
843 # remove a user
844 sub handle_17
845 {
846         my $self = shift;
847         my $pcno = shift;
848         my $line = shift;
849         my $origin = shift;
850         my $dxchan;
851         my $ncall = $_[2];
852         my $ucall = $_[1];
853
854         eph_del_regex("^PC16\\^$ncall.*$ucall");
855                         
856         # do I want users from this channel?
857         unless ($self->user->wantpc16) {
858                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
859                 return;
860         }
861         if ($ncall eq $main::mycall) {
862                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
863                 return;
864         }
865
866         RouteDB::delete($ncall, $self->{call});
867
868         # do we believe this call? 
869         unless ($ncall eq $self->{call} || $self->is_believed($ncall)) {
870                 if (my $ivp = Investigate::get($ncall, $self->{call})) {
871                         $ivp->store_pcxx($pcno,$line,$origin,@_);
872                 } else {
873                         dbg("PCPROT: We don't believe $ncall on $self->{call}") if isdbg('chanerr');
874                 }
875                 return;
876         }
877
878         my $uref = Route::User::get($ucall);
879         unless ($uref) {
880                 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
881         }
882         my $parent = Route::Node::get($ncall);
883         unless ($parent) {
884                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
885         }                       
886
887         $dxchan = $parent->dxchan if $parent;
888         if ($dxchan && $dxchan ne $self) {
889                 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
890                 return;
891         }
892
893         # input filter if required and then remove user if present
894         if ($parent) {
895 #               return unless $self->in_filter_route($parent);  
896                 $parent->del_user($uref) if $uref;
897         } else {
898                 $parent = Route->new($ncall);  # throw away
899         }
900
901         if (eph_dup($line)) {
902                 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
903                 return;
904         }
905
906         $uref = Route->new($ucall) unless $uref; # throw away
907         $self->route_pc17($origin, $line, $parent, $uref);
908 }
909                 
910 # link request
911 sub handle_18
912 {
913         my $self = shift;
914         my $pcno = shift;
915         my $line = shift;
916         my $origin = shift;
917         $self->state('init');   
918
919         # record the type and version offered
920         if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
921                 $self->version(53 + $1);
922                 $self->user->version(53 + $1);
923                 $self->build(0 + $2);
924                 $self->user->build(0 + $2);
925                 unless ($self->is_spider) {
926                         $self->user->sort('S');
927                         $self->user->put;
928                         $self->sort('S');
929                 }
930         } else {
931                 $self->version(50.0);
932                 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
933                 $self->user->version($self->version);
934         }
935
936         # first clear out any nodes on this dxchannel
937         my $parent = Route::Node::get($self->{call});
938         my @rout = $parent->del_nodes;
939         $self->route_pc21($origin, $line, @rout, $parent) if @rout;
940         $self->send_local_config();
941         $self->send(pc20());
942 }
943                 
944 # incoming cluster list
945 sub handle_19
946 {
947         my $self = shift;
948         my $pcno = shift;
949         my $line = shift;
950         my $origin = shift;
951
952         my $i;
953         my $newline = "PC19^";
954
955         # new routing list
956         my @rout;
957
958         # first get the INTERFACE node
959         my $parent = Route::Node::get($self->{call});
960         unless ($parent) {
961                 dbg("DXPROT: my parent $self->{call} has disappeared");
962                 $self->disconnect;
963                 return;
964         }
965
966         # if the origin isn't the same as the INTERFACE, then reparent, creating nodes as necessary
967         if ($origin ne $self->call) {
968                 my $op = Route::Node::get($origin);
969                 unless ($op) {
970                         $op = $parent->add($origin, 5000, Route::here(1));
971                         my $user = DXUser->get_current($origin);
972                         if (!$user) {
973                                 $user = DXUser->new($origin);
974                                 $user->priv(1);         # I have relented and defaulted nodes
975                                 $user->lockout(1);
976                                 $user->homenode($origin);
977                                 $user->node($origin);
978                                 $user->wantroutepc19(1);
979                         }
980                         $user->sort('A') unless $user->is_node;
981                         $user->put;
982                 }
983                 $parent = $op;
984         }
985
986         # parse the PC19
987         for ($i = 1; $i < $#_-1; $i += 4) {
988                 my $here = $_[$i];
989                 my $call = uc $_[$i+1];
990                 my $conf = $_[$i+2];
991                 my $ver = $_[$i+3];
992                 next unless defined $here && defined $conf && is_callsign($call);
993
994                 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
995                                 
996                 # check for sane parameters
997                 #                               $ver = 5000 if $ver eq '0000';
998                 next if $ver < 5000;    # only works with version 5 software
999                 next if length $call < 3; # min 3 letter callsigns
1000                 next if $call eq $main::mycall;
1001
1002                 # check that this PC19 isn't trying to alter the wrong dxchan
1003                 my $dxchan = DXChannel->get($call);
1004                 if ($dxchan && $dxchan != $self) {
1005                         dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
1006                         next;
1007                 }
1008
1009                 # add this station to the user database, if required (don't remove SSID from nodes)
1010                 my $user = DXUser->get_current($call);
1011                 if (!$user) {
1012                         $user = DXUser->new($call);
1013                         $user->priv(1);         # I have relented and defaulted nodes
1014                         $user->lockout(1);
1015                         $user->homenode($call);
1016                         $user->node($call);
1017                 }
1018                 $user->sort('A') unless $user->is_node;
1019
1020                 RouteDB::update($call, $self->{call});
1021
1022                 # do we believe this call?
1023                 my $genline = "PC19^$here^$call^$conf^$ver^$_[-1]^"; 
1024                 unless ($call eq $self->{call} || $self->is_believed($call)) {
1025                         my $pt = $user->lastping($self->{call}) || 0;
1026                         if ($pt+$investigation_int < $main::systime && !Investigate::get($call, $self->{call})) {
1027                                 my $ivp  = Investigate->new($call, $self->{call});
1028                                 $ivp->version($ver);
1029                                 $ivp->here($here);
1030                                 $ivp->store_pcxx($pcno,$genline,$origin,'PC19',$here,$call,$conf,$ver,$_[-1]);
1031                         } else {
1032                                 dbg("PCPROT: We don't believe $call on $self->{call}") if isdbg('chanerr');
1033                         }
1034                         $user->put;
1035                         next;
1036                 }
1037
1038                 if (eph_dup($genline)) {
1039                         dbg("PCPROT: dup PC19 for $call detected") if isdbg('chanerr');
1040                         next;
1041                 }
1042
1043                 my $r = Route::Node::get($call);
1044                 my $flags = Route::here($here)|Route::conf($conf);
1045
1046                 # modify the routing table if it is in it, otherwise store it in the pc19list for now
1047                 if ($r) {
1048                         my $ar;
1049                         if ($call ne $parent->call) {
1050                                 if ($self->in_filter_route($r)) {
1051                                         $ar = $parent->add($call, $ver, $flags);
1052                                         push @rout, $ar if $ar;
1053                                 } else {
1054                                         next;
1055                                 }
1056                         }
1057                         if ($r->version ne $ver || $r->flags != $flags) {
1058                                 $r->version($ver);
1059                                 $r->flags($flags);
1060                                 push @rout, $r unless $ar;
1061                         }
1062                 } else {
1063
1064                         # if he is directly connected or allowed then add him, otherwise store him up for later
1065                         if ($call eq $self->{call} || $user->wantroutepc19) {
1066                                 my $new = Route->new($call); # throw away
1067                                 if ($self->in_filter_route($new)) {
1068                                         my $ar = $parent->add($call, $ver, $flags);
1069                                         $user->wantroutepc19(1) unless defined $user->wantroutepc19;
1070                                         push @rout, $ar if $ar;
1071                                 } else {
1072                                         next;
1073                                 }
1074                         } else {
1075                                 $pc19list{$call} = [] unless exists $pc19list{$call};
1076                                 my $nl = $pc19list{$call};
1077                                 push @{$pc19list{$call}}, [$self->{call}, $ver, $flags] unless grep $_->[0] eq $self->{call}, @$nl;
1078                         }
1079                 }
1080
1081                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
1082                 my $mref = DXMsg::get_busy($call);
1083                 $mref->stop_msg($call) if $mref;
1084                                 
1085                 $user->lastin($main::systime) unless DXChannel->get($call);
1086                 $user->put;
1087         }
1088
1089
1090         $self->route_pc19($origin, $line, @rout) if @rout;
1091 }
1092                 
1093 # send local configuration
1094 sub handle_20
1095 {
1096         my $self = shift;
1097         my $pcno = shift;
1098         my $line = shift;
1099         my $origin = shift;
1100         $self->send_local_config();
1101         $self->send(pc22());
1102         $self->state('normal');
1103         $self->{lastping} = 0;
1104 }
1105                 
1106 # delete a cluster from the list
1107 sub handle_21
1108 {
1109         my $self = shift;
1110         my $pcno = shift;
1111         my $line = shift;
1112         my $origin = shift;
1113         my $call = uc $_[1];
1114
1115         eph_del_regex("^PC1[679].*$call");
1116                         
1117         # if I get a PC21 from the same callsign as self then treat it
1118         # as a PC39: I have gone away
1119         if ($call eq $self->call) {
1120                 $self->disconnect(1);
1121                 return;
1122         }
1123
1124         RouteDB::delete($call, $self->{call});
1125
1126         # check if we believe this
1127         unless ($call eq $self->{call} || $self->is_believed($call)) {
1128                 if (my $ivp = Investigate::get($call, $self->{call})) {
1129                         $ivp->store_pcxx($pcno,$line,$origin,@_);
1130                 } else {
1131                         dbg("PCPROT: We don't believe $call on $self->{call}") if isdbg('chanerr');
1132                 }
1133                 return;
1134         }
1135
1136         # check to see if we are in the pc19list, if we are then don't bother with any of
1137         # this routing table manipulation, just remove it from the list and dump it
1138         my @rout;
1139         if (my $nl = $pc19list{$call}) {
1140                 $pc19list{$call} = [ grep {$_->[0] ne $self->{call}} @$nl ];
1141                 delete $pc19list{$call} unless @{$pc19list{$call}};
1142         } else {
1143                                 
1144                 my $parent = Route::Node::get($self->{call});
1145                 unless ($parent) {
1146                         dbg("DXPROT: my parent $self->{call} has disappeared");
1147                         $self->disconnect;
1148                         return;
1149                 }
1150                 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
1151                         my $node = Route::Node::get($call);
1152                         if ($node) {
1153                                                 
1154                                 my $dxchan = DXChannel->get($call);
1155                                 if ($dxchan && $dxchan != $self) {
1156                                         dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
1157                                         return;
1158                                 }
1159                                                 
1160                                 # input filter it
1161                                 return unless $self->in_filter_route($node);
1162                                                 
1163                                 # routing objects
1164                                 push @rout, $node->del($parent);
1165                         }
1166                 } else {
1167                         dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
1168                         return;
1169                 }
1170         }
1171
1172         $self->route_pc21($origin, $line, @rout) if @rout;
1173 }
1174                 
1175
1176 sub handle_22
1177 {
1178         my $self = shift;
1179         my $pcno = shift;
1180         my $line = shift;
1181         my $origin = shift;
1182         $self->state('normal');
1183         $self->{lastping} = 0;
1184 }
1185                                 
1186 # WWV info
1187 sub handle_23
1188 {
1189         my $self = shift;
1190         my $pcno = shift;
1191         my $line = shift;
1192         my $origin = shift;
1193                         
1194         # route foreign' pc27s 
1195         if ($pcno == 27) {
1196                 if ($_[8] ne $main::mycall) {
1197                         $self->route($_[8], $line);
1198                         return;
1199                 }
1200         }
1201
1202         # only do a rspf check on PC23 (not 27)
1203         if ($pcno == 23) {
1204                 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
1205         }
1206
1207         # do some de-duping
1208         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
1209         my $sfi = unpad($_[3]);
1210         my $k = unpad($_[4]);
1211         my $i = unpad($_[5]);
1212         my ($r) = $_[6] =~ /R=(\d+)/;
1213         $r = 0 unless $r;
1214         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1215                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
1216                 return;
1217         }
1218         if (Geomag::dup($d,$sfi,$k,$i,$_[6])) {
1219                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1220                 return;
1221         }
1222         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
1223                 
1224         my $wwv = Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
1225
1226         my $rep;
1227         eval {
1228                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
1229         };
1230         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
1231         return if $rep;
1232
1233         # DON'T be silly and send on PC27s!
1234         return if $pcno == 27;
1235
1236         # broadcast to the eager world
1237         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
1238 }
1239                 
1240 # set here status
1241 sub handle_24
1242 {
1243         my $self = shift;
1244         my $pcno = shift;
1245         my $line = shift;
1246         my $origin = shift;
1247         my $call = uc $_[1];
1248         my ($nref, $uref);
1249         $nref = Route::Node::get($call);
1250         $uref = Route::User::get($call);
1251         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1252                         
1253         if (eph_dup($line)) {
1254                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
1255                 return;
1256         }
1257         
1258         $nref->here($_[2]) if $nref;
1259         $uref->here($_[2]) if $uref;
1260         my $ref = $nref || $uref;
1261         return unless $self->in_filter_route($ref);
1262
1263         $self->route_pc24($origin, $line, $ref, $_[3]);
1264 }
1265                 
1266 # merge request
1267 sub handle_25
1268 {
1269         my $self = shift;
1270         my $pcno = shift;
1271         my $line = shift;
1272         my $origin = shift;
1273         if ($_[1] ne $main::mycall) {
1274                 $self->route($_[1], $line);
1275                 return;
1276         }
1277         if ($_[2] eq $main::mycall) {
1278                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1279                 return;
1280         }
1281
1282         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1283                         
1284         # spots
1285         if ($_[3] > 0) {
1286                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1287                 my $in;
1288                 foreach $in (@in) {
1289                         $self->send(pc26(@{$in}[0..4], $_[2]));
1290                 }
1291         }
1292
1293         # wwv
1294         if ($_[4] > 0) {
1295                 my @in = reverse Geomag::search(0, $_[4], time, 1);
1296                 my $in;
1297                 foreach $in (@in) {
1298                         $self->send(pc27(@{$in}[0..5], $_[2]));
1299                 }
1300         }
1301 }
1302
1303 sub handle_26 {goto &handle_11}
1304 sub handle_27 {goto &handle_23}
1305
1306 # mail/file handling
1307 sub handle_28
1308 {
1309         my $self = shift;
1310         my $pcno = shift;
1311         my $line = shift;
1312         my $origin = shift;
1313         if ($_[1] eq $main::mycall) {
1314                 no strict 'refs';
1315                 my $sub = "DXMsg::handle_$pcno";
1316                 &$sub($self, @_);
1317         } else {
1318                 $self->route($_[1], $line) unless $self->is_clx;
1319         }
1320 }
1321
1322 sub handle_29 {goto &handle_28}
1323 sub handle_30 {goto &handle_28}
1324 sub handle_31 {goto &handle_28}
1325 sub handle_32 {goto &handle_28}
1326 sub handle_33 {goto &handle_28}
1327                 
1328 sub handle_34
1329 {
1330         my $self = shift;
1331         my $pcno = shift;
1332         my $line = shift;
1333         my $origin = shift;
1334         if (eph_dup($line, $eph_pc34_restime)) {
1335                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1336         } else {
1337                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1338         }
1339 }
1340                 
1341 # remote command replies
1342 sub handle_35
1343 {
1344         my $self = shift;
1345         my $pcno = shift;
1346         my $line = shift;
1347         my $origin = shift;
1348         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1349         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1350 }
1351                 
1352 sub handle_36 {goto &handle_34}
1353
1354 # database stuff
1355 sub handle_37
1356 {
1357         my $self = shift;
1358         my $pcno = shift;
1359         my $line = shift;
1360         my $origin = shift;
1361         if ($_[1] eq $main::mycall) {
1362                 no strict 'refs';
1363                 my $sub = "DXDb::handle_$pcno";
1364                 &$sub($self, @_);
1365         } else {
1366                 $self->route($_[1], $line) unless $self->is_clx;
1367         }
1368 }
1369
1370 # node connected list from neighbour
1371 sub handle_38
1372 {
1373         my $self = shift;
1374         my $pcno = shift;
1375         my $line = shift;
1376         my $origin = shift;
1377 }
1378                 
1379 # incoming disconnect
1380 sub handle_39
1381 {
1382         my $self = shift;
1383         my $pcno = shift;
1384         my $line = shift;
1385         my $origin = shift;
1386         if ($_[1] eq $self->{call}) {
1387                 $self->disconnect(1);
1388         } else {
1389                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1390         }
1391 }
1392
1393 sub handle_40 {goto &handle_28}
1394                 
1395 # user info
1396 sub handle_41
1397 {
1398         my $self = shift;
1399         my $pcno = shift;
1400         my $line = shift;
1401         my $origin = shift;
1402         my $call = $_[1];
1403
1404         my $l = $line;
1405         $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1406         if (eph_dup($l, $eph_info_restime)) {
1407                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1408                 return;
1409         }
1410                         
1411         # input filter if required
1412         #                       my $ref = Route::get($call) || Route->new($call);
1413         #                       return unless $self->in_filter_route($ref);
1414
1415         if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1416                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1417                 return;
1418         }
1419
1420         # add this station to the user database, if required
1421         my $user = DXUser->get_current($call);
1422         $user = DXUser->new($call) unless $user;
1423                         
1424         if ($_[2] == 1) {
1425                 $user->name($_[3]);
1426         } elsif ($_[2] == 2) {
1427                 $user->qth($_[3]);
1428         } elsif ($_[2] == 3) {
1429                 if (is_latlong($_[3])) {
1430                         my ($lat, $long) = DXBearing::stoll($_[3]);
1431                         $user->lat($lat);
1432                         $user->long($long);
1433                         $user->qra(DXBearing::lltoqra($lat, $long));
1434                 } else {
1435                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1436                         return;
1437                 }
1438         } elsif ($_[2] == 4) {
1439                 $user->homenode($_[3]);
1440         } elsif ($_[2] == 5) {
1441                 if (is_qra(uc $_[3])) {
1442                         my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1443                         $user->lat($lat);
1444                         $user->long($long);
1445                         $user->qra(uc $_[3]);
1446                 } else {
1447                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1448                         return;
1449                 }
1450         }
1451         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1452         $user->put;
1453
1454         unless ($self->{isolate}) {
1455                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1456         }
1457
1458         #  perhaps this IS what we want after all
1459         #                       $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1460 }
1461
1462 sub handle_42 {goto &handle_28}
1463
1464
1465 # database
1466 sub handle_44 {goto &handle_37}
1467 sub handle_45 {goto &handle_37}
1468 sub handle_46 {goto &handle_37}
1469 sub handle_47 {goto &handle_37}
1470 sub handle_48 {goto &handle_37}
1471                 
1472 # message and database
1473 sub handle_49
1474 {
1475         my $self = shift;
1476         my $pcno = shift;
1477         my $line = shift;
1478         my $origin = shift;
1479
1480         if (eph_dup($line)) {
1481                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1482                 return;
1483         }
1484         
1485         if ($_[1] eq $main::mycall) {
1486                 DXMsg::handle_49($self, @_);
1487         } else {
1488                 $self->route($_[1], $line) unless $self->is_clx;
1489         }
1490 }
1491
1492 # keep alive/user list
1493 sub handle_50
1494 {
1495         my $self = shift;
1496         my $pcno = shift;
1497         my $line = shift;
1498         my $origin = shift;
1499
1500         my $call = $_[1];
1501
1502         RouteDB::update($call, $self->{call});
1503
1504         my $node = Route::Node::get($call);
1505         if ($node) {
1506                 return unless $node->call eq $self->{call};
1507                 $node->usercount($_[2]);
1508
1509                 # input filter if required
1510                 return unless $self->in_filter_route($node);
1511
1512                 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1513         }
1514 }
1515                 
1516 # incoming ping requests/answers
1517 sub handle_51
1518 {
1519         my $self = shift;
1520         my $pcno = shift;
1521         my $line = shift;
1522         my $origin = shift;
1523         my $to = $_[1];
1524         my $from = $_[2];
1525         my $flag = $_[3];
1526
1527                         
1528         # is it for us?
1529         if ($to eq $main::mycall) {
1530                 if ($flag == 1) {
1531                         $self->send(pc51($from, $to, '0'));
1532                 } else {
1533                         # it's a reply, look in the ping list for this one
1534                         my $ref = $pings{$from};
1535                         if ($ref) {
1536                                 my $tochan =  DXChannel->get($from);
1537                                 while (@$ref) {
1538                                         my $r = shift @$ref;
1539                                         my $dxchan = DXChannel->get($r->{call});
1540                                         next unless $dxchan;
1541                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
1542                                         if ($dxchan->is_user) {
1543                                                 my $s = sprintf "%.2f", $t; 
1544                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1545                                                 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1546                                         } elsif ($dxchan->is_node) {
1547                                                 if ($tochan) {
1548                                                         my $nopings = $tochan->user->nopings || $obscount;
1549                                                         push @{$tochan->{pingtime}}, $t;
1550                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1551                                                         
1552                                                         # cope with a missed ping, this means you must set the pingint large enough
1553                                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
1554                                                                 $t -= $tochan->{pingint};
1555                                                         }
1556                                                         
1557                                                         # calc smoothed RTT a la TCP
1558                                                         if (@{$tochan->{pingtime}} == 1) {
1559                                                                 $tochan->{pingave} = $t;
1560                                                         } else {
1561                                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1562                                                         }
1563                                                         $tochan->{nopings} = $nopings; # pump up the timer
1564                                                         if (my $ivp = Investigate::get($from, $self->{call})) {
1565                                                                 $ivp->handle_ping;
1566                                                         }
1567                                                 } elsif (my $rref = Route::Node::get($r->{call})) {
1568                                                         if (my $ivp = Investigate::get($from, $self->{call})) {
1569                                                                 $ivp->handle_ping;
1570                                                         }
1571                                                 }
1572                                         }
1573                                 }
1574                         }
1575                 }
1576         } else {
1577
1578                 RouteDB::update($from, $self->{call});
1579
1580                 if (eph_dup($line)) {
1581                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1582                         return;
1583                 }
1584                 # route down an appropriate thingy
1585                 $self->route($to, $line);
1586         }
1587 }
1588
1589 # dunno but route it
1590 sub handle_75
1591 {
1592         my $self = shift;
1593         my $pcno = shift;
1594         my $line = shift;
1595         my $origin = shift;
1596         my $call = $_[1];
1597         if ($call ne $main::mycall) {
1598                 $self->route($call, $line);
1599         }
1600 }
1601
1602 # WCY broadcasts
1603 sub handle_73
1604 {
1605         my $self = shift;
1606         my $pcno = shift;
1607         my $line = shift;
1608         my $origin = shift;
1609         my $call = $_[1];
1610                         
1611         # do some de-duping
1612         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1613         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1614                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1615                 return;
1616         }
1617         @_ = map { unpad($_) } @_;
1618         if (WCY::dup($d)) {
1619                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1620                 return;
1621         }
1622                 
1623         my $wcy = WCY::update($d, @_[2..12]);
1624
1625         my $rep;
1626         eval {
1627                 $rep = Local::wcy($self, @_[1..12]);
1628         };
1629         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1630         return if $rep;
1631
1632         # broadcast to the eager world
1633         send_wcy_spot($self, $line, $d, @_[2..12]);
1634 }
1635
1636 # remote commands (incoming)
1637 sub handle_84
1638 {
1639         my $self = shift;
1640         my $pcno = shift;
1641         my $line = shift;
1642         my $origin = shift;
1643         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1644 }
1645
1646 # remote command replies
1647 sub handle_85
1648 {
1649         my $self = shift;
1650         my $pcno = shift;
1651         my $line = shift;
1652         my $origin = shift;
1653         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1654 }
1655
1656 # if get here then rebroadcast the thing with its Hop count decremented (if
1657 # there is one). If it has a hop count and it decrements to zero then don't
1658 # rebroadcast it.
1659 #
1660 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1661 #        REBROADCAST!!!!
1662 #
1663
1664 sub handle_default
1665 {
1666         my $self = shift;
1667         my $pcno = shift;
1668         my $line = shift;
1669         my $origin = shift;
1670
1671         if (eph_dup($line)) {
1672                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1673         } else {
1674                 unless ($self->{isolate}) {
1675                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1676                 }
1677         }
1678 }
1679
1680 #
1681 # This is called from inside the main cluster processing loop and is used
1682 # for despatching commands that are doing some long processing job
1683 #
1684 sub process
1685 {
1686         my $t = time;
1687         my @dxchan = DXChannel->get_all();
1688         my $dxchan;
1689         my $pc50s;
1690         
1691         # send out a pc50 on EVERY channel all at once
1692         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1693                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
1694                 eph_dup($pc50s);
1695                 $last_pc50 = $t;
1696         }
1697
1698         foreach $dxchan (@dxchan) {
1699                 next unless $dxchan->is_node();
1700                 next if $dxchan == $main::me;
1701
1702                 # send the pc50
1703                 $dxchan->send($pc50s) if $pc50s;
1704                 
1705                 # send a ping out on this channel
1706                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1707                         if ($dxchan->{nopings} <= 0) {
1708                                 $dxchan->disconnect;
1709                         } else {
1710                                 addping($main::mycall, $dxchan->call);
1711                                 $dxchan->{nopings} -= 1;
1712                                 $dxchan->{lastping} = $t;
1713                         }
1714                 }
1715         }
1716
1717         Investigate::process();
1718
1719         # every ten seconds
1720         if ($t - $last10 >= 10) {       
1721                 # clean out ephemera 
1722
1723                 eph_clean();
1724
1725                 $last10 = $t;
1726         }
1727         
1728         if ($main::systime - 3600 > $last_hour) {
1729                 $last_hour = $main::systime;
1730         }
1731 }
1732
1733 #
1734 # finish up a pc context
1735 #
1736
1737 #
1738 # some active measures
1739 #
1740
1741
1742 sub send_dx_spot
1743 {
1744         my $self = shift;
1745         my $line = shift;
1746         my @dxchan = DXChannel->get_all();
1747         my $dxchan;
1748         
1749         # send it if it isn't the except list and isn't isolated and still has a hop count
1750         # taking into account filtering and so on
1751         foreach $dxchan (@dxchan) {
1752                 next if $dxchan == $main::me;
1753                 next if $dxchan == $self && $self->is_node;
1754                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1755         }
1756 }
1757
1758 sub dx_spot
1759 {
1760         my $self = shift;
1761         my $line = shift;
1762         my $isolate = shift;
1763         my ($filter, $hops);
1764
1765         if ($self->{spotsfilter}) {
1766                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1767                 return unless $filter;
1768         }
1769         send_prot_line($self, $filter, $hops, $isolate, $line);
1770 }
1771
1772 sub send_prot_line
1773 {
1774         my ($self, $filter, $hops, $isolate, $line) = @_;
1775         my $routeit;
1776
1777
1778         if ($hops) {
1779                 $routeit = $line;
1780                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1781         } else {
1782                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1783                 return unless $routeit;
1784         }
1785         if ($filter) {
1786                 $self->send($routeit);
1787         } else {
1788                 $self->send($routeit) unless $self->{isolate} || $isolate;
1789         }
1790 }
1791
1792
1793 sub send_wwv_spot
1794 {
1795         my $self = shift;
1796         my $line = shift;
1797         my @dxchan = DXChannel->get_all();
1798         my $dxchan;
1799         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
1800
1801         # send it if it isn't the except list and isn't isolated and still has a hop count
1802         # taking into account filtering and so on
1803         foreach $dxchan (@dxchan) {
1804                 next if $dxchan == $main::me;
1805                 next if $dxchan == $self && $self->is_node;
1806                 my $routeit;
1807                 my ($filter, $hops);
1808
1809                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1810         }
1811 }
1812
1813 sub wwv
1814 {
1815         my $self = shift;
1816         my $line = shift;
1817         my $isolate = shift;
1818         my ($filter, $hops);
1819         
1820         if ($self->{wwvfilter}) {
1821                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1822                 return unless $filter;
1823         }
1824         send_prot_line($self, $filter, $hops, $isolate, $line)
1825 }
1826
1827 sub send_wcy_spot
1828 {
1829         my $self = shift;
1830         my $line = shift;
1831         my @dxchan = DXChannel->get_all();
1832         my $dxchan;
1833         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
1834         
1835         # send it if it isn't the except list and isn't isolated and still has a hop count
1836         # taking into account filtering and so on
1837         foreach $dxchan (@dxchan) {
1838                 next if $dxchan == $main::me;
1839                 next if $dxchan == $self;
1840
1841                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1842         }
1843 }
1844
1845 sub wcy
1846 {
1847         my $self = shift;
1848         my $line = shift;
1849         my $isolate = shift;
1850         my ($filter, $hops);
1851
1852         if ($self->{wcyfilter}) {
1853                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1854                 return unless $filter;
1855         }
1856         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1857 }
1858
1859 # send an announce
1860 sub send_announce
1861 {
1862         my $self = shift;
1863         my $line = shift;
1864         my @dxchan = DXChannel->get_all();
1865         my $dxchan;
1866         my $target;
1867         my $to = 'To ';
1868         my $text = unpad($_[2]);
1869                                 
1870         if ($_[3] eq '*') {     # sysops
1871                 $target = "SYSOP";
1872         } elsif ($_[3] gt ' ') { # speciality list handling
1873                 my ($name) = split /\./, $_[3]; 
1874                 $target = "$name"; # put the rest in later (if bothered) 
1875         } 
1876         
1877         if ($_[5] eq '1') {
1878                 $target = "WX"; 
1879                 $to = '';
1880         }
1881         $target = "ALL" if !$target;
1882
1883
1884         # obtain country codes etc 
1885         my @a = Prefix::cty_data($_[0]);
1886         my @b = Prefix::cty_data($_[4]);
1887         if ($self->{inannfilter}) {
1888                 my ($filter, $hops) = 
1889                         $self->{inannfilter}->it(@_, $self->{call}, 
1890                                                                          @a[0..2],
1891                                                                          @b[0..2], $a[3], $b[3]);
1892                 unless ($filter) {
1893                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1894                         return;
1895                 }
1896         }
1897
1898         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
1899                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1900                 return;
1901         }
1902
1903         Log('ann', $target, $_[0], $text);
1904
1905         # send it if it isn't the except list and isn't isolated and still has a hop count
1906         # taking into account filtering and so on
1907         foreach $dxchan (@dxchan) {
1908                 next if $dxchan == $main::me;
1909                 next if $dxchan == $self && $self->is_node;
1910                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
1911                                                   @a[0..2], @b[0..2]);
1912         }
1913 }
1914
1915 my $msgid = 0;
1916
1917 sub nextchatmsgid
1918 {
1919         $msgid++;
1920         $msgid = 1 if $msgid > 999;
1921         return $msgid;
1922 }
1923
1924 # send a chat line
1925 sub send_chat
1926 {
1927         my $self = shift;
1928         my $line = shift;
1929         my @dxchan = DXChannel->get_all();
1930         my $dxchan;
1931         my $target = $_[3];
1932         my $text = unpad($_[2]);
1933         my $ak1a_line;
1934                                 
1935         # munge the group and recast the line if required
1936         if ($target =~ s/\.LST$//) {
1937                 $ak1a_line = $line;
1938         }
1939         
1940         # obtain country codes etc 
1941         my @a = Prefix::cty_data($_[0]);
1942         my @b = Prefix::cty_data($_[4]);
1943         if ($self->{inannfilter}) {
1944                 my ($filter, $hops) = 
1945                         $self->{inannfilter}->it(@_, $self->{call}, 
1946                                                                          @a[0..2],
1947                                                                          @b[0..2], $a[3], $b[3]);
1948                 unless ($filter) {
1949                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1950                         return;
1951                 }
1952         }
1953
1954         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
1955                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1956                 return;
1957         }
1958
1959
1960         Log('chat', $target, $_[0], $text);
1961
1962         # send it if it isn't the except list and isn't isolated and still has a hop count
1963         # taking into account filtering and so on
1964         foreach $dxchan (@dxchan) {
1965                 my $is_ak1a = $dxchan->is_ak1a;
1966                 
1967                 if ($dxchan->is_node) {
1968                         next if $dxchan == $main::me;
1969                         next if $dxchan == $self;
1970                         next unless $dxchan->is_spider || $is_ak1a;
1971                         next if $target eq 'LOCAL';
1972                         if (!$ak1a_line && $is_ak1a) {
1973                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
1974                         }
1975                 }
1976                 
1977                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
1978                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
1979         }
1980 }
1981
1982 sub announce
1983 {
1984         my $self = shift;
1985         my $line = shift;
1986         my $isolate = shift;
1987         my $to = shift;
1988         my $target = shift;
1989         my $text = shift;
1990         my ($filter, $hops);
1991
1992         if ($self->{annfilter}) {
1993                 ($filter, $hops) = $self->{annfilter}->it(@_);
1994                 return unless $filter;
1995         }
1996         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
1997 }
1998
1999 sub chat
2000 {
2001         goto &announce;
2002 }
2003
2004
2005 sub send_local_config
2006 {
2007         my $self = shift;
2008         my $node;
2009         my @nodes;
2010         my @localnodes;
2011         my @remotenodes;
2012
2013         dbg('DXProt::send_local_config') if isdbg('trace');
2014         
2015         # send our nodes
2016         if ($self->{isolate}) {
2017                 @localnodes = ( $main::routeroot );
2018                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
2019         } else {
2020                 # create a list of all the nodes that are not connected to this connection
2021                 # and are not themselves isolated, this to make sure that isolated nodes
2022         # don't appear outside of this node
2023
2024                 # send locally connected nodes
2025                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
2026                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
2027                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
2028
2029                 my $node;
2030                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
2031                 my @intcalls;
2032                 for $node (@rawintcalls) {
2033                         push @intcalls, $node unless grep $node eq $_, @intcalls; 
2034                 }
2035                 my $ref = Route::Node::get($self->{call});
2036                 my @rnodes = $ref->nodes;
2037                 for $node (@intcalls) {
2038                         push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
2039                 }
2040                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
2041         }
2042         
2043         # get all the users connected on the above nodes and send them out
2044         foreach $node ($main::routeroot, @localnodes, @remotenodes) {
2045                 if ($node) {
2046                         my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
2047                         $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
2048                 } else {
2049                         dbg("sent a null value") if isdbg('chanerr');
2050                 }
2051         }
2052 }
2053
2054 #
2055 # route a message down an appropriate interface for a callsign
2056 #
2057 # is called route(to, pcline);
2058 #
2059
2060 sub route
2061 {
2062         my ($self, $call, $line) = @_;
2063
2064         if (ref $self && $call eq $self->{call}) {
2065                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2066                 return;
2067         }
2068
2069         # always send it down the local interface if available
2070         my $dxchan = DXChannel->get($call);
2071         if ($dxchan) {
2072                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
2073         } else {
2074                 my $cl = Route::get($call);
2075                 $dxchan = $cl->dxchan if $cl;
2076                 if (ref $dxchan) {
2077                         if (ref $self && $dxchan eq $self) {
2078                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2079                                 return;
2080                         }
2081                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
2082                 }
2083         }
2084
2085         # try the backstop method
2086         unless ($dxchan) {
2087                 my $rcall = RouteDB::get($call);
2088                 if ($rcall) {
2089                         if ($self && $rcall eq $self->{call}) {
2090                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2091                                 return;
2092                         }
2093                         $dxchan = DXChannel->get($rcall);
2094                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
2095                 }
2096         }
2097
2098         if ($dxchan) {
2099                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
2100                 if ($routeit) {
2101                         $dxchan->send($routeit) unless $dxchan == $main::me;
2102                 }
2103         } else {
2104                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
2105         }
2106 }
2107
2108 #
2109 # obtain the hops from the list for this callsign and pc no 
2110 #
2111
2112 sub get_hops
2113 {
2114         my $pcno = shift;
2115         my $hops = $DXProt::hopcount{$pcno};
2116         $hops = $DXProt::def_hopcount if !$hops;
2117         return "H$hops";       
2118 }
2119
2120
2121 # adjust the hop count on a per node basis using the user loadable 
2122 # hop table if available or else decrement an existing one
2123 #
2124
2125 sub adjust_hops
2126 {
2127         my $self = shift;
2128         my $s = shift;
2129         my $call = $self->{call};
2130         my $hops;
2131         
2132         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
2133                 my ($pcno) = $s =~ /^PC(\d\d)/o;
2134                 confess "$call called adjust_hops with '$s'" unless $pcno;
2135                 my $ref = $nodehops{$call} if %nodehops;
2136                 if ($ref) {
2137                         my $newhops = $ref->{$pcno};
2138                         return "" if defined $newhops && $newhops == 0;
2139                         $newhops = $ref->{default} unless $newhops;
2140                         return "" if defined $newhops && $newhops == 0;
2141                         $newhops = $hops if !$newhops;
2142                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
2143                 } else {
2144                         # simply decrement it
2145                         $hops--;
2146                         return "" if !$hops;
2147                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
2148                 }
2149         }
2150         return $s;
2151 }
2152
2153
2154 # load hop tables
2155 #
2156 sub load_hops
2157 {
2158         my $self = shift;
2159         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
2160         do "$main::data/hop_table.pl";
2161         return $@ if $@;
2162         return ();
2163 }
2164
2165
2166 # add a ping request to the ping queues
2167 sub addping
2168 {
2169         my ($from, $to, $via) = @_;
2170         my $ref = $pings{$to} || [];
2171         my $r = {};
2172         $r->{call} = $from;
2173         $r->{t} = [ gettimeofday ];
2174         if ($via && (my $dxchan = DXChannel->get($via))) {
2175                 $dxchan->send(pc51($to, $main::mycall, 1));
2176         } else {
2177                 route(undef, $to, pc51($to, $main::mycall, 1));
2178         }
2179         push @$ref, $r;
2180         $pings{$to} = $ref;
2181         my $u = DXUser->get_current($to);
2182         if ($u) {
2183                 $u->lastping(($via || $from), $main::systime);
2184                 $u->put;
2185         }
2186 }
2187
2188 sub process_rcmd
2189 {
2190         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
2191         if ($tonode eq $main::mycall) {
2192                 my $ref = DXUser->get_current($fromnode);
2193                 my $cref = Route::Node::get($fromnode);
2194                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
2195                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
2196                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
2197                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
2198                                 my $oldpriv = $self->{priv};
2199                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
2200                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
2201                                 $self->{priv} = $oldpriv;
2202                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
2203                                 delete $self->{remotecmd};
2204                         } else {
2205                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
2206                         }
2207                 } else {
2208                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
2209                 }
2210         } else {
2211                 my $ref = DXUser->get_current($tonode);
2212                 if ($ref && $ref->is_clx) {
2213                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
2214                 } else {
2215                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
2216                 }
2217         }
2218 }
2219
2220 sub process_rcmd_reply
2221 {
2222         my ($self, $tonode, $fromnode, $user, $line) = @_;
2223         if ($tonode eq $main::mycall) {
2224                 my $s = $rcmds{$fromnode};
2225                 if ($s) {
2226                         my $dxchan = DXChannel->get($s->{call});
2227                         my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
2228                         $ref->send($line) if $ref;
2229                         delete $rcmds{$fromnode} if !$dxchan;
2230                 } else {
2231                         # send unsolicited ones to the sysop
2232                         my $dxchan = DXChannel->get($main::myalias);
2233                         $dxchan->send($line) if $dxchan;
2234                 }
2235         } else {
2236                 my $ref = DXUser->get_current($tonode);
2237                 if ($ref && $ref->is_clx) {
2238                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
2239                 } else {
2240                         $self->route($tonode, pc35($fromnode, $tonode, $line));
2241                 }
2242         }
2243 }
2244
2245 sub send_rcmd_reply
2246 {
2247         my $self = shift;
2248         my $tonode = shift;
2249         my $fromnode = shift;
2250         my $user = shift;
2251         while (@_) {
2252                 my $line = shift;
2253                 $line =~ s/\s*$//;
2254                 Log('rcmd', 'out', $fromnode, $line);
2255                 if ($self->is_clx) {
2256                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
2257                 } else {
2258                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
2259                 }
2260         }
2261 }
2262
2263 # add a rcmd request to the rcmd queues
2264 sub addrcmd
2265 {
2266         my ($self, $to, $cmd) = @_;
2267
2268         my $r = {};
2269         $r->{call} = $self->{call};
2270         $r->{t} = $main::systime;
2271         $r->{cmd} = $cmd;
2272         $rcmds{$to} = $r;
2273         
2274         my $ref = Route::Node::get($to);
2275         my $dxchan = $ref->dxchan;
2276         if ($dxchan && $dxchan->is_clx) {
2277                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
2278         } else {
2279                 route(undef, $to, pc34($main::mycall, $to, $cmd));
2280         }
2281 }
2282
2283 sub disconnect
2284 {
2285         my $self = shift;
2286         my $pc39flag = shift;
2287         my $call = $self->call;
2288
2289         return if $self->{disconnecting}++;
2290         
2291         unless ($pc39flag && $pc39flag == 1) {
2292                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
2293         }
2294
2295         # get rid of any PC16/17/19
2296         eph_del_regex("^PC1[679]*$call");
2297
2298         # do routing stuff, remove me from routing table
2299         my $node = Route::Node::get($call);
2300         my @rout;
2301         if ($node) {
2302                 @rout = $node->del($main::routeroot);
2303                 
2304                 # and all my ephemera as well
2305                 for (@rout) {
2306                         my $c = $_->call;
2307                         eph_del_regex("^PC1[679].*$c");
2308                 }
2309         }
2310
2311         RouteDB::delete_interface($call);
2312         
2313         # remove them from the pc19list as well
2314         while (my ($k,$v) = each %pc19list) {
2315                 my @l = grep {$_->[0] ne $call} @{$pc19list{$k}};
2316                 if (@l) {
2317                         $pc19list{$k} = \@l;
2318                 } else {
2319                         delete $pc19list{$k};
2320                 }
2321                 
2322                 # and the ephemera
2323                 eph_del_regex("^PC1[679].*$k");
2324         }
2325
2326         # unbusy and stop and outgoing mail
2327         my $mref = DXMsg::get_busy($call);
2328         $mref->stop_msg($call) if $mref;
2329         
2330         # broadcast to all other nodes that all the nodes connected to via me are gone
2331         unless ($pc39flag && $pc39flag == 2) {
2332                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
2333         }
2334
2335         # remove outstanding pings
2336         delete $pings{$call};
2337         
2338         # I was the last node visited
2339     $self->user->node($main::mycall);
2340
2341         # send info to all logged in thingies
2342         $self->tell_login('logoutn');
2343
2344         Log('DXProt', $call . " Disconnected");
2345
2346         $self->SUPER::disconnect;
2347 }
2348
2349
2350
2351 # send a talk message to this thingy
2352 #
2353 sub talk
2354 {
2355         my ($self, $from, $to, $via, $line, $origin) = @_;
2356         
2357         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
2358         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
2359         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
2360 }
2361
2362 # send it if it isn't the except list and isn't isolated and still has a hop count
2363 # taking into account filtering and so on
2364
2365 sub send_route
2366 {
2367         my $self = shift;
2368         my $origin = shift;
2369         my $generate = shift;
2370         my $no = shift;     # the no of things to filter on 
2371         my $routeit;
2372         my ($filter, $hops);
2373         my @rin;
2374         
2375         for (; @_ && $no; $no--) {
2376                 my $r = shift;
2377                 
2378                 if (!$self->{isolate} && $self->{routefilter}) {
2379                         $filter = undef;
2380                         if ($r) {
2381                                 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->{state}, $r->{state});
2382                                 if ($filter) {
2383                                         push @rin, $r;
2384                                 } else {
2385                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
2386                                 }
2387                         } else {
2388                                 dbg("was sent a null value") if isdbg('chanerr');
2389                         }
2390                 } else {
2391                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
2392                 }
2393         }
2394         if (@rin) {
2395                 foreach my $line (&$generate(@rin, @_)) {
2396                         if ($hops) {
2397                                 $routeit = $line;
2398                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
2399                         } else {
2400                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
2401                                 next unless $routeit;
2402                         }
2403                         
2404                         $self->send($routeit);
2405                 }
2406         }
2407 }
2408
2409 sub broadcast_route
2410 {
2411         my $self = shift;
2412         my $origin = shift;
2413         my $generate = shift;
2414         my $line = shift;
2415         my @dxchan = DXChannel::get_all_nodes();
2416         my $dxchan;
2417         
2418         unless ($self->{isolate}) {
2419                 foreach $dxchan (@dxchan) {
2420                         next if $dxchan == $self;
2421                         next if $dxchan == $main::me;
2422                         next unless $dxchan->isa('DXProt');
2423                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
2424  
2425                         $dxchan->send_route($origin, $generate, @_);
2426                 }
2427         }
2428 }
2429
2430 sub route_pc16
2431 {
2432         my $self = shift;
2433         return unless $self->user->wantpc16;
2434         my $origin = shift;
2435         my $line = shift;
2436         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
2437 }
2438
2439 sub route_pc17
2440 {
2441         my $self = shift;
2442         return unless $self->user->wantpc16;
2443         my $origin = shift;
2444         my $line = shift;
2445         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
2446 }
2447
2448 sub route_pc19
2449 {
2450         my $self = shift;
2451         my $origin = shift;
2452         my $line = shift;
2453         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
2454 }
2455
2456 sub route_pc21
2457 {
2458         my $self = shift;
2459         my $origin = shift;
2460         my $line = shift;
2461         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
2462 }
2463
2464 sub route_pc24
2465 {
2466         my $self = shift;
2467         my $origin = shift;
2468         my $line = shift;
2469         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
2470 }
2471
2472 sub route_pc41
2473 {
2474         my $self = shift;
2475         my $origin = shift;
2476         my $line = shift;
2477         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
2478 }
2479
2480 sub route_pc50
2481 {
2482         my $self = shift;
2483         my $origin = shift;
2484         my $line = shift;
2485         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
2486 }
2487
2488 sub in_filter_route
2489 {
2490         my $self = shift;
2491         my $r = shift;
2492         my ($filter, $hops) = (1, 1);
2493         
2494         if ($self->{inroutefilter}) {
2495                 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->state, $r->state);
2496                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
2497         }
2498         return $filter;
2499 }
2500
2501 sub eph_dup
2502 {
2503         my $s = shift;
2504         my $t = shift || $eph_restime;
2505         my $r;
2506
2507         # chop the end off
2508         $s =~ s/\^H\d\d?\^?\~?$//;
2509         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
2510         $eph{$s} = $main::systime + $t;
2511         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
2512         return $r;
2513 }
2514
2515 sub eph_del_regex
2516 {
2517         my $regex = shift;
2518         my ($key, $val);
2519         while (($key, $val) = each %eph) {
2520                 if ($key =~ m{$regex}) {
2521                         delete $eph{$key};
2522                 }
2523         }
2524 }
2525
2526 sub eph_clean
2527 {
2528         my ($key, $val);
2529         
2530         while (($key, $val) = each %eph) {
2531                 if ($main::systime >= $val) {
2532                         delete $eph{$key};
2533                 }
2534         }
2535 }
2536
2537 sub eph_list
2538 {
2539         my ($key, $val);
2540         my @out;
2541
2542         while (($key, $val) = each %eph) {
2543                 push @out, $key, $val;
2544         }
2545         return @out;
2546 }
2547
2548 sub run_cmd
2549 {
2550         goto &DXCommandmode::run_cmd;
2551 }
2552 1;
2553 __END__