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