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