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