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