added some rsfp checking
[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
37 use strict;
38
39 use vars qw($VERSION $BRANCH);
40 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
41 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
42 $main::build += $VERSION;
43 $main::branch += $BRANCH;
44
45 use vars qw($me $pc11_max_age $pc23_max_age $last_pc50
46                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
47                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
48                         $allowzero $decode_dk0wcy $send_opernam @checklist);
49
50 $me = undef;                                    # the channel id for this cluster
51 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
52 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
53
54 $last_hour = time;                              # last time I did an hourly periodic update
55 %pings = ();                    # outstanding ping requests outbound
56 %rcmds = ();                    # outstanding rcmd requests outbound
57 %nodehops = ();                 # node specific hop control
58 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
59                                                                 # loads of 'bad things'
60 $baddx = new DXHash "baddx";
61 $badspotter = new DXHash "badspotter";
62 $badnode = new DXHash "badnode";
63 $last10 = $last_pc50 = time;
64 $ann_to_talk = 1;
65 $rspfcheck = 1;
66
67 @checklist = 
68 (
69  [ qw(c c m bp bc c) ],                 # pc10
70  [ qw(f m d t m c c h) ],               # pc11
71  [ qw(c bc m bp bm p h) ],              # pc12
72  [ qw(c h) ],                                   # 
73  [ qw(c h) ],                                   # 
74  [ qw(c m h) ],                                 # 
75  undef ,                                                # pc16 has to be validated manually
76  [ qw(c c h) ],                                 # pc17
77  [ qw(m n) ],                                   # pc18
78  undef ,                                                # pc19 has to be validated manually
79  undef ,                                                # pc20 no validation
80  [ qw(c m h) ],                                 # pc21
81  undef ,                                                # pc22 no validation
82  [ qw(d n n n n m c c h) ],             # pc23
83  [ qw(c p h) ],                                 # pc24
84  [ qw(c c n n) ],                               # pc25
85  [ qw(f m d t m c c bc) ],              # pc26
86  [ qw(d n n n n m c c bc) ],    # pc27
87  [ qw(c c m c d t p m bp n p bp bc) ], # pc28
88  [ qw(c c n m) ],                               # pc29
89  [ qw(c c n) ],                                 # pc30
90  [ qw(c c n) ],                                 # pc31
91  [ qw(c c n) ],                                 # pc32
92  [ qw(c c n) ],                                 # pc33
93  [ qw(c c m) ],                                 # pc34
94  [ qw(c c m) ],                                 # pc35
95  [ qw(c c m) ],                                 # pc36
96  [ qw(c c n m) ],                               # pc37
97  undef,                                                 # pc38 not interested
98  [ qw(c m) ],                                   # pc39
99  [ qw(c c m p n) ],                             # pc40
100  [ qw(c n m h) ],                               # pc41
101  [ qw(c c n) ],                                 # pc42
102  undef,                                                 # pc43 don't handle it
103  [ qw(c c n m m c) ],                   # pc44
104  [ qw(c c n m) ],                               # pc45
105  [ qw(c c n) ],                                 # pc46
106  undef,                                                 # pc47
107  undef,                                                 # pc48
108  [ qw(c m h) ],                                 # pc49
109  [ qw(c n h) ],                                 # pc50
110  [ qw(c c n) ],                                 # pc51
111  undef,
112  undef,
113  undef,
114  undef,
115  undef,
116  undef,
117  undef,
118  undef,
119  undef,                                                 # pc60
120  undef,
121  undef,
122  undef,
123  undef,
124  undef,
125  undef,
126  undef,
127  undef,
128  undef,
129  undef,                                                 # pc70
130  undef,
131  undef,
132  [ qw(d n n n n n n m m m c c h) ],     # pc73
133  undef,
134  undef,
135  undef,
136  undef,
137  undef,
138  undef,
139  undef,                                                 # pc80
140  undef,
141  undef,
142  undef,
143  [ qw(c c c m) ],                               # pc84
144  [ qw(c c c m) ],                               # pc85
145 );
146
147 # use the entry in the check list to check the field list presented
148 # return OK if line NOT in check list (for now)
149 sub check
150 {
151         my $n = shift;
152         $n -= 10;
153         return 0 if $n < 0 || $n > @checklist; 
154         my $ref = $checklist[$n];
155         return 0 unless ref $ref;
156         
157         my $i;
158         shift;    # not interested in the first field
159         for ($i = 0; $i < @$ref; $i++) {
160                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
161                 return 0 unless $act;
162                 next if $blank && $_[$i] =~ /^[ \*]$/;
163                 if ($act eq 'c') {
164                         return $i+1 unless is_callsign($_[$i]);
165                 } elsif ($act eq 'm') {
166                         return $i+1 unless is_pctext($_[$i]);
167                 } elsif ($act eq 'p') {
168                         return $i+1 unless is_pcflag($_[$i]);
169                 } elsif ($act eq 'f') {
170                         return $i+1 unless is_freq($_[$i]);
171                 } elsif ($act eq 'n') {
172                         return $i+1 unless $_[$i] =~ /^[\d ]+$/;
173                 } elsif ($act eq 'h') {
174                         return $i+1 unless $_[$i] =~ /^H\d\d?$/;
175                 } elsif ($act eq 'd') {
176                         return $i+1 unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
177                 } elsif ($act eq 't') {
178                         return $i+1 unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
179                 }
180         }
181         return 0;
182 }
183
184 sub init
185 {
186         my $user = DXUser->get($main::mycall);
187         $DXProt::myprot_version += $main::version*100;
188         $me = DXProt->new($main::mycall, 0, $user); 
189         $me->{here} = 1;
190         $me->{state} = "indifferent";
191         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
192         confess $@ if $@;
193         $me->{sort} = 'S';    # S for spider
194         $me->{priv} = 9;
195 #       $Route::Node::me->adddxchan($me);
196 }
197
198 #
199 # obtain a new connection this is derived from dxchannel
200 #
201
202 sub new 
203 {
204         my $self = DXChannel::alloc(@_);
205
206         # add this node to the table, the values get filled in later
207         my $pkg = shift;
208         my $call = shift;
209         $main::routeroot->add($call, '0000', Route::here(1)) if $call ne $main::mycall;
210
211         return $self;
212 }
213
214 # this is how a pc connection starts (for an incoming connection)
215 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
216 # all the crap that comes between).
217 sub start
218 {
219         my ($self, $line, $sort) = @_;
220         my $call = $self->{call};
221         my $user = $self->{user};
222
223         # log it
224         my $host = $self->{conn}->{peerhost} || "unknown";
225         Log('DXProt', "$call connected from $host");
226         
227         # remember type of connection
228         $self->{consort} = $line;
229         $self->{outbound} = $sort eq 'O';
230         my $priv = $user->priv;
231         $priv = $user->priv(1) unless $priv;
232         $self->{priv} = $priv;     # other clusters can always be 'normal' users
233         $self->{lang} = $user->lang || 'en';
234         $self->{isolate} = $user->{isolate};
235         $self->{consort} = $line;       # save the connection type
236         $self->{here} = 1;
237         $self->{width} = 80;
238
239         # get the output filters
240         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
241         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
242         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
243         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
244         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
245
246
247         # get the INPUT filters (these only pertain to Clusters)
248         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
249         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
250         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
251         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
252         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
253         
254         # set unbuffered and no echo
255         $self->send_now('B',"0");
256         $self->send_now('E',"0");
257         
258         # ping neighbour node stuff
259         my $ping = $user->pingint;
260         $ping = 5*60 unless defined $ping;
261         $self->{pingint} = $ping;
262         $self->{nopings} = $user->nopings || 2;
263         $self->{pingtime} = [ ];
264         $self->{pingave} = 999;
265         $self->{lastping} = $main::systime;
266
267         # send initialisation string
268         unless ($self->{outbound}) {
269                 $self->send(pc18());
270         }
271         
272         $self->state('init');
273         $self->{pc50_t} = $main::systime;
274
275         # send info to all logged in thingies
276         $self->tell_login('loginn');
277
278         # run a script send the output to the debug file
279         my $script = new Script(lc $call) || new Script('node_default');
280         $script->run($self) if $script;
281 }
282
283 #
284 # This is the normal pcxx despatcher
285 #
286 sub normal
287 {
288         my ($self, $line) = @_;
289         my @field = split /\^/, $line;
290         return unless @field;
291         
292         pop @field if $field[-1] eq '~';
293         
294 #       print join(',', @field), "\n";
295                                                 
296         
297         # process PC frames, this will fail unless the frame starts PCnn
298         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
299         return unless $pcno;
300         return if $pcno < 10 || $pcno > 99;
301
302         # check for and dump bad protocol messages
303         my $n = check($pcno, @field);
304         if ($n) {
305                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
306                 return;
307         }
308
309         # local processing 1
310         my $pcr;
311         eval {
312                 $pcr = Local::pcprot($self, $pcno, @field);
313         };
314 #       dbg("Local::pcprot error $@") if isdbg('local') if $@;
315         return if $pcr;
316         
317  SWITCH: {
318                 if ($pcno == 10) {              # incoming talk
319
320                         # rsfp check
321                         return if $rspfcheck and !$self->rspfcheck(0, $field[6], $field[1]);
322                         
323                         # will we allow it at all?
324                         if ($censorpc) {
325                                 my @bad;
326                                 if (@bad = BadWords::check($field[3])) {
327                                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
328                                         return;
329                                 }
330                         }
331
332                         # is it for me or one of mine?
333                         my ($from, $to, $via, $call, $dxchan);
334                         $from = $field[1];
335                         if ($field[5] gt ' ') {
336                                 $via = $field[2];
337                                 $to = $field[5];
338                         } else {
339                                 $to = $field[2];
340                         }
341
342                         # if we are converting announces to talk is it a dup?
343                         if ($ann_to_talk) {
344                                 if (AnnTalk::is_talk_candidate($from, $field[3]) && AnnTalk::dup($from, $to, $field[3])) {
345                                         dbg("DXPROT: Dupe talk from announce, dropped") if isdbg('chanerr');
346                                         return;
347                                 }
348                         }
349
350                         # it is here and logged on
351                         $dxchan = DXChannel->get($main::myalias) if $to eq $main::mycall;
352                         $dxchan = DXChannel->get($to) unless $dxchan;
353                         if ($dxchan && $dxchan->is_user) {
354                                 $field[3] =~ s/\%5E/^/g;
355                                 $dxchan->talk($from, $to, $via, $field[3]);
356                                 return;
357                         }
358
359                         # is it elsewhere, visible on the cluster via the to address?
360                         # note: this discards the via unless the to address is on
361                         # the via address
362                         my ($ref, $vref);
363                         if ($ref = Route::get($to)) {
364                                 $vref = Route::Node::get($via) if $via;
365                                 $vref = undef unless $vref && grep $to eq $_, $vref->users;
366                                 $ref->dxchan->talk($from, $to, $vref ? $via : undef, $field[3], $field[6]);
367                                 return;
368                         }
369
370                         # not visible here, send a message of condolence
371                         $vref = undef;
372                         $ref = Route::get($from);
373                         $vref = $ref = Route::Node::get($field[6]) unless $ref; 
374                         if ($ref) {
375                                 $dxchan = $ref->dxchan;
376                                 $dxchan->talk($main::mycall, $from, $vref ? $vref->call : undef, $dxchan->msg('talknh', $to) );
377                         }
378                         return;
379                 }
380                 
381                 if ($pcno == 11 || $pcno == 26) { # dx spot
382
383                         # route 'foreign' pc26s 
384                         if ($pcno == 26) {
385                                 if ($field[7] ne $main::mycall) {
386                                         $self->route($field[7], $line);
387                                         return;
388                                 }
389                         }
390                         
391                         # rsfp check
392                         return if $rspfcheck and !$self->rspfcheck(1, $field[7], $field[6]);
393
394                         # if this is a 'nodx' node then ignore it
395                         if ($badnode->in($field[7])) {
396                                 dbg("PCPROT: Bad Node, dropped") if isdbg('chanerr');
397                                 return;
398                         }
399                         
400                         # if this is a 'bad spotter' user then ignore it
401                         if ($badspotter->in($field[6])) {
402                                 dbg("PCPROT: Bad Spotter, dropped") if isdbg('chanerr');
403                                 return;
404                         }
405                         
406                         # convert the date to a unix date
407                         my $d = cltounix($field[3], $field[4]);
408                         # bang out (and don't pass on) if date is invalid or the spot is too old (or too young)
409                         if (!$d || ($pcno == 11 && ($d < $main::systime - $pc11_max_age || $d > $main::systime + 900))) {
410                                 dbg("PCPROT: Spot ignored, invalid date or out of range ($field[3] $field[4])\n") if isdbg('chanerr');
411                                 return;
412                         }
413
414                         # is it 'baddx'
415                         if ($baddx->in($field[2])) {
416                                 dbg("PCPROT: Bad DX spot, ignored") if isdbg('chanerr');
417                                 return;
418                         }
419                         
420                         # do some de-duping
421                         $field[5] =~ s/^\s+//;      # take any leading blanks off
422                         $field[2] = unpad($field[2]);   # take off leading and trailing blanks from spotted callsign
423                         if ($field[2] =~ /BUST\w*$/) {
424                                 dbg("PCPROT: useless 'BUSTED' spot") if isdbg('chanerr');
425                                 return;
426                         }
427                         if (Spot::dup($field[1], $field[2], $d, $field[5])) {
428                                 dbg("PCPROT: Duplicate Spot ignored\n") if isdbg('chanerr');
429                                 return;
430                         }
431                         if ($censorpc) {
432                                 my @bad;
433                                 if (@bad = BadWords::check($field[5])) {
434                                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
435                                         return;
436                                 }
437                         }
438
439                         my @spot = Spot::prepare($field[1], $field[2], $d, $field[5], $field[6], $field[7]);
440                         # global spot filtering on INPUT
441                         if ($self->{inspotsfilter}) {
442                                 my ($filter, $hops) = $self->{inspotsfilter}->it(@spot);
443                                 unless ($filter) {
444                                         dbg("PCPROT: Rejected by input spot filter") if isdbg('chanerr');
445                                         return;
446                                 }
447                         }
448                         
449                         # add it 
450                         Spot::add(@spot);
451
452             #
453                         # @spot at this point contains:-
454             # freq, spotted call, time, text, spotter, spotted cc, spotters cc, orig node
455                         # then  spotted itu, spotted cq, spotters itu, spotters cq
456                         # you should be able to route on any of these
457             #
458                         
459                         # fix up qra locators of known users 
460                         my $user = DXUser->get_current($spot[4]);
461                         if ($user) {
462                                 my $qra = $user->qra;
463                                 unless ($qra && is_qra($qra)) {
464                                         my $lat = $user->lat;
465                                         my $long = $user->long;
466                                         if (defined $lat && defined $long) {
467                                                 $user->qra(DXBearing::lltoqra($lat, $long)); 
468                                                 $user->put;
469                                         }
470                                 }
471
472                                 # send a remote command to a distant cluster if it is visible and there is no
473                                 # qra locator and we havn't done it for a month.
474
475                                 unless ($user->qra) {
476                                         my $node;
477                                         my $to = $user->homenode;
478                                         my $last = $user->lastoper || 0;
479                                         if ($send_opernam && $to && $to ne $main::mycall && $main::systime > $last + $DXUser::lastoperinterval && ($node = Route::Node::get($to)) ) {
480                                                 my $cmd = "forward/opernam $spot[4]";
481                                                 # send the rcmd but we aren't interested in the replies...
482                                                 my $dxchan = $node->dxchan;
483                                                 if ($dxchan && $dxchan->is_clx) {
484                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
485                                                 } else {
486                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
487                                                 }
488                                                 if ($to ne $field[7]) {
489                                                         $to = $field[7];
490                                                         $node = Route::Node::get($to);
491                                                         if ($node) {
492                                                                 $dxchan = $node->dxchan;
493                                                                 if ($dxchan && $dxchan->is_clx) {
494                                                                         route(undef, $to, pc84($main::mycall, $to, $main::mycall, $cmd));
495                                                                 } else {
496                                                                         route(undef, $to, pc34($main::mycall, $to, $cmd));
497                                                                 }
498                                                         }
499                                                 }
500                                                 $user->lastoper($main::systime);
501                                                 $user->put;
502                                         }
503                                 }
504                         }
505                                 
506                         # local processing 
507                         my $r;
508                         eval {
509                                 $r = Local::spot($self, @spot);
510                         };
511 #                       dbg("Local::spot1 error $@") if isdbg('local') if $@;
512                         return if $r;
513
514                         # DON'T be silly and send on PC26s!
515                         return if $pcno == 26;
516
517                         # send out the filtered spots
518                         send_dx_spot($self, $line, @spot) if @spot;
519                         return;
520                 }
521                 
522                 if ($pcno == 12) {              # announces
523
524                         return if $rspfcheck and !$self->rspfcheck(1, $field[5], $field[1]);
525
526                         # announce duplicate checking
527                         $field[3] =~ s/^\s+//;  # remove leading blanks
528                         if (AnnTalk::dup($field[1], $field[2], $field[3])) {
529                                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
530                                 return;
531                         }
532
533                         if ($censorpc) {
534                                 my @bad;
535                                 if (@bad = BadWords::check($field[3])) {
536                                         dbg("PCPROT: Bad words: @bad, dropped") if isdbg('chanerr');
537                                         return;
538                                 }
539                         }
540
541                         if ($field[2] eq '*' || $field[2] eq $main::mycall) {
542
543
544                                 # here's a bit of fun, convert incoming ann with a callsign in the first word
545                                 # or one saying 'to <call>' to a talk if we can route to the recipient
546                                 if ($ann_to_talk) {
547                                         my $call = AnnTalk::is_talk_candidate($field[1], $field[3]);
548                                         if ($call) {
549                                                 my $ref = Route::get($call);
550                                                 if ($ref) {
551                                                         my $dxchan = $ref->dxchan;
552                                                         $dxchan->talk($field[1], $call, undef, $field[3], $field[5]) if $dxchan != $self;
553                                                         return;
554                                                 }
555                                         }
556                                 }
557         
558                                 # send it
559                                 $self->send_announce($line, @field[1..6]);
560                         } else {
561                                 $self->route($field[2], $line);
562                         }
563                         return;
564                 }
565                 
566                 if ($pcno == 13) {
567                         last SWITCH;
568                 }
569                 if ($pcno == 14) {
570                         last SWITCH;
571                 }
572                 if ($pcno == 15) {
573                         last SWITCH;
574                 }
575                 
576                 if ($pcno == 16) {              # add a user
577
578                         # general checks
579                         my $dxchan;
580                         my $ncall = $field[1];
581                         my $newline = "PC16^";
582                         
583                         if ($ncall eq $main::mycall) {
584                                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
585                                 return;
586                         }
587                         $dxchan = DXChannel->get($ncall);
588                         if ($dxchan && $dxchan ne $self) {
589                                 dbg("PCPROT: PC16 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
590                                 return;
591                         }
592                         my $parent = Route::Node::get($ncall); 
593                         unless ($parent) {
594                                 dbg("PCPROT: Node $ncall not in config") if isdbg('chanerr');
595                                 return;
596                         }
597                         
598                         # input filter if required
599                         return unless $self->in_filter_route($parent);
600                         
601                         my $i;
602                         my @rout;
603                         for ($i = 2; $i < $#field; $i++) {
604                                 my ($call, $conf, $here) = $field[$i] =~ /^(\S+) (\S) (\d)/o;
605                                 next unless $call && $conf && defined $here && is_callsign($call);
606                                 next if $call eq $main::mycall;
607
608                                 eph_del_regex("^PC17\^$call\^$ncall");
609                                 
610                                 $conf = $conf eq '*';
611
612                                 # reject this if we think it is a node already
613                                 my $r = Route::Node::get($call);
614                                 my $u = DXUser->get_current($call) unless $r;
615                                 if ($r || ($u && $u->is_node)) {
616                                         dbg("PCPROT: $call is a node") if isdbg('chanerr');
617                                         next;
618                                 }
619                                 
620                                 $r = Route::User::get($call);
621                                 my $flags = Route::here($here)|Route::conf($conf);
622                                 
623                                 if ($r) {
624                                         if ($r->flags != $flags) {
625                                                 $r->flags($flags);
626                                                 push @rout, $r;
627                                         }
628                                         $r->addparent($parent);
629                                 } else {
630                                         push @rout, $parent->add_user($call, $flags);
631                                 }
632                                 
633                                 # add this station to the user database, if required
634                                 $call =~ s/-\d+$//o;        # remove ssid for users
635                                 my $user = DXUser->get_current($call);
636                                 $user = DXUser->new($call) if !$user;
637                                 $user->homenode($parent->call) if !$user->homenode;
638                                 $user->node($parent->call);
639                                 $user->lastin($main::systime) unless DXChannel->get($call);
640                                 $user->put;
641                         }
642
643                         if (eph_dup($line)) {
644                                 dbg("PCPROT: dup PC16 detected") if isdbg('chanerr');
645                                 return;
646                         }
647                         
648                         # queue up any messages (look for privates only)
649                         DXMsg::queue_msg(1) if $self->state eq 'normal';     
650
651                         $self->route_pc16($parent, @rout) if @rout;
652                         return;
653                 }
654                 
655                 if ($pcno == 17) {              # remove a user
656                         my $dxchan;
657                         my $ncall = $field[2];
658                         my $ucall = $field[1];
659
660                         eph_del_regex("^PC16.*$ncall.*$ucall");
661                         
662                         if ($ncall eq $main::mycall) {
663                                 dbg("PCPROT: trying to alter config on this node from outside!") if isdbg('chanerr');
664                                 return;
665                         }
666                         $dxchan = DXChannel->get($ncall);
667                         if ($dxchan && $dxchan ne $self) {
668                                 dbg("PCPROT: PC17 from $self->{call} trying to alter locally connected $ncall, ignored!") if isdbg('chanerr');
669                                 return;
670                         }
671
672                         my $uref = Route::User::get($ucall);
673                         unless ($uref) {
674                                 dbg("PCPROT: Route::User $ucall not in config") if isdbg('chanerr');
675                                 return;
676                         }
677                         my $parent = Route::Node::get($ncall);
678                         unless ($parent) {
679                                 dbg("PCPROT: Route::Node $ncall not in config") if isdbg('chanerr');
680                                 return;
681                         }                       
682
683                         # input filter if required
684                         return unless $self->in_filter_route($parent);
685                         
686                         my @rout = $parent->del_user($uref);
687
688                         if (eph_dup($line)) {
689                                 dbg("PCPROT: dup PC17 detected") if isdbg('chanerr');
690                                 return;
691                         }
692
693                         $self->route_pc17($parent, @rout) if @rout;
694                         return;
695                 }
696                 
697                 if ($pcno == 18) {              # link request
698                         $self->state('init');   
699
700                         # first clear out any nodes on this dxchannel
701                         my $parent = Route::Node::get($self->{call});
702                         my @rout = $parent->del_nodes;
703                         $self->route_pc21(@rout, $parent) if @rout;
704                         $self->send_local_config();
705                         $self->send(pc20());
706                         return;             # we don't pass these on
707                 }
708                 
709                 if ($pcno == 19) {              # incoming cluster list
710                         my $i;
711                         my $newline = "PC19^";
712
713                         # new routing list
714                         my @rout;
715                         my $parent = Route::Node::get($self->{call});
716                         unless ($parent) {
717                                 dbg("DXPROT: my parent $self->{call} has disappeared");
718                                 $self->disconnect;
719                                 return;
720                         }
721
722                         # parse the PC19
723                         for ($i = 1; $i < $#field-1; $i += 4) {
724                                 my $here = $field[$i];
725                                 my $call = uc $field[$i+1];
726                                 my $conf = $field[$i+2];
727                                 my $ver = $field[$i+3];
728                                 next unless defined $here && defined $conf && is_callsign($call);
729
730                                 eph_del_regex("^PC(?:21\^$call|17\^[^\^]+\^$call)");
731                                 
732                                 # check for sane parameters
733                                 $ver = 5000 if $ver eq '0000';
734                                 next if $ver < 5000; # only works with version 5 software
735                                 next if length $call < 3; # min 3 letter callsigns
736                                 next if $call eq $main::mycall;
737
738                                 # update it if required
739                                 my $r = Route::Node::get($call);
740                                 my $flags = Route::here($here)|Route::conf($conf);
741                                 if ($r) {
742                                         my $ar;
743                                         if ($call ne $parent->call) {
744                                                 if ($self->in_filter_route($r)) {
745                                                         $ar = $parent->add($call, $ver, $flags);
746                                                         push @rout, $ar if $ar;
747                                                 } else {
748                                                         next;
749                                                 }
750                                         }
751                                         if ($r->version ne $ver || $r->flags != $flags) {
752                                                 $r->version($ver);
753                                                 $r->flags($flags);
754                                                 push @rout, $r unless $ar;
755                                         }
756                                 } else {
757                                         if ($call eq $self->{call}) {
758                                                 dbg("DXPROT: my channel route for $call has disappeared");
759                                                 next;
760                                         };
761                                         
762                                         my $new = Route->new($call);          # throw away
763                                     if ($self->in_filter_route($new)) {
764                                                 my $r = $parent->add($call, $ver, $flags);
765                                                 push @rout, $r;
766                                         } else {
767                                                 next;
768                                         }
769                                 }
770
771                                 # unbusy and stop and outgoing mail (ie if somehow we receive another PC19 without a disconnect)
772                                 my $mref = DXMsg::get_busy($call);
773                                 $mref->stop_msg($call) if $mref;
774                                 
775                                 # add this station to the user database, if required (don't remove SSID from nodes)
776                                 my $user = DXUser->get_current($call);
777                                 if (!$user) {
778                                         $user = DXUser->new($call);
779                                         $user->sort('A');
780                                         $user->priv(1);                   # I have relented and defaulted nodes
781                                         $user->lockout(1);
782                                         $user->homenode($call);
783                                         $user->node($call);
784                                 }
785                                 $user->lastin($main::systime) unless DXChannel->get($call);
786                                 $user->put;
787                         }
788
789                         if (eph_dup($line)) {
790                                 dbg("PCPROT: dup PC19 detected") if isdbg('chanerr');
791                                 return;
792                         }
793
794                         $self->route_pc19(@rout) if @rout;
795                         return;
796                 }
797                 
798                 if ($pcno == 20) {              # send local configuration
799                         $self->send_local_config();
800                         $self->send(pc22());
801                         $self->state('normal');
802                         $self->{lastping} = 0;
803                         return;
804                 }
805                 
806                 if ($pcno == 21) {              # delete a cluster from the list
807                         my $call = uc $field[1];
808
809                         eph_del_regex("^PC1[79].*$call");
810                         
811                         my @rout;
812                         my $parent = Route::Node::get($self->{call});
813                         unless ($parent) {
814                                 dbg("DXPROT: my parent $self->{call} has disappeared");
815                                 $self->disconnect;
816                                 return;
817                         }
818                         my $node = Route::Node::get($call);
819                         if ($call ne $main::mycall) { # don't allow malicious buggers to disconnect me!
820                                 if ($call eq $self->{call}) {
821                                         dbg("PCPROT: Trying to disconnect myself with PC21") if isdbg('chanerr');
822                                         return;
823                                 }
824
825                                 if ($node) {
826                                         # input filter it
827                                         return unless $self->in_filter_route($node);
828
829                                         # routing objects
830                                         push @rout, $node->del($parent);
831                                 }
832                         } else {
833                                 dbg("PCPROT: I WILL _NOT_ be disconnected!") if isdbg('chanerr');
834                                 return;
835                         }
836
837                         if (eph_dup($line)) {
838                                 dbg("PCPROT: dup PC21 detected") if isdbg('chanerr');
839                                 return;
840                         }
841
842                         $self->route_pc21(@rout) if @rout;
843                         return;
844                 }
845                 
846                 if ($pcno == 22) {
847                         $self->state('normal');
848                         $self->{lastping} = 0;
849                         return;
850                 }
851                                 
852                 if ($pcno == 23 || $pcno == 27) { # WWV info
853                         
854                         # route 'foreign' pc27s 
855                         if ($pcno == 27) {
856                                 if ($field[8] ne $main::mycall) {
857                                         $self->route($field[8], $line);
858                                         return;
859                                 }
860                         }
861
862                         return if $rspfcheck and !$self->rspfcheck(1, $field[8], $field[7]);
863
864                         # do some de-duping
865                         my $d = cltounix($field[1], sprintf("%02d18Z", $field[2]));
866                         my $sfi = unpad($field[3]);
867                         my $k = unpad($field[4]);
868                         my $i = unpad($field[5]);
869                         my ($r) = $field[6] =~ /R=(\d+)/;
870                         $r = 0 unless $r;
871                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
872                                 dbg("PCPROT: WWV Date ($field[1] $field[2]) out of range") if isdbg('chanerr');
873                                 return;
874                         }
875                         if (Geomag::dup($d,$sfi,$k,$i,$field[6])) {
876                                 dbg("PCPROT: Dup WWV Spot ignored\n") if isdbg('chanerr');
877                                 return;
878                         }
879                         $field[7] =~ s/-\d+$//o;            # remove spotter's ssid
880                 
881                         my $wwv = Geomag::update($d, $field[2], $sfi, $k, $i, @field[6..8], $r);
882
883                         my $rep;
884                         eval {
885                                 $rep = Local::wwv($self, $field[1], $field[2], $sfi, $k, $i, @field[6..8], $r);
886                         };
887 #                       dbg("Local::wwv2 error $@") if isdbg('local') if $@;
888                         return if $rep;
889
890                         # DON'T be silly and send on PC27s!
891                         return if $pcno == 27;
892
893                         # broadcast to the eager world
894                         send_wwv_spot($self, $line, $d, $field[2], $sfi, $k, $i, @field[6..8]);
895                         return;
896                 }
897                 
898                 if ($pcno == 24) {              # set here status
899                         my $call = uc $field[1];
900                         my ($nref, $uref);
901                         $nref = Route::Node::get($call);
902                         $uref = Route::User::get($call);
903                         return unless $nref || $uref;   # if we don't know where they are, it's pointless sending it on
904                         
905                         unless (eph_dup($line)) {
906                                 $nref->here($field[2]) if $nref;
907                                 $uref->here($field[2]) if $uref;
908                                 my $ref = $nref || $uref;
909                                 return unless $self->in_filter_route($ref);
910                                 $self->route_pc24($ref, $field[3]);
911                         }
912                         return;
913                 }
914                 
915                 if ($pcno == 25) {      # merge request
916                         if ($field[1] ne $main::mycall) {
917                                 $self->route($field[1], $line);
918                                 return;
919                         }
920                         if ($field[2] eq $main::mycall) {
921                                 dbg("PCPROT: Trying to merge to myself, ignored") if isdbg('chanerr');
922                                 return;
923                         }
924
925                         Log('DXProt', "Merge request for $field[3] spots and $field[4] WWV from $field[2]");
926                         
927                         # spots
928                         if ($field[3] > 0) {
929                                 my @in = reverse Spot::search(1, undef, undef, 0, $field[3]);
930                                 my $in;
931                                 foreach $in (@in) {
932                                         $self->send(pc26(@{$in}[0..4], $field[2]));
933                                 }
934                         }
935
936                         # wwv
937                         if ($field[4] > 0) {
938                                 my @in = reverse Geomag::search(0, $field[4], time, 1);
939                                 my $in;
940                                 foreach $in (@in) {
941                                         $self->send(pc27(@{$in}[0..5], $field[2]));
942                                 }
943                         }
944                         return;
945                 }
946
947                 if (($pcno >= 28 && $pcno <= 33) || $pcno == 40 || $pcno == 42 || $pcno == 49) { # mail/file handling
948                         if ($pcno == 49 || $field[1] eq $main::mycall) {
949                                 DXMsg::process($self, $line);
950                         } else {
951                                 $self->route($field[1], $line) unless $self->is_clx;
952                         }
953                         return;
954                 }
955                 
956                 if ($pcno == 34 || $pcno == 36) { # remote commands (incoming)
957                         $self->process_rcmd($field[1], $field[2], $field[2], $field[3]);
958                         return;
959                 }
960                 
961                 if ($pcno == 35) {              # remote command replies
962                         $self->process_rcmd_reply($field[1], $field[2], $field[1], $field[3]);
963                         return;
964                 }
965                 
966                 # for pc 37 see 44 onwards
967
968                 if ($pcno == 38) {              # node connected list from neighbour
969                         return;
970                 }
971                 
972                 if ($pcno == 39) {              # incoming disconnect
973                         if ($field[1] eq $self->{call}) {
974                                 $self->disconnect(1);
975                                 eph_del_regex("^PC(?:1[679]|21).*$field[1]");
976                         } else {
977                                 dbg("PCPROT: came in on wrong channel") if isdbg('chanerr');
978                         }
979                         return;
980                 }
981                 
982                 if ($pcno == 41) {              # user info
983                         my $call = $field[1];
984
985                         # input filter if required
986 #                       my $ref = Route::get($call) || Route->new($call);
987 #                       return unless $self->in_filter_route($ref);
988
989                         if ($field[3] eq $field[2] || $field[3] =~ /^\s*$/) {
990                                 dbg('PCPROT: invalid value') if isdbg('chanerr');
991                                 return;
992                         }
993
994                         # add this station to the user database, if required
995                         my $user = DXUser->get_current($call);
996                         $user = DXUser->new($call) if !$user;
997                         
998                         if ($field[2] == 1) {
999                                 $user->name($field[3]);
1000                         } elsif ($field[2] == 2) {
1001                                 $user->qth($field[3]);
1002                         } elsif ($field[2] == 3) {
1003                                 if (is_latlong($field[3])) {
1004                                         my ($lat, $long) = DXBearing::stoll($field[3]);
1005                                         $user->lat($lat);
1006                                         $user->long($long);
1007                                         $user->qra(DXBearing::lltoqra($lat, $long));
1008                                 } else {
1009                                         dbg('PCPROT: not a valid lat/long') if isdbg('chanerr');
1010                                         return;
1011                                 }
1012                         } elsif ($field[2] == 4) {
1013                                 $user->homenode($field[3]);
1014                         } elsif ($field[2] == 5) {
1015                                 if (is_qra($field[3])) {
1016                                         my ($lat, $long) = DXBearing::qratoll($field[3]);
1017                                         $user->lat($lat);
1018                                         $user->long($long);
1019                                         $user->qra($field[3]);
1020                                 } else {
1021                                         dbg('PCPROT: not a valid QRA locator') if isdbg('chanerr');
1022                                         return;
1023                                 }
1024                         }
1025                         $user->lastoper($main::systime);   # to cut down on excessive for/opers being generated
1026                         $user->put;
1027
1028 #  perhaps this IS what we want after all
1029 #                       $self->route_pc41($ref, $call, $field[2], $field[3], $field[4]);
1030 #                       return;
1031                 }
1032
1033                 if ($pcno == 43) {
1034                         last SWITCH;
1035                 }
1036
1037                 if ($pcno == 37 || $pcno == 44 || $pcno == 45 || $pcno == 46 || $pcno == 47 || $pcno == 48) {
1038                         DXDb::process($self, $line);
1039                         return;
1040                 }
1041                 
1042                 if ($pcno == 50) {              # keep alive/user list
1043                         my $call = $field[1];
1044                         my $node = Route::Node::get($call);
1045                         if ($node) {
1046                                 return unless $node->call eq $self->{call};
1047                                 $node->usercount($field[2]);
1048
1049                                 # input filter if required
1050                                 return unless $self->in_filter_route($node);
1051
1052                                 $self->route_pc50($node, $field[2], $field[3]) unless eph_dup($line);
1053                         }
1054                         return;
1055                 }
1056                 
1057                 if ($pcno == 51) {              # incoming ping requests/answers
1058                         my $to = $field[1];
1059                         my $from = $field[2];
1060                         my $flag = $field[3];
1061
1062                         
1063                         # is it for us?
1064                         if ($to eq $main::mycall) {
1065                                 if ($flag == 1) {
1066                                         $self->send(pc51($from, $to, '0'));
1067                                 } else {
1068                                         # it's a reply, look in the ping list for this one
1069                                         my $ref = $pings{$from};
1070                                         if ($ref) {
1071                                                 my $tochan =  DXChannel->get($from);
1072                                                 while (@$ref) {
1073                                                         my $r = shift @$ref;
1074                                                         my $dxchan = DXChannel->get($r->{call});
1075                                                         next unless $dxchan;
1076                                                         my $t = tv_interval($r->{t}, [ gettimeofday ]);
1077                                                         if ($dxchan->is_user) {
1078                                                                 my $s = sprintf "%.2f", $t; 
1079                                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
1080                                                                 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
1081                                                         } elsif ($dxchan->is_node) {
1082                                                                 if ($tochan) {
1083                                                                         my $nopings = $tochan->user->nopings || 2;
1084                                                                         push @{$tochan->{pingtime}}, $t;
1085                                                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
1086
1087                                                                         # cope with a missed ping, this means you must set the pingint large enough
1088                                                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
1089                                                                                 $t -= $tochan->{pingint};
1090                                                                         }
1091
1092                                                                         # calc smoothed RTT a la TCP
1093                                                                         if (@{$tochan->{pingtime}} == 1) {
1094                                                                                 $tochan->{pingave} = $t;
1095                                                                         } else {
1096                                                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
1097                                                                         }
1098 #                                                                       my $st;
1099 #                                                                       for (@{$tochan->{pingtime}}) {
1100 #                                                                               $st += $_;
1101 #                                                                       }
1102 #                                                                       $tochan->{pingave} = $st / @{$tochan->{pingtime}};
1103                                                                         $tochan->{nopings} = $nopings; # pump up the timer
1104                                                                 }
1105                                                         } 
1106                                                 }
1107                                         }
1108                                 }
1109                         } else {
1110                                 if (eph_dup($line)) {
1111                                         dbg("PCPROT: dup PC51 detected") if isdbg('chanerr');
1112                                         return;
1113                                 }
1114                                 # route down an appropriate thingy
1115                                 $self->route($to, $line);
1116                         }
1117                         return;
1118                 }
1119
1120                 if ($pcno == 75) {              # dunno but route it
1121                         my $call = $field[1];
1122                         if ($call ne $main::mycall) {
1123                                 $self->route($call, $line);
1124                         }
1125                         return;
1126                 }
1127
1128                 if ($pcno == 73) {  # WCY broadcasts
1129                         my $call = $field[1];
1130                         
1131                         # do some de-duping
1132                         my $d = cltounix($call, sprintf("%02d18Z", $field[2]));
1133                         if (($pcno == 23 && $d < $main::systime - $pc23_max_age) || $d > $main::systime + 1500 || $field[2] < 0 || $field[2] > 23) {
1134                                 dbg("PCPROT: WCY Date ($call $field[2]) out of range") if isdbg('chanerr');
1135                                 return;
1136                         }
1137                         @field = map { unpad($_) } @field;
1138                         if (WCY::dup($d,@field[3..7])) {
1139                                 dbg("PCPROT: Dup WCY Spot ignored\n") if isdbg('chanerr');
1140                                 return;
1141                         }
1142                 
1143                         my $wcy = WCY::update($d, @field[2..12]);
1144
1145                         my $rep;
1146                         eval {
1147                                 $rep = Local::wwv($self, @field[1..12]);
1148                         };
1149                         # dbg("Local::wcy error $@") if isdbg('local') if $@;
1150                         return if $rep;
1151
1152                         # broadcast to the eager world
1153                         send_wcy_spot($self, $line, $d, @field[2..12]);
1154                         return;
1155                 }
1156
1157                 if ($pcno == 84) { # remote commands (incoming)
1158                         $self->process_rcmd($field[1], $field[2], $field[3], $field[4]);
1159                         return;
1160                 }
1161
1162                 if ($pcno == 85) {              # remote command replies
1163                         $self->process_rcmd_reply($field[1], $field[2], $field[3], $field[4]);
1164                         
1165                         return;
1166                 }
1167         }
1168          
1169         # if get here then rebroadcast the thing with its Hop count decremented (if
1170         # there is one). If it has a hop count and it decrements to zero then don't
1171         # rebroadcast it.
1172         #
1173         # NOTE - don't arrive here UNLESS YOU WANT this lump of protocol to be
1174         #        REBROADCAST!!!!
1175         #
1176
1177         if (eph_dup($line)) {
1178                 dbg("PCPROT: Ephemeral dup, dropped") if isdbg('chanerr');
1179         } else {
1180                 unless ($self->{isolate}) {
1181                         broadcast_ak1a($line, $self); # send it to everyone but me
1182                 }
1183         }
1184 }
1185
1186 #
1187 # This is called from inside the main cluster processing loop and is used
1188 # for despatching commands that are doing some long processing job
1189 #
1190 sub process
1191 {
1192         my $t = time;
1193         my @dxchan = DXChannel->get_all();
1194         my $dxchan;
1195         my $pc50s;
1196         
1197         # send out a pc50 on EVERY channel all at once
1198         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
1199                 $pc50s = pc50($me, scalar DXChannel::get_all_users);
1200                 eph_dup($pc50s);
1201                 $last_pc50 = $t;
1202         }
1203
1204         foreach $dxchan (@dxchan) {
1205                 next unless $dxchan->is_node();
1206                 next if $dxchan == $me;
1207
1208                 # send the pc50
1209                 $dxchan->send($pc50s) if $pc50s;
1210                 
1211                 # send a ping out on this channel
1212                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
1213                         if ($dxchan->{nopings} <= 0) {
1214                                 $dxchan->disconnect;
1215                         } else {
1216                                 addping($main::mycall, $dxchan->call);
1217                                 $dxchan->{nopings} -= 1;
1218                                 $dxchan->{lastping} = $t;
1219                         }
1220                 }
1221         }
1222
1223         # every ten seconds
1224         if ($t - $last10 >= 10) {       
1225                 # clean out ephemera 
1226
1227                 eph_clean();
1228
1229                 $last10 = $t;
1230         }
1231         
1232         if ($main::systime - 3600 > $last_hour) {
1233                 $last_hour = $main::systime;
1234         }
1235 }
1236
1237 #
1238 # finish up a pc context
1239 #
1240
1241 #
1242 # some active measures
1243 #
1244
1245 sub send_dx_spot
1246 {
1247         my $self = shift;
1248         my $line = shift;
1249         my @dxchan = DXChannel->get_all();
1250         my $dxchan;
1251         
1252         # send it if it isn't the except list and isn't isolated and still has a hop count
1253         # taking into account filtering and so on
1254         foreach $dxchan (@dxchan) {
1255                 next if $dxchan == $me;
1256                 next if $dxchan == $self && $self->is_node;
1257                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
1258         }
1259 }
1260
1261 sub dx_spot
1262 {
1263         my $self = shift;
1264         my $line = shift;
1265         my $isolate = shift;
1266         my ($filter, $hops);
1267
1268         if ($self->{spotsfilter}) {
1269                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
1270                 return unless $filter;
1271         }
1272         send_prot_line($self, $filter, $hops, $isolate, $line);
1273 }
1274
1275 sub send_prot_line
1276 {
1277         my ($self, $filter, $hops, $isolate, $line) = @_;
1278         my $routeit;
1279         
1280         if ($hops) {
1281                 $routeit = $line;
1282                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1283         } else {
1284                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1285                 return unless $routeit;
1286         }
1287         if ($filter) {
1288                 $self->send($routeit);
1289         } else {
1290                 $self->send($routeit) unless $self->{isolate} || $isolate;
1291         }
1292 }
1293
1294
1295 sub send_wwv_spot
1296 {
1297         my $self = shift;
1298         my $line = shift;
1299         my @dxchan = DXChannel->get_all();
1300         my $dxchan;
1301         my ($wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1302         my @dxcc = Prefix::extract($_[6]);
1303         if (@dxcc > 0) {
1304                 $wwv_dxcc = $dxcc[1]->dxcc;
1305                 $wwv_itu = $dxcc[1]->itu;
1306                 $wwv_cq = $dxcc[1]->cq;                                         
1307         }
1308         @dxcc = Prefix::extract($_[7]);
1309         if (@dxcc > 0) {
1310                 $org_dxcc = $dxcc[1]->dxcc;
1311                 $org_itu = $dxcc[1]->itu;
1312                 $org_cq = $dxcc[1]->cq;                                         
1313         }
1314         
1315         # send it if it isn't the except list and isn't isolated and still has a hop count
1316         # taking into account filtering and so on
1317         foreach $dxchan (@dxchan) {
1318                 next if $dxchan == $me;
1319                 next if $dxchan == $self && $self->is_node;
1320                 my $routeit;
1321                 my ($filter, $hops);
1322
1323                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, $wwv_dxcc, $wwv_itu, $wwv_cq, $org_dxcc, $org_itu, $org_cq);
1324         }
1325         
1326 }
1327
1328 sub wwv
1329 {
1330         my $self = shift;
1331         my $line = shift;
1332         my $isolate = shift;
1333         my ($filter, $hops);
1334         
1335         if ($self->{wwvfilter}) {
1336                 ($filter, $hops) = $self->{wwvfilter}->it(@_);
1337                 return unless $filter;
1338         }
1339         send_prot_line($self, $filter, $hops, $isolate, $line)
1340 }
1341
1342 sub send_wcy_spot
1343 {
1344         my $self = shift;
1345         my $line = shift;
1346         my @dxchan = DXChannel->get_all();
1347         my $dxchan;
1348         my ($wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1349         my @dxcc = Prefix::extract($_[10]);
1350         if (@dxcc > 0) {
1351                 $wcy_dxcc = $dxcc[1]->dxcc;
1352                 $wcy_itu = $dxcc[1]->itu;
1353                 $wcy_cq = $dxcc[1]->cq;                                         
1354         }
1355         @dxcc = Prefix::extract($_[11]);
1356         if (@dxcc > 0) {
1357                 $org_dxcc = $dxcc[1]->dxcc;
1358                 $org_itu = $dxcc[1]->itu;
1359                 $org_cq = $dxcc[1]->cq;                                         
1360         }
1361         
1362         # send it if it isn't the except list and isn't isolated and still has a hop count
1363         # taking into account filtering and so on
1364         foreach $dxchan (@dxchan) {
1365                 next if $dxchan == $me;
1366                 next if $dxchan == $self;
1367
1368                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, $wcy_dxcc, $wcy_itu, $wcy_cq, $org_dxcc, $org_itu, $org_cq);
1369         }
1370 }
1371
1372 sub wcy
1373 {
1374         my $self = shift;
1375         my $line = shift;
1376         my $isolate = shift;
1377         my ($filter, $hops);
1378
1379         if ($self->{wcyfilter}) {
1380                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
1381                 return unless $filter;
1382         }
1383         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
1384 }
1385
1386 # send an announce
1387 sub send_announce
1388 {
1389         my $self = shift;
1390         my $line = shift;
1391         my @dxchan = DXChannel->get_all();
1392         my $dxchan;
1393         my $target;
1394         my $to = 'To ';
1395         my $text = unpad($_[2]);
1396                                 
1397         if ($_[3] eq '*') {     # sysops
1398                 $target = "SYSOP";
1399         } elsif ($_[3] gt ' ') { # speciality list handling
1400                 my ($name) = split /\./, $_[3]; 
1401                 $target = "$name"; # put the rest in later (if bothered) 
1402         } 
1403         
1404         if ($_[5] eq '1') {
1405                 $target = "WX"; 
1406                 $to = '';
1407         }
1408         $target = "ALL" if !$target;
1409
1410
1411         # obtain country codes etc 
1412         my ($ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq) = (0..0);
1413         my @dxcc = Prefix::extract($_[0]);
1414         if (@dxcc > 0) {
1415                 $ann_dxcc = $dxcc[1]->dxcc;
1416                 $ann_itu = $dxcc[1]->itu;
1417                 $ann_cq = $dxcc[1]->cq;                                         
1418         }
1419         @dxcc = Prefix::extract($_[4]);
1420         if (@dxcc > 0) {
1421                 $org_dxcc = $dxcc[1]->dxcc;
1422                 $org_itu = $dxcc[1]->itu;
1423                 $org_cq = $dxcc[1]->cq;                                         
1424         }
1425
1426         if ($self->{inannfilter}) {
1427                 my ($filter, $hops) = 
1428                         $self->{inannfilter}->it(@_, $self->{call}, 
1429                                                                          $ann_dxcc, $ann_itu, $ann_cq,
1430                                                                          $org_dxcc, $org_itu, $org_cq);
1431                 unless ($filter) {
1432                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
1433                         return;
1434                 }
1435         }
1436
1437         Log('ann', $target, $_[0], $text);
1438
1439         # send it if it isn't the except list and isn't isolated and still has a hop count
1440         # taking into account filtering and so on
1441         foreach $dxchan (@dxchan) {
1442                 next if $dxchan == $me;
1443                 next if $dxchan == $self && $self->is_node;
1444                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call}, $ann_dxcc, $ann_itu, $ann_cq, $org_dxcc, $org_itu, $org_cq);
1445         }
1446 }
1447
1448 sub announce
1449 {
1450         my $self = shift;
1451         my $line = shift;
1452         my $isolate = shift;
1453         my $to = shift;
1454         my $target = shift;
1455         my $text = shift;
1456         my ($filter, $hops);
1457
1458         if ($self->{annfilter}) {
1459                 ($filter, $hops) = $self->{annfilter}->it(@_);
1460                 return unless $filter;
1461         }
1462         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
1463 }
1464
1465
1466 sub send_local_config
1467 {
1468         my $self = shift;
1469         my $n;
1470         my @nodes;
1471         my @localnodes;
1472         my @remotenodes;
1473
1474         dbg('DXProt::send_local_config') if isdbg('trace');
1475         
1476         # send our nodes
1477         if ($self->{isolate}) {
1478                 @localnodes = ( $main::routeroot );
1479         } else {
1480                 # create a list of all the nodes that are not connected to this connection
1481                 # and are not themselves isolated, this to make sure that isolated nodes
1482         # don't appear outside of this node
1483                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
1484                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
1485                 my @intcalls = map { $_->nodes } @localnodes if @localnodes;
1486                 my $ref = Route::Node::get($self->{call});
1487                 my @rnodes = $ref->nodes;
1488                 for my $n (@intcalls) {
1489                         push @remotenodes, Route::Node::get($n) unless grep $n eq $_, @rnodes;
1490                 }
1491                 unshift @localnodes, $main::routeroot;
1492         }
1493         
1494         send_route($self, \&pc19, scalar(@localnodes)+scalar(@remotenodes), @localnodes, @remotenodes);
1495         
1496         # get all the users connected on the above nodes and send them out
1497         foreach $n (@localnodes, @remotenodes) {
1498                 if ($n) {
1499                         send_route($self, \&pc16, 1, $n, map {my $r = Route::User::get($_); $r ? ($r) : ()} $n->users);
1500                 } else {
1501                         dbg("sent a null value") if isdbg('chanerr');
1502                 }
1503         }
1504 }
1505
1506 #
1507 # route a message down an appropriate interface for a callsign
1508 #
1509 # is called route(to, pcline);
1510 #
1511 sub route
1512 {
1513         my ($self, $call, $line) = @_;
1514
1515         if (ref $self && $call eq $self->{call}) {
1516                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1517                 return;
1518         }
1519
1520         # always send it down the local interface if available
1521         my $dxchan = DXChannel->get($call);
1522         unless ($dxchan) {
1523                 my $cl = Route::get($call);
1524                 $dxchan = $cl->dxchan if $cl;
1525                 if (ref $dxchan) {
1526                         if (ref $self && $dxchan eq $self) {
1527                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1528                                 return;
1529                         }
1530                 }
1531         }
1532         if ($dxchan) {
1533                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1534                 if ($routeit) {
1535                         $dxchan->send($routeit) unless $dxchan == $me;
1536                 }
1537         } else {
1538                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1539         }
1540 }
1541
1542 # broadcast a message to all clusters taking into account isolation
1543 # [except those mentioned after buffer]
1544 sub broadcast_ak1a
1545 {
1546         my $s = shift;                          # the line to be rebroadcast
1547         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1548         my @dxchan = DXChannel::get_all_nodes();
1549         my $dxchan;
1550         
1551         # send it if it isn't the except list and isn't isolated and still has a hop count
1552         foreach $dxchan (@dxchan) {
1553                 next if grep $dxchan == $_, @except;
1554                 next if $dxchan == $me;
1555                 
1556                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1557                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
1558         }
1559 }
1560
1561 # broadcast a message to all clusters ignoring isolation
1562 # [except those mentioned after buffer]
1563 sub broadcast_all_ak1a
1564 {
1565         my $s = shift;                          # the line to be rebroadcast
1566         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1567         my @dxchan = DXChannel::get_all_nodes();
1568         my $dxchan;
1569         
1570         # send it if it isn't the except list and isn't isolated and still has a hop count
1571         foreach $dxchan (@dxchan) {
1572                 next if grep $dxchan == $_, @except;
1573                 next if $dxchan == $me;
1574
1575                 my $routeit = adjust_hops($dxchan, $s);      # adjust its hop count by node name
1576                 $dxchan->send($routeit);
1577         }
1578 }
1579
1580 # broadcast to all users
1581 # storing the spot or whatever until it is in a state to receive it
1582 sub broadcast_users
1583 {
1584         my $s = shift;                          # the line to be rebroadcast
1585         my $sort = shift;           # the type of transmission
1586         my $fref = shift;           # a reference to an object to filter on
1587         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
1588         my @dxchan = DXChannel::get_all_users();
1589         my $dxchan;
1590         my @out;
1591         
1592         foreach $dxchan (@dxchan) {
1593                 next if grep $dxchan == $_, @except;
1594                 push @out, $dxchan;
1595         }
1596         broadcast_list($s, $sort, $fref, @out);
1597 }
1598
1599 # broadcast to a list of users
1600 sub broadcast_list
1601 {
1602         my $s = shift;
1603         my $sort = shift;
1604         my $fref = shift;
1605         my $dxchan;
1606         
1607         foreach $dxchan (@_) {
1608                 my $filter = 1;
1609                 next if $dxchan == $me;
1610                 
1611                 if ($sort eq 'dx') {
1612                     next unless $dxchan->{dx};
1613                         ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
1614                         next unless $filter;
1615                 }
1616                 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
1617                 next if $sort eq 'wwv' && !$dxchan->{wwv};
1618                 next if $sort eq 'wcy' && !$dxchan->{wcy};
1619                 next if $sort eq 'wx' && !$dxchan->{wx};
1620
1621                 $s =~ s/\a//og unless $dxchan->{beep};
1622
1623                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
1624                         $dxchan->send($s);      
1625                 } else {
1626                         $dxchan->delay($s);
1627                 }
1628         }
1629 }
1630
1631
1632 #
1633 # obtain the hops from the list for this callsign and pc no 
1634 #
1635
1636 sub get_hops
1637 {
1638         my $pcno = shift;
1639         my $hops = $DXProt::hopcount{$pcno};
1640         $hops = $DXProt::def_hopcount if !$hops;
1641         return "H$hops";       
1642 }
1643
1644
1645 # adjust the hop count on a per node basis using the user loadable 
1646 # hop table if available or else decrement an existing one
1647 #
1648
1649 sub adjust_hops
1650 {
1651         my $self = shift;
1652         my $s = shift;
1653         my $call = $self->{call};
1654         my $hops;
1655         
1656         if (($hops) = $s =~ /\^H(\d+)\^~?$/o) {
1657                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1658                 confess "$call called adjust_hops with '$s'" unless $pcno;
1659                 my $ref = $nodehops{$call} if %nodehops;
1660                 if ($ref) {
1661                         my $newhops = $ref->{$pcno};
1662                         return "" if defined $newhops && $newhops == 0;
1663                         $newhops = $ref->{default} unless $newhops;
1664                         return "" if defined $newhops && $newhops == 0;
1665                         $newhops = $hops if !$newhops;
1666                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1667                 } else {
1668                         # simply decrement it
1669                         $hops--;
1670                         return "" if !$hops;
1671                         $s =~ s/\^H(\d+)(\^~?)$/\^H$hops$2/ if $hops;
1672                 }
1673         }
1674         return $s;
1675 }
1676
1677
1678 # load hop tables
1679 #
1680 sub load_hops
1681 {
1682         my $self = shift;
1683         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1684         do "$main::data/hop_table.pl";
1685         return $@ if $@;
1686         return 0;
1687 }
1688
1689
1690 # add a ping request to the ping queues
1691 sub addping
1692 {
1693         my ($from, $to) = @_;
1694         my $ref = $pings{$to} || [];
1695         my $r = {};
1696         $r->{call} = $from;
1697         $r->{t} = [ gettimeofday ];
1698         route(undef, $to, pc51($to, $main::mycall, 1));
1699         push @$ref, $r;
1700         $pings{$to} = $ref;
1701 }
1702
1703 sub process_rcmd
1704 {
1705         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1706         if ($tonode eq $main::mycall) {
1707                 my $ref = DXUser->get_current($fromnode);
1708                 my $cref = Route::Node::get($fromnode);
1709                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1710                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1711                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1712                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1713                                 my $oldpriv = $self->{priv};
1714                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1715                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1716                                 $self->{priv} = $oldpriv;
1717                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1718                                 delete $self->{remotecmd};
1719                         } else {
1720                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1721                         }
1722                 } else {
1723                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1724                 }
1725         } else {
1726                 my $ref = DXUser->get_current($tonode);
1727                 if ($ref && $ref->is_clx) {
1728                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1729                 } else {
1730                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1731                 }
1732         }
1733 }
1734
1735 sub process_rcmd_reply
1736 {
1737         my ($self, $tonode, $fromnode, $user, $line) = @_;
1738         if ($tonode eq $main::mycall) {
1739                 my $s = $rcmds{$fromnode};
1740                 if ($s) {
1741                         my $dxchan = DXChannel->get($s->{call});
1742                         my $ref = $user eq $tonode ? $dxchan : (DXChannel->get($user) || $dxchan);
1743                         $ref->send($line) if $ref;
1744                         delete $rcmds{$fromnode} if !$dxchan;
1745                 } else {
1746                         # send unsolicited ones to the sysop
1747                         my $dxchan = DXChannel->get($main::myalias);
1748                         $dxchan->send($line) if $dxchan;
1749                 }
1750         } else {
1751                 my $ref = DXUser->get_current($tonode);
1752                 if ($ref && $ref->is_clx) {
1753                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1754                 } else {
1755                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1756                 }
1757         }
1758 }
1759
1760 sub send_rcmd_reply
1761 {
1762         my $self = shift;
1763         my $tonode = shift;
1764         my $fromnode = shift;
1765         my $user = shift;
1766         while (@_) {
1767                 my $line = shift;
1768                 $line =~ s/\s*$//;
1769                 Log('rcmd', 'out', $fromnode, $line);
1770                 if ($self->is_clx) {
1771                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1772                 } else {
1773                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1774                 }
1775         }
1776 }
1777
1778 # add a rcmd request to the rcmd queues
1779 sub addrcmd
1780 {
1781         my ($self, $to, $cmd) = @_;
1782
1783         my $r = {};
1784         $r->{call} = $self->{call};
1785         $r->{t} = $main::systime;
1786         $r->{cmd} = $cmd;
1787         $rcmds{$to} = $r;
1788         
1789         my $ref = Route::Node::get($to);
1790         my $dxchan = $ref->dxchan;
1791         if ($dxchan && $dxchan->is_clx) {
1792                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1793         } else {
1794                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1795         }
1796 }
1797
1798 sub disconnect
1799 {
1800         my $self = shift;
1801         my $pc39flag = shift;
1802         my $call = $self->call;
1803
1804         return if $self->{disconnecting}++;
1805         
1806         unless ($pc39flag && $pc39flag == 1) {
1807                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1808         }
1809
1810         # do routing stuff
1811         my $node = Route::Node::get($call);
1812         my @rout;
1813         if ($node) {
1814                 @rout = $node->del($main::routeroot);
1815         }
1816         
1817         # unbusy and stop and outgoing mail
1818         my $mref = DXMsg::get_busy($call);
1819         $mref->stop_msg($call) if $mref;
1820         
1821         # broadcast to all other nodes that all the nodes connected to via me are gone
1822         unless ($pc39flag && $pc39flag == 2) {
1823                 $self->route_pc21(@rout) if @rout;
1824         }
1825
1826         # remove outstanding pings
1827         delete $pings{$call};
1828         
1829         # I was the last node visited
1830     $self->user->node($main::mycall);
1831
1832         # send info to all logged in thingies
1833         $self->tell_login('logoutn');
1834
1835         Log('DXProt', $call . " Disconnected");
1836
1837         $self->SUPER::disconnect;
1838 }
1839
1840
1841
1842 # send a talk message to this thingy
1843 #
1844 sub talk
1845 {
1846         my ($self, $from, $to, $via, $line, $origin) = @_;
1847         
1848         $line =~ s/\^/\\5E/g;                   # remove any ^ characters
1849         $self->send(DXProt::pc10($from, $to, $via, $line, $origin));
1850         Log('talk', $self->call, $from, $via?$via:$main::mycall, $line) unless $origin && $origin ne $main::mycall;
1851 }
1852
1853 # send it if it isn't the except list and isn't isolated and still has a hop count
1854 # taking into account filtering and so on
1855 sub send_route
1856 {
1857         my $self = shift;
1858         my $generate = shift;
1859         my $no = shift;     # the no of things to filter on 
1860         my $routeit;
1861         my ($filter, $hops);
1862         my @rin;
1863         
1864         for (; @_ && $no; $no--) {
1865                 my $r = shift;
1866                 
1867                 if (!$self->{isolate} && $self->{routefilter}) {
1868                         $filter = undef;
1869                         if ($r) {
1870                                 ($filter, $hops) = $self->{routefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1871                                 if ($filter) {
1872                                         push @rin, $r;
1873                                 } else {
1874                                         dbg("DXPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('chanerr');
1875                                 }
1876                         } else {
1877                                 dbg("was sent a null value") if isdbg('chanerr');
1878                         }
1879                 } else {
1880                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1881                 }
1882         }
1883         if (@rin) {
1884                 foreach my $line (&$generate(@rin, @_)) {
1885                         if ($hops) {
1886                                 $routeit = $line;
1887                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1888                         } else {
1889                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1890                                 next unless $routeit;
1891                         }
1892                         $self->send($routeit);
1893                 }
1894         }
1895 }
1896
1897 sub broadcast_route
1898 {
1899         my $self = shift;
1900         my $generate = shift;
1901         my @dxchan = DXChannel::get_all_nodes();
1902         my $dxchan;
1903         my $line;
1904         
1905         unless ($self->{isolate}) {
1906                 foreach $dxchan (@dxchan) {
1907                         next if $dxchan == $self;
1908                         next if $dxchan == $me;
1909                         $dxchan->send_route($generate, @_);
1910                 }
1911         }
1912 }
1913
1914 sub route_pc16
1915 {
1916         my $self = shift;
1917         broadcast_route($self, \&pc16, 1, @_);
1918 }
1919
1920 sub route_pc17
1921 {
1922         my $self = shift;
1923         broadcast_route($self, \&pc17, 1, @_);
1924 }
1925
1926 sub route_pc19
1927 {
1928         my $self = shift;
1929         broadcast_route($self, \&pc19, scalar @_, @_);
1930 }
1931
1932 sub route_pc21
1933 {
1934         my $self = shift;
1935         broadcast_route($self, \&pc21, scalar @_, @_);
1936 }
1937
1938 sub route_pc24
1939 {
1940         my $self = shift;
1941         broadcast_route($self, \&pc24, 1, @_);
1942 }
1943
1944 sub route_pc41
1945 {
1946         my $self = shift;
1947         broadcast_route($self, \&pc41, 1, @_);
1948 }
1949
1950 sub route_pc50
1951 {
1952         my $self = shift;
1953         broadcast_route($self, \&pc50, 1, @_);
1954 }
1955
1956 sub in_filter_route
1957 {
1958         my $self = shift;
1959         my $r = shift;
1960         my ($filter, $hops) = (1, 1);
1961         
1962         if ($self->{inroutefilter}) {
1963                 ($filter, $hops) = $self->{inroutefilter}->it($self->{call}, $self->{dxcc}, $self->{itu}, $self->{cq}, $r->call, $r->dxcc, $r->itu, $r->cq);
1964                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1965         }
1966         return $filter;
1967 }
1968
1969 sub eph_dup
1970 {
1971         my $s = shift;
1972         my $r;
1973
1974         # chop the end off
1975         $s =~ s/\^H\d\d?\^?\~?$//;
1976         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1977         $eph{$s} = $main::systime;
1978         return $r;
1979 }
1980
1981 sub eph_del_regex
1982 {
1983         my $regex = shift;
1984         my ($key, $val);
1985         while (($key, $val) = each %eph) {
1986                 if ($key =~ m{$regex}) {
1987                         delete $eph{$key};
1988                 }
1989         }
1990 }
1991
1992 sub eph_clean
1993 {
1994         my ($key, $val);
1995         
1996         while (($key, $val) = each %eph) {
1997                 if ($main::systime - $val > 180) {
1998                         delete $eph{$key};
1999                 }
2000         }
2001 }
2002
2003 1;
2004 __END__