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