use the correct call 'new_cf' everywhere
[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} = 5252 + $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         my $thing = Thingy::Rt->new_cf(user=>$self->{call});
1098         my $nref = Route::Node::get($self->{call});
1099         $thing->broadcast if $thing->copy_pc16_data($nref);
1100 }
1101                 
1102 # delete a cluster from the list
1103 sub handle_21
1104 {
1105         my $self = shift;
1106         my $pcno = shift;
1107         my $line = shift;
1108         my $origin = shift;
1109         my $call = uc $_[1];
1110
1111         eph_del_regex("^PC1[679].*$call");
1112                         
1113         # if I get a PC21 from the same callsign as self then treat it
1114         # as a PC39: I have gone away
1115         if ($call eq $self->call) {
1116                 $self->disconnect(1);
1117                 return;
1118         }
1119
1120         RouteDB::delete($call, $origin);
1121
1122         # check if we believe this
1123         unless ($call eq $origin || $self->is_believed($call)) {
1124                 if (my $ivp = Investigate::get($call, $origin)) {
1125                         $ivp->store_pcxx($pcno,$line,$origin,@_);
1126                 } else {
1127                         dbg("PCPROT: We don't believe $call on $origin") if isdbg('chanerr');
1128                 }
1129                 return;
1130         }
1131
1132         # check to see if we are in the pc19list, if we are then don't bother with any of
1133         # this routing table manipulation, just remove it from the list and dump it
1134         my @rout;
1135         if (my $nl = $pc19list{$call}) {
1136                 $pc19list{$call} = [ grep {$_->[0] ne $origin} @$nl ];
1137                 delete $pc19list{$call} unless @{$pc19list{$call}};
1138         } else {
1139                                 
1140                 my $parent = Route::Node::get($origin);
1141                 unless ($parent) {
1142                         dbg("DXPROT: my parent $origin has disappeared");
1143                         $self->disconnect;
1144                         return;
1145                 }
1146                 if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
1147                         my $node = Route::Node::get($call);
1148                         if ($node) {
1149                                                 
1150                                 my $dxchan = DXChannel::get($call);
1151                                 if ($dxchan && $dxchan != $self) {
1152                                         dbg("PCPROT: PC21 from $origin trying to alter locally connected $call, ignored!") if isdbg('chanerr');
1153                                         return;
1154                                 }
1155                                                 
1156                                 # input filter it
1157                                 return unless $self->in_filter_route($node);
1158                                                 
1159                                 # routing objects
1160                                 push @rout, $node->del($parent);
1161                         }
1162                 } else {
1163                         dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
1164                         return;
1165                 }
1166         }
1167
1168         $self->route_pc21($origin, $line, @rout) if @rout;
1169 }
1170                 
1171
1172 sub handle_22
1173 {
1174         my $self = shift;
1175         my $pcno = shift;
1176         my $line = shift;
1177         my $origin = shift;
1178         $self->state('normal');
1179         $self->{lastping} = 0;
1180         my $thing = Thingy::Rt->new_cf(user=>$self->{call});
1181         my $nref = Route::Node::get($self->{call});
1182         $thing->broadcast if $thing->copy_pc16_data($nref);
1183 }
1184                                 
1185 # WWV info
1186 sub handle_23
1187 {
1188         my $self = shift;
1189         my $pcno = shift;
1190         my $line = shift;
1191         my $origin = shift;
1192                         
1193         # route foreign' pc27s 
1194         if ($pcno == 27) {
1195                 if ($_[8] ne $main::mycall) {
1196                         $self->route($_[8], $line);
1197                         return;
1198                 }
1199         }
1200
1201         # only do a rspf check on PC23 (not 27)
1202         if ($pcno == 23) {
1203                 return if $rspfcheck and !$self->rspfcheck(1, $_[8], $_[7])
1204         }
1205
1206         # do some de-duping
1207         my $d = cltounix($_[1], sprintf("%02d18Z", $_[2]));
1208         my $sfi = unpad($_[3]);
1209         my $k = unpad($_[4]);
1210         my $i = unpad($_[5]);
1211         my ($r) = $_[6] =~ /R=(\d+)/;
1212         $r = 0 unless $r;
1213         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1214                 dbg("PCPROT: WWV Date ($_[1] $_[2]) out of range") if isdbg('chanerr');
1215                 return;
1216         }
1217         if (Geomag::dup($d,$sfi,$k,$i,$_[6])) {
1218                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
1219                 return;
1220         }
1221         $_[7] =~ s/-\d+$//o;            # remove spotter's ssid
1222                 
1223         my $wwv = Geomag::update($d, $_[2], $sfi, $k, $i, @_[6..8], $r);
1224
1225         my $rep;
1226         eval {
1227                 $rep = Local::wwv($self, $_[1], $_[2], $sfi, $k, $i, @_[6..8], $r);
1228         };
1229         #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
1230         return if $rep;
1231
1232         # DON'T be silly and send on PC27s!
1233         return if $pcno == 27;
1234
1235         # broadcast to the eager world
1236         send_wwv_spot($self, $line, $d, $_[2], $sfi, $k, $i, @_[6..8]);
1237 }
1238                 
1239 # set here status
1240 sub handle_24
1241 {
1242         my $self = shift;
1243         my $pcno = shift;
1244         my $line = shift;
1245         my $origin = shift;
1246         my $call = uc $_[1];
1247         my ($nref, $uref);
1248         $nref = Route::Node::get($call);
1249         $uref = Route::User::get($call);
1250         return unless $nref || $uref; # if we don't know where they are, it's pointless sending it on
1251                         
1252         if (eph_dup($line)) {
1253                 dbg("PCPROT: Dup PC24 ignored\n") if isdbg('chanerr');
1254                 return;
1255         }
1256         
1257         $nref->here($_[2]) if $nref;
1258         $uref->here($_[2]) if $uref;
1259         my $ref = $nref || $uref;
1260         return unless $self->in_filter_route($ref);
1261
1262         $self->route_pc24($origin, $line, $ref, $_[3]);
1263 }
1264                 
1265 # merge request
1266 sub handle_25
1267 {
1268         my $self = shift;
1269         my $pcno = shift;
1270         my $line = shift;
1271         my $origin = shift;
1272         if ($_[1] ne $main::mycall) {
1273                 $self->route($_[1], $line);
1274                 return;
1275         }
1276         if ($_[2] eq $main::mycall) {
1277                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
1278                 return;
1279         }
1280
1281         Log('DXProt', "Merge request for $_[3] spots and $_[4] WWV from $_[2]");
1282                         
1283         # spots
1284         if ($_[3] > 0) {
1285                 my @in = reverse Spot::search(1, undef, undef, 0, $_[3]);
1286                 my $in;
1287                 foreach $in (@in) {
1288                         $self->send(pc26(@{$in}[0..4], $_[2]));
1289                 }
1290         }
1291
1292         # wwv
1293         if ($_[4] > 0) {
1294                 my @in = reverse Geomag::search(0, $_[4], time, 1);
1295                 my $in;
1296                 foreach $in (@in) {
1297                         $self->send(pc27(@{$in}[0..5], $_[2]));
1298                 }
1299         }
1300 }
1301
1302 sub handle_26 {goto &handle_11}
1303 sub handle_27 {goto &handle_23}
1304
1305 # mail/file handling
1306 sub handle_28
1307 {
1308         my $self = shift;
1309         my $pcno = shift;
1310         my $line = shift;
1311         my $origin = shift;
1312         if ($_[1] eq $main::mycall) {
1313                 no strict 'refs';
1314                 my $sub = "DXMsg::handle_$pcno";
1315                 &$sub($self, @_);
1316         } else {
1317                 $self->route($_[1], $line) unless $self->is_clx;
1318         }
1319 }
1320
1321 sub handle_29 {goto &handle_28}
1322 sub handle_30 {goto &handle_28}
1323 sub handle_31 {goto &handle_28}
1324 sub handle_32 {goto &handle_28}
1325 sub handle_33 {goto &handle_28}
1326                 
1327 sub handle_34
1328 {
1329         my $self = shift;
1330         my $pcno = shift;
1331         my $line = shift;
1332         my $origin = shift;
1333         if (eph_dup($line, $eph_pc34_restime)) {
1334                 dbg("PCPROT: dupe PC34, ignored") if isdbg('chanerr');
1335         } else {
1336                 $self->process_rcmd($_[1], $_[2], $_[2], $_[3]);
1337         }
1338 }
1339                 
1340 # remote command replies
1341 sub handle_35
1342 {
1343         my $self = shift;
1344         my $pcno = shift;
1345         my $line = shift;
1346         my $origin = shift;
1347         eph_del_regex("^PC35\\^$_[2]\\^$_[1]\\^");
1348         $self->process_rcmd_reply($_[1], $_[2], $_[1], $_[3]);
1349 }
1350                 
1351 sub handle_36 {goto &handle_34}
1352
1353 # database stuff
1354 sub handle_37
1355 {
1356         my $self = shift;
1357         my $pcno = shift;
1358         my $line = shift;
1359         my $origin = shift;
1360         if ($_[1] eq $main::mycall) {
1361                 no strict 'refs';
1362                 my $sub = "DXDb::handle_$pcno";
1363                 &$sub($self, @_);
1364         } else {
1365                 $self->route($_[1], $line) unless $self->is_clx;
1366         }
1367 }
1368
1369 # node connected list from neighbour
1370 sub handle_38
1371 {
1372         my $self = shift;
1373         my $pcno = shift;
1374         my $line = shift;
1375         my $origin = shift;
1376 }
1377                 
1378 # incoming disconnect
1379 sub handle_39
1380 {
1381         my $self = shift;
1382         my $pcno = shift;
1383         my $line = shift;
1384         my $origin = shift;
1385         if ($_[1] eq $origin) {
1386                 $self->disconnect(1);
1387         } else {
1388                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
1389         }
1390 }
1391
1392 sub handle_40 {goto &handle_28}
1393                 
1394 # user info
1395 sub handle_41
1396 {
1397         my $self = shift;
1398         my $pcno = shift;
1399         my $line = shift;
1400         my $origin = shift;
1401         my $call = $_[1];
1402
1403         my $l = $line;
1404         $l =~ s/[\x00-\x20\x7f-\xff]+//g; # remove all funny characters and spaces for dup checking
1405         if (eph_dup($l, $eph_info_restime)) {
1406                 dbg("PCPROT: dup PC41, ignored") if isdbg('chanerr');
1407                 return;
1408         }
1409                         
1410         # input filter if required
1411         #                       my $ref = Route::get($call) || Route->new($call);
1412         #                       return unless $self->in_filter_route($ref);
1413
1414         if ($_[3] eq $_[2] || $_[3] =~ /^\s*$/) {
1415                 dbg('PCPROT: invalid value') if isdbg('chanerr');
1416                 return;
1417         }
1418
1419         # add this station to the user database, if required
1420         my $user = DXUser->get_current($call);
1421         $user = DXUser->new($call) unless $user;
1422                         
1423         if ($_[2] == 1) {
1424                 $user->name($_[3]);
1425         } elsif ($_[2] == 2) {
1426                 $user->qth($_[3]);
1427         } elsif ($_[2] == 3) {
1428                 if (is_latlong($_[3])) {
1429                         my ($lat, $long) = DXBearing::stoll($_[3]);
1430                         $user->lat($lat);
1431                         $user->long($long);
1432                         $user->qra(DXBearing::lltoqra($lat, $long));
1433                 } else {
1434                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1435                         return;
1436                 }
1437         } elsif ($_[2] == 4) {
1438                 $user->homenode($_[3]);
1439         } elsif ($_[2] == 5) {
1440                 if (is_qra(uc $_[3])) {
1441                         my ($lat, $long) = DXBearing::qratoll(uc $_[3]);
1442                         $user->lat($lat);
1443                         $user->long($long);
1444                         $user->qra(uc $_[3]);
1445                 } else {
1446                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1447                         return;
1448                 }
1449         }
1450         $user->lastoper($main::systime); # to cut down on excessive for/opers being generated
1451         $user->put;
1452
1453         unless ($self->{isolate}) {
1454                 DXChannel::broadcast_nodes($line, $self); # send it to everyone but me
1455         }
1456
1457         #  perhaps this IS what we want after all
1458         #                       $self->route_pc41($ref, $call, $_[2], $_[3], $_[4]);
1459 }
1460
1461 sub handle_42 {goto &handle_28}
1462
1463
1464 # database
1465 sub handle_44 {goto &handle_37}
1466 sub handle_45 {goto &handle_37}
1467 sub handle_46 {goto &handle_37}
1468 sub handle_47 {goto &handle_37}
1469 sub handle_48 {goto &handle_37}
1470                 
1471 # message and database
1472 sub handle_49
1473 {
1474         my $self = shift;
1475         my $pcno = shift;
1476         my $line = shift;
1477         my $origin = shift;
1478
1479         if (eph_dup($line)) {
1480                 dbg("PCPROT: Dup PC49 ignored\n") if isdbg('chanerr');
1481                 return;
1482         }
1483         
1484         if ($_[1] eq $main::mycall) {
1485                 DXMsg::handle_49($self, @_);
1486         } else {
1487                 $self->route($_[1], $line) unless $self->is_clx;
1488         }
1489 }
1490
1491 # keep alive/user list
1492 sub handle_50
1493 {
1494         my $self = shift;
1495         my $pcno = shift;
1496         my $line = shift;
1497         my $origin = shift;
1498
1499         my $call = $_[1];
1500
1501         RouteDB::update($call, $origin);
1502
1503         my $node = Route::Node::get($call);
1504         if ($node) {
1505                 return unless $node->call eq $origin;
1506                 $node->usercount($_[2]);
1507
1508                 # input filter if required
1509                 return unless $self->in_filter_route($node);
1510
1511                 $self->route_pc50($origin, $line, $node, $_[2], $_[3]) unless eph_dup($line);
1512         }
1513 }
1514                 
1515 # incoming ping requests/answers
1516 sub handle_51
1517 {
1518         my $self = shift;
1519         my $pcno = shift;
1520         my $line = shift;
1521         my $origin = shift;
1522         my $to = $_[1];
1523         my $from = $_[2];
1524         my $flag = $_[3];
1525
1526                         
1527         # is it for us?
1528         if ($to eq $main::mycall) {
1529                 if ($flag == 1) {
1530                         $self->send(pc51($from, $to, '0'));
1531                 } else {
1532                         # it's a reply, look in the ping list for this one
1533                         my $ref = $pings{$from};
1534                         if ($ref) {
1535                                 my $tochan =  DXChannel::get($from);
1536                                 while (@$ref) {
1537                                         my $r = shift @$ref;
1538                                         my $dxchan = DXChannel::get($r->{call});
1539                                         next unless $dxchan;
1540                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
1541                                         if ($dxchan->is_user) {
1542                                                 my $s = sprintf "%.2f", $t; 
1543                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1544                                                 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1545                                         } elsif ($dxchan->is_node) {
1546                                                 if ($tochan) {
1547                                                         my $nopings = $tochan->user->nopings || $obscount;
1548                                                         push @{$tochan->{pingtime}}, $t;
1549                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1550                                                         
1551                                                         # cope with a missed ping, this means you must set the pingint large enough
1552                                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
1553                                                                 $t -= $tochan->{pingint};
1554                                                         }
1555                                                         
1556                                                         # calc smoothed RTT a la TCP
1557                                                         if (@{$tochan->{pingtime}} == 1) {
1558                                                                 $tochan->{pingave} = $t;
1559                                                         } else {
1560                                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1561                                                         }
1562                                                         $tochan->{nopings} = $nopings; # pump up the timer
1563                                                         if (my $ivp = Investigate::get($from, $origin)) {
1564                                                                 $ivp->handle_ping;
1565                                                         }
1566                                                 } elsif (my $rref = Route::Node::get($r->{call})) {
1567                                                         if (my $ivp = Investigate::get($from, $origin)) {
1568                                                                 $ivp->handle_ping;
1569                                                         }
1570                                                 }
1571                                         }
1572                                 }
1573                         }
1574                 }
1575         } else {
1576
1577                 RouteDB::update($from, $origin);
1578
1579                 if (eph_dup($line)) {
1580                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1581                         return;
1582                 }
1583                 # route down an appropriate thingy
1584                 $self->route($to, $line);
1585         }
1586 }
1587
1588 # dunno but route it
1589 sub handle_75
1590 {
1591         my $self = shift;
1592         my $pcno = shift;
1593         my $line = shift;
1594         my $origin = shift;
1595         my $call = $_[1];
1596         if ($call ne $main::mycall) {
1597                 $self->route($call, $line);
1598         }
1599 }
1600
1601 # WCY broadcasts
1602 sub handle_73
1603 {
1604         my $self = shift;
1605         my $pcno = shift;
1606         my $line = shift;
1607         my $origin = shift;
1608         my $call = $_[1];
1609                         
1610         # do some de-duping
1611         my $d = cltounix($call, sprintf("%02d18Z", $_[2]));
1612         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $_[2] < 0 || $_[2] > 23) {
1613                 dbg("PCPROT: WCY Date ($call $_[2]) out of range") if isdbg('chanerr');
1614                 return;
1615         }
1616         @_ = map { unpad($_) } @_;
1617         if (WCY::dup($d)) {
1618                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1619                 return;
1620         }
1621                 
1622         my $wcy = WCY::update($d, @_[2..12]);
1623
1624         my $rep;
1625         eval {
1626                 $rep = Local::wcy($self, @_[1..12]);
1627         };
1628         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1629         return if $rep;
1630
1631         # broadcast to the eager world
1632         send_wcy_spot($self, $line, $d, @_[2..12]);
1633 }
1634
1635 # remote commands (incoming)
1636 sub handle_84
1637 {
1638         my $self = shift;
1639         my $pcno = shift;
1640         my $line = shift;
1641         my $origin = shift;
1642         $self->process_rcmd($_[1], $_[2], $_[3], $_[4]);
1643 }
1644
1645 # remote command replies
1646 sub handle_85
1647 {
1648         my $self = shift;
1649         my $pcno = shift;
1650         my $line = shift;
1651         my $origin = shift;
1652         $self->process_rcmd_reply($_[1], $_[2], $_[3], $_[4]);
1653 }
1654
1655 # if get here then rebroadcast the thing with its Hop count decremented (if
1656 # there is one). If it has a hop count and it decrements to zero then don't
1657 # rebroadcast it.
1658 #
1659 # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1660 #        REBROADCAST!!!!
1661 #
1662
1663 sub handle_default
1664 {
1665         my $self = shift;
1666         my $pcno = shift;
1667         my $line = shift;
1668         my $origin = shift;
1669
1670         if (eph_dup($line)) {
1671                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1672         } else {
1673                 unless ($self->{isolate}) {
1674                         DXChannel::broadcast_nodes($line, $self) if $line =~ /\^H\d+\^?~?$/; # send it to everyone but me
1675                 }
1676         }
1677 }
1678
1679 #
1680 # This is called from inside the main cluster processing loop and is used
1681 # for despatching commands that are doing some long processing job
1682 #
1683 sub process
1684 {
1685         my $t = time;
1686         my @dxchan = DXChannel::get_all();
1687         my $dxchan;
1688         my $pc50s;
1689         
1690         # send out a pc50 on EVERY channel all at once
1691         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1692                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
1693                 eph_dup($pc50s);
1694                 $last_pc50 = $t;
1695         }
1696
1697         foreach $dxchan (@dxchan) {
1698                 next unless $dxchan->is_node();
1699                 next if $dxchan == $main::me;
1700
1701                 # send the pc50
1702                 $dxchan->send($pc50s) if $pc50s;
1703                 
1704                 # send a ping out on this channel
1705                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1706                         if ($dxchan->{nopings} <= 0) {
1707                                 $dxchan->disconnect;
1708                         } else {
1709                                 addping($main::mycall, $dxchan->call);
1710                                 $dxchan->{nopings} -= 1;
1711                                 $dxchan->{lastping} = $t;
1712                         }
1713                 }
1714         }
1715
1716         Investigate::process();
1717
1718         # every ten seconds
1719         if ($t - $last10 >= 10) {       
1720                 # clean out ephemera 
1721
1722                 eph_clean();
1723                 import_chat();
1724                 
1725
1726                 $last10 = $t;
1727         }
1728         
1729         if ($main::systime - 3600 > $last_hour) {
1730                 $last_hour = $main::systime;
1731         }
1732 }
1733
1734 #
1735 # finish up a pc context
1736 #
1737
1738 #
1739 # some active measures
1740 #
1741
1742
1743 sub send_prot_line
1744 {
1745         my ($self, $filter, $hops, $isolate, $line) = @_;
1746         my $routeit;
1747
1748
1749         if ($hops) {
1750                 $routeit = $line;
1751                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1752         } else {
1753                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1754                 return unless $routeit;
1755         }
1756         if ($filter) {
1757                 $self->send($routeit);
1758         } else {
1759                 $self->send($routeit) unless $self->{isolate} || $isolate;
1760         }
1761 }
1762
1763
1764 sub send_wwv_spot
1765 {
1766         my $self = shift;
1767         my $line = shift;
1768         my @dxchan = DXChannel::get_all();
1769         my $dxchan;
1770         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
1771
1772         # send it if it isn't the except list and isn't isolated and still has a hop count
1773         # taking into account filtering and so on
1774         foreach $dxchan (@dxchan) {
1775                 next if $dxchan == $main::me;
1776                 next if $dxchan == $self && $self->is_node;
1777                 my $routeit;
1778                 my ($filter, $hops);
1779
1780                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1781         }
1782 }
1783
1784 sub wwv
1785 {
1786         my $self = shift;
1787         my $line = shift;
1788         my $isolate = shift;
1789         my ($filter, $hops);
1790         
1791         if ($self->{wwvfilter}) {
1792                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1793                 return unless $filter;
1794         }
1795         send_prot_line($self, $filter, $hops, $isolate, $line)
1796 }
1797
1798 sub send_wcy_spot
1799 {
1800         my $self = shift;
1801         my $line = shift;
1802         my @dxchan = DXChannel::get_all();
1803         my $dxchan;
1804         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
1805         
1806         # send it if it isn't the except list and isn't isolated and still has a hop count
1807         # taking into account filtering and so on
1808         foreach $dxchan (@dxchan) {
1809                 next if $dxchan == $main::me;
1810                 next if $dxchan == $self;
1811
1812                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
1813         }
1814 }
1815
1816 sub wcy
1817 {
1818         my $self = shift;
1819         my $line = shift;
1820         my $isolate = shift;
1821         my ($filter, $hops);
1822
1823         if ($self->{wcyfilter}) {
1824                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1825                 return unless $filter;
1826         }
1827         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1828 }
1829
1830 # send an announce
1831 sub send_announce
1832 {
1833         my $self = shift;
1834         my $line = shift;
1835         my @dxchan = DXChannel::get_all();
1836         my $dxchan;
1837         my $target;
1838         my $to = 'To ';
1839         my $text = unpad($_[2]);
1840                                 
1841         if ($_[3] eq '*') {     # sysops
1842                 $target = "SYSOP";
1843         } elsif ($_[3] gt ' ') { # speciality list handling
1844                 my ($name) = split /\./, $_[3]; 
1845                 $target = "$name"; # put the rest in later (if bothered) 
1846         } 
1847         
1848         if ($_[5] eq '1') {
1849                 $target = "WX"; 
1850                 $to = '';
1851         }
1852         $target = "ALL" if !$target;
1853
1854
1855         # obtain country codes etc 
1856         my @a = Prefix::cty_data($_[0]);
1857         my @b = Prefix::cty_data($_[4]);
1858         if ($self->{inannfilter}) {
1859                 my ($filter, $hops) = 
1860                         $self->{inannfilter}->it(@_, $self->{call}, 
1861                                                                          @a[0..2],
1862                                                                          @b[0..2], $a[3], $b[3]);
1863                 unless ($filter) {
1864                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1865                         return;
1866                 }
1867         }
1868
1869         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
1870                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1871                 return;
1872         }
1873
1874         Log('ann', $target, $_[0], $text);
1875
1876         # send it if it isn't the except list and isn't isolated and still has a hop count
1877         # taking into account filtering and so on
1878         foreach $dxchan (@dxchan) {
1879                 next if $dxchan == $main::me;
1880                 next if $dxchan == $self && $self->is_node;
1881                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
1882                                                   @a[0..2], @b[0..2]);
1883         }
1884 }
1885
1886 my $msgid = 0;
1887
1888 sub nextchatmsgid
1889 {
1890         $msgid++;
1891         $msgid = 1 if $msgid > 999;
1892         return $msgid;
1893 }
1894
1895 # send a chat line
1896 sub send_chat
1897 {
1898         my $self = shift;
1899         my $line = shift;
1900         my @dxchan = DXChannel::get_all();
1901         my $dxchan;
1902         my $target = $_[3];
1903         my $text = unpad($_[2]);
1904         my $ak1a_line;
1905                                 
1906         # munge the group and recast the line if required
1907         if ($target =~ s/\.LST$//) {
1908                 $ak1a_line = $line;
1909         }
1910         
1911         # obtain country codes etc 
1912         my @a = Prefix::cty_data($_[0]);
1913         my @b = Prefix::cty_data($_[4]);
1914         if ($self->{inannfilter}) {
1915                 my ($filter, $hops) = 
1916                         $self->{inannfilter}->it(@_, $self->{call}, 
1917                                                                          @a[0..2],
1918                                                                          @b[0..2], $a[3], $b[3]);
1919                 unless ($filter) {
1920                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1921                         return;
1922                 }
1923         }
1924
1925         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
1926                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
1927                 return;
1928         }
1929
1930
1931         Log('chat', $target, $_[0], $text);
1932
1933         # send it if it isn't the except list and isn't isolated and still has a hop count
1934         # taking into account filtering and so on
1935         foreach $dxchan (@dxchan) {
1936                 my $is_ak1a = $dxchan->is_ak1a;
1937                 
1938                 if ($dxchan->is_node) {
1939                         next if $dxchan == $main::me;
1940                         next if $dxchan == $self;
1941                         next unless $dxchan->is_spider || $is_ak1a;
1942                         next if $target eq 'LOCAL';
1943                         if (!$ak1a_line && $is_ak1a) {
1944                                 $ak1a_line = DXProt::pc12($_[0], $text, $_[1], "$target.LST");
1945                         }
1946                 }
1947                 
1948                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1], 
1949                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
1950         }
1951 }
1952
1953 sub announce
1954 {
1955         my $self = shift;
1956         my $line = shift;
1957         my $isolate = shift;
1958         my $to = shift;
1959         my $target = shift;
1960         my $text = shift;
1961         my ($filter, $hops);
1962
1963         if ($self->{annfilter}) {
1964                 ($filter, $hops) = $self->{annfilter}->it(@_);
1965                 return unless $filter;
1966         }
1967         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
1968 }
1969
1970 sub chat
1971 {
1972         goto &announce;
1973 }
1974
1975
1976 sub send_local_config
1977 {
1978         my $self = shift;
1979         my $node;
1980         my @nodes;
1981         my @localnodes;
1982         my @remotenodes;
1983
1984         dbg('DXProt::send_local_config') if isdbg('trace');
1985         
1986         # send our nodes
1987         if ($self->{isolate}) {
1988                 @localnodes = ( $main::routeroot );
1989                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
1990         } else {
1991                 # create a list of all the nodes that are not connected to this connection
1992                 # and are not themselves isolated, this to make sure that isolated nodes
1993         # don't appear outside of this node
1994
1995                 # send locally connected nodes
1996                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && ($_->is_node || $_->is_aranea) } DXChannel::get_all();
1997                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
1998                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
1999
2000                 my $node;
2001                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
2002                 my @intcalls;
2003                 for $node (@rawintcalls) {
2004                         push @intcalls, $node unless grep $node eq $_, @intcalls; 
2005                 }
2006                 my $ref = Route::Node::get($self->{call});
2007                 my @rnodes = $ref->nodes;
2008                 for $node (@intcalls) {
2009                         push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
2010                 }
2011                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
2012         }
2013         
2014         # get all the users connected on the above nodes and send them out
2015         foreach $node ($main::routeroot, @localnodes, @remotenodes) {
2016                 if ($node) {
2017                         my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
2018                         $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
2019                 } else {
2020                         dbg("sent a null value") if isdbg('chanerr');
2021                 }
2022         }
2023 }
2024
2025 #
2026 # route a message down an appropriate interface for a callsign
2027 #
2028 # is called route(to, pcline);
2029 #
2030
2031 sub route
2032 {
2033         my ($self, $call, $line) = @_;
2034
2035         if (ref $self && $call eq $self->{call}) {
2036                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2037                 return;
2038         }
2039
2040         # always send it down the local interface if available
2041         my $dxchan = DXChannel::get($call);
2042         if ($dxchan) {
2043                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
2044         } else {
2045                 my $cl = Route::get($call);
2046                 $dxchan = $cl->dxchan if $cl;
2047                 if (ref $dxchan) {
2048                         if (ref $self && $dxchan eq $self) {
2049                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2050                                 return;
2051                         }
2052                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
2053                 }
2054         }
2055
2056         # try the backstop method
2057         unless ($dxchan) {
2058                 my $rcall = RouteDB::get($call);
2059                 if ($rcall) {
2060                         if ($self && $rcall eq $self->{call}) {
2061                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
2062                                 return;
2063                         }
2064                         $dxchan = DXChannel::get($rcall);
2065                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
2066                 }
2067         }
2068
2069         if ($dxchan) {
2070                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
2071                 if ($routeit) {
2072                         $dxchan->send($routeit) unless $dxchan == $main::me;
2073                 }
2074         } else {
2075                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
2076         }
2077 }
2078
2079 #
2080 # obtain the hops from the list for this callsign and pc no 
2081 #
2082
2083 sub get_hops
2084 {
2085         my $pcno = shift;
2086         my $hops = $DXProt::hopcount{$pcno};
2087         $hops = $DXProt::def_hopcount if !$hops;
2088         return "H$hops";       
2089 }
2090
2091
2092 # adjust the hop count on a per node basis using the user loadable 
2093 # hop table if available or else decrement an existing one
2094 #
2095
2096 sub adjust_hops
2097 {
2098         my $self = shift;
2099         my $s = shift;
2100         my $call = $self->{call};
2101         my $hops;
2102         
2103         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
2104                 my ($pcno) = $s =~ /^PC(\d\d)/o;
2105                 confess "$call called adjust_hops with '$s'" unless $pcno;
2106                 my $ref = $nodehops{$call} if %nodehops;
2107                 if ($ref) {
2108                         my $newhops = $ref->{$pcno};
2109                         return "" if defined $newhops && $newhops == 0;
2110                         $newhops = $ref->{default} unless $newhops;
2111                         return "" if defined $newhops && $newhops == 0;
2112                         $newhops = $hops if !$newhops;
2113                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
2114                 } else {
2115                         # simply decrement it
2116 #                       $hops--;               this is done on receipt now
2117                         return "" if !$hops;
2118                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
2119                 }
2120         }
2121         return $s;
2122 }
2123
2124
2125 # load hop tables
2126 #
2127 sub load_hops
2128 {
2129         my $self = shift;
2130         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
2131         do "$main::data/hop_table.pl";
2132         return $@ if $@;
2133         return ();
2134 }
2135
2136
2137 # add a ping request to the ping queues
2138 sub addping
2139 {
2140         my ($from, $to, $via) = @_;
2141         my $ref = $pings{$to} || [];
2142         my $r = {};
2143         $r->{call} = $from;
2144         $r->{t} = [ gettimeofday ];
2145         if ($via && (my $dxchan = DXChannel::get($via))) {
2146                 $dxchan->send(pc51($to, $main::mycall, 1));
2147         } else {
2148                 route(undef, $to, pc51($to, $main::mycall, 1));
2149         }
2150         push @$ref, $r;
2151         $pings{$to} = $ref;
2152         my $u = DXUser->get_current($to);
2153         if ($u) {
2154                 $u->lastping(($via || $from), $main::systime);
2155                 $u->put;
2156         }
2157 }
2158
2159 sub process_rcmd
2160 {
2161         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
2162         if ($tonode eq $main::mycall) {
2163                 my $ref = DXUser->get_current($fromnode);
2164                 my $cref = Route::Node::get($fromnode);
2165                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
2166                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
2167                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
2168                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
2169                                 my $oldpriv = $self->{priv};
2170                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
2171                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
2172                                 $self->{priv} = $oldpriv;
2173                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
2174                                 delete $self->{remotecmd};
2175                         } else {
2176                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
2177                         }
2178                 } else {
2179                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
2180                 }
2181         } else {
2182                 my $ref = DXUser->get_current($tonode);
2183                 if ($ref && $ref->is_clx) {
2184                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
2185                 } else {
2186                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
2187                 }
2188         }
2189 }
2190
2191 sub process_rcmd_reply
2192 {
2193         my ($self, $tonode, $fromnode, $user, $line) = @_;
2194         if ($tonode eq $main::mycall) {
2195                 my $s = $rcmds{$fromnode};
2196                 if ($s) {
2197                         my $dxchan = DXChannel::get($s->{call});
2198                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
2199                         $ref->send($line) if $ref;
2200                         delete $rcmds{$fromnode} if !$dxchan;
2201                 } else {
2202                         # send unsolicited ones to the sysop
2203                         my $dxchan = DXChannel::get($main::myalias);
2204                         $dxchan->send($line) if $dxchan;
2205                 }
2206         } else {
2207                 my $ref = DXUser->get_current($tonode);
2208                 if ($ref && $ref->is_clx) {
2209                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
2210                 } else {
2211                         $self->route($tonode, pc35($fromnode, $tonode, $line));
2212                 }
2213         }
2214 }
2215
2216 sub send_rcmd_reply
2217 {
2218         my $self = shift;
2219         my $tonode = shift;
2220         my $fromnode = shift;
2221         my $user = shift;
2222         while (@_) {
2223                 my $line = shift;
2224                 $line =~ s/\s*$//;
2225                 Log('rcmd', 'out', $fromnode, $line);
2226                 if ($self->is_clx) {
2227                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
2228                 } else {
2229                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
2230                 }
2231         }
2232 }
2233
2234 # add a rcmd request to the rcmd queues
2235 sub addrcmd
2236 {
2237         my ($self, $to, $cmd) = @_;
2238
2239         my $r = {};
2240         $r->{call} = $self->{call};
2241         $r->{t} = $main::systime;
2242         $r->{cmd} = $cmd;
2243         $rcmds{$to} = $r;
2244         
2245         my $ref = Route::Node::get($to);
2246         my $dxchan = $ref->dxchan;
2247         if ($dxchan && $dxchan->is_clx) {
2248                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
2249         } else {
2250                 route(undef, $to, pc34($main::mycall, $to, $cmd));
2251         }
2252 }
2253
2254 sub disconnect
2255 {
2256         my $self = shift;
2257         my $pc39flag = shift;
2258         my $call = $self->call;
2259
2260         return if $self->{disconnecting}++;
2261         
2262         unless ($pc39flag && $pc39flag == 1) {
2263                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
2264         }
2265
2266         # get rid of any PC16/17/19
2267         eph_del_regex("^PC1[679]*$call");
2268
2269         # do routing stuff, remove me from routing table
2270         my $node = Route::Node::get($call);
2271         my @rout;
2272         if ($node) {
2273                 @rout = $node->del($main::routeroot);
2274                 
2275                 # and all my ephemera as well
2276                 for (@rout) {
2277                         my $c = $_->call;
2278                         eph_del_regex("^PC1[679].*$c");
2279                 }
2280         }
2281
2282         RouteDB::delete_interface($call);
2283         
2284         # remove them from the pc19list as well
2285         while (my ($k,$v) = each %pc19list) {
2286                 my @l = grep {$_->[0] ne $call} @{$pc19list{$k}};
2287                 if (@l) {
2288                         $pc19list{$k} = \@l;
2289                 } else {
2290                         delete $pc19list{$k};
2291                 }
2292                 
2293                 # and the ephemera
2294                 eph_del_regex("^PC1[679].*$k");
2295         }
2296
2297         # unbusy and stop and outgoing mail
2298         my $mref = DXMsg::get_busy($call);
2299         $mref->stop_msg($call) if $mref;
2300         
2301         # broadcast to all other nodes that all the nodes connected to via me are gone
2302         unless ($pc39flag && $pc39flag == 2) {
2303                 my $thing = Thingy::Bye->new(user=>$call);
2304                 $thing->broadcast($self);
2305
2306                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
2307         }
2308
2309         # remove outstanding pings
2310         delete $pings{$call};
2311         
2312         # I was the last node visited
2313     $self->user->node($main::mycall);
2314
2315         # send info to all logged in thingies
2316         $self->tell_login('logoutn');
2317
2318         Log('DXProt', $call . " Disconnected");
2319
2320         $self->SUPER::disconnect;
2321 }
2322
2323
2324
2325 # send a talk message to this thingy
2326 #
2327 sub talk
2328 {
2329         my ($self, $from, $to, $via, $line, $origin) = @_;
2330         
2331         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
2332         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
2333         Log('talk', $to, $from, $via?$via:$self->call, $line) unless $origin && $origin ne $main::mycall;
2334 }
2335
2336 # send it if it isn't the except list and isn't isolated and still has a hop count
2337 # taking into account filtering and so on
2338
2339 sub send_route
2340 {
2341         my $self = shift;
2342         my $origin = shift;
2343         my $generate = shift;
2344         my $no = shift;     # the no of things to filter on 
2345         my $routeit;
2346         my ($filter, $hops);
2347         my @rin;
2348         
2349         for (; @_ && $no; $no--) {
2350                 my $r = shift;
2351                 
2352                 if (!$self->{isolate} && $self->{routefilter}) {
2353                         $filter = undef;
2354                         if ($r) {
2355                                 ($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});
2356                                 if ($filter) {
2357                                         push @rin, $r;
2358                                 } else {
2359                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
2360                                 }
2361                         } else {
2362                                 dbg("was sent a null value") if isdbg('chanerr');
2363                         }
2364                 } else {
2365                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
2366                 }
2367         }
2368         if (@rin) {
2369                 foreach my $line (&$generate(@rin, @_)) {
2370                         if ($hops) {
2371                                 $routeit = $line;
2372                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
2373                         } else {
2374                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
2375                                 next unless $routeit;
2376                         }
2377                         
2378                         $self->send($routeit);
2379                 }
2380         }
2381 }
2382
2383 sub broadcast_route
2384 {
2385         my $self = shift;
2386         my $origin = shift;
2387         my $generate = shift;
2388         my $line = shift;
2389         my @dxchan = DXChannel::get_all_nodes();
2390         my $dxchan;
2391         
2392         unless ($self->{isolate}) {
2393                 foreach $dxchan (@dxchan) {
2394                         next if $dxchan == $self;
2395                         next if $dxchan == $main::me;
2396                         next unless $dxchan->isa('DXProt');
2397                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
2398  
2399                         $dxchan->send_route($origin, $generate, @_);
2400                 }
2401         }
2402 }
2403
2404 sub route_pc16
2405 {
2406         my $self = shift;
2407         return unless $self->user->wantpc16;
2408         my $origin = shift;
2409         my $line = shift;
2410         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
2411 }
2412
2413 sub route_pc17
2414 {
2415         my $self = shift;
2416         return unless $self->user->wantpc16;
2417         my $origin = shift;
2418         my $line = shift;
2419         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
2420 }
2421
2422 sub route_pc19
2423 {
2424         my $self = shift;
2425         my $origin = shift;
2426         my $line = shift;
2427         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
2428 }
2429
2430 sub route_pc21
2431 {
2432         my $self = shift;
2433         my $origin = shift;
2434         my $line = shift;
2435         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
2436 }
2437
2438 sub route_pc24
2439 {
2440         my $self = shift;
2441         my $origin = shift;
2442         my $line = shift;
2443         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
2444 }
2445
2446 sub route_pc41
2447 {
2448         my $self = shift;
2449         my $origin = shift;
2450         my $line = shift;
2451         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
2452 }
2453
2454 sub route_pc50
2455 {
2456         my $self = shift;
2457         my $origin = shift;
2458         my $line = shift;
2459         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
2460 }
2461
2462 sub in_filter_route
2463 {
2464         my $self = shift;
2465         my $r = shift;
2466         my ($filter, $hops) = (1, 1);
2467         
2468         if ($self->{inroutefilter}) {
2469                 ($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);
2470                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
2471         }
2472         return $filter;
2473 }
2474
2475 sub eph_dup
2476 {
2477         my $s = shift;
2478         my $t = shift || $eph_restime;
2479         my $r;
2480
2481         # chop the end off
2482         $s =~ s/\^H\d\d?\^?\~?$//;
2483         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
2484         $eph{$s} = $main::systime + $t;
2485         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr'); 
2486         return $r;
2487 }
2488
2489 sub eph_del_regex
2490 {
2491         my $regex = shift;
2492         my ($key, $val);
2493         while (($key, $val) = each %eph) {
2494                 if ($key =~ m{$regex}) {
2495                         delete $eph{$key};
2496                 }
2497         }
2498 }
2499
2500 sub eph_clean
2501 {
2502         my ($key, $val);
2503         
2504         while (($key, $val) = each %eph) {
2505                 if ($main::systime >= $val) {
2506                         delete $eph{$key};
2507                 }
2508         }
2509 }
2510
2511 sub eph_list
2512 {
2513         my ($key, $val);
2514         my @out;
2515
2516         while (($key, $val) = each %eph) {
2517                 push @out, $key, $val;
2518         }
2519         return @out;
2520 }
2521
2522 sub run_cmd
2523 {
2524         goto &DXCommandmode::run_cmd;
2525 }
2526
2527
2528 # import any msgs in the chat directory
2529 # the messages are sent to the chat group which forms the
2530 # the first part of the name (eg: solar.1243.txt would be
2531 # sent to chat group SOLAR)
2532
2533 # Each message found is sent: one non-blank line to one chat
2534 # message. So 4 lines = 4 chat messages.
2535
2536 # The special name LOCAL is for local users ANN
2537 # The special name ALL is for ANN/FULL
2538 # The special name SYSOP is for ANN/SYSOP
2539 #
2540 sub import_chat
2541 {
2542         # are there any to do in this directory?
2543         return unless -d $chatimportfn;
2544         unless (opendir(DIR, $chatimportfn)) {
2545                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
2546                 Log('msg', "can\'t open $chatimportfn $!");
2547                 return;
2548         } 
2549
2550         my @names = readdir(DIR);
2551         closedir(DIR);
2552         my $name;
2553         foreach $name (@names) {
2554                 next if $name =~ /^\./;
2555                 my $splitit = $name =~ /^split/;
2556                 my $fn = "$chatimportfn/$name";
2557                 next unless -f $fn;
2558                 unless (open(MSG, $fn)) {
2559                         dbg("can\'t open import file $fn $!") if isdbg('msg');
2560                         Log('msg', "can\'t open import file $fn $!");
2561                         unlink($fn);
2562                         next;
2563                 }
2564                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
2565                 close(MSG);
2566                 unlink($fn);
2567
2568                 my @cat = split /\./, $name;
2569                 my $target = uc $cat[0];
2570
2571                 foreach my $text (@msg) {
2572                         next unless $text && $text !~ /^\s*#/;
2573                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
2574                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
2575                                 if ($target ne 'LOCAL') {
2576                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
2577                                 } else {
2578                                         Log('ann', 'LOCAL', $main::mycall, $text);
2579                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
2580                                 }
2581                         } else {
2582                                 my $msgid = nextchatmsgid();
2583                                 $text = "#$msgid $text";
2584                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
2585                         }
2586                 }
2587         }
2588 }
2589
2590 1;
2591 __END__