start PC90 development
[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 use Script;
36
37 use strict;
38
39 use vars qw($VERSION $BRANCH);
40 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
41 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
42 $main::build += $VERSION;
43 $main::branch += $BRANCH;
44
45 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
46                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
47                         $pingint $obscount %pc19list $chatdupeage
48                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
49                         $allowzero $decode_dk0wcy $send_opernam @checklist);
50
51 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
52 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
53
54 $last_hour = time;                              # last time I did an hourly periodic update
55 %pings = ();                    # outstanding ping requests outbound
56 %rcmds = ();                    # outstanding rcmd requests outbound
57 %nodehops = ();                 # node specific hop control
58 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
59
60 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
61                                                                 # loads of 'bad things'
62 $baddx = new DXHash "baddx";
63 $badspotter = new DXHash "badspotter";
64 $badnode = new DXHash "badnode";
65 $last10 = $last_pc50 = time;
66 $ann_to_talk = 1;
67 $rspfcheck = 1;
68 $eph_restime = 180;
69 $eph_info_restime = 60*60;
70 $eph_pc34_restime = 30;
71 $pingint = 5*60;
72 $obscount = 2;
73 $chatdupeage = 20 * 60 * 60;
74
75 @checklist = 
76 (
77  [ qw(i c c m bp bc c) ],                       # pc10
78  [ qw(i f m d t m c c h) ],             # pc11
79  [ qw(i c bm m bm bm p h) ],            # pc12
80  [ qw(i c h) ],                                 # 
81  [ qw(i c h) ],                                 # 
82  [ qw(i c m h) ],                                       # 
83  undef ,                                                # pc16 has to be validated manually
84  [ qw(i c c h) ],                                       # pc17
85  [ qw(i m n) ],                                 # pc18
86  undef ,                                                # pc19 has to be validated manually
87  undef ,                                                # pc20 no validation
88  [ qw(i c m h) ],                                       # pc21
89  undef ,                                                # pc22 no validation
90  [ qw(i d n n n n m c c h) ],           # pc23
91  [ qw(i c p h) ],                                       # pc24
92  [ qw(i c c n n) ],                             # pc25
93  [ qw(i f m d t m c c bc) ],            # pc26
94  [ qw(i d n n n n m c c bc) ],  # pc27
95  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
96  [ qw(i c c n m) ],                             # pc29
97  [ qw(i c c n) ],                                       # pc30
98  [ qw(i c c n) ],                                       # pc31
99  [ qw(i c c n) ],                                       # pc32
100  [ qw(i c c n) ],                                       # pc33
101  [ qw(i c c m) ],                                       # pc34
102  [ qw(i c c m) ],                                       # pc35
103  [ qw(i c c m) ],                                       # pc36
104  [ qw(i c c n m) ],                             # pc37
105  undef,                                                 # pc38 not interested
106  [ qw(i c m) ],                                 # pc39
107  [ qw(i c c m p n) ],                           # pc40
108  [ qw(i c n m h) ],                             # pc41
109  [ qw(i c c n) ],                                       # pc42
110  undef,                                                 # pc43 don't handle it
111  [ qw(i c c n m m c) ],                 # pc44
112  [ qw(i c c n m) ],                             # pc45
113  [ qw(i c c n) ],                                       # pc46
114  undef,                                                 # pc47
115  undef,                                                 # pc48
116  [ qw(i c m h) ],                                       # pc49
117  [ qw(i c n h) ],                                       # pc50
118  [ qw(i c c n) ],                                       # pc51
119  undef,
120  undef,
121  undef,
122  undef,
123  undef,
124  undef,
125  undef,
126  undef,
127  undef,                                                 # pc60
128  undef,
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,
134  undef,
135  undef,
136  undef,
137  undef,                                                 # pc70
138  undef,
139  undef,
140  [ qw(i d n n n n n n m m m c c h) ],   # pc73
141  undef,
142  undef,
143  undef,
144  undef,
145  undef,
146  undef,
147  undef,                                                 # pc80
148  undef,
149  undef,
150  undef,
151  [ qw(i c c c m) ],                             # pc84
152  [ qw(i c c c m) ],                             # pc85
153  undef,
154  undef,
155  undef,
156  undef,
157  [ qw(i c n) ],                                 # pc90
158 );
159
160 # use the entry in the check list to check the field list presented
161 # return OK if line NOT in check list (for now)
162 sub check
163 {
164         my $n = shift;
165         $n -= 10;
166         return 0 if $n < 0 || $n > @checklist; 
167         my $ref = $checklist[$n];
168         return 0 unless ref $ref;
169         
170         my $i;
171         for ($i = 1; $i < @$ref; $i++) {
172                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
173                 return 0 unless $act;
174                 next if $blank && $_[$i] =~ /^[ \*]$/;
175                 if ($act eq 'c') {
176                         return $i unless is_callsign($_[$i]);
177                 } elsif ($act eq 'i') {                 
178                         ;                                       # do nothing
179                 } elsif ($act eq 'm') {
180                         return $i unless is_pctext($_[$i]);
181                 } elsif ($act eq 'p') {
182                         return $i unless is_pcflag($_[$i]);
183                 } elsif ($act eq 'f') {
184                         return $i unless is_freq($_[$i]);
185                 } elsif ($act eq 'n') {
186                         return $i unless $_[$i] =~ /^[\d ]+$/;
187                 } elsif ($act eq 'h') {
188                         return $i unless $_[$i] =~ /^H\d\d?$/;
189                 } elsif ($act eq 'd') {
190                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
191                 } elsif ($act eq 't') {
192                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
193                 } 
194         }
195         return 0;
196 }
197
198 sub init
199 {
200         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
201         confess $@ if $@;
202 }
203
204 #
205 # obtain a new connection this is derived from dxchannel
206 #
207
208 sub new 
209 {
210         my $self = DXChannel::alloc(@_);
211
212         # add this node to the table, the values get filled in later
213         my $pkg = shift;
214         my $call = shift;
215         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
216
217         return $self;
218 }
219
220 # this is how a pc connection starts (for an incoming connection)
221 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
222 # all the crap that comes between).
223 sub start
224 {
225         my ($self, $line, $sort) = @_;
226         my $call = $self->{call};
227         my $user = $self->{user};
228
229         # log it
230         my $host = $self->{conn}->{peerhost} || "unknown";
231         Log('DXProt', "$call connected from $host");
232         
233         # remember type of connection
234         $self->{consort} = $line;
235         $self->{outbound} = $sort eq 'O';
236         my $priv = $user->priv;
237         $priv = $user->priv(1) unless $priv;
238         $self->{priv} = $priv;     # other clusters can always be 'normal' users
239         $self->{lang} = $user->lang || 'en';
240         $self->{isolate} = $user->{isolate};
241         $self->{consort} = $line;       # save the connection type
242         $self->{here} = 1;
243         $self->{width} = 80;
244
245         # sort out registration
246         $self->{registered} = 1;
247
248         # get the output filters
249         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
250         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
251         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
252         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
253         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
254
255
256         # get the INPUT filters (these only pertain to Clusters)
257         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
258         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
259         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
260         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
261         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
262         
263         # set unbuffered and no echo
264         $self->send_now('B',"0");
265         $self->send_now('E',"0");
266         $self->conn->echo(0) if $self->conn->can('echo');
267         
268         # ping neighbour node stuff
269         my $ping = $user->pingint;
270         $ping = $pingint unless defined $ping;
271         $self->{pingint} = $ping;
272         $self->{nopings} = $user->nopings || $obscount;
273         $self->{pingtime} = [ ];
274         $self->{pingave} = 999;
275         $self->{metric} ||= 100;
276         $self->{lastping} = $main::systime;
277
278         # send initialisation string
279         unless ($self->{outbound}) {
280                 $self->sendinit;
281         }
282         
283         $self->state('init');
284         $self->{pc50_t} = $main::systime;
285
286         # send info to all logged in thingies
287         $self->tell_login('loginn');
288
289         # run a script send the output to the debug file
290         my $script = new Script(lc $call) || new Script('node_default');
291         $script->run($self) if $script;
292 }
293
294 #
295 # send outgoing 'challenge'
296 #
297
298 sub sendinit
299 {
300         my $self = shift;
301         $self->send(pc18());
302 }
303
304 sub removepc90
305 {
306         $_[0] =~ s/^PC90\^[-A-Z0-9]+\^\d+\^//;
307 }
308
309 sub send
310 {
311         my $self = shift;
312         my $line = shift;
313         if ($self->user->wantpc90) {
314                 $line = mungepc90($line);
315         } else {
316                 removepc90($line);
317         }
318         $self->SUPER::send($line);
319 }
320
321 my $pc90msgid = 0;
322
323 sub nextpc90
324 {
325         $pc90msgid = 0 if $pc90msgid > 9999;
326         return $pc90msgid++;
327 }
328
329 sub mungepc90
330 {
331         unless ($_[0] =~ /^PC90/) {
332                 my $id = nextpc90();
333                 return "PC90^$main::mycall^$id^" . $_[0]; 
334         } 
335         return $_[0];
336 }
337
338 #
339 # This is the normal pcxx despatcher
340 #
341 sub normal
342 {
343         my ($self, $line) = @_;
344         my @field = split /\^/, $line;
345         return unless @field;
346         
347         pop @field if $field[-1] eq '~';
348         
349 #       print join(',', @field), "\n";
350                                                 
351         
352         # process PC frames, this will fail unless the frame starts PCnn
353         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
354         return unless $pcno;
355         return if $pcno < 10 || $pcno > 99;
356
357         # check for and dump bad protocol messages
358         my $n = check($pcno, @field);
359         if ($n) {
360                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
361                 return;
362         }
363
364         # handle PC90 frames in a special way.
365     # 
366         # PC90 frames are normal frames that that are wrapped in inside a PC90 
367     # The extra fields are "originating node" and a sequence number.
368     # The sequence number is checked against the nodes 'last one' to see if
369         # it is a duplicate and, if so, is dropped at this stage; before any
370         # other processing.
371         #
372         # This is done here simply for efficiency. Adding another function would
373         # add more copying and so on.
374         #
375
376         my $origin = $self->call;
377         
378         if ($pcno >= 90) {
379                 $origin = $field[1];
380                 if ($origin eq $main::mycall) {
381                         dbg("PCPROT: loop dupe") if isdbg('chanerr');
382                         return;
383                 }
384                 my $seq = $field[2];
385                 my $node = Route::Node::get($origin);
386                 if ($node) {
387                         if (my $lid = $node->lid) {
388                                 my $cmp = $seq >= $lid ? $seq : $seq + 9999;
389                                 if ($cmp <= $lid) {
390                                         dbg("PCPROT: sequence dupe $seq ($cmp) <= $lid") if isdbg('chanerr');
391                                         return;
392                                 }
393                         }
394                         $node->lid($seq);
395                 }
396
397                 # do a recheck on the contents of the PC90
398                 if ($pcno == 90) {
399                         shift @field;
400                         shift @field;
401                         shift @field;
402                         
403                         ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
404                         return unless $pcno;
405                         return if $pcno < 10 || $pcno > 99;
406                         
407                         # check for and dump bad protocol messages
408                         my $n = check($pcno, @field);
409                         if ($n) {
410                                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
411                                 return;
412                         }
413                 }
414         }
415
416         # local processing 1
417         my $pcr;
418         eval {
419                 $pcr = Local::pcprot($self, $pcno, @field);
420         };
421 #       dbg("Local::pcprot error $@") if isdbg('local') if $@;
422         return if $pcr;
423
424         no strict 'subs';
425         my $sub = "handle_$pcno";
426
427         # add missing PC90 if not present (for ongoing distribution)
428         $line = mungepc90($line) if $pcno < 90;
429
430         if ($self->can($sub)) {
431                 $self->$sub($pcno, $line, $origin, @field);
432         } else {
433                 $self->handle_default($pcno, $line, $origin, @field);
434         }
435 }
436         
437 # incoming talk commands
438 sub handle_10
439 {
440         my $self = shift;
441         my $pcno = shift;
442         my $line = shift;
443         my $origin = shift;
444
445         # rsfp check
446         return if $rspfcheck and !$self->rspfcheck(0, $_[6], $_[1]);
447                         
448         # will we allow it at all?
449         if ($censorpc) {
450                 my @bad;
451                 if (@bad = BadWords::check($_[3])) {
452                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
453                         return;
454                 }
455         }
456
457         # is it for me or one of mine?
458         my ($from, $to, $via, $call, $dxchan);
459         $from = $_[1];
460         if ($_[5] gt ' ') {
461                 $via = $_[2];
462                 $to = $_[5];
463         } else {
464                 $to = $_[2];
465         }
466
467         # if this is a 'nodx' node then ignore it
468         if ($badnode->in($_[6]) || ($via && $badnode->in($via))) {
469                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
470                 return;
471         }
472
473         # if this is a 'bad spotter' user then ignore it
474         my $nossid = $from;
475         $nossid =~ s/-\d+$//;
476         if ($badspotter->in($nossid)) {
477                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
478                 return;
479         }
480
481         # if we are converting announces to talk is it a dup?
482         if ($ann_to_talk) {
483                 if (AnnTalk::is_talk_candidate($from, $_[3]) && AnnTalk::dup($from, $to, $_[3])) {
484                         dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
485                         return;
486                 }
487         }
488
489         # it is here and logged on
490         $dxchan = DXChannel->get($main::myalias) if $to eq $main::mycall;
491         $dxchan = DXChannel->get($to) unless $dxchan;
492         if ($dxchan && $dxchan->is_user) {
493                 $_[3] =~ s/\%5E/^/g;
494                 $dxchan->talk($from, $to, $via, $_[3]);
495                 return;
496         }
497
498         # is it elsewhere, visible on the cluster via the to address?
499         # note: this discards the via unless the to address is on
500         # the via address
501         my ($ref, $vref);
502         if ($ref = Route::get($to)) {
503                 $vref = Route::Node::get($via) if $via;
504                 $vref = undef unless $vref && grep $to eq $_, $vref->users;
505                 $ref->dxchan->talk($from, $to, $vref ? $via : undef, $_[3], $_[6]);
506                 return;
507         }
508
509         # not visible here, send a message of condolence
510         $vref = undef;
511         $ref = Route::get($from);
512         $vref = $ref = Route::Node::get($_[6]) unless $ref; 
513         if ($ref) {
514                 $dxchan = $ref->dxchan;
515                 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
516         }
517 }
518
519 # DX Spot handling
520 sub handle_11
521 {
522         my $self = shift;
523         my $pcno = shift;
524         my $line = shift;
525         my $origin = shift;
526
527         # route 'foreign' pc26s 
528         if ($pcno == 26) {
529                 if ($_[7] ne $main::mycall) {
530                         $self->route($_[7], $line);
531                         return;
532                 }
533         }
534                         
535         # rsfp check
536         #                       return if $rspfcheck and !$self->rspfcheck(1, $_[7], $_[6]);
537
538         # if this is a 'nodx' node then ignore it
539         if ($badnode->in($_[7])) {
540                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
541                 return;
542         }
543                         
544         # if this is a 'bad spotter' user then ignore it
545         my $nossid = $_[6];
546         $nossid =~ s/-\d+$//;
547         if ($badspotter->in($nossid)) {
548                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
549                 return;
550         }
551                         
552         # convert the date to a unix date
553         my $d = cltounix($_[3], $_[4]);
554         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
555         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
556                 dbg("PCPROT: Spot ignored, invalid date or out of range ($_[3] $_[4])\n") if isdbg('chanerr');
557                 return;
558         }
559
560         # is it 'baddx'
561         if ($baddx->in($_[2]) || BadWords::check($_[2]) || $_[2] =~ /COCK/) {
562                 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
563                 return;
564         }
565                         
566         # do some de-duping
567         $_[5] =~ s/^\s+//;                      # take any leading blanks off
568         $_[2] = unpad($_[2]);           # take off leading and trailing blanks from spotted callsign
569         if ($_[2] =~ /BUST\w*$/) {
570                 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
571                 return;
572         }
573         if ($censorpc) {
574                 my @bad;
575                 if (@bad = BadWords::check($_[5])) {
576                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
577                         return;
578                 }
579         }
580
581
582         my @spot = Spot::prepare($_[1], $_[2], $d, $_[5], $_[6], $_[7]);
583         # global spot filtering on INPUT
584         if ($self->{inspotsfilter}) {
585                 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
586                 unless ($filter) {
587                         dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
588                         return;
589                 }
590         }
591
592         # this goes after the input filtering, but before the add
593         # so that if it is input filtered, it isn't added to the dup
594         # list. This allows it to come in from a "legitimate" source
595         if (Spot::dup($_[1], $_[2], $d, $_[5], $_[6])) {
596                 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
597                 return;
598         }
599
600         # add it 
601         Spot::add(@spot);
602
603         #
604         # @spot at this point contains:-
605         # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
606         # then  spotted itu, spotted cq, spotters itu, spotters cq
607         # you should be able to route on any of these
608         #
609                         
610         # fix up qra locators of known users 
611         my $user = DXUser->get_current($spot[4]);
612         if ($user) {
613                 my $qra = $user->qra;
614                 unless ($qra && is_qra($qra)) {
615                         my $lat = $user->lat;
616                         my $long = $user->long;
617                         if (defined $lat && defined $long) {
618                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
619                                 $user->put;
620                         }
621                 }
622
623                 # send a remote command to a distant cluster if it is visible and there is no
624                 # qra locator and we havn't done it for a month.
625
626                 unless ($user->qra) {
627                         my $node;
628                         my $to = $user->homenode;
629                         my $last = $user->lastoper || 0;
630                         if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
631                                 my $cmd = "forward/opernam $spot[4]";
632                                 # send the rcmd but we aren't interested in the replies...
633                                 my $dxchan = $node->dxchan;
634                                 if ($dxchan && $dxchan->is_clx) {
635                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
636                                 } else {
637                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
638                                 }
639                                 if ($to ne $_[7]) {
640                                         $to = $_[7];
641                                         $node = Route::Node::get($to);
642                                         if ($node) {
643                                                 $dxchan = $node->dxchan;
644                                                 if ($dxchan && $dxchan->is_clx) {
645                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
646                                                 } else {
647                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
648                                                 }
649                                         }
650                                 }
651                                 $user->lastoper($main::systime);
652                                 $user->put;
653                         }
654                 }
655         }
656                                 
657         # local processing 
658         my $r;
659         eval {
660                 $r = Local::spot($self, @spot);
661         };
662         #                       dbg("Local::spot1 error $@") if isdbg('local') if $@;
663         return if $r;
664
665         # DON'T be silly and send on PC26s!
666         return if $pcno == 26;
667
668         # send out the filtered spots
669         send_dx_spot($self, $line, @spot) if @spot;
670 }
671                 
672 # announces
673 sub handle_12
674 {
675         my $self = shift;
676         my $pcno = shift;
677         my $line = shift;
678         my $origin = shift;
679
680         #                       return if $rspfcheck and !$self->rspfcheck(1, $_[5], $_[1]);
681
682         # announce duplicate checking
683         $_[3] =~ s/^\s+//;                      # remove leading blanks
684
685         if ($censorpc) {
686                 my @bad;
687                 if (@bad = BadWords::check($_[3])) {
688                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
689                         return;
690                 }
691         }
692
693         # if this is a 'nodx' node then ignore it
694         if ($badnode->in($_[5])) {
695                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
696                 return;
697         }
698
699         # if this is a 'bad spotter' user then ignore it
700         my $nossid = $_[1];
701         $nossid =~ s/-\d+$//;
702         if ($badspotter->in($nossid)) {
703                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
704                 return;
705         }
706
707         my $dxchan;
708         
709         if ((($dxchan = DXChannel->get($_[2])) && $dxchan->is_user) || $_[4] =~ /^[\#\w.]+$/){
710                 $self->send_chat($line, @_[1..6]);
711         } elsif ($_[2] eq '*' || $_[2] eq $main::mycall) {
712
713                 # ignore something that looks like a chat line coming in with sysop
714                 # flag - this is a kludge...
715                 if ($_[3] =~ /^\#\d+ / && $_[4] eq '*') {
716                         dbg('PCPROT: Probable chat rewrite, dropped') if isdbg('chanerr');
717                         return;
718                 }
719
720                 # here's a bit of fun, convert incoming ann with a callsign in the first word
721                 # or one saying 'to <call>' to a talk if we can route to the recipient
722                 if ($ann_to_talk) {
723                         my $call = AnnTalk::is_talk_candidate($_[1], $_[3]);
724                         if ($call) {
725                                 my $ref = Route::get($call);
726                                 if ($ref) {
727                                         $dxchan = $ref->dxchan;
728                                         $dxchan->talk($_[1], $call, undef, $_[3], $_[5]) if $dxchan != $self;
729                                         return;
730                                 }
731                         }
732                 }
733         
734                 # send it
735                 $self->send_announce($line, @_[1..6]);
736         } else {
737                 $self->route($_[2], $line);
738         }
739 }
740                 
741 # incoming user         
742 sub handle_16
743 {
744         my $self = shift;
745         my $pcno = shift;
746         my $line = shift;
747         my $origin = shift;
748
749         if (eph_dup($line)) {
750                 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
751                 return;
752         }
753
754         # general checks
755         my $dxchan;
756         my $ncall = $_[1];
757         my $newline = "PC16^";
758                         
759         # do I want users from this channel?
760         unless ($self->user->wantpc16) {
761                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
762                 return;
763         }
764         # is it me?
765         if ($ncall eq $main::mycall) {
766                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
767                 return;
768         }
769         my $parent = Route::Node::get($ncall); 
770
771         # if there is a parent, proceed, otherwise if there is a latent PC19 in the PC19list, 
772         # fix it up in the routing tables and issue it forth before the PC16
773         unless ($parent) {
774                 my $nl = $pc19list{$ncall};
775
776                 if ($nl && @_ > 3) { # 3 because of the hop count!
777
778                         # this is a new (remembered) node, now attach it to me if it isn't in filtered
779                         # and we haven't disallowed it
780                         my $user = DXUser->get_current($ncall);
781                         if (!$user) {
782                                 $user = DXUser->new($ncall);
783                                 $user->sort('A');
784                                 $user->priv(1); # I have relented and defaulted nodes
785                                 $user->lockout(1);
786                                 $user->homenode($ncall);
787                                 $user->node($ncall);
788                         }
789
790                         my $wantpc19 = $user->wantroutepc19;
791                         if ($wantpc19 || !defined $wantpc19) {
792                                 my $new = Route->new($ncall); # throw away
793                                 if ($self->in_filter_route($new)) {
794                                         my @nrout;
795                                         for (@$nl) {
796                                                 $parent = Route::Node::get($_->[0]);
797                                                 $dxchan = $parent->dxchan if $parent;
798                                                 if ($dxchan && $dxchan ne $self) {
799                                                         dbg("PCPROT: PC19 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
800                                                         $parent = undef;
801                                                 }
802                                                 if ($parent) {
803                                                         my $r = $parent->add($ncall, $_->[1], $_->[2]);
804                                                         push @nrout, $r unless @nrout;
805                                                 }
806                                         }
807                                         $user->wantroutepc19(1) unless defined $wantpc19; # for now we work on the basis that pc16 = real route 
808                                         $user->lastin($main::systime) unless DXChannel->get($ncall);
809                                         $user->put;
810                                                 
811                                         # route the pc19 - this will cause 'stuttering PC19s' for a while
812                                         $self->route_pc19(@nrout) if @nrout ;
813                                         $parent = Route::Node::get($ncall);
814                                         unless ($parent) {
815                                                 dbg("PCPROT: lost $ncall after sending PC19 for it?");
816                                                 return;
817                                         }
818                                 } else {
819                                         return;
820                                 }
821                                 delete $pc19list{$ncall};
822                         }
823                 } else {
824                         dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
825                         return;
826                 }
827         } else {
828                                 
829                 $dxchan = $parent->dxchan;
830                 if ($dxchan && $dxchan ne $self) {
831                         dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
832                         return;
833                 }
834
835                 # input filter if required
836                 return unless $self->in_filter_route($parent);
837         }
838
839         my $i;
840         my @rout;
841         for ($i = 2; $i < $#_; $i++) {
842                 my ($call, $conf, $here) = $_[$i] =~ /^(\S+) (\S) (\d)/o;
843                 next unless $call && $conf && defined $here && is_callsign($call);
844                 next if $call eq $main::mycall;
845
846                 eph_del_regex("^PC17\\^$call\\^$ncall");
847                                 
848                 $conf = $conf eq '*';
849
850                 # reject this if we think it is a node already
851                 my $r = Route::Node::get($call);
852                 my $u = DXUser->get_current($call) unless $r;
853                 if ($r || ($u && $u->is_node)) {
854                         dbg("PCPROT: $call is a node") if isdbg('chanerr');
855                         next;
856                 }
857                                 
858                 $r = Route::User::get($call);
859                 my $flags = Route::here($here)|Route::conf($conf);
860                                 
861                 if ($r) {
862                         my $au = $r->addparent($parent);                                        
863                         if ($r->flags != $flags) {
864                                 $r->flags($flags);
865                                 $au = $r;
866                         }
867                         push @rout, $r if $au;
868                 } else {
869                         push @rout, $parent->add_user($call, $flags);
870                 }
871                 
872                                 
873                 # add this station to the user database, if required
874                 $call =~ s/-\d+$//o;    # remove ssid for users
875                 my $user = DXUser->get_current($call);
876                 $user = DXUser->new($call) if !$user;
877                 $user->homenode($parent->call) if !$user->homenode;
878                 $user->node($parent->call);
879                 $user->lastin($main::systime) unless DXChannel->get($call);
880                 $user->put;
881         }
882                         
883         $self->route_pc16($parent, @rout) if @rout;
884 }
885                 
886 # remove a user
887 sub handle_17
888 {
889         my $self = shift;
890         my $pcno = shift;
891         my $line = shift;
892         my $origin = shift;
893         my $dxchan;
894         my $ncall = $_[2];
895         my $ucall = $_[1];
896
897         eph_del_regex("^PC16\\^$ncall.*$ucall");
898                         
899         # do I want users from this channel?
900         unless ($self->user->wantpc16) {
901                 dbg("PCPROT: don't send users to $self->{call}") if isdbg('chanerr');
902                 return;
903         }
904         if ($ncall eq $main::mycall) {
905                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
906                 return;
907         }
908
909         my $uref = Route::User::get($ucall);
910         unless ($uref) {
911                 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
912                 return;
913         }
914         my $parent = Route::Node::get($ncall);
915         unless ($parent) {
916                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
917                 return;
918         }                       
919
920         $dxchan = $parent->dxchan;
921         if ($dxchan && $dxchan ne $self) {
922                 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
923                 return;
924         }
925
926         # input filter if required
927         return unless $self->in_filter_route($parent);
928                         
929         $parent->del_user($uref);
930
931         if (eph_dup($line)) {
932                 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
933                 return;
934         }
935
936         $self->route_pc17($parent, $uref);
937 }
938                 
939 # link request
940 sub handle_18
941 {
942         my $self = shift;
943         my $pcno = shift;
944         my $line = shift;
945         my $origin = shift;
946         $self->state('init');   
947
948         # record the type and version offered
949         if ($_[1] =~ /DXSpider Version: (\d+\.\d+) Build: (\d+\.\d+)/) {
950                 $self->version(53 + $1);
951                 $self->user->version(53 + $1);
952                 $self->build(0 + $2);
953                 $self->user->build(0 + $2);
954                 unless ($self->is_spider) {
955                         $self->user->sort('S');
956                         $self->user->put;
957                         $self->sort('S');
958                 }
959         } else {
960                 $self->version(50.0);
961                 $self->version($_[2] / 100) if $_[2] && $_[2] =~ /^\d+$/;
962                 $self->user->version($self->version);
963         }
964
965         # first clear out any nodes on this dxchannel
966         my $parent = Route::Node::get($self->{call});
967         my @rout = $parent->del_nodes;
968         $self->route_pc21(@rout, $parent) if @rout;
969         $self->send_local_config();
970         $self->send(pc20());
971 }
972                 
973 # incoming cluster list
974 sub handle_19
975 {
976         my $self = shift;
977         my $pcno = shift;
978         my $line = shift;
979         my $origin = shift;
980
981         my $i;
982         my $newline = "PC19^";
983
984         if (eph_dup($line)) {
985                 dbg("PCPROT: dup PC19 detected") if isdbg('chanerr');
986                 return;
987         }
988
989         # new routing list
990         my @rout;
991         my $parent = Route::Node::get($self->{call});
992         unless ($parent) {
993                 dbg("DXPROT: my parent $self->{call} has disappeared");
994                 $self->disconnect;
995                 return;
996         }
997
998         # parse the PC19
999         for ($i = 1; $i < $#_-1; $i += 4) {
1000                 my $here = $_[$i];
1001                 my $call = uc $_[$i+1];
1002                 my $conf = $_[$i+2];
1003                 my $ver = $_[$i+3];
1004                 next unless defined $here && defined $conf && is_callsign($call);
1005
1006                 eph_del_regex("^PC(?:21\\^$call|17\\^[^\\^]+\\^$call)");
1007                                 
1008                 # check for sane parameters
1009                 #                               $ver = 5000 if $ver eq '0000';
1010                 next if $ver < 5000;    # only works with version 5 software
1011                 next if length $call < 3; # min 3 letter callsigns
1012                 next if $call eq $main::mycall;
1013
1014                 # check that this PC19 isn't trying to alter the wrong dxchan
1015                 my $dxchan = DXChannel->get($call);
1016                 if ($dxchan && $dxchan != $self) {
1017                         dbg("PCPROT: PC19 from $self->{call} trying to alter wrong locally connected $call, ignored!") if isdbg('chanerr');
1018                         next;
1019                 }
1020
1021                 # add this station to the user database, if required (don't remove SSID from nodes)
1022                 my $user = DXUser->get_current($call);
1023                 if (!$user) {
1024                         $user = DXUser->new($call);
1025                         $user->sort('A');
1026                         $user->priv(1);         # I have relented and defaulted nodes
1027                         $user->lockout(1);
1028                         $user->homenode($call);
1029                         $user->node($call);
1030                 }
1031
1032                 my $r = Route::Node::get($call);
1033                 my $flags = Route::here($here)|Route::conf($conf);
1034
1035                 # modify the routing table if it is in it, otherwise store it in the pc19list for now
1036                 if ($r) {
1037                         my $ar;
1038                         if ($call ne $parent->call) {
1039                                 if ($self->in_filter_route($r)) {
1040                                         $ar = $parent->add($call, $ver, $flags);
1041                                         push @rout, $ar if $ar;
1042                                 } else {
1043                                         next;
1044                                 }
1045                         }
1046                         if ($r->version ne $ver || $r->flags != $flags) {
1047                                 $r->version($ver);
1048                                 $r->flags($flags);
1049                                 push @rout, $r unless $ar;
1050                         }
1051                 } else {
1052
1053                         # if he is directly connected or allowed then add him, otherwise store him up for later
1054                         if ($call eq $self->{call} || $user->wantroutepc19) {
1055                                 my $new = Route->new($call); # throw away
1056                                 if ($self->in_filter_route($new)) {
1057                                         my $ar = $parent->add($call, $ver, $flags);
1058                                         $user->wantroutepc19(1) unless defined $user->wantroutepc19;
1059                                         push @rout, $ar if $ar;
1060                                 } else {
1061                                         next;
1062                                 }
1063                         } else {
1064                                 $pc19list{$call} = [] unless exists $pc19list{$call};
1065                                 my $nl = $pc19list{$call};
1066                                 push @{$pc19list{$call}}, [$self->{call}, $ver, $flags] unless grep $_->[0] eq $self->{call}, @$nl;
1067                         }
1068                 }
1069
1070                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
1071                 my $mref = DXMsg::get_busy($call);
1072                 $mref->stop_msg($call) if $mref;
1073                                 
1074                 $user->lastin($main::systime) unless DXChannel->get($call);
1075                 $user->put;
1076         }
1077
1078
1079         $self->route_pc19(@rout) if @rout;
1080 }
1081                 
1082 # send local configuration
1083 sub handle_20
1084 {
1085         my $self = shift;
1086         my $pcno = shift;
1087         my $line = shift;
1088         my $origin = shift;
1089         $self->send_local_config();
1090         $self->send(pc22());
1091         $self->state('normal');
1092         $self->{lastping} = 0;
1093 }
1094                 
1095 # delete a cluster from the list
1096 sub handle_21
1097 {
1098         my $self = shift;
1099         my $pcno = shift;
1100         my $line = shift;
1101         my $origin = shift;
1102         my $call = uc $_[1];
1103
1104         eph_del_regex("^PC1[679].*$call");
1105                         
1106         # if I get a PC21 from the same callsign as self then treat it
1107         # as a PC39: I have gone away
1108         if ($call eq $self->call) {
1109                 $self->disconnect(1);
1110                 return;
1111         }
1112
1113         # check to see if we are in the pc19list, if we are then don't bother with any of
1114         # this routing table manipulation, just remove it from the list and dump it
1115         my @rout;
1116         if (my $nl = $pc19list{$call}) {
1117                 $pc19list{$call} = [ grep {$_->[0] ne $self->{call}} @$nl ];
1118                 delete $pc19list{$call} unless @{$pc19list{$call}};
1119         } else {
1120                                 
1121                 my $parent = Route::Node::get($self->{call});
1122                 unless ($parent) {
1123                         dbg("DXPROT: my parent $self->{call} has disappeared");
1124                         $self->disconnect;
1125                         return;
1126                 }
1127                 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
1128                         my $node = Route::Node::get($call);
1129                         if ($node) {
1130                                                 
1131                                 my $dxchan = DXChannel->get($call);
1132                                 if ($dxchan && $dxchan != $self) {
1133                                         dbg("PCPROT: PC21 from $self->{call} trying to alter locally connected $call, ignored!") if isdbg('chanerr');
1134                                         return;
1135                                 }
1136                                                 
1137                                 # input filter it
1138                                 return unless $self->in_filter_route($node);
1139                                                 
1140                                 # routing objects
1141                                 push @rout, $node->del($parent);
1142                         }
1143                 } else {
1144                         dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
1145                         return;
1146                 }
1147         }
1148
1149         $self->route_pc21(@rout) if @rout;
1150 }
1151                 
1152
1153 sub handle_22
1154 {
1155         my $self = shift;
1156         my $pcno = shift;
1157         my $line = shift;
1158         my $origin = shift;
1159         $self->state('normal');
1160         $self->{lastping} = 0;
1161 }
1162                                 
1163 # WWV info
1164 sub handle_23
1165 {
1166         my $self = shift;
1167         my $pcno = shift;
1168         my $line = shift;
1169         my $origin = shift;
1170                         
1171         # route foreign' pc27s 
1172         if ($pcno == 27) {
1173                 if ($_[8] ne $main::mycall) {
1174                         $self->route($_[8], $line);
1175                         return;
1176                 }
1177         }
1178
1179         return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7]);
1180
1181         # do some de-duping
1182         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
1183         my $sfi = unpad($_[3]);
1184         my $k = unpad($_[4]);
1185         my $i = unpad($_[5]);
1186         my ($r) = $_[6] =~ /R=(\d+)/;
1187         $r = 0 unless $r;
1188         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1189                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
1190                 return;
1191         }
1192         if (Geomag::dup($d,$sfi,$k,$i,$_[6])) {
1193                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1194                 return;
1195         }
1196         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
1197                 
1198         my $wwv = Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
1199
1200         my $rep;
1201         eval {
1202                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
1203         };
1204         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
1205         return if $rep;
1206
1207         # DON'T be silly and send on PC27s!
1208         return if $pcno == 27;
1209
1210         # broadcast to the eager world
1211         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
1212 }
1213                 
1214 # set here status
1215 sub handle_24
1216 {
1217         my $self = shift;
1218         my $pcno = shift;
1219         my $line = shift;
1220         my $origin = shift;
1221         my $call = uc $_[1];
1222         my ($nref, $uref);
1223         $nref = Route::Node::get($call);
1224         $uref = Route::User::get($call);
1225         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1226                         
1227         if (eph_dup($line)) {
1228                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
1229                 return;
1230         }
1231         
1232         $nref->here($_[2]) if $nref;
1233         $uref->here($_[2]) if $uref;
1234         my $ref = $nref || $uref;
1235         return unless $self->in_filter_route($ref);
1236
1237         $self->route_pc24($ref, $_[3]);
1238 }
1239                 
1240 # merge request
1241 sub handle_25
1242 {
1243         my $self = shift;
1244         my $pcno = shift;
1245         my $line = shift;
1246         my $origin = shift;
1247         if ($_[1] ne $main::mycall) {
1248                 $self->route($_[1], $line);
1249                 return;
1250         }
1251         if ($_[2] eq $main::mycall) {
1252                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1253                 return;
1254         }
1255
1256         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1257                         
1258         # spots
1259         if ($_[3] > 0) {
1260                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1261                 my $in;
1262                 foreach $in (@in) {
1263                         $self->send(pc26(@{$in}[0..4], $_[2]));
1264                 }
1265         }
1266
1267         # wwv
1268         if ($_[4] > 0) {
1269                 my @in = reverse Geomag::search(0, $_[4], time, 1);
1270                 my $in;
1271                 foreach $in (@in) {
1272                         $self->send(pc27(@{$in}[0..5], $_[2]));
1273                 }
1274         }
1275 }
1276
1277 sub handle_26 {goto &handle_11}
1278 sub handle_27 {goto &handle_23}
1279
1280 # mail/file handling
1281 sub handle_28
1282 {
1283         my $self = shift;
1284         my $pcno = shift;
1285         my $line = shift;
1286         my $origin = shift;
1287         if ($_[1] eq $main::mycall) {
1288                 no strict 'refs';
1289                 my $sub = "DXMsg::handle_$pcno";
1290                 &$sub($self, @_);
1291         } else {
1292                 $self->route($_[1], $line) unless $self->is_clx;
1293         }
1294 }
1295
1296 sub handle_29 {goto &handle_28}
1297 sub handle_30 {goto &handle_28}
1298 sub handle_31 {goto &handle_28}
1299 sub handle_32 {goto &handle_28}
1300 sub handle_33 {goto &handle_28}
1301                 
1302 sub handle_34
1303 {
1304         my $self = shift;
1305         my $pcno = shift;
1306         my $line = shift;
1307         my $origin = shift;
1308         if (eph_dup($line, $eph_pc34_restime)) {
1309                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1310         } else {
1311                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1312         }
1313 }
1314                 
1315 # remote command replies
1316 sub handle_35
1317 {
1318         my $self = shift;
1319         my $pcno = shift;
1320         my $line = shift;
1321         my $origin = shift;
1322         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1323         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1324 }
1325                 
1326 sub handle_36 {goto &handle_34}
1327
1328 # database stuff
1329 sub handle_37
1330 {
1331         my $self = shift;
1332         my $pcno = shift;
1333         my $line = shift;
1334         my $origin = shift;
1335         DXDb::process($self, $line);
1336 }
1337
1338 # node connected list from neighbour
1339 sub handle_38
1340 {
1341         my $self = shift;
1342         my $pcno = shift;
1343         my $line = shift;
1344         my $origin = shift;
1345 }
1346                 
1347 # incoming disconnect
1348 sub handle_39
1349 {
1350         my $self = shift;
1351         my $pcno = shift;
1352         my $line = shift;
1353         my $origin = shift;
1354         if ($_[1] eq $self->{call}) {
1355                 $self->disconnect(1);
1356         } else {
1357                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1358         }
1359 }
1360
1361 sub handle_40 {goto &handle_28}
1362                 
1363 # user info
1364 sub handle_41
1365 {
1366         my $self = shift;
1367         my $pcno = shift;
1368         my $line = shift;
1369         my $origin = shift;
1370         my $call = $_[1];
1371
1372         my $l = $line;
1373         $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1374         if (eph_dup($l, $eph_info_restime)) {
1375                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1376                 return;
1377         }
1378                         
1379         # input filter if required
1380         #                       my $ref = Route::get($call) || Route->new($call);
1381         #                       return unless $self->in_filter_route($ref);
1382
1383         if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1384                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1385                 return;
1386         }
1387
1388         # add this station to the user database, if required
1389         my $user = DXUser->get_current($call);
1390         $user = DXUser->new($call) unless $user;
1391                         
1392         if ($_[2] == 1) {
1393                 $user->name($_[3]);
1394         } elsif ($_[2] == 2) {
1395                 $user->qth($_[3]);
1396         } elsif ($_[2] == 3) {
1397                 if (is_latlong($_[3])) {
1398                         my ($lat, $long) = DXBearing::stoll($_[3]);
1399                         $user->lat($lat);
1400                         $user->long($long);
1401                         $user->qra(DXBearing::lltoqra($lat, $long));
1402                 } else {
1403                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1404                         return;
1405                 }
1406         } elsif ($_[2] == 4) {
1407                 $user->homenode($_[3]);
1408         } elsif ($_[2] == 5) {
1409                 if (is_qra(uc $_[3])) {
1410                         my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1411                         $user->lat($lat);
1412                         $user->long($long);
1413                         $user->qra(uc $_[3]);
1414                 } else {
1415                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1416                         return;
1417                 }
1418         }
1419         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1420         $user->put;
1421
1422         unless ($self->{isolate}) {
1423                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1424         }
1425
1426         #  perhaps this IS what we want after all
1427         #                       $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1428 }
1429
1430 sub handle_42 {goto &handle_28}
1431
1432
1433 # database
1434 sub handle_44 {goto &handle_37}
1435 sub handle_45 {goto &handle_37}
1436 sub handle_46 {goto &handle_37}
1437 sub handle_47 {goto &handle_37}
1438 sub handle_48 {goto &handle_37}
1439                 
1440 # message and database
1441 sub handle_49
1442 {
1443         my $self = shift;
1444         my $pcno = shift;
1445         my $line = shift;
1446         my $origin = shift;
1447
1448         if (eph_dup($line)) {
1449                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1450                 return;
1451         }
1452         
1453         if ($_[1] eq $main::mycall) {
1454                 DXMsg::handle_49($self, @_);
1455         } else {
1456                 $self->route($_[1], $line) unless $self->is_clx;
1457         }
1458 }
1459
1460 # keep alive/user list
1461 sub handle_50
1462 {
1463         my $self = shift;
1464         my $pcno = shift;
1465         my $line = shift;
1466         my $origin = shift;
1467
1468         my $call = $_[1];
1469         my $node = Route::Node::get($call);
1470         if ($node) {
1471                 return unless $node->call eq $self->{call};
1472                 $node->usercount($_[2]);
1473
1474                 # input filter if required
1475                 return unless $self->in_filter_route($node);
1476
1477                 $self->route_pc50($node, $_[2], $_[3]) unless eph_dup($line);
1478         }
1479 }
1480                 
1481 # incoming ping requests/answers
1482 sub handle_51
1483 {
1484         my $self = shift;
1485         my $pcno = shift;
1486         my $line = shift;
1487         my $origin = shift;
1488         my $to = $_[1];
1489         my $from = $_[2];
1490         my $flag = $_[3];
1491
1492                         
1493         # is it for us?
1494         if ($to eq $main::mycall) {
1495                 if ($flag == 1) {
1496                         $self->send(pc51($from, $to, '0'));
1497                 } else {
1498                         # it's a reply, look in the ping list for this one
1499                         my $ref = $pings{$from};
1500                         if ($ref) {
1501                                 my $tochan =  DXChannel->get($from);
1502                                 while (@$ref) {
1503                                         my $r = shift @$ref;
1504                                         my $dxchan = DXChannel->get($r->{call});
1505                                         next unless $dxchan;
1506                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
1507                                         if ($dxchan->is_user) {
1508                                                 my $s = sprintf "%.2f", $t; 
1509                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1510                                                 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1511                                         } elsif ($dxchan->is_node) {
1512                                                 if ($tochan) {
1513                                                         my $nopings = $tochan->user->nopings || $obscount;
1514                                                         push @{$tochan->{pingtime}}, $t;
1515                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1516
1517                                                                 # cope with a missed ping, this means you must set the pingint large enough
1518                                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
1519                                                                 $t -= $tochan->{pingint};
1520                                                         }
1521
1522                                                                 # calc smoothed RTT a la TCP
1523                                                         if (@{$tochan->{pingtime}} == 1) {
1524                                                                 $tochan->{pingave} = $t;
1525                                                         } else {
1526                                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1527                                                         }
1528                                                         $tochan->{nopings} = $nopings; # pump up the timer
1529                                                 }
1530                                         } 
1531                                 }
1532                         }
1533                 }
1534         } else {
1535                 if (eph_dup($line)) {
1536                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1537                         return;
1538                 }
1539                 # route down an appropriate thingy
1540                 $self->route($to, $line);
1541         }
1542 }
1543
1544 # dunno but route it
1545 sub handle_75
1546 {
1547         my $self = shift;
1548         my $pcno = shift;
1549         my $line = shift;
1550         my $origin = shift;
1551         my $call = $_[1];
1552         if ($call ne $main::mycall) {
1553                 $self->route($call, $line);
1554         }
1555 }
1556
1557 # WCY broadcasts
1558 sub handle_73
1559 {
1560         my $self = shift;
1561         my $pcno = shift;
1562         my $line = shift;
1563         my $origin = shift;
1564         my $call = $_[1];
1565                         
1566         # do some de-duping
1567         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1568         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1569                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1570                 return;
1571         }
1572         @_ = map { unpad($_) } @_;
1573         if (WCY::dup($d)) {
1574                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1575                 return;
1576         }
1577                 
1578         my $wcy = WCY::update($d, @_[2..12]);
1579
1580         my $rep;
1581         eval {
1582                 $rep = Local::wcy($self, @_[1..12]);
1583         };
1584         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1585         return if $rep;
1586
1587         # broadcast to the eager world
1588         send_wcy_spot($self, $line, $d, @_[2..12]);
1589 }
1590
1591 # remote commands (incoming)
1592 sub handle_84
1593 {
1594         my $self = shift;
1595         my $pcno = shift;
1596         my $line = shift;
1597         my $origin = shift;
1598         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1599 }
1600
1601 # remote command replies
1602 sub handle_85
1603 {
1604         my $self = shift;
1605         my $pcno = shift;
1606         my $line = shift;
1607         my $origin = shift;
1608         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1609 }
1610
1611 # if get here then rebroadcast the thing with its Hop count decremented (if
1612 # there is one). If it has a hop count and it decrements to zero then don't
1613 # rebroadcast it.
1614 #
1615 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1616 #        REBROADCAST!!!!
1617 #
1618
1619 sub handle_default
1620 {
1621         my $self = shift;
1622         my $pcno = shift;
1623         my $line = shift;
1624         my $origin = shift;
1625
1626         if (eph_dup($line)) {
1627                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1628         } else {
1629                 unless ($self->{isolate}) {
1630                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1631                 }
1632         }
1633 }
1634
1635 #
1636 # This is called from inside the main cluster processing loop and is used
1637 # for despatching commands that are doing some long processing job
1638 #
1639 sub process
1640 {
1641         my $t = time;
1642         my @dxchan = DXChannel->get_all();
1643         my $dxchan;
1644         my $pc50s;
1645         
1646         # send out a pc50 on EVERY channel all at once
1647         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1648                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
1649                 eph_dup($pc50s);
1650                 $last_pc50 = $t;
1651         }
1652
1653         foreach $dxchan (@dxchan) {
1654                 next unless $dxchan->is_node();
1655                 next if $dxchan == $main::me;
1656
1657                 # send the pc50
1658                 $dxchan->send($pc50s) if $pc50s;
1659                 
1660                 # send a ping out on this channel
1661                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1662                         if ($dxchan->{nopings} <= 0) {
1663                                 $dxchan->disconnect;
1664                         } else {
1665                                 addping($main::mycall, $dxchan->call);
1666                                 $dxchan->{nopings} -= 1;
1667                                 $dxchan->{lastping} = $t;
1668                         }
1669                 }
1670         }
1671
1672         # every ten seconds
1673         if ($t - $last10 >= 10) {       
1674                 # clean out ephemera 
1675
1676                 eph_clean();
1677
1678                 $last10 = $t;
1679         }
1680         
1681         if ($main::systime - 3600 > $last_hour) {
1682                 $last_hour = $main::systime;
1683         }
1684 }
1685
1686 #
1687 # finish up a pc context
1688 #
1689
1690 #
1691 # some active measures
1692 #
1693
1694
1695 sub send_dx_spot
1696 {
1697         my $self = shift;
1698         my $line = shift;
1699         my @dxchan = DXChannel->get_all();
1700         my $dxchan;
1701         
1702         # send it if it isn't the except list and isn't isolated and still has a hop count
1703         # taking into account filtering and so on
1704         foreach $dxchan (@dxchan) {
1705                 next if $dxchan == $main::me;
1706                 next if $dxchan == $self && $self->is_node;
1707                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1708         }
1709 }
1710
1711 sub dx_spot
1712 {
1713         my $self = shift;
1714         my $line = shift;
1715         my $isolate = shift;
1716         my ($filter, $hops);
1717
1718         if ($self->{spotsfilter}) {
1719                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1720                 return unless $filter;
1721         }
1722         send_prot_line($self, $filter, $hops, $isolate, $line);
1723 }
1724
1725 sub send_prot_line
1726 {
1727         my ($self, $filter, $hops, $isolate, $line) = @_;
1728         my $routeit;
1729
1730
1731         if ($hops) {
1732                 $routeit = $line;
1733                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1734         } else {
1735                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1736                 return unless $routeit;
1737         }
1738         if ($filter) {
1739                 $self->send($routeit);
1740         } else {
1741                 $self->send($routeit) unless $self->{isolate} || $isolate;
1742         }
1743 }
1744
1745
1746 sub send_wwv_spot
1747 {
1748         my $self = shift;
1749         my $line = shift;
1750         my @dxchan = DXChannel->get_all();
1751         my $dxchan;
1752         my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1753         my @dxcc = Prefix::extract($_[6]);
1754         if (@dxcc > 0) {
1755                 $wwv_dxcc = $dxcc[1]->dxcc;
1756                 $wwv_itu = $dxcc[1]->itu;
1757                 $wwv_cq = $dxcc[1]->cq;                                         
1758         }
1759         @dxcc = Prefix::extract($_[7]);
1760         if (@dxcc > 0) {
1761                 $org_dxcc = $dxcc[1]->dxcc;
1762                 $org_itu = $dxcc[1]->itu;
1763                 $org_cq = $dxcc[1]->cq;                                         
1764         }
1765         
1766         # send it if it isn't the except list and isn't isolated and still has a hop count
1767         # taking into account filtering and so on
1768         foreach $dxchan (@dxchan) {
1769                 next if $dxchan == $main::me;
1770                 next if $dxchan == $self && $self->is_node;
1771                 my $routeit;
1772                 my ($filter, $hops);
1773
1774                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1775         }
1776         
1777 }
1778
1779 sub wwv
1780 {
1781         my $self = shift;
1782         my $line = shift;
1783         my $isolate = shift;
1784         my ($filter, $hops);
1785         
1786         if ($self->{wwvfilter}) {
1787                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1788                 return unless $filter;
1789         }
1790         send_prot_line($self, $filter, $hops, $isolate, $line)
1791 }
1792
1793 sub send_wcy_spot
1794 {
1795         my $self = shift;
1796         my $line = shift;
1797         my @dxchan = DXChannel->get_all();
1798         my $dxchan;
1799         my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1800         my @dxcc = Prefix::extract($_[10]);
1801         if (@dxcc > 0) {
1802                 $wcy_dxcc = $dxcc[1]->dxcc;
1803                 $wcy_itu = $dxcc[1]->itu;
1804                 $wcy_cq = $dxcc[1]->cq;                                         
1805         }
1806         @dxcc = Prefix::extract($_[11]);
1807         if (@dxcc > 0) {
1808                 $org_dxcc = $dxcc[1]->dxcc;
1809                 $org_itu = $dxcc[1]->itu;
1810                 $org_cq = $dxcc[1]->cq;                                         
1811         }
1812         
1813         # send it if it isn't the except list and isn't isolated and still has a hop count
1814         # taking into account filtering and so on
1815         foreach $dxchan (@dxchan) {
1816                 next if $dxchan == $main::me;
1817                 next if $dxchan == $self;
1818
1819                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1820         }
1821 }
1822
1823 sub wcy
1824 {
1825         my $self = shift;
1826         my $line = shift;
1827         my $isolate = shift;
1828         my ($filter, $hops);
1829
1830         if ($self->{wcyfilter}) {
1831                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1832                 return unless $filter;
1833         }
1834         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1835 }
1836
1837 # send an announce
1838 sub send_announce
1839 {
1840         my $self = shift;
1841         my $line = shift;
1842         my @dxchan = DXChannel->get_all();
1843         my $dxchan;
1844         my $target;
1845         my $to = 'To ';
1846         my $text = unpad($_[2]);
1847                                 
1848         if ($_[3] eq '*') {     # sysops
1849                 $target = "SYSOP";
1850         } elsif ($_[3] gt ' ') { # speciality list handling
1851                 my ($name) = split /\./, $_[3]; 
1852                 $target = "$name"; # put the rest in later (if bothered) 
1853         } 
1854         
1855         if ($_[5] eq '1') {
1856                 $target = "WX"; 
1857                 $to = '';
1858         }
1859         $target = "ALL" if !$target;
1860
1861
1862         # obtain country codes etc 
1863         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1864         my ($ann_state, $org_state) = ("", "");
1865         my @dxcc = Prefix::extract($_[0]);
1866         if (@dxcc > 0) {
1867                 $ann_dxcc = $dxcc[1]->dxcc;
1868                 $ann_itu = $dxcc[1]->itu;
1869                 $ann_cq = $dxcc[1]->cq;                                         
1870                 $ann_state = $dxcc[1]->state;
1871         }
1872         @dxcc = Prefix::extract($_[4]);
1873         if (@dxcc > 0) {
1874                 $org_dxcc = $dxcc[1]->dxcc;
1875                 $org_itu = $dxcc[1]->itu;
1876                 $org_cq = $dxcc[1]->cq;                                         
1877                 $org_state = $dxcc[1]->state;
1878         }
1879
1880         if ($self->{inannfilter}) {
1881                 my ($filter, $hops) = 
1882                         $self->{inannfilter}->it(@_, $self->{call}, 
1883                                                                          $ann_dxcc, $ann_itu, $ann_cq,
1884                                                                          $org_dxcc, $org_itu, $org_cq, $ann_state, $org_state);
1885                 unless ($filter) {
1886                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1887                         return;
1888                 }
1889         }
1890
1891         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
1892                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1893                 return;
1894         }
1895
1896         Log('ann', $target, $_[0], $text);
1897
1898         # send it if it isn't the except list and isn't isolated and still has a hop count
1899         # taking into account filtering and so on
1900         foreach $dxchan (@dxchan) {
1901                 next if $dxchan == $main::me;
1902                 next if $dxchan == $self && $self->is_node;
1903                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1904         }
1905 }
1906
1907 my $msgid = 0;
1908
1909 sub nextchatmsgid
1910 {
1911         $msgid++;
1912         $msgid = 1 if $msgid > 999;
1913         return $msgid;
1914 }
1915
1916 # send a chat line
1917 sub send_chat
1918 {
1919         my $self = shift;
1920         my $line = shift;
1921         my @dxchan = DXChannel->get_all();
1922         my $dxchan;
1923         my $target = $_[3];
1924         my $text = unpad($_[2]);
1925         my $ak1a_line;
1926                                 
1927         # munge the group and recast the line if required
1928         if ($target =~ s/\.LST$//) {
1929                 $ak1a_line = $line;
1930         }
1931         
1932         # obtain country codes etc 
1933         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1934         my ($ann_state, $org_state) = ("", "");
1935         my @dxcc = Prefix::extract($_[0]);
1936         if (@dxcc > 0) {
1937                 $ann_dxcc = $dxcc[1]->dxcc;
1938                 $ann_itu = $dxcc[1]->itu;
1939                 $ann_cq = $dxcc[1]->cq;                                         
1940                 $ann_state = $dxcc[1]->state;
1941         }
1942         @dxcc = Prefix::extract($_[4]);
1943         if (@dxcc > 0) {
1944                 $org_dxcc = $dxcc[1]->dxcc;
1945                 $org_itu = $dxcc[1]->itu;
1946                 $org_cq = $dxcc[1]->cq;                                         
1947                 $org_state = $dxcc[1]->state;
1948         }
1949
1950         if ($self->{inannfilter}) {
1951                 my ($filter, $hops) = 
1952                         $self->{inannfilter}->it(@_, $self->{call}, 
1953                                                                          $ann_dxcc, $ann_itu, $ann_cq,
1954                                                                          $org_dxcc, $org_itu, $org_cq, $ann_state, $org_state);
1955                 unless ($filter) {
1956                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1957                         return;
1958                 }
1959         }
1960
1961         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
1962                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1963                 return;
1964         }
1965
1966
1967         Log('chat', $target, $_[0], $text);
1968
1969         # send it if it isn't the except list and isn't isolated and still has a hop count
1970         # taking into account filtering and so on
1971         foreach $dxchan (@dxchan) {
1972                 my $is_ak1a = $dxchan->is_ak1a;
1973                 
1974                 if ($dxchan->is_node) {
1975                         next if $dxchan == $main::me;
1976                         next if $dxchan == $self;
1977                         next unless $dxchan->is_spider || $is_ak1a;
1978                         next if $target eq 'LOCAL';
1979                         if (!$ak1a_line && $is_ak1a) {
1980                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
1981                         }
1982                 }
1983                 
1984                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], $text, @_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1985         }
1986 }
1987
1988 sub announce
1989 {
1990         my $self = shift;
1991         my $line = shift;
1992         my $isolate = shift;
1993         my $to = shift;
1994         my $target = shift;
1995         my $text = shift;
1996         my ($filter, $hops);
1997
1998         if ($self->{annfilter}) {
1999                 ($filter, $hops) = $self->{annfilter}->it(@_);
2000                 return unless $filter;
2001         }
2002         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
2003 }
2004
2005 sub chat
2006 {
2007         goto &announce;
2008 }
2009
2010
2011 sub send_local_config
2012 {
2013         my $self = shift;
2014         my $node;
2015         my @nodes;
2016         my @localnodes;
2017         my @remotenodes;
2018
2019         dbg('DXProt::send_local_config') if isdbg('trace');
2020         
2021         # send our nodes
2022         if ($self->{isolate}) {
2023                 @localnodes = ( $main::routeroot );
2024         } else {
2025                 # create a list of all the nodes that are not connected to this connection
2026                 # and are not themselves isolated, this to make sure that isolated nodes
2027         # don't appear outside of this node
2028                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
2029                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
2030                 my @intcalls = map { $_->nodes } @localnodes if @localnodes;
2031                 my $ref = Route::Node::get($self->{call});
2032                 my @rnodes = $ref->nodes;
2033                 for my $node (@intcalls) {
2034                         push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes;
2035                 }
2036                 unshift @localnodes, $main::routeroot;
2037         }
2038         
2039
2040         $self->send_route(\&pc19, scalar(@localnodes)+scalar(@remotenodes), @localnodes, @remotenodes);
2041         
2042         # get all the users connected on the above nodes and send them out
2043         foreach $node (@localnodes, @remotenodes) {
2044                 if ($node) {
2045                         my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
2046                         $self->send_route(\&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
2047                 } else {
2048                         dbg("sent a null value") if isdbg('chanerr');
2049                 }
2050         }
2051 }
2052
2053 #
2054 # route a message down an appropriate interface for a callsign
2055 #
2056 # is called route(to, pcline);
2057 #
2058 sub route
2059 {
2060         my ($self, $call, $line) = @_;
2061
2062         if (ref $self && $call eq $self->{call}) {
2063                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2064                 return;
2065         }
2066
2067         # always send it down the local interface if available
2068         my $dxchan = DXChannel->get($call);
2069         unless ($dxchan) {
2070                 my $cl = Route::get($call);
2071                 $dxchan = $cl->dxchan if $cl;
2072                 if (ref $dxchan) {
2073                         if (ref $self && $dxchan eq $self) {
2074                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2075                                 return;
2076                         }
2077                 }
2078         }
2079         if ($dxchan) {
2080                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
2081                 if ($routeit) {
2082                         $dxchan->send($routeit) unless $dxchan == $main::me;
2083                 }
2084         } else {
2085                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
2086         }
2087 }
2088
2089 #
2090 # obtain the hops from the list for this callsign and pc no 
2091 #
2092
2093 sub get_hops
2094 {
2095         my $pcno = shift;
2096         my $hops = $DXProt::hopcount{$pcno};
2097         $hops = $DXProt::def_hopcount if !$hops;
2098         return "H$hops";       
2099 }
2100
2101
2102 # adjust the hop count on a per node basis using the user loadable 
2103 # hop table if available or else decrement an existing one
2104 #
2105
2106 sub adjust_hops
2107 {
2108         my $self = shift;
2109         my $s = shift;
2110         my $call = $self->{call};
2111         my $hops;
2112         
2113         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
2114                 my ($pcno) = $s =~ /^PC(\d\d)/o;
2115                 confess "$call called adjust_hops with '$s'" unless $pcno;
2116                 my $ref = $nodehops{$call} if %nodehops;
2117                 if ($ref) {
2118                         my $newhops = $ref->{$pcno};
2119                         return "" if defined $newhops && $newhops == 0;
2120                         $newhops = $ref->{default} unless $newhops;
2121                         return "" if defined $newhops && $newhops == 0;
2122                         $newhops = $hops if !$newhops;
2123                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
2124                 } else {
2125                         # simply decrement it
2126                         $hops--;
2127                         return "" if !$hops;
2128                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
2129                 }
2130         }
2131         return $s;
2132 }
2133
2134
2135 # load hop tables
2136 #
2137 sub load_hops
2138 {
2139         my $self = shift;
2140         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
2141         do "$main::data/hop_table.pl";
2142         return $@ if $@;
2143         return ();
2144 }
2145
2146
2147 # add a ping request to the ping queues
2148 sub addping
2149 {
2150         my ($from, $to) = @_;
2151         my $ref = $pings{$to} || [];
2152         my $r = {};
2153         $r->{call} = $from;
2154         $r->{t} = [ gettimeofday ];
2155         route(undef, $to, pc51($to, $main::mycall, 1));
2156         push @$ref, $r;
2157         $pings{$to} = $ref;
2158 }
2159
2160 sub process_rcmd
2161 {
2162         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
2163         if ($tonode eq $main::mycall) {
2164                 my $ref = DXUser->get_current($fromnode);
2165                 my $cref = Route::Node::get($fromnode);
2166                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
2167                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
2168                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
2169                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
2170                                 my $oldpriv = $self->{priv};
2171                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
2172                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
2173                                 $self->{priv} = $oldpriv;
2174                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
2175                                 delete $self->{remotecmd};
2176                         } else {
2177                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
2178                         }
2179                 } else {
2180                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
2181                 }
2182         } else {
2183                 my $ref = DXUser->get_current($tonode);
2184                 if ($ref && $ref->is_clx) {
2185                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
2186                 } else {
2187                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
2188                 }
2189         }
2190 }
2191
2192 sub process_rcmd_reply
2193 {
2194         my ($self, $tonode, $fromnode, $user, $line) = @_;
2195         if ($tonode eq $main::mycall) {
2196                 my $s = $rcmds{$fromnode};
2197                 if ($s) {
2198                         my $dxchan = DXChannel->get($s->{call});
2199                         my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
2200                         $ref->send($line) if $ref;
2201                         delete $rcmds{$fromnode} if !$dxchan;
2202                 } else {
2203                         # send unsolicited ones to the sysop
2204                         my $dxchan = DXChannel->get($main::myalias);
2205                         $dxchan->send($line) if $dxchan;
2206                 }
2207         } else {
2208                 my $ref = DXUser->get_current($tonode);
2209                 if ($ref && $ref->is_clx) {
2210                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
2211                 } else {
2212                         $self->route($tonode, pc35($fromnode, $tonode, $line));
2213                 }
2214         }
2215 }
2216
2217 sub send_rcmd_reply
2218 {
2219         my $self = shift;
2220         my $tonode = shift;
2221         my $fromnode = shift;
2222         my $user = shift;
2223         while (@_) {
2224                 my $line = shift;
2225                 $line =~ s/\s*$//;
2226                 Log('rcmd', 'out', $fromnode, $line);
2227                 if ($self->is_clx) {
2228                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
2229                 } else {
2230                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
2231                 }
2232         }
2233 }
2234
2235 # add a rcmd request to the rcmd queues
2236 sub addrcmd
2237 {
2238         my ($self, $to, $cmd) = @_;
2239
2240         my $r = {};
2241         $r->{call} = $self->{call};
2242         $r->{t} = $main::systime;
2243         $r->{cmd} = $cmd;
2244         $rcmds{$to} = $r;
2245         
2246         my $ref = Route::Node::get($to);
2247         my $dxchan = $ref->dxchan;
2248         if ($dxchan && $dxchan->is_clx) {
2249                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
2250         } else {
2251                 route(undef, $to, pc34($main::mycall, $to, $cmd));
2252         }
2253 }
2254
2255 sub disconnect
2256 {
2257         my $self = shift;
2258         my $pc39flag = shift;
2259         my $call = $self->call;
2260
2261         return if $self->{disconnecting}++;
2262         
2263         unless ($pc39flag && $pc39flag == 1) {
2264                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
2265         }
2266
2267         # get rid of any PC16/17/19
2268         eph_del_regex("^PC1[679]*$call");
2269
2270         # do routing stuff, remove me from routing table
2271         my $node = Route::Node::get($call);
2272         my @rout;
2273         if ($node) {
2274                 @rout = $node->del($main::routeroot);
2275                 
2276                 # and all my ephemera as well
2277                 for (@rout) {
2278                         my $c = $_->call;
2279                         eph_del_regex("^PC1[679].*$c");
2280                 }
2281         }
2282         
2283         # remove them from the pc19list as well
2284         while (my ($k,$v) = each %pc19list) {
2285                 my @l = grep {$_->[0] ne $call} @{$pc19list{$k}};
2286                 if (@l) {
2287                         $pc19list{$k} = \@l;
2288                 } else {
2289                         delete $pc19list{$k};
2290                 }
2291                 
2292                 # and the ephemera
2293                 eph_del_regex("^PC1[679].*$k");
2294         }
2295
2296         # unbusy and stop and outgoing mail
2297         my $mref = DXMsg::get_busy($call);
2298         $mref->stop_msg($call) if $mref;
2299         
2300         # broadcast to all other nodes that all the nodes connected to via me are gone
2301         unless ($pc39flag && $pc39flag == 2) {
2302                 $self->route_pc21(@rout) if @rout;
2303         }
2304
2305         # remove outstanding pings
2306         delete $pings{$call};
2307         
2308         # I was the last node visited
2309     $self->user->node($main::mycall);
2310
2311         # send info to all logged in thingies
2312         $self->tell_login('logoutn');
2313
2314         Log('DXProt', $call . " Disconnected");
2315
2316         $self->SUPER::disconnect;
2317 }
2318
2319
2320
2321 # send a talk message to this thingy
2322 #
2323 sub talk
2324 {
2325         my ($self, $from, $to, $via, $line, $origin) = @_;
2326         
2327         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
2328         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
2329         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
2330 }
2331
2332 # send it if it isn't the except list and isn't isolated and still has a hop count
2333 # taking into account filtering and so on
2334 sub send_route
2335 {
2336         my $self = shift;
2337         my $generate = shift;
2338         my $no = shift;     # the no of things to filter on 
2339         my $routeit;
2340         my ($filter, $hops);
2341         my @rin;
2342         
2343         for (; @_ && $no; $no--) {
2344                 my $r = shift;
2345                 
2346                 if (!$self->{isolate} && $self->{routefilter}) {
2347                         $filter = undef;
2348                         if ($r) {
2349                                 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->{state}, $r->{state});
2350                                 if ($filter) {
2351                                         push @rin, $r;
2352                                 } else {
2353                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
2354                                 }
2355                         } else {
2356                                 dbg("was sent a null value") if isdbg('chanerr');
2357                         }
2358                 } else {
2359                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
2360                 }
2361         }
2362         if (@rin) {
2363                 foreach my $line (&$generate(@rin, @_)) {
2364                         if ($hops) {
2365                                 $routeit = $line;
2366                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
2367                         } else {
2368                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
2369                                 next unless $routeit;
2370                         }
2371                         $self->send($routeit);
2372                 }
2373         }
2374 }
2375
2376 sub broadcast_route
2377 {
2378         my $self = shift;
2379         my $generate = shift;
2380         my @dxchan = DXChannel::get_all_nodes();
2381         my $dxchan;
2382         my $line;
2383         
2384         unless ($self->{isolate}) {
2385                 foreach $dxchan (@dxchan) {
2386                         next if $dxchan == $self;
2387                         next if $dxchan == $main::me;
2388                         next unless $dxchan->isa('DXProt');
2389                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
2390  
2391                         $dxchan->send_route($generate, @_);
2392                 }
2393         }
2394 }
2395
2396 sub route_pc16
2397 {
2398         my $self = shift;
2399         return unless $self->user->wantpc16;
2400         broadcast_route($self, \&pc16, 1, @_);
2401 }
2402
2403 sub route_pc17
2404 {
2405         my $self = shift;
2406         return unless $self->user->wantpc16;
2407         broadcast_route($self, \&pc17, 1, @_);
2408 }
2409
2410 sub route_pc19
2411 {
2412         my $self = shift;
2413         broadcast_route($self, \&pc19, scalar @_, @_);
2414 }
2415
2416 sub route_pc21
2417 {
2418         my $self = shift;
2419         broadcast_route($self, \&pc21, scalar @_, @_);
2420 }
2421
2422 sub route_pc24
2423 {
2424         my $self = shift;
2425         broadcast_route($self, \&pc24, 1, @_);
2426 }
2427
2428 sub route_pc41
2429 {
2430         my $self = shift;
2431         broadcast_route($self, \&pc41, 1, @_);
2432 }
2433
2434 sub route_pc50
2435 {
2436         my $self = shift;
2437         broadcast_route($self, \&pc50, 1, @_);
2438 }
2439
2440 sub in_filter_route
2441 {
2442         my $self = shift;
2443         my $r = shift;
2444         my ($filter, $hops) = (1, 1);
2445         
2446         if ($self->{inroutefilter}) {
2447                 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq, $self->state, $r->state);
2448                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
2449         }
2450         return $filter;
2451 }
2452
2453 sub eph_dup
2454 {
2455         my $s = shift;
2456         my $t = shift || $eph_restime;
2457         my $r;
2458
2459         # chop the end off
2460         $s =~ s/\^H\d\d?\^?\~?$//;
2461         removepc90($s);
2462         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
2463         $eph{$s} = $main::systime + $t;
2464         return $r;
2465 }
2466
2467 sub eph_del_regex
2468 {
2469         my $regex = shift;
2470         my ($key, $val);
2471         while (($key, $val) = each %eph) {
2472                 if ($key =~ m{$regex}) {
2473                         delete $eph{$key};
2474                 }
2475         }
2476 }
2477
2478 sub eph_clean
2479 {
2480         my ($key, $val);
2481         
2482         while (($key, $val) = each %eph) {
2483                 if ($main::systime >= $val) {
2484                         delete $eph{$key};
2485                 }
2486         }
2487 }
2488
2489 sub eph_list
2490 {
2491         my ($key, $val);
2492         my @out;
2493
2494         while (($key, $val) = each %eph) {
2495                 push @out, $key, $val;
2496         }
2497         return @out;
2498 }
2499
2500 sub run_cmd
2501 {
2502         goto &DXCommandmode::run_cmd;
2503 }
2504 1;
2505 __END__