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