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