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