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