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