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