497a26712b63f64b279b7bde6992f4d4fc2d525b
[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->dxchan->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->dxchan;
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->dxchan;
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->dxchan;
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->dxchan;
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         next unless $ncall eq $self->{call} || $self->is_believed($ncall);
711
712         my $node = Route::Node::get($ncall);
713         unless ($node) {
714                 dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
715                 return;
716         }
717
718         # dedupe only that which we potentially process
719         if (eph_dup($line)) {
720                 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
721                 return;
722         }
723
724         my $i;
725         my @rout;
726         my @new;
727         for ($i = 2; $i < $#_; $i++) {
728                 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
729                 next unless $call && $conf && defined $here && is_callsign($call);
730                 next if $call eq $main::mycall;
731
732                 eph_del_regex("^PC17\\^$call\\^$ncall");
733                                 
734                 $conf = $conf eq '*';
735
736                 # reject this if we think it is a node already
737                 my $r = Route::Node::get($call);
738                 my $u = DXUser->get_current($call) unless $r;
739                 if ($r || ($u && $u->is_node)) {
740                         dbg("PCPROT: $call is a node") if isdbg('chanerr');
741                         next;
742                 }
743                                 
744                 $r = Route::User::get($call) || Route::User::get($call);
745                 $r->here($here);
746                 $r->conf($conf);
747                 $node->lastseen($main::systime);
748
749                 push @new, $node->add_user($r);
750                                 
751                 # add this station to the user database, if required
752                 $call =~ s/-\d+$//o;    # remove ssid for users
753                 my $user = DXUser->get_current($call);
754                 $user = DXUser->new($call) if !$user;
755                 $user->homenode($node->call) if !$user->homenode;
756                 $user->node($node->call);
757                 $user->lastin($main::systime) unless DXChannel->get($call);
758                 $user->put;
759         }
760         $self->route_pc16($origin, $line, $node, @new) if @new;
761 }
762                 
763 # remove a user
764 sub handle_17
765 {
766         my $self = shift;
767         my $pcno = shift;
768         my $line = shift;
769         my $origin = shift;
770         my $dxchan;
771         my $ncall = $_[2];
772         my $ucall = $_[1];
773
774         eph_del_regex("^PC16\\^$ncall.*$ucall");
775                         
776         # do I want users from this channel?
777         unless ($self->user->wantpc16) {
778                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
779                 return;
780         }
781         if ($ncall eq $main::mycall) {
782                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
783                 return;
784         }
785
786         # do we believe this call? 
787         next unless $ncall eq $self->{call} || $self->is_believed($ncall);
788
789         my $uref = Route::User::get($ucall);
790         unless ($uref) {
791                 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
792         }
793         my $node = Route::Node::get($ncall);
794         unless ($node) {
795                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
796         }                       
797
798         return unless $node && $uref;
799         
800         my @rout;
801         my @new;
802         if ($self->in_filter_route($node)) {
803                 
804                 if (eph_dup($line)) {
805                         dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
806                         return;
807                 }
808                 push @new, $node->del_user($uref);
809         }
810
811         $self->route_pc17($origin, $line, $node, $uref) if @new;
812
813         # get rid of orphaned users;
814         $_->delete for @new;
815 }
816                 
817 # link request
818 sub handle_18
819 {
820         my $self = shift;
821         my $pcno = shift;
822         my $line = shift;
823         my $origin = shift;
824         $self->state('init');   
825
826         # record the type and version offered
827         if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
828                 $self->version(53 + $1);
829                 $self->user->version(53 + $1);
830                 $self->build(0 + $2);
831                 $self->user->build(0 + $2);
832                 unless ($self->is_spider) {
833                         $self->user->sort('S');
834                         $self->user->put;
835                         $self->sort('S');
836                 }
837         } else {
838                 $self->version(50.0);
839                 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
840                 $self->user->version($self->version);
841         }
842         $self->newroute( $_[1] =~ /NewRoute/ );
843
844         # first clear out any nodes on this dxchannel
845         my $node = Route::Node::get($self->{call}) ;
846         my @rout = grep {$_ != $node } $main::routeroot->remove_route($node, $self) if $node;
847         my @rusers;
848         push @rusers, $_->unlink_all_users for @rout;
849         $self->route_pc21($origin, $line, @rout) if @rout;
850         for (@rout, @rusers) {
851                 $_->delete;
852         };
853         
854         # send the new config
855         $self->send_local_config();
856         $self->send(pc20());
857 }
858                 
859 # incoming cluster list
860 sub handle_19
861 {
862         my $self = shift;
863         my $pcno = shift;
864         my $line = shift;
865         my $origin = shift;
866
867         my $i;
868         my $newline = "PC19^";
869
870         if (eph_dup($line)) {
871                 dbg("PCPROT: dup PC19 detected") if isdbg('chanerr');
872                 return;
873         }
874
875         # new routing list
876         my @rout;
877
878         # first get the INTERFACE node
879         my $parent = Route::Node::get($self->{call});
880         unless ($parent) {
881                 dbg("DXPROT: my parent $self->{call} has disappeared");
882                 $self->disconnect;
883                 return;
884         }
885
886         my @new;
887         
888         # parse the PC19
889         for ($i = 1; $i < $#_-1; $i += 4) {
890                 my $here = $_[$i];
891                 my $call = uc $_[$i+1];
892                 my $conf = $_[$i+2];
893                 my $ver = $_[$i+3];
894                 next unless defined $here && defined $conf && is_callsign($call);
895
896                 # check for sane parameters
897                 #                               $ver = 5000 if $ver eq '0000';
898                 next if $ver < 5000;    # only works with version 5 software
899                 next if length $call < 3; # min 3 letter callsigns
900                 next if $call eq $main::mycall;
901
902                 # do we believe this call? 
903                 next unless $call eq $self->{call} || $self->is_believed($call);
904
905                 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
906                                 
907                 # add this station to the user database, if required (don't remove SSID from nodes)
908                 my $user = DXUser->get_current($call);
909                 if (!$user) {
910                         $user = DXUser->new($call);
911                         $user->sort('A');
912                         $user->priv(1);         # I have relented and defaulted nodes
913                         $user->lockout(1);
914                         $user->homenode($call);
915                         $user->node($call);
916                 }
917                 $user->wantroutepc19(1) unless defined $user->wantroutepc19;
918
919                 my $r = Route::Node::get($call) || Route::Node->new($call);
920                 $r->here($here);
921                 $r->conf($conf);
922                 $r->version($ver);
923                 $r->lastseen($main::systime);
924
925                 if ($self->in_filter_route($r)) {
926                         push @new, $parent->link_node($r, $self);
927                         push @rout, $r;
928                 }
929
930                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
931                 my $mref = DXMsg::get_busy($call);
932                 $mref->stop_msg($call) if $mref;
933                                 
934                 $user->lastin($main::systime) unless DXChannel->get($call);
935                 $user->put;
936         }
937
938         # route out new nodes to legacy nodes
939         $self->route_pc19($origin, $line, @new) if @new;
940         $self->route_pc59('A', 0, $self->{call}, @rout) if @rout;
941 }
942                 
943 # send local configuration
944 sub handle_20
945 {
946         my $self = shift;
947         my $pcno = shift;
948         my $line = shift;
949         my $origin = shift;
950         $self->send_local_config();
951         $self->send(pc22());
952         $self->state('normal');
953         $self->{lastping} = 0;
954 }
955                 
956 # delete a cluster from the list
957 sub handle_21
958 {
959         my $self = shift;
960         my $pcno = shift;
961         my $line = shift;
962         my $origin = shift;
963         my $call = uc $_[1];
964
965         return if $call eq $main::mycall;  # don't allow malicious buggers to disconnect me (or ignore loops)!
966
967         eph_del_regex("^PC1[679].*$call");
968                         
969         # if I get a PC21 from the same callsign as self then treat it
970         # as a PC39: I have gone away
971         if ($call eq $self->call) {
972                 $self->disconnect(1);
973                 return;
974         }
975
976         my @rout;
977         my @new;
978         my $parent = Route::Node::get($self->{call});
979         unless ($parent) {
980                 dbg("DXPROT: my parent $self->{call} has disappeared");
981                 $self->disconnect;
982                 return;
983         }
984         $parent->lastseen;
985
986         my $node = Route::Node::get($call);
987         if ($node) {
988                 $node->lastseen($main::systime);
989                 
990                 # input filter it
991                 return unless $self->in_filter_route($node);
992                 push @rout, $node;
993                 push @new, $node->link_node($parent, $self);
994         }
995
996         $self->route_pc21($origin, $line, @new) if @new;
997         $self->route_pc59('D', 0, $self->{call}, @rout) if @rout;
998
999         # get rid of orphaned nodes;
1000         $_->delete for @new;
1001 }
1002                 
1003
1004 sub handle_22
1005 {
1006         my $self = shift;
1007         my $pcno = shift;
1008         my $line = shift;
1009         my $origin = shift;
1010         $self->state('normal');
1011         $self->{lastping} = 0;
1012 }
1013                                 
1014 # WWV info
1015 sub handle_23
1016 {
1017         my $self = shift;
1018         my $pcno = shift;
1019         my $line = shift;
1020         my $origin = shift;
1021                         
1022         # route foreign' pc27s 
1023         if ($pcno == 27) {
1024                 if ($_[8] ne $main::mycall) {
1025                         $self->route($_[8], $line);
1026                         return;
1027                 }
1028         }
1029
1030         # only do a rspf check on PC23 (not 27)
1031         if ($pcno == 23) {
1032                 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
1033         }
1034
1035         # do some de-duping
1036         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
1037         my $sfi = unpad($_[3]);
1038         my $k = unpad($_[4]);
1039         my $i = unpad($_[5]);
1040         my ($r) = $_[6] =~ /R=(\d+)/;
1041         $r = 0 unless $r;
1042         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1043                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
1044                 return;
1045         }
1046         if (Geomag::dup($d,$sfi,$k,$i,$_[6])) {
1047                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1048                 return;
1049         }
1050         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
1051                 
1052         my $wwv = Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
1053
1054         my $rep;
1055         eval {
1056                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
1057         };
1058         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
1059         return if $rep;
1060
1061         # DON'T be silly and send on PC27s!
1062         return if $pcno == 27;
1063
1064         # broadcast to the eager world
1065         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
1066 }
1067                 
1068 # set here status
1069 sub handle_24
1070 {
1071         my $self = shift;
1072         my $pcno = shift;
1073         my $line = shift;
1074         my $origin = shift;
1075         my $call = uc $_[1];
1076         my ($nref, $uref);
1077         $nref = Route::Node::get($call);
1078         $uref = Route::User::get($call);
1079         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1080                         
1081         if (eph_dup($line)) {
1082                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
1083                 return;
1084         }
1085         
1086         $nref->here($_[2]) if $nref;
1087         $uref->here($_[2]) if $uref;
1088         my $ref = $nref || $uref;
1089         return unless $self->in_filter_route($ref);
1090
1091         $self->route_pc24($origin, $line, $ref, $_[3]);
1092 }
1093                 
1094 # merge request
1095 sub handle_25
1096 {
1097         my $self = shift;
1098         my $pcno = shift;
1099         my $line = shift;
1100         my $origin = shift;
1101         if ($_[1] ne $main::mycall) {
1102                 $self->route($_[1], $line);
1103                 return;
1104         }
1105         if ($_[2] eq $main::mycall) {
1106                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1107                 return;
1108         }
1109
1110         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1111                         
1112         # spots
1113         if ($_[3] > 0) {
1114                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1115                 my $in;
1116                 foreach $in (@in) {
1117                         $self->send(pc26(@{$in}[0..4], $_[2]));
1118                 }
1119         }
1120
1121         # wwv
1122         if ($_[4] > 0) {
1123                 my @in = reverse Geomag::search(0, $_[4], time, 1);
1124                 my $in;
1125                 foreach $in (@in) {
1126                         $self->send(pc27(@{$in}[0..5], $_[2]));
1127                 }
1128         }
1129 }
1130
1131 sub handle_26 {goto &handle_11}
1132 sub handle_27 {goto &handle_23}
1133
1134 # mail/file handling
1135 sub handle_28
1136 {
1137         my $self = shift;
1138         my $pcno = shift;
1139         my $line = shift;
1140         my $origin = shift;
1141         if ($_[1] eq $main::mycall) {
1142                 no strict 'refs';
1143                 my $sub = "DXMsg::handle_$pcno";
1144                 &$sub($self, @_);
1145         } else {
1146                 $self->route($_[1], $line) unless $self->is_clx;
1147         }
1148 }
1149
1150 sub handle_29 {goto &handle_28}
1151 sub handle_30 {goto &handle_28}
1152 sub handle_31 {goto &handle_28}
1153 sub handle_32 {goto &handle_28}
1154 sub handle_33 {goto &handle_28}
1155                 
1156 sub handle_34
1157 {
1158         my $self = shift;
1159         my $pcno = shift;
1160         my $line = shift;
1161         my $origin = shift;
1162         if (eph_dup($line, $eph_pc34_restime)) {
1163                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1164         } else {
1165                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1166         }
1167 }
1168                 
1169 # remote command replies
1170 sub handle_35
1171 {
1172         my $self = shift;
1173         my $pcno = shift;
1174         my $line = shift;
1175         my $origin = shift;
1176         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1177         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1178 }
1179                 
1180 sub handle_36 {goto &handle_34}
1181
1182 # database stuff
1183 sub handle_37
1184 {
1185         my $self = shift;
1186         my $pcno = shift;
1187         my $line = shift;
1188         my $origin = shift;
1189         if ($_[1] eq $main::mycall) {
1190                 no strict 'refs';
1191                 my $sub = "DXDb::handle_$pcno";
1192                 &$sub($self, @_);
1193         } else {
1194                 $self->route($_[1], $line) unless $self->is_clx;
1195         }
1196 }
1197
1198 # node connected list from neighbour
1199 sub handle_38
1200 {
1201         my $self = shift;
1202         my $pcno = shift;
1203         my $line = shift;
1204         my $origin = shift;
1205 }
1206                 
1207 # incoming disconnect
1208 sub handle_39
1209 {
1210         my $self = shift;
1211         my $pcno = shift;
1212         my $line = shift;
1213         my $origin = shift;
1214         if ($_[1] eq $self->{call}) {
1215                 $self->disconnect(1);
1216         } else {
1217                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1218         }
1219 }
1220
1221 sub handle_40 {goto &handle_28}
1222                 
1223 # user info
1224 sub handle_41
1225 {
1226         my $self = shift;
1227         my $pcno = shift;
1228         my $line = shift;
1229         my $origin = shift;
1230         my $call = $_[1];
1231
1232         my $l = $line;
1233         $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1234         if (eph_dup($l, $eph_info_restime)) {
1235                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1236                 return;
1237         }
1238                         
1239         # input filter if required
1240         #                       my $ref = Route::get($call) || Route->new($call);
1241         #                       return unless $self->in_filter_route($ref);
1242
1243         if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1244                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1245                 return;
1246         }
1247
1248         # add this station to the user database, if required
1249         my $user = DXUser->get_current($call);
1250         $user = DXUser->new($call) unless $user;
1251                         
1252         if ($_[2] == 1) {
1253                 $user->name($_[3]);
1254         } elsif ($_[2] == 2) {
1255                 $user->qth($_[3]);
1256         } elsif ($_[2] == 3) {
1257                 if (is_latlong($_[3])) {
1258                         my ($lat, $long) = DXBearing::stoll($_[3]);
1259                         $user->lat($lat);
1260                         $user->long($long);
1261                         $user->qra(DXBearing::lltoqra($lat, $long));
1262                 } else {
1263                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1264                         return;
1265                 }
1266         } elsif ($_[2] == 4) {
1267                 $user->homenode($_[3]);
1268         } elsif ($_[2] == 5) {
1269                 if (is_qra(uc $_[3])) {
1270                         my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1271                         $user->lat($lat);
1272                         $user->long($long);
1273                         $user->qra(uc $_[3]);
1274                 } else {
1275                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1276                         return;
1277                 }
1278         }
1279         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1280         $user->put;
1281
1282         unless ($self->{isolate}) {
1283                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1284         }
1285
1286         #  perhaps this IS what we want after all
1287         #                       $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1288 }
1289
1290 sub handle_42 {goto &handle_28}
1291
1292
1293 # database
1294 sub handle_44 {goto &handle_37}
1295 sub handle_45 {goto &handle_37}
1296 sub handle_46 {goto &handle_37}
1297 sub handle_47 {goto &handle_37}
1298 sub handle_48 {goto &handle_37}
1299                 
1300 # message and database
1301 sub handle_49
1302 {
1303         my $self = shift;
1304         my $pcno = shift;
1305         my $line = shift;
1306         my $origin = shift;
1307
1308         if (eph_dup($line)) {
1309                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1310                 return;
1311         }
1312         
1313         if ($_[1] eq $main::mycall) {
1314                 DXMsg::handle_49($self, @_);
1315         } else {
1316                 $self->route($_[1], $line) unless $self->is_clx;
1317         }
1318 }
1319
1320 # keep alive/user list
1321 sub handle_50
1322 {
1323         my $self = shift;
1324         my $pcno = shift;
1325         my $line = shift;
1326         my $origin = shift;
1327
1328         my $call = $_[1];
1329         my $node = Route::Node::get($call);
1330         if ($node) {
1331                 return unless $node->call eq $self->{call};
1332                 $node->usercount($_[2]);
1333
1334                 # input filter if required
1335                 return unless $self->in_filter_route($node);
1336
1337                 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1338         }
1339 }
1340                 
1341 # incoming ping requests/answers
1342 sub handle_51
1343 {
1344         my $self = shift;
1345         my $pcno = shift;
1346         my $line = shift;
1347         my $origin = shift;
1348         my $to = $_[1];
1349         my $from = $_[2];
1350         my $flag = $_[3];
1351
1352                         
1353         # is it for us?
1354         if ($to eq $main::mycall) {
1355                 if ($flag == 1) {
1356                         $self->send(pc51($from, $to, '0'));
1357                 } else {
1358                         # it's a reply, look in the ping list for this one
1359                         my $ref = $pings{$from};
1360                         if ($ref) {
1361                                 my $tochan =  DXChannel->get($from);
1362                                 while (@$ref) {
1363                                         my $r = shift @$ref;
1364                                         my $dxchan = DXChannel->get($r->{call});
1365                                         next unless $dxchan;
1366                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
1367                                         if ($dxchan->is_user) {
1368                                                 my $s = sprintf "%.2f", $t; 
1369                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1370                                                 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1371                                         } elsif ($dxchan->is_node) {
1372                                                 if ($tochan) {
1373                                                         my $nopings = $tochan->user->nopings || $obscount;
1374                                                         push @{$tochan->{pingtime}}, $t;
1375                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1376
1377                                                                 # cope with a missed ping, this means you must set the pingint large enough
1378                                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
1379                                                                 $t -= $tochan->{pingint};
1380                                                         }
1381
1382                                                                 # calc smoothed RTT a la TCP
1383                                                         if (@{$tochan->{pingtime}} == 1) {
1384                                                                 $tochan->{pingave} = $t;
1385                                                         } else {
1386                                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1387                                                         }
1388                                                         $tochan->{nopings} = $nopings; # pump up the timer
1389                                                 }
1390                                         } 
1391                                 }
1392                         }
1393                 }
1394         } else {
1395                 if (eph_dup($line)) {
1396                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1397                         return;
1398                 }
1399                 # route down an appropriate thingy
1400                 $self->route($to, $line);
1401         }
1402 }
1403
1404 # New style routing handler
1405 sub handle_59
1406 {
1407         my $self = shift;
1408         my $pcno = shift;
1409         my $line = shift;
1410         my $origin = shift;
1411
1412         return unless eph_dup($line);
1413
1414         my ($sort, $hextime, $ncall) = @_[1,2,3];
1415         if ($ncall eq $main::mycall) {
1416                 dbg("PCPROT: ignoring PC59 for me") if isdbg('chan');
1417                 return;
1418         }
1419
1420         # mark myself as NewRoute if I get a PC59
1421         $self->{newroute} = 1 if $ncall eq $self->{call};
1422
1423         # do this once for filtering with a throwaway routing entry if a new node
1424         my $fnode = Route::Node::get($ncall) || Route::new($ncall);
1425         return unless $self->in_filter_route($fnode);
1426
1427         # now do it properly for actions
1428         my $node = Route::Node::get($ncall) || Route::Node::new($ncall);
1429         $node->newroute(1);
1430
1431         # find each of the entries (or create new ones)
1432         my @refs;
1433         for my $ent (@_[4..-1]) {
1434                 my ($esort, $ehere, $ecall) = unpack "A A A*", $ent;
1435                 my $ref;
1436
1437                 # create user, if required
1438                 my $user = DXUser->get_current($ecall);
1439                 unless ($user) {
1440                         $user = DXUser->new($ecall);
1441                         $user->sort();
1442                         $user->priv(1);         # I have relented and defaulted nodes
1443                         $user->lockout(1);
1444                         $user->homenode($ncall);
1445                         $user->node($ncall);
1446                 }
1447                 if ($esort eq 'U') {
1448                         $ref = Route::User::get($ecall);
1449                         unless ($ref) {
1450                                 # create user, if required
1451                                 my $user = DXUser->get_current($ecall);
1452                                 unless ($user) {
1453                                         $user = DXUser->new($ecall);
1454                                         $user->sort('U');
1455                                         $user->homenode($ncall);
1456                                         $user->node($ncall);
1457                                         $user->put;
1458                                 }
1459                                 $ref = Route::User::new($ecall, 0); 
1460                         }
1461                 } elsif ($esort eq 'N') {
1462                         $ref = Route::Node::get($ecall);
1463                         unless ($ref) {
1464                                 # create user, if required
1465                                 my $user = DXUser->get_current($ecall);
1466                                 unless ($user) {
1467                                         $user = DXUser->new($ecall);
1468                                         $user->priv(1);         # I have relented and defaulted nodes
1469                                         $user->lockout(1);
1470                                         $user->sort('A');
1471                                         $user->homenode($ncall);
1472                                         $user->node($ncall);
1473                                         $user->put;
1474                                 }
1475                                 $ref = Route::Node::new($ecall, 0); 
1476                         } 
1477                 } else {
1478                         dbg("DXPROT: unknown entity type '$esort' on $ecall for node $ncall") if isdbg('chan');
1479                         next;
1480                 }
1481                 $ref->here($ehere);             # might as well set this here
1482                 $ref->lastseen($main::systime);
1483                 push @refs, $ref;
1484         }
1485
1486         # if it is a delete, disconnect all the entries mentioned
1487         # from this node (which is a parent in this context).
1488         my @delnode;
1489         my @deluser;
1490         if ($sort eq 'D') {
1491                 for my $ref (@refs) {
1492                         next if $ref->call eq $ncall;
1493                         if ($ref->isa('Route::Node')) {
1494                                 push @delnode, $node->unlink_node($ref, $self);
1495                         } elsif ($ref->isa('Route::User')) {
1496                                 push @deluser, $node->del_user($ref);
1497                         }
1498                 }
1499         }
1500
1501         # if it is an add, connect all the entries
1502         my @addnode;
1503         my @adduser;
1504         if ($sort eq 'A') {
1505                 for my $ref (@refs) {
1506                         next if $ref->call eq $ncall;
1507                         if ($ref->isa('Route::Node')) {
1508                                 my $new = $node->link_node($ref, $self);
1509                                 push @addnode, $new if $new;
1510                         } elsif ($ref->isa('Route::User')) {
1511                                 push @adduser, $node->del_user($ref);
1512                         }
1513                 }
1514         }
1515
1516         # if it is a configure, unlink all the nodes and users that 
1517         # are not in @refs but are in the node, then add all the
1518         # nodes and users that are @refs but not in the node.
1519         #
1520         if ($sort eq 'C') {
1521                 my @dn;
1522                 my @du;
1523                 my @an;
1524                 my @au;
1525                 for my $r (map {Route::Node::get($_)} $node->nodes) {
1526                         next unless $r;
1527                         push @dn, $r unless grep $_->call eq $r->call, @refs;
1528                 }
1529                 for my $r (map {Route::User::get($_)} $node->users) {
1530                         next unless $r;
1531                         push @du, $r unless grep $_->call eq $r->call, @refs;
1532                 }
1533                 for my $r (@refs) {
1534                         next unless $r;
1535                         if ($r->isa('Route::Node')) {
1536                                 push @an, $r unless grep $r->call eq $_, $node->nodes;
1537                         } elsif ($r->isa('Route::User')) {
1538                                 push @au, $r unless grep $r->call eq $_, $node->users;
1539                         }
1540                 }
1541                 push @delnode, $node->unlink_node($_, $self) for @dn;
1542                 push @deluser, $node->del_user($_) for @du;
1543                 push @addnode, $node->link_node($_, $self) for @an;
1544                 push @adduser, $node->add_user($_) for @au;
1545         }
1546
1547         $self->route_pc21($origin, $line, @delnode) if @delnode;
1548         $self->route_pc19($origin, $line, @addnode) if @addnode;
1549         $self->route_pc17($origin, $line, @deluser) if @deluser;
1550         $self->route_pc16($origin, $line, @adduser) if @adduser;
1551         
1552         $self->route_pc59($sort, $hextime, $ncall, @refs) if @refs;
1553         $_->delete for @delnode, @deluser;
1554 }
1555         
1556
1557 # dunno but route it
1558 sub handle_75
1559 {
1560         my $self = shift;
1561         my $pcno = shift;
1562         my $line = shift;
1563         my $origin = shift;
1564         my $call = $_[1];
1565         if ($call ne $main::mycall) {
1566                 $self->route($call, $line);
1567         }
1568 }
1569
1570 # WCY broadcasts
1571 sub handle_73
1572 {
1573         my $self = shift;
1574         my $pcno = shift;
1575         my $line = shift;
1576         my $origin = shift;
1577         my $call = $_[1];
1578                         
1579         # do some de-duping
1580         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1581         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1582                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1583                 return;
1584         }
1585         @_ = map { unpad($_) } @_;
1586         if (WCY::dup($d)) {
1587                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1588                 return;
1589         }
1590                 
1591         my $wcy = WCY::update($d, @_[2..12]);
1592
1593         my $rep;
1594         eval {
1595                 $rep = Local::wcy($self, @_[1..12]);
1596         };
1597         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1598         return if $rep;
1599
1600         # broadcast to the eager world
1601         send_wcy_spot($self, $line, $d, @_[2..12]);
1602 }
1603
1604 # remote commands (incoming)
1605 sub handle_84
1606 {
1607         my $self = shift;
1608         my $pcno = shift;
1609         my $line = shift;
1610         my $origin = shift;
1611         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1612 }
1613
1614 # remote command replies
1615 sub handle_85
1616 {
1617         my $self = shift;
1618         my $pcno = shift;
1619         my $line = shift;
1620         my $origin = shift;
1621         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1622 }
1623
1624 # if get here then rebroadcast the thing with its Hop count decremented (if
1625 # there is one). If it has a hop count and it decrements to zero then don't
1626 # rebroadcast it.
1627 #
1628 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1629 #        REBROADCAST!!!!
1630 #
1631
1632 sub handle_default
1633 {
1634         my $self = shift;
1635         my $pcno = shift;
1636         my $line = shift;
1637         my $origin = shift;
1638
1639         if (eph_dup($line)) {
1640                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1641         } else {
1642                 unless ($self->{isolate}) {
1643                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1644                 }
1645         }
1646 }
1647
1648 #
1649 # This is called from inside the main cluster processing loop and is used
1650 # for despatching commands that are doing some long processing job
1651 #
1652 sub process
1653 {
1654         my $t = time;
1655         my @dxchan = DXChannel->get_all();
1656         my $dxchan;
1657         my $pc50s;
1658         
1659         # send out a pc50 on EVERY channel all at once
1660         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1661                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
1662                 eph_dup($pc50s);
1663                 $last_pc50 = $t;
1664         }
1665
1666         foreach $dxchan (@dxchan) {
1667                 next unless $dxchan->is_node();
1668                 next if $dxchan == $main::me;
1669
1670                 # send the pc50
1671                 $dxchan->send($pc50s) if $pc50s;
1672                 
1673                 # send a ping out on this channel
1674                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1675                         if ($dxchan->{nopings} <= 0) {
1676                                 $dxchan->disconnect;
1677                         } else {
1678                                 addping($main::mycall, $dxchan->call);
1679                                 $dxchan->{nopings} -= 1;
1680                                 $dxchan->{lastping} = $t;
1681                         }
1682                 }
1683         }
1684
1685         # every ten seconds
1686         if ($t - $last10 >= 10) {       
1687                 # clean out ephemera 
1688
1689                 eph_clean();
1690
1691                 $last10 = $t;
1692         }
1693         
1694         if ($main::systime - 3600 > $last_hour) {
1695                 $last_hour = $main::systime;
1696         }
1697 }
1698
1699 #
1700 # finish up a pc context
1701 #
1702
1703 #
1704 # some active measures
1705 #
1706
1707
1708 sub send_dx_spot
1709 {
1710         my $self = shift;
1711         my $line = shift;
1712         my @dxchan = DXChannel->get_all();
1713         my $dxchan;
1714         
1715         # send it if it isn't the except list and isn't isolated and still has a hop count
1716         # taking into account filtering and so on
1717         foreach $dxchan (@dxchan) {
1718                 next if $dxchan == $main::me;
1719                 next if $dxchan == $self && $self->is_node;
1720                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1721         }
1722 }
1723
1724 sub dx_spot
1725 {
1726         my $self = shift;
1727         my $line = shift;
1728         my $isolate = shift;
1729         my ($filter, $hops);
1730
1731         if ($self->{spotsfilter}) {
1732                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1733                 return unless $filter;
1734         }
1735         send_prot_line($self, $filter, $hops, $isolate, $line);
1736 }
1737
1738 sub send_prot_line
1739 {
1740         my ($self, $filter, $hops, $isolate, $line) = @_;
1741         my $routeit;
1742
1743
1744         if ($hops) {
1745                 $routeit = $line;
1746                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1747         } else {
1748                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1749                 return unless $routeit;
1750         }
1751         if ($filter) {
1752                 $self->send($routeit);
1753         } else {
1754                 $self->send($routeit) unless $self->{isolate} || $isolate;
1755         }
1756 }
1757
1758
1759 sub send_wwv_spot
1760 {
1761         my $self = shift;
1762         my $line = shift;
1763         my @dxchan = DXChannel->get_all();
1764         my $dxchan;
1765         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
1766
1767         # send it if it isn't the except list and isn't isolated and still has a hop count
1768         # taking into account filtering and so on
1769         foreach $dxchan (@dxchan) {
1770                 next if $dxchan == $main::me;
1771                 next if $dxchan == $self && $self->is_node;
1772                 my $routeit;
1773                 my ($filter, $hops);
1774
1775                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1776         }
1777 }
1778
1779 sub wwv
1780 {
1781         my $self = shift;
1782         my $line = shift;
1783         my $isolate = shift;
1784         my ($filter, $hops);
1785         
1786         if ($self->{wwvfilter}) {
1787                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1788                 return unless $filter;
1789         }
1790         send_prot_line($self, $filter, $hops, $isolate, $line)
1791 }
1792
1793 sub send_wcy_spot
1794 {
1795         my $self = shift;
1796         my $line = shift;
1797         my @dxchan = DXChannel->get_all();
1798         my $dxchan;
1799         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
1800         
1801         # send it if it isn't the except list and isn't isolated and still has a hop count
1802         # taking into account filtering and so on
1803         foreach $dxchan (@dxchan) {
1804                 next if $dxchan == $main::me;
1805                 next if $dxchan == $self;
1806
1807                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1808         }
1809 }
1810
1811 sub wcy
1812 {
1813         my $self = shift;
1814         my $line = shift;
1815         my $isolate = shift;
1816         my ($filter, $hops);
1817
1818         if ($self->{wcyfilter}) {
1819                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1820                 return unless $filter;
1821         }
1822         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1823 }
1824
1825 # send an announce
1826 sub send_announce
1827 {
1828         my $self = shift;
1829         my $line = shift;
1830         my @dxchan = DXChannel->get_all();
1831         my $dxchan;
1832         my $target;
1833         my $to = 'To ';
1834         my $text = unpad($_[2]);
1835                                 
1836         if ($_[3] eq '*') {     # sysops
1837                 $target = "SYSOP";
1838         } elsif ($_[3] gt ' ') { # speciality list handling
1839                 my ($name) = split /\./, $_[3]; 
1840                 $target = "$name"; # put the rest in later (if bothered) 
1841         } 
1842         
1843         if ($_[5] eq '1') {
1844                 $target = "WX"; 
1845                 $to = '';
1846         }
1847         $target = "ALL" if !$target;
1848
1849
1850         # obtain country codes etc 
1851         my @a = Prefix::cty_data($_[0]);
1852         my @b = Prefix::cty_data($_[4]);
1853         if ($self->{inannfilter}) {
1854                 my ($filter, $hops) = 
1855                         $self->{inannfilter}->it(@_, $self->{call}, 
1856                                                                          @a[0..2],
1857                                                                          @b[0..2], $a[3], $b[3]);
1858                 unless ($filter) {
1859                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1860                         return;
1861                 }
1862         }
1863
1864         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
1865                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1866                 return;
1867         }
1868
1869         Log('ann', $target, $_[0], $text);
1870
1871         # send it if it isn't the except list and isn't isolated and still has a hop count
1872         # taking into account filtering and so on
1873         foreach $dxchan (@dxchan) {
1874                 next if $dxchan == $main::me;
1875                 next if $dxchan == $self && $self->is_node;
1876                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
1877                                                   @a[0..2], @b[0..2]);
1878         }
1879 }
1880
1881 my $msgid = 0;
1882
1883 sub nextchatmsgid
1884 {
1885         $msgid++;
1886         $msgid = 1 if $msgid > 999;
1887         return $msgid;
1888 }
1889
1890 # send a chat line
1891 sub send_chat
1892 {
1893         my $self = shift;
1894         my $line = shift;
1895         my @dxchan = DXChannel->get_all();
1896         my $dxchan;
1897         my $target = $_[3];
1898         my $text = unpad($_[2]);
1899         my $ak1a_line;
1900                                 
1901         # munge the group and recast the line if required
1902         if ($target =~ s/\.LST$//) {
1903                 $ak1a_line = $line;
1904         }
1905         
1906         # obtain country codes etc 
1907         my @a = Prefix::cty_data($_[0]);
1908         my @b = Prefix::cty_data($_[4]);
1909         if ($self->{inannfilter}) {
1910                 my ($filter, $hops) = 
1911                         $self->{inannfilter}->it(@_, $self->{call}, 
1912                                                                          @a[0..2],
1913                                                                          @b[0..2], $a[3], $b[3]);
1914                 unless ($filter) {
1915                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1916                         return;
1917                 }
1918         }
1919
1920         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
1921                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1922                 return;
1923         }
1924
1925
1926         Log('chat', $target, $_[0], $text);
1927
1928         # send it if it isn't the except list and isn't isolated and still has a hop count
1929         # taking into account filtering and so on
1930         foreach $dxchan (@dxchan) {
1931                 my $is_ak1a = $dxchan->is_ak1a;
1932                 
1933                 if ($dxchan->is_node) {
1934                         next if $dxchan == $main::me;
1935                         next if $dxchan == $self;
1936                         next unless $dxchan->is_spider || $is_ak1a;
1937                         next if $target eq 'LOCAL';
1938                         if (!$ak1a_line && $is_ak1a) {
1939                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
1940                         }
1941                 }
1942                 
1943                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
1944                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
1945         }
1946 }
1947
1948 sub announce
1949 {
1950         my $self = shift;
1951         my $line = shift;
1952         my $isolate = shift;
1953         my $to = shift;
1954         my $target = shift;
1955         my $text = shift;
1956         my ($filter, $hops);
1957
1958         if ($self->{annfilter}) {
1959                 ($filter, $hops) = $self->{annfilter}->it(@_);
1960                 return unless $filter;
1961         }
1962         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
1963 }
1964
1965 sub chat
1966 {
1967         goto &announce;
1968 }
1969
1970
1971 sub send_local_config
1972 {
1973         my $self = shift;
1974         my $node;
1975         my @nodes;
1976         my @localnodes;
1977         my @remotenodes;
1978
1979         dbg('DXProt::send_local_config') if isdbg('trace');
1980
1981         if ($self->{newroute}) {
1982                 my @nodes = $self->{isolate} ? ($main::routeroot) : grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
1983                 my @users = DXChannel::get_all_users();
1984                 $self->send_route($main::mycall, \&pc59, @nodes+@users+4, 'C', 0, $main::mycall, (grep { Route::get($_) } $main::routeroot, @nodes, @users));
1985         } else {
1986                 # send our nodes
1987                 if ($self->{isolate}) {
1988                         @localnodes = ( $main::routeroot );
1989                         $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
1990                 } else {
1991                         # create a list of all the nodes that are not connected to this connection
1992                         # and are not themselves isolated, this to make sure that isolated nodes
1993                         # don't appear outside of this node
1994                         
1995                         # send locally connected nodes
1996                         my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
1997                         @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
1998                         $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
1999                         
2000                         my $node;
2001                         my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
2002                         my @intcalls;
2003                         for $node (@rawintcalls) {
2004                                 push @intcalls, $node unless grep $node eq $_, @intcalls; 
2005                         }
2006                         my $ref = Route::Node::get($self->{call});
2007                         my @rnodes = $ref->nodes;
2008                         for $node (@intcalls) {
2009                                 push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
2010                         }
2011                         $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
2012                 }
2013                 
2014                 # get all the users connected on the above nodes and send them out
2015                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
2016                         if ($node) {
2017                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
2018                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
2019                         } else {
2020                                 dbg("sent a null value") if isdbg('chanerr');
2021                         }
2022                 }
2023         }
2024 }
2025
2026 #
2027 # route a message down an appropriate interface for a callsign
2028 #
2029 # is called route(to, pcline);
2030 #
2031 sub route
2032 {
2033         my ($self, $call, $line) = @_;
2034
2035         if (ref $self && $call eq $self->{call}) {
2036                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2037                 return;
2038         }
2039
2040         # always send it down the local interface if available
2041         my $dxchan = DXChannel->get($call);
2042         unless ($dxchan) {
2043                 my $cl = Route::get($call);
2044                 $dxchan = $cl->dxchan if $cl;
2045                 if (ref $dxchan) {
2046                         if (ref $self && $dxchan eq $self) {
2047                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2048                                 return;
2049                         }
2050                 }
2051         }
2052         if ($dxchan) {
2053                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
2054                 if ($routeit) {
2055                         $dxchan->send($routeit) unless $dxchan == $main::me;
2056                 }
2057         } else {
2058                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
2059         }
2060 }
2061
2062 #
2063 # obtain the hops from the list for this callsign and pc no 
2064 #
2065
2066 sub get_hops
2067 {
2068         my $pcno = shift;
2069         my $hops = $DXProt::hopcount{$pcno};
2070         $hops = $DXProt::def_hopcount if !$hops;
2071         return "H$hops";       
2072 }
2073
2074
2075 # adjust the hop count on a per node basis using the user loadable 
2076 # hop table if available or else decrement an existing one
2077 #
2078
2079 sub adjust_hops
2080 {
2081         my $self = shift;
2082         my $s = shift;
2083         my $call = $self->{call};
2084         my $hops;
2085         
2086         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
2087                 my ($pcno) = $s =~ /^PC(\d\d)/o;
2088                 confess "$call called adjust_hops with '$s'" unless $pcno;
2089                 my $ref = $nodehops{$call} if %nodehops;
2090                 if ($ref) {
2091                         my $newhops = $ref->{$pcno};
2092                         return "" if defined $newhops && $newhops == 0;
2093                         $newhops = $ref->{default} unless $newhops;
2094                         return "" if defined $newhops && $newhops == 0;
2095                         $newhops = $hops if !$newhops;
2096                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
2097                 } else {
2098                         # simply decrement it
2099                         $hops--;
2100                         return "" if !$hops;
2101                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
2102                 }
2103         }
2104         return $s;
2105 }
2106
2107
2108 # load hop tables
2109 #
2110 sub load_hops
2111 {
2112         my $self = shift;
2113         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
2114         do "$main::data/hop_table.pl";
2115         return $@ if $@;
2116         return ();
2117 }
2118
2119
2120 # add a ping request to the ping queues
2121 sub addping
2122 {
2123         my ($from, $to) = @_;
2124         my $ref = $pings{$to} || [];
2125         my $r = {};
2126         $r->{call} = $from;
2127         $r->{t} = [ gettimeofday ];
2128         route(undef, $to, pc51($to, $main::mycall, 1));
2129         push @$ref, $r;
2130         $pings{$to} = $ref;
2131 }
2132
2133 sub process_rcmd
2134 {
2135         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
2136         if ($tonode eq $main::mycall) {
2137                 my $ref = DXUser->get_current($fromnode);
2138                 my $cref = Route::Node::get($fromnode);
2139                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
2140                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
2141                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
2142                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
2143                                 my $oldpriv = $self->{priv};
2144                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
2145                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
2146                                 $self->{priv} = $oldpriv;
2147                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
2148                                 delete $self->{remotecmd};
2149                         } else {
2150                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
2151                         }
2152                 } else {
2153                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
2154                 }
2155         } else {
2156                 my $ref = DXUser->get_current($tonode);
2157                 if ($ref && $ref->is_clx) {
2158                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
2159                 } else {
2160                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
2161                 }
2162         }
2163 }
2164
2165 sub process_rcmd_reply
2166 {
2167         my ($self, $tonode, $fromnode, $user, $line) = @_;
2168         if ($tonode eq $main::mycall) {
2169                 my $s = $rcmds{$fromnode};
2170                 if ($s) {
2171                         my $dxchan = DXChannel->get($s->{call});
2172                         my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
2173                         $ref->send($line) if $ref;
2174                         delete $rcmds{$fromnode} if !$dxchan;
2175                 } else {
2176                         # send unsolicited ones to the sysop
2177                         my $dxchan = DXChannel->get($main::myalias);
2178                         $dxchan->send($line) if $dxchan;
2179                 }
2180         } else {
2181                 my $ref = DXUser->get_current($tonode);
2182                 if ($ref && $ref->is_clx) {
2183                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
2184                 } else {
2185                         $self->route($tonode, pc35($fromnode, $tonode, $line));
2186                 }
2187         }
2188 }
2189
2190 sub send_rcmd_reply
2191 {
2192         my $self = shift;
2193         my $tonode = shift;
2194         my $fromnode = shift;
2195         my $user = shift;
2196         while (@_) {
2197                 my $line = shift;
2198                 $line =~ s/\s*$//;
2199                 Log('rcmd', 'out', $fromnode, $line);
2200                 if ($self->is_clx) {
2201                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
2202                 } else {
2203                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
2204                 }
2205         }
2206 }
2207
2208 # add a rcmd request to the rcmd queues
2209 sub addrcmd
2210 {
2211         my ($self, $to, $cmd) = @_;
2212
2213         my $r = {};
2214         $r->{call} = $self->{call};
2215         $r->{t} = $main::systime;
2216         $r->{cmd} = $cmd;
2217         $rcmds{$to} = $r;
2218         
2219         my $ref = Route::Node::get($to);
2220         my $dxchan = $ref->dxchan;
2221         if ($dxchan && $dxchan->is_clx) {
2222                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
2223         } else {
2224                 route(undef, $to, pc34($main::mycall, $to, $cmd));
2225         }
2226 }
2227
2228 sub disconnect
2229 {
2230         my $self = shift;
2231         my $pc39flag = shift;
2232         my $call = $self->call;
2233
2234         return if $self->{disconnecting}++;
2235         
2236         unless ($pc39flag && $pc39flag == 1) {
2237                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
2238         }
2239
2240         # get rid of any PC16/17/19
2241         eph_del_regex("^PC1[679]*$call");
2242
2243         # do routing stuff, remove me from routing table
2244         my $node = Route::Node::get($call);
2245         my @rout;
2246         my @rusers;
2247         if ($node) {
2248
2249                 # remove the route from this node and return a list
2250                 # of nodes that have become orphanned as a result. 
2251                 push @rout, $main::routeroot->remove_route($node, $self);
2252
2253                 # remove all consequently orphanned users from these nodes
2254                 push @rusers, $_->unlink_all_users for @rout;
2255                 
2256                 # remove all my ephemera as well
2257                 for (@rout) {
2258                         my $c = $_->call;
2259                         eph_del_regex("^PC1[679].*$c");
2260                 }
2261         }
2262         
2263         # unbusy and stop and outgoing mail
2264         my $mref = DXMsg::get_busy($call);
2265         $mref->stop_msg($call) if $mref;
2266         
2267         # broadcast to all other nodes that all the nodes connected to via me are gone
2268         unless ($pc39flag && $pc39flag == 2) {
2269                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
2270                 $self->route_pc59('D', 0, $main::mycall, $node);
2271         }
2272
2273         # delete all the unwanted nodes
2274         $_->delete for @rout, @rusers;
2275         
2276         # remove outstanding pings
2277         delete $pings{$call};
2278         
2279         # I was the last node visited
2280     $self->user->node($main::mycall);
2281
2282         # send info to all logged in thingies
2283         $self->tell_login('logoutn');
2284
2285         Log('DXProt', $call . " Disconnected");
2286
2287         $self->SUPER::disconnect;
2288 }
2289
2290
2291
2292 # send a talk message to this thingy
2293 #
2294 sub talk
2295 {
2296         my ($self, $from, $to, $via, $line, $origin) = @_;
2297         
2298         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
2299         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
2300         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
2301 }
2302
2303 # send it if it isn't the except list and isn't isolated and still has a hop count
2304 # taking into account filtering and so on
2305
2306 sub send_route
2307 {
2308         my $self = shift;
2309         my $origin = shift;
2310         my $generate = shift;
2311         my $no = shift;     # the no of things to filter on 
2312         my $routeit;
2313         my ($filter, $hops);
2314         my @rin;
2315         
2316         for (; @_ && $no; $no--) {
2317                 my $r = shift;
2318
2319                 # deal with non routing parameters
2320                 unless (ref $r && $r->isa('Route')) {
2321                         push @rin, $r;
2322                         next;
2323                 }
2324                 
2325                 if (!$self->{isolate} && $self->{routefilter}) {
2326                         $filter = undef;
2327                         if ($r) {
2328                                 ($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});
2329                                 if ($filter) {
2330                                         push @rin, $r;
2331                                 } else {
2332                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
2333                                 }
2334                         } else {
2335                                 dbg("was sent a null value") if isdbg('chanerr');
2336                         }
2337                 } else {
2338                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
2339                 }
2340         }
2341         if (@rin) {
2342                 foreach my $line (&$generate(@rin, @_)) {
2343                         if ($hops) {
2344                                 $routeit = $line;
2345                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
2346                         } else {
2347                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
2348                                 next unless $routeit;
2349                         }
2350                         
2351                         $self->send($routeit);
2352                 }
2353         }
2354 }
2355
2356 sub broadcast_route
2357 {
2358         my $self = shift;
2359         my $origin = shift;
2360         my $generate = shift;
2361         my $line = shift;
2362         my @dxchan = DXChannel::get_all_nodes();
2363         my $dxchan;
2364         
2365         unless ($self->{isolate}) {
2366                 foreach $dxchan (@dxchan) {
2367                         next if $dxchan == $self;
2368                         next if $dxchan == $main::me;
2369                         next unless $dxchan->isa('DXProt');
2370                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
2371                         next if ($generate == \&pc19 || $generate==\&pc21) && !$dxchan->user->wantsendpc19;
2372                         next if ($generate == \&pc59) && !$dxchan->{newroute};
2373  
2374                         $dxchan->send_route($origin, $generate, @_);
2375                 }
2376         }
2377 }
2378
2379 sub route_pc16
2380 {
2381         my $self = shift;
2382         return unless $self->user->wantpc16;
2383         my $origin = shift;
2384         my $line = shift;
2385         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
2386 }
2387
2388 sub route_pc17
2389 {
2390         my $self = shift;
2391         return unless $self->user->wantpc16;
2392         my $origin = shift;
2393         my $line = shift;
2394         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
2395 }
2396
2397 sub route_pc19
2398 {
2399         my $self = shift;
2400         my $origin = shift;
2401         my $line = shift;
2402         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
2403 }
2404
2405 sub route_pc21
2406 {
2407         my $self = shift;
2408         my $origin = shift;
2409         my $line = shift;
2410         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
2411 }
2412
2413 sub route_pc24
2414 {
2415         my $self = shift;
2416         my $origin = shift;
2417         my $line = shift;
2418         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
2419 }
2420
2421 sub route_pc41
2422 {
2423         my $self = shift;
2424         my $origin = shift;
2425         my $line = shift;
2426         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
2427 }
2428
2429 sub route_pc50
2430 {
2431         my $self = shift;
2432         my $origin = shift;
2433         my $line = shift;
2434         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
2435 }
2436
2437 sub route_pc59
2438 {
2439         my $self = shift;
2440         my $origin = shift;
2441         my $line = shift;
2442
2443         broadcast_route($self, $origin, \&pc59, $line, scalar @_, @_);
2444 }
2445
2446 sub in_filter_route
2447 {
2448         my $self = shift;
2449         my $r = shift;
2450         my ($filter, $hops) = (1, 1);
2451         
2452         if ($self->{inroutefilter}) {
2453                 ($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);
2454                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
2455         }
2456         return $filter;
2457 }
2458
2459 sub eph_dup
2460 {
2461         my $s = shift;
2462         my $t = shift || $eph_restime;
2463         my $r;
2464
2465         # chop the end off
2466         $s =~ s/\^H\d\d?\^?\~?$//;
2467         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
2468         $eph{$s} = $main::systime + $t;
2469         return $r;
2470 }
2471
2472 sub eph_del_regex
2473 {
2474         my $regex = shift;
2475         my ($key, $val);
2476         while (($key, $val) = each %eph) {
2477                 if ($key =~ m{$regex}) {
2478                         delete $eph{$key};
2479                 }
2480         }
2481 }
2482
2483 sub eph_clean
2484 {
2485         my ($key, $val);
2486         
2487         while (($key, $val) = each %eph) {
2488                 if ($main::systime >= $val) {
2489                         delete $eph{$key};
2490                 }
2491         }
2492 }
2493
2494 sub eph_list
2495 {
2496         my ($key, $val);
2497         my @out;
2498
2499         while (($key, $val) = each %eph) {
2500                 push @out, $key, $val;
2501         }
2502         return @out;
2503 }
2504
2505 sub run_cmd
2506 {
2507         goto &DXCommandmode::run_cmd;
2508 }
2509 1;
2510 __END__