fix period updates (I think)
[spider.git] / perl / DXProt.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the protocal mode for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8 #
9
10 package DXProt;
11
12 @ISA = qw(DXChannel);
13
14 use DXUtil;
15 use DXChannel;
16 use DXUser;
17 use DXM;
18 use DXProtVars;
19 use DXCommandmode;
20 use DXLog;
21 use Spot;
22 use DXProtout;
23 use DXDebug;
24 use Filter;
25 use Local;
26 use DXDb;
27 use AnnTalk;
28 use Geomag;
29 use WCY;
30 use BadWords;
31 use DXHash;
32 use Route;
33 use Route::Node;
34 use Script;
35 use RouteDB;
36 use DXProtHandle;
37
38 use strict;
39
40 use vars qw($pc11_max_age $pc23_max_age $last_pc50 $eph_restime $eph_info_restime $eph_pc34_restime
41                         $last_hour $last10 %eph  %pings %rcmds $ann_to_talk
42                         $pingint $obscount %pc19list $chatdupeage $chatimportfn
43                         $investigation_int $pc19_version $myprot_version
44                         %nodehops $baddx $badspotter $badnode $censorpc $rspfcheck
45                         $allowzero $decode_dk0wcy $send_opernam @checklist
46                         $eph_pc15_restime $pc92_update_period $pc92_obs_timeout
47                         %pc92_find $pc92_find_timeout $pc92_short_update_period
48                    );
49
50 $pc11_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc11
51 $pc23_max_age = 1*3600;                 # the maximum age for an incoming 'real-time' pc23
52
53 $last_hour = time;                              # last time I did an hourly periodic update
54 %rcmds = ();                    # outstanding rcmd requests outbound
55 %nodehops = ();                 # node specific hop control
56 %pc19list = ();                                 # list of outstanding PC19s that haven't had PC16s on them
57
58 $censorpc = 1;                                  # Do a BadWords::check on text fields and reject things
59                                                                 # loads of 'bad things'
60 $baddx = new DXHash "baddx";
61 $badspotter = new DXHash "badspotter";
62 $badnode = new DXHash "badnode";
63 $last10 = $last_pc50 = time;
64 $ann_to_talk = 1;
65 $rspfcheck = 1;
66 $eph_restime = 180;
67 $eph_info_restime = 60*60;
68 $eph_pc15_restime = 6*60;
69 $eph_pc34_restime = 30;
70 $pingint = 5*60;
71 $obscount = 2;
72 $chatdupeage = 20 * 60 * 60;
73 $chatimportfn = "$main::root/chat_import";
74 $investigation_int = 12*60*60;  # time between checks to see if we can see this node
75 $pc19_version = 5466;                   # the visible version no for outgoing PC19s generated from pc59
76 $pc92_update_period = 60*60;    # the period between PC92 C updates
77 $pc92_short_update_period = 15*60; # shorten the update period after a connection
78 %pc92_find = ();                                # outstanding pc92 find operations
79 $pc92_find_timeout = 30;                # maximum time to wait for a reply
80 $pc92_obs_timeout = $pc92_update_period; # the time between obscount countdowns
81
82
83
84 @checklist =
85 (
86  [ qw(i c c m bp bc c) ],                       # pc10
87  [ qw(i f m d t m c c h) ],             # pc11
88  [ qw(i c bm m bm bm p h) ],            # pc12
89  [ qw(i c h) ],                                 #
90  [ qw(i c h) ],                                 #
91  [ qw(i c m h) ],                                       #
92  undef ,                                                # pc16 has to be validated manually
93  [ qw(i c c h) ],                                       # pc17
94  [ qw(i m n) ],                                 # pc18
95  undef ,                                                # pc19 has to be validated manually
96  undef ,                                                # pc20 no validation
97  [ qw(i c m h) ],                                       # pc21
98  undef ,                                                # pc22 no validation
99  [ qw(i d n n n n m c c h) ],           # pc23
100  [ qw(i c p h) ],                                       # pc24
101  [ qw(i c c n n) ],                             # pc25
102  [ qw(i f m d t m c c bc) ],            # pc26
103  [ qw(i d n n n n m c c bc) ],  # pc27
104  [ qw(i c c m c d t p m bp n p bp bc) ], # pc28
105  [ qw(i c c n m) ],                             # pc29
106  [ qw(i c c n) ],                                       # pc30
107  [ qw(i c c n) ],                                       # pc31
108  [ qw(i c c n) ],                                       # pc32
109  [ qw(i c c n) ],                                       # pc33
110  [ qw(i c c m) ],                                       # pc34
111  [ qw(i c c m) ],                                       # pc35
112  [ qw(i c c m) ],                                       # pc36
113  [ qw(i c c n m) ],                             # pc37
114  undef,                                                 # pc38 not interested
115  [ qw(i c m) ],                                 # pc39
116  [ qw(i c c m p n) ],                           # pc40
117  [ qw(i c n m h) ],                             # pc41
118  [ qw(i c c n) ],                                       # pc42
119  undef,                                                 # pc43 don't handle it
120  [ qw(i c c n m m c) ],                 # pc44
121  [ qw(i c c n m) ],                             # pc45
122  [ qw(i c c n) ],                                       # pc46
123  undef,                                                 # pc47
124  undef,                                                 # pc48
125  [ qw(i c m h) ],                                       # pc49
126  [ qw(i c n h) ],                                       # pc50
127  [ qw(i c c n) ],                                       # pc51
128  undef,
129  undef,
130  undef,
131  undef,
132  undef,
133  undef,
134  undef,
135  undef,
136  undef,                                                 # pc60
137  undef,
138  undef,
139  undef,
140  undef,
141  undef,
142  undef,
143  undef,
144  undef,
145  undef,
146  undef,                                                 # pc70
147  undef,
148  undef,
149  [ qw(i d n n n n n n m m m c c h) ],   # pc73
150  undef,
151  undef,
152  undef,
153  undef,
154  undef,
155  undef,
156  undef,                                                 # pc80
157  undef,
158  undef,
159  undef,
160  [ qw(i c c c m) ],                             # pc84
161  [ qw(i c c c m) ],                             # pc85
162  undef,
163  undef,
164  undef,
165  undef,
166  [ qw(i c n) ],                                 # pc90
167 );
168
169 # use the entry in the check list to check the field list presented
170 # return OK if line NOT in check list (for now)
171 sub check
172 {
173         my $n = shift;
174         $n -= 10;
175         return 0 if $n < 0 || $n > @checklist;
176         my $ref = $checklist[$n];
177         return 0 unless ref $ref;
178
179         my $i;
180         for ($i = 1; $i < @$ref; $i++) {
181                 my ($blank, $act) = $$ref[$i] =~ /^(b?)(\w)$/;
182                 return 0 unless $act;
183                 next if $blank && $_[$i] =~ /^[ \*]$/;
184                 if ($act eq 'c') {
185                         return $i unless is_callsign($_[$i]);
186                 } elsif ($act eq 'i') {
187                         ;                                       # do nothing
188                 } elsif ($act eq 'm') {
189                         return $i unless is_pctext($_[$i]);
190                 } elsif ($act eq 'p') {
191                         return $i unless is_pcflag($_[$i]);
192                 } elsif ($act eq 'f') {
193                         return $i unless is_freq($_[$i]);
194                 } elsif ($act eq 'n') {
195                         return $i unless $_[$i] =~ /^[\d ]+$/;
196                 } elsif ($act eq 'h') {
197                         return $i unless $_[$i] =~ /^H\d\d?$/;
198                 } elsif ($act eq 'd') {
199                         return $i unless $_[$i] =~ /^\s*\d+-\w\w\w-[12][90]\d\d$/;
200                 } elsif ($act eq 't') {
201                         return $i unless $_[$i] =~ /^[012]\d[012345]\dZ$/;
202                 }
203         }
204         return 0;
205 }
206
207 sub update_pc92_next
208 {
209         my $self = shift;
210         my $period = shift || $pc92_update_period;
211         $self->{next_pc92_update} = $main::systime + $period - int rand($period / 4);
212 }
213
214 sub init
215 {
216         do "$main::data/hop_table.pl" if -e "$main::data/hop_table.pl";
217         confess $@ if $@;
218
219         my $user = DXUser->get($main::mycall);
220         die "User $main::mycall not setup or disappeared RTFM" unless $user;
221
222         $myprot_version += $main::version*100;
223         $main::me = DXProt->new($main::mycall, 0, $user);
224         $main::me->{here} = 1;
225         $main::me->{state} = "indifferent";
226         $main::me->{sort} = 'S';    # S for spider
227         $main::me->{priv} = 9;
228         $main::me->{metric} = 0;
229         $main::me->{pingave} = 0;
230         $main::me->{registered} = 1;
231         $main::me->{version} = $main::version;
232         $main::me->{build} = "$main::subversion.$main::build";
233         $main::me->{do_pc9x} = 1;
234         $main::me->update_pc92_next($pc92_update_period);
235 }
236
237 #
238 # obtain a new connection this is derived from dxchannel
239 #
240
241 sub new
242 {
243         my $self = DXChannel::alloc(@_);
244
245         # add this node to the table, the values get filled in later
246         my $pkg = shift;
247         my $call = shift;
248
249         # if we have an entry already, then send a PC21 to all connect
250         # old style connections, because we are about to get the real deal
251         if (my $ref = Route::Node::get($call)) {
252                 dbg("ROUTE: $call is already in the routing table, deleting") if isdbg('route');
253                 my @rout = $ref->delete;
254                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
255         }
256         $main::routeroot->add($call, '5000', Route::here(1)) if $call ne $main::mycall;
257
258         return $self;
259 }
260
261 # this is how a pc connection starts (for an incoming connection)
262 # issue a PC38 followed by a PC18, then wait for a PC20 (remembering
263 # all the crap that comes between).
264 sub start
265 {
266         my ($self, $line, $sort) = @_;
267         my $call = $self->{call};
268         my $user = $self->{user};
269
270         # log it
271         my $host = $self->{conn}->{peerhost};
272         $host ||= "AGW Port #$self->{conn}->{agwport}" if exists $self->{conn}->{agwport};
273         $host ||= "unknown";
274
275         Log('DXProt', "$call connected from $host");
276
277         # remember type of connection
278         $self->{consort} = $line;
279         $self->{outbound} = $sort eq 'O';
280         my $priv = $user->priv;
281         $priv = $user->priv(1) unless $priv;
282         $self->{priv} = $priv;     # other clusters can always be 'normal' users
283         $self->{lang} = $user->lang || 'en';
284         $self->{isolate} = $user->{isolate};
285         $self->{consort} = $line;       # save the connection type
286         $self->{here} = 1;
287         $self->{width} = 80;
288
289         # sort out registration
290         $self->{registered} = 1;
291
292         # get the output filters
293         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) || Filter::read_in('spots', 'node_default', 0);
294         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0) || Filter::read_in('wwv', 'node_default', 0);
295         $self->{wcyfilter} = Filter::read_in('wcy', $call, 0) || Filter::read_in('wcy', 'node_default', 0);
296         $self->{annfilter} = Filter::read_in('ann', $call, 0) || Filter::read_in('ann', 'node_default', 0) ;
297         $self->{routefilter} = Filter::read_in('route', $call, 0) || Filter::read_in('route', 'node_default', 0) unless $self->{isolate} ;
298
299
300         # get the INPUT filters (these only pertain to Clusters)
301         $self->{inspotsfilter} = Filter::read_in('spots', $call, 1) || Filter::read_in('spots', 'node_default', 1);
302         $self->{inwwvfilter} = Filter::read_in('wwv', $call, 1) || Filter::read_in('wwv', 'node_default', 1);
303         $self->{inwcyfilter} = Filter::read_in('wcy', $call, 1) || Filter::read_in('wcy', 'node_default', 1);
304         $self->{inannfilter} = Filter::read_in('ann', $call, 1) || Filter::read_in('ann', 'node_default', 1);
305         $self->{inroutefilter} = Filter::read_in('route', $call, 1) || Filter::read_in('route', 'node_default', 1) unless $self->{isolate};
306         # if there is no route input filter then specify a default one.
307         # obviously this can be changed later by the sysop.
308         if (!$self->{inroutefilter}) {
309                 my $dxcc = $self->dxcc;
310                 $Route::filterdef->cmd($self, 'route', 'accept', "input by_dxcc $dxcc" );
311         }
312
313         # set unbuffered and no echo
314         $self->send_now('B',"0");
315         $self->send_now('E',"0");
316         $self->conn->echo(0) if $self->conn->can('echo');
317
318         # ping neighbour node stuff
319         my $ping = $user->pingint;
320         $ping = $pingint unless defined $ping;
321         $self->{pingint} = $ping;
322         $self->{nopings} = $user->nopings || $obscount;
323         $self->{pingtime} = [ ];
324         $self->{pingave} = 999;
325         $self->{metric} ||= 100;
326         $self->{lastping} = $main::systime;
327
328         # send initialisation string
329         unless ($self->{outbound}) {
330                 $self->sendinit;
331         }
332
333         $self->state('init');
334         $self->{pc50_t} = $main::systime;
335
336         # send info to all logged in thingies
337         $self->tell_login('loginn');
338
339         # run a script send the output to the debug file
340         my $script = new Script(lc $call) || new Script('node_default');
341         $script->run($self) if $script;
342
343         # set next_pc92_update time for this node sooner
344         $self->update_pc92_next($pc92_short_update_period);
345 }
346
347 #
348 # send outgoing 'challenge'
349 #
350
351 sub sendinit
352 {
353         my $self = shift;
354         $self->send(pc18());
355 }
356
357 #
358 # This is the normal pcxx despatcher
359 #
360 sub normal
361 {
362         my ($self, $line) = @_;
363
364         if ($line =~ '^<\w+\s' && $main::do_xml) {
365                 DXXml::normal($self, $line);
366                 return;
367         }
368
369         my @field = split /\^/, $line;
370         return unless @field;
371
372         pop @field if $field[-1] eq '~';
373
374 #       print join(',', @field), "\n";
375
376
377         # process PC frames, this will fail unless the frame starts PCnn
378         my ($pcno) = $field[0] =~ /^PC(\d\d)/; # just get the number
379         unless (defined $pcno && $pcno >= 10 && $pcno <= 99) {
380                 dbg("PCPROT: unknown protocol") if isdbg('chanerr');
381                 return;
382         }
383
384         # check for and dump bad protocol messages
385         my $n = check($pcno, @field);
386         if ($n) {
387                 dbg("PCPROT: bad field $n, dumped (" . parray($checklist[$pcno-10]) . ")") if isdbg('chanerr');
388                 return;
389         }
390
391         # modify the hop count here
392         if ($self != $main::me) {
393                 if (my ($hops, $trail) = $line =~ /\^H(\d+)(\^?\~?)?$/) {
394                         $trail ||= '';
395                         $hops--;
396                         return if $hops < 0;
397                         $line =~ s/\^H(\d+)(\^?\~?)?$/sprintf('^H%d%s', $hops, $trail)/e;
398                         $field[-1] = "H$hops";
399                 }
400         }
401
402         # send it out for processing
403         my $origin = $self->{call};
404         no strict 'subs';
405         my $sub = "handle_$pcno";
406
407         if ($self->can($sub)) {
408                 $self->$sub($pcno, $line, $origin, @field);
409         } else {
410                 $self->handle_default($pcno, $line, $origin, @field);
411         }
412 }
413
414 #
415 # This is called from inside the main cluster processing loop and is used
416 # for despatching commands that are doing some long processing job
417 #
418 sub process
419 {
420         my $t = time;
421         my @dxchan = DXChannel::get_all();
422         my $dxchan;
423         my $pc50s;
424
425         # send out a pc50 on EVERY channel all at once
426         if ($t >= $last_pc50 + $DXProt::pc50_interval) {
427                 $pc50s = pc50($main::me, scalar DXChannel::get_all_users);
428                 eph_dup($pc50s);
429                 $last_pc50 = $t;
430         }
431
432         foreach $dxchan (@dxchan) {
433                 next unless $dxchan->is_node;
434                 next if $dxchan->handle_xml;
435                 next if $dxchan == $main::me;
436
437                 # send the pc50
438                 $dxchan->send($pc50s) if $pc50s;
439
440                 # send a ping out on this channel
441                 if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
442                         if ($dxchan->{nopings} <= 0) {
443                                 $dxchan->disconnect;
444                         } else {
445                                 DXXml::Ping::add($main::me, $dxchan->call);
446                                 $dxchan->{nopings} -= 1;
447                                 $dxchan->{lastping} = $t;
448                                 $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
449                         }
450                 }
451
452                 # send out a PC92 config record if required
453                 if ($main::systime >= $dxchan->{next_pc92_update}) {
454                         if ($dxchan->{call} eq $main::mycall || !$dxchan->{do_pc9x}) {
455                                 $dxchan->send_pc92_update($dxchan->{call});
456                         }
457                         $dxchan->update_pc92_next($pc92_update_period);
458                 }
459         }
460
461         Investigate::process();
462         clean_pc92_find();
463
464         # every ten seconds
465         if ($t - $last10 >= 10) {
466                 # clean out ephemera
467
468                 eph_clean();
469                 import_chat();
470
471                 if ($main::systime >= $pc92_obs_timeout) {
472                         time_out_pc92_routes();
473                         $pc92_obs_timeout = $main::systime + $pc92_update_period;
474                 }
475
476                 $last10 = $t;
477         }
478
479         if ($main::systime - 3600 > $last_hour) {
480                 $last_hour = $main::systime;
481         }
482 }
483
484 #
485 # finish up a pc context
486 #
487
488 #
489 # some active measures
490 #
491
492
493 sub send_dx_spot
494 {
495         my $self = shift;
496         my $line = shift;
497         my @dxchan = DXChannel::get_all();
498         my $dxchan;
499
500         # send it if it isn't the except list and isn't isolated and still has a hop count
501         # taking into account filtering and so on
502         foreach $dxchan (@dxchan) {
503                 next if $dxchan == $main::me;
504                 next if $dxchan == $self && $self->is_node;
505                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
506         }
507 }
508
509 sub dx_spot
510 {
511         my $self = shift;
512         my $line = shift;
513         my $isolate = shift;
514         my ($filter, $hops);
515
516         if ($self->{spotsfilter}) {
517                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
518                 return unless $filter;
519         }
520         send_prot_line($self, $filter, $hops, $isolate, $line);
521 }
522
523 sub send_prot_line
524 {
525         my ($self, $filter, $hops, $isolate, $line) = @_;
526         my $routeit;
527
528
529         if ($hops) {
530                 $routeit = $line;
531                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
532         } else {
533                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
534                 return unless $routeit;
535         }
536         if ($filter) {
537                 $self->send($routeit);
538         } else {
539                 $self->send($routeit) unless $self->{isolate} || $isolate;
540         }
541 }
542
543
544 sub send_wwv_spot
545 {
546         my $self = shift;
547         my $line = shift;
548         my @dxchan = DXChannel::get_all();
549         my $dxchan;
550         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
551
552         # send it if it isn't the except list and isn't isolated and still has a hop count
553         # taking into account filtering and so on
554         foreach $dxchan (@dxchan) {
555                 next if $dxchan == $main::me;
556                 next if $dxchan == $self && $self->is_node;
557                 my $routeit;
558                 my ($filter, $hops);
559
560                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
561         }
562 }
563
564 sub wwv
565 {
566         my $self = shift;
567         my $line = shift;
568         my $isolate = shift;
569         my ($filter, $hops);
570
571         if ($self->{wwvfilter}) {
572                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
573                 return unless $filter;
574         }
575         send_prot_line($self, $filter, $hops, $isolate, $line)
576 }
577
578 sub send_wcy_spot
579 {
580         my $self = shift;
581         my $line = shift;
582         my @dxchan = DXChannel::get_all();
583         my $dxchan;
584         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
585
586         # send it if it isn't the except list and isn't isolated and still has a hop count
587         # taking into account filtering and so on
588         foreach $dxchan (@dxchan) {
589                 next if $dxchan == $main::me;
590                 next if $dxchan == $self;
591
592                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
593         }
594 }
595
596 sub wcy
597 {
598         my $self = shift;
599         my $line = shift;
600         my $isolate = shift;
601         my ($filter, $hops);
602
603         if ($self->{wcyfilter}) {
604                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
605                 return unless $filter;
606         }
607         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
608 }
609
610 # send an announce
611 sub send_announce
612 {
613         my $self = shift;
614         my $from_pc9x = shift;
615         my $line = shift;
616         my @dxchan = DXChannel::get_all();
617         my $dxchan;
618         my $target = $_[6];
619         my $to = 'To ';
620         my $text = unpad($_[2]);
621
622         if ($_[3] eq '*') {     # sysops
623                 $target = "SYSOP";
624         } elsif ($_[3] gt ' ') { # speciality list handling
625                 my ($name) = split /\./, $_[3];
626                 $target = "$name"; # put the rest in later (if bothered)
627         }
628
629         if ($_[5] eq '1') {
630                 $target = "WX";
631                 $to = '';
632         }
633         $target = "ALL" if !$target;
634
635
636         # obtain country codes etc
637         my @a = Prefix::cty_data($_[0]);
638         my @b = Prefix::cty_data($_[4]);
639         if ($self->{inannfilter}) {
640                 my ($filter, $hops) =
641                         $self->{inannfilter}->it(@_, $self->{call},
642                                                                          @a[0..2],
643                                                                          @b[0..2], $a[3], $b[3]);
644                 unless ($filter) {
645                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
646                         return;
647                 }
648         }
649
650         if (AnnTalk::dup($_[0], $_[1], $_[2])) {
651                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
652                 return;
653         }
654
655         Log('ann', $target, $_[0], $text);
656
657         # send it if it isn't the except list and isn't isolated and still has a hop count
658         # taking into account filtering and so on
659         foreach $dxchan (@dxchan) {
660                 next if $dxchan == $main::me;
661                 next if $dxchan == $self && $self->is_node;
662                 next if $from_pc9x && $dxchan->{do_pc9x};
663                 next if $target eq 'LOCAL' && $dxchan->is_node;
664                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
665                                                   @a[0..2], @b[0..2]);
666         }
667 }
668
669 my $msgid = 0;
670
671 sub nextchatmsgid
672 {
673         $msgid++;
674         $msgid = 1 if $msgid > 999;
675         return $msgid;
676 }
677
678 # send a chat line
679 sub send_chat
680 {
681         my $self = shift;
682         my $from_pc9x = shift;
683         my $line = shift;
684         my @dxchan = DXChannel::get_all();
685         my $dxchan;
686         my $target = $_[3];
687         my $text = unpad($_[2]);
688         my $ak1a_line;
689
690         # munge the group and recast the line if required
691         if ($target =~ s/\.LST$//) {
692                 $ak1a_line = $line;
693         }
694
695         # obtain country codes etc
696         my @a = Prefix::cty_data($_[0]);
697         my @b = Prefix::cty_data($_[4]);
698         if ($self->{inannfilter}) {
699                 my ($filter, $hops) =
700                         $self->{inannfilter}->it(@_, $self->{call},
701                                                                          @a[0..2],
702                                                                          @b[0..2], $a[3], $b[3]);
703                 unless ($filter) {
704                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
705                         return;
706                 }
707         }
708
709         if (AnnTalk::dup($_[0], $_[1], $_[2], $chatdupeage)) {
710                 dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
711                 return;
712         }
713
714
715         Log('chat', $target, $_[0], $text);
716
717         # send it if it isn't the except list and isn't isolated and still has a hop count
718         # taking into account filtering and so on
719         foreach $dxchan (@dxchan) {
720                 my $is_ak1a = $dxchan->is_ak1a;
721
722                 if ($dxchan->is_node) {
723                         next if $dxchan == $main::me;
724                         next if $dxchan == $self;
725                         next if $from_pc9x && $dxchan->{do_pc9x};
726                         next unless $dxchan->is_spider || $is_ak1a;
727                         next if $target eq 'LOCAL';
728                         if (!$ak1a_line && $is_ak1a) {
729                                 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
730                         }
731                 }
732
733                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
734                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
735         }
736 }
737
738 sub announce
739 {
740         my $self = shift;
741         my $line = shift;
742         my $isolate = shift;
743         my $to = shift;
744         my $target = shift;
745         my $text = shift;
746         my ($filter, $hops);
747
748         if ($self->{annfilter}) {
749                 ($filter, $hops) = $self->{annfilter}->it(@_);
750                 return unless $filter;
751         }
752         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
753 }
754
755 sub chat
756 {
757         goto &announce;
758 }
759
760
761 sub send_local_config
762 {
763         my $self = shift;
764
765         dbg('DXProt::send_local_config') if isdbg('trace');
766
767         # send our nodes
768         my $node;
769         my @nodes;
770         my @localnodes;
771         my @remotenodes;
772
773         if ($self->{isolate}) {
774                 @localnodes = ( $main::routeroot );
775                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
776         } elsif ($self->{do_pc9x}) {
777                 my $node = Route::Node::get($self->{call});
778                 $self->send_last_pc92_config($main::routeroot);
779                 $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
780         } else {
781                 # create a list of all the nodes that are not connected to this connection
782                 # and are not themselves isolated, this to make sure that isolated nodes
783                 # don't appear outside of this node
784
785                 # send locally connected nodes
786                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
787                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
788                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
789
790                 my $node;
791                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
792                 my @intcalls;
793                 for $node (@rawintcalls) {
794                         push @intcalls, $node unless grep $node eq $_, @intcalls;
795                 }
796                 my $ref = Route::Node::get($self->{call});
797                 my @rnodes = $ref->nodes;
798                 for $node (@intcalls) {
799                         push @remotenodes, Route::Node::get($node) unless grep $node eq $_, @rnodes, @remotenodes;
800                 }
801                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
802         }
803
804         # get all the users connected on the above nodes and send them out
805         unless ($self->{do_pc9x}) {
806                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
807                         if ($node) {
808                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
809                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
810                         } else {
811                                 dbg("sent a null value") if isdbg('chanerr');
812                         }
813                 }
814         }
815 }
816
817 sub gen_my_pc92_config
818 {
819         my $node = shift;
820
821         if ($node->{call} eq $main::mycall) {
822                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
823                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
824                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
825                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
826                 return pc92c($main::routeroot, @localnodes);
827         } else {
828                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
829                 return pc92c($node, @rout);
830         }
831 }
832
833 sub gen_pc92_update
834 {
835         my $self = shift;
836         my $with_pc92_nodes = shift;
837         my $node;
838         my @lines;
839         my @dxchan;
840         my @localnodes;
841
842         dbg('ROUTE: DXProt::gen_pc92_update start') if isdbg('routelow');
843
844         # send 'my' configuration for all channels
845         push @lines, gen_my_pc92_config($main::routeroot);
846
847 #       if ($with_pc92_nodes) {
848                 # send out the configuration of all the directly connected PC92 nodes with current configuration
849                 # but with the dates that the last config came in with.
850 #               @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && $_->{do_pc9x} } DXChannel::get_all_nodes();
851 #               dbg("ROUTE: pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
852 #               @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
853 #               dbg("ROUTE: pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
854 #               foreach $node (@localnodes) {
855 #                       if ($node && $node->lastid->{92}) {
856 #                               my @rout = map {my $r = Route::get($_); $r ? ($r) : ()} $node->nodes, $node->users;
857 #                               push @lines, gen_pc92_with_time($node->call, 'C', $node->lastid->{92}, @rout);
858 #                       }
859 #               }
860 #       }
861
862         # send the configuration of all the directly connected 'external' nodes that don't handle PC92
863         # out with the 'external' marker on the first node.
864 #       @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} && !$_->{do_pc9x} } DXChannel::get_all_nodes();
865 #       dbg("ROUTE: non pc92 dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
866 #       @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan;
867 #       dbg("ROUTE: non pc92 localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
868 #       foreach $node (@localnodes) {
869 #               if ($node) {
870 #                       push @lines, gen_my_pc92_config($node);
871 #               }
872 #       }
873
874         dbg('ROUTE: DXProt::gen_pc92_update end with ' . scalar @lines . ' lines') if isdbg('routelow');
875         return @lines;
876 }
877
878
879 sub send_last_pc92_config
880 {
881         my $self = shift;
882         my $node = shift;
883         if (my $l = $node->last_PC92C) {
884                 $self->send($l);
885         } else {
886                 $self->send_pc92_config($node);
887         }
888 }
889
890 sub send_pc92_config
891 {
892         my $self = shift;
893         my $node = shift;
894
895         dbg('DXProt::send_pc92_config') if isdbg('trace');
896
897         $node->last_PC92C(gen_my_pc92_config($node));
898         $self->send($node->last_PC92C);
899 }
900
901 sub send_pc92_update
902 {
903         my $call = shift;
904
905         dbg('DXProt::send_pc92_update') if isdbg('trace');
906
907         my $l = gen_my_pc92_config(Route::Node::get($call));
908         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
909 }
910
911 sub time_out_pc92_routes
912 {
913         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
914         my @rdel;
915         foreach my $n (@nodes) {
916                 my $o = $n->dec_obs;
917                 if ($o <= 0) {
918                         if (my $dxchan = DXChannel::get($n->call)) {
919                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('route');
920                                 $dxchan->disconnect;
921                                 next;
922                         }
923                         my @parents = map {Route::Node::get($_)} $n->parents;
924                         for (@parents) {
925                                 if ($_) {
926                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount") if isdbg('route');
927                                         push @rdel, $n->del($_);
928                                 }
929                         }
930                 } else {
931                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('route');
932                 }
933         }
934         for (@rdel) {
935                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
936         }
937 }
938
939 #
940 # route a message down an appropriate interface for a callsign
941 #
942 # is called route(to, pcline);
943 #
944
945 sub route
946 {
947         my ($self, $call, $line) = @_;
948
949         if (ref $self && $call eq $self->{call}) {
950                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
951                 return;
952         }
953
954         # always send it down the local interface if available
955         my $dxchan = DXChannel::get($call);
956         if ($dxchan) {
957                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
958         } else {
959                 my $cl = Route::get($call);
960                 $dxchan = $cl->dxchan if $cl;
961                 if (ref $dxchan) {
962                         if (ref $self && $dxchan eq $self) {
963                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
964                                 return;
965                         }
966                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
967                 }
968         }
969
970         # try the backstop method
971         unless ($dxchan) {
972                 my $rcall = RouteDB::get($call);
973                 if ($rcall) {
974                         if ($self && $rcall eq $self->{call}) {
975                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
976                                 return;
977                         }
978                         $dxchan = DXChannel::get($rcall);
979                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
980                 }
981         }
982
983         if ($dxchan) {
984                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
985                 if ($routeit) {
986                         $dxchan->send($routeit) unless $dxchan == $main::me;
987                 }
988         } else {
989                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
990         }
991 }
992
993 #
994 # obtain the hops from the list for this callsign and pc no
995 #
996
997 sub get_hops
998 {
999         my $pcno = shift;
1000         my $hops = $DXProt::hopcount{$pcno};
1001         $hops = $DXProt::def_hopcount if !$hops;
1002         return "H$hops";
1003 }
1004
1005 #
1006 # adjust the hop count on a per node basis using the user loadable
1007 # hop table if available or else decrement an existing one
1008 #
1009
1010 sub adjust_hops
1011 {
1012         my $self = shift;
1013         my $s = shift;
1014         my $call = $self->{call};
1015         my $hops;
1016
1017         if (($hops) = $s =~ /\^H(\d+)\^?~?$/o) {
1018                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1019                 confess "$call called adjust_hops with '$s'" unless $pcno;
1020                 my $ref = $nodehops{$call} if %nodehops;
1021                 if ($ref) {
1022                         my $newhops = $ref->{$pcno};
1023                         return "" if defined $newhops && $newhops == 0;
1024                         $newhops = $ref->{default} unless $newhops;
1025                         return "" if defined $newhops && $newhops == 0;
1026                         $newhops = $hops if !$newhops;
1027                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops;
1028                 }
1029         }
1030         return $s;
1031 }
1032
1033 #
1034 # load hop tables
1035 #
1036 sub load_hops
1037 {
1038         my $self = shift;
1039         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1040         do "$main::data/hop_table.pl";
1041         return $@ if $@;
1042         return ();
1043 }
1044
1045 sub process_rcmd
1046 {
1047         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1048         if ($tonode eq $main::mycall) {
1049                 my $ref = DXUser->get_current($fromnode);
1050                 my $cref = Route::Node::get($fromnode);
1051                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1052                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1053                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1054                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1055                                 my $oldpriv = $self->{priv};
1056                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1057                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1058                                 $self->{priv} = $oldpriv;
1059                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1060                                 delete $self->{remotecmd};
1061                         } else {
1062                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1063                         }
1064                 } else {
1065                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1066                 }
1067         } else {
1068                 my $ref = DXUser->get_current($tonode);
1069                 if ($ref && $ref->is_clx) {
1070                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1071                 } else {
1072                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1073                 }
1074         }
1075 }
1076
1077 sub process_rcmd_reply
1078 {
1079         my ($self, $tonode, $fromnode, $user, $line) = @_;
1080         if ($tonode eq $main::mycall) {
1081                 my $s = $rcmds{$fromnode};
1082                 if ($s) {
1083                         my $dxchan = DXChannel::get($s->{call});
1084                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1085                         $ref->send($line) if $ref;
1086                         delete $rcmds{$fromnode} if !$dxchan;
1087                 } else {
1088                         # send unsolicited ones to the sysop
1089                         my $dxchan = DXChannel::get($main::myalias);
1090                         $dxchan->send($line) if $dxchan;
1091                 }
1092         } else {
1093                 my $ref = DXUser->get_current($tonode);
1094                 if ($ref && $ref->is_clx) {
1095                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1096                 } else {
1097                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1098                 }
1099         }
1100 }
1101
1102 sub send_rcmd_reply
1103 {
1104         my $self = shift;
1105         my $tonode = shift;
1106         my $fromnode = shift;
1107         my $user = shift;
1108         while (@_) {
1109                 my $line = shift;
1110                 $line =~ s/\s*$//;
1111                 Log('rcmd', 'out', $fromnode, $line);
1112                 if ($self->is_clx) {
1113                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1114                 } else {
1115                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1116                 }
1117         }
1118 }
1119
1120 # add a rcmd request to the rcmd queues
1121 sub addrcmd
1122 {
1123         my ($self, $to, $cmd) = @_;
1124
1125         my $r = {};
1126         $r->{call} = $self->{call};
1127         $r->{t} = $main::systime;
1128         $r->{cmd} = $cmd;
1129         $rcmds{$to} = $r;
1130
1131         my $ref = Route::Node::get($to);
1132         my $dxchan = $ref->dxchan;
1133         if ($dxchan && $dxchan->is_clx) {
1134                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1135         } else {
1136                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1137         }
1138 }
1139
1140 sub disconnect
1141 {
1142         my $self = shift;
1143         my $pc39flag = shift;
1144         my $call = $self->call;
1145
1146         return if $self->{disconnecting}++;
1147
1148         unless ($pc39flag && $pc39flag == 1) {
1149                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1150         }
1151
1152         # get rid of any PC16/17/19
1153         eph_del_regex("^PC1[679]*$call");
1154
1155         # do routing stuff, remove me from routing table
1156         my $node = Route::Node::get($call);
1157         my @rout;
1158         if ($node) {
1159                 @rout = $node->del($main::routeroot);
1160
1161                 # and all my ephemera as well
1162                 for (@rout) {
1163                         my $c = $_->call;
1164                         eph_del_regex("^PC1[679].*$c");
1165                 }
1166         }
1167
1168         RouteDB::delete_interface($call);
1169
1170         # unbusy and stop and outgoing mail
1171         my $mref = DXMsg::get_busy($call);
1172         $mref->stop_msg($call) if $mref;
1173
1174         # broadcast to all other nodes that all the nodes connected to via me are gone
1175         unless ($pc39flag && $pc39flag == 2)  {
1176                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1177                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1178         }
1179
1180         # remove outstanding pings
1181         delete $pings{$call};
1182
1183         # I was the last node visited
1184     $self->user->node($main::mycall);
1185
1186         # send info to all logged in thingies
1187         $self->tell_login('logoutn');
1188
1189         Log('DXProt', $call . " Disconnected");
1190
1191         $self->SUPER::disconnect;
1192 }
1193
1194
1195 #
1196 # send a talk message to this thingy
1197 #
1198 sub talk
1199 {
1200         my ($self, $from, $to, $via, $line, $origin) = @_;
1201
1202         if ($self->{do_pc9x}) {
1203                 $self->send(pc93($to, $from, $via, $line));
1204         } else {
1205                 $self->send(pc10($from, $to, $via, $line, $origin));
1206         }
1207         Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1208 }
1209
1210 # send it if it isn't the except list and isn't isolated and still has a hop count
1211 # taking into account filtering and so on
1212
1213 sub send_route
1214 {
1215         my $self = shift;
1216         my $origin = shift;
1217         my $generate = shift;
1218         my $no = shift;     # the no of things to filter on
1219         my $routeit;
1220         my ($filter, $hops);
1221         my @rin;
1222
1223         for (; @_ && $no; $no--) {
1224                 my $r = shift;
1225
1226                 if (!$self->{isolate} && $self->{routefilter}) {
1227                         $filter = undef;
1228                         if ($r) {
1229                                 ($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});
1230                                 if ($filter) {
1231                                         push @rin, $r;
1232                                 } else {
1233                                         dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1234                                 }
1235                         } else {
1236                                 dbg("was sent a null value") if isdbg('chanerr');
1237                         }
1238                 } else {
1239                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1240                 }
1241         }
1242         if (@rin) {
1243                 foreach my $line (&$generate(@rin, @_)) {
1244                         if ($hops) {
1245                                 $routeit = $line;
1246                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1247                         } else {
1248                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1249                                 next unless $routeit;
1250                         }
1251
1252                         $self->send($routeit);
1253                 }
1254         }
1255 }
1256
1257 sub broadcast_route
1258 {
1259         my $self = shift;
1260         my $origin = shift;
1261         my $generate = shift;
1262         my $line = shift;
1263         my @dxchan = DXChannel::get_all_nodes();
1264         my $dxchan;
1265
1266         if ($line) {
1267                 $line =~ /\^H(\d+)\^?\~?$/;
1268                 return unless $1 > 0;
1269         }
1270         unless ($self->{isolate}) {
1271                 foreach $dxchan (@dxchan) {
1272                         next if $dxchan == $self;
1273                         next if $dxchan == $main::me;
1274                         next unless $dxchan->isa('DXProt');
1275                         next if $dxchan->{do_pc9x};
1276                         next if ($generate == \&pc16 || $generate==\&pc17) && !$dxchan->user->wantsendpc16;
1277
1278                         $dxchan->send_route($origin, $generate, @_);
1279                 }
1280         }
1281 }
1282
1283 # this is only used for next door nodes on init
1284 sub send_route_pc92
1285 {
1286         my $self = shift;
1287
1288         return unless $self->{do_pc9x};
1289
1290         my $origin = shift;
1291         my $generate = shift;
1292         my $no = shift;     # the no of things to filter on
1293         my $line;
1294
1295         $line = &$generate(@_);
1296         $self->send($line);
1297 }
1298
1299 sub broadcast_route_pc9x
1300 {
1301         my $self = shift;
1302         my $origin = shift;
1303         my $generate = shift;
1304         my $line = shift;
1305         my $no = shift;
1306         my @dxchan = DXChannel::get_all_nodes();
1307         my $dxchan;
1308
1309         if ($origin eq $main::mycall && $generate && !$line) {
1310                 $line = &$generate(@_);
1311         }
1312
1313         $line =~ /\^H(\d+)\^\~?$/;
1314         unless ($1 > 0 && $self->{isolate}) {
1315                 foreach $dxchan (@dxchan) {
1316                         next if $dxchan == $self || $dxchan == $main::me;
1317                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1318                         next unless $dxchan->{do_pc9x};
1319                         next unless $dxchan->isa('DXProt');
1320
1321                         $dxchan->send($line);
1322                 }
1323         }
1324 }
1325
1326 sub route_pc16
1327 {
1328         my $self = shift;
1329         return unless $self->user->wantpc16;
1330         my $origin = shift;
1331         my $line = shift;
1332         broadcast_route($self, $origin, \&pc16, $line, 1, @_);
1333 }
1334
1335 sub route_pc17
1336 {
1337         my $self = shift;
1338         return unless $self->user->wantpc16;
1339         my $origin = shift;
1340         my $line = shift;
1341         broadcast_route($self, $origin, \&pc17, $line, 1, @_);
1342 }
1343
1344 sub route_pc19
1345 {
1346         my $self = shift;
1347         my $origin = shift;
1348         my $line = shift;
1349         broadcast_route($self, $origin, \&pc19, $line, scalar @_, @_);
1350 }
1351
1352 sub route_pc21
1353 {
1354         my $self = shift;
1355         my $origin = shift;
1356         my $line = shift;
1357         broadcast_route($self, $origin, \&pc21, $line, scalar @_, @_);
1358 }
1359
1360 sub route_pc24
1361 {
1362         my $self = shift;
1363         my $origin = shift;
1364         my $line = shift;
1365         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1366 }
1367
1368 sub route_pc41
1369 {
1370         my $self = shift;
1371         my $origin = shift;
1372         my $line = shift;
1373         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1374 }
1375
1376 sub route_pc50
1377 {
1378         my $self = shift;
1379         my $origin = shift;
1380         my $line = shift;
1381         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1382 }
1383
1384 sub route_pc92c
1385 {
1386         my $self = shift;
1387         my $origin = shift;
1388         my $line = shift;
1389         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1390 }
1391
1392 sub route_pc92a
1393 {
1394         my $self = shift;
1395         my $origin = shift;
1396         my $line = shift;
1397         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1398 }
1399
1400 sub route_pc92d
1401 {
1402         my $self = shift;
1403         my $origin = shift;
1404         my $line = shift;
1405         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1406 }
1407
1408 sub in_filter_route
1409 {
1410         my $self = shift;
1411         my $r = shift;
1412         my ($filter, $hops) = (1, 1);
1413
1414         if ($self->{inroutefilter}) {
1415                 ($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);
1416                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1417         }
1418         return $filter;
1419 }
1420
1421 sub eph_dup
1422 {
1423         my $s = shift;
1424         my $t = shift || $eph_restime;
1425         my $r;
1426
1427         # chop the end off
1428         $s =~ s/\^H\d\d?\^?\~?$//;
1429         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1430         $eph{$s} = $main::systime + $t;
1431         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1432         return $r;
1433 }
1434
1435 sub eph_del_regex
1436 {
1437         my $regex = shift;
1438         my ($key, $val);
1439         while (($key, $val) = each %eph) {
1440                 if ($key =~ m{$regex}) {
1441                         delete $eph{$key};
1442                 }
1443         }
1444 }
1445
1446 sub eph_clean
1447 {
1448         my ($key, $val);
1449
1450         while (($key, $val) = each %eph) {
1451                 if ($main::systime >= $val) {
1452                         delete $eph{$key};
1453                 }
1454         }
1455 }
1456
1457 sub eph_list
1458 {
1459         my ($key, $val);
1460         my @out;
1461
1462         while (($key, $val) = each %eph) {
1463                 push @out, $key, $val;
1464         }
1465         return @out;
1466 }
1467
1468 sub run_cmd
1469 {
1470         goto &DXCommandmode::run_cmd;
1471 }
1472
1473
1474 # import any msgs in the chat directory
1475 # the messages are sent to the chat group which forms the
1476 # the first part of the name (eg: solar.1243.txt would be
1477 # sent to chat group SOLAR)
1478 #
1479 # Each message found is sent: one non-blank line to one chat
1480 # message. So 4 lines = 4 chat messages.
1481 #
1482 # The special name LOCAL is for local users ANN
1483 # The special name ALL is for ANN/FULL
1484 # The special name SYSOP is for ANN/SYSOP
1485 #
1486 sub import_chat
1487 {
1488         # are there any to do in this directory?
1489         return unless -d $chatimportfn;
1490         unless (opendir(DIR, $chatimportfn)) {
1491                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1492                 Log('msg', "can\'t open $chatimportfn $!");
1493                 return;
1494         }
1495
1496         my @names = readdir(DIR);
1497         closedir(DIR);
1498         my $name;
1499         foreach $name (@names) {
1500                 next if $name =~ /^\./;
1501                 my $splitit = $name =~ /^split/;
1502                 my $fn = "$chatimportfn/$name";
1503                 next unless -f $fn;
1504                 unless (open(MSG, $fn)) {
1505                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1506                         Log('msg', "can\'t open import file $fn $!");
1507                         unlink($fn);
1508                         next;
1509                 }
1510                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1511                 close(MSG);
1512                 unlink($fn);
1513
1514                 my @cat = split /\./, $name;
1515                 my $target = uc $cat[0];
1516
1517                 foreach my $text (@msg) {
1518                         next unless $text && $text !~ /^\s*#/;
1519                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP') {
1520                                 my $sysopflag = $target eq 'SYSOP' ? '*' : ' ';
1521                                 if ($target ne 'LOCAL') {
1522                                         send_announce($main::me, pc12($main::mycall, $text, '*', $sysopflag), $main::mycall, '*', $text, $sysopflag, $main::mycall, '0');
1523                                 } else {
1524                                         Log('ann', 'LOCAL', $main::mycall, $text);
1525                                         DXChannel::broadcast_list("To LOCAL de ${main::mycall}: $text\a", 'ann', undef, DXCommandmode->get_all());
1526                                 }
1527                         } else {
1528                                 my $msgid = nextchatmsgid();
1529                                 $text = "#$msgid $text";
1530                                 send_chat($main::me, pc12($main::mycall, $text, '*', $target), $main::mycall, '*', $text, $target, $main::mycall, '0');
1531                         }
1532                 }
1533         }
1534 }
1535
1536 # start a pc92 find operation
1537 sub start_pc92_find
1538 {
1539         my $dxchan = shift;
1540         my $target = shift;
1541         my $key = "$dxchan->{call}|$target";
1542         if ($pc92_find{$key}) {
1543
1544         }
1545 }
1546
1547 # function (not method) to handle pc92 find returns
1548 sub handle_pc92_find_reply
1549 {
1550         my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1551
1552         $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');
1553 }
1554
1555 sub clean_pc92_find
1556 {
1557
1558 }
1559 1;
1560 __END__