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