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