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