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