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