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