fix a load of missing things
[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                                 push @rout, $_->del_node;
611                         }
612                         $self->route_pc21(@rout, $node);
613                         $self->send_local_config();
614                         $self->send(pc20());
615                         return;             # we don't pass these on
616                 }
617                 
618                 if ($pcno == 19) {              # incoming cluster list
619                         my $i;
620                         my $newline = "PC19^";
621
622                         # new routing list
623                         my @rout;
624                         my $node = Route::Node::get($self->{call});
625
626                         # parse the PC19
627                         for ($i = 1; $i < $#field-1; $i += 4) {
628                                 my $here = $field[$i];
629                                 my $call = uc $field[$i+1];
630                                 my $conf = $field[$i+2];
631                                 my $ver = $field[$i+3];
632                                 next unless defined $here && defined $conf && is_callsign($call);
633                                 # check for sane parameters
634                                 $ver = 5000 if $ver eq '0000';
635                                 next if $ver < 5000; # only works with version 5 software
636                                 next if length $call < 3; # min 3 letter callsigns
637
638                                 # update it if required
639                                 if ($node->call eq $call && !$node->version) {
640                                         $node->version($ver);
641                                         $node->flags(Route::here($here)|Route::conf($conf));
642                                         push @rout, $node;
643                                 } elsif ($node->call ne $call) {
644                                         my $r = $node->add($call, $ver, Route::here($here)|Route::conf($conf));
645                                         push @rout, $r if $r;
646                                 }
647
648                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
649                                 my $mref = DXMsg::get_busy($call);
650                                 $mref->stop_msg($call) if $mref;
651                                 
652                                 # add this station to the user database, if required (don't remove SSID from nodes)
653                                 my $user = DXUser->get_current($call);
654                                 if (!$user) {
655                                         $user = DXUser->new($call);
656                                         $user->sort('A');
657                                         $user->priv(1);                   # I have relented and defaulted nodes
658                                         $self->{priv} = 1;                # to user RCMDs allowed
659                                         $user->homenode($call);
660                                         $user->node($call);
661                                 }
662                                 $user->lastin($main::systime) unless DXChannel->get($call);
663                                 $user->put;
664                         }
665
666                         dbg('route', "B/C PC19 for: " . join(',', map{$_->call} @rout)) if @rout;
667                         
668                         $self->route_pc19(@rout) if @rout;
669                         return;
670                 }
671                 
672                 if ($pcno == 20) {              # send local configuration
673                         $self->send_local_config();
674                         $self->send(pc22());
675                         $self->state('normal');
676                         return;
677                 }
678                 
679                 if ($pcno == 21) {              # delete a cluster from the list
680                         my $call = uc $field[1];
681                         my @rout;
682                         my $node = Route::Node::get($call);
683                         
684                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
685                                 if ($call eq $self->{call}) {
686                                         dbg('chan', "PCPROT: Trying to disconnect myself with PC21");
687                                         return;
688                                 }
689
690                                 # routing objects
691                                 if ($node) {
692                                         push @rout, $node->del_node($call);
693                                 } else {
694                                         dbg('chan', "PCPROT: Route::Node $call not in config");
695                                 }
696                         } else {
697                                 dbg('chan', "PCPROT: I WILL _NOT_ be disconnected!");
698                                 return;
699                         }
700                         dbg('route', "B/C PC21 for: " . join(',', (map{$_->call} @rout))) if @rout;
701                         
702                         $self->route_pc21(@rout) if @rout;
703                         return;
704                 }
705                 
706                 if ($pcno == 22) {
707                         $self->state('normal');
708                         return;
709                 }
710                                 
711                 if ($pcno == 23 || $pcno == 27) { # WWV info
712                         
713                         # route 'foreign' pc27s 
714                         if ($pcno == 27) {
715                                 if ($field[8] ne $main::mycall) {
716                                         $self->route($field[8], $line);
717                                         return;
718                                 }
719                         }
720
721                         # do some de-duping
722                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
723                         my $sfi = unpad($field[3]);
724                         my $k = unpad($field[4]);
725                         my $i = unpad($field[5]);
726                         my ($r) = $field[6] =~ /R=(\d+)/;
727                         $r = 0 unless $r;
728                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
729                                 dbg('chan', "PCPROT: WWV Date ($field[1] $field[2]) out of range");
730                                 return;
731                         }
732                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
733                                 dbg('chan', "PCPROT: Dup WWV Spot ignored\n");
734                                 return;
735                         }
736                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
737                 
738                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
739
740                         my $rep;
741                         eval {
742                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
743                         };
744 #                       dbg('local', "Local::wwv2 error $@") if $@;
745                         return if $rep;
746
747                         # DON'T be silly and send on PC27s!
748                         return if $pcno == 27;
749
750                         # broadcast to the eager world
751                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
752                         return;
753                 }
754                 
755                 if ($pcno == 24) {              # set here status
756                         my $call = uc $field[1];
757                         my $ref = Route::Node::get($call);
758                         $ref->here($field[2]) if $ref;
759                         $ref = Route::User::get($call);
760                         $ref->here($field[2]) if $ref;
761                         last SWITCH;
762                 }
763                 
764                 if ($pcno == 25) {      # merge request
765                         if ($field[1] ne $main::mycall) {
766                                 $self->route($field[1], $line);
767                                 return;
768                         }
769                         if ($field[2] eq $main::mycall) {
770                                 dbg('chan', "PCPROT: Trying to merge to myself, ignored");
771                                 return;
772                         }
773
774                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[1]");
775                         
776                         # spots
777                         if ($field[3] > 0) {
778                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
779                                 my $in;
780                                 foreach $in (@in) {
781                                         $self->send(pc26(@{$in}[0..4], $field[2]));
782                                 }
783                         }
784
785                         # wwv
786                         if ($field[4] > 0) {
787                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
788                                 my $in;
789                                 foreach $in (@in) {
790                                         $self->send(pc27(@{$in}[0..5], $field[2]));
791                                 }
792                         }
793                         return;
794                 }
795
796                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
797                         if ($pcno == 49 || $field[1] eq $main::mycall) {
798                                 DXMsg::process($self, $line);
799                         } else {
800                                 $self->route($field[1], $line) unless $self->is_clx;
801                         }
802                         return;
803                 }
804                 
805                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
806                         if ($field[1] eq $main::mycall) {
807                                 my $ref = DXUser->get_current($field[2]);
808                                 my $cref = Route::Node::get($field[2]);
809                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[3]);
810                                 unless (!$cref || !$ref || $cref->call ne $ref->homenode) {    # not allowed to relay RCMDS!
811                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
812                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
813                                                 my $oldpriv = $self->{priv};
814                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
815                                                 my @in = (DXCommandmode::run_cmd($self, $field[3]));
816                                                 $self->{priv} = $oldpriv;
817                                                 for (@in) {
818                                                         s/\s*$//og;
819                                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:$_"));
820                                                         Log('rcmd', 'out', $field[2], $_);
821                                                 }
822                                                 delete $self->{remotecmd};
823                                         } else {
824                                                 $self->send(pc35($main::mycall, $field[2], "$main::mycall:sorry...!"));
825                                         }
826                                 } else {
827                                         $self->send(pc35($main::mycall, $field[2], "$main::mycall:your attempt is logged, Tut tut tut...!"));
828                                 }
829                         } else {
830                                 my $ref = DXUser->get_current($field[1]);
831                                 if ($ref && $ref->is_clx) {
832                                         $self->route($field[1], pc84($field[2], $field[1], $field[2], $field[3]));
833                                 } else {
834                                         $self->route($field[1], $line);
835                                 }
836                         }
837                         return;
838                 }
839                 
840                 if ($pcno == 35) {              # remote command replies
841                         if ($field[1] eq $main::mycall) {
842                                 my $s = $rcmds{$field[2]};
843                                 if ($s) {
844                                         my $dxchan = DXChannel->get($s->{call});
845                                         $dxchan->send($field[3]) if $dxchan;
846                                         delete $rcmds{$field[2]} if !$dxchan;
847                                 } else {
848                                         # send unsolicited ones to the sysop
849                                         my $dxchan = DXChannel->get($main::myalias);
850                                         $dxchan->send($field[3]) if $dxchan;
851                                 }
852                         } else {
853                                 my $ref = DXUser->get_current($field[1]);
854                                 if ($ref && $ref->is_clx) {
855                                         $self->route($field[1], pc85($field[2], $field[1], $field[2], $field[3]));
856                                 } else {
857                                         $self->route($field[1], $line);
858                                 }
859                         }
860                         return;
861                 }
862                 
863                 # for pc 37 see 44 onwards
864
865                 if ($pcno == 38) {              # node connected list from neighbour
866                         return;
867                 }
868                 
869                 if ($pcno == 39) {              # incoming disconnect
870                         if ($field[1] eq $self->{call}) {
871                                 $self->disconnect(1);
872                         } else {
873                                 dbg('chan', "PCPROT: came in on wrong channel");
874                         }
875                         return;
876                 }
877                 
878                 if ($pcno == 41) {              # user info
879                         # add this station to the user database, if required
880                         my $user = DXUser->get_current($field[1]);
881                         $user = DXUser->new($field[1]) if !$user;
882                         
883                         if ($field[2] == 1) {
884                                 $user->name($field[3]);
885                         } elsif ($field[2] == 2) {
886                                 $user->qth($field[3]);
887                         } elsif ($field[2] == 3) {
888                                 my ($lat, $long) = DXBearing::stoll($field[3]);
889                                 $user->lat($lat);
890                                 $user->long($long);
891                                 $user->qra(DXBearing::lltoqra($lat, $long)) unless $user->qra && DXBearing::is_qra($user->qra);
892                         } elsif ($field[2] == 4) {
893                                 $user->homenode($field[3]);
894                         }
895                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
896                         $user->put;
897                         last SWITCH;
898                 }
899                 if ($pcno == 43) {
900                         last SWITCH;
901                 }
902                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
903                         DXDb::process($self, $line);
904                         return;
905                 }
906                 
907                 if ($pcno == 50) {              # keep alive/user list
908                         my $node = Route::Node::get($field[1]);
909                         if ($node) {
910                                 return unless $node->call eq $self->{call};
911                                 $node->usercount($field[2]);
912                         }
913                         last SWITCH;
914                 }
915                 
916                 if ($pcno == 51) {              # incoming ping requests/answers
917                         
918                         # is it for us?
919                         if ($field[1] eq $main::mycall) {
920                                 my $flag = $field[3];
921                                 if ($flag == 1) {
922                                         $self->send(pc51($field[2], $field[1], '0'));
923                                 } else {
924                                         # it's a reply, look in the ping list for this one
925                                         my $ref = $pings{$field[2]};
926                                         if ($ref) {
927                                                 my $tochan =  DXChannel->get($field[2]);
928                                                 while (@$ref) {
929                                                         my $r = shift @$ref;
930                                                         my $dxchan = DXChannel->get($r->{call});
931                                                         next unless $dxchan;
932                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
933                                                         if ($dxchan->is_user) {
934                                                                 my $s = sprintf "%.2f", $t; 
935                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
936                                                                 $dxchan->send($dxchan->msg('pingi', $field[2], $s, $ave))
937                                                         } elsif ($dxchan->is_node) {
938                                                                 if ($tochan) {
939                                                                         $tochan->{nopings} = $tochan->user->nopings || 2; # pump up the timer
940                                                                         push @{$tochan->{pingtime}}, $t;
941                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
942                                                                         my $st;
943                                                                         for (@{$tochan->{pingtime}}) {
944                                                                                 $st += $_;
945                                                                         }
946                                                                         $tochan->{pingave} = $st / @{$tochan->{pingtime}};
947                                                                 }
948                                                         } 
949                                                 }
950                                         }
951                                 }
952                         } else {
953                                 # route down an appropriate thingy
954                                 $self->route($field[1], $line);
955                         }
956                         return;
957                 }
958
959                 if ($pcno == 75) {              # dunno but route it
960                         if ($field[1] ne $main::mycall) {
961                                 $self->route($field[1], $line);
962                         }
963                         return;
964                 }
965
966                 if ($pcno == 73) {  # WCY broadcasts
967                         
968                         # do some de-duping
969                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
970                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
971                                 dbg('chan', "PCPROT: WCY Date ($field[1] $field[2]) out of range");
972                                 return;
973                         }
974                         @field = map { unpad($_) } @field;
975                         if (WCY::dup($d,@field[3..7])) {
976                                 dbg('chan', "PCPROT: Dup WCY Spot ignored\n");
977                                 return;
978                         }
979                 
980                         my $wcy = WCY::update($d, @field[2..12]);
981
982                         my $rep;
983                         eval {
984                                 $rep = Local::wwv($self, @field[1..12]);
985                         };
986                         # dbg('local', "Local::wcy error $@") if $@;
987                         return if $rep;
988
989                         # broadcast to the eager world
990                         send_wcy_spot($self, $line, $d, @field[2..12]);
991                         return;
992                 }
993
994                 if ($pcno == 84) { # remote commands (incoming)
995                         if ($field[1] eq $main::mycall) {
996                                 my $ref = DXUser->get_current($field[2]);
997                                 my $cref = Route::Node::get($field[2]);
998                                 Log('rcmd', 'in', $ref->{priv}, $field[2], $field[4]);
999                                 unless ($field[4] =~ /rcmd/i || !$cref || !$ref || $cref->call ne $ref->homenode) {    # not allowed to relay RCMDS!
1000                                         if ($ref->{priv}) {     # you have to have SOME privilege, the commands have further filtering
1001                                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1002                                                 my $oldpriv = $self->{priv};
1003                                                 $self->{priv} = $ref->{priv};     # assume the user's privilege level
1004                                                 my @in = (DXCommandmode::run_cmd($self, $field[4]));
1005                                                 $self->{priv} = $oldpriv;
1006                                                 for (@in) {
1007                                                         s/\s*$//og;
1008                                                         $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:$_"));
1009                                                         Log('rcmd', 'out', $field[2], $_);
1010                                                 }
1011                                                 delete $self->{remotecmd};
1012                                         } else {
1013                                                 $self->send(pc85($main::mycall, $field[2], $field[3], "$main::mycall:sorry...!"));
1014                                         }
1015                                 } else {
1016                                         $self->send(pc85($main::mycall, $field[2], $field[3],"$main::mycall:your attempt is logged, Tut tut tut...!"));
1017                                 }
1018                         } else {
1019                                 my $ref = DXUser->get_current($field[1]);
1020                                 if ($ref && $ref->is_clx) {
1021                                         $self->route($field[1], $line);
1022                                 } else {
1023                                         $self->route($field[1], pc34($field[2], $field[1], $field[4]));
1024                                 }
1025                         }
1026                         return;
1027                 }
1028
1029                 if ($pcno == 85) {              # remote command replies
1030                         if ($field[1] eq $main::mycall) {
1031                                 my $dxchan = DXChannel->get($field[3]);
1032                                 if ($dxchan) {
1033                                         $dxchan->send($field[4]);
1034                                 } else {
1035                                         my $s = $rcmds{$field[2]};
1036                                         if ($s) {
1037                                                 $dxchan = DXChannel->get($s->{call});
1038                                                 $dxchan->send($field[4]) if $dxchan;
1039                                                 delete $rcmds{$field[2]} if !$dxchan;
1040                                         } else {
1041                                                 # send unsolicited ones to the sysop
1042                                                 my $dxchan = DXChannel->get($main::myalias);
1043                                                 $dxchan->send($field[4]) if $dxchan;
1044                                         }
1045                                 }
1046                         } else {
1047                                 my $ref = DXUser->get_current($field[1]);
1048                                 if ($ref && $ref->is_clx) {
1049                                         $self->route($field[1], $line);
1050                                 } else {
1051                                         $self->route($field[1], pc35($field[2], $field[1], $field[4]));
1052                                 }
1053                         }
1054                         return;
1055                 }
1056         }
1057          
1058         # if get here then rebroadcast the thing with its Hop count decremented (if
1059         # there is one). If it has a hop count and it decrements to zero then don't
1060         # rebroadcast it.
1061         #
1062         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1063         #        REBROADCAST!!!!
1064         #
1065          
1066         unless ($self->{isolate}) {
1067                 broadcast_ak1a($line, $self); # send it to everyone but me
1068         }
1069 }
1070
1071 #
1072 # This is called from inside the main cluster processing loop and is used
1073 # for despatching commands that are doing some long processing job
1074 #
1075 sub process
1076 {
1077         my $t = time;
1078         my @dxchan = DXChannel->get_all();
1079         my $dxchan;
1080         
1081         foreach $dxchan (@dxchan) {
1082                 next unless $dxchan->is_node();
1083                 next if $dxchan == $me;
1084                 
1085                 # send a pc50 out on this channel
1086                 $dxchan->{pc50_t} = $main::systime unless exists $dxchan->{pc50_t};
1087                 if ($t >= $dxchan->{pc50_t} + $DXProt::pc50_interval) {
1088                         $dxchan->send(pc50(scalar DXChannel::get_all_users));
1089                         $dxchan->{pc50_t} = $t;
1090                 } 
1091
1092                 # send a ping out on this channel
1093                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1094                         if ($dxchan->{nopings} <= 0) {
1095                                 $dxchan->disconnect;
1096                         } else {
1097                                 addping($main::mycall, $dxchan->call);
1098                                 $dxchan->{nopings} -= 1;
1099                                 $dxchan->{lastping} = $t;
1100                         }
1101                 }
1102         }
1103         
1104         my $key;
1105         my $val;
1106         my $cutoff;
1107         if ($main::systime - 3600 > $last_hour) {
1108 #               Spot::process;
1109 #               Geomag::process;
1110 #               AnnTalk::process;
1111                 $last_hour = $main::systime;
1112         }
1113 }
1114
1115 #
1116 # finish up a pc context
1117 #
1118
1119 #
1120 # some active measures
1121 #
1122
1123 sub send_dx_spot
1124 {
1125         my $self = shift;
1126         my $line = shift;
1127         my @dxchan = DXChannel->get_all();
1128         my $dxchan;
1129         
1130         # send it if it isn't the except list and isn't isolated and still has a hop count
1131         # taking into account filtering and so on
1132         foreach $dxchan (@dxchan) {
1133                 next if $dxchan == $me;
1134                 my $routeit;
1135                 my ($filter, $hops);
1136
1137                 if ($dxchan->{spotsfilter}) {
1138                     ($filter, $hops) = $dxchan->{spotsfilter}->it(@_, $self->{call} );
1139                         next unless $filter;
1140                 }
1141                 
1142                 if ($dxchan->is_node) {
1143                         next if $dxchan == $self;
1144                         if ($hops) {
1145                                 $routeit = $line;
1146                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1147                         } else {
1148                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1149                                 next unless $routeit;
1150                         }
1151                         if ($filter) {
1152                                 $dxchan->send($routeit) if $routeit;
1153                         } else {
1154                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1155                         }
1156                 } elsif ($dxchan->is_user && $dxchan->{dx}) {
1157                         my $buf = Spot::formatb($dxchan->{user}->wantgrid, $_[0], $_[1], $_[2], $_[3], $_[4]);
1158                         $buf .= "\a\a" if $dxchan->{beep};
1159                         $buf =~ s/\%5E/^/g;
1160                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1161                                 $dxchan->send($buf);
1162                         } else {
1163                                 $dxchan->delay($buf);
1164                         }
1165                 }                                       
1166         }
1167 }
1168
1169 sub send_wwv_spot
1170 {
1171         my $self = shift;
1172         my $line = shift;
1173         my @dxchan = DXChannel->get_all();
1174         my $dxchan;
1175         my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1176         my @dxcc = Prefix::extract($_[7]);
1177         if (@dxcc > 0) {
1178                 $wwv_dxcc = $dxcc[1]->dxcc;
1179                 $wwv_itu = $dxcc[1]->itu;
1180                 $wwv_cq = $dxcc[1]->cq;                                         
1181         }
1182         @dxcc = Prefix::extract($_[8]);
1183         if (@dxcc > 0) {
1184                 $org_dxcc = $dxcc[1]->dxcc;
1185                 $org_itu = $dxcc[1]->itu;
1186                 $org_cq = $dxcc[1]->cq;                                         
1187         }
1188         
1189         # send it if it isn't the except list and isn't isolated and still has a hop count
1190         # taking into account filtering and so on
1191         foreach $dxchan (@dxchan) {
1192                 next if $dxchan == $self;
1193                 next if $dxchan == $me;
1194                 my $routeit;
1195                 my ($filter, $hops);
1196
1197                 if ($dxchan->{wwvfilter}) {
1198                         ($filter, $hops) = $dxchan->{wwvfilter}->it(@_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1199                          next unless $filter;
1200                 }
1201                 if ($dxchan->is_node) {
1202                         if ($hops) {
1203                                 $routeit = $line;
1204                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1205                         } else {
1206                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1207                                 next unless $routeit;
1208                         }
1209                         if ($filter) {
1210                                 $dxchan->send($routeit) if $routeit;
1211                         } else {
1212                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1213                                 
1214                         }
1215                 } elsif ($dxchan->is_user && $dxchan->{wwv}) {
1216                         my $buf = "WWV de $_[6] <$_[1]>:   SFI=$_[2], A=$_[3], K=$_[4], $_[5]";
1217                         $buf .= "\a\a" if $dxchan->{beep};
1218                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1219                                 $dxchan->send($buf);
1220                         } else {
1221                                 $dxchan->delay($buf);
1222                         }
1223                 }                                       
1224         }
1225 }
1226
1227 sub send_wcy_spot
1228 {
1229         my $self = shift;
1230         my $line = shift;
1231         my @dxchan = DXChannel->get_all();
1232         my $dxchan;
1233         my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1234         my @dxcc = Prefix::extract($_[11]);
1235         if (@dxcc > 0) {
1236                 $wcy_dxcc = $dxcc[1]->dxcc;
1237                 $wcy_itu = $dxcc[1]->itu;
1238                 $wcy_cq = $dxcc[1]->cq;                                         
1239         }
1240         @dxcc = Prefix::extract($_[12]);
1241         if (@dxcc > 0) {
1242                 $org_dxcc = $dxcc[1]->dxcc;
1243                 $org_itu = $dxcc[1]->itu;
1244                 $org_cq = $dxcc[1]->cq;                                         
1245         }
1246         
1247         # send it if it isn't the except list and isn't isolated and still has a hop count
1248         # taking into account filtering and so on
1249         foreach $dxchan (@dxchan) {
1250                 next if $dxchan == $me;
1251                 my $routeit;
1252                 my ($filter, $hops);
1253
1254                 if ($dxchan->{wcyfilter}) {
1255                         ($filter, $hops) = $dxchan->{wcyfilter}->it(@_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1256                          next unless $filter;
1257                 }
1258                 if ($dxchan->is_clx || $dxchan->is_spider || $dxchan->is_dxnet) {
1259                         if ($hops) {
1260                                 $routeit = $line;
1261                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1262                         } else {
1263                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1264                                 next unless $routeit;
1265                         }
1266                         if ($filter) {
1267                                 $dxchan->send($routeit) if $routeit;
1268                         } else {
1269                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1270                         }
1271                 } elsif ($dxchan->is_user && $dxchan->{wcy}) {
1272                         my $buf = "WCY de $_[10] <$_[1]> : K=$_[4] expK=$_[5] A=$_[3] R=$_[6] SFI=$_[2] SA=$_[7] GMF=$_[8] Au=$_[9]";
1273                         $buf .= "\a\a" if $dxchan->{beep};
1274                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1275                                 $dxchan->send($buf);
1276                         } else {
1277                                 $dxchan->delay($buf);
1278                         }
1279                 }                                       
1280         }
1281 }
1282
1283 # send an announce
1284 sub send_announce
1285 {
1286         my $self = shift;
1287         my $line = shift;
1288         my @dxchan = DXChannel->get_all();
1289         my $dxchan;
1290         my $text = unpad($_[2]);
1291         my $target;
1292         my $to = 'To ';
1293                                 
1294         if ($_[3] eq '*') {     # sysops
1295                 $target = "SYSOP";
1296         } elsif ($_[3] gt ' ') { # speciality list handling
1297                 my ($name) = split /\./, $_[3]; 
1298                 $target = "$name"; # put the rest in later (if bothered) 
1299         } 
1300         
1301         if ($_[5] eq '1') {
1302                 $target = "WX"; 
1303                 $to = '';
1304         }
1305         $target = "ALL" if !$target;
1306         
1307         Log('ann', $target, $_[0], $text);
1308
1309         # obtain country codes etc 
1310         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1311         my @dxcc = Prefix::extract($_[0]);
1312         if (@dxcc > 0) {
1313                 $ann_dxcc = $dxcc[1]->dxcc;
1314                 $ann_itu = $dxcc[1]->itu;
1315                 $ann_cq = $dxcc[1]->cq;                                         
1316         }
1317         @dxcc = Prefix::extract($_[4]);
1318         if (@dxcc > 0) {
1319                 $org_dxcc = $dxcc[1]->dxcc;
1320                 $org_itu = $dxcc[1]->itu;
1321                 $org_cq = $dxcc[1]->cq;                                         
1322         }
1323
1324         # send it if it isn't the except list and isn't isolated and still has a hop count
1325         # taking into account filtering and so on
1326         foreach $dxchan (@dxchan) {
1327                 next if $dxchan == $self;
1328                 next if $dxchan == $me;
1329                 my $routeit;
1330                 my ($filter, $hops);
1331
1332                 if ($dxchan->{annfilter}) {
1333                         ($filter, $hops) = $dxchan->{annfilter}->it(@_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1334                         next unless $filter;
1335                 } 
1336                 if ($dxchan->is_node && $_[1] ne $main::mycall) {  # i.e not specifically routed to me
1337                         if ($hops) {
1338                                 $routeit = $line;
1339                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1340                         } else {
1341                                 $routeit = adjust_hops($dxchan, $line);  # adjust its hop count by node name
1342                                 next unless $routeit;
1343                         }
1344                         if ($filter) {
1345                                 $dxchan->send($routeit) if $routeit;
1346                         } else {
1347                                 $dxchan->send($routeit) unless $dxchan->{isolate} || $self->{isolate};
1348                                 
1349                         }
1350                 } elsif ($dxchan->is_user) {
1351                         unless ($dxchan->{ann}) {
1352                                 next if $_[0] ne $main::myalias && $_[0] ne $main::mycall;
1353                         }
1354                         next if $target eq 'SYSOP' && $dxchan->{priv} < 5;
1355                         my $buf = "$to$target de $_[0]: $text";
1356                         $buf =~ s/\%5E/^/g;
1357                         $buf .= "\a\a" if $dxchan->{beep};
1358                         if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1359                                 $dxchan->send($buf);
1360                         } else {
1361                                 $dxchan->delay($buf);
1362                         }
1363                 }                                       
1364         }
1365 }
1366
1367 sub send_local_config
1368 {
1369         my $self = shift;
1370         my $n;
1371         my @nodes;
1372         my @localcalls;
1373         my @remotecalls;
1374                 
1375         # send our nodes
1376         if ($self->{isolate}) {
1377                 @localcalls = ( $main::mycall );
1378         } else {
1379                 # create a list of all the nodes that are not connected to this connection
1380                 # and are not themselves isolated, this to make sure that isolated nodes
1381         # don't appear outside of this node
1382                 my @dxchan = grep { $_->call ne $main::mycall && $_->call ne $self->{call} && !$_->{isolate} } DXChannel::get_all_nodes();
1383                 @localcalls = map { $_->{call} } @dxchan if @dxchan;
1384                 @remotecalls = map {my $r = Route::Node::get($_); $r ? $r->rnodes(@localcalls, $main::mycall, $self->{call}) : () } @localcalls if @localcalls;
1385                 unshift @localcalls, $main::mycall;
1386         }
1387         @nodes = map {my $r = Route::Node::get($_); $r ? $r : ()} (@localcalls, @remotecalls);
1388         
1389         send_route($self, \&pc19, scalar @nodes, @nodes);
1390         
1391         # get all the users connected on the above nodes and send them out
1392         foreach $n (@nodes) {
1393                 send_route($self, \&pc16, 1, $n, map {my $r = Route::User::get($_); $r ? ($r) : ()} $n->users);
1394         }
1395 }
1396
1397 #
1398 # route a message down an appropriate interface for a callsign
1399 #
1400 # is called route(to, pcline);
1401 #
1402 sub route
1403 {
1404         my ($self, $call, $line) = @_;
1405
1406         if (ref $self && $call eq $self->{call}) {
1407                 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1408                 return;
1409         }
1410
1411         # always send it down the local interface if available
1412         my $dxchan = DXChannel->get($call);
1413         unless ($dxchan) {
1414                 my $cl = Route::Node::get($call);
1415                 $dxchan = $cl->dxchan if $cl;
1416                 if (ref $dxchan) {
1417                         if (ref $self && $dxchan eq $self) {
1418                                 dbg('chan', "PCPROT: Trying to route back to source, dropped");
1419                                 return;
1420                         }
1421                 }
1422         }
1423         if ($dxchan) {
1424                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1425                 if ($routeit) {
1426                         $dxchan->send($routeit);
1427                 }
1428         } else {
1429                 dbg('chan', "PCPROT: No route available, dropped");
1430         }
1431 }
1432
1433 # broadcast a message to all clusters taking into account isolation
1434 # [except those mentioned after buffer]
1435 sub broadcast_ak1a
1436 {
1437         my $s = shift;                          # the line to be rebroadcast
1438         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1439         my @dxchan = DXChannel::get_all_nodes();
1440         my $dxchan;
1441         
1442         # send it if it isn't the except list and isn't isolated and still has a hop count
1443         foreach $dxchan (@dxchan) {
1444                 next if grep $dxchan == $_, @except;
1445                 next if $dxchan == $me;
1446                 
1447                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1448                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1449         }
1450 }
1451
1452 # broadcast a message to all clusters ignoring isolation
1453 # [except those mentioned after buffer]
1454 sub broadcast_all_ak1a
1455 {
1456         my $s = shift;                          # the line to be rebroadcast
1457         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1458         my @dxchan = DXChannel::get_all_nodes();
1459         my $dxchan;
1460         
1461         # send it if it isn't the except list and isn't isolated and still has a hop count
1462         foreach $dxchan (@dxchan) {
1463                 next if grep $dxchan == $_, @except;
1464                 next if $dxchan == $me;
1465
1466                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1467                 $dxchan->send($routeit);
1468         }
1469 }
1470
1471 # broadcast to all users
1472 # storing the spot or whatever until it is in a state to receive it
1473 sub broadcast_users
1474 {
1475         my $s = shift;                          # the line to be rebroadcast
1476         my $sort = shift;           # the type of transmission
1477         my $fref = shift;           # a reference to an object to filter on
1478         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1479         my @dxchan = DXChannel::get_all_users();
1480         my $dxchan;
1481         my @out;
1482         
1483         foreach $dxchan (@dxchan) {
1484                 next if grep $dxchan == $_, @except;
1485                 push @out, $dxchan;
1486         }
1487         broadcast_list($s, $sort, $fref, @out);
1488 }
1489
1490 # broadcast to a list of users
1491 sub broadcast_list
1492 {
1493         my $s = shift;
1494         my $sort = shift;
1495         my $fref = shift;
1496         my $dxchan;
1497         
1498         foreach $dxchan (@_) {
1499                 my $filter = 1;
1500                 next if $dxchan == $me;
1501                 
1502                 if ($sort eq 'dx') {
1503                     next unless $dxchan->{dx};
1504                         ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1505                         next unless $filter;
1506                 }
1507                 next if $sort eq 'ann' && !$dxchan->{ann};
1508                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1509                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1510                 next if $sort eq 'wx' && !$dxchan->{wx};
1511
1512                 $s =~ s/\a//og unless $dxchan->{beep};
1513
1514                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1515                         $dxchan->send($s);      
1516                 } else {
1517                         $dxchan->delay($s);
1518                 }
1519         }
1520 }
1521
1522
1523 #
1524 # obtain the hops from the list for this callsign and pc no 
1525 #
1526
1527 sub get_hops
1528 {
1529         my $pcno = shift;
1530         my $hops = $DXProt::hopcount{$pcno};
1531         $hops = $DXProt::def_hopcount if !$hops;
1532         return "H$hops";       
1533 }
1534
1535
1536 # adjust the hop count on a per node basis using the user loadable 
1537 # hop table if available or else decrement an existing one
1538 #
1539
1540 sub adjust_hops
1541 {
1542         my $self = shift;
1543         my $s = shift;
1544         my $call = $self->{call};
1545         my $hops;
1546         
1547         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1548                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1549                 confess "$call called adjust_hops with '$s'" unless $pcno;
1550                 my $ref = $nodehops{$call} if %nodehops;
1551                 if ($ref) {
1552                         my $newhops = $ref->{$pcno};
1553                         return "" if defined $newhops && $newhops == 0;
1554                         $newhops = $ref->{default} unless $newhops;
1555                         return "" if defined $newhops && $newhops == 0;
1556                         $newhops = $hops if !$newhops;
1557                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1558                 } else {
1559                         # simply decrement it
1560                         $hops--;
1561                         return "" if !$hops;
1562                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1563                 }
1564         }
1565         return $s;
1566 }
1567
1568
1569 # load hop tables
1570 #
1571 sub load_hops
1572 {
1573         my $self = shift;
1574         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1575         do "$main::data/hop_table.pl";
1576         return $@ if $@;
1577         return 0;
1578 }
1579
1580
1581 # add a ping request to the ping queues
1582 sub addping
1583 {
1584         my ($from, $to) = @_;
1585         my $ref = $pings{$to} || [];
1586         my $r = {};
1587         $r->{call} = $from;
1588         $r->{t} = [ gettimeofday ];
1589         route(undef, $to, pc51($to, $main::mycall, 1));
1590         push @$ref, $r;
1591         $pings{$to} = $ref;
1592 }
1593
1594 # add a rcmd request to the rcmd queues
1595 sub addrcmd
1596 {
1597         my ($self, $to, $cmd) = @_;
1598
1599         my $r = {};
1600         $r->{call} = $self->{call};
1601         $r->{t} = $main::systime;
1602         $r->{cmd} = $cmd;
1603         $rcmds{$to} = $r;
1604
1605         my $ref = Route::Node::get($to);
1606         my $dxchan = $ref->dxchan;
1607     if ($dxchan && $dxchan->is_clx) {
1608                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1609         } else {
1610                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1611         }
1612 }
1613
1614 sub disconnect
1615 {
1616         my $self = shift;
1617         my $pc39flag = shift;
1618         my $call = $self->call;
1619
1620         unless ($pc39flag && $pc39flag == 1) {
1621                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1622         }
1623
1624         # do routing stuff
1625 #       my $node = Route::Node::get($self->{call});
1626 #       my @rout = $node->del_nodes if $node;
1627         my @rout = $main::routeroot->del_node($call);
1628         dbg('route', "B/C PC21 (from PC39) for: " . join(',', (map{ $_->call } @rout))) if @rout;
1629         
1630         # unbusy and stop and outgoing mail
1631         my $mref = DXMsg::get_busy($call);
1632         $mref->stop_msg($call) if $mref;
1633         
1634         # broadcast to all other nodes that all the nodes connected to via me are gone
1635         unless ($pc39flag && $pc39flag == 2) {
1636                 $self->route_pc21(@rout) if @rout;
1637         }
1638
1639         # remove outstanding pings
1640         delete $pings{$call};
1641         
1642         # I was the last node visited
1643     $self->user->node($main::mycall);
1644
1645         # send info to all logged in thingies
1646         $self->tell_login('logoutn');
1647
1648         Log('DXProt', $call . " Disconnected");
1649
1650         $self->SUPER::disconnect;
1651 }
1652
1653
1654
1655 # send a talk message to this thingy
1656 #
1657 sub talk
1658 {
1659         my ($self, $from, $to, $via, $line) = @_;
1660         
1661         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1662         $self->send(DXProt::pc10($from, $to, $via, $line));
1663         Log('talk', $self->call, $from, $via?$via:$main::mycall, $line);
1664 }
1665
1666 # send it if it isn't the except list and isn't isolated and still has a hop count
1667 # taking into account filtering and so on
1668 sub send_route
1669 {
1670         my $self = shift;
1671         my $generate = shift;
1672         my $no = shift;     # the no of things to filter on 
1673         my $routeit;
1674         my ($filter, $hops);
1675         my @rin;
1676         
1677         if ($self->{routefilter}) {
1678                 for (; @_ && $no; $no--) {
1679                         my $r = shift;
1680                         ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1681                         push @rin, $r if $filter;
1682                 }
1683         }
1684         if (@rin) {
1685                 foreach my $line (&$generate(@rin, @_)) {
1686                         if ($hops) {
1687                                 $routeit = $line;
1688                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1689                         } else {
1690                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1691                                 next unless $routeit;
1692                         }
1693                         $self->send($routeit) unless $self->{isolate} || $self->{isolate};
1694                 }
1695         }
1696 }
1697
1698 sub broadcast_route
1699 {
1700         my $self = shift;
1701         my $generate = shift;
1702         my @dxchan = DXChannel::get_all_nodes();
1703         my $dxchan;
1704         my $line;
1705         
1706         foreach $dxchan (@dxchan) {
1707                 next if $dxchan == $self;
1708                 next if $dxchan == $me;
1709                 $dxchan->send_route($generate, @_);
1710         }
1711 }
1712
1713 sub route_pc16
1714 {
1715         my $self = shift;
1716         broadcast_route($self, \&pc16, 1, @_);
1717 }
1718
1719 sub route_pc17
1720 {
1721         my $self = shift;
1722         broadcast_route($self, \&pc17, 1, @_);
1723 }
1724
1725 sub route_pc19
1726 {
1727         my $self = shift;
1728         broadcast_route($self, \&pc19, scalar @_, @_);
1729 }
1730
1731 sub route_pc21
1732 {
1733         my $self = shift;
1734         broadcast_route($self, \&pc21, scalar @_, @_);
1735 }
1736
1737 1;
1738 __END__