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