K records only come from $mycall
[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 = 2*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                         time_out_pc92_routes();
461                 }
462
463                 foreach $dxchan (@dxchan) {
464                         next unless $dxchan->is_node;
465                         next if $dxchan->handle_xml;
466                         next if $dxchan == $main::me;
467
468                         # send the pc50
469                         $dxchan->send($pc50s) if !$dxchan->{do_pc9x} && $pc50s;
470
471                         # send a ping out on this channel
472                         if ($dxchan->{pingint} && $t >= $dxchan->{pingint} + $dxchan->{lastping}) {
473                                 if ($dxchan->{nopings} <= 0) {
474                                         $dxchan->disconnect;
475                                 } else {
476                                         DXXml::Ping::add($main::me, $dxchan->call);
477                                         $dxchan->{nopings} -= 1;
478                                         $dxchan->{lastping} = $t;
479                                         $dxchan->{lastping} += $dxchan->{pingint} / 2 unless @{$dxchan->{pingtime}};
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                         $main::me->broadcast_pc92_keepalive($main::mycall);
514                 }
515
516                 if ($pc92_slug_changes && $main::systime >= $last_pc92_slug + $pc92_slug_changes) {
517                         my ($add, $del) = gen_pc92_changes();
518                         $main::me->route_pc92d($main::mycall, undef, $main::routeroot, @$del) if @$del;
519                         $main::me->route_pc92a($main::mycall, undef, $main::routeroot, @$add) if @$add;
520                         clear_pc92_changes();
521                 }
522         }
523
524         if ($main::systime - 3600 > $last_hour) {
525                 $last_hour = $main::systime;
526         }
527 }
528
529 #
530 # finish up a pc context
531 #
532
533 #
534 # some active measures
535 #
536
537
538 sub send_dx_spot
539 {
540         my $self = shift;
541         my $line = shift;
542         my @dxchan = DXChannel::get_all();
543         my $dxchan;
544
545         # send it if it isn't the except list and isn't isolated and still has a hop count
546         # taking into account filtering and so on
547         foreach $dxchan (@dxchan) {
548                 next if $dxchan == $main::me;
549                 next if $dxchan == $self && $self->is_node;
550                 $dxchan->dx_spot($line, $self->{isolate}, @_, $self->{call});
551         }
552 }
553
554 sub dx_spot
555 {
556         my $self = shift;
557         my $line = shift;
558         my $isolate = shift;
559         my ($filter, $hops);
560
561         if ($self->{spotsfilter}) {
562                 ($filter, $hops) = $self->{spotsfilter}->it(@_);
563                 return unless $filter;
564         }
565         send_prot_line($self, $filter, $hops, $isolate, $line);
566 }
567
568 sub send_prot_line
569 {
570         my ($self, $filter, $hops, $isolate, $line) = @_;
571         my $routeit;
572
573
574         if ($hops) {
575                 $routeit = $line;
576                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
577         } else {
578                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
579                 return unless $routeit;
580         }
581         if ($filter) {
582                 $self->send($routeit);
583         } else {
584                 $self->send($routeit) unless $self->{isolate} || $isolate;
585         }
586 }
587
588
589 sub send_wwv_spot
590 {
591         my $self = shift;
592         my $line = shift;
593         my @dxchan = DXChannel::get_all();
594         my $dxchan;
595         my @dxcc = ((Prefix::cty_data($_[6]))[0..2], (Prefix::cty_data($_[7]))[0..2]);
596
597         # send it if it isn't the except list and isn't isolated and still has a hop count
598         # taking into account filtering and so on
599         foreach $dxchan (@dxchan) {
600                 next if $dxchan == $main::me;
601                 next if $dxchan == $self && $self->is_node;
602                 my $routeit;
603                 my ($filter, $hops);
604
605                 $dxchan->wwv($line, $self->{isolate}, @_, $self->{call}, @dxcc);
606         }
607 }
608
609 sub wwv
610 {
611         my $self = shift;
612         my $line = shift;
613         my $isolate = shift;
614         my ($filter, $hops);
615
616         if ($self->{wwvfilter}) {
617                 ($filter, $hops) = $self->{wwvfilter}->it(@_[7..$#_]);
618                 return unless $filter;
619         }
620         send_prot_line($self, $filter, $hops, $isolate, $line)
621 }
622
623 sub send_wcy_spot
624 {
625         my $self = shift;
626         my $line = shift;
627         my @dxchan = DXChannel::get_all();
628         my $dxchan;
629         my @dxcc = ((Prefix::cty_data($_[10]))[0..2], (Prefix::cty_data($_[11]))[0..2]);
630
631         # send it if it isn't the except list and isn't isolated and still has a hop count
632         # taking into account filtering and so on
633         foreach $dxchan (@dxchan) {
634                 next if $dxchan == $main::me;
635                 next if $dxchan == $self;
636
637                 $dxchan->wcy($line, $self->{isolate}, @_, $self->{call}, @dxcc);
638         }
639 }
640
641 sub wcy
642 {
643         my $self = shift;
644         my $line = shift;
645         my $isolate = shift;
646         my ($filter, $hops);
647
648         if ($self->{wcyfilter}) {
649                 ($filter, $hops) = $self->{wcyfilter}->it(@_);
650                 return unless $filter;
651         }
652         send_prot_line($self, $filter, $hops, $isolate, $line) if $self->is_clx || $self->is_spider || $self->is_dxnet;
653 }
654
655 # send an announce
656 sub send_announce
657 {
658         my $self = shift;
659         my $from_pc9x = shift;
660         my $line = shift;
661         my @dxchan = DXChannel::get_all();
662         my $dxchan;
663         my $target = $_[6];
664         my $to = 'To ';
665         my $text = unpad($_[2]);
666         my $from = $_[0];
667
668         if ($_[3] eq '*') {     # sysops
669                 $target = "SYSOP";
670         } elsif ($_[3] gt ' ') { # speciality list handling
671                 my ($name) = split /\./, $_[3];
672                 $target = "$name"; # put the rest in later (if bothered)
673         }
674
675         if ($_[5] eq '1') {
676                 $target = "WX";
677                 $to = '';
678         }
679         $target = "ALL" if !$target;
680
681
682         # obtain country codes etc
683         my @a = Prefix::cty_data($from);
684         my @b = Prefix::cty_data($_[4]);
685         if ($self->{inannfilter}) {
686                 my ($filter, $hops) =
687                         $self->{inannfilter}->it(@_, $self->{call},
688                                                                          @a[0..2],
689                                                                          @b[0..2], $a[3], $b[3]);
690                 unless ($filter) {
691                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
692                         return;
693                 }
694         }
695
696         # the sysop ('*') thing is an attempt to minimise the damage caused by non-updated PC93 generators
697         if (AnnTalk::dup($from, $target, $_[2]) || ($_[3] eq '*' && AnnTalk::dup($from, 'ALL', $_[2]))) {
698                 my $dxchan = DXChannel::get($from);
699                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
700                         if ($dxchan->priv < 5) {
701                                 $dxchan->send($dxchan->msg('dup'));
702                                 return;
703                         }
704                 } else {
705                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
706                         return;
707                 }
708         }
709
710         Log('ann', $target, $from, $text);
711
712         # send it if it isn't the except list and isn't isolated and still has a hop count
713         # taking into account filtering and so on
714         foreach $dxchan (@dxchan) {
715                 next if $dxchan == $main::me;
716                 next if $dxchan == $self && $self->is_node;
717                 next if $from_pc9x && $dxchan->{do_pc9x};
718                 next if $target eq 'LOCAL' && $dxchan->is_node;
719                 $dxchan->announce($line, $self->{isolate}, $to, $target, $text, @_, $self->{call},
720                                                   @a[0..2], @b[0..2]);
721         }
722 }
723
724 my $msgid = int rand(1000);
725
726 sub nextchatmsgid
727 {
728         $msgid++;
729         $msgid = 1 if $msgid > 999;
730         return $msgid;
731 }
732
733 # send a chat line
734 sub send_chat
735 {
736         my $self = shift;
737         my $from_pc9x = shift;
738         my $line = shift;
739         my @dxchan = DXChannel::get_all();
740         my $dxchan;
741         my $target = $_[3];
742         my $text = unpad($_[2]);
743         my $ak1a_line;
744         my $from = $_[0];
745
746         # munge the group and recast the line if required
747         if ($target =~ s/\.LST$//) {
748                 $ak1a_line = $line;
749         }
750
751         # obtain country codes etc
752         my @a = Prefix::cty_data($from);
753         my @b = Prefix::cty_data($_[4]);
754         if ($self->{inannfilter}) {
755                 my ($filter, $hops) =
756                         $self->{inannfilter}->it(@_, $self->{call},
757                                                                          @a[0..2],
758                                                                          @b[0..2], $a[3], $b[3]);
759                 unless ($filter) {
760                         dbg("PCPROT: Rejected by input announce filter") if isdbg('chanerr');
761                         return;
762                 }
763         }
764
765         if (AnnTalk::dup($from, $target, $_[2], $main::systime + $chatdupeage)) {
766                 my $dxchan = DXChannel::get($from);
767                 if ($self == $main::me && $dxchan && $dxchan->is_user) {
768                         if ($dxchan->priv < 5) {
769                                 $dxchan->send($dxchan->msg('dup'));
770                                 return;
771                         }
772                 } else {
773                         dbg("PCPROT: Duplicate Announce ignored") if isdbg('chanerr');
774                         return;
775                 }
776         }
777
778
779         Log('chat', $target, $from, $text);
780
781         # send it if it isn't the except list and isn't isolated and still has a hop count
782         # taking into account filtering and so on
783         foreach $dxchan (@dxchan) {
784                 my $is_ak1a = $dxchan->is_ak1a;
785
786                 if ($dxchan->is_node) {
787                         next if $dxchan == $main::me;
788                         next if $dxchan == $self;
789                         next if $from_pc9x && $dxchan->{do_pc9x};
790                         next unless $dxchan->is_spider || $is_ak1a;
791                         next if $target eq 'LOCAL';
792                         if (!$ak1a_line && $is_ak1a) {
793                                 $ak1a_line = pc12($_[0], $text, $_[1], "$target.LST");
794                         }
795                 }
796
797                 $dxchan->chat($is_ak1a ? $ak1a_line : $line, $self->{isolate}, $target, $_[1],
798                                           $text, @_, $self->{call}, @a[0..2], @b[0..2]);
799         }
800 }
801
802 sub announce
803 {
804         my $self = shift;
805         my $line = shift;
806         my $isolate = shift;
807         my $to = shift;
808         my $target = shift;
809         my $text = shift;
810         my ($filter, $hops);
811
812         if ($self->{annfilter}) {
813                 ($filter, $hops) = $self->{annfilter}->it(@_);
814                 return unless $filter;
815         }
816         send_prot_line($self, $filter, $hops, $isolate, $line) unless $_[1] eq $main::mycall;
817 }
818
819 sub chat
820 {
821         my $self = shift;
822         my $line = shift;
823         my $isolate = shift;
824         my $to = shift;
825         my $target = shift;
826         my $text = shift;
827         my ($filter, $hops);
828
829         if ($self->{annfilter}) {
830                 ($filter, $hops) = $self->{annfilter}->it(@_);
831                 return unless $filter;
832         }
833         if (($self->is_spider || $self->is_ak1a) && $_[1] ne $main::mycall) {
834                 send_prot_line($self, $filter, $hops, $isolate, $line);
835         }
836 }
837
838
839 sub send_local_config
840 {
841         my $self = shift;
842
843         dbg('DXProt::send_local_config') if isdbg('trace');
844
845         # send our nodes
846         my $node;
847         my @nodes;
848         my @localnodes;
849         my @remotenodes;
850
851         if ($self->{isolate}) {
852                 dbg("send_local_config: isolated");
853                 @localnodes = ( $main::routeroot );
854                 $self->send_route($main::mycall, \&pc19, 1, $main::routeroot);
855         } elsif ($self->{do_pc9x}) {
856                 dbg("send_local_config: doing pc9x");
857                 my $node = Route::Node::get($self->{call});
858                 $self->send_last_pc92_config($main::routeroot);
859                 $self->send(pc92a($main::routeroot, $node)) unless $main::routeroot->last_PC92C =~ /$self->{call}/;
860         } else {
861                 # create a list of all the nodes that are not connected to this connection
862                 # and are not themselves isolated, this to make sure that isolated nodes
863                 # don't appear outside of this node
864
865                 dbg("send_local_config: traditional");
866
867                 # send locally connected nodes
868                 my @dxchan = grep { $_->call ne $main::mycall && $_ != $self && !$_->{isolate} } DXChannel::get_all_nodes();
869                 @localnodes = map { my $r = Route::Node::get($_->{call}); $r ? $r : () } @dxchan if @dxchan;
870                 $self->send_route($main::mycall, \&pc19, scalar(@localnodes)+1, $main::routeroot, @localnodes);
871
872                 my $node;
873                 my @rawintcalls = map { $_->nodes } @localnodes if @localnodes;
874                 my @intcalls;
875                 foreach $node (@rawintcalls) {
876                         push @intcalls, $node if grep $_ && $node != $_, @intcalls;
877                 }
878                 my $ref = Route::Node::get($self->{call});
879                 my @rnodes = $ref->nodes;
880                 foreach $node (@intcalls) {
881                         push @remotenodes, Route::Node::get($node) if grep $_ && $node != $_, @rnodes, @remotenodes;
882                 }
883                 $self->send_route($main::mycall, \&pc19, scalar(@remotenodes), @remotenodes);
884         }
885
886         # get all the users connected on the above nodes and send them out
887         unless ($self->{do_pc9x}) {
888                 foreach $node ($main::routeroot, @localnodes, @remotenodes) {
889                         if ($node) {
890                                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
891                                 $self->send_route($main::mycall, \&pc16, 1, $node, @rout) if @rout && $self->user->wantsendpc16;
892                         } else {
893                                 dbg("sent a null value") if isdbg('chanerr');
894                         }
895                 }
896         }
897 }
898
899 sub gen_my_pc92_config
900 {
901         my $node = shift;
902
903         if ($node->{call} eq $main::mycall) {
904                 clear_pc92_changes();           # remove any slugged data, we are generating it as now
905                 my @dxchan = grep { $_->call ne $main::mycall && !$_->{isolate} } DXChannel::get_all();
906                 dbg("ROUTE: all dxchan: " . join(',', map{$_->{call}} @dxchan)) if isdbg('routelow');
907                 my @localnodes = map { my $r = Route::get($_->{call}); $r ? $r : () } @dxchan;
908                 dbg("ROUTE: localnodes: " . join(',', map{$_->{call}} @localnodes)) if isdbg('routelow');
909                 return pc92c($node, @localnodes);
910         } else {
911                 my @rout = map {my $r = Route::User::get($_); $r ? ($r) : ()} $node->users;
912                 return pc92c($node, @rout);
913         }
914 }
915
916 sub send_last_pc92_config
917 {
918         my $self = shift;
919         my $node = shift;
920         if (my $l = $node->last_PC92C) {
921                 $self->send($l);
922         } else {
923                 $self->send_pc92_config($node);
924         }
925 }
926
927 sub send_pc92_config
928 {
929         my $self = shift;
930         my $node = shift;
931
932         dbg('DXProt::send_pc92_config') if isdbg('trace');
933
934         $node->last_PC92C(gen_my_pc92_config($node));
935         $self->send($node->last_PC92C);
936 }
937
938 sub broadcast_pc92_update
939 {
940         my $self = shift;
941         my $call = shift;
942
943         dbg("ROUTE: broadcast_pc92_update $call") if isdbg('obscount');
944
945         my $nref = Route::Node::get($call);
946         unless ($nref) {
947                 dbg("ERROR: broadcast_pc92_update - Route::Node $call disappeared");
948                 return;
949         }
950         my $l = $nref->last_PC92C(gen_my_pc92_config($nref));
951         $nref->lastid(last_pc9x_id());
952         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
953         $self->update_pc92_next;
954 }
955
956 sub broadcast_pc92_keepalive
957 {
958         my $self = shift;
959         my $call = shift;
960
961         dbg("ROUTE: broadcast_pc92_keepalive $call") if isdbg('obscount');
962
963         my $nref = Route::Node::get($call);
964         unless ($nref) {
965                 dbg("ERROR: broadcast_pc92_keepalive - Route::Node $call disappeared");
966                 return;
967         }
968         my $l = pc92k($nref);
969         $nref->lastid(last_pc9x_id());
970         $main::me->broadcast_route_pc9x($main::mycall, undef, $l, 0);
971         $self->update_pc92_keepalive;
972 }
973
974 sub time_out_pc92_routes
975 {
976         my @nodes = grep {$_->call ne $main::mycall && ($_->do_pc9x || $_->via_pc92)} Route::Node::get_all();
977         my @rdel;
978         foreach my $n (@nodes) {
979                 my $o = $n->dec_obs;
980                 if ($o <= 0) {
981                         if (my $dxchan = DXChannel::get($n->call)) {
982                                 dbg("ROUTE: disconnecting local pc92 $dxchan->{call} on obscount") if isdbg('obscount');
983                                 $dxchan->disconnect;
984                                 next;
985                         }
986                         my @parents = map {Route::Node::get($_)} $n->parents;
987                         for (@parents) {
988                                 if ($_) {
989                                         dbg("ROUTE: deleting pc92 $_->{call} from $n->{call} on obscount")  if isdbg('obscount');
990                                         push @rdel, $n->del($_);
991                                 }
992                         }
993                 } else {
994                         dbg("ROUTE: obscount on $n->{call} now $o") if isdbg('obscount');
995                 }
996         }
997         for (@rdel) {
998                 $main::me->route_pc21($main::mycall, undef, $_) if $_;
999         }
1000 }
1001
1002 #
1003 # route a message down an appropriate interface for a callsign
1004 #
1005 # is called route(to, pcline);
1006 #
1007
1008 sub route
1009 {
1010         my ($self, $call, $line) = @_;
1011
1012         if (ref $self && $call eq $self->{call}) {
1013                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1014                 return;
1015         }
1016
1017         # always send it down the local interface if available
1018         my $dxchan = DXChannel::get($call);
1019         if ($dxchan) {
1020                 dbg("route: $call -> $dxchan->{call} direct" ) if isdbg('route');
1021         } else {
1022                 my $cl = Route::get($call);
1023                 $dxchan = $cl->dxchan if $cl;
1024                 if (ref $dxchan) {
1025                         if (ref $self && $dxchan eq $self) {
1026                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1027                                 return;
1028                         }
1029                         dbg("route: $call -> $dxchan->{call} using normal route" ) if isdbg('route');
1030                 }
1031         }
1032
1033         # try the backstop method
1034         unless ($dxchan) {
1035                 my $rcall = RouteDB::get($call);
1036                 if ($rcall) {
1037                         if ($self && $rcall eq $self->{call}) {
1038                                 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
1039                                 return;
1040                         }
1041                         $dxchan = DXChannel::get($rcall);
1042                         dbg("route: $call -> $rcall using RouteDB" ) if isdbg('route') && $dxchan;
1043                 }
1044         }
1045
1046         if ($dxchan) {
1047                 my $routeit = adjust_hops($dxchan, $line);   # adjust its hop count by node name
1048                 if ($routeit) {
1049                         $dxchan->send($routeit) unless $dxchan == $main::me;
1050                 }
1051         } else {
1052                 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
1053         }
1054 }
1055
1056 #
1057 # obtain the hops from the list for this callsign and pc no
1058 #
1059
1060 sub get_hops
1061 {
1062         my $pcno = shift;
1063         my $hops = $DXProt::hopcount{$pcno};
1064         $hops = $DXProt::def_hopcount if !$hops;
1065         return "H$hops";
1066 }
1067
1068 #
1069 # adjust the hop count on a per node basis using the user loadable
1070 # hop table if available or else decrement an existing one
1071 #
1072
1073 sub adjust_hops
1074 {
1075         my $self = shift;
1076         my $s = shift;
1077         my $call = $self->{call};
1078         my $hops;
1079
1080         if (($hops) = $s =~ /\^H([-\d]+)\^?~?$/o) {
1081                 my ($pcno) = $s =~ /^PC(\d\d)/o;
1082                 confess "$call called adjust_hops with '$s'" unless $pcno;
1083                 my $ref = $nodehops{$call} if %nodehops;
1084                 if ($ref) {
1085                         my $newhops = $ref->{$pcno};
1086                         return "" if defined $newhops && $newhops == 0;
1087                         $newhops = $ref->{default} unless $newhops;
1088                         return "" if defined $newhops && $newhops == 0;
1089                         $newhops = $hops unless $newhops;
1090                         return "" unless $newhops > 0;
1091                         $s =~ s/\^H(\d+)(\^~?)$/\^H$newhops$2/ if $newhops != $hops;
1092                 } else {
1093                         return "" unless $hops > 0;
1094                 }
1095         }
1096         return $s;
1097 }
1098
1099 #
1100 # load hop tables
1101 #
1102 sub load_hops
1103 {
1104         my $self = shift;
1105         return $self->msg('lh1') unless -e "$main::data/hop_table.pl";
1106         do "$main::data/hop_table.pl";
1107         return $@ if $@;
1108         return ();
1109 }
1110
1111 sub process_rcmd
1112 {
1113         my ($self, $tonode, $fromnode, $user, $cmd) = @_;
1114         if ($tonode eq $main::mycall) {
1115                 my $ref = DXUser->get_current($fromnode);
1116                 my $cref = Route::Node::get($fromnode);
1117                 Log('rcmd', 'in', $ref->{priv}, $fromnode, $cmd);
1118                 if ($cmd !~ /^\s*rcmd/i && $cref && $ref && $cref->call eq $ref->homenode) { # not allowed to relay RCMDS!
1119                         if ($ref->{priv}) {             # you have to have SOME privilege, the commands have further filtering
1120                                 $self->{remotecmd} = 1; # for the benefit of any command that needs to know
1121                                 my $oldpriv = $self->{priv};
1122                                 $self->{priv} = $ref->{priv}; # assume the user's privilege level
1123                                 my @in = (DXCommandmode::run_cmd($self, $cmd));
1124                                 $self->{priv} = $oldpriv;
1125                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, @in);
1126                                 delete $self->{remotecmd};
1127                         } else {
1128                                 $self->send_rcmd_reply($main::mycall, $fromnode, $user, "sorry...!");
1129                         }
1130                 } else {
1131                         $self->send_rcmd_reply($main::mycall, $fromnode, $user, "your attempt is logged, Tut tut tut...!");
1132                 }
1133         } else {
1134                 my $ref = DXUser->get_current($tonode);
1135                 if ($ref && $ref->is_clx) {
1136                         $self->route($tonode, pc84($fromnode, $tonode, $user, $cmd));
1137                 } else {
1138                         $self->route($tonode, pc34($fromnode, $tonode, $cmd));
1139                 }
1140         }
1141 }
1142
1143 sub process_rcmd_reply
1144 {
1145         my ($self, $tonode, $fromnode, $user, $line) = @_;
1146         if ($tonode eq $main::mycall) {
1147                 my $s = $rcmds{$fromnode};
1148                 if ($s) {
1149                         my $dxchan = DXChannel::get($s->{call});
1150                         my $ref = $user eq $tonode ? $dxchan : (DXChannel::get($user) || $dxchan);
1151                         $ref->send($line) if $ref;
1152                         delete $rcmds{$fromnode} if !$dxchan;
1153                 } else {
1154                         # send unsolicited ones to the sysop
1155                         my $dxchan = DXChannel::get($main::myalias);
1156                         $dxchan->send($line) if $dxchan;
1157                 }
1158         } else {
1159                 my $ref = DXUser->get_current($tonode);
1160                 if ($ref && $ref->is_clx) {
1161                         $self->route($tonode, pc85($fromnode, $tonode, $user, $line));
1162                 } else {
1163                         $self->route($tonode, pc35($fromnode, $tonode, $line));
1164                 }
1165         }
1166 }
1167
1168 sub send_rcmd_reply
1169 {
1170         my $self = shift;
1171         my $tonode = shift;
1172         my $fromnode = shift;
1173         my $user = shift;
1174         while (@_) {
1175                 my $line = shift;
1176                 $line =~ s/\s*$//;
1177                 Log('rcmd', 'out', $fromnode, $line);
1178                 if ($self->is_clx) {
1179                         $self->send(pc85($main::mycall, $fromnode, $user, "$main::mycall:$line"));
1180                 } else {
1181                         $self->send(pc35($main::mycall, $fromnode, "$main::mycall:$line"));
1182                 }
1183         }
1184 }
1185
1186 # add a rcmd request to the rcmd queues
1187 sub addrcmd
1188 {
1189         my ($self, $to, $cmd) = @_;
1190
1191         my $r = {};
1192         $r->{call} = $self->{call};
1193         $r->{t} = $main::systime;
1194         $r->{cmd} = $cmd;
1195         $rcmds{$to} = $r;
1196
1197         my $ref = Route::Node::get($to);
1198         my $dxchan = $ref->dxchan;
1199         if ($dxchan && $dxchan->is_clx) {
1200                 route(undef, $to, pc84($main::mycall, $to, $self->{call}, $cmd));
1201         } else {
1202                 route(undef, $to, pc34($main::mycall, $to, $cmd));
1203         }
1204 }
1205
1206 sub disconnect
1207 {
1208         my $self = shift;
1209         my $pc39flag = shift;
1210         my $call = $self->call;
1211
1212         return if $self->{disconnecting}++;
1213
1214         unless ($pc39flag && $pc39flag == 1) {
1215                 $self->send_now("D", DXProt::pc39($main::mycall, $self->msg('disc1', "System Op")));
1216         }
1217
1218         # get rid of any PC16/17/19
1219         eph_del_regex("^PC1[679]*$call");
1220
1221         # do routing stuff, remove me from routing table
1222         my $node = Route::Node::get($call);
1223
1224         RouteDB::delete_interface($call);
1225
1226         # unbusy and stop and outgoing mail
1227         my $mref = DXMsg::get_busy($call);
1228         $mref->stop_msg($call) if $mref;
1229
1230         # remove outstanding pings
1231         delete $pings{$call};
1232
1233         # I was the last node visited
1234     $self->user->node($main::mycall);
1235
1236         # send info to all logged in thingies
1237         $self->tell_login('logoutn');
1238
1239         Log('DXProt', $call . " Disconnected");
1240
1241         $self->SUPER::disconnect;
1242
1243         # here we determine what needs to go out of the routing table
1244         my @rout;
1245         if ($node && $pc39flag != 2) {
1246                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1247
1248                 @rout = $node->del($main::routeroot);
1249
1250                 dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1251
1252                 # now we need to see what can't be routed anymore and came
1253                 # in via this node (probably).
1254                 my $n = 0;
1255                 while ($n != @rout) {
1256                         $n = @rout;
1257                         for (Route::Node::get_all()) {
1258                                 unless ($_->dxchan) {
1259                                         push @rout, $_->delete;
1260                                 }
1261                         }
1262                         dbg('@rout = ' . join(',', sort map {$_->call} @rout)) if isdbg('routedisc');
1263                 }
1264
1265                 dbg('%Route::Node::List = ' . join(',', sort keys %Route::Node::list)) if isdbg('routedisc');
1266
1267                 # and all my ephemera as well
1268                 for (@rout) {
1269                         my $c = $_->call;
1270                         eph_del_regex("^PC1[679].*$c");
1271                 }
1272         }
1273
1274         # broadcast to all other nodes that all the nodes connected to via me are gone
1275         unless ($pc39flag && $pc39flag == 2)  {
1276                 $self->route_pc21($main::mycall, undef, @rout) if @rout;
1277                 $self->route_pc92d($main::mycall, undef, $main::routeroot, $node) if $node;
1278         }
1279 }
1280
1281
1282 #
1283 # send a talk message to this thingy
1284 #
1285 sub talk
1286 {
1287         my ($self, $from, $to, $via, $line, $origin) = @_;
1288
1289         if ($self->{do_pc9x}) {
1290                 $self->send(pc93($to, $from, $via, $line));
1291         } else {
1292                 $self->send(pc10($from, $to, $via, $line, $origin));
1293         }
1294         Log('talk', $to, $from, '>' . ($via || $origin || $self->call), $line) unless $origin && $origin ne $main::mycall;
1295 }
1296
1297 # send it if it isn't the except list and isn't isolated and still has a hop count
1298 # taking into account filtering and so on
1299
1300 sub send_route
1301 {
1302         my $self = shift;
1303         my $origin = shift;
1304         my $generate = shift;
1305         my $no = shift;     # the no of things to filter on
1306         my $routeit;
1307         my ($filter, $hops);
1308         my @rin;
1309
1310         for (; @_ && $no; $no--) {
1311                 my $r = shift;
1312
1313                 # don't send messages with $self's call in back to them
1314                 if ($r->call eq $self->{call}) {
1315                         dbg("PCPROT: trying to send $self->{call} back itself") if isdbg('chanerr');
1316                         next;
1317                 }
1318
1319                 if (!$self->{isolate} && $self->{routefilter}) {
1320                         $filter = undef;
1321                         if ($r) {
1322                                 ($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});
1323                                 if ($filter) {
1324                                         push @rin, $r;
1325                                 } else {
1326                                         dbg("PCPROT: $self->{call}/" . $r->call . " rejected by output filter") if isdbg('filter');
1327                                 }
1328                         } else {
1329                                 dbg("was sent a null value") if isdbg('chanerr');
1330                         }
1331                 } else {
1332                         push @rin, $r unless $self->{isolate} && $r->call ne $main::mycall;
1333                 }
1334         }
1335         if (@rin) {
1336                 foreach my $line (&$generate(@rin, @_)) {
1337                         if ($hops) {
1338                                 $routeit = $line;
1339                                 $routeit =~ s/\^H\d+\^\~$/\^H$hops\^\~/;
1340                         } else {
1341                                 $routeit = adjust_hops($self, $line);  # adjust its hop count by node name
1342                                 next unless $routeit;
1343                         }
1344
1345                         $self->send($routeit);
1346                 }
1347         }
1348 }
1349
1350 # broadcast everywhere
1351 sub broadcast_route
1352 {
1353         my $self = shift;
1354         my $origin = shift;
1355         my $generate = shift;
1356         my $line = shift;
1357         my @dxchan = DXChannel::get_all_nodes();
1358         my $dxchan;
1359
1360         if ($line) {
1361                 $line =~ /\^H(\d+)\^?\~?$/;
1362                 return unless $1 > 0;
1363         }
1364         unless ($self->{isolate}) {
1365                 foreach $dxchan (@dxchan) {
1366                         next if $dxchan == $self || $dxchan == $main::me;
1367                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1368                         next unless $dxchan->isa('DXProt');
1369
1370                         $dxchan->send_route($origin, $generate, @_);
1371                 }
1372         }
1373 }
1374
1375 # broadcast to non-pc9x nodes
1376 sub broadcast_route_nopc9x
1377 {
1378         my $self = shift;
1379         my $origin = shift;
1380         my $generate = shift;
1381         my $line = shift;
1382         my @dxchan = DXChannel::get_all_nodes();
1383         my $dxchan;
1384
1385         if ($line) {
1386                 $line =~ /\^H(\d+)\^?\~?$/;
1387                 return unless $1 > 0;
1388         }
1389         unless ($self->{isolate}) {
1390                 foreach $dxchan (@dxchan) {
1391                         next if $dxchan == $self || $dxchan == $main::me;
1392                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1393                         next unless $dxchan->isa('DXProt');
1394                         next if $dxchan->{do_pc9x};
1395                         if ($generate == \&pc16 || $generate==\&pc17) {
1396                                 next unless $dxchan->user->wantsendpc16;
1397                         }
1398                         $dxchan->send_route($origin, $generate, @_);
1399                 }
1400         }
1401 }
1402
1403 # this is only used for next door nodes on init
1404 sub send_route_pc92
1405 {
1406         my $self = shift;
1407
1408         return unless $self->{do_pc9x};
1409
1410         my $origin = shift;
1411         my $generate = shift;
1412         my $no = shift;     # the no of things to filter on
1413         my $line;
1414
1415         $line = &$generate(@_);
1416         $self->send($line);
1417 }
1418
1419 # broadcast only to pc9x nodes
1420 sub broadcast_route_pc9x
1421 {
1422         my $self = shift;
1423         my $origin = shift;
1424         my $generate = shift;
1425         my $line = shift;
1426         my $no = shift;
1427         my @dxchan = DXChannel::get_all_nodes();
1428         my $dxchan;
1429
1430         if ($origin eq $main::mycall && $generate && !$line) {
1431                 $line = &$generate(@_);
1432         }
1433
1434         $line =~ /\^H(\d+)\^\~?$/;
1435         unless ($1 > 0 && $self->{isolate}) {
1436                 foreach $dxchan (@dxchan) {
1437                         next if $dxchan == $self || $dxchan == $main::me;
1438                         next if $origin eq $dxchan->{call};     # don't route some from this call back again.
1439                         next unless $dxchan->isa('DXProt');
1440                         next unless $dxchan->{do_pc9x};
1441
1442                         $dxchan->send($line);
1443                 }
1444         }
1445 }
1446
1447 sub route_pc16
1448 {
1449         my $self = shift;
1450         return unless $self->user->wantpc16;
1451         my $origin = shift;
1452         my $line = shift;
1453         broadcast_route_nopc9x($self, $origin, \&pc16, $line, 1, @_);
1454 }
1455
1456 sub route_pc17
1457 {
1458         my $self = shift;
1459         return unless $self->user->wantpc16;
1460         my $origin = shift;
1461         my $line = shift;
1462         broadcast_route_nopc9x($self, $origin, \&pc17, $line, 1, @_);
1463 }
1464
1465 sub route_pc19
1466 {
1467         my $self = shift;
1468         my $origin = shift;
1469         my $line = shift;
1470         broadcast_route_nopc9x($self, $origin, \&pc19, $line, scalar @_, @_);
1471 }
1472
1473 sub route_pc21
1474 {
1475         my $self = shift;
1476         my $origin = shift;
1477         my $line = shift;
1478         broadcast_route_nopc9x($self, $origin, \&pc21, $line, scalar @_, @_);
1479 }
1480
1481 sub route_pc24
1482 {
1483         my $self = shift;
1484         my $origin = shift;
1485         my $line = shift;
1486         broadcast_route($self, $origin, \&pc24, $line, 1, @_);
1487 }
1488
1489 sub route_pc41
1490 {
1491         my $self = shift;
1492         my $origin = shift;
1493         my $line = shift;
1494         broadcast_route($self, $origin, \&pc41, $line, 1, @_);
1495 }
1496
1497 # this is probably obsolete now
1498 sub route_pc50
1499 {
1500         my $self = shift;
1501         my $origin = shift;
1502         my $line = shift;
1503
1504         broadcast_route($self, $origin, \&pc50, $line, 1, @_);
1505 }
1506
1507 sub route_pc92c
1508 {
1509         my $self = shift;
1510         my $origin = shift;
1511         my $line = shift;
1512         broadcast_route_pc9x($self, $origin, \&pc92c, $line, 1, @_);
1513 }
1514
1515 sub route_pc92a
1516 {
1517         my $self = shift;
1518         my $origin = shift;
1519         my $line = shift;
1520         broadcast_route_pc9x($self, $origin, \&pc92a, $line, 1, @_);
1521 }
1522
1523 sub route_pc92d
1524 {
1525         my $self = shift;
1526         my $origin = shift;
1527         my $line = shift;
1528         broadcast_route_pc9x($self, $origin, \&pc92d, $line, 1, @_);
1529 }
1530
1531 sub in_filter_route
1532 {
1533         my $self = shift;
1534         my $r = shift;
1535         my ($filter, $hops) = (1, 1);
1536
1537         if ($self->{inroutefilter}) {
1538                 ($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);
1539                 dbg("PCPROT: $self->{call}/" . $r->call . ' rejected by in_filter_route') if !$filter && isdbg('chanerr');
1540         }
1541         return $filter;
1542 }
1543
1544 sub eph_dup
1545 {
1546         my $s = shift;
1547         my $t = shift || $eph_restime;
1548         my $r;
1549
1550         # chop the end off
1551         $s =~ s/\^H\d\d?\^?\~?$//;
1552         $r = 1 if exists $eph{$s};    # pump up the dup if it keeps circulating
1553         $eph{$s} = $main::systime + $t;
1554         dbg("PCPROT: emphemeral duplicate") if $r && isdbg('chanerr');
1555         return $r;
1556 }
1557
1558 sub eph_del_regex
1559 {
1560         my $regex = shift;
1561         my ($key, $val);
1562         while (($key, $val) = each %eph) {
1563                 if ($key =~ m{$regex}) {
1564                         delete $eph{$key};
1565                 }
1566         }
1567 }
1568
1569 sub eph_clean
1570 {
1571         my ($key, $val);
1572
1573         while (($key, $val) = each %eph) {
1574                 if ($main::systime >= $val) {
1575                         delete $eph{$key};
1576                 }
1577         }
1578 }
1579
1580 sub eph_list
1581 {
1582         my ($key, $val);
1583         my @out;
1584
1585         while (($key, $val) = each %eph) {
1586                 push @out, $key, $val;
1587         }
1588         return @out;
1589 }
1590
1591 sub run_cmd
1592 {
1593         goto &DXCommandmode::run_cmd;
1594 }
1595
1596
1597 # import any msgs in the chat directory
1598 # the messages are sent to the chat group which forms the
1599 # the first part of the name (eg: solar.1243.txt would be
1600 # sent to chat group SOLAR)
1601 #
1602 # Each message found is sent: one non-blank line to one chat
1603 # message. So 4 lines = 4 chat messages.
1604 #
1605 # The special name LOCAL is for local users ANN
1606 # The special name ALL is for ANN/FULL
1607 # The special name SYSOP is for ANN/SYSOP
1608 #
1609 sub import_chat
1610 {
1611         # are there any to do in this directory?
1612         return unless -d $chatimportfn;
1613         unless (opendir(DIR, $chatimportfn)) {
1614                 dbg("can\'t open $chatimportfn $!") if isdbg('msg');
1615                 Log('msg', "can\'t open $chatimportfn $!");
1616                 return;
1617         }
1618
1619         my @names = readdir(DIR);
1620         closedir(DIR);
1621         my $name;
1622         foreach $name (@names) {
1623                 next if $name =~ /^\./;
1624                 my $splitit = $name =~ /^split/;
1625                 my $fn = "$chatimportfn/$name";
1626                 next unless -f $fn;
1627                 unless (open(MSG, $fn)) {
1628                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1629                         Log('msg', "can\'t open import file $fn $!");
1630                         unlink($fn);
1631                         next;
1632                 }
1633                 my @msg = map { s/\r?\n$//; $_ } <MSG>;
1634                 close(MSG);
1635                 unlink($fn);
1636
1637                 my @cat = split /\./, $name;
1638                 my $target = uc $cat[0];
1639
1640                 foreach my $text (@msg) {
1641                         next unless $text && $text !~ /^\s*#/;
1642                         if ($target eq 'ALL' || $target eq 'LOCAL' || $target eq 'SYSOP' || $target eq 'WX') {
1643                                 my $sysop = uc $target eq 'SYSOP' ? '*' : ' ';
1644                                 my $wx = uc $target eq 'WX' ? '1' : '0';
1645                                 my $via = $target;
1646                                 $via = '*' if $target eq 'ALL' || $target eq 'SYSOP';
1647                                 Log('ann', $target, $main::mycall, $text);
1648                                 $main::me->normal(DXProt::pc93($target, $main::mycall, $via, $text));
1649                         } else {
1650                                 DXCommandmode::send_chats($main::me, $target, $text);
1651                         }
1652                 }
1653         }
1654 }
1655
1656 # start a pc92 find operation
1657 sub start_pc92_find
1658 {
1659         my $dxchan = shift;
1660         my $target = shift;
1661         my $key = "$dxchan->{call}|$target";
1662         if ($pc92_find{$key}) {
1663
1664         }
1665 }
1666
1667 # function (not method) to handle pc92 find returns
1668 sub handle_pc92_find_reply
1669 {
1670         my ($dxchan, $node, $from, $target, $flag, $ms) = @_;
1671
1672         $dxchan->print_pc92_find_reply($node, $target, $flag, $ms) if $dxchan->can('print_pc92_find_return');
1673 }
1674
1675 sub clean_pc92_find
1676 {
1677
1678 }
1679 1;
1680 __END__