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