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