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