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