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