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