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