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