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