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