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