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